Browse Source

Simple block of playback thread based on buffer size.

Will Stott 6 years ago
parent
commit
587aa9c711
2 changed files with 10 additions and 2 deletions
  1. 1 1
      playback/Cargo.toml
  2. 9 1
      playback/src/audio_backend/rodio.rs

+ 1 - 1
playback/Cargo.toml

@@ -20,7 +20,7 @@ portaudio-rs    = { version = "0.3.0", optional = true }
 libpulse-sys    = { version = "0.0.0", optional = true }
 jack            = { version = "0.5.3", optional = true }
 libc            = { version = "0.2", optional = true }
-rodio           = { version = "0.8.1", optional = true, default-features = false }
+rodio           = { git = "https://github.com/tomaka/rodio", optional = true, default-features = false}
 
 [features]
 alsa-backend = ["alsa"]

+ 9 - 1
playback/src/audio_backend/rodio.rs

@@ -1,6 +1,6 @@
 use super::{Open, Sink};
 extern crate rodio;
-use std::io;
+use std::{io, thread, time};
 use std::process::exit;
 
 pub struct RodioSink {
@@ -83,6 +83,14 @@ impl Sink for RodioSink {
     fn write(&mut self, data: &[i16]) -> io::Result<()> {
         let source = rodio::buffer::SamplesBuffer::new(2, 44100, data);
         self.rodio_sink.append(source);
+
+        // Chunk sizes seem to be about 256 to 3000 ish items long.
+        // Assuming they're on average 1628 then a half second buffer is:
+        // 44100 elements --> about 27 chunks
+        while self.rodio_sink.len() > 26 {
+            // sleep and wait for rodio to drain a bit
+            thread::sleep(time::Duration::from_millis(10));
+        }
         Ok(())
     }
 }