fixed stacktrack, crashing issue
This commit is contained in:
parent
92a6b4b1d3
commit
3482b6952a
@ -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.
|
||||
|
@ -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)
|
||||
|
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user