From 303121032dde16f43b83b6197e341f17998e6768 Mon Sep 17 00:00:00 2001 From: Paul Lietar Date: Wed, 20 Jan 2016 13:55:36 +0000 Subject: [PATCH] Store the volume as a u16 --- src/player.rs | 37 ++++++++++++++++++++++--------------- src/spirc.rs | 8 ++++---- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/player.rs b/src/player.rs index 731490f..43c9df8 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:i32, + volume: u16, end_of_track: bool, } @@ -36,7 +36,7 @@ enum PlayerCommand { Load(SpotifyId, bool, u32), Play, Pause, - Volume(i32), + Volume(u16), Stop, Seek(u32), } @@ -184,11 +184,11 @@ impl PlayerInternal { stream.stop().unwrap(); } - Some(PlayerCommand::Volume(vol)) =>{ - self.update(|state| { - state.volume = vol; - true - }); + Some(PlayerCommand::Volume(vol)) => { + self.update(|state| { + state.volume = vol; + true + }); } Some(PlayerCommand::Stop) => { self.update(|state| { @@ -207,8 +207,15 @@ 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 as i32*self.state.0.lock().unwrap().volume)/0xFFFF) as i16).collect::>(); - match stream.write(&buffer) { + let buffer = packet.data + .iter() + .map(|&x| { + (x as i32 + * self.state.0.lock().unwrap().volume as i32 + / 0xFFFF) as i16 + }) + .collect::>(); + match stream.write(&buffer) { Ok(_) => (), Err(portaudio::PaError::OutputUnderflowed) => eprintln!("Underflow"), Err(e) => panic!("PA Error {}", e), @@ -287,9 +294,9 @@ impl SpircDelegate for Player { fn state(&self) -> MutexGuard { self.state.0.lock().unwrap() } - - fn volume(&self, vol:i32){ - self.command(PlayerCommand::Volume(vol)); + + fn volume(&self, vol: u16) { + self.command(PlayerCommand::Volume(vol)); } fn updates(&self) -> mpsc::Receiver { @@ -321,9 +328,9 @@ 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 volume(&self) -> u16 { + self.volume } fn update_time(&self) -> i64 { diff --git a/src/spirc.rs b/src/spirc.rs index ec38516..66f7dd1 100644 --- a/src/spirc.rs +++ b/src/spirc.rs @@ -43,7 +43,7 @@ pub trait SpircDelegate { fn play(&self); fn pause(&self); fn seek(&self, position_ms: u32); - fn volume(&self, vol: i32); + fn volume(&self, vol: u16); fn stop(&self); fn state(&self) -> MutexGuard; @@ -55,7 +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; + fn volume(&self) -> u16; } impl SpircManager { @@ -187,7 +187,7 @@ impl SpircManager { } } protocol::spirc::MessageType::kMessageTypeVolume => { - self.delegate.volume(frame.get_volume() as i32); + self.delegate.volume(frame.get_volume() as u16); } _ => (), } @@ -268,7 +268,7 @@ impl SpircManager { sw_version: version_string(), is_active: self.is_active, can_play: self.can_play, - volume: self.delegate.state().volume(), + volume: self.delegate.state().volume() as u32, name: self.name.clone(), error_code: 0, became_active_at: if self.is_active { self.became_active_at as i64 } else { 0 },