From 636de3fe71b84aed447724b2a8e03a17f330849f Mon Sep 17 00:00:00 2001 From: Daniel Romero Date: Wed, 25 Jan 2017 22:56:06 +0100 Subject: [PATCH] Use stream_editor in player --- src/main.rs | 2 +- src/mixer/mod.rs | 2 +- src/mixer/softmixer.rs | 4 ++-- src/player.rs | 18 +++++++++++++----- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index ac460665..7fbad5fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -129,7 +129,7 @@ fn setup(args: &[String]) -> (Session, Player, Box) { let mixer = mixer::find(&mixer_name).unwrap(); let device_name = matches.opt_str("device"); - let player = Player::new(session.clone(), move || { + let player = Player::new(session.clone(), mixer.get_stream_editor(), move || { (backend)(device_name.as_ref().map(AsRef::as_ref)) }); diff --git a/src/mixer/mod.rs b/src/mixer/mod.rs index e4a2cb76..bcd4564c 100644 --- a/src/mixer/mod.rs +++ b/src/mixer/mod.rs @@ -8,7 +8,7 @@ pub trait Mixer { fn stop(&self); fn set_volume(&self, volume: u16); fn volume(&self) -> u16; - fn get_stream_editor(&self) -> Option> + fn get_stream_editor(&self) -> Option> { None } diff --git a/src/mixer/softmixer.rs b/src/mixer/softmixer.rs index 7dfd1f6b..15333873 100644 --- a/src/mixer/softmixer.rs +++ b/src/mixer/softmixer.rs @@ -28,14 +28,14 @@ impl Mixer for SoftMixer { fn set_volume(&self, volume: u16) { *self.volume.write().unwrap() = volume; } - fn get_stream_editor(&self) -> Option> { + fn get_stream_editor(&self) -> Option> { let vol = self.volume.clone(); Some(Box::new(SoftVolumeApplier { get_volume: Box::new(move || *vol.read().unwrap() ) })) } } struct SoftVolumeApplier { - get_volume: Box u16> + get_volume: Box u16 + Send> } impl StreamEditor for SoftVolumeApplier { diff --git a/src/player.rs b/src/player.rs index bc38263b..a41ab489 100644 --- a/src/player.rs +++ b/src/player.rs @@ -9,6 +9,7 @@ use audio_decrypt::AudioDecrypt; use audio_backend::Sink; use metadata::{FileFormat, Track, TrackRef}; use session::{Bitrate, Session}; +use mixer::StreamEditor; use util::{self, ReadSeek, SpotifyId, Subfile}; pub use spirc::PlayStatus; @@ -71,7 +72,7 @@ enum PlayerCommand { } impl Player { - pub fn new(session: Session, sink_builder: F) -> Player + pub fn new(session: Session, stream_editor: Option>, sink_builder: F) -> Player where F: FnOnce() -> Box + Send + 'static { let (cmd_tx, cmd_rx) = mpsc::channel(); @@ -92,7 +93,7 @@ impl Player { observers: observers.clone(), }; - thread::spawn(move || internal.run(sink_builder())); + thread::spawn(move || internal.run(sink_builder(), stream_editor)); Player { commands: cmd_tx, @@ -138,6 +139,10 @@ impl Player { } } +fn borrow_data<'a>(data: &'a [i16]) -> Cow<'a, [i16]> { + Cow::Borrowed(&data) +} + fn find_available_alternative<'a>(session: &Session, track: &'a Track) -> Option> { if track.available { Some(Cow::Borrowed(track)) @@ -205,7 +210,7 @@ fn run_onstop(session: &Session) { } impl PlayerInternal { - fn run(self, mut sink: Box) { + fn run(self, mut sink: Box, stream_editor: Option>) { let mut decoder = None; loop { @@ -340,8 +345,11 @@ impl PlayerInternal { match packet { Some(Ok(packet)) => { - //let buffer = mixer.apply_volume(&packet.data); - let buffer = Cow::Borrowed(&packet.data); + let buffer = if let Some(ref editor) = stream_editor { + editor.modify_stream(&packet.data) + } else { + borrow_data(&packet.data) + }; sink.write(&buffer).unwrap(); self.update(|state| {