From 17a5bb122aa115b3559efced293a7c368de8986c Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 14 Jan 2016 13:12:01 +0100 Subject: [PATCH] Added Volume in a fishy manner by modulating stream, could maybe be optimized. --- src/main.rs | 1 + src/player.rs | 16 +++++++++++++++- src/spirc.rs | 8 +++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8bdf2a6b..84c42591 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,7 @@ fn usage(program: &str, opts: &Options) -> String { } fn main() { + println!("{:?}",i16::max_value()); let args: Vec = std::env::args().collect(); let program = args[0].clone(); diff --git a/src/player.rs b/src/player.rs index 23564b13..9b07de44 100644 --- a/src/player.rs +++ b/src/player.rs @@ -21,6 +21,7 @@ pub struct PlayerState { position_ms: u32, position_measured_at: i64, update_time: i64, + volume:u16, end_of_track: bool, } @@ -35,6 +36,7 @@ enum PlayerCommand { Load(SpotifyId, bool, u32), Play, Pause, + Volume(u16), Stop, Seek(u32), } @@ -48,6 +50,7 @@ impl Player { position_ms: 0, position_measured_at: 0, update_time: util::now_ms(), + volume: 5000, end_of_track: false, }), Condvar::new())); @@ -181,6 +184,12 @@ impl PlayerInternal { stream.stop().unwrap(); } + Some(PlayerCommand::Volume(vol)) =>{ + self.update(|state| { + state.volume = vol; + true + }); + } Some(PlayerCommand::Stop) => { self.update(|state| { if state.status == PlayStatus::kPlayStatusPlay { @@ -198,7 +207,8 @@ impl PlayerInternal { if self.state.0.lock().unwrap().status == PlayStatus::kPlayStatusPlay { match decoder.as_mut().unwrap().packets().next() { Some(Ok(packet)) => { - match stream.write(&packet.data) { + let buffer = packet.data.iter().map(|x| x/100*(self.state.0.lock().unwrap().volume/655) as i16).collect::>(); + match stream.write(&buffer) { Ok(_) => (), Err(portaudio::PaError::OutputUnderflowed) => eprintln!("Underflow"), Err(e) => panic!("PA Error {}", e), @@ -277,6 +287,10 @@ impl SpircDelegate for Player { fn state(&self) -> MutexGuard { self.state.0.lock().unwrap() } + + fn volume(&self, vol:u16){ + self.command(PlayerCommand::Volume(vol)); + } fn updates(&self) -> mpsc::Receiver { let state = self.state.clone(); diff --git a/src/spirc.rs b/src/spirc.rs index 62229dbb..de7a8f56 100644 --- a/src/spirc.rs +++ b/src/spirc.rs @@ -44,6 +44,7 @@ pub trait SpircDelegate { fn play(&self); fn pause(&self); fn seek(&self, position_ms: u32); + fn volume(&self, vol:u16); fn stop(&self); fn state(&self) -> MutexGuard; @@ -77,7 +78,7 @@ impl SpircManager { repeat: false, shuffle: false, - volume: 0x8000, + volume: 32767, is_active: false, became_active_at: 0, @@ -190,6 +191,11 @@ impl SpircManager { self.delegate.stop(); } } + protocol::spirc::MessageType::kMessageTypeVolume =>{ + println!("{:?}",frame.get_volume()); + self.volume=frame.get_volume() as u16; + self.delegate.volume(self.volume); + } _ => (), } }