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 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,
}
@ -260,7 +262,6 @@ fn add_source_handle(
.expect("Failed to create source")
.into()
} else {
println!("Adding new direct source");
syz::DirectSource::new(&context)
.expect("Failed to create source")
.into()
@ -304,6 +305,11 @@ fn add_generator(
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
@ -365,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());
@ -493,7 +500,6 @@ fn update_source_properties(
.set(closeness_boost_distance)
.expect("Failed to set closeness_boost_distance");
} else {
println!("Clearing 3D source because no transform");
clear_source = true;
}
} else if let Some(source) = handle
@ -542,6 +548,7 @@ fn update_source_properties(
source.handle = None;
if let Some(mut sound) = sound {
sound.generator = None;
sound.playback_position = 0.;
}
}
}
@ -557,6 +564,18 @@ fn update_sound_properties(mut query: Query<&mut Sound>) {
} = *sound;
assert!(gain >= 0.);
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 {
continue;
};