Compare commits

..

No commits in common. "4c1471c2abed1003815ccc4adba61f6fcc514979" and "2553ea9bc2fdb0ba806528a621556172ed2366ef" have entirely different histories.

View File

@ -153,6 +153,7 @@ pub struct Sound {
pub pitch: f64,
pub looping: bool,
pub paused: bool,
pub restart: bool,
pub generator: Option<syz::Generator>,
}
@ -164,6 +165,7 @@ impl Default for Sound {
pitch: 1.,
looping: false,
paused: false,
restart: false,
generator: None,
}
}
@ -274,8 +276,8 @@ fn add_generator(
if sound.generator.is_none() {
let mut source = if let Ok(s) = sources.get_mut(entity) {
Some(s)
} else if parent.is_some() {
let mut parent = parent;
} 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() {
@ -355,7 +357,7 @@ fn swap_buffers(
) {
for (entity, mut sound) in &mut query {
if let Some(l) = last_audio.get(&entity) {
if sound.generator.is_some() && sound.audio != *l {
if sound.audio != *l {
sound.generator = None;
}
}
@ -531,6 +533,20 @@ 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
@ -574,7 +590,7 @@ 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<Source>) {
for entity in removed.iter() {
last_buffer.remove(&entity);
}
@ -651,9 +667,11 @@ 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));
@ -667,13 +685,16 @@ fn events(
}
}
}
if !matched {
println!("No match");
}
}
});
}
#[derive(SystemSet, Clone, Hash, Debug, PartialEq, Eq)]
pub enum SynthizerSets {
PreUpdate,
First,
UpdateHandles,
UpdateProperties,
UpdateState,
@ -740,12 +761,21 @@ impl Plugin for SynthizerPlugin {
.add_event::<SynthizerEvent>()
.add_systems(
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(
PostUpdate,
(add_sound_without_source, add_source_handle, add_generator)
.in_set(SynthizerSets::UpdateHandles),
(add_source_handle, add_generator).in_set(SynthizerSets::UpdateHandles),
)
.configure_set(
PostUpdate,