aboutsummaryrefslogtreecommitdiff
path: root/music/playqueue.go
diff options
context:
space:
mode:
Diffstat (limited to 'music/playqueue.go')
-rw-r--r--music/playqueue.go59
1 files changed, 51 insertions, 8 deletions
diff --git a/music/playqueue.go b/music/playqueue.go
index fe82c4e..7c21b9b 100644
--- a/music/playqueue.go
+++ b/music/playqueue.go
@@ -81,9 +81,7 @@ func (q *Queue) PlaySong(s *subsonic.Child) error {
81 speaker.Clear() 81 speaker.Clear()
82 speaker.Play(beep.Seq(streamer, beep.Callback(func() { go q.Next() }))) 82 speaker.Play(beep.Seq(streamer, beep.Callback(func() { go q.Next() })))
83 83
84 if q.onChange != nil { 84 q.triggerChange()
85 q.onChange(s, false)
86 }
87 85
88 return nil 86 return nil
89} 87}
@@ -135,11 +133,7 @@ func (q *Queue) TogglePause() {
135 133
136 q.isPaused = !q.isPaused 134 q.isPaused = !q.isPaused
137 135
138 if q.onChange != nil { 136 q.triggerChange()
139 if len(q.songs) > 0 {
140 q.onChange(q.songs[0], q.isPaused)
141 }
142 }
143} 137}
144 138
145func (q *Queue) SkipTo(s *subsonic.Child) { 139func (q *Queue) SkipTo(s *subsonic.Child) {
@@ -159,6 +153,55 @@ func (q *Queue) SkipTo(s *subsonic.Child) {
159 q.Play() 153 q.Play()
160} 154}
161 155
156func (q *Queue) RemoveSong(i int) error {
157 if i >= len(q.songs) {
158 return fmt.Errorf("index out of bounds")
159 }
160
161 q.songs = append(q.songs[:i], q.songs[i+1:]...)
162 if i == 0 {
163 // We removed the first song: this stops it and prepares for the next
164 q.Stop()
165 if !q.isPaused {
166 q.Play()
167 }
168 }
169 q.triggerChange()
170
171 return nil
172}
173
174func (q *Queue) Switch(a, b int) error {
175 if a >= len(q.songs) {
176 return fmt.Errorf("%d is out of bounds", a)
177 }
178
179 if b >= len(q.songs) {
180 return fmt.Errorf("%d is out of bounds", b)
181 }
182
183 tmp := q.songs[a]
184 q.songs[a] = q.songs[b]
185 q.songs[b] = tmp
186
187 if (a == 0 || b == 0) && !q.isPaused {
188 // If we're switching the first song, and it's currently playing, start Play() again
189 q.Play()
190 }
191
192 q.triggerChange()
193
194 return nil
195}
196
197func (q *Queue) triggerChange() {
198 if q.onChange != nil {
199 if len(q.songs) > 0 {
200 q.onChange(q.songs[0], q.isPaused)
201 }
202 }
203}
204
162func (p *Queue) setupSpeaker(s beep.Streamer, format beep.Format) (beep.Streamer, error) { 205func (p *Queue) setupSpeaker(s beep.Streamer, format beep.Format) (beep.Streamer, error) {
163 if !p.speakerInitialized { 206 if !p.speakerInitialized {
164 err := speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10)) 207 err := speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))