improve detection

This commit is contained in:
Skyler Lehmkuhl 2025-01-31 07:12:12 -05:00
parent 18fad499c5
commit ff76b93b9d
3 changed files with 29 additions and 18 deletions

View File

@ -57,6 +57,9 @@ impl StutterDetector {
scheduling_threshold: AtomicU32::new(1200), // 1.2 stored in fixed point scheduling_threshold: AtomicU32::new(1200), // 1.2 stored in fixed point
} }
} }
pub fn reset(&mut self) {
*self = Self::new();
}
fn get_scheduling_threshold(&self) -> f32 { fn get_scheduling_threshold(&self) -> f32 {
self.scheduling_threshold.load(Ordering::Relaxed) as f32 / 1000.0 self.scheduling_threshold.load(Ordering::Relaxed) as f32 / 1000.0
} }
@ -257,9 +260,9 @@ impl CpalAudioOutput {
fn recreate_stream(&mut self) -> Result<(), Box<dyn std::error::Error>> { fn recreate_stream(&mut self) -> Result<(), Box<dyn std::error::Error>> {
// Stop and destroy old stream first // Stop and destroy old stream first
if let Some(old_stream) = self._stream.take() { if let Some(old_stream) = self._stream.take() {
old_stream.pause()?; old_stream.pause()?;
// Explicitly drop the stream // Explicitly drop the stream
drop(old_stream); drop(old_stream);
} }
// Add a small delay to ensure resources are freed (especially important in WASM) // Add a small delay to ensure resources are freed (especially important in WASM)
@ -268,28 +271,36 @@ impl CpalAudioOutput {
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
{ {
use wasm_bindgen_futures::spawn_local; use wasm_bindgen_futures::spawn_local;
use gloo_timers::future::sleep; use gloo_timers::future::sleep;
spawn_local(async { spawn_local(async {
sleep(std::time::Duration::from_millis(50)).await; sleep(std::time::Duration::from_millis(50)).await;
}); });
} }
// Recreate stream with current configuration // Recreate stream with current configuration
let host = cpal::default_host(); let host = cpal::default_host();
let device = host.default_output_device() let device = host.default_output_device()
.ok_or_else(|| "No output device available")?; .ok_or_else(|| "No output device available")?;
let supported_config = device.default_output_config()?; let supported_config = device.default_output_config()?;
{
let mut detector = self.stutter_detector.lock().unwrap();
let desired_buffer_size = detector.desired_buffer_size.load(Ordering::Relaxed);
detector.reset();
detector.desired_buffer_size.store(desired_buffer_size, Ordering::Relaxed);
}
// let mut history = detector.delay_history.lock().unwrap();
self._stream = Some(self.build_stream::<f32>(&device, supported_config)?); self._stream = Some(self.build_stream::<f32>(&device, supported_config)?);
// Restart playback if needed // Restart playback if needed
if self.audio_state == AudioState::Running { if self.audio_state == AudioState::Running {
self._stream.as_ref().unwrap().play()?; self._stream.as_ref().unwrap().play()?;
} }
Ok(()) Ok(())
} }
} }
impl AudioOutput for CpalAudioOutput { impl AudioOutput for CpalAudioOutput {

View File

@ -624,7 +624,7 @@ function __wbg_get_imports() {
const ret = false; const ret = false;
return ret; return ret;
}; };
imports.wbg.__wbindgen_closure_wrapper207 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper208 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 82, __wbg_adapter_20); const ret = makeMutClosure(arg0, arg1, 82, __wbg_adapter_20);
return addHeapObject(ret); return addHeapObject(ret);
}; };

Binary file not shown.