2022-11-18 21:18:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-11-19 16:37:49 +00:00
|
|
|
"container/list"
|
2022-11-18 21:18:12 +00:00
|
|
|
"log"
|
2022-11-19 16:37:49 +00:00
|
|
|
"time"
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
"github.com/faiface/beep"
|
2022-11-23 22:29:45 +00:00
|
|
|
"github.com/faiface/beep/effects"
|
2022-11-18 21:18:12 +00:00
|
|
|
"github.com/kataras/go-events"
|
|
|
|
|
2022-11-19 16:37:49 +00:00
|
|
|
"dndmusicbot/ffmpeg"
|
2022-11-18 21:18:12 +00:00
|
|
|
discordspeaker "dndmusicbot/speaker"
|
|
|
|
)
|
|
|
|
|
2022-12-03 18:22:05 +00:00
|
|
|
var pl_volume *effects.Volume
|
|
|
|
var amb_volume *effects.Volume
|
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
func init() {
|
2022-11-23 07:37:59 +00:00
|
|
|
log.Println("queue.go loading..")
|
|
|
|
|
2022-11-19 16:37:49 +00:00
|
|
|
app.ambiance = beep.Mixer{}
|
2022-12-03 18:22:05 +00:00
|
|
|
|
|
|
|
amb_volume = &effects.Volume{
|
|
|
|
Streamer: &app.ambiance,
|
|
|
|
Base: 2,
|
|
|
|
Volume: 2,
|
|
|
|
Silent: false,
|
|
|
|
}
|
|
|
|
discordspeaker.Play(amb_volume)
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2022-11-23 07:37:59 +00:00
|
|
|
mpdstream, err := NewMPD()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-12-03 18:22:05 +00:00
|
|
|
pl_volume = &effects.Volume{
|
2022-11-23 22:29:45 +00:00
|
|
|
Streamer: mpdstream,
|
|
|
|
Base: 2,
|
|
|
|
Volume: -2,
|
|
|
|
Silent: false,
|
|
|
|
}
|
|
|
|
|
2022-12-03 18:22:05 +00:00
|
|
|
discordspeaker.Play(pl_volume)
|
2022-11-23 07:37:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
app.queue = new(Queue)
|
|
|
|
app.queue.list = list.New()
|
|
|
|
app.queue.Events = app.events
|
|
|
|
discordspeaker.Play(app.queue)
|
|
|
|
*/
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
log.Println("queue.go done.")
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:37:49 +00:00
|
|
|
type Song struct {
|
|
|
|
Title string
|
|
|
|
Channel string
|
|
|
|
VideoID string
|
|
|
|
Length time.Duration
|
|
|
|
PCM *ffmpeg.PCM
|
|
|
|
Playlist Playlist
|
|
|
|
DLuri string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Song) NewStream() (err error) {
|
|
|
|
s.PCM, err = ffmpeg.NewPCM(s.DLuri, sampleRate, channels)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
type Queue struct {
|
2022-11-19 16:37:49 +00:00
|
|
|
Events events.EventEmmiter
|
|
|
|
playing bool
|
|
|
|
list *list.List
|
|
|
|
current *list.Element
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q Queue) IsPlaying() bool {
|
|
|
|
return q.playing
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:37:49 +00:00
|
|
|
func (q *Queue) Play() {
|
2022-11-18 21:18:12 +00:00
|
|
|
q.playing = true
|
2022-11-19 16:37:49 +00:00
|
|
|
if q.list.Len() > 0 {
|
|
|
|
play := q.list.Front()
|
|
|
|
q.current = play
|
|
|
|
app.events.Emit("song_start")
|
|
|
|
}
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 16:37:49 +00:00
|
|
|
func (q *Queue) Add(s *Song) {
|
|
|
|
el := q.list.PushBack(s)
|
|
|
|
if el == q.list.Front() {
|
|
|
|
q.Play()
|
|
|
|
}
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 16:37:49 +00:00
|
|
|
func (q Queue) QLen() int {
|
|
|
|
return q.list.Len()
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 16:37:49 +00:00
|
|
|
func (q Queue) Current() *Song {
|
|
|
|
if q.current == nil {
|
|
|
|
return new(Song)
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
2022-11-19 16:37:49 +00:00
|
|
|
return q.current.Value.(*Song)
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queue) Reset() {
|
2022-11-23 07:37:59 +00:00
|
|
|
err := app.mpd.Clear()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-11-19 16:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queue) Next() {
|
|
|
|
if q.current == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
next := q.current.Next()
|
|
|
|
if next == nil {
|
|
|
|
next = q.list.Front()
|
|
|
|
}
|
|
|
|
|
|
|
|
pcm := next.Value.(*Song).PCM
|
|
|
|
if pcm != nil && pcm.Position() != 0 {
|
|
|
|
err := next.Value.(*Song).NewStream()
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
q.current = next
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queue) Prev() {
|
|
|
|
if q.current == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
prev := q.current.Prev()
|
|
|
|
if prev == nil {
|
|
|
|
prev = q.list.Back()
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
2022-11-19 16:37:49 +00:00
|
|
|
|
|
|
|
err := prev.Value.(*Song).NewStream()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2022-11-20 16:30:22 +00:00
|
|
|
q.current = prev
|
2022-11-19 16:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queue) Preload() {
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queue) Stream(samples [][2]float64) (n int, ok bool) {
|
|
|
|
// We use the filled variable to track how many samples we've
|
|
|
|
// successfully filled already. We loop until all samples are filled.
|
|
|
|
filled := 0
|
|
|
|
|
|
|
|
for filled < len(samples) {
|
|
|
|
// There are no streamers in the queue, so we stream silence.
|
2022-11-19 16:37:49 +00:00
|
|
|
if q.current == nil || q.list.Len() == 0 {
|
2022-11-18 21:18:12 +00:00
|
|
|
for i := range samples[filled:] {
|
|
|
|
samples[i][0] = 0
|
|
|
|
samples[i][1] = 0
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// We stream from the first streamer in the queue.
|
2022-11-19 16:37:49 +00:00
|
|
|
n, ok := q.current.Value.(*Song).PCM.Stream(samples[filled:])
|
2022-11-18 21:18:12 +00:00
|
|
|
// If it's drained, we pop it from the queue, thus continuing with
|
|
|
|
// the next streamer.
|
|
|
|
if !ok {
|
2022-11-19 16:37:49 +00:00
|
|
|
q.Next()
|
2022-11-18 21:18:12 +00:00
|
|
|
q.Events.Emit("song_over", nil)
|
|
|
|
}
|
|
|
|
// We update the number of filled samples.
|
|
|
|
filled += n
|
|
|
|
}
|
2022-11-20 13:28:03 +00:00
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
return len(samples), true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queue) Err() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queue) Position() int {
|
2022-11-19 16:37:49 +00:00
|
|
|
if q.current == nil || q.current.Value.(*Song).PCM == nil {
|
2022-11-18 21:18:12 +00:00
|
|
|
return 0
|
|
|
|
}
|
2022-11-19 16:37:49 +00:00
|
|
|
|
|
|
|
return q.current.Value.(*Song).PCM.Position()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q Queue) Len() int {
|
|
|
|
if q.current == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(q.current.Value.(*Song).Length.Milliseconds())
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|