Skip to content

Commit

Permalink
Simple block of playback thread based on buffer size.
Browse files Browse the repository at this point in the history
  • Loading branch information
willstott101 committed Mar 7, 2019
1 parent cd6f27c commit f257998
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion playback/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
10 changes: 9 additions & 1 deletion playback/src/audio_backend/rodio.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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(())
}
}

0 comments on commit f257998

Please sign in to comment.