From f257998ab952202b5586da5030aa6c95cd32f8a9 Mon Sep 17 00:00:00 2001 From: Will Stott Date: Thu, 7 Mar 2019 18:57:25 +0000 Subject: [PATCH] Simple block of playback thread based on buffer size. --- Cargo.lock | 6 +++--- playback/Cargo.toml | 2 +- playback/src/audio_backend/rodio.rs | 10 +++++++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c5553430c..22085b49d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -636,7 +636,7 @@ dependencies = [ "librespot-metadata 0.1.0", "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "portaudio-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rodio 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rodio 0.8.1 (git+https://github.com/tomaka/rodio)", ] [[package]] @@ -1062,7 +1062,7 @@ dependencies = [ [[package]] name = "rodio" version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" +source = "git+https://github.com/tomaka/rodio#8dd11878ff907b6feecafa8f842a8220ba5841d6" dependencies = [ "cgmath 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", "cpal 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1868,7 +1868,7 @@ dependencies = [ "checksum regex-syntax 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "bd90079345f4a4c3409214734ae220fd773c6f2e8a543d07370c6c1c369cfbfb" "checksum relay 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1576e382688d7e9deecea24417e350d3062d97e32e45d70b1cde65994ff1489a" "checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" -"checksum rodio 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "10cb47941163cb747978d13a5c1b5c8fcd17f501817c4b77b9d69aed9ea240bc" +"checksum rodio 0.8.1 (git+https://github.com/tomaka/rodio)" = "" "checksum rpassword 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ec4bdede957362ec6fdd550f7e79c6d14cad2bc26b2d062786234c6ee0cb27bb" "checksum rust-crypto 0.2.36 (git+https://github.com/awmath/rust-crypto.git?branch=avx2)" = "" "checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a" diff --git a/playback/Cargo.toml b/playback/Cargo.toml index 6b4e7756e..cf07d65e8 100644 --- a/playback/Cargo.toml +++ b/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"] diff --git a/playback/src/audio_backend/rodio.rs b/playback/src/audio_backend/rodio.rs index 6269a7846..b00a3be61 100644 --- a/playback/src/audio_backend/rodio.rs +++ b/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(()) } }