feat: Add Sound.playback_position to support initializing new buffers at non-zero playback position.

This commit is contained in:
Nolan Darilek 2024-12-02 12:46:06 -06:00
parent 45746803c9
commit 041165cf61

View File

@ -156,6 +156,7 @@ pub struct Sound {
pub gain: f64, pub gain: f64,
pub pitch: f64, pub pitch: f64,
pub looping: bool, pub looping: bool,
pub playback_position: f64,
pub paused: bool, pub paused: bool,
pub generator: Option<syz::Generator>, pub generator: Option<syz::Generator>,
} }
@ -167,6 +168,7 @@ impl Default for Sound {
gain: 1., gain: 1.,
pitch: 1., pitch: 1.,
looping: false, looping: false,
playback_position: default(),
paused: false, paused: false,
generator: None, generator: None,
} }
@ -260,7 +262,6 @@ 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()
@ -304,6 +305,11 @@ fn add_generator(
let generator = syz::BufferGenerator::new(&context) let generator = syz::BufferGenerator::new(&context)
.expect("Failed to create generator"); .expect("Failed to create generator");
generator.buffer().set(&**b).expect("Unable to set buffer"); 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()) Some(generator.into())
} else { } else {
None None
@ -365,6 +371,7 @@ fn swap_buffers(
if let Some(l) = last_audio.get(&entity) { if let Some(l) = last_audio.get(&entity) {
if sound.generator.is_some() && sound.audio != *l { if sound.generator.is_some() && sound.audio != *l {
sound.generator = None; sound.generator = None;
sound.playback_position = 0.;
} }
} }
last_audio.insert(entity, sound.audio.clone()); last_audio.insert(entity, sound.audio.clone());
@ -493,7 +500,6 @@ fn update_source_properties(
.set(closeness_boost_distance) .set(closeness_boost_distance)
.expect("Failed to set closeness_boost_distance"); .expect("Failed to set closeness_boost_distance");
} else { } else {
println!("Clearing 3D source because no transform");
clear_source = true; clear_source = true;
} }
} else if let Some(source) = handle } else if let Some(source) = handle
@ -542,6 +548,7 @@ fn update_source_properties(
source.handle = None; source.handle = None;
if let Some(mut sound) = sound { if let Some(mut sound) = sound {
sound.generator = None; sound.generator = None;
sound.playback_position = 0.;
} }
} }
} }
@ -557,6 +564,18 @@ 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.);
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 { let Some(generator) = sound.generator.as_mut() else {
continue; continue;
}; };