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

This commit is contained in:
Erik 2016-01-14 13:12:01 +01:00
parent 3c8d709ff1
commit 17a5bb122a
3 changed files with 23 additions and 2 deletions

View file

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

View file

@ -21,6 +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:u16,
end_of_track: bool, end_of_track: bool,
} }
@ -35,6 +36,7 @@ enum PlayerCommand {
Load(SpotifyId, bool, u32), Load(SpotifyId, bool, u32),
Play, Play,
Pause, Pause,
Volume(u16),
Stop, Stop,
Seek(u32), Seek(u32),
} }
@ -48,6 +50,7 @@ impl Player {
position_ms: 0, position_ms: 0,
position_measured_at: 0, position_measured_at: 0,
update_time: util::now_ms(), update_time: util::now_ms(),
volume: 5000,
end_of_track: false, end_of_track: false,
}), }),
Condvar::new())); Condvar::new()));
@ -181,6 +184,12 @@ impl PlayerInternal {
stream.stop().unwrap(); stream.stop().unwrap();
} }
Some(PlayerCommand::Volume(vol)) =>{
self.update(|state| {
state.volume = vol;
true
});
}
Some(PlayerCommand::Stop) => { Some(PlayerCommand::Stop) => {
self.update(|state| { self.update(|state| {
if state.status == PlayStatus::kPlayStatusPlay { if state.status == PlayStatus::kPlayStatusPlay {
@ -198,7 +207,8 @@ 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)) => {
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(_) => (), 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),
@ -278,6 +288,10 @@ impl SpircDelegate for Player {
self.state.0.lock().unwrap() self.state.0.lock().unwrap()
} }
fn volume(&self, vol:u16){
self.command(PlayerCommand::Volume(vol));
}
fn updates(&self) -> mpsc::Receiver<i64> { fn updates(&self) -> mpsc::Receiver<i64> {
let state = self.state.clone(); let state = self.state.clone();
let (update_tx, update_rx) = mpsc::channel(); let (update_tx, update_rx) = mpsc::channel();

View file

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