aboutsummaryrefslogtreecommitdiff
path: root/music/playqueue.go
blob: cdae0aec0d9047a516d9456f521dad8a45caca0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package music

import (
	"fmt"
	"path/filepath"
	"time"

	"github.com/delucks/go-subsonic"
	"github.com/faiface/beep"
	"github.com/faiface/beep/flac"
	"github.com/faiface/beep/mp3"
	"github.com/faiface/beep/speaker"
	"github.com/faiface/beep/vorbis"
)

type Queue struct {
	songs    []*subsonic.Child
	isPaused bool

	sub                *subsonic.Client
	speakerInitialized bool
	oldSampleRate      beep.SampleRate
	onChange           func(newSong *subsonic.Child, isPaused bool)
}

func NewQueue(client *subsonic.Client) *Queue {
	return &Queue{
		sub:                client,
		speakerInitialized: false,
	}
}

func (q *Queue) SetClient(client *subsonic.Client) {
	q.Clear()
	q.sub = client
}

func (p *Queue) GetSongs() []*subsonic.Child {
	return p.songs
}

func (q *Queue) Append(s *subsonic.Child) {
	q.songs = append(q.songs, s)
}

func (q *Queue) Insert(i int, s *subsonic.Child) {
	if len(q.songs) == 0 {
		q.Append(s)
		return
	}
	q.songs = append(q.songs[:i], append([]*subsonic.Child{s}, q.songs[i:]...)...)
}

func (q *Queue) Clear() {
	q.songs = make([]*subsonic.Child, 0)
	if q.isPaused {
		q.TogglePause()
	}
	q.Stop()
	q.triggerChange()
}

func (q *Queue) PlaySong(s *subsonic.Child) error {
	if q.isPaused {
		q.TogglePause()
	}

	rc, err := Download2(q.sub, s.ID)
	if err != nil {
		return err
	}

	var ssc beep.StreamSeekCloser
	var format beep.Format

	switch filepath.Ext(s.Path) {
	case ".mp3":
		ssc, format, err = mp3.Decode(rc)
	case ".ogg":
		fallthrough
	case ".oga":
		ssc, format, err = vorbis.Decode(rc)
	case ".flac":
		ssc, format, err = flac.Decode(rc)
	default:
		return fmt.Errorf("unknown file type: %s", filepath.Ext(s.Path))
	}

	if err != nil {
		return err
	}

	streamer, err := q.setupSpeaker(ssc, format)
	if err != nil {
		return err
	}
	speaker.Clear()
	speaker.Play(beep.Seq(streamer, beep.Callback(func() { go q.Next() })))

	q.triggerChange()

	return nil
}

func (q *Queue) Play() error {
	if len(q.songs) == 0 {
		return fmt.Errorf("the queue is empty")
	}

	s := q.songs[0]
	q.PlaySong(s)

	return nil
}

func (q *Queue) Next() error {
	q.Stop()

	if len(q.songs) == 0 {
		return nil
	}

	q.songs = q.songs[1:]

	if len(q.songs) == 0 {
		if q.onChange != nil {
			q.onChange(nil, false)
		}
		return nil
	}

	return q.Play()
}

func (q *Queue) Stop() {
	if q.isPaused {
		q.TogglePause()
	}
	speaker.Clear()
}

func (q *Queue) SetOnChangeCallback(f func(newSong *subsonic.Child, isPlaying bool)) {
	q.onChange = f
}

func (q *Queue) TogglePause() {
	if q.isPaused {
		speaker.Unlock()
	} else {
		speaker.Lock()
	}

	q.isPaused = !q.isPaused

	q.triggerChange()
}

func (q *Queue) SkipTo(s *subsonic.Child) {
	i := -1
	for n, s2 := range q.GetSongs() {
		if s.ID == s2.ID {
			i = n
			break
		}
	}

	if i == -1 {
		return
	}

	q.songs = q.songs[i:]
	q.Play()
}

func (q *Queue) RemoveSong(i int) error {
	if i >= len(q.songs) {
		return fmt.Errorf("index out of bounds")
	}

	q.songs = append(q.songs[:i], q.songs[i+1:]...)
	if i == 0 {
		// We removed the first song: this stops it and prepares for the next
		q.Stop()
		if !q.isPaused {
			q.Play()
		}
	}
	q.triggerChange()

	return nil
}

func (q *Queue) Switch(a, b int) error {
	if a >= len(q.songs) {
		return fmt.Errorf("%d is out of bounds", a)
	}

	if b >= len(q.songs) {
		return fmt.Errorf("%d is out of bounds", b)
	}

	tmp := q.songs[a]
	q.songs[a] = q.songs[b]
	q.songs[b] = tmp

	if (a == 0 || b == 0) && !q.isPaused {
		// If we're switching the first song, and it's currently playing, start Play() again
		q.Play()
	}

	q.triggerChange()

	return nil
}

func (q *Queue) triggerChange() {
	if q.onChange != nil {
		if len(q.songs) > 0 {
			q.onChange(q.songs[0], q.isPaused)
		}
	}
}

func (p *Queue) setupSpeaker(s beep.Streamer, format beep.Format) (beep.Streamer, error) {
	if !p.speakerInitialized {
		err := speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
		if err != nil {
			return nil, fmt.Errorf("speaker.Init: %v", err)
		}
		p.speakerInitialized = true
		p.oldSampleRate = format.SampleRate

		return s, nil
	} else {
		sr := p.oldSampleRate
		p.oldSampleRate = format.SampleRate
		return beep.Resample(4, format.SampleRate, sr, s), nil
	}
}