Merge remote-tracking branch 'realtwister/master'

This commit is contained in:
Paul Lietar 2016-01-18 21:41:04 +00:00
commit 2a7c9c312b
2 changed files with 36 additions and 4 deletions

View file

@ -21,6 +21,7 @@ pub struct PlayerState {
position_ms: u32,
position_measured_at: i64,
update_time: i64,
volume:i32,
end_of_track: bool,
}
@ -35,6 +36,7 @@ enum PlayerCommand {
Load(SpotifyId, bool, u32),
Play,
Pause,
Volume(i32),
Stop,
Seek(u32),
}
@ -48,6 +50,7 @@ impl Player {
position_ms: 0,
position_measured_at: 0,
update_time: util::now_ms(),
volume: 0x8000,
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 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"),
Err(e) => panic!("PA Error {}", e),
@ -278,6 +288,10 @@ impl SpircDelegate for Player {
self.state.0.lock().unwrap()
}
fn volume(&self, vol:i32){
self.command(PlayerCommand::Volume(vol));
}
fn updates(&self) -> mpsc::Receiver<i64> {
let state = self.state.clone();
let (update_tx, update_rx) = mpsc::channel();
@ -308,6 +322,10 @@ impl SpircState for PlayerState {
(self.position_ms, self.position_measured_at)
}
fn volume(&self) -> u32{
self.volume as u32
}
fn update_time(&self) -> i64 {
self.update_time
}

View file

@ -25,7 +25,6 @@ pub struct SpircManager<D: SpircDelegate> {
repeat: bool,
shuffle: bool,
volume: u16,
is_active: bool,
became_active_at: i64,
@ -44,6 +43,7 @@ pub trait SpircDelegate {
fn play(&self);
fn pause(&self);
fn seek(&self, position_ms: u32);
fn volume(&self, vol:i32);
fn stop(&self);
fn state(&self) -> MutexGuard<Self::State>;
@ -55,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> {
@ -77,7 +78,6 @@ impl<D: SpircDelegate> SpircManager<D> {
repeat: false,
shuffle: false,
volume: 0x8000,
is_active: false,
became_active_at: 0,
@ -156,6 +156,7 @@ impl<D: SpircDelegate> SpircManager<D> {
self.tracks = frame.get_state()
.get_track()
.iter()
.filter(|track| track.get_gid().len()==16)
.map(|track| SpotifyId::from_raw(track.get_gid()))
.collect();
@ -170,6 +171,16 @@ impl<D: SpircDelegate> SpircManager<D> {
protocol::spirc::MessageType::kMessageTypePause => {
self.delegate.pause();
}
protocol::spirc::MessageType::kMessageTypeNext => {
self.index = (self.index + 1) % self.tracks.len() as u32;
let track = self.tracks[self.index as usize];
self.delegate.load(track, true, 0);
}
protocol::spirc::MessageType::kMessageTypePrev => {
self.index = (self.index - 1) % self.tracks.len() as u32;
let track = self.tracks[self.index as usize];
self.delegate.load(track, true, 0);
}
protocol::spirc::MessageType::kMessageTypeSeek => {
self.delegate.seek(frame.get_position());
}
@ -179,6 +190,9 @@ impl<D: SpircDelegate> SpircManager<D> {
self.delegate.stop();
}
}
protocol::spirc::MessageType::kMessageTypeVolume =>{
self.delegate.volume(frame.get_volume() as i32);
}
_ => (),
}
}
@ -249,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 },