2022-05-18 18:04:58 +00:00
|
|
|
#![allow(clippy::type_complexity)]
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use bevy::{
|
|
|
|
asset::{AssetLoader, LoadContext, LoadedAsset},
|
|
|
|
prelude::*,
|
|
|
|
reflect::TypeUuid,
|
|
|
|
transform::TransformSystem,
|
|
|
|
utils::BoxedFuture,
|
|
|
|
};
|
|
|
|
pub use synthizer as syz;
|
|
|
|
|
2022-12-06 22:43:37 +00:00
|
|
|
#[derive(Clone, Debug, Deref, DerefMut, PartialEq, Eq, TypeUuid)]
|
2022-05-18 18:04:58 +00:00
|
|
|
#[uuid = "6b6b533a-bb1f-11ec-bda2-00155d8fdde9"]
|
|
|
|
pub struct Buffer(syz::Buffer);
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
|
|
struct BufferAssetLoader;
|
|
|
|
|
|
|
|
impl AssetLoader for BufferAssetLoader {
|
|
|
|
fn load<'a>(
|
|
|
|
&'a self,
|
2022-05-18 20:39:37 +00:00
|
|
|
bytes: &'a [u8],
|
2022-05-18 18:04:58 +00:00
|
|
|
load_context: &'a mut LoadContext,
|
|
|
|
) -> BoxedFuture<'a, Result<(), anyhow::Error>> {
|
|
|
|
Box::pin(async move {
|
|
|
|
let buffer: Option<Buffer> =
|
|
|
|
match load_context.path().extension().unwrap().to_str().unwrap() {
|
|
|
|
"flac" | "mp3" | "wav" => {
|
2022-05-18 20:39:37 +00:00
|
|
|
syz::Buffer::from_encoded_data(bytes).map(Buffer).ok()
|
2022-05-18 18:04:58 +00:00
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
if let Some(buffer) = buffer {
|
|
|
|
load_context.set_default_asset(LoadedAsset::new(buffer));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extensions(&self) -> &[&str] {
|
|
|
|
&["flac", "mp3", "wav"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 22:43:37 +00:00
|
|
|
#[derive(Resource, Clone, Debug, Deref, DerefMut)]
|
|
|
|
pub struct Context(syz::Context);
|
|
|
|
|
2022-05-18 20:39:37 +00:00
|
|
|
#[derive(Component, Clone, Debug, Reflect)]
|
2022-05-18 18:04:58 +00:00
|
|
|
#[reflect(Component)]
|
2022-08-05 22:04:40 +00:00
|
|
|
pub struct Source {
|
2022-05-18 18:04:58 +00:00
|
|
|
pub gain: f64,
|
|
|
|
pub paused: bool,
|
|
|
|
#[reflect(ignore)]
|
2022-08-05 22:04:40 +00:00
|
|
|
pub handle: Option<syz::Source>,
|
2022-05-18 18:04:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 22:04:40 +00:00
|
|
|
impl Default for Source {
|
2022-05-18 18:04:58 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2022-05-18 20:39:37 +00:00
|
|
|
gain: 1.,
|
2022-05-18 18:04:58 +00:00
|
|
|
paused: false,
|
2022-08-05 22:04:40 +00:00
|
|
|
handle: None,
|
2022-05-18 18:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 14:14:11 +00:00
|
|
|
#[derive(Component, Clone, Copy, Debug, Deref, DerefMut)]
|
|
|
|
pub struct PannerStrategy(pub syz::PannerStrategy);
|
|
|
|
|
|
|
|
impl Default for PannerStrategy {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(syz::PannerStrategy::Delegate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 18:04:58 +00:00
|
|
|
#[derive(Component, Clone, Copy, Debug, Deref, DerefMut)]
|
2022-05-23 17:47:01 +00:00
|
|
|
pub struct DistanceModel(pub syz::DistanceModel);
|
2022-05-18 18:04:58 +00:00
|
|
|
|
|
|
|
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
|
|
|
|
#[reflect(Component)]
|
2022-05-23 17:47:01 +00:00
|
|
|
pub struct DistanceRef(pub f64);
|
2022-05-18 18:04:58 +00:00
|
|
|
|
|
|
|
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
|
|
|
|
#[reflect(Component)]
|
2022-05-23 17:47:01 +00:00
|
|
|
pub struct DistanceMax(pub f64);
|
2022-05-18 18:04:58 +00:00
|
|
|
|
|
|
|
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
|
|
|
|
#[reflect(Component)]
|
2022-05-23 17:47:01 +00:00
|
|
|
pub struct Rolloff(pub f64);
|
2022-05-18 18:04:58 +00:00
|
|
|
|
|
|
|
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
|
|
|
|
#[reflect(Component)]
|
2022-05-23 17:47:01 +00:00
|
|
|
pub struct ClosenessBoost(pub f64);
|
2022-05-18 18:04:58 +00:00
|
|
|
|
|
|
|
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
|
|
|
|
#[reflect(Component)]
|
2022-05-23 17:47:01 +00:00
|
|
|
pub struct ClosenessBoostDistance(pub f64);
|
2022-05-18 18:04:58 +00:00
|
|
|
|
2022-05-19 12:59:58 +00:00
|
|
|
#[derive(Component, Clone, Copy, Debug, Default, Reflect)]
|
|
|
|
#[reflect(Component)]
|
|
|
|
pub struct AngularPan {
|
|
|
|
pub azimuth: f64,
|
|
|
|
pub elevation: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
|
|
|
|
#[reflect(Component)]
|
|
|
|
pub struct ScalarPan(pub f64);
|
|
|
|
|
|
|
|
impl ScalarPan {
|
|
|
|
pub fn left() -> Self {
|
|
|
|
Self(-1.)
|
|
|
|
}
|
|
|
|
|
2022-06-03 16:23:03 +00:00
|
|
|
pub fn center() -> Self {
|
|
|
|
Self(0.)
|
|
|
|
}
|
|
|
|
|
2022-05-19 12:59:58 +00:00
|
|
|
pub fn right() -> Self {
|
|
|
|
Self(1.)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 19:59:53 +00:00
|
|
|
#[derive(Component, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum Audio {
|
|
|
|
Buffer(Handle<Buffer>),
|
|
|
|
Generator(syz::Generator),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Audio {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Buffer(default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Handle<Buffer>> for Audio {
|
|
|
|
fn from(value: Handle<Buffer>) -> Self {
|
|
|
|
Audio::Buffer(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<syz::Generator> for Audio {
|
|
|
|
fn from(value: syz::Generator) -> Self {
|
|
|
|
Self::Generator(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component, Clone, Debug)]
|
2022-08-05 22:04:40 +00:00
|
|
|
pub struct Sound {
|
2023-03-06 19:59:53 +00:00
|
|
|
pub audio: Audio,
|
2022-08-05 22:04:40 +00:00
|
|
|
pub gain: f64,
|
|
|
|
pub pitch: f64,
|
|
|
|
pub looping: bool,
|
|
|
|
pub paused: bool,
|
|
|
|
pub restart: bool,
|
2022-09-09 13:23:54 +00:00
|
|
|
pub generator: Option<syz::Generator>,
|
2022-08-05 22:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Sound {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2023-03-06 19:59:53 +00:00
|
|
|
audio: default(),
|
2022-08-05 22:04:40 +00:00
|
|
|
gain: 1.,
|
|
|
|
pitch: 1.,
|
|
|
|
looping: false,
|
|
|
|
paused: false,
|
|
|
|
restart: false,
|
|
|
|
generator: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-23 22:17:58 +00:00
|
|
|
pub enum SynthizerEvent {
|
|
|
|
Finished(Entity),
|
|
|
|
Looped(Entity),
|
|
|
|
}
|
|
|
|
|
2022-05-18 18:04:58 +00:00
|
|
|
#[derive(Component, Clone, Copy, Debug, Default, Reflect)]
|
|
|
|
#[reflect(Component)]
|
|
|
|
pub struct Listener;
|
|
|
|
|
2022-08-05 22:04:40 +00:00
|
|
|
fn update_listener(
|
2022-12-06 22:43:37 +00:00
|
|
|
context: ResMut<Context>,
|
2022-08-05 22:04:40 +00:00
|
|
|
listener: Query<Option<&GlobalTransform>, With<Listener>>,
|
2022-05-18 18:04:58 +00:00
|
|
|
) {
|
2022-08-05 22:04:40 +00:00
|
|
|
if let Ok(transform) = listener.get_single() {
|
|
|
|
let transform: Transform = transform
|
2022-05-18 18:04:58 +00:00
|
|
|
.map(|v| {
|
|
|
|
let transform: Transform = (*v).into();
|
|
|
|
transform
|
|
|
|
})
|
2022-08-05 22:04:40 +00:00
|
|
|
.unwrap_or_default();
|
|
|
|
let look = transform.local_x();
|
|
|
|
let up = transform.local_z();
|
|
|
|
context
|
|
|
|
.position()
|
|
|
|
.set((
|
2022-05-18 18:04:58 +00:00
|
|
|
transform.translation.x as f64,
|
|
|
|
transform.translation.y as f64,
|
|
|
|
transform.translation.z as f64,
|
2022-08-05 22:04:40 +00:00
|
|
|
))
|
|
|
|
.expect("Failed to set listener position");
|
|
|
|
context
|
|
|
|
.orientation()
|
|
|
|
.set((
|
2022-05-18 18:04:58 +00:00
|
|
|
look.x as f64,
|
|
|
|
look.y as f64,
|
|
|
|
look.z as f64,
|
|
|
|
up.x as f64,
|
|
|
|
up.y as f64,
|
|
|
|
up.z as f64,
|
2022-08-05 22:04:40 +00:00
|
|
|
))
|
|
|
|
.expect("Failed to set listener orientation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_source_handle(
|
2022-12-06 22:43:37 +00:00
|
|
|
context: Res<Context>,
|
2022-08-05 22:04:40 +00:00
|
|
|
mut query: Query<(
|
|
|
|
&mut Source,
|
|
|
|
Option<&PannerStrategy>,
|
|
|
|
Option<&GlobalTransform>,
|
|
|
|
Option<&AngularPan>,
|
|
|
|
Option<&ScalarPan>,
|
|
|
|
)>,
|
|
|
|
) {
|
2022-09-09 13:14:15 +00:00
|
|
|
for (mut source, panner_strategy, transform, angular_pan, scalar_pan) in &mut query {
|
2022-08-05 22:04:40 +00:00
|
|
|
if source.handle.is_none() {
|
|
|
|
let panner_strategy = panner_strategy.cloned().unwrap_or_default();
|
|
|
|
let handle: syz::Source = if let Some(transform) = transform {
|
|
|
|
let translation = transform.translation();
|
|
|
|
syz::Source3D::new(
|
|
|
|
&context,
|
|
|
|
*panner_strategy,
|
|
|
|
(
|
|
|
|
translation.x as f64,
|
|
|
|
translation.y as f64,
|
|
|
|
translation.z as f64,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.expect("Failed to create source")
|
|
|
|
.into()
|
|
|
|
} else if let Some(scalar_pan) = scalar_pan {
|
|
|
|
syz::ScalarPannedSource::new(&context, *panner_strategy, **scalar_pan)
|
|
|
|
.expect("Failed to create source")
|
|
|
|
.into()
|
|
|
|
} else if let Some(angular_pan) = angular_pan {
|
|
|
|
syz::AngularPannedSource::new(
|
|
|
|
&context,
|
|
|
|
*panner_strategy,
|
|
|
|
angular_pan.azimuth,
|
|
|
|
angular_pan.elevation,
|
|
|
|
)
|
|
|
|
.expect("Failed to create source")
|
|
|
|
.into()
|
|
|
|
} else {
|
|
|
|
syz::DirectSource::new(&context)
|
|
|
|
.expect("Failed to create source")
|
|
|
|
.into()
|
|
|
|
};
|
|
|
|
source.handle = Some(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_generator(
|
2022-12-06 22:43:37 +00:00
|
|
|
context: Res<Context>,
|
2022-08-05 22:04:40 +00:00
|
|
|
buffers: Res<Assets<Buffer>>,
|
|
|
|
mut query: Query<(Entity, Option<&Parent>, &mut Sound)>,
|
|
|
|
mut sources: Query<&mut Source>,
|
|
|
|
parents: Query<&Parent>,
|
|
|
|
) {
|
|
|
|
for (entity, parent, mut sound) in &mut query {
|
|
|
|
if sound.generator.is_none() {
|
2023-03-06 19:59:53 +00:00
|
|
|
let mut source = if let Ok(s) = sources.get_mut(entity) {
|
|
|
|
Some(s)
|
|
|
|
} else if let Some(parent) = parent {
|
|
|
|
let mut parent: Option<&Parent> = Some(parent);
|
|
|
|
let mut target = None;
|
|
|
|
while let Some(p) = parent {
|
|
|
|
if sources.get(**p).is_ok() {
|
|
|
|
target = Some(**p);
|
|
|
|
break;
|
2022-08-05 22:04:40 +00:00
|
|
|
}
|
2023-03-06 19:59:53 +00:00
|
|
|
parent = parents.get(**p).ok();
|
|
|
|
}
|
|
|
|
target.map(|v| sources.get_mut(v).unwrap())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
if let Some(source) = source.as_mut() {
|
|
|
|
if let Some(handle) = source.handle.as_mut() {
|
|
|
|
let generator: Option<syz::Generator> = match &sound.audio {
|
|
|
|
Audio::Buffer(buffer) => {
|
|
|
|
if let Some(b) = buffers.get(buffer) {
|
|
|
|
let generator = syz::BufferGenerator::new(&context)
|
|
|
|
.expect("Failed to create generator");
|
|
|
|
generator.buffer().set(&**b).expect("Unable to set buffer");
|
|
|
|
Some(generator.into())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Audio::Generator(generator) => Some(generator.clone()),
|
|
|
|
};
|
|
|
|
if let Some(generator) = generator {
|
2022-09-04 22:28:18 +00:00
|
|
|
assert!(sound.gain >= 0.);
|
|
|
|
generator
|
|
|
|
.gain()
|
|
|
|
.set(sound.gain)
|
|
|
|
.expect("Failed to set gain");
|
2023-03-06 19:59:53 +00:00
|
|
|
assert!(sound.pitch > 0. && sound.pitch <= 2.);
|
2022-09-04 17:01:32 +00:00
|
|
|
generator
|
|
|
|
.pitch_bend()
|
|
|
|
.set(sound.pitch)
|
|
|
|
.expect("Failed to set pitch");
|
2022-08-05 22:04:40 +00:00
|
|
|
handle
|
2023-03-06 19:59:53 +00:00
|
|
|
.add_generator(generator.handle())
|
2022-08-05 22:04:40 +00:00
|
|
|
.expect("Unable to add generator");
|
2023-03-06 19:59:53 +00:00
|
|
|
sound.generator = Some(generator);
|
2022-08-05 22:04:40 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-18 18:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 22:53:48 +00:00
|
|
|
fn add_sound_without_source(
|
|
|
|
mut commands: Commands,
|
2022-09-05 23:25:00 +00:00
|
|
|
query: Query<Entity, (Added<Sound>, Without<Source>)>,
|
2022-09-06 14:00:47 +00:00
|
|
|
parents: Query<(&Parent, Option<&Source>)>,
|
2022-08-05 22:53:48 +00:00
|
|
|
) {
|
2022-09-05 23:25:00 +00:00
|
|
|
for entity in &query {
|
2022-09-05 15:37:35 +00:00
|
|
|
let mut has_source = false;
|
|
|
|
let mut target = entity;
|
2022-09-06 14:00:47 +00:00
|
|
|
while let Ok((parent, source)) = parents.get(target) {
|
|
|
|
if source.is_some() {
|
2022-09-05 23:25:00 +00:00
|
|
|
has_source = true;
|
|
|
|
break;
|
2022-08-06 02:00:36 +00:00
|
|
|
}
|
2022-09-05 23:25:00 +00:00
|
|
|
target = **parent;
|
2022-09-05 15:37:35 +00:00
|
|
|
}
|
|
|
|
if !has_source {
|
|
|
|
commands.entity(entity).insert(Source::default());
|
2022-08-05 22:53:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 22:43:37 +00:00
|
|
|
#[derive(Resource, Default, Deref, DerefMut)]
|
2023-03-06 19:59:53 +00:00
|
|
|
struct LastAudio(HashMap<Entity, Audio>);
|
2022-05-18 18:04:58 +00:00
|
|
|
|
2022-06-03 00:28:09 +00:00
|
|
|
fn swap_buffers(
|
2023-03-06 19:59:53 +00:00
|
|
|
mut last_audio: ResMut<LastAudio>,
|
2022-06-03 00:28:09 +00:00
|
|
|
mut query: Query<(Entity, &mut Sound), Changed<Sound>>,
|
|
|
|
) {
|
2022-08-05 22:04:40 +00:00
|
|
|
for (entity, mut sound) in &mut query {
|
2023-03-06 19:59:53 +00:00
|
|
|
if let Some(l) = last_audio.get(&entity) {
|
|
|
|
if sound.audio != *l {
|
2022-05-18 18:04:58 +00:00
|
|
|
sound.generator = None;
|
|
|
|
}
|
|
|
|
}
|
2023-03-06 19:59:53 +00:00
|
|
|
last_audio.insert(entity, sound.audio.clone());
|
2022-05-18 18:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 14:24:30 +00:00
|
|
|
fn change_panner_strategy(
|
2023-03-06 20:24:23 +00:00
|
|
|
changed: Query<(Entity, Ref<PannerStrategy>)>,
|
|
|
|
mut removed: RemovedComponents<PannerStrategy>,
|
2022-08-05 22:04:40 +00:00
|
|
|
mut sources: Query<&mut Source>,
|
2022-06-13 14:24:30 +00:00
|
|
|
) {
|
|
|
|
let mut check = vec![];
|
2022-09-05 17:24:06 +00:00
|
|
|
for (entity, change) in &changed {
|
|
|
|
if !change.is_added() && change.is_changed() {
|
|
|
|
check.push(entity);
|
|
|
|
}
|
2022-06-13 14:24:30 +00:00
|
|
|
}
|
|
|
|
for entity in removed.iter() {
|
|
|
|
check.push(entity);
|
|
|
|
}
|
|
|
|
for entity in check.iter() {
|
2022-08-05 22:04:40 +00:00
|
|
|
if let Ok(mut source) = sources.get_mut(*entity) {
|
|
|
|
if source.handle.is_some() {
|
|
|
|
source.handle = None;
|
2022-06-13 14:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 22:04:40 +00:00
|
|
|
fn update_source_properties(
|
2022-12-06 22:43:37 +00:00
|
|
|
context: Res<Context>,
|
2022-05-18 18:04:58 +00:00
|
|
|
mut query: Query<(
|
2022-08-05 22:04:40 +00:00
|
|
|
&mut Source,
|
2022-05-18 18:04:58 +00:00
|
|
|
Option<&DistanceModel>,
|
|
|
|
Option<&DistanceRef>,
|
|
|
|
Option<&DistanceMax>,
|
|
|
|
Option<&Rolloff>,
|
|
|
|
Option<&ClosenessBoost>,
|
|
|
|
Option<&ClosenessBoostDistance>,
|
2022-05-19 12:59:58 +00:00
|
|
|
Option<&AngularPan>,
|
|
|
|
Option<&ScalarPan>,
|
2022-05-18 18:04:58 +00:00
|
|
|
Option<&GlobalTransform>,
|
|
|
|
)>,
|
|
|
|
) {
|
|
|
|
for (
|
2022-08-05 22:04:40 +00:00
|
|
|
mut source,
|
2022-05-18 18:04:58 +00:00
|
|
|
distance_model,
|
|
|
|
distance_ref,
|
|
|
|
distance_max,
|
|
|
|
rolloff,
|
|
|
|
closeness_boost,
|
|
|
|
closeness_boost_distance,
|
2022-05-19 12:59:58 +00:00
|
|
|
angular_pan,
|
|
|
|
scalar_pan,
|
2022-05-18 18:04:58 +00:00
|
|
|
transform,
|
2022-08-05 22:04:40 +00:00
|
|
|
) in &mut query
|
2022-05-18 18:04:58 +00:00
|
|
|
{
|
2022-08-05 22:04:40 +00:00
|
|
|
let Source { gain, .. } = *source;
|
2022-06-03 16:23:03 +00:00
|
|
|
assert!(gain >= 0.);
|
2022-08-05 22:04:40 +00:00
|
|
|
if let Some(handle) = source.handle.as_mut() {
|
|
|
|
handle.gain().set(gain).expect("Failed to set gain");
|
2022-05-19 12:59:58 +00:00
|
|
|
let mut clear_source = false;
|
2022-08-05 22:04:40 +00:00
|
|
|
if let Some(transform) = transform {
|
2022-08-15 15:53:59 +00:00
|
|
|
if let Some(source) = handle.cast_to::<syz::Source3D>().expect("Failed to cast") {
|
2022-08-05 22:04:40 +00:00
|
|
|
let translation = transform.translation();
|
2022-05-19 00:44:31 +00:00
|
|
|
source
|
|
|
|
.position()
|
|
|
|
.set((
|
|
|
|
translation.x as f64,
|
|
|
|
translation.y as f64,
|
|
|
|
translation.z as f64,
|
|
|
|
))
|
|
|
|
.expect("Failed to set position");
|
|
|
|
let distance_model = distance_model
|
|
|
|
.cloned()
|
|
|
|
.map(|v| *v)
|
2022-05-24 17:38:42 +00:00
|
|
|
.unwrap_or_else(|| context.default_distance_model().get().unwrap());
|
2022-05-18 18:04:58 +00:00
|
|
|
source
|
|
|
|
.distance_model()
|
2022-05-19 00:44:31 +00:00
|
|
|
.set(distance_model)
|
2022-05-18 18:04:58 +00:00
|
|
|
.expect("Failed to set distance_model");
|
2022-05-19 00:44:31 +00:00
|
|
|
let distance_ref = distance_ref
|
|
|
|
.map(|v| **v)
|
|
|
|
.unwrap_or_else(|| context.default_distance_ref().get().unwrap());
|
2022-06-03 16:23:03 +00:00
|
|
|
assert!(distance_ref >= 0.);
|
2022-05-18 18:04:58 +00:00
|
|
|
source
|
|
|
|
.distance_ref()
|
2022-05-19 00:44:31 +00:00
|
|
|
.set(distance_ref)
|
2022-05-18 18:04:58 +00:00
|
|
|
.expect("Failed to set distance_ref");
|
2022-05-19 00:44:31 +00:00
|
|
|
let distance_max = distance_max
|
|
|
|
.map(|v| **v)
|
|
|
|
.unwrap_or_else(|| context.default_distance_max().get().unwrap());
|
2022-06-03 16:23:03 +00:00
|
|
|
assert!(distance_max >= 0.);
|
2022-05-18 18:04:58 +00:00
|
|
|
source
|
|
|
|
.distance_max()
|
2022-05-19 00:44:31 +00:00
|
|
|
.set(distance_max)
|
2022-05-18 18:04:58 +00:00
|
|
|
.expect("Failed to set distance_max");
|
2022-05-19 00:44:31 +00:00
|
|
|
let rolloff = rolloff
|
|
|
|
.map(|v| **v)
|
|
|
|
.unwrap_or_else(|| context.default_rolloff().get().unwrap());
|
2022-06-03 16:23:03 +00:00
|
|
|
assert!(rolloff >= 0.);
|
2022-05-18 18:04:58 +00:00
|
|
|
source
|
|
|
|
.rolloff()
|
2022-05-19 00:44:31 +00:00
|
|
|
.set(rolloff)
|
2022-05-18 18:04:58 +00:00
|
|
|
.expect("Failed to set rolloff");
|
2022-05-19 00:44:31 +00:00
|
|
|
let closeness_boost = closeness_boost
|
|
|
|
.map(|v| **v)
|
|
|
|
.unwrap_or_else(|| context.default_closeness_boost().get().unwrap());
|
2022-06-03 16:23:03 +00:00
|
|
|
assert!(closeness_boost >= 0.);
|
2022-05-18 18:04:58 +00:00
|
|
|
source
|
|
|
|
.closeness_boost()
|
2022-05-19 00:44:31 +00:00
|
|
|
.set(closeness_boost)
|
2022-05-18 18:04:58 +00:00
|
|
|
.expect("Failed to set closeness_boost");
|
2022-05-19 00:44:31 +00:00
|
|
|
let closeness_boost_distance =
|
|
|
|
closeness_boost_distance.map(|v| **v).unwrap_or_else(|| {
|
|
|
|
context.default_closeness_boost_distance().get().unwrap()
|
|
|
|
});
|
2022-06-03 16:23:03 +00:00
|
|
|
assert!(closeness_boost_distance >= 0.);
|
2022-05-18 18:04:58 +00:00
|
|
|
source
|
|
|
|
.closeness_boost_distance()
|
2022-05-19 00:44:31 +00:00
|
|
|
.set(closeness_boost_distance)
|
2022-05-18 18:04:58 +00:00
|
|
|
.expect("Failed to set closeness_boost_distance");
|
2022-05-19 12:59:58 +00:00
|
|
|
} else {
|
|
|
|
clear_source = true;
|
2022-05-18 18:04:58 +00:00
|
|
|
}
|
2022-05-19 12:59:58 +00:00
|
|
|
} else if let Some(angular_pan) = angular_pan {
|
2022-08-15 15:53:59 +00:00
|
|
|
if let Some(source) = handle
|
|
|
|
.cast_to::<syz::AngularPannedSource>()
|
|
|
|
.expect("Failed to cast")
|
|
|
|
{
|
2022-06-03 16:23:03 +00:00
|
|
|
assert!(angular_pan.azimuth >= 0. && angular_pan.azimuth <= 360.);
|
2022-05-19 12:59:58 +00:00
|
|
|
source
|
|
|
|
.azimuth()
|
|
|
|
.set(angular_pan.azimuth)
|
|
|
|
.expect("Failed to set azimuth");
|
2022-06-03 16:23:03 +00:00
|
|
|
assert!(angular_pan.elevation >= -90. && angular_pan.elevation <= 90.);
|
2022-05-19 12:59:58 +00:00
|
|
|
source
|
|
|
|
.elevation()
|
|
|
|
.set(angular_pan.elevation)
|
|
|
|
.expect("Failed to set elevation");
|
|
|
|
} else {
|
|
|
|
clear_source = true;
|
|
|
|
}
|
|
|
|
} else if let Some(scalar_pan) = scalar_pan {
|
2022-08-15 15:53:59 +00:00
|
|
|
if let Some(source) = handle
|
|
|
|
.cast_to::<syz::ScalarPannedSource>()
|
|
|
|
.expect("Failed to cast")
|
|
|
|
{
|
2022-06-03 16:23:03 +00:00
|
|
|
assert!(**scalar_pan >= -1. && **scalar_pan <= 1.);
|
2022-05-19 12:59:58 +00:00
|
|
|
source
|
|
|
|
.panning_scalar()
|
|
|
|
.set(**scalar_pan)
|
|
|
|
.expect("Failed to set scalar panning");
|
|
|
|
} else {
|
|
|
|
clear_source = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if clear_source {
|
2022-08-05 22:04:40 +00:00
|
|
|
source.handle = None;
|
2022-05-18 18:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 22:04:40 +00:00
|
|
|
fn update_sound_properties(mut query: Query<&mut Sound>) {
|
|
|
|
for mut sound in &mut query {
|
|
|
|
let Sound {
|
|
|
|
gain,
|
|
|
|
pitch,
|
|
|
|
looping,
|
|
|
|
..
|
|
|
|
} = *sound;
|
|
|
|
assert!(gain >= 0.);
|
2022-09-04 22:28:18 +00:00
|
|
|
assert!(pitch > 0. && pitch <= 2.);
|
2022-08-05 22:04:40 +00:00
|
|
|
if sound.restart {
|
|
|
|
if let Some(generator) = sound.generator.as_mut() {
|
2022-09-09 13:23:54 +00:00
|
|
|
if let Some(generator) = generator
|
|
|
|
.cast_to::<syz::BufferGenerator>()
|
|
|
|
.expect("Failed to cast")
|
|
|
|
{
|
|
|
|
generator
|
|
|
|
.playback_position()
|
|
|
|
.set(0.)
|
|
|
|
.expect("Failed to restart");
|
|
|
|
}
|
2022-08-05 22:04:40 +00:00
|
|
|
}
|
|
|
|
sound.restart = false;
|
|
|
|
}
|
|
|
|
if let Some(generator) = sound.generator.as_mut() {
|
|
|
|
generator.gain().set(gain).expect("Failed to set gain");
|
|
|
|
generator
|
|
|
|
.pitch_bend()
|
|
|
|
.set(pitch)
|
|
|
|
.expect("Failed to set pitch");
|
2022-09-09 13:23:54 +00:00
|
|
|
if let Some(generator) = generator
|
|
|
|
.cast_to::<syz::BufferGenerator>()
|
|
|
|
.expect("Failed to cast")
|
|
|
|
{
|
|
|
|
generator
|
|
|
|
.looping()
|
|
|
|
.set(looping)
|
|
|
|
.expect("Failed to set looping");
|
|
|
|
}
|
2022-08-05 22:04:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_source_playback_state(query: Query<&Source>) {
|
|
|
|
for source in &query {
|
|
|
|
if let Some(handle) = &source.handle {
|
|
|
|
if source.paused {
|
|
|
|
handle.pause().expect("Failed to pause");
|
|
|
|
} else {
|
|
|
|
handle.play().expect("Failed to play");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_sound_playback_state(query: Query<&Sound>) {
|
|
|
|
for sound in &query {
|
2022-05-18 18:04:58 +00:00
|
|
|
if let Some(generator) = &sound.generator {
|
|
|
|
if sound.paused {
|
|
|
|
generator.pause().expect("Failed to pause");
|
|
|
|
} else {
|
|
|
|
generator.play().expect("Failed to play");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 20:24:23 +00:00
|
|
|
fn remove_sound(mut last_buffer: ResMut<LastAudio>, mut removed: RemovedComponents<Source>) {
|
2022-05-18 18:04:58 +00:00
|
|
|
for entity in removed.iter() {
|
|
|
|
last_buffer.remove(&entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 22:43:37 +00:00
|
|
|
#[derive(Resource, Debug)]
|
2022-06-02 23:37:08 +00:00
|
|
|
pub struct SynthizerDefaults {
|
2022-06-03 00:28:09 +00:00
|
|
|
pub panner_strategy: syz::PannerStrategy,
|
|
|
|
pub distance_model: syz::DistanceModel,
|
|
|
|
pub distance_ref: f64,
|
|
|
|
pub distance_max: f64,
|
|
|
|
pub rolloff: f64,
|
|
|
|
pub closeness_boost: f64,
|
|
|
|
pub closeness_boost_distance: f64,
|
2022-05-19 15:18:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 00:28:09 +00:00
|
|
|
fn sync_config(
|
2022-12-06 22:43:37 +00:00
|
|
|
context: Res<Context>,
|
2022-12-15 20:04:57 +00:00
|
|
|
config: Res<SynthizerPlugin>,
|
2022-06-03 00:28:09 +00:00
|
|
|
defaults: Res<SynthizerDefaults>,
|
|
|
|
) {
|
2022-05-19 15:18:41 +00:00
|
|
|
if config.is_changed() {
|
|
|
|
context
|
|
|
|
.default_panner_strategy()
|
|
|
|
.set(
|
|
|
|
config
|
|
|
|
.default_panner_strategy
|
|
|
|
.unwrap_or(defaults.panner_strategy),
|
|
|
|
)
|
|
|
|
.expect("Failed to set panner strategy");
|
|
|
|
context
|
2022-05-24 17:38:42 +00:00
|
|
|
.default_distance_model()
|
2022-05-19 15:18:41 +00:00
|
|
|
.set(
|
|
|
|
config
|
|
|
|
.default_distance_model
|
|
|
|
.unwrap_or(defaults.distance_model),
|
|
|
|
)
|
|
|
|
.expect("Failed to set distance model");
|
|
|
|
context
|
|
|
|
.default_distance_ref()
|
|
|
|
.set(config.default_distance_ref.unwrap_or(defaults.distance_ref))
|
|
|
|
.expect("Failed to set distance_ref");
|
|
|
|
context
|
|
|
|
.default_distance_max()
|
|
|
|
.set(config.default_distance_max.unwrap_or(defaults.distance_max))
|
|
|
|
.expect("Failed to set distance_max");
|
|
|
|
context
|
|
|
|
.default_rolloff()
|
|
|
|
.set(config.default_rolloff.unwrap_or(defaults.rolloff))
|
|
|
|
.expect("Failed to set rolloff");
|
|
|
|
context
|
|
|
|
.default_closeness_boost()
|
|
|
|
.set(
|
|
|
|
config
|
|
|
|
.default_closeness_boost
|
|
|
|
.unwrap_or(defaults.closeness_boost),
|
|
|
|
)
|
|
|
|
.expect("Failed to set closeness_boost");
|
|
|
|
context
|
|
|
|
.default_closeness_boost_distance()
|
|
|
|
.set(
|
|
|
|
config
|
|
|
|
.default_closeness_boost_distance
|
|
|
|
.unwrap_or(defaults.closeness_boost_distance),
|
|
|
|
)
|
|
|
|
.expect("Failed to set closeness_boost_distance");
|
2022-05-19 00:56:41 +00:00
|
|
|
}
|
2022-05-18 18:04:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 22:17:58 +00:00
|
|
|
fn events(
|
2022-12-06 22:43:37 +00:00
|
|
|
context: Res<Context>,
|
2022-05-23 22:17:58 +00:00
|
|
|
sounds: Query<(Entity, &Sound)>,
|
|
|
|
mut output: EventWriter<SynthizerEvent>,
|
|
|
|
) {
|
|
|
|
context.get_events().for_each(|event| {
|
|
|
|
if let Ok(event) = event {
|
2022-09-06 14:00:47 +00:00
|
|
|
let mut matched = false;
|
2022-08-05 22:04:40 +00:00
|
|
|
for (entity, sound) in &sounds {
|
2022-05-23 22:17:58 +00:00
|
|
|
if let Some(generator) = &sound.generator {
|
|
|
|
if *generator.handle() == event.source {
|
2022-09-06 14:00:47 +00:00
|
|
|
matched = true;
|
2022-05-23 22:17:58 +00:00
|
|
|
match event.r#type {
|
|
|
|
syz::EventType::Finished => {
|
|
|
|
output.send(SynthizerEvent::Finished(entity));
|
|
|
|
}
|
|
|
|
syz::EventType::Looped => {
|
|
|
|
output.send(SynthizerEvent::Looped(entity));
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2022-05-23 22:29:59 +00:00
|
|
|
break;
|
2022-05-23 22:17:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-06 14:00:47 +00:00
|
|
|
if !matched {
|
|
|
|
println!("No match");
|
|
|
|
}
|
2022-05-23 22:17:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-06 20:24:23 +00:00
|
|
|
#[derive(SystemSet, Clone, Hash, Debug, PartialEq, Eq)]
|
2023-04-03 19:24:29 +00:00
|
|
|
pub enum SynthizerSets {
|
2023-04-03 19:48:05 +00:00
|
|
|
First,
|
2022-08-05 22:04:40 +00:00
|
|
|
UpdateHandles,
|
|
|
|
UpdateProperties,
|
|
|
|
UpdateState,
|
2023-04-03 19:48:05 +00:00
|
|
|
Last,
|
2022-08-05 22:04:40 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 22:43:37 +00:00
|
|
|
#[derive(Resource)]
|
|
|
|
struct InitializationGuard(syz::InitializationGuard);
|
|
|
|
|
2022-12-15 22:37:34 +00:00
|
|
|
#[derive(Resource, Clone, Copy, Default, Debug)]
|
2022-12-15 20:04:57 +00:00
|
|
|
pub struct SynthizerPlugin {
|
|
|
|
pub default_panner_strategy: Option<syz::PannerStrategy>,
|
|
|
|
pub default_distance_model: Option<syz::DistanceModel>,
|
|
|
|
pub default_distance_ref: Option<f64>,
|
|
|
|
pub default_distance_max: Option<f64>,
|
|
|
|
pub default_rolloff: Option<f64>,
|
|
|
|
pub default_closeness_boost: Option<f64>,
|
|
|
|
pub default_closeness_boost_distance: Option<f64>,
|
|
|
|
pub log_level: syz::LogLevel,
|
|
|
|
pub log_to_stderr: bool,
|
|
|
|
}
|
2022-05-18 18:04:58 +00:00
|
|
|
|
|
|
|
impl Plugin for SynthizerPlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
2022-12-15 20:04:57 +00:00
|
|
|
if !app.world.contains_resource::<SynthizerPlugin>() {
|
2022-12-15 22:37:34 +00:00
|
|
|
app.insert_resource(*self);
|
2022-05-18 18:04:58 +00:00
|
|
|
}
|
2022-12-15 22:37:34 +00:00
|
|
|
let config = *app.world.get_resource::<SynthizerPlugin>().unwrap();
|
2022-08-15 15:53:59 +00:00
|
|
|
let mut syz_config = syz::LibraryConfig::new();
|
|
|
|
syz_config.log_level(config.log_level);
|
|
|
|
if config.log_to_stderr {
|
|
|
|
syz_config.log_to_stderr();
|
|
|
|
}
|
|
|
|
let guard = syz_config
|
|
|
|
.initialize()
|
|
|
|
.expect("Failed to initialize Synthizer");
|
2022-12-06 22:43:37 +00:00
|
|
|
let guard = InitializationGuard(guard);
|
2022-08-13 13:48:31 +00:00
|
|
|
let context = syz::Context::new().expect("Failed to create Synthizer context");
|
2022-06-02 23:37:08 +00:00
|
|
|
let defaults = SynthizerDefaults {
|
2022-05-19 15:18:41 +00:00
|
|
|
panner_strategy: context.default_panner_strategy().get().unwrap(),
|
2022-05-24 17:38:42 +00:00
|
|
|
distance_model: context.default_distance_model().get().unwrap(),
|
2022-05-19 15:18:41 +00:00
|
|
|
distance_ref: context.default_distance_ref().get().unwrap(),
|
|
|
|
distance_max: context.default_distance_max().get().unwrap(),
|
|
|
|
rolloff: context.default_rolloff().get().unwrap(),
|
|
|
|
closeness_boost: context.default_closeness_boost().get().unwrap(),
|
|
|
|
closeness_boost_distance: context.default_closeness_boost_distance().get().unwrap(),
|
|
|
|
};
|
2022-05-23 22:17:58 +00:00
|
|
|
context.enable_events().expect("Failed to enable events");
|
2022-12-06 22:43:37 +00:00
|
|
|
let context = Context(context);
|
2022-05-18 18:04:58 +00:00
|
|
|
app.add_asset::<Buffer>()
|
|
|
|
.init_asset_loader::<BufferAssetLoader>()
|
2022-12-06 22:59:49 +00:00
|
|
|
.register_type::<DistanceRef>()
|
|
|
|
.register_type::<DistanceMax>()
|
|
|
|
.register_type::<Rolloff>()
|
|
|
|
.register_type::<ClosenessBoostDistance>()
|
|
|
|
.register_type::<AngularPan>()
|
|
|
|
.register_type::<ScalarPan>()
|
2022-12-07 17:18:26 +00:00
|
|
|
.register_type::<Source>()
|
2022-05-18 18:04:58 +00:00
|
|
|
.register_type::<Listener>()
|
|
|
|
.insert_resource(guard)
|
|
|
|
.insert_resource(context)
|
2023-03-06 19:59:53 +00:00
|
|
|
.init_resource::<LastAudio>()
|
2022-05-19 15:18:41 +00:00
|
|
|
.insert_resource(defaults)
|
2022-05-23 22:17:58 +00:00
|
|
|
.add_event::<SynthizerEvent>()
|
2023-04-03 19:48:05 +00:00
|
|
|
.add_systems(
|
|
|
|
(sync_config, swap_buffers, change_panner_strategy)
|
|
|
|
.in_set(SynthizerSets::First)
|
|
|
|
.in_base_set(CoreSet::PreUpdate),
|
2022-08-06 01:08:25 +00:00
|
|
|
)
|
2023-04-03 19:48:05 +00:00
|
|
|
.configure_set(SynthizerSets::First.before(SynthizerSets::UpdateHandles))
|
|
|
|
.add_systems(
|
|
|
|
(add_source_handle, add_generator, add_sound_without_source)
|
2023-03-06 20:24:23 +00:00
|
|
|
.in_base_set(CoreSet::PostUpdate)
|
2023-04-03 19:48:05 +00:00
|
|
|
.in_set(SynthizerSets::UpdateHandles),
|
2022-06-13 14:24:30 +00:00
|
|
|
)
|
2023-04-03 19:48:05 +00:00
|
|
|
.configure_set(SynthizerSets::UpdateHandles.before(SynthizerSets::UpdateProperties))
|
|
|
|
.add_systems(
|
|
|
|
(
|
|
|
|
update_listener,
|
|
|
|
update_source_properties,
|
|
|
|
update_sound_properties,
|
|
|
|
)
|
2023-03-06 20:24:23 +00:00
|
|
|
.in_base_set(CoreSet::PostUpdate)
|
2023-04-03 19:48:05 +00:00
|
|
|
.in_set(SynthizerSets::UpdateProperties),
|
2022-05-18 18:04:58 +00:00
|
|
|
)
|
2023-04-03 19:48:05 +00:00
|
|
|
.configure_set(
|
|
|
|
SynthizerSets::UpdateProperties
|
|
|
|
.before(SynthizerSets::UpdateState)
|
|
|
|
.after(TransformSystem::TransformPropagate),
|
2022-08-05 22:04:40 +00:00
|
|
|
)
|
2023-04-03 19:48:05 +00:00
|
|
|
.add_systems(
|
|
|
|
(update_source_playback_state, update_sound_playback_state)
|
2023-03-06 20:24:23 +00:00
|
|
|
.in_base_set(CoreSet::PostUpdate)
|
2023-04-03 19:24:29 +00:00
|
|
|
.in_set(SynthizerSets::UpdateState),
|
2022-08-05 22:04:40 +00:00
|
|
|
)
|
2023-04-03 19:48:05 +00:00
|
|
|
.configure_set(SynthizerSets::UpdateState.before(SynthizerSets::Last))
|
|
|
|
.add_systems(
|
|
|
|
(remove_sound, events)
|
|
|
|
.in_set(SynthizerSets::Last)
|
|
|
|
.in_base_set(CoreSet::PostUpdate),
|
2022-09-05 18:23:33 +00:00
|
|
|
);
|
2022-05-18 18:04:58 +00:00
|
|
|
}
|
|
|
|
}
|