chore: Clean up code.
This commit is contained in:
parent
4c02a98eb4
commit
45746803c9
46
src/lib.rs
46
src/lib.rs
|
@ -229,7 +229,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();
|
||||||
|
@ -258,6 +260,7 @@ fn add_source_handle(
|
||||||
.expect("Failed to create source")
|
.expect("Failed to create source")
|
||||||
.into()
|
.into()
|
||||||
} else {
|
} else {
|
||||||
|
println!("Adding new direct source");
|
||||||
syz::DirectSource::new(&context)
|
syz::DirectSource::new(&context)
|
||||||
.expect("Failed to create source")
|
.expect("Failed to create source")
|
||||||
.into()
|
.into()
|
||||||
|
@ -265,7 +268,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 +277,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() {
|
||||||
|
@ -307,7 +311,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 +332,6 @@ fn add_generator(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_sound_without_source(
|
fn add_sound_without_source(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
|
@ -382,13 +386,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>,
|
||||||
|
@ -552,7 +557,9 @@ 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.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()
|
||||||
|
@ -569,11 +576,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 {
|
||||||
|
@ -581,11 +589,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 {
|
||||||
|
@ -593,7 +602,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() {
|
||||||
|
@ -671,9 +679,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 => {
|
||||||
|
@ -687,8 +699,6 @@ fn events(
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user