mirror of
https://github.com/librespot-org/librespot.git
synced 2024-11-08 16:45:43 +00:00
Store the volume as a u16
This commit is contained in:
parent
c5ed04f70d
commit
303121032d
2 changed files with 26 additions and 19 deletions
|
@ -21,7 +21,7 @@ pub struct PlayerState {
|
|||
position_ms: u32,
|
||||
position_measured_at: i64,
|
||||
update_time: i64,
|
||||
volume:i32,
|
||||
volume: u16,
|
||||
|
||||
end_of_track: bool,
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ enum PlayerCommand {
|
|||
Load(SpotifyId, bool, u32),
|
||||
Play,
|
||||
Pause,
|
||||
Volume(i32),
|
||||
Volume(u16),
|
||||
Stop,
|
||||
Seek(u32),
|
||||
}
|
||||
|
@ -184,11 +184,11 @@ impl PlayerInternal {
|
|||
|
||||
stream.stop().unwrap();
|
||||
}
|
||||
Some(PlayerCommand::Volume(vol)) =>{
|
||||
self.update(|state| {
|
||||
state.volume = vol;
|
||||
true
|
||||
});
|
||||
Some(PlayerCommand::Volume(vol)) => {
|
||||
self.update(|state| {
|
||||
state.volume = vol;
|
||||
true
|
||||
});
|
||||
}
|
||||
Some(PlayerCommand::Stop) => {
|
||||
self.update(|state| {
|
||||
|
@ -207,8 +207,15 @@ 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 as i32*self.state.0.lock().unwrap().volume)/0xFFFF) as i16).collect::<Vec<i16>>();
|
||||
match stream.write(&buffer) {
|
||||
let buffer = packet.data
|
||||
.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(_) => (),
|
||||
Err(portaudio::PaError::OutputUnderflowed) => eprintln!("Underflow"),
|
||||
Err(e) => panic!("PA Error {}", e),
|
||||
|
@ -287,9 +294,9 @@ impl SpircDelegate for Player {
|
|||
fn state(&self) -> MutexGuard<Self::State> {
|
||||
self.state.0.lock().unwrap()
|
||||
}
|
||||
|
||||
fn volume(&self, vol:i32){
|
||||
self.command(PlayerCommand::Volume(vol));
|
||||
|
||||
fn volume(&self, vol: u16) {
|
||||
self.command(PlayerCommand::Volume(vol));
|
||||
}
|
||||
|
||||
fn updates(&self) -> mpsc::Receiver<i64> {
|
||||
|
@ -321,9 +328,9 @@ 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 volume(&self) -> u16 {
|
||||
self.volume
|
||||
}
|
||||
|
||||
fn update_time(&self) -> i64 {
|
||||
|
|
|
@ -43,7 +43,7 @@ pub trait SpircDelegate {
|
|||
fn play(&self);
|
||||
fn pause(&self);
|
||||
fn seek(&self, position_ms: u32);
|
||||
fn volume(&self, vol: i32);
|
||||
fn volume(&self, vol: u16);
|
||||
fn stop(&self);
|
||||
|
||||
fn state(&self) -> MutexGuard<Self::State>;
|
||||
|
@ -55,7 +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;
|
||||
fn volume(&self) -> u16;
|
||||
}
|
||||
|
||||
impl<D: SpircDelegate> SpircManager<D> {
|
||||
|
@ -187,7 +187,7 @@ impl<D: SpircDelegate> SpircManager<D> {
|
|||
}
|
||||
}
|
||||
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(),
|
||||
is_active: self.is_active,
|
||||
can_play: self.can_play,
|
||||
volume: self.delegate.state().volume(),
|
||||
volume: self.delegate.state().volume() as u32,
|
||||
name: self.name.clone(),
|
||||
error_code: 0,
|
||||
became_active_at: if self.is_active { self.became_active_at as i64 } else { 0 },
|
||||
|
|
Loading…
Reference in a new issue