Compare commits

..

7 Commits

3 changed files with 317 additions and 167 deletions

View File

@ -10,7 +10,7 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1" anyhow = "1"
bevy = { version = "0.8", default-features = false, features = ["bevy_asset"] } bevy = { version = "0.8", default-features = false, features = ["bevy_asset"] }
synthizer = { git = "https://github.com/synthizer/synthizer-rs" } synthizer = { git = "https://github.com/ndarilek/synthizer-rs" }
[dev-dependencies] [dev-dependencies]
bevy = { version = "0.8", default-features = true } bevy = { version = "0.8", default-features = true }

View File

@ -35,15 +35,16 @@ fn load_and_create(
== LoadState::Loaded; == LoadState::Loaded;
if handles.loaded { if handles.loaded {
commands commands
.spawn() .spawn_bundle(TransformBundle::default())
.insert(Listener) .insert(Listener)
.insert(Transform::default())
.insert(RotationTimer::default()); .insert(RotationTimer::default());
let handle = handles.sounds[0].clone(); let handle = handles.sounds[0].clone();
let buffer = asset_server.get_handle(handle); let buffer = asset_server.get_handle(handle);
commands commands
.spawn() .spawn_bundle(TransformBundle::from(Transform::from_translation(
.insert(Transform::from_translation(Vec3::new(10., 0., 0.))) Vec3::new(10., 0., 0.),
)))
.insert(Source::default())
.insert(Sound { .insert(Sound {
buffer, buffer,
looping: true, looping: true,

View File

@ -45,30 +45,19 @@ impl AssetLoader for BufferAssetLoader {
#[derive(Component, Clone, Debug, Reflect)] #[derive(Component, Clone, Debug, Reflect)]
#[reflect(Component)] #[reflect(Component)]
pub struct Sound { pub struct Source {
pub buffer: Handle<Buffer>,
pub gain: f64, pub gain: f64,
pub pitch: f64,
pub looping: bool,
pub paused: bool, pub paused: bool,
pub restart: bool,
#[reflect(ignore)] #[reflect(ignore)]
pub source: Option<syz::Source>, pub handle: Option<syz::Source>,
#[reflect(ignore)]
pub generator: Option<syz::BufferGenerator>,
} }
impl Default for Sound { impl Default for Source {
fn default() -> Self { fn default() -> Self {
Self { Self {
buffer: Default::default(),
gain: 1., gain: 1.,
pitch: 1.,
looping: false,
paused: false, paused: false,
restart: false, handle: None,
source: None,
generator: None,
} }
} }
} }
@ -131,6 +120,33 @@ impl ScalarPan {
} }
} }
#[derive(Component, Clone, Debug, Reflect)]
#[reflect(Component)]
pub struct Sound {
pub buffer: Handle<Buffer>,
pub gain: f64,
pub pitch: f64,
pub looping: bool,
pub paused: bool,
pub restart: bool,
#[reflect(ignore)]
pub generator: Option<syz::BufferGenerator>,
}
impl Default for Sound {
fn default() -> Self {
Self {
buffer: default(),
gain: 1.,
pitch: 1.,
looping: false,
paused: false,
restart: false,
generator: None,
}
}
}
pub enum SynthizerEvent { pub enum SynthizerEvent {
Finished(Entity), Finished(Entity),
Looped(Entity), Looped(Entity),
@ -140,44 +156,173 @@ pub enum SynthizerEvent {
#[reflect(Component)] #[reflect(Component)]
pub struct Listener; pub struct Listener;
pub fn update_listener( fn update_listener(
context: ResMut<syz::Context>, context: ResMut<syz::Context>,
listener: Query<(Option<&Transform>, Option<&GlobalTransform>), With<Listener>>, listener: Query<Option<&GlobalTransform>, With<Listener>>,
) { ) {
if let Ok((transform, global_transform)) = listener.get_single() { if let Ok(transform) = listener.get_single() {
let transform: Option<Transform> = global_transform let transform: Transform = transform
.map(|v| { .map(|v| {
let transform: Transform = (*v).into(); let transform: Transform = (*v).into();
transform transform
}) })
.or_else(|| transform.cloned()); .unwrap_or_default();
if let Some(transform) = transform { let look = transform.local_x();
let look = transform.local_x(); let up = transform.local_z();
let up = transform.local_z(); context
if let Err(e) = context.position().set(( .position()
.set((
transform.translation.x as f64, transform.translation.x as f64,
transform.translation.y as f64, transform.translation.y as f64,
transform.translation.z as f64, transform.translation.z as f64,
)) { ))
error!("Error setting listener position: {:?}", e); .expect("Failed to set listener position");
} context
if let Err(e) = context.orientation().set(( .orientation()
.set((
look.x as f64, look.x as f64,
look.y as f64, look.y as f64,
look.z as f64, look.z as f64,
up.x as f64, up.x as f64,
up.y as f64, up.y as f64,
up.z as f64, up.z as f64,
)) { ))
error!("Error setting listener orientation: {:?}", e); .expect("Failed to set listener orientation");
} }
} else { }
context.position().set((0., 0., 0.)).ok();
context.orientation().set((0., 0., 1., 0., 1., 0.)).ok(); fn add_source_handle(
context: Res<syz::Context>,
mut query: Query<(
&mut Source,
Option<&PannerStrategy>,
Option<&GlobalTransform>,
Option<&AngularPan>,
Option<&ScalarPan>,
)>,
) {
for (mut source, panner_strategy, transform, angular_pan, scalar_pan) in &mut query {
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(
context: Res<syz::Context>,
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() {
if let Some(b) = buffers.get(&sound.buffer) {
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;
}
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 = syz::BufferGenerator::new(&context)
.expect("Failed to create generator");
generator.buffer().set(&**b).expect("Unable to set buffer");
handle
.add_generator(&generator)
.expect("Unable to add generator");
sound.generator = Some(generator);
}
}
}
}
}
}
fn add_sound_without_source(
mut commands: Commands,
query: Query<
(Entity, Option<&Parent>, Option<&GlobalTransform>),
(Added<Sound>, Without<Source>),
>,
parents: Query<&Parent>,
sources: Query<&Source>,
transforms: Query<&GlobalTransform>,
) {
for (entity, parent, transform) in &query {
let should_check_for_transform = transform.is_none();
let mut has_transform = false;
let source = if let Some(parent) = parent {
if should_check_for_transform {
has_transform = transforms.get(**parent).is_ok();
}
let mut target = None;
let mut parent: Option<&Parent> = Some(parent);
while let Some(p) = parent {
if should_check_for_transform && !has_transform {
has_transform = transforms.get(**p).is_ok();
}
if sources.get(**p).is_ok() {
target = Some(**p);
break;
}
parent = parents.get(**p).ok();
}
target.map(|v| sources.get(v).unwrap())
} else {
None
};
if source.is_none() {
let id = commands.entity(entity).insert(Source::default()).id();
if has_transform {
commands
.entity(id)
.insert_bundle(TransformBundle::default());
}
} }
} else {
context.position().set((0., 0., 0.)).ok();
context.orientation().set((0., 0., 1., 0., 1., 0.)).ok();
} }
} }
@ -188,10 +333,9 @@ fn swap_buffers(
mut last_buffer: ResMut<LastBuffer>, mut last_buffer: ResMut<LastBuffer>,
mut query: Query<(Entity, &mut Sound), Changed<Sound>>, mut query: Query<(Entity, &mut Sound), Changed<Sound>>,
) { ) {
for (entity, mut sound) in query.iter_mut() { for (entity, mut sound) in &mut query {
if let Some(l) = last_buffer.get(&entity) { if let Some(l) = last_buffer.get(&entity) {
if sound.buffer != *l { if sound.buffer != *l {
sound.source = None;
sound.generator = None; sound.generator = None;
} }
} }
@ -202,30 +346,28 @@ fn swap_buffers(
fn change_panner_strategy( fn change_panner_strategy(
changed: Query<Entity, Changed<PannerStrategy>>, changed: Query<Entity, Changed<PannerStrategy>>,
removed: RemovedComponents<PannerStrategy>, removed: RemovedComponents<PannerStrategy>,
mut sounds: Query<&mut Sound>, mut sources: Query<&mut Source>,
) { ) {
let mut check = vec![]; let mut check = vec![];
for entity in changed.iter() { for entity in &changed {
check.push(entity); check.push(entity);
} }
for entity in removed.iter() { for entity in removed.iter() {
check.push(entity); check.push(entity);
} }
for entity in check.iter() { for entity in check.iter() {
if let Ok(mut sound) = sounds.get_mut(*entity) { if let Ok(mut source) = sources.get_mut(*entity) {
if sound.source.is_some() { if source.handle.is_some() {
sound.source = None; source.handle = None;
} }
} }
} }
} }
pub fn update_sound_properties( fn update_source_properties(
context: Res<syz::Context>, context: Res<syz::Context>,
buffers: Res<Assets<Buffer>>,
mut query: Query<( mut query: Query<(
&mut Sound, &mut Source,
Option<&PannerStrategy>,
Option<&DistanceModel>, Option<&DistanceModel>,
Option<&DistanceRef>, Option<&DistanceRef>,
Option<&DistanceMax>, Option<&DistanceMax>,
@ -234,13 +376,11 @@ pub fn update_sound_properties(
Option<&ClosenessBoostDistance>, Option<&ClosenessBoostDistance>,
Option<&AngularPan>, Option<&AngularPan>,
Option<&ScalarPan>, Option<&ScalarPan>,
Option<&Transform>,
Option<&GlobalTransform>, Option<&GlobalTransform>,
)>, )>,
) { ) {
for ( for (
mut sound, mut source,
panner_strategy,
distance_model, distance_model,
distance_ref, distance_ref,
distance_max, distance_max,
@ -250,103 +390,16 @@ pub fn update_sound_properties(
angular_pan, angular_pan,
scalar_pan, scalar_pan,
transform, transform,
global_transform, ) in &mut query
) in query.iter_mut()
{ {
let Sound { let Source { gain, .. } = *source;
gain,
pitch,
looping,
..
} = *sound;
assert!(gain >= 0.); assert!(gain >= 0.);
assert!(pitch >= 0.); if let Some(handle) = source.handle.as_mut() {
if sound.restart { handle.gain().set(gain).expect("Failed to set gain");
if let Some(generator) = sound.generator.as_mut() {
generator
.playback_position()
.set(0.)
.expect("Failed to restart");
}
sound.restart = false;
}
if sound.generator.is_none() {
let generator =
syz::BufferGenerator::new(&context).expect("Failed to create generator");
sound.generator = Some(generator);
}
let translation = global_transform
.map(|v| v.translation())
.or_else(|| transform.map(|v| v.translation));
if sound.source.is_none() {
if let Some(b) = buffers.get(&sound.buffer) {
if let Some(generator) = sound.generator.as_mut() {
generator.buffer().set(&**b).expect("Unable to set buffer");
if let Some(translation) = translation {
let panner_strategy = panner_strategy.cloned().unwrap_or_default();
let source = syz::Source3D::new(
&context,
*panner_strategy,
(
translation.x as f64,
translation.y as f64,
translation.z as f64,
),
)
.expect("Failed to create source");
source
.add_generator(generator)
.expect("Unable to add generator");
sound.source = Some(source.into());
} else if let Some(scalar_pan) = scalar_pan {
let panner_strategy = panner_strategy.cloned().unwrap_or_default();
let source =
syz::ScalarPannedSource::new(&context, *panner_strategy, **scalar_pan)
.expect("Failed to create source");
source
.add_generator(generator)
.expect("Failed to add generator");
sound.source = Some(source.into());
} else if let Some(angular_pan) = angular_pan {
let panner_strategy = panner_strategy.cloned().unwrap_or_default();
let source = syz::AngularPannedSource::new(
&context,
*panner_strategy,
angular_pan.azimuth,
angular_pan.elevation,
)
.expect("Failed to create source");
source
.add_generator(generator)
.expect("Failed to add generator");
sound.source = Some(source.into());
} else {
let source =
syz::DirectSource::new(&context).expect("Failed to create source");
source
.add_generator(generator)
.expect("Failed to add generator");
sound.source = Some(source.into());
}
}
}
}
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");
generator
.looping()
.set(looping)
.expect("Failed to set looping");
}
if let Some(source) = sound.source.as_mut() {
source.gain().set(gain).expect("Failed to set gain");
let mut clear_source = false; let mut clear_source = false;
if let Some(translation) = translation { if let Some(transform) = transform {
if let Some(source) = source.cast_to::<syz::Source3D>().unwrap() { if let Some(source) = handle.cast_to::<syz::Source3D>().unwrap() {
let translation = transform.translation();
source source
.position() .position()
.set(( .set((
@ -408,7 +461,7 @@ pub fn update_sound_properties(
clear_source = true; clear_source = true;
} }
} else if let Some(angular_pan) = angular_pan { } else if let Some(angular_pan) = angular_pan {
if let Some(source) = source.cast_to::<syz::AngularPannedSource>().unwrap() { if let Some(source) = handle.cast_to::<syz::AngularPannedSource>().unwrap() {
assert!(angular_pan.azimuth >= 0. && angular_pan.azimuth <= 360.); assert!(angular_pan.azimuth >= 0. && angular_pan.azimuth <= 360.);
source source
.azimuth() .azimuth()
@ -423,7 +476,7 @@ pub fn update_sound_properties(
clear_source = true; clear_source = true;
} }
} else if let Some(scalar_pan) = scalar_pan { } else if let Some(scalar_pan) = scalar_pan {
if let Some(source) = source.cast_to::<syz::ScalarPannedSource>().unwrap() { if let Some(source) = handle.cast_to::<syz::ScalarPannedSource>().unwrap() {
assert!(**scalar_pan >= -1. && **scalar_pan <= 1.); assert!(**scalar_pan >= -1. && **scalar_pan <= 1.);
source source
.panning_scalar() .panning_scalar()
@ -434,14 +487,59 @@ pub fn update_sound_properties(
} }
} }
if clear_source { if clear_source {
sound.source = None; source.handle = None;
} }
} }
} }
} }
pub fn update_playback_state(query: Query<&Sound>) { fn update_sound_properties(mut query: Query<&mut Sound>) {
for sound in query.iter() { for mut sound in &mut query {
let Sound {
gain,
pitch,
looping,
..
} = *sound;
assert!(gain >= 0.);
assert!(pitch >= 0.);
if sound.restart {
if let Some(generator) = sound.generator.as_mut() {
generator
.playback_position()
.set(0.)
.expect("Failed to restart");
}
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");
generator
.looping()
.set(looping)
.expect("Failed to set looping");
}
}
}
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 {
if let Some(generator) = &sound.generator { if let Some(generator) = &sound.generator {
if sound.paused { if sound.paused {
generator.pause().expect("Failed to pause"); generator.pause().expect("Failed to pause");
@ -452,13 +550,13 @@ pub fn update_playback_state(query: Query<&Sound>) {
} }
} }
fn remove_sound(mut last_buffer: ResMut<LastBuffer>, removed: RemovedComponents<Sound>) { fn remove_sound(mut last_buffer: ResMut<LastBuffer>, removed: RemovedComponents<Source>) {
for entity in removed.iter() { for entity in removed.iter() {
last_buffer.remove(&entity); last_buffer.remove(&entity);
} }
} }
#[derive(Clone, Copy, Default, Debug)] #[derive(Clone, Default, Debug)]
pub struct SynthizerConfig { pub struct SynthizerConfig {
pub default_panner_strategy: Option<syz::PannerStrategy>, pub default_panner_strategy: Option<syz::PannerStrategy>,
pub default_distance_model: Option<syz::DistanceModel>, pub default_distance_model: Option<syz::DistanceModel>,
@ -467,6 +565,8 @@ pub struct SynthizerConfig {
pub default_rolloff: Option<f64>, pub default_rolloff: Option<f64>,
pub default_closeness_boost: Option<f64>, pub default_closeness_boost: Option<f64>,
pub default_closeness_boost_distance: Option<f64>, pub default_closeness_boost_distance: Option<f64>,
pub log_level: syz::LogLevel,
pub log_to_stderr: bool,
} }
#[derive(Debug)] #[derive(Debug)]
@ -530,6 +630,8 @@ fn sync_config(
.unwrap_or(defaults.closeness_boost_distance), .unwrap_or(defaults.closeness_boost_distance),
) )
.expect("Failed to set closeness_boost_distance"); .expect("Failed to set closeness_boost_distance");
// syz::log_set_level(config.log_level);
// syz::log_to_stderr(config.log_to_stderr);
} }
} }
@ -540,7 +642,7 @@ fn events(
) { ) {
context.get_events().for_each(|event| { context.get_events().for_each(|event| {
if let Ok(event) = event { if let Ok(event) = event {
for (entity, sound) in sounds.iter() { for (entity, sound) in &sounds {
if let Some(generator) = &sound.generator { if let Some(generator) = &sound.generator {
if *generator.handle() == event.source { if *generator.handle() == event.source {
match event.r#type { match event.r#type {
@ -560,15 +662,29 @@ fn events(
}); });
} }
#[derive(SystemLabel, Clone, Hash, Debug, PartialEq, Eq)]
pub enum SynthizerSystems {
UpdateHandles,
UpdateProperties,
UpdateState,
}
pub struct SynthizerPlugin; pub struct SynthizerPlugin;
impl Plugin for SynthizerPlugin { impl Plugin for SynthizerPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
let guard = syz::initialize().expect("Failed to initialize Synthizer");
let context = syz::Context::new().expect("Failed to create Synthizer context");
if !app.world.contains_resource::<SynthizerConfig>() { if !app.world.contains_resource::<SynthizerConfig>() {
app.insert_resource(SynthizerConfig::default()); app.insert_resource(SynthizerConfig::default());
} }
let config = app
.world
.get_resource::<SynthizerConfig>()
.unwrap()
.clone();
// syz::log_set_level(config.log_level);
// syz::log_to_stderr(config.log_to_stderr);
let guard = syz::initialize().expect("Failed to initialize Synthizer");
let context = syz::Context::new().expect("Failed to create Synthizer context");
let defaults = SynthizerDefaults { let defaults = SynthizerDefaults {
panner_strategy: context.default_panner_strategy().get().unwrap(), panner_strategy: context.default_panner_strategy().get().unwrap(),
distance_model: context.default_distance_model().get().unwrap(), distance_model: context.default_distance_model().get().unwrap(),
@ -590,26 +706,59 @@ impl Plugin for SynthizerPlugin {
.add_system_to_stage(CoreStage::PreUpdate, sync_config) .add_system_to_stage(CoreStage::PreUpdate, sync_config)
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,
swap_buffers.before(update_sound_properties), swap_buffers.before(SynthizerSystems::UpdateHandles),
) )
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,
change_panner_strategy.before(update_sound_properties), change_panner_strategy.before(SynthizerSystems::UpdateHandles),
)
.add_system_to_stage(
CoreStage::PostUpdate,
add_source_handle
.label(SynthizerSystems::UpdateHandles)
.before(SynthizerSystems::UpdateProperties),
)
.add_system_to_stage(
CoreStage::PostUpdate,
add_generator
.label(SynthizerSystems::UpdateHandles)
.before(SynthizerSystems::UpdateProperties),
)
.add_system_to_stage(
CoreStage::PostUpdate,
add_sound_without_source
.label(SynthizerSystems::UpdateHandles)
.before(SynthizerSystems::UpdateProperties),
) )
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,
update_listener update_listener
.label(SynthizerSystems::UpdateProperties)
.after(TransformSystem::TransformPropagate) .after(TransformSystem::TransformPropagate)
.before(update_playback_state), .before(SynthizerSystems::UpdateState),
)
.add_system_to_stage(
CoreStage::PostUpdate,
update_source_properties
.label(SynthizerSystems::UpdateProperties)
.after(TransformSystem::TransformPropagate)
.before(SynthizerSystems::UpdateState),
) )
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,
update_sound_properties update_sound_properties
.after(TransformSystem::TransformPropagate) .label(SynthizerSystems::UpdateProperties)
.before(update_playback_state), .before(SynthizerSystems::UpdateState),
)
.add_system_to_stage(
CoreStage::PostUpdate,
update_source_playback_state.label(SynthizerSystems::UpdateState),
)
.add_system_to_stage(
CoreStage::PostUpdate,
update_sound_playback_state.label(SynthizerSystems::UpdateState),
) )
.add_system_to_stage(CoreStage::PostUpdate, update_playback_state)
.add_system_to_stage(CoreStage::PostUpdate, remove_sound) .add_system_to_stage(CoreStage::PostUpdate, remove_sound)
.add_system(events); .add_system_to_stage(CoreStage::PreUpdate, events);
} }
} }