mirror of
https://github.com/jamiepine/voicebox.git
synced 2026-09-15 04:40:40 -07:00
The cpal Stream was created and play() called but then immediately dropped when play_to_device() returned. When a cpal Stream is dropped, audio output stops immediately. This caused silent playback. Fix: add a spin-wait loop that holds the Stream in scope until all samples have been consumed (or stop_flag is set).
This commit is contained in:
@@ -401,9 +401,25 @@ impl AudioOutputState {
|
||||
eprintln!("play_to_device: Failed to play stream: {}", e);
|
||||
format!("Failed to play stream: {}", e)
|
||||
})?;
|
||||
|
||||
|
||||
eprintln!("play_to_device: Stream started successfully");
|
||||
|
||||
// Keep the stream alive until playback finishes.
|
||||
// Previously the stream was dropped immediately on function return,
|
||||
// causing silent playback (cpal stops output when its Stream is dropped).
|
||||
let total_samples = {
|
||||
buffer.lock().unwrap().len()
|
||||
};
|
||||
loop {
|
||||
let pos = position.load(std::sync::atomic::Ordering::Relaxed);
|
||||
if pos >= total_samples || stop_flag.load(std::sync::atomic::Ordering::Relaxed) {
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
}
|
||||
|
||||
// stream is dropped here, after audio has finished playing
|
||||
drop(stream);
|
||||
eprintln!("play_to_device: Function completed successfully");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user