- add error handeling

- add loop parameter
- improve documentation
This commit is contained in:
Sören Oesterwind 2022-05-26 17:46:03 +02:00
parent aa41e9c1ff
commit f3e07d3f4d
4 changed files with 26 additions and 6 deletions

View File

@ -14,6 +14,11 @@ paths:
description: A base64 encoded version of the file name description: A base64 encoded version of the file name
schema: schema:
type: string type: string
- in: query
name: loop
description: Defaults to false; if true, will loop the sound until stopped
schema:
type: boolean
responses: responses:
'200': '200':
description: OK description: OK

View File

@ -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{ playbacks[index] = playback{
File: file, File: file,
IsLoaded: false, IsLoaded: false,
Streamer: nil, Streamer: nil,
Control: nil, Control: nil,
Loop: loop,
} }
fmt.Println("Playing sound: " + file) fmt.Println("Playing sound: " + file)
@ -93,10 +94,15 @@ func PlaySound(file string, index int) int {
IsLoaded: true, IsLoaded: true,
Streamer: streamer, Streamer: streamer,
Control: ctrl, Control: ctrl,
Loop: loop,
} }
speaker.Play(ctrl) speaker.Play(ctrl)
<-done <-done
fmt.Println("Finished playing sound: " + file) 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 return 1
} }

View File

@ -16,6 +16,7 @@ type playback struct {
IsLoaded bool IsLoaded bool
Streamer beep.Streamer Streamer beep.Streamer
Control *beep.Ctrl Control *beep.Ctrl
Loop bool
} }
type playbackWebReturn struct { type playbackWebReturn struct {

View File

@ -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 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 bytArr, err := base64.StdEncoding.DecodeString(cnt) // Decode the base64 string
if err != nil { 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 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 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 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 var temp []string
files, err := ioutil.ReadDir("./sounds/") // Read the directory files, err := ioutil.ReadDir("./sounds/") // Read the directory
if err != nil { 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 // Loop through the files and add the file name to the temp array
// Also triggers the buffer process for the file // 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 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 bytArr, err := base64.StdEncoding.DecodeString(cnt) // Decode the base64 string
if err != nil { 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 t, err := os.Stat("./sounds/" + string(bytArr[:])) // Check if the file exists