Skip to content

Commit fb7b8d7

Browse files
committed
Implement volume change from the Spotify application
1 parent 9a82143 commit fb7b8d7

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/lms.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ impl LMS {
4444
self.request(r#"["spottyconnect","change"]"#.to_string())
4545
}
4646

47+
pub fn volume(&self, volume: u16) {
48+
// we're not using the volume here, as LMS will read player state anyway
49+
self.request(format!(r#"["spottyconnect","volume",{}]"#, volume.to_string()))
50+
}
51+
4752
pub fn request(&self, command: String) {
4853
debug!("Base URL to talk to LMS: {}", self.base_url.clone().unwrap());
4954

src/player.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ enum PlayerCommand {
4040
Pause,
4141
Stop,
4242
Seek(u32),
43+
Volume(u32),
4344
}
4445

4546
impl Player {
@@ -96,6 +97,10 @@ impl Player {
9697
pub fn seek(&self, position_ms: u32) {
9798
self.command(PlayerCommand::Seek(position_ms));
9899
}
100+
101+
pub fn set_volume(&self, volume: u32) {
102+
self.command(PlayerCommand::Volume(volume));
103+
}
99104
}
100105

101106
type Decoder = vorbis::Decoder<Subfile<AudioDecrypt<AudioFile>>>;
@@ -286,6 +291,25 @@ impl PlayerInternal {
286291
self.run_onchange();
287292
}
288293

294+
PlayerCommand::Volume(mut volume) => {
295+
if self.lms.is_configured() {
296+
if volume > 0 {
297+
volume = volume * 100 / std::u16::MAX as u32;
298+
} else {
299+
volume = 0;
300+
}
301+
302+
// LMS volume is 0-100. We need to convert
303+
let v2 = if volume > 100 {
304+
Some(100 as u16)
305+
} else {
306+
Some(volume as u16)
307+
};
308+
309+
self.lms.volume(v2.unwrap());
310+
}
311+
}
312+
289313
PlayerCommand::Play => {
290314
if let PlayerState::Paused { .. } = self.state {
291315
self.state.paused_to_playing();
@@ -453,6 +477,11 @@ impl ::std::fmt::Debug for PlayerCommand {
453477
.field(&position)
454478
.finish()
455479
}
480+
PlayerCommand::Volume(volume) => {
481+
f.debug_tuple("Volume")
482+
.field(&volume)
483+
.finish()
484+
}
456485
}
457486
}
458487
}

src/spirc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ impl SpircTask {
410410

411411
MessageType::kMessageTypeVolume => {
412412
let volume = frame.get_volume();
413+
self.player.set_volume(volume);
413414
self.device.set_volume(volume);
414415
self.mixer.set_volume(frame.get_volume() as u16);
415416
self.notify(None);

0 commit comments

Comments
 (0)