Store the volume as a u16

This commit is contained in:
Paul Lietar 2016-01-20 13:55:36 +00:00
parent c5ed04f70d
commit 303121032d
2 changed files with 26 additions and 19 deletions

View file

@ -21,7 +21,7 @@ pub struct PlayerState {
position_ms: u32, position_ms: u32,
position_measured_at: i64, position_measured_at: i64,
update_time: i64, update_time: i64,
volume:i32, volume: u16,
end_of_track: bool, end_of_track: bool,
} }
@ -36,7 +36,7 @@ enum PlayerCommand {
Load(SpotifyId, bool, u32), Load(SpotifyId, bool, u32),
Play, Play,
Pause, Pause,
Volume(i32), Volume(u16),
Stop, Stop,
Seek(u32), Seek(u32),
} }
@ -184,11 +184,11 @@ impl PlayerInternal {
stream.stop().unwrap(); stream.stop().unwrap();
} }
Some(PlayerCommand::Volume(vol)) =>{ Some(PlayerCommand::Volume(vol)) => {
self.update(|state| { self.update(|state| {
state.volume = vol; state.volume = vol;
true true
}); });
} }
Some(PlayerCommand::Stop) => { Some(PlayerCommand::Stop) => {
self.update(|state| { self.update(|state| {
@ -207,8 +207,15 @@ impl PlayerInternal {
if self.state.0.lock().unwrap().status == PlayStatus::kPlayStatusPlay { if self.state.0.lock().unwrap().status == PlayStatus::kPlayStatusPlay {
match decoder.as_mut().unwrap().packets().next() { match decoder.as_mut().unwrap().packets().next() {
Some(Ok(packet)) => { Some(Ok(packet)) => {
let buffer = packet.data.iter().map(|&x| ((x as i32*self.state.0.lock().unwrap().volume)/0xFFFF) as i16).collect::<Vec<i16>>(); let buffer = packet.data
match stream.write(&buffer) { .iter()
.map(|&x| {
(x as i32
* self.state.0.lock().unwrap().volume as i32
/ 0xFFFF) as i16
})
.collect::<Vec<i16>>();
match stream.write(&buffer) {
Ok(_) => (), Ok(_) => (),
Err(portaudio::PaError::OutputUnderflowed) => eprintln!("Underflow"), Err(portaudio::PaError::OutputUnderflowed) => eprintln!("Underflow"),
Err(e) => panic!("PA Error {}", e), Err(e) => panic!("PA Error {}", e),
@ -287,9 +294,9 @@ impl SpircDelegate for Player {
fn state(&self) -> MutexGuard<Self::State> { fn state(&self) -> MutexGuard<Self::State> {
self.state.0.lock().unwrap() self.state.0.lock().unwrap()
} }
fn volume(&self, vol:i32){ fn volume(&self, vol: u16) {
self.command(PlayerCommand::Volume(vol)); self.command(PlayerCommand::Volume(vol));
} }
fn updates(&self) -> mpsc::Receiver<i64> { fn updates(&self) -> mpsc::Receiver<i64> {
@ -321,9 +328,9 @@ impl SpircState for PlayerState {
fn position(&self) -> (u32, i64) { fn position(&self) -> (u32, i64) {
(self.position_ms, self.position_measured_at) (self.position_ms, self.position_measured_at)
} }
fn volume(&self) -> u32{ fn volume(&self) -> u16 {
self.volume as u32 self.volume
} }
fn update_time(&self) -> i64 { fn update_time(&self) -> i64 {

View file

@ -43,7 +43,7 @@ pub trait SpircDelegate {
fn play(&self); fn play(&self);
fn pause(&self); fn pause(&self);
fn seek(&self, position_ms: u32); fn seek(&self, position_ms: u32);
fn volume(&self, vol: i32); fn volume(&self, vol: u16);
fn stop(&self); fn stop(&self);
fn state(&self) -> MutexGuard<Self::State>; fn state(&self) -> MutexGuard<Self::State>;
@ -55,7 +55,7 @@ pub trait SpircState {
fn position(&self) -> (u32, i64); fn position(&self) -> (u32, i64);
fn update_time(&self) -> i64; fn update_time(&self) -> i64;
fn end_of_track(&self) -> bool; fn end_of_track(&self) -> bool;
fn volume(&self) -> u32; fn volume(&self) -> u16;
} }
impl<D: SpircDelegate> SpircManager<D> { impl<D: SpircDelegate> SpircManager<D> {
@ -187,7 +187,7 @@ impl<D: SpircDelegate> SpircManager<D> {
} }
} }
protocol::spirc::MessageType::kMessageTypeVolume => { 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<D: SpircDelegate> SpircManager<D> {
sw_version: version_string(), sw_version: version_string(),
is_active: self.is_active, is_active: self.is_active,
can_play: self.can_play, can_play: self.can_play,
volume: self.delegate.state().volume(), volume: self.delegate.state().volume() as u32,
name: self.name.clone(), name: self.name.clone(),
error_code: 0, error_code: 0,
became_active_at: if self.is_active { self.became_active_at as i64 } else { 0 }, became_active_at: if self.is_active { self.became_active_at as i64 } else { 0 },