From 8b198040bd3b7b1331cde64e91f6e41d6674bab5 Mon Sep 17 00:00:00 2001 From: grey Date: Thu, 13 Oct 2022 16:04:51 +0200 Subject: [PATCH] - add a new remaining endpoint --- handlerFunctions.go | 24 +++++++++++++----------- soundr.go | 5 ++++- webRoutes.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/handlerFunctions.go b/handlerFunctions.go index f73fb51..31aa6da 100644 --- a/handlerFunctions.go +++ b/handlerFunctions.go @@ -73,36 +73,38 @@ func PlaySound(file string, index int, loop bool) int { Streamer: nil, Control: nil, Loop: loop, + Format: streamMap[file].Format, } fmt.Println("Playing sound: " + file) var buffer *beep.Buffer BufferSound(file) buffer = streamMap[file].Buffer - streamer := streamMap[file].Streamer + // streamer := streamMap[file].Streamer fmt.Println("Trying to play sound") - shot := buffer.Streamer(0, buffer.Len()) + amountOfLoops := 1 + if loop { + amountOfLoops = -1 + } + shot := buffer.Streamer(amountOfLoops, buffer.Len()) done := make(chan bool) - ctrl := &beep.Ctrl{Streamer: beep.Seq(shot, beep.Callback(func() { - done <- true - })), Paused: false} + ctrl := &beep.Ctrl{Streamer: shot, Paused: false} playbacks[index] = playback{ File: file, IsLoaded: true, - Streamer: streamer, + Streamer: shot, Control: ctrl, Loop: loop, + Format: streamMap[file].Format, + Done: done, } speaker.Play(ctrl) <-done fmt.Println("Finished playing sound: " + file) - if playbacks[index].Loop { - playbacks[index].Control.Paused = true - PlaySound(file, index, loop) - delete(playbacks, index) - } + delete(playbacks, index) + return 1 } diff --git a/soundr.go b/soundr.go index d158cd7..7e54a0b 100644 --- a/soundr.go +++ b/soundr.go @@ -14,9 +14,11 @@ import ( type playback struct { File string IsLoaded bool - Streamer beep.Streamer + Streamer beep.StreamSeeker Control *beep.Ctrl Loop bool + Format beep.Format + Done chan bool } type playbackWebReturn struct { @@ -90,6 +92,7 @@ func main() { http.HandleFunc("/v1/stopAll", handleStopAll) http.HandleFunc("/v1/current", handleCurrent) http.HandleFunc("/v1/list", handleListing) + http.HandleFunc("/v1/remaining", handleRemaining) http.HandleFunc("/", handleRoot) fmt.Println("Listening on port " + fmt.Sprint(configuration.Port)) diff --git a/webRoutes.go b/webRoutes.go index 44b76cf..f40fb7d 100644 --- a/webRoutes.go +++ b/webRoutes.go @@ -284,6 +284,42 @@ func handleListing(w http.ResponseWriter, r *http.Request) { } } +func handleRemaining(w http.ResponseWriter, r *http.Request) { + // Rejct everything else then GET requests + if r.Method != "GET" { + http.Error(w, "Method is not supported.", http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/json") // Set the content type to json + var cnt, err = strconv.Atoi(r.URL.Query().Get("id")) // Retrieve the id, first convert it to an int + if err != nil { + fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\"invalid id\"}") + } + fmt.Println(cnt) + plyB := playbacks[cnt] + // fmt.Println(beep.SampleRate.D(plyB.Streamer.Stream().Len())) + seeker := plyB.Streamer + format := plyB.Format + n := plyB.Format.SampleRate // Streamer.Stream() // .At(beep.SampleRate.D(plyB.Streamer.Stream().Len())) + + if seeker != nil { + fmt.Println(format.SampleRate) + // fmt.Println(plyB.Seeker.) + position := plyB.Format.SampleRate.D(seeker.Position()) + length := plyB.Format.SampleRate.D(seeker.Len()) + remaining := length - position + if remaining == 0 { + plyB.Done <- true + } + fmt.Println(position) + fmt.Fprintf(w, "{\"status\":\"ok\", \"SampleRate\":%d, \"Length\":%d, \"Position\":%d, \"Remaining\": %d, \"LengthSec\":\"%v\", \"PosSec\":\"%v\", \"RemaningSec\":\"%v\"}", n, length, position, remaining, length, position, remaining) + } else { + fmt.Println("Seeker is nil") + fmt.Fprintf(w, "{\"status\":\"ok\", \"SampleRate\":%d}", n) + } + +} + func handleRoot(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Soundr is running.") }