Compare commits

...

4 Commits

View File

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