Skip to content

Commit

Permalink
add support for seeking to jukebox
Browse files Browse the repository at this point in the history
  • Loading branch information
lxea authored and sentriz committed Jan 18, 2021
1 parent f16f097 commit 6f02b58
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
3 changes: 2 additions & 1 deletion server/ctrlsubsonic/handlers_common.go
Expand Up @@ -271,7 +271,8 @@ func (c *Controller) ServeJukebox(r *http.Request) *spec.Response {
if err != nil {
return spec.NewError(10, "please provide an index for skip actions")
}
c.Jukebox.Skip(index)
offset, _ := params.GetInt("offset")
c.Jukebox.Skip(index, offset)
case "get":
sub := spec.NewResponse()
sub.JukeboxPlaylist = &spec.JukeboxPlaylist{
Expand Down
29 changes: 19 additions & 10 deletions server/jukebox/jukebox.go
Expand Up @@ -59,13 +59,15 @@ const (
)

type update struct {
action updateType
index int
tracks []*db.Track
action updateType
index int
tracks []*db.Track
skipOffset int
}

type updateSpeaker struct {
index int
index int
offset int
}

func New(musicPath string) *Jukebox {
Expand Down Expand Up @@ -117,14 +119,14 @@ func (j *Jukebox) doUpdate(u update) {
j.index = u.index
j.playing = true
j.Unlock()
j.speaker <- updateSpeaker{j.index}
j.speaker <- updateSpeaker{index: j.index, offset: u.skipOffset}
case add:
if len(j.playlist) == 0 {
j.playlist = u.tracks
j.playing = true
j.index = 0
j.Unlock()
j.speaker <- updateSpeaker{0}
j.speaker <- updateSpeaker{index: 0}
return
}
j.playlist = append(j.playlist, u.tracks...)
Expand Down Expand Up @@ -182,14 +184,20 @@ func (j *Jukebox) doUpdateSpeaker(su updateSpeaker) error {
j.Lock()
j.info = &strmInfo{}
j.info.strm = streamer.(beep.StreamSeekCloser)
if su.offset != 0 {
samples := format.SampleRate.N(time.Second * time.Duration(su.offset))
if err := j.info.strm.Seek(samples); err != nil {
return err
}
}
j.info.ctrlStrmr.Streamer = beep.Resample(
4, format.SampleRate,
j.sr, j.info.strm,
)
j.info.format = format
j.Unlock()
speaker.Play(beep.Seq(&j.info.ctrlStrmr, beep.Callback(func() {
j.speaker <- updateSpeaker{su.index + 1}
j.speaker <- updateSpeaker{index: su.index + 1}
})))
return nil
}
Expand All @@ -215,10 +223,11 @@ func (j *Jukebox) RemoveTrack(i int) {
}
}

func (j *Jukebox) Skip(i int) {
func (j *Jukebox) Skip(i int, offset int) {
j.updates <- update{
action: skip,
index: i,
action: skip,
index: i,
skipOffset: offset,
}
}

Expand Down

0 comments on commit 6f02b58

Please sign in to comment.