4 Commits

3 changed files with 24 additions and 6 deletions

View File

@ -23,6 +23,8 @@ This is the basic json configuration file layout:
} }
``` ```
> All sounds should be in the same bitrate. It will assume the bitrate of the first loaded sample.
# Usage # Usage
Drop your sounds into the /sounds. You can play them by sending a GET request to the /v1/play endpoint. Drop your sounds into the /sounds. You can play them by sending a GET request to the /v1/play endpoint.
You need to know the base64 encoded file name of the sound you want to play. You can get started by querying /v1/list. It will return a list of all sounds with their respective base64 encoded file name. You need to know the base64 encoded file name of the sound you want to play. You can get started by querying /v1/list. It will return a list of all sounds with their respective base64 encoded file name.

View File

@ -16,6 +16,8 @@ import (
"github.com/h2non/filetype" "github.com/h2non/filetype"
) )
var firstLoad = true
func BufferSound(file string) bool { func BufferSound(file string) bool {
_, ok := streamMap[file] _, ok := streamMap[file]
if !ok { if !ok {
@ -26,9 +28,18 @@ func BufferSound(file string) bool {
} }
fmt.Println("Opened file") 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) fmt.Println("File type: " + kind.MIME.Subtype)
var streamer beep.StreamSeekCloser var streamer beep.StreamSeekCloser
@ -45,8 +56,10 @@ func BufferSound(file string) bool {
fmt.Println("!!!!! Unsupported file type for " + file) fmt.Println("!!!!! Unsupported file type for " + file)
return false return false
} }
if firstLoad {
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10)) speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
firstLoad = false
}
fmt.Println("Decoded file") fmt.Println("Decoded file")
buffer := beep.NewBuffer(format) buffer := beep.NewBuffer(format)
@ -86,11 +99,12 @@ func PlaySound(file string, index int, loop bool) int {
amountOfLoops := 1 amountOfLoops := 1
if loop { if loop {
amountOfLoops = -1 amountOfLoops = -1
fmt.Println("Looping sound: " + file)
} }
shot := buffer.Streamer(amountOfLoops, buffer.Len()) shot := buffer.Streamer(0, buffer.Len())
done := make(chan bool) done := make(chan bool)
ctrl := &beep.Ctrl{Streamer: shot, Paused: false} ctrl := &beep.Ctrl{Streamer: beep.Loop(amountOfLoops, shot), Paused: false}
playbacks[index] = playback{ playbacks[index] = playback{
File: file, File: file,

View File

@ -10,6 +10,7 @@ import (
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"time"
"github.com/faiface/beep/speaker" "github.com/faiface/beep/speaker"
"github.com/h2non/filetype" "github.com/h2non/filetype"
@ -116,6 +117,7 @@ func handleBufferAll(w http.ResponseWriter, r *http.Request) {
} }
temp = append(temp, f.Name()) temp = append(temp, f.Name())
go BufferSound(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 // Return the amount of files buffered
fmt.Fprintf(w, "{\"status\":\"ok\", \"amount\":%d}", len(temp)) fmt.Fprintf(w, "{\"status\":\"ok\", \"amount\":%d}", len(temp))