瀏覽代碼

Added Volume in a fishy manner by modulating stream, could maybe be optimized.

Erik 9 年之前
父節點
當前提交
17a5bb122a
共有 3 個文件被更改,包括 23 次插入2 次删除
  1. 1 0
      src/main.rs
  2. 15 1
      src/player.rs
  3. 7 1
      src/spirc.rs

+ 1 - 0
src/main.rs

@@ -24,6 +24,7 @@ fn usage(program: &str, opts: &Options) -> String {
 }
 
 fn main() {
+		println!("{:?}",i16::max_value());
     let args: Vec<String> = std::env::args().collect();
     let program = args[0].clone();
 

+ 15 - 1
src/player.rs

@@ -21,6 +21,7 @@ pub struct PlayerState {
     position_ms: u32,
     position_measured_at: i64,
     update_time: i64,
+    volume:u16,
 
     end_of_track: bool,
 }
@@ -35,6 +36,7 @@ enum PlayerCommand {
     Load(SpotifyId, bool, u32),
     Play,
     Pause,
+    Volume(u16),
     Stop,
     Seek(u32),
 }
@@ -48,6 +50,7 @@ impl Player {
             position_ms: 0,
             position_measured_at: 0,
             update_time: util::now_ms(),
+            volume: 5000,
             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/100*(self.state.0.lock().unwrap().volume/655) as i16).collect::<Vec<i16>>();
+                    		    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> {
         self.state.0.lock().unwrap()
     }
+    
+    fn volume(&self, vol:u16){
+    		self.command(PlayerCommand::Volume(vol));
+    }
 
     fn updates(&self) -> mpsc::Receiver<i64> {
         let state = self.state.clone();

+ 7 - 1
src/spirc.rs

@@ -44,6 +44,7 @@ pub trait SpircDelegate {
     fn play(&self);
     fn pause(&self);
     fn seek(&self, position_ms: u32);
+    fn volume(&self, vol:u16);
     fn stop(&self);
 
     fn state(&self) -> MutexGuard<Self::State>;
@@ -77,7 +78,7 @@ impl<D: SpircDelegate> SpircManager<D> {
 
             repeat: false,
             shuffle: false,
-            volume: 0x8000,
+            volume:	32767,
 
             is_active: false,
             became_active_at: 0,
@@ -190,6 +191,11 @@ impl<D: SpircDelegate> SpircManager<D> {
                     self.delegate.stop();
                 }
             }
+            protocol::spirc::MessageType::kMessageTypeVolume =>{
+            	println!("{:?}",frame.get_volume());
+            	self.volume=frame.get_volume() as u16;
+            	self.delegate.volume(self.volume);
+            }
             _ => (),
         }
     }