Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Use prior volume updates as a reference for fast relative updates #142

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions cmd/daemon/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import (
"sync"
"time"

log "github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"

librespot "github.com/devgianlu/go-librespot"
"github.com/devgianlu/go-librespot/ap"
"github.com/devgianlu/go-librespot/dealer"
"github.com/devgianlu/go-librespot/player"
connectpb "github.com/devgianlu/go-librespot/proto/spotify/connectstate"
"github.com/devgianlu/go-librespot/session"
"github.com/devgianlu/go-librespot/tracks"
log "github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
)

type AppPlayer struct {
Expand All @@ -43,6 +44,9 @@ type AppPlayer struct {
secondaryStream *player.Stream

prefetchTimer *time.Timer

lastVolumeUpdateTime time.Time
lastVolumeUpdateVal int32
}

func (p *AppPlayer) handleAccesspointPacket(pktType ap.PacketType, payload []byte) error {
Expand Down Expand Up @@ -498,13 +502,21 @@ func (p *AppPlayer) handleApiRequest(req ApiRequest) (any, error) {

var volume int32
if data.Relative {
volume = int32(p.apiVolume())
if p.lastVolumeUpdateTime.IsZero() || time.Since(p.lastVolumeUpdateTime).Seconds() > 1 {
volume = int32(p.apiVolume())
} else {
volume = p.lastVolumeUpdateVal
}
volume += data.Volume
volume = max(min(volume, int32(p.app.cfg.VolumeSteps)), 0)
} else {
volume = data.Volume
}

// TODO: we should only store it if the update was successful
p.lastVolumeUpdateTime = time.Now()
p.lastVolumeUpdateVal = volume

p.updateVolume(uint32(volume) * player.MaxStateVolume / p.app.cfg.VolumeSteps)
return nil, nil
case ApiRequestTypeSetRepeatingContext:
Expand Down
Loading