Compare commits

...

4 Commits

View File

@ -153,7 +153,6 @@ pub struct Sound {
pub pitch: f64, pub pitch: f64,
pub looping: bool, pub looping: bool,
pub paused: bool, pub paused: bool,
pub restart: bool,
pub generator: Option<syz::Generator>, pub generator: Option<syz::Generator>,
} }
@ -165,7 +164,6 @@ impl Default for Sound {
pitch: 1., pitch: 1.,
looping: false, looping: false,
paused: false, paused: false,
restart: false,
generator: None, generator: None,
} }
} }
@ -276,8 +274,8 @@ fn add_generator(
if sound.generator.is_none() { if sound.generator.is_none() {
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 let Some(parent) = parent { } else if parent.is_some() {
let mut parent: Option<&Parent> = Some(parent); let mut parent = parent;
let mut target = None; let mut target = None;
while let Some(p) = parent { while let Some(p) = parent {
if sources.get(**p).is_ok() { if sources.get(**p).is_ok() {
@ -357,7 +355,7 @@ fn swap_buffers(
) { ) {
for (entity, mut sound) in &mut query { for (entity, mut sound) in &mut query {
if let Some(l) = last_audio.get(&entity) { if let Some(l) = last_audio.get(&entity) {
if sound.audio != *l { if sound.generator.is_some() && sound.audio != *l {
sound.generator = None; sound.generator = None;
} }
} }
@ -533,20 +531,6 @@ 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 sound.restart {
if let Some(generator) = sound.generator.as_mut() {
if let Some(generator) = generator
.cast_to::<syz::BufferGenerator>()
.expect("Failed to cast")
{
generator
.playback_position()
.set(0.)
.expect("Failed to restart");
}
}
sound.restart = false;
}
if let Some(generator) = sound.generator.as_mut() { if let Some(generator) = sound.generator.as_mut() {
generator.gain().set(gain).expect("Failed to set gain"); generator.gain().set(gain).expect("Failed to set gain");
generator generator
@ -590,7 +574,7 @@ fn update_sound_playback_state(query: Query<&Sound>) {
} }
} }
fn remove_sound(mut last_buffer: ResMut<LastAudio>, mut removed: RemovedComponents<Source>) { fn remove_sound(mut last_buffer: ResMut<LastAudio>, mut removed: RemovedComponents<Sound>) {
for entity in removed.iter() { for entity in removed.iter() {
last_buffer.remove(&entity); last_buffer.remove(&entity);
} }
@ -667,11 +651,9 @@ fn events(
) { ) {
context.get_events().for_each(|event| { context.get_events().for_each(|event| {
if let Ok(event) = event { if let Ok(event) = event {
let mut matched = false;
for (entity, sound) in &sounds { 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 {
matched = true;
match event.r#type { match event.r#type {
syz::EventType::Finished => { syz::EventType::Finished => {
output.send(SynthizerEvent::Finished(entity)); output.send(SynthizerEvent::Finished(entity));
@ -685,16 +667,13 @@ fn events(
} }
} }
} }
if !matched {
println!("No match");
}
} }
}); });
} }
#[derive(SystemSet, Clone, Hash, Debug, PartialEq, Eq)] #[derive(SystemSet, Clone, Hash, Debug, PartialEq, Eq)]
pub enum SynthizerSets { pub enum SynthizerSets {
First, PreUpdate,
UpdateHandles, UpdateHandles,
UpdateProperties, UpdateProperties,
UpdateState, UpdateState,
@ -761,21 +740,12 @@ impl Plugin for SynthizerPlugin {
.add_event::<SynthizerEvent>() .add_event::<SynthizerEvent>()
.add_systems( .add_systems(
PreUpdate, PreUpdate,
( (sync_config, swap_buffers, change_panner_strategy).in_set(SynthizerSets::PreUpdate),
sync_config,
swap_buffers,
change_panner_strategy,
add_sound_without_source,
)
.in_set(SynthizerSets::First),
)
.configure_set(
PreUpdate,
SynthizerSets::First.before(SynthizerSets::UpdateHandles),
) )
.add_systems( .add_systems(
PostUpdate, PostUpdate,
(add_source_handle, add_generator).in_set(SynthizerSets::UpdateHandles), (add_sound_without_source, add_source_handle, add_generator)
.in_set(SynthizerSets::UpdateHandles),
) )
.configure_set( .configure_set(
PostUpdate, PostUpdate,