Documentation
¶
Overview ¶
Package tts implements the single-slot TTS audio store and its HTTP handlers.
Overview ¶
Store holds at most one Blob at a time. A new Push() atomically replaces the previous blob so memory never accumulates between TTS utterances.
Two HTTP handlers are exposed:
PushHandler — POST /api/tts/push Accepts {"audio": "<base64-mp3>", "text": "<transcript>"}. Decodes the audio, stores it, and calls the broadcaster callback so all WebSocket clients receive a {"type":"tts","id":"…","text":"…"} frame. v0: pass nil as broadcaster to disable the broadcast side-effect.
GetHandler — GET /api/tts/{id} Serves the stored MP3 as audio/mpeg. Returns 404 if the id does not match the current blob.
IDs ¶
IDs are generated without an external library using nanosecond timestamp + random nanoseconds: fmt.Sprintf("%d%d", nowNs, randNs). This guarantees uniqueness in practice because two successive pushes cannot share the same wall-clock nanosecond, and the random component adds additional entropy.
Wire-up (server.go) ¶
ttsStore := tts.NewStore()
mux.HandleFunc("POST /api/tts/push", ttsStore.PushHandler(nil))
mux.HandleFunc("GET /api/tts/{id}", ttsStore.GetHandler())
Replace nil with ws.Hub.BroadcastTTS once the WebSocket hub is wired.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Blob ¶
type Blob struct {
ID string
Text string
Audio []byte // raw MP3 bytes
TS int64 // Unix millisecond timestamp of Push
}
Blob is a single TTS utterance held in the store.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a single-slot, lock-free TTS audio store. A new Push atomically replaces the previous blob — only the latest utterance is kept, matching the Python _tts_store.clear() + update() idiom.
func (*Store) GetHandler ¶
func (s *Store) GetHandler() http.HandlerFunc
GetHandler returns an http.HandlerFunc for GET /api/tts/{id}. It serves the stored MP3 with Content-Type: audio/mpeg and Cache-Control: no-store. Returns 404 if the id does not match the current blob.
func (*Store) Push ¶
Push stores a new TTS blob and returns it. The ID is generated from the current nanosecond timestamp + random entropy (pure stdlib, no uuid dependency).
func (*Store) PushHandler ¶
func (s *Store) PushHandler(broadcaster func(id, text string)) http.HandlerFunc
PushHandler returns an http.HandlerFunc for POST /api/tts/push.
broadcaster is called after Push with the new blob's (id, text). Pass nil to disable broadcasting (safe in v0 before the WS hub is wired). In production pass ws.Hub.BroadcastTTS.