add support for seeking to jukebox

This commit is contained in:
Alex McGrath
2021-01-18 19:24:29 +00:00
committed by Senan Kelly
parent f16f09717c
commit 6f02b58add
2 changed files with 21 additions and 11 deletions

View File

@@ -271,7 +271,8 @@ func (c *Controller) ServeJukebox(r *http.Request) *spec.Response {
if err != nil { if err != nil {
return spec.NewError(10, "please provide an index for skip actions") 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": case "get":
sub := spec.NewResponse() sub := spec.NewResponse()
sub.JukeboxPlaylist = &spec.JukeboxPlaylist{ sub.JukeboxPlaylist = &spec.JukeboxPlaylist{

View File

@@ -59,13 +59,15 @@ const (
) )
type update struct { type update struct {
action updateType action updateType
index int index int
tracks []*db.Track tracks []*db.Track
skipOffset int
} }
type updateSpeaker struct { type updateSpeaker struct {
index int index int
offset int
} }
func New(musicPath string) *Jukebox { func New(musicPath string) *Jukebox {
@@ -117,14 +119,14 @@ func (j *Jukebox) doUpdate(u update) {
j.index = u.index j.index = u.index
j.playing = true j.playing = true
j.Unlock() j.Unlock()
j.speaker <- updateSpeaker{j.index} j.speaker <- updateSpeaker{index: j.index, offset: u.skipOffset}
case add: case add:
if len(j.playlist) == 0 { if len(j.playlist) == 0 {
j.playlist = u.tracks j.playlist = u.tracks
j.playing = true j.playing = true
j.index = 0 j.index = 0
j.Unlock() j.Unlock()
j.speaker <- updateSpeaker{0} j.speaker <- updateSpeaker{index: 0}
return return
} }
j.playlist = append(j.playlist, u.tracks...) j.playlist = append(j.playlist, u.tracks...)
@@ -182,6 +184,12 @@ func (j *Jukebox) doUpdateSpeaker(su updateSpeaker) error {
j.Lock() j.Lock()
j.info = &strmInfo{} j.info = &strmInfo{}
j.info.strm = streamer.(beep.StreamSeekCloser) 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( j.info.ctrlStrmr.Streamer = beep.Resample(
4, format.SampleRate, 4, format.SampleRate,
j.sr, j.info.strm, j.sr, j.info.strm,
@@ -189,7 +197,7 @@ func (j *Jukebox) doUpdateSpeaker(su updateSpeaker) error {
j.info.format = format j.info.format = format
j.Unlock() j.Unlock()
speaker.Play(beep.Seq(&j.info.ctrlStrmr, beep.Callback(func() { 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 return nil
} }
@@ -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{ j.updates <- update{
action: skip, action: skip,
index: i, index: i,
skipOffset: offset,
} }
} }