diff --git a/events.go b/events.go index 13f80c0..9d75bc7 100644 --- a/events.go +++ b/events.go @@ -54,11 +54,112 @@ func init() { app.events.On("next", app.nextSong) app.events.On("prev", app.prevSong) + app.events.On("vol_up", app.volup) + app.events.On("vol_down", app.voldown) + app.events.On("vol_set", app.volset) + //app.events.On("tick", app.checkQueue) app.events.On("tick", app.songPosition) //app.events.On("tick", app.checkTimeleft) } +func (app *App) volup(payload ...interface{}) { + if !(len(payload) > 0) { + log.Println("volup called without a payload.") + return + } + + var t string + switch data := payload[0].(type) { + case json.RawMessage: + var err error + err = json.Unmarshal(data, &t) + if err != nil { + log.Println(err) + return + } + default: + log.Println("volup called with invalid payload.") + return + } + + switch t { + case "playlist": + pl_volume.Volume = pl_volume.Volume + 0.1 + case "ambiance": + amb_volume.Volume = amb_volume.Volume + 0.1 + } +} + +func (app *App) voldown(payload ...interface{}) { + if !(len(payload) > 0) { + log.Println("voldown called without a payload.") + return + } + + var t string + switch data := payload[0].(type) { + case json.RawMessage: + var err error + err = json.Unmarshal(data, &t) + if err != nil { + log.Println(err) + return + } + default: + log.Println("voldown called with invalid payload.") + return + } + + switch t { + case "playlist": + pl_volume.Volume = pl_volume.Volume - 0.1 + case "ambiance": + amb_volume.Volume = amb_volume.Volume - 0.1 + } +} + +func (app *App) volset(payload ...interface{}) { + if !(len(payload) > 0) { + log.Println("volset called without a payload.") + return + } + + var data map[string]string + switch js := payload[0].(type) { + case json.RawMessage: + json.Unmarshal(js, &data) + default: + log.Println("volset called with invalid payload.") + return + } + + vol, err := strconv.ParseFloat(data["vol"], 64) + if err != nil { + log.Println(err) + return + } + + switch data["type"] { + case "playlist": + pl_volume.Volume = vol + case "ambiance": + amb_volume.Volume = vol + } + + app.sendVolume() +} + +func (app *App) sendVolume() { + msg := make(map[string]interface{}) + out := make(map[string]float64) + msg["event"] = "volume" + out["playlist"] = pl_volume.Volume + out["ambiance"] = amb_volume.Volume + msg["payload"] = out + ws_msg <- msg +} + func (app *App) songInfoEvent(event string) map[string]interface{} { msg := make(map[string]interface{}) msg["event"] = event diff --git a/queue.go b/queue.go index 6591ef9..072f343 100644 --- a/queue.go +++ b/queue.go @@ -13,25 +13,35 @@ import ( discordspeaker "dndmusicbot/speaker" ) +var pl_volume *effects.Volume +var amb_volume *effects.Volume + func init() { log.Println("queue.go loading..") app.ambiance = beep.Mixer{} - discordspeaker.Play(&app.ambiance) + + amb_volume = &effects.Volume{ + Streamer: &app.ambiance, + Base: 2, + Volume: 2, + Silent: false, + } + discordspeaker.Play(amb_volume) mpdstream, err := NewMPD() if err != nil { log.Fatal(err) } - volume := &effects.Volume{ + pl_volume = &effects.Volume{ Streamer: mpdstream, Base: 2, Volume: -2, Silent: false, } - discordspeaker.Play(volume) + discordspeaker.Play(pl_volume) /* app.queue = new(Queue) diff --git a/src/script.js b/src/script.js index bd496fd..91875eb 100644 --- a/src/script.js +++ b/src/script.js @@ -35,7 +35,41 @@ window.onload = function () { const link = document.querySelector("#link") const time = document.querySelector("#time") const waiting = document.querySelector("#waiting") + const pvol = document.querySelector("#playlist-volume") + const avol = document.querySelector("#ambiance-volume") + pvol.addEventListener("change", (e) => { + e.target.nextElementSibling.value = e.target.value + ws.send(JSON.stringify({ + "event": "vol_set", + "payload": { + "type": "playlist", + "vol": e.target.value + } + })) + }) + + pvol.nextElementSibling.addEventListener("change", (e) => { + e.target.previousElementSibling.value = e.target.value + e.target.previousElementSibling.dispatchEvent(new Event('change')); + }) + + avol.addEventListener("change", (e) => { + e.target.nextElementSibling.value = e.target.value + ws.send(JSON.stringify({ + "event": "vol_set", + "payload": { + "type": "ambiance", + "vol": e.target.value + } + })) + }) + + avol.nextElementSibling.addEventListener("change", (e) => { + e.target.previousElementSibling.value = e.target.value + e.target.previousElementSibling.dispatchEvent(new Event('change')); + }) + addInteractHandler( document.querySelector("input#next"), (e) => { ws.send(JSON.stringify({ @@ -107,6 +141,12 @@ window.onload = function () { ws.onmessage = (e) => { data = JSON.parse(e.data) switch (data.event) { + case "volume": + pvol.value = data.payload.playlist + pvol.nextElementSibling.value = data.payload.playlist + avol.value = data.payload.ambiance + avol.nextElementSibling.value = data.payload.ambiance + break case "ambiance_add": const container = document.querySelector("#ambiance") var newdiv = document.createElement('div'); diff --git a/tmpl/index.tmpl b/tmpl/index.tmpl index e5c5495..0ed4ad5 100644 --- a/tmpl/index.tmpl +++ b/tmpl/index.tmpl @@ -67,6 +67,19 @@ +
+
+ + + +
+
+ + + +
+
+