diff --git a/src/player.rs b/src/player.rs index 23564b13..731490f5 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:i32, end_of_track: bool, } @@ -35,6 +36,7 @@ enum PlayerCommand { Load(SpotifyId, bool, u32), Play, Pause, + Volume(i32), Stop, Seek(u32), } @@ -48,6 +50,7 @@ impl Player { position_ms: 0, position_measured_at: 0, update_time: util::now_ms(), + volume: 0x8000, 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 as i32*self.state.0.lock().unwrap().volume)/0xFFFF) 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:i32){ + self.command(PlayerCommand::Volume(vol)); + } fn updates(&self) -> mpsc::Receiver { let state = self.state.clone(); @@ -307,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 cd7ba756..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,6 +43,7 @@ pub trait SpircDelegate { fn play(&self); fn pause(&self); fn seek(&self, position_ms: u32); + fn volume(&self, vol:i32); fn stop(&self); fn state(&self) -> MutexGuard; @@ -55,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 { @@ -77,7 +78,6 @@ impl SpircManager { repeat: false, shuffle: false, - volume: 0x8000, is_active: false, became_active_at: 0, @@ -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()); } @@ -179,6 +190,9 @@ impl SpircManager { self.delegate.stop(); } } + protocol::spirc::MessageType::kMessageTypeVolume =>{ + self.delegate.volume(frame.get_volume() as i32); + } _ => (), } } @@ -249,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 },