From f3e07d3f4d46452b4d481d99757a32d2878455b9 Mon Sep 17 00:00:00 2001 From: grey Date: Thu, 26 May 2022 17:46:03 +0200 Subject: [PATCH] - add error handeling - add loop parameter - improve documentation --- apiDocs.yml | 5 +++++ handlerFunctions.go | 10 ++++++++-- soundr.go | 1 + webRoutes.go | 16 ++++++++++++---- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/apiDocs.yml b/apiDocs.yml index 3fd5dda..9cd14c9 100644 --- a/apiDocs.yml +++ b/apiDocs.yml @@ -14,6 +14,11 @@ paths: description: A base64 encoded version of the file name schema: type: string + - in: query + name: loop + description: Defaults to false; if true, will loop the sound until stopped + schema: + type: boolean responses: '200': description: OK diff --git a/handlerFunctions.go b/handlerFunctions.go index 7bdf2d4..f73fb51 100644 --- a/handlerFunctions.go +++ b/handlerFunctions.go @@ -66,12 +66,13 @@ func BufferSound(file string) bool { } } -func PlaySound(file string, index int) int { +func PlaySound(file string, index int, loop bool) int { playbacks[index] = playback{ File: file, IsLoaded: false, Streamer: nil, Control: nil, + Loop: loop, } fmt.Println("Playing sound: " + file) @@ -93,10 +94,15 @@ func PlaySound(file string, index int) int { IsLoaded: true, Streamer: streamer, Control: ctrl, + Loop: loop, } speaker.Play(ctrl) <-done fmt.Println("Finished playing sound: " + file) - delete(playbacks, index) + if playbacks[index].Loop { + playbacks[index].Control.Paused = true + PlaySound(file, index, loop) + delete(playbacks, index) + } return 1 } diff --git a/soundr.go b/soundr.go index dd13de7..b64f051 100644 --- a/soundr.go +++ b/soundr.go @@ -16,6 +16,7 @@ type playback struct { IsLoaded bool Streamer beep.Streamer Control *beep.Ctrl + Loop bool } type playbackWebReturn struct { diff --git a/webRoutes.go b/webRoutes.go index 0f71c1e..2127c51 100644 --- a/webRoutes.go +++ b/webRoutes.go @@ -25,7 +25,13 @@ func handlePlay(w http.ResponseWriter, r *http.Request) { var cnt = r.URL.Query().Get("file") // Retrieve the file name from the query string bytArr, err := base64.StdEncoding.DecodeString(cnt) // Decode the base64 string if err != nil { - log.Fatal(err) + fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\""+err.Error()+"\"}") + return + } + loop := r.URL.Query().Get("loop") // Retrieve the loop value from the query string + loopBool := false + if loop == "true" { + loopBool = true } t, err := os.Stat("./sounds/" + string(bytArr[:])) // Check if the file exists @@ -57,7 +63,7 @@ func handlePlay(w http.ResponseWriter, r *http.Request) { var currIndex = len(playbacks) // Create a new index for the playback fmt.Fprintf(w, "{\"status\":\"ok\", \"id\":%d}", currIndex) // Return a JSON object to the user - go PlaySound(string(bytArr[:]), currIndex) // Play the sound + go PlaySound(string(bytArr[:]), currIndex, loopBool) // Play the sound } @@ -74,7 +80,8 @@ func handleBufferAll(w http.ResponseWriter, r *http.Request) { var temp []string files, err := ioutil.ReadDir("./sounds/") // Read the directory if err != nil { - log.Fatal(err) + fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\""+err.Error()+"\"}") + return } // Loop through the files and add the file name to the temp array // Also triggers the buffer process for the file @@ -109,7 +116,8 @@ func handleBuffer(w http.ResponseWriter, r *http.Request) { var cnt = r.URL.Query().Get("file") // Retrieve the file name from the query string bytArr, err := base64.StdEncoding.DecodeString(cnt) // Decode the base64 string if err != nil { - log.Fatal(err) + fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\""+err.Error()+"\"}") + return } t, err := os.Stat("./sounds/" + string(bytArr[:])) // Check if the file exists