chore: Clean up code.
This commit is contained in:
parent
4c02a98eb4
commit
45746803c9
258
src/lib.rs
258
src/lib.rs
|
@ -229,41 +229,43 @@ fn add_source_handle(
|
|||
)>,
|
||||
) {
|
||||
for (mut source, panner_strategy, transform, angular_pan, scalar_pan) in &mut query {
|
||||
if source.handle.is_none() {
|
||||
let panner_strategy = panner_strategy.cloned().unwrap_or_default();
|
||||
let handle: syz::Source = if let Some(transform) = transform {
|
||||
let translation = transform.translation();
|
||||
syz::Source3D::new(
|
||||
&context,
|
||||
*panner_strategy,
|
||||
(
|
||||
translation.x as f64,
|
||||
translation.y as f64,
|
||||
translation.z as f64,
|
||||
),
|
||||
)
|
||||
.expect("Failed to create source")
|
||||
.into()
|
||||
} else if let Some(scalar_pan) = scalar_pan {
|
||||
syz::ScalarPannedSource::new(&context, *panner_strategy, **scalar_pan)
|
||||
.expect("Failed to create source")
|
||||
.into()
|
||||
} else if let Some(angular_pan) = angular_pan {
|
||||
syz::AngularPannedSource::new(
|
||||
&context,
|
||||
*panner_strategy,
|
||||
angular_pan.azimuth,
|
||||
angular_pan.elevation,
|
||||
)
|
||||
.expect("Failed to create source")
|
||||
.into()
|
||||
} else {
|
||||
syz::DirectSource::new(&context)
|
||||
.expect("Failed to create source")
|
||||
.into()
|
||||
};
|
||||
source.handle = Some(handle);
|
||||
if source.handle.is_some() {
|
||||
continue;
|
||||
}
|
||||
let panner_strategy = panner_strategy.cloned().unwrap_or_default();
|
||||
let handle: syz::Source = if let Some(transform) = transform {
|
||||
let translation = transform.translation();
|
||||
syz::Source3D::new(
|
||||
&context,
|
||||
*panner_strategy,
|
||||
(
|
||||
translation.x as f64,
|
||||
translation.y as f64,
|
||||
translation.z as f64,
|
||||
),
|
||||
)
|
||||
.expect("Failed to create source")
|
||||
.into()
|
||||
} else if let Some(scalar_pan) = scalar_pan {
|
||||
syz::ScalarPannedSource::new(&context, *panner_strategy, **scalar_pan)
|
||||
.expect("Failed to create source")
|
||||
.into()
|
||||
} else if let Some(angular_pan) = angular_pan {
|
||||
syz::AngularPannedSource::new(
|
||||
&context,
|
||||
*panner_strategy,
|
||||
angular_pan.azimuth,
|
||||
angular_pan.elevation,
|
||||
)
|
||||
.expect("Failed to create source")
|
||||
.into()
|
||||
} else {
|
||||
println!("Adding new direct source");
|
||||
syz::DirectSource::new(&context)
|
||||
.expect("Failed to create source")
|
||||
.into()
|
||||
};
|
||||
source.handle = Some(handle);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -275,55 +277,57 @@ fn add_generator(
|
|||
parents: Query<&Parent>,
|
||||
) {
|
||||
for (entity, parent, mut sound) in &mut query {
|
||||
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;
|
||||
let mut target = None;
|
||||
while let Some(p) = parent {
|
||||
if sources.get(**p).is_ok() {
|
||||
target = Some(**p);
|
||||
break;
|
||||
}
|
||||
parent = parents.get(**p).ok();
|
||||
if sound.generator.is_some() {
|
||||
continue;
|
||||
}
|
||||
let mut source = if let Ok(s) = sources.get_mut(entity) {
|
||||
Some(s)
|
||||
} else if parent.is_some() {
|
||||
let mut parent = parent;
|
||||
let mut target = None;
|
||||
while let Some(p) = parent {
|
||||
if sources.get(**p).is_ok() {
|
||||
target = Some(**p);
|
||||
break;
|
||||
}
|
||||
target.map(|v| sources.get_mut(v).unwrap())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some(source) = source.as_mut() {
|
||||
if let Some(handle) = source.handle.as_mut() {
|
||||
let generator: Option<syz::Generator> = match &sound.audio {
|
||||
Audio::Buffer(buffer) => {
|
||||
if let Some(b) = buffers.get(buffer) {
|
||||
let generator = syz::BufferGenerator::new(&context)
|
||||
.expect("Failed to create generator");
|
||||
generator.buffer().set(&**b).expect("Unable to set buffer");
|
||||
Some(generator.into())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
parent = parents.get(**p).ok();
|
||||
}
|
||||
target.map(|v| sources.get_mut(v).unwrap())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some(source) = source.as_mut() {
|
||||
if let Some(handle) = source.handle.as_mut() {
|
||||
let generator: Option<syz::Generator> = match &sound.audio {
|
||||
Audio::Buffer(buffer) => {
|
||||
if let Some(b) = buffers.get(buffer) {
|
||||
let generator = syz::BufferGenerator::new(&context)
|
||||
.expect("Failed to create generator");
|
||||
generator.buffer().set(&**b).expect("Unable to set buffer");
|
||||
Some(generator.into())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
Audio::Generator(generator) => Some(generator.clone()),
|
||||
};
|
||||
if let Some(generator) = generator {
|
||||
assert!(sound.gain >= 0.);
|
||||
generator
|
||||
.gain()
|
||||
.set(sound.gain)
|
||||
.expect("Failed to set gain");
|
||||
assert!(sound.pitch > 0. && sound.pitch <= 2.);
|
||||
generator
|
||||
.pitch_bend()
|
||||
.set(sound.pitch)
|
||||
.expect("Failed to set pitch");
|
||||
handle
|
||||
.add_generator(generator.handle())
|
||||
.expect("Unable to add generator");
|
||||
sound.generator = Some(generator);
|
||||
}
|
||||
}
|
||||
Audio::Generator(generator) => Some(generator.clone()),
|
||||
};
|
||||
let Some(generator) = generator else {
|
||||
continue;
|
||||
};
|
||||
assert!(sound.gain >= 0.);
|
||||
generator
|
||||
.gain()
|
||||
.set(sound.gain)
|
||||
.expect("Failed to set gain");
|
||||
assert!(sound.pitch > 0. && sound.pitch <= 2.);
|
||||
generator
|
||||
.pitch_bend()
|
||||
.set(sound.pitch)
|
||||
.expect("Failed to set pitch");
|
||||
handle
|
||||
.add_generator(generator.handle())
|
||||
.expect("Unable to add generator");
|
||||
sound.generator = Some(generator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -382,10 +386,11 @@ fn change_panner_strategy(
|
|||
check.push(entity);
|
||||
}
|
||||
for entity in check.iter() {
|
||||
if let Ok(mut source) = sources.get_mut(*entity) {
|
||||
if source.handle.is_some() {
|
||||
source.handle = None;
|
||||
}
|
||||
let Ok(mut source) = sources.get_mut(*entity) else {
|
||||
continue;
|
||||
};
|
||||
if source.handle.is_some() {
|
||||
source.handle = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -552,45 +557,48 @@ fn update_sound_properties(mut query: Query<&mut Sound>) {
|
|||
} = *sound;
|
||||
assert!(gain >= 0.);
|
||||
assert!(pitch > 0. && pitch <= 2.);
|
||||
if let Some(generator) = sound.generator.as_mut() {
|
||||
generator.gain().set(gain).expect("Failed to set gain");
|
||||
let Some(generator) = sound.generator.as_mut() else {
|
||||
continue;
|
||||
};
|
||||
generator.gain().set(gain).expect("Failed to set gain");
|
||||
generator
|
||||
.pitch_bend()
|
||||
.set(pitch)
|
||||
.expect("Failed to set pitch");
|
||||
if let Some(generator) = generator
|
||||
.cast_to::<syz::BufferGenerator>()
|
||||
.expect("Failed to cast")
|
||||
{
|
||||
generator
|
||||
.pitch_bend()
|
||||
.set(pitch)
|
||||
.expect("Failed to set pitch");
|
||||
if let Some(generator) = generator
|
||||
.cast_to::<syz::BufferGenerator>()
|
||||
.expect("Failed to cast")
|
||||
{
|
||||
generator
|
||||
.looping()
|
||||
.set(looping)
|
||||
.expect("Failed to set looping");
|
||||
}
|
||||
.looping()
|
||||
.set(looping)
|
||||
.expect("Failed to set looping");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_source_playback_state(query: Query<&Source>) {
|
||||
for source in &query {
|
||||
if let Some(handle) = &source.handle {
|
||||
if source.paused {
|
||||
handle.pause().expect("Failed to pause");
|
||||
} else {
|
||||
handle.play().expect("Failed to play");
|
||||
}
|
||||
let Some(handle) = &source.handle else {
|
||||
continue;
|
||||
};
|
||||
if source.paused {
|
||||
handle.pause().expect("Failed to pause");
|
||||
} else {
|
||||
handle.play().expect("Failed to play");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_sound_playback_state(query: Query<&Sound>) {
|
||||
for sound in &query {
|
||||
if let Some(generator) = &sound.generator {
|
||||
if sound.paused {
|
||||
generator.pause().expect("Failed to pause");
|
||||
} else {
|
||||
generator.play().expect("Failed to play");
|
||||
}
|
||||
let Some(generator) = &sound.generator else {
|
||||
continue;
|
||||
};
|
||||
if sound.paused {
|
||||
generator.pause().expect("Failed to pause");
|
||||
} else {
|
||||
generator.play().expect("Failed to play");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -671,22 +679,24 @@ fn events(
|
|||
mut output: EventWriter<SynthizerEvent>,
|
||||
) {
|
||||
context.get_events().for_each(|event| {
|
||||
if let Ok(event) = event {
|
||||
for (entity, sound) in &sounds {
|
||||
if let Some(generator) = &sound.generator {
|
||||
if *generator.handle() == event.source {
|
||||
match event.r#type {
|
||||
syz::EventType::Finished => {
|
||||
output.send(SynthizerEvent::Finished(entity));
|
||||
}
|
||||
syz::EventType::Looped => {
|
||||
output.send(SynthizerEvent::Looped(entity));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
break;
|
||||
let Ok(event) = event else {
|
||||
return;
|
||||
};
|
||||
for (entity, sound) in &sounds {
|
||||
let Some(generator) = &sound.generator else {
|
||||
continue;
|
||||
};
|
||||
if *generator.handle() == event.source {
|
||||
match event.r#type {
|
||||
syz::EventType::Finished => {
|
||||
output.send(SynthizerEvent::Finished(entity));
|
||||
}
|
||||
syz::EventType::Looped => {
|
||||
output.send(SynthizerEvent::Looped(entity));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user