Files
Aroll-Cutter/handlers/analyze.go
2026-02-25 00:27:36 -08:00

59 lines
1.2 KiB
Go

package handlers
import (
"encoding/json"
"net/http"
"aroll/store"
"aroll/transcode"
"aroll/ws"
)
type analyzeRequest struct {
Filename string `json:"filename"`
NoiseDb float64 `json:"noiseDb"`
MinSilence float64 `json:"minSilence"`
Padding float64 `json:"padding"`
}
func AnalyzeHandler(st *store.Store, hub *ws.Hub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
var req analyzeRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "bad JSON: "+err.Error(), http.StatusBadRequest)
return
}
path, ok := st.Path(req.Filename)
if !ok {
http.Error(w, "file not found", http.StatusNotFound)
return
}
go func() {
_, err := transcode.DetectSpeechSegments(
path,
req.NoiseDb,
req.MinSilence,
req.Padding,
hub.Broadcast,
)
if err != nil {
broadcastError(hub, err.Error())
}
}()
w.WriteHeader(http.StatusAccepted)
}
}
func broadcastError(hub *ws.Hub, msg string) {
data, _ := json.Marshal(map[string]string{"type": "error", "message": msg})
hub.Broadcast(data)
}