Skip to content

Commit 5c8f359

Browse files
committed
feat: remember last volume set
Closes #147
1 parent 9066a23 commit 5c8f359

6 files changed

Lines changed: 19 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ audio_output_pipe_format: s16le # Audio output pipe format (s16le, s32le, f32le)
167167
bitrate: 160 # Playback bitrate (96, 160, 320)
168168
volume_steps: 100 # Volume steps count
169169
initial_volume: 100 # Initial volume in steps (not applied to the mixer device)
170+
ignore_last_volume: false # Whether to ignore the last saved volume and always use initial_volume
170171
external_volume: false # Whether volume is controlled externally
171172
disable_autoplay: false # Whether autoplay of more songs should be disabled
172173
```

cmd/daemon/controls.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,12 @@ func (p *AppPlayer) updateVolume(newVal uint32) {
663663
p.app.log.Debugf("update volume requested to %d/%d", newVal, player.MaxStateVolume)
664664
p.player.SetVolume(newVal)
665665

666+
// Save the volume to the state
667+
p.app.state.LastVolume = &newVal
668+
if err := p.app.state.Write(); err != nil {
669+
p.app.log.WithError(err).Error("failed writing state after volume change")
670+
}
671+
666672
// If there is a value in the channel buffer, remove it.
667673
select {
668674
case <-p.volumeUpdate:

cmd/daemon/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ type Config struct {
383383
Bitrate int `koanf:"bitrate"`
384384
VolumeSteps uint32 `koanf:"volume_steps"`
385385
InitialVolume uint32 `koanf:"initial_volume"`
386+
IgnoreLastVolume bool `koanf:"ignore_last_volume"`
386387
NormalisationDisabled bool `koanf:"normalisation_disabled"`
387388
NormalisationUseAlbumGain bool `koanf:"normalisation_use_album_gain"`
388389
NormalisationPregain float32 `koanf:"normalisation_pregain"`

cmd/daemon/player.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ func (p *AppPlayer) handleDealerMessage(ctx context.Context, msg dealer.Message)
8484
if !p.app.cfg.ExternalVolume && len(p.app.cfg.MixerDevice) == 0 {
8585
// update initial volume
8686
p.initialVolumeOnce.Do(func() {
87-
p.updateVolume(p.app.cfg.InitialVolume * player.MaxStateVolume / p.app.cfg.VolumeSteps)
87+
if lastVolume := p.app.state.LastVolume; !p.app.cfg.IgnoreLastVolume && lastVolume != nil {
88+
p.updateVolume(*lastVolume)
89+
} else {
90+
p.updateVolume(p.app.cfg.InitialVolume * player.MaxStateVolume / p.app.cfg.VolumeSteps)
91+
}
8892
})
8993
}
9094
} else if strings.HasPrefix(msg.Uri, "hm://connect-state/v1/connect/volume") {

config_schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@
232232
"min": 0,
233233
"default": 100
234234
},
235+
"ignore_last_volume": {
236+
"type": "boolean",
237+
"description": "Whether the last saved volume should be ignored",
238+
"default": false
239+
},
235240
"normalisation_disabled": {
236241
"type": "boolean",
237242
"description": "Whether track/album normalisation should be disabled",

state.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type AppState struct {
2020
Username string `json:"username"`
2121
Data []byte `json:"data"`
2222
} `json:"credentials"`
23+
LastVolume *uint32 `json:"last_volume"`
2324
}
2425

2526
func (s *AppState) SetLogger(log Logger) {

0 commit comments

Comments
 (0)