- add a new remaining endpoint

This commit is contained in:
Sören Oesterwind 2022-10-13 16:04:51 +02:00
parent 1af34dfcf3
commit 8b198040bd
3 changed files with 53 additions and 12 deletions

View File

@ -73,36 +73,38 @@ func PlaySound(file string, index int, loop bool) int {
Streamer: nil,
Control: nil,
Loop: loop,
Format: streamMap[file].Format,
}
fmt.Println("Playing sound: " + file)
var buffer *beep.Buffer
BufferSound(file)
buffer = streamMap[file].Buffer
streamer := streamMap[file].Streamer
// streamer := streamMap[file].Streamer
fmt.Println("Trying to play sound")
shot := buffer.Streamer(0, buffer.Len())
amountOfLoops := 1
if loop {
amountOfLoops = -1
}
shot := buffer.Streamer(amountOfLoops, buffer.Len())
done := make(chan bool)
ctrl := &beep.Ctrl{Streamer: beep.Seq(shot, beep.Callback(func() {
done <- true
})), Paused: false}
ctrl := &beep.Ctrl{Streamer: shot, Paused: false}
playbacks[index] = playback{
File: file,
IsLoaded: true,
Streamer: streamer,
Streamer: shot,
Control: ctrl,
Loop: loop,
Format: streamMap[file].Format,
Done: done,
}
speaker.Play(ctrl)
<-done
fmt.Println("Finished playing sound: " + file)
if playbacks[index].Loop {
playbacks[index].Control.Paused = true
PlaySound(file, index, loop)
delete(playbacks, index)
}
return 1
}

View File

@ -14,9 +14,11 @@ import (
type playback struct {
File string
IsLoaded bool
Streamer beep.Streamer
Streamer beep.StreamSeeker
Control *beep.Ctrl
Loop bool
Format beep.Format
Done chan bool
}
type playbackWebReturn struct {
@ -90,6 +92,7 @@ func main() {
http.HandleFunc("/v1/stopAll", handleStopAll)
http.HandleFunc("/v1/current", handleCurrent)
http.HandleFunc("/v1/list", handleListing)
http.HandleFunc("/v1/remaining", handleRemaining)
http.HandleFunc("/", handleRoot)
fmt.Println("Listening on port " + fmt.Sprint(configuration.Port))

View File

@ -284,6 +284,42 @@ func handleListing(w http.ResponseWriter, r *http.Request) {
}
}
func handleRemaining(w http.ResponseWriter, r *http.Request) {
// Rejct everything else then GET requests
if r.Method != "GET" {
http.Error(w, "Method is not supported.", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json") // Set the content type to json
var cnt, err = strconv.Atoi(r.URL.Query().Get("id")) // Retrieve the id, first convert it to an int
if err != nil {
fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\"invalid id\"}")
}
fmt.Println(cnt)
plyB := playbacks[cnt]
// fmt.Println(beep.SampleRate.D(plyB.Streamer.Stream().Len()))
seeker := plyB.Streamer
format := plyB.Format
n := plyB.Format.SampleRate // Streamer.Stream() // .At(beep.SampleRate.D(plyB.Streamer.Stream().Len()))
if seeker != nil {
fmt.Println(format.SampleRate)
// fmt.Println(plyB.Seeker.)
position := plyB.Format.SampleRate.D(seeker.Position())
length := plyB.Format.SampleRate.D(seeker.Len())
remaining := length - position
if remaining == 0 {
plyB.Done <- true
}
fmt.Println(position)
fmt.Fprintf(w, "{\"status\":\"ok\", \"SampleRate\":%d, \"Length\":%d, \"Position\":%d, \"Remaining\": %d, \"LengthSec\":\"%v\", \"PosSec\":\"%v\", \"RemaningSec\":\"%v\"}", n, length, position, remaining, length, position, remaining)
} else {
fmt.Println("Seeker is nil")
fmt.Fprintf(w, "{\"status\":\"ok\", \"SampleRate\":%d}", n)
}
}
func handleRoot(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Soundr is running.")
}