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 },