2022-11-18 21:18:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-11-28 13:02:17 +00:00
|
|
|
"context"
|
2023-11-25 19:05:26 +00:00
|
|
|
"dndmusicbot/reisenstreamer"
|
|
|
|
discordspeaker "dndmusicbot/speaker"
|
2022-11-18 21:18:12 +00:00
|
|
|
"encoding/json"
|
|
|
|
"log"
|
2023-10-26 20:50:02 +00:00
|
|
|
"math/rand"
|
2022-11-18 21:18:12 +00:00
|
|
|
"net/url"
|
2023-11-02 20:30:29 +00:00
|
|
|
"path/filepath"
|
2022-11-18 21:18:12 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2023-11-25 19:05:26 +00:00
|
|
|
"github.com/gopxl/beep"
|
2022-11-18 21:18:12 +00:00
|
|
|
"github.com/kataras/go-events"
|
|
|
|
"golang.org/x/time/rate"
|
2023-10-23 18:24:52 +00:00
|
|
|
|
|
|
|
ytdl "github.com/kkdai/youtube/v2"
|
2022-11-18 21:18:12 +00:00
|
|
|
)
|
|
|
|
|
2022-11-23 07:37:59 +00:00
|
|
|
var l = rate.Sometimes{Interval: 800 * time.Millisecond}
|
2023-10-23 18:24:52 +00:00
|
|
|
var ytdl_client = ytdl.Client{}
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
log.Println("events.go loading...")
|
2022-11-23 07:37:59 +00:00
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
app.events = events.New()
|
|
|
|
|
|
|
|
app.events.On("load_playlist", app.loadPlaylist)
|
|
|
|
app.events.On("add_playlist", app.addPlaylist)
|
|
|
|
|
2022-11-19 16:37:49 +00:00
|
|
|
//app.events.On("preload_song", app.preloadSong)
|
2022-11-23 07:37:59 +00:00
|
|
|
//app.events.On("song_over", app.songInfo)
|
|
|
|
//app.events.On("song_start", app.songInfo)
|
|
|
|
app.events.On("player", app.songInfo)
|
2022-11-18 21:18:12 +00:00
|
|
|
//app.events.On("song_position", app.songPosition)
|
|
|
|
|
|
|
|
app.events.On("ambiance_play", app.ambiancePlay)
|
|
|
|
app.events.On("ambiance_stop", app.ambianceStop)
|
|
|
|
app.events.On("ambiance_add", app.ambianceAdd)
|
|
|
|
|
2023-11-25 19:05:26 +00:00
|
|
|
app.events.On("play", app.play)
|
2022-11-18 21:18:12 +00:00
|
|
|
app.events.On("stop", app.stop)
|
|
|
|
app.events.On("next", app.nextSong)
|
|
|
|
app.events.On("prev", app.prevSong)
|
|
|
|
|
2022-12-03 18:22:05 +00:00
|
|
|
app.events.On("vol_up", app.volup)
|
|
|
|
app.events.On("vol_down", app.voldown)
|
|
|
|
app.events.On("vol_set", app.volset)
|
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
//app.events.On("tick", app.checkQueue)
|
|
|
|
app.events.On("tick", app.songPosition)
|
2022-11-23 07:37:59 +00:00
|
|
|
//app.events.On("tick", app.checkTimeleft)
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 10:16:48 +00:00
|
|
|
func (app *App) songInfoEvent(event string) (ev Event, err error) {
|
|
|
|
ev.Event = event
|
2023-11-25 19:05:26 +00:00
|
|
|
info := new(SongInfo)
|
2023-11-04 10:16:48 +00:00
|
|
|
|
2023-11-25 19:05:26 +00:00
|
|
|
info.Pause = !player.Playing
|
|
|
|
|
|
|
|
if player.current == nil || info.Pause {
|
|
|
|
ev.Payload = *info
|
2023-11-04 10:16:48 +00:00
|
|
|
return
|
2022-11-23 07:37:59 +00:00
|
|
|
}
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2023-11-25 19:05:26 +00:00
|
|
|
item := player.current.Value.(*PlayerItem)
|
|
|
|
info.Title = item.Title
|
|
|
|
info.Channel = item.Artist
|
|
|
|
info.Song = item.ID
|
|
|
|
info.Length = item.Duration.Milliseconds()
|
|
|
|
info.Position = item.Position.Milliseconds()
|
|
|
|
info.Index = item.Idx
|
|
|
|
pl, err := app.GetPlaylist(player.PlaylistID)
|
2022-11-23 07:37:59 +00:00
|
|
|
if err != nil {
|
2023-11-25 19:05:26 +00:00
|
|
|
log.Println(err)
|
2023-11-04 10:16:48 +00:00
|
|
|
return
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 19:05:26 +00:00
|
|
|
info.Playlist = pl.Id
|
|
|
|
info.PlaylistName = pl.Title
|
2022-11-23 07:37:59 +00:00
|
|
|
|
2023-11-04 10:16:48 +00:00
|
|
|
ev.Payload = *info
|
2022-11-23 07:37:59 +00:00
|
|
|
|
2023-11-04 10:16:48 +00:00
|
|
|
return ev, nil
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) ambiancePlay(payload ...interface{}) {
|
|
|
|
if !(len(payload) > 0) {
|
|
|
|
log.Println("ambiance_play called without a payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-25 13:06:26 +00:00
|
|
|
var id string
|
2022-11-18 21:18:12 +00:00
|
|
|
switch data := payload[0].(type) {
|
|
|
|
case json.RawMessage:
|
|
|
|
var err error
|
2022-11-25 13:06:26 +00:00
|
|
|
err = json.Unmarshal(data, &id)
|
2022-11-18 21:18:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Println("loadPlaylist called with invalid payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-25 13:06:26 +00:00
|
|
|
amb, err := GetAmbiance(id)
|
2022-11-18 21:18:12 +00:00
|
|
|
if err != nil {
|
2022-11-25 13:06:26 +00:00
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-25 19:05:26 +00:00
|
|
|
discordspeaker.Lock()
|
|
|
|
ambMixer.Clear()
|
|
|
|
stream := reisenstreamer.New(context.Background(), filepath.Join(cwd, amb.Path))
|
|
|
|
stream.Repeat = true
|
|
|
|
ambMixer.Add(beep.Resample(3, beep.SampleRate(stream.SampleRate), beep.SampleRate(48000), stream.StreamerFunc()))
|
|
|
|
discordspeaker.Unlock()
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2023-11-04 10:16:48 +00:00
|
|
|
ev := Event{"ambiance_play", map[string]string{
|
|
|
|
"id": id,
|
|
|
|
}}
|
|
|
|
|
|
|
|
go ws.SendEvent(ev)
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) ambianceStop(payload ...interface{}) {
|
|
|
|
log.Println("Stopping ambiance")
|
2023-11-02 20:30:29 +00:00
|
|
|
|
2023-11-25 19:05:26 +00:00
|
|
|
discordspeaker.Lock()
|
|
|
|
ambMixer.Clear()
|
|
|
|
discordspeaker.Unlock()
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2023-11-04 10:16:48 +00:00
|
|
|
go ws.SendEvent(Event{"ambiance_stop", nil})
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) ambianceAdd(payload ...interface{}) {
|
|
|
|
if !(len(payload) > 0) {
|
|
|
|
log.Println("addPlaylist called without a payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("ambiance_add event received")
|
|
|
|
|
|
|
|
var data map[string]string
|
|
|
|
switch js := payload[0].(type) {
|
|
|
|
case json.RawMessage:
|
|
|
|
json.Unmarshal(js, &data)
|
|
|
|
default:
|
|
|
|
log.Println("newPlaylist called with invalid payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
amburl, ok := data["url"]
|
|
|
|
if !ok {
|
|
|
|
log.Println("addPlaylist without url")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ambtitle, ok := data["title"]
|
|
|
|
if !ok {
|
|
|
|
log.Println("addPlaylist without title")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-25 13:06:26 +00:00
|
|
|
amb, err := AddAmbiance(amburl, ambtitle)
|
2022-11-18 21:18:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-04 10:16:48 +00:00
|
|
|
ev := Event{"ambiance_add", map[string]string{
|
|
|
|
"title": amb.Title,
|
|
|
|
"id": amb.Id,
|
|
|
|
}}
|
|
|
|
|
|
|
|
go ws.SendEvent(ev)
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) songPosition(payload ...interface{}) {
|
2023-11-25 19:05:26 +00:00
|
|
|
if player.current == nil {
|
2022-11-23 07:37:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-25 19:05:26 +00:00
|
|
|
item := player.current.Value.(*PlayerItem)
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
l.Do(func() {
|
2023-11-04 10:16:48 +00:00
|
|
|
ev := Event{"song_position", map[string]int64{
|
2023-11-25 19:05:26 +00:00
|
|
|
"len": item.Duration.Milliseconds(),
|
|
|
|
"position": item.Position.Milliseconds(),
|
2023-11-04 10:16:48 +00:00
|
|
|
}}
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2023-11-04 10:16:48 +00:00
|
|
|
go ws.SendEvent(ev)
|
2022-11-18 21:18:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:37:49 +00:00
|
|
|
func (app *App) songInfo(payload ...interface{}) {
|
2022-11-23 07:37:59 +00:00
|
|
|
log.Println("song_info event received")
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2023-11-04 10:16:48 +00:00
|
|
|
ev, err := app.songInfoEvent("song_info")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
2022-11-23 07:37:59 +00:00
|
|
|
}
|
2023-11-04 10:16:48 +00:00
|
|
|
go ws.SendEvent(ev)
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
2023-11-25 19:05:26 +00:00
|
|
|
func (app *App) play(payload ...interface{}) {
|
|
|
|
if !(len(payload) > 0) {
|
|
|
|
log.Println("play called without a payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
raw := payload[0].(json.RawMessage)
|
|
|
|
var idx int
|
|
|
|
|
|
|
|
json.Unmarshal(raw, &idx)
|
|
|
|
|
|
|
|
go player.Play(idx)
|
|
|
|
}
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
func (app *App) stop(payload ...interface{}) {
|
|
|
|
log.Println("stop event received")
|
2022-11-23 07:37:59 +00:00
|
|
|
|
2023-11-25 19:05:26 +00:00
|
|
|
go player.Stop()
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2023-11-04 10:16:48 +00:00
|
|
|
go ws.SendEvent(Event{"stop", nil})
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) prevSong(payload ...interface{}) {
|
|
|
|
log.Println("prev_song event received")
|
2023-11-25 19:05:26 +00:00
|
|
|
go player.Prev()
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) nextSong(payload ...interface{}) {
|
|
|
|
log.Println("next_song event received")
|
2023-11-25 19:05:26 +00:00
|
|
|
go player.Next()
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) addPlaylist(payload ...interface{}) {
|
|
|
|
if !(len(payload) > 0) {
|
|
|
|
log.Println("addPlaylist called without a payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("add_playlist event received")
|
|
|
|
|
|
|
|
var data map[string]string
|
|
|
|
switch js := payload[0].(type) {
|
|
|
|
case json.RawMessage:
|
|
|
|
json.Unmarshal(js, &data)
|
|
|
|
default:
|
|
|
|
log.Println("newPlaylist called with invalid payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
plurl, ok := data["url"]
|
|
|
|
if !ok {
|
|
|
|
log.Println("addPlaylist without url")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pltitle, ok := data["title"]
|
|
|
|
if !ok {
|
|
|
|
log.Println("addPlaylist without title")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pl, err := url.Parse(plurl)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("addPlaylist invalid url")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
plid := pl.Query().Get("list")
|
|
|
|
if plid == "" {
|
|
|
|
log.Println("addPlaylist missing list in url")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-23 18:24:52 +00:00
|
|
|
_, err = ytdl_client.GetPlaylist(plurl)
|
2022-11-18 21:18:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error getting youtube playlist info,", plid)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := app.AddPlaylist(pltitle, plid)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error getting youtube playlist info,", plid)
|
|
|
|
}
|
|
|
|
|
2023-11-04 10:16:48 +00:00
|
|
|
ev := Event{"new_playlist", map[string]string{
|
|
|
|
"url": id.String(),
|
|
|
|
"title": pltitle,
|
|
|
|
}}
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2023-11-04 10:16:48 +00:00
|
|
|
go ws.SendEvent(ev)
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) loadPlaylist(payload ...interface{}) {
|
|
|
|
log.Println("load_playlist event received")
|
|
|
|
|
|
|
|
if !(len(payload) > 0) {
|
|
|
|
log.Println("loadPlaylist called without a payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var id uuid.UUID
|
|
|
|
switch data := payload[0].(type) {
|
|
|
|
case json.RawMessage:
|
|
|
|
var err error
|
|
|
|
var tmp string
|
|
|
|
|
|
|
|
json.Unmarshal(data, &tmp)
|
|
|
|
id, err = uuid.Parse(tmp)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to parse UUID,", err)
|
|
|
|
}
|
|
|
|
case uuid.UUID:
|
|
|
|
id = data
|
|
|
|
default:
|
|
|
|
log.Println("loadPlaylist called with invalid payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Loading new playlist: ", id)
|
|
|
|
pl, err := app.GetPlaylist(id)
|
|
|
|
if err != nil {
|
2023-11-25 19:05:26 +00:00
|
|
|
log.Printf("Unable to find playlist with id %s (%v)\n", id, err)
|
2022-11-18 21:18:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-23 18:24:52 +00:00
|
|
|
list, err := ytdl_client.GetPlaylist(pl.Url)
|
2022-11-18 21:18:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error getting playlist info,", id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-26 20:50:02 +00:00
|
|
|
rand.Shuffle(len(list.Videos), func(i, j int) { list.Videos[i], list.Videos[j] = list.Videos[j], list.Videos[i] })
|
|
|
|
|
2023-11-25 19:05:26 +00:00
|
|
|
go player.LoadPlaylist(pl.Id, list)
|
2022-11-19 16:37:49 +00:00
|
|
|
}
|