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 {
|
for (mut source, panner_strategy, transform, angular_pan, scalar_pan) in &mut query {
|
||||||
if source.handle.is_none() {
|
if source.handle.is_some() {
|
||||||
let panner_strategy = panner_strategy.cloned().unwrap_or_default();
|
continue;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
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>,
|
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() {
|
||||||
let mut source = if let Ok(s) = sources.get_mut(entity) {
|
continue;
|
||||||
Some(s)
|
}
|
||||||
} else if parent.is_some() {
|
let mut source = if let Ok(s) = sources.get_mut(entity) {
|
||||||
let mut parent = parent;
|
Some(s)
|
||||||
let mut target = None;
|
} else if parent.is_some() {
|
||||||
while let Some(p) = parent {
|
let mut parent = parent;
|
||||||
if sources.get(**p).is_ok() {
|
let mut target = None;
|
||||||
target = Some(**p);
|
while let Some(p) = parent {
|
||||||
break;
|
if sources.get(**p).is_ok() {
|
||||||
}
|
target = Some(**p);
|
||||||
parent = parents.get(**p).ok();
|
break;
|
||||||
}
|
}
|
||||||
target.map(|v| sources.get_mut(v).unwrap())
|
parent = parents.get(**p).ok();
|
||||||
} else {
|
}
|
||||||
None
|
target.map(|v| sources.get_mut(v).unwrap())
|
||||||
};
|
} else {
|
||||||
if let Some(source) = source.as_mut() {
|
None
|
||||||
if let Some(handle) = source.handle.as_mut() {
|
};
|
||||||
let generator: Option<syz::Generator> = match &sound.audio {
|
if let Some(source) = source.as_mut() {
|
||||||
Audio::Buffer(buffer) => {
|
if let Some(handle) = source.handle.as_mut() {
|
||||||
if let Some(b) = buffers.get(buffer) {
|
let generator: Option<syz::Generator> = match &sound.audio {
|
||||||
let generator = syz::BufferGenerator::new(&context)
|
Audio::Buffer(buffer) => {
|
||||||
.expect("Failed to create generator");
|
if let Some(b) = buffers.get(buffer) {
|
||||||
generator.buffer().set(&**b).expect("Unable to set buffer");
|
let generator = syz::BufferGenerator::new(&context)
|
||||||
Some(generator.into())
|
.expect("Failed to create generator");
|
||||||
} else {
|
generator.buffer().set(&**b).expect("Unable to set buffer");
|
||||||
None
|
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);
|
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 {
|
||||||
if source.handle.is_some() {
|
continue;
|
||||||
source.handle = None;
|
};
|
||||||
}
|
if source.handle.is_some() {
|
||||||
|
source.handle = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -552,45 +557,48 @@ 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 {
|
||||||
generator.gain().set(gain).expect("Failed to set gain");
|
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
|
generator
|
||||||
.pitch_bend()
|
.looping()
|
||||||
.set(pitch)
|
.set(looping)
|
||||||
.expect("Failed to set pitch");
|
.expect("Failed to set looping");
|
||||||
if let Some(generator) = generator
|
|
||||||
.cast_to::<syz::BufferGenerator>()
|
|
||||||
.expect("Failed to cast")
|
|
||||||
{
|
|
||||||
generator
|
|
||||||
.looping()
|
|
||||||
.set(looping)
|
|
||||||
.expect("Failed to set looping");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
if source.paused {
|
continue;
|
||||||
handle.pause().expect("Failed to pause");
|
};
|
||||||
} else {
|
if source.paused {
|
||||||
handle.play().expect("Failed to play");
|
handle.pause().expect("Failed to pause");
|
||||||
}
|
} else {
|
||||||
|
handle.play().expect("Failed to play");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
if sound.paused {
|
continue;
|
||||||
generator.pause().expect("Failed to pause");
|
};
|
||||||
} else {
|
if sound.paused {
|
||||||
generator.play().expect("Failed to play");
|
generator.pause().expect("Failed to pause");
|
||||||
}
|
} else {
|
||||||
|
generator.play().expect("Failed to play");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -671,22 +679,24 @@ 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 {
|
||||||
for (entity, sound) in &sounds {
|
return;
|
||||||
if let Some(generator) = &sound.generator {
|
};
|
||||||
if *generator.handle() == event.source {
|
for (entity, sound) in &sounds {
|
||||||
match event.r#type {
|
let Some(generator) = &sound.generator else {
|
||||||
syz::EventType::Finished => {
|
continue;
|
||||||
output.send(SynthizerEvent::Finished(entity));
|
};
|
||||||
}
|
if *generator.handle() == event.source {
|
||||||
syz::EventType::Looped => {
|
match event.r#type {
|
||||||
output.send(SynthizerEvent::Looped(entity));
|
syz::EventType::Finished => {
|
||||||
}
|
output.send(SynthizerEvent::Finished(entity));
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
syz::EventType::Looped => {
|
||||||
|
output.send(SynthizerEvent::Looped(entity));
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user