From 3482b6952a1abb4a9e274c8b7e34dc90ee6e23d4 Mon Sep 17 00:00:00 2001 From: grey Date: Tue, 25 Oct 2022 18:16:23 +0200 Subject: [PATCH] fixed stacktrack, crashing issue --- README.MD | 4 +++- handlerFunctions.go | 21 +++++++++++++++++---- webRoutes.go | 2 ++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README.MD b/README.MD index 1dae3fa..70a9e1d 100644 --- a/README.MD +++ b/README.MD @@ -21,7 +21,9 @@ This is the basic json configuration file layout: { "Port": 8082 } -``` +`` + +> All sounds should be in the same bitrate. It will assume the bitrate of the first loaded sample. # Usage Drop your sounds into the /sounds. You can play them by sending a GET request to the /v1/play endpoint. diff --git a/handlerFunctions.go b/handlerFunctions.go index 31aa6da..6c979af 100644 --- a/handlerFunctions.go +++ b/handlerFunctions.go @@ -16,6 +16,8 @@ import ( "github.com/h2non/filetype" ) +var firstLoad = true + func BufferSound(file string) bool { _, ok := streamMap[file] if !ok { @@ -26,9 +28,18 @@ func BufferSound(file string) bool { } fmt.Println("Opened file") - buf, _ := ioutil.ReadFile("sounds/" + string(file)) + buf, err := ioutil.ReadFile("sounds/" + string(file)) + if err != nil { + log.Fatal("Fatal error while opening: " + err.Error()) + return false + } - kind, _ := filetype.Match(buf) + kind, err := filetype.Match(buf) + + if err != nil { + log.Fatal("Fatal error while detecting file type: " + err.Error()) + return false + } fmt.Println("File type: " + kind.MIME.Subtype) var streamer beep.StreamSeekCloser @@ -45,8 +56,10 @@ func BufferSound(file string) bool { fmt.Println("!!!!! Unsupported file type for " + file) return false } - - speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10)) + if firstLoad { + speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10)) + firstLoad = false + } fmt.Println("Decoded file") buffer := beep.NewBuffer(format) diff --git a/webRoutes.go b/webRoutes.go index 228d311..0f1b576 100644 --- a/webRoutes.go +++ b/webRoutes.go @@ -10,6 +10,7 @@ import ( "net/http" "os" "strconv" + "time" "github.com/faiface/beep/speaker" "github.com/h2non/filetype" @@ -116,6 +117,7 @@ func handleBufferAll(w http.ResponseWriter, r *http.Request) { } temp = append(temp, f.Name()) go BufferSound(f.Name()) + time.Sleep(200 * time.Millisecond) // Wait a bit to not overload the system } // Return the amount of files buffered fmt.Fprintf(w, "{\"status\":\"ok\", \"amount\":%d}", len(temp))