Compare commits

...

4 Commits

2 changed files with 282 additions and 222 deletions

View File

@ -2,6 +2,20 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## Version 0.8.0 - 2024-12-02
### Bug Fixes
- Clear generator when source is cleared, and improve handling for changing source types.
### Features
- Add `Sound.playback_position` to support initializing new buffers at non-zero playback position.
### Miscellaneous Tasks
- Clean up code.
## Version 0.7.0 - 2024-07-07 ## Version 0.7.0 - 2024-07-07
### Miscellaneous Tasks ### Miscellaneous Tasks

View File

@ -156,6 +156,7 @@ pub struct Sound {
pub gain: f64, pub gain: f64,
pub pitch: f64, pub pitch: f64,
pub looping: bool, pub looping: bool,
pub playback_position: f64,
pub paused: bool, pub paused: bool,
pub generator: Option<syz::Generator>, pub generator: Option<syz::Generator>,
} }
@ -167,6 +168,7 @@ impl Default for Sound {
gain: 1., gain: 1.,
pitch: 1., pitch: 1.,
looping: false, looping: false,
playback_position: default(),
paused: false, paused: false,
generator: None, generator: None,
} }
@ -229,7 +231,9 @@ fn add_source_handle(
)>, )>,
) { ) {
for (mut source, panner_strategy, transform, angular_pan, scalar_pan) in &mut query { for (mut source, panner_strategy, transform, angular_pan, scalar_pan) in &mut query {
if source.handle.is_none() { if source.handle.is_some() {
continue;
}
let panner_strategy = panner_strategy.cloned().unwrap_or_default(); let panner_strategy = panner_strategy.cloned().unwrap_or_default();
let handle: syz::Source = if let Some(transform) = transform { let handle: syz::Source = if let Some(transform) = transform {
let translation = transform.translation(); let translation = transform.translation();
@ -265,7 +269,6 @@ fn add_source_handle(
source.handle = Some(handle); source.handle = Some(handle);
} }
} }
}
fn add_generator( fn add_generator(
context: Res<Context>, context: Res<Context>,
@ -275,7 +278,9 @@ fn add_generator(
parents: Query<&Parent>, parents: Query<&Parent>,
) { ) {
for (entity, parent, mut sound) in &mut query { for (entity, parent, mut sound) in &mut query {
if sound.generator.is_none() { if sound.generator.is_some() {
continue;
}
let mut source = if let Ok(s) = sources.get_mut(entity) { let mut source = if let Ok(s) = sources.get_mut(entity) {
Some(s) Some(s)
} else if parent.is_some() { } else if parent.is_some() {
@ -300,6 +305,11 @@ fn add_generator(
let generator = syz::BufferGenerator::new(&context) let generator = syz::BufferGenerator::new(&context)
.expect("Failed to create generator"); .expect("Failed to create generator");
generator.buffer().set(&**b).expect("Unable to set buffer"); generator.buffer().set(&**b).expect("Unable to set buffer");
assert!(sound.playback_position >= 0.);
generator
.playback_position()
.set(sound.playback_position)
.expect("Failed to set playback position");
Some(generator.into()) Some(generator.into())
} else { } else {
None None
@ -307,7 +317,9 @@ fn add_generator(
} }
Audio::Generator(generator) => Some(generator.clone()), Audio::Generator(generator) => Some(generator.clone()),
}; };
if let Some(generator) = generator { let Some(generator) = generator else {
continue;
};
assert!(sound.gain >= 0.); assert!(sound.gain >= 0.);
generator generator
.gain() .gain()
@ -326,8 +338,6 @@ fn add_generator(
} }
} }
} }
}
}
fn add_sound_without_source( fn add_sound_without_source(
mut commands: Commands, mut commands: Commands,
@ -361,6 +371,7 @@ fn swap_buffers(
if let Some(l) = last_audio.get(&entity) { if let Some(l) = last_audio.get(&entity) {
if sound.generator.is_some() && sound.audio != *l { if sound.generator.is_some() && sound.audio != *l {
sound.generator = None; sound.generator = None;
sound.playback_position = 0.;
} }
} }
last_audio.insert(entity, sound.audio.clone()); last_audio.insert(entity, sound.audio.clone());
@ -382,13 +393,14 @@ fn change_panner_strategy(
check.push(entity); check.push(entity);
} }
for entity in check.iter() { for entity in check.iter() {
if let Ok(mut source) = sources.get_mut(*entity) { let Ok(mut source) = sources.get_mut(*entity) else {
continue;
};
if source.handle.is_some() { if source.handle.is_some() {
source.handle = None; source.handle = None;
} }
} }
} }
}
fn update_source_properties( fn update_source_properties(
context: Res<Context>, context: Res<Context>,
@ -403,6 +415,7 @@ fn update_source_properties(
Option<&AngularPan>, Option<&AngularPan>,
Option<&ScalarPan>, Option<&ScalarPan>,
Option<&GlobalTransform>, Option<&GlobalTransform>,
Option<&mut Sound>,
)>, )>,
) { ) {
for ( for (
@ -416,15 +429,18 @@ fn update_source_properties(
angular_pan, angular_pan,
scalar_pan, scalar_pan,
transform, transform,
sound,
) in &mut query ) in &mut query
{ {
let Source { gain, .. } = *source; let Source { gain, .. } = *source;
assert!(gain >= 0.); assert!(gain >= 0.);
if let Some(handle) = source.handle.as_mut() { let Some(handle) = source.handle.as_mut() else {
continue;
};
handle.gain().set(gain).expect("Failed to set gain"); handle.gain().set(gain).expect("Failed to set gain");
let mut clear_source = false; let mut clear_source = false;
if let Some(transform) = transform {
if let Some(source) = handle.cast_to::<syz::Source3D>().expect("Failed to cast") { if let Some(source) = handle.cast_to::<syz::Source3D>().expect("Failed to cast") {
if let Some(transform) = transform {
let translation = transform.translation(); let translation = transform.translation();
source source
.position() .position()
@ -462,6 +478,7 @@ fn update_source_properties(
.map(|v| **v) .map(|v| **v)
.unwrap_or_else(|| context.default_rolloff().get().unwrap()); .unwrap_or_else(|| context.default_rolloff().get().unwrap());
assert!(rolloff >= 0.); assert!(rolloff >= 0.);
assert!(rolloff >= 0.);
source source
.rolloff() .rolloff()
.set(rolloff) .set(rolloff)
@ -474,10 +491,9 @@ fn update_source_properties(
.closeness_boost() .closeness_boost()
.set(closeness_boost) .set(closeness_boost)
.expect("Failed to set closeness_boost"); .expect("Failed to set closeness_boost");
let closeness_boost_distance = let closeness_boost_distance = closeness_boost_distance
closeness_boost_distance.map(|v| **v).unwrap_or_else(|| { .map(|v| **v)
context.default_closeness_boost_distance().get().unwrap() .unwrap_or_else(|| context.default_closeness_boost_distance().get().unwrap());
});
assert!(closeness_boost_distance >= 0.); assert!(closeness_boost_distance >= 0.);
source source
.closeness_boost_distance() .closeness_boost_distance()
@ -486,11 +502,11 @@ fn update_source_properties(
} else { } else {
clear_source = true; clear_source = true;
} }
} else if let Some(angular_pan) = angular_pan { } else if let Some(source) = handle
if let Some(source) = handle
.cast_to::<syz::AngularPannedSource>() .cast_to::<syz::AngularPannedSource>()
.expect("Failed to cast") .expect("Failed to cast")
{ {
if let Some(angular_pan) = angular_pan {
assert!(angular_pan.azimuth >= 0. && angular_pan.azimuth <= 360.); assert!(angular_pan.azimuth >= 0. && angular_pan.azimuth <= 360.);
source source
.azimuth() .azimuth()
@ -504,11 +520,11 @@ fn update_source_properties(
} else { } else {
clear_source = true; clear_source = true;
} }
} else if let Some(scalar_pan) = scalar_pan { } else if let Some(source) = handle
if let Some(source) = handle
.cast_to::<syz::ScalarPannedSource>() .cast_to::<syz::ScalarPannedSource>()
.expect("Failed to cast") .expect("Failed to cast")
{ {
if let Some(scalar_pan) = scalar_pan {
assert!(**scalar_pan >= -1. && **scalar_pan <= 1.); assert!(**scalar_pan >= -1. && **scalar_pan <= 1.);
source source
.panning_scalar() .panning_scalar()
@ -517,9 +533,22 @@ fn update_source_properties(
} else { } else {
clear_source = true; clear_source = true;
} }
} else if handle
.cast_to::<syz::DirectSource>()
.expect("Failed to cast")
.is_some()
{
if transform.is_some() || angular_pan.is_some() || scalar_pan.is_some() {
clear_source = true;
}
} else if source.handle.is_some() {
clear_source = true;
} }
if clear_source { if clear_source {
source.handle = None; source.handle = None;
if let Some(mut sound) = sound {
sound.generator = None;
sound.playback_position = 0.;
} }
} }
} }
@ -535,7 +564,21 @@ fn update_sound_properties(mut query: Query<&mut Sound>) {
} = *sound; } = *sound;
assert!(gain >= 0.); assert!(gain >= 0.);
assert!(pitch > 0. && pitch <= 2.); assert!(pitch > 0. && pitch <= 2.);
if let Some(generator) = sound.generator.as_mut() { let Some(generator) = &sound.generator else {
continue;
};
if let Some(generator) = generator
.cast_to::<syz::BufferGenerator>()
.expect("Failed to cast")
{
sound.playback_position = generator
.playback_position()
.get()
.expect("Failed to getplayback position");
}
let Some(generator) = sound.generator.as_mut() else {
continue;
};
generator.gain().set(gain).expect("Failed to set gain"); generator.gain().set(gain).expect("Failed to set gain");
generator generator
.pitch_bend() .pitch_bend()
@ -552,11 +595,12 @@ fn update_sound_properties(mut query: Query<&mut Sound>) {
} }
} }
} }
}
fn update_source_playback_state(query: Query<&Source>) { fn update_source_playback_state(query: Query<&Source>) {
for source in &query { for source in &query {
if let Some(handle) = &source.handle { let Some(handle) = &source.handle else {
continue;
};
if source.paused { if source.paused {
handle.pause().expect("Failed to pause"); handle.pause().expect("Failed to pause");
} else { } else {
@ -564,11 +608,12 @@ fn update_source_playback_state(query: Query<&Source>) {
} }
} }
} }
}
fn update_sound_playback_state(query: Query<&Sound>) { fn update_sound_playback_state(query: Query<&Sound>) {
for sound in &query { for sound in &query {
if let Some(generator) = &sound.generator { let Some(generator) = &sound.generator else {
continue;
};
if sound.paused { if sound.paused {
generator.pause().expect("Failed to pause"); generator.pause().expect("Failed to pause");
} else { } else {
@ -576,7 +621,6 @@ fn update_sound_playback_state(query: Query<&Sound>) {
} }
} }
} }
}
fn remove_sound(mut last_buffer: ResMut<LastAudio>, mut removed: RemovedComponents<Sound>) { fn remove_sound(mut last_buffer: ResMut<LastAudio>, mut removed: RemovedComponents<Sound>) {
for entity in removed.read() { for entity in removed.read() {
@ -654,9 +698,13 @@ fn events(
mut output: EventWriter<SynthizerEvent>, mut output: EventWriter<SynthizerEvent>,
) { ) {
context.get_events().for_each(|event| { context.get_events().for_each(|event| {
if let Ok(event) = event { let Ok(event) = event else {
return;
};
for (entity, sound) in &sounds { for (entity, sound) in &sounds {
if let Some(generator) = &sound.generator { let Some(generator) = &sound.generator else {
continue;
};
if *generator.handle() == event.source { if *generator.handle() == event.source {
match event.r#type { match event.r#type {
syz::EventType::Finished => { syz::EventType::Finished => {
@ -670,8 +718,6 @@ fn events(
break; break;
} }
} }
}
}
}); });
} }