From 041165cf6197f48a3f0ebc330883158eb4437a02 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Mon, 2 Dec 2024 12:46:06 -0600 Subject: [PATCH] feat: Add `Sound.playback_position` to support initializing new buffers at non-zero playback position. --- src/lib.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c01f655..4ea563f 100644 --- a/src/lib.rs +++ b/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, } @@ -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::() + .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; };