Compare commits
4 Commits
2364f638db
...
0cb5bd59f6
Author | SHA1 | Date | |
---|---|---|---|
0cb5bd59f6 | |||
041165cf61 | |||
45746803c9 | |||
4c02a98eb4 |
14
CHANGELOG.md
14
CHANGELOG.md
|
@ -2,6 +2,20 @@
|
|||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## Version 0.8.0 - 2024-12-02
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- Clear generator when source is cleared, and improve handling for changing source types.
|
||||
|
||||
### Features
|
||||
|
||||
- Add `Sound.playback_position` to support initializing new buffers at non-zero playback position.
|
||||
|
||||
### Miscellaneous Tasks
|
||||
|
||||
- Clean up code.
|
||||
|
||||
## Version 0.7.0 - 2024-07-07
|
||||
|
||||
### Miscellaneous Tasks
|
||||
|
|
490
src/lib.rs
490
src/lib.rs
|
@ -156,6 +156,7 @@ pub struct Sound {
|
|||
pub gain: f64,
|
||||
pub pitch: f64,
|
||||
pub looping: bool,
|
||||
pub playback_position: f64,
|
||||
pub paused: bool,
|
||||
pub generator: Option<syz::Generator>,
|
||||
}
|
||||
|
@ -167,6 +168,7 @@ impl Default for Sound {
|
|||
gain: 1.,
|
||||
pitch: 1.,
|
||||
looping: false,
|
||||
playback_position: default(),
|
||||
paused: false,
|
||||
generator: None,
|
||||
}
|
||||
|
@ -229,41 +231,42 @@ 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 {
|
||||
syz::DirectSource::new(&context)
|
||||
.expect("Failed to create source")
|
||||
.into()
|
||||
};
|
||||
source.handle = Some(handle);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -275,55 +278,62 @@ 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");
|
||||
assert!(sound.playback_position >= 0.);
|
||||
generator
|
||||
.playback_position()
|
||||
.set(sound.playback_position)
|
||||
.expect("Failed to set playback position");
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -361,6 +371,7 @@ fn swap_buffers(
|
|||
if let Some(l) = last_audio.get(&entity) {
|
||||
if sound.generator.is_some() && sound.audio != *l {
|
||||
sound.generator = None;
|
||||
sound.playback_position = 0.;
|
||||
}
|
||||
}
|
||||
last_audio.insert(entity, sound.audio.clone());
|
||||
|
@ -382,10 +393,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -403,6 +415,7 @@ fn update_source_properties(
|
|||
Option<&AngularPan>,
|
||||
Option<&ScalarPan>,
|
||||
Option<&GlobalTransform>,
|
||||
Option<&mut Sound>,
|
||||
)>,
|
||||
) {
|
||||
for (
|
||||
|
@ -416,110 +429,126 @@ fn update_source_properties(
|
|||
angular_pan,
|
||||
scalar_pan,
|
||||
transform,
|
||||
sound,
|
||||
) in &mut query
|
||||
{
|
||||
let Source { gain, .. } = *source;
|
||||
assert!(gain >= 0.);
|
||||
if let Some(handle) = source.handle.as_mut() {
|
||||
handle.gain().set(gain).expect("Failed to set gain");
|
||||
let mut clear_source = false;
|
||||
let Some(handle) = source.handle.as_mut() else {
|
||||
continue;
|
||||
};
|
||||
handle.gain().set(gain).expect("Failed to set gain");
|
||||
let mut clear_source = false;
|
||||
if let Some(source) = handle.cast_to::<syz::Source3D>().expect("Failed to cast") {
|
||||
if let Some(transform) = transform {
|
||||
if let Some(source) = handle.cast_to::<syz::Source3D>().expect("Failed to cast") {
|
||||
let translation = transform.translation();
|
||||
source
|
||||
.position()
|
||||
.set((
|
||||
translation.x as f64,
|
||||
translation.y as f64,
|
||||
translation.z as f64,
|
||||
))
|
||||
.expect("Failed to set position");
|
||||
let distance_model = distance_model
|
||||
.cloned()
|
||||
.map(|v| *v)
|
||||
.unwrap_or_else(|| context.default_distance_model().get().unwrap());
|
||||
source
|
||||
.distance_model()
|
||||
.set(distance_model)
|
||||
.expect("Failed to set distance_model");
|
||||
let distance_ref = distance_ref
|
||||
.map(|v| **v)
|
||||
.unwrap_or_else(|| context.default_distance_ref().get().unwrap());
|
||||
assert!(distance_ref >= 0.);
|
||||
source
|
||||
.distance_ref()
|
||||
.set(distance_ref)
|
||||
.expect("Failed to set distance_ref");
|
||||
let distance_max = distance_max
|
||||
.map(|v| **v)
|
||||
.unwrap_or_else(|| context.default_distance_max().get().unwrap());
|
||||
assert!(distance_max >= 0.);
|
||||
source
|
||||
.distance_max()
|
||||
.set(distance_max)
|
||||
.expect("Failed to set distance_max");
|
||||
let rolloff = rolloff
|
||||
.map(|v| **v)
|
||||
.unwrap_or_else(|| context.default_rolloff().get().unwrap());
|
||||
assert!(rolloff >= 0.);
|
||||
source
|
||||
.rolloff()
|
||||
.set(rolloff)
|
||||
.expect("Failed to set rolloff");
|
||||
let closeness_boost = closeness_boost
|
||||
.map(|v| **v)
|
||||
.unwrap_or_else(|| context.default_closeness_boost().get().unwrap());
|
||||
assert!(closeness_boost >= 0.);
|
||||
source
|
||||
.closeness_boost()
|
||||
.set(closeness_boost)
|
||||
.expect("Failed to set closeness_boost");
|
||||
let closeness_boost_distance =
|
||||
closeness_boost_distance.map(|v| **v).unwrap_or_else(|| {
|
||||
context.default_closeness_boost_distance().get().unwrap()
|
||||
});
|
||||
assert!(closeness_boost_distance >= 0.);
|
||||
source
|
||||
.closeness_boost_distance()
|
||||
.set(closeness_boost_distance)
|
||||
.expect("Failed to set closeness_boost_distance");
|
||||
} else {
|
||||
clear_source = true;
|
||||
}
|
||||
} else if let Some(angular_pan) = angular_pan {
|
||||
if let Some(source) = handle
|
||||
.cast_to::<syz::AngularPannedSource>()
|
||||
.expect("Failed to cast")
|
||||
{
|
||||
assert!(angular_pan.azimuth >= 0. && angular_pan.azimuth <= 360.);
|
||||
source
|
||||
.azimuth()
|
||||
.set(angular_pan.azimuth)
|
||||
.expect("Failed to set azimuth");
|
||||
assert!(angular_pan.elevation >= -90. && angular_pan.elevation <= 90.);
|
||||
source
|
||||
.elevation()
|
||||
.set(angular_pan.elevation)
|
||||
.expect("Failed to set elevation");
|
||||
} else {
|
||||
clear_source = true;
|
||||
}
|
||||
} else if let Some(scalar_pan) = scalar_pan {
|
||||
if let Some(source) = handle
|
||||
.cast_to::<syz::ScalarPannedSource>()
|
||||
.expect("Failed to cast")
|
||||
{
|
||||
assert!(**scalar_pan >= -1. && **scalar_pan <= 1.);
|
||||
source
|
||||
.panning_scalar()
|
||||
.set(**scalar_pan)
|
||||
.expect("Failed to set scalar panning");
|
||||
} else {
|
||||
clear_source = true;
|
||||
}
|
||||
let translation = transform.translation();
|
||||
source
|
||||
.position()
|
||||
.set((
|
||||
translation.x as f64,
|
||||
translation.y as f64,
|
||||
translation.z as f64,
|
||||
))
|
||||
.expect("Failed to set position");
|
||||
let distance_model = distance_model
|
||||
.cloned()
|
||||
.map(|v| *v)
|
||||
.unwrap_or_else(|| context.default_distance_model().get().unwrap());
|
||||
source
|
||||
.distance_model()
|
||||
.set(distance_model)
|
||||
.expect("Failed to set distance_model");
|
||||
let distance_ref = distance_ref
|
||||
.map(|v| **v)
|
||||
.unwrap_or_else(|| context.default_distance_ref().get().unwrap());
|
||||
assert!(distance_ref >= 0.);
|
||||
source
|
||||
.distance_ref()
|
||||
.set(distance_ref)
|
||||
.expect("Failed to set distance_ref");
|
||||
let distance_max = distance_max
|
||||
.map(|v| **v)
|
||||
.unwrap_or_else(|| context.default_distance_max().get().unwrap());
|
||||
assert!(distance_max >= 0.);
|
||||
source
|
||||
.distance_max()
|
||||
.set(distance_max)
|
||||
.expect("Failed to set distance_max");
|
||||
let rolloff = rolloff
|
||||
.map(|v| **v)
|
||||
.unwrap_or_else(|| context.default_rolloff().get().unwrap());
|
||||
assert!(rolloff >= 0.);
|
||||
assert!(rolloff >= 0.);
|
||||
source
|
||||
.rolloff()
|
||||
.set(rolloff)
|
||||
.expect("Failed to set rolloff");
|
||||
let closeness_boost = closeness_boost
|
||||
.map(|v| **v)
|
||||
.unwrap_or_else(|| context.default_closeness_boost().get().unwrap());
|
||||
assert!(closeness_boost >= 0.);
|
||||
source
|
||||
.closeness_boost()
|
||||
.set(closeness_boost)
|
||||
.expect("Failed to set closeness_boost");
|
||||
let closeness_boost_distance = closeness_boost_distance
|
||||
.map(|v| **v)
|
||||
.unwrap_or_else(|| context.default_closeness_boost_distance().get().unwrap());
|
||||
assert!(closeness_boost_distance >= 0.);
|
||||
source
|
||||
.closeness_boost_distance()
|
||||
.set(closeness_boost_distance)
|
||||
.expect("Failed to set closeness_boost_distance");
|
||||
} else {
|
||||
clear_source = true;
|
||||
}
|
||||
if clear_source {
|
||||
source.handle = None;
|
||||
} else if let Some(source) = handle
|
||||
.cast_to::<syz::AngularPannedSource>()
|
||||
.expect("Failed to cast")
|
||||
{
|
||||
if let Some(angular_pan) = angular_pan {
|
||||
assert!(angular_pan.azimuth >= 0. && angular_pan.azimuth <= 360.);
|
||||
source
|
||||
.azimuth()
|
||||
.set(angular_pan.azimuth)
|
||||
.expect("Failed to set azimuth");
|
||||
assert!(angular_pan.elevation >= -90. && angular_pan.elevation <= 90.);
|
||||
source
|
||||
.elevation()
|
||||
.set(angular_pan.elevation)
|
||||
.expect("Failed to set elevation");
|
||||
} else {
|
||||
clear_source = true;
|
||||
}
|
||||
} else if let Some(source) = handle
|
||||
.cast_to::<syz::ScalarPannedSource>()
|
||||
.expect("Failed to cast")
|
||||
{
|
||||
if let Some(scalar_pan) = scalar_pan {
|
||||
assert!(**scalar_pan >= -1. && **scalar_pan <= 1.);
|
||||
source
|
||||
.panning_scalar()
|
||||
.set(**scalar_pan)
|
||||
.expect("Failed to set scalar panning");
|
||||
} else {
|
||||
clear_source = true;
|
||||
}
|
||||
} else if handle
|
||||
.cast_to::<syz::DirectSource>()
|
||||
.expect("Failed to cast")
|
||||
.is_some()
|
||||
{
|
||||
if transform.is_some() || angular_pan.is_some() || scalar_pan.is_some() {
|
||||
clear_source = true;
|
||||
}
|
||||
} else if source.handle.is_some() {
|
||||
clear_source = true;
|
||||
}
|
||||
if clear_source {
|
||||
source.handle = None;
|
||||
if let Some(mut sound) = sound {
|
||||
sound.generator = None;
|
||||
sound.playback_position = 0.;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -535,45 +564,60 @@ 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 else {
|
||||
continue;
|
||||
};
|
||||
if let Some(generator) = generator
|
||||
.cast_to::<syz::BufferGenerator>()
|
||||
.expect("Failed to cast")
|
||||
{
|
||||
sound.playback_position = generator
|
||||
.playback_position()
|
||||
.get()
|
||||
.expect("Failed to getplayback position");
|
||||
}
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -654,22 +698,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