From 3c8d709ff1e71af8b37ac8b164f12cf00bbc087e Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 14 Jan 2016 04:27:34 +0100 Subject: [PATCH 1/4] Added: Next and Previous track Bug resolved: opening a list with "dissallowed tracks" is now possible --- src/spirc.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/spirc.rs b/src/spirc.rs index cd7ba756..62229dbb 100644 --- a/src/spirc.rs +++ b/src/spirc.rs @@ -156,6 +156,7 @@ impl SpircManager { self.tracks = frame.get_state() .get_track() .iter() + .filter(|track| track.get_gid().len()==16) .map(|track| SpotifyId::from_raw(track.get_gid())) .collect(); @@ -170,6 +171,16 @@ impl SpircManager { protocol::spirc::MessageType::kMessageTypePause => { self.delegate.pause(); } + protocol::spirc::MessageType::kMessageTypeNext => { + self.index = (self.index + 1) % self.tracks.len() as u32; + let track = self.tracks[self.index as usize]; + self.delegate.load(track, true, 0); + } + protocol::spirc::MessageType::kMessageTypePrev => { + self.index = (self.index - 1) % self.tracks.len() as u32; + let track = self.tracks[self.index as usize]; + self.delegate.load(track, true, 0); + } protocol::spirc::MessageType::kMessageTypeSeek => { self.delegate.seek(frame.get_position()); } From 17a5bb122aa115b3559efced293a7c368de8986c Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 14 Jan 2016 13:12:01 +0100 Subject: [PATCH 2/4] 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); + } _ => (), } } From 5788da8ddc304ae686b6e5d08d0d4225a2c15a72 Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 14 Jan 2016 13:13:57 +0100 Subject: [PATCH 3/4] Oops, left a debug still standing.. --- src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 84c42591..8bdf2a6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,6 @@ 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(); From 4310162b88f5b5f86d9b55087a4ffa469771adab Mon Sep 17 00:00:00 2001 From: Erik Date: Fri, 15 Jan 2016 01:12:08 +0100 Subject: [PATCH 4/4] Fixed the multiple volume vars and the calculation is a bit simplified. --- src/player.rs | 14 +++++++++----- src/spirc.rs | 11 ++++------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/player.rs b/src/player.rs index 9b07de44..731490f5 100644 --- a/src/player.rs +++ b/src/player.rs @@ -21,7 +21,7 @@ pub struct PlayerState { position_ms: u32, position_measured_at: i64, update_time: i64, - volume:u16, + volume:i32, end_of_track: bool, } @@ -36,7 +36,7 @@ enum PlayerCommand { Load(SpotifyId, bool, u32), Play, Pause, - Volume(u16), + Volume(i32), Stop, Seek(u32), } @@ -50,7 +50,7 @@ impl Player { position_ms: 0, position_measured_at: 0, update_time: util::now_ms(), - volume: 5000, + volume: 0x8000, end_of_track: false, }), Condvar::new())); @@ -207,7 +207,7 @@ impl PlayerInternal { if self.state.0.lock().unwrap().status == PlayStatus::kPlayStatusPlay { match decoder.as_mut().unwrap().packets().next() { Some(Ok(packet)) => { - let buffer = packet.data.iter().map(|x| x/100*(self.state.0.lock().unwrap().volume/655) as i16).collect::>(); + let buffer = packet.data.iter().map(|&x| ((x as i32*self.state.0.lock().unwrap().volume)/0xFFFF) as i16).collect::>(); match stream.write(&buffer) { Ok(_) => (), Err(portaudio::PaError::OutputUnderflowed) => eprintln!("Underflow"), @@ -288,7 +288,7 @@ impl SpircDelegate for Player { self.state.0.lock().unwrap() } - fn volume(&self, vol:u16){ + fn volume(&self, vol:i32){ self.command(PlayerCommand::Volume(vol)); } @@ -321,6 +321,10 @@ impl SpircState for PlayerState { fn position(&self) -> (u32, i64) { (self.position_ms, self.position_measured_at) } + + fn volume(&self) -> u32{ + self.volume as u32 + } fn update_time(&self) -> i64 { self.update_time diff --git a/src/spirc.rs b/src/spirc.rs index de7a8f56..9b779596 100644 --- a/src/spirc.rs +++ b/src/spirc.rs @@ -25,7 +25,6 @@ pub struct SpircManager { repeat: bool, shuffle: bool, - volume: u16, is_active: bool, became_active_at: i64, @@ -44,7 +43,7 @@ pub trait SpircDelegate { fn play(&self); fn pause(&self); fn seek(&self, position_ms: u32); - fn volume(&self, vol:u16); + fn volume(&self, vol:i32); fn stop(&self); fn state(&self) -> MutexGuard; @@ -56,6 +55,7 @@ pub trait SpircState { fn position(&self) -> (u32, i64); fn update_time(&self) -> i64; fn end_of_track(&self) -> bool; + fn volume(&self) -> u32; } impl SpircManager { @@ -78,7 +78,6 @@ impl SpircManager { repeat: false, shuffle: false, - volume: 32767, is_active: false, became_active_at: 0, @@ -192,9 +191,7 @@ impl SpircManager { } } protocol::spirc::MessageType::kMessageTypeVolume =>{ - println!("{:?}",frame.get_volume()); - self.volume=frame.get_volume() as u16; - self.delegate.volume(self.volume); + self.delegate.volume(frame.get_volume() as i32); } _ => (), } @@ -266,7 +263,7 @@ impl SpircManager { sw_version: version_string(), is_active: self.is_active, can_play: self.can_play, - volume: self.volume as u32, + volume: self.delegate.state().volume(), name: self.name.clone(), error_code: 0, became_active_at: if self.is_active { self.became_active_at as i64 } else { 0 },