瀏覽代碼

Fixed the multiple volume vars and the calculation is a bit simplified.

Erik 9 年之前
父節點
當前提交
4310162b88
共有 2 個文件被更改,包括 13 次插入12 次删除
  1. 9 5
      src/player.rs
  2. 4 7
      src/spirc.rs

+ 9 - 5
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::<Vec<i16>>();
+                    				let buffer = packet.data.iter().map(|&x| ((x as i32*self.state.0.lock().unwrap().volume)/0xFFFF) as i16).collect::<Vec<i16>>();
                     		    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

+ 4 - 7
src/spirc.rs

@@ -25,7 +25,6 @@ pub struct SpircManager<D: SpircDelegate> {
 
     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<Self::State>;
@@ -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<D: SpircDelegate> SpircManager<D> {
@@ -78,7 +78,6 @@ impl<D: SpircDelegate> SpircManager<D> {
 
             repeat: false,
             shuffle: false,
-            volume:	32767,
 
             is_active: false,
             became_active_at: 0,
@@ -192,9 +191,7 @@ impl<D: SpircDelegate> SpircManager<D> {
                 }
             }
             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<D: SpircDelegate> SpircManager<D> {
             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 },