Add volume control

nostruct
Stein Ivar Berghei 2022-12-03 19:22:05 +01:00
parent ddf7ea2f15
commit 38a43bde59
5 changed files with 175 additions and 3 deletions

101
events.go
View File

@ -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

View File

@ -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)

View File

@ -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');

View File

@ -67,6 +67,19 @@
</div>
</section>
<section>
<div id="volume_playlist" class="input-container">
<label for="playlist-volume">Playlist</label>
<input type="range" id="playlist-volume" min="-6" max="1" step="0.1">
<input id="playlist-volume-number" type="number" min="-6" max="1" step="0.1" style="width:50px" />
</div>
<div id="volume_ambiance" class="input-container">
<label for="ambiance-volume">Ambiance</label>
<input type="range" id="ambiance-volume" min="-4" max="4" step="0.1">
<input id="ambiance-volume-number" type="number" min="-4" max="4" step="0.1" style="width:50px" />
</div>
</section>
<!-- The Modal -->
<div id="waiting" class="modal">
<!-- Modal content -->

8
ws.go
View File

@ -66,8 +66,16 @@ func handleWS(c *websocket.Conn) error {
msg := app.songInfoEvent("song_info")
vol := make(map[string]interface{})
volout := make(map[string]float64)
vol["event"] = "volume"
volout["playlist"] = pl_volume.Volume
volout["ambiance"] = amb_volume.Volume
vol["payload"] = volout
c.SetWriteDeadline(time.Now().Add(10 * time.Second))
c.WriteJSON(msg)
c.WriteJSON(vol)
if app.ambiance.Len() > 0 {
msg := make(map[string]interface{})