CookbooksVoice Cookbooks
Realtime Streaming with Sonus
Stream speech with sub-200ms first byte using POST https://api.sagea.space/v1/audio/stream.
- Open a chunked stream and play audio while it generates
- Measure time to first byte against the 200ms target
- Pipe chunks to a player for smooth realtime playback
Time to complete: ~15 minutes
Prerequisites
- Python 3.9+ with
pip install requests. - A media player (
ffplay,mpv, oraplay) for chunked MP3 playback. - A SAGEA API key exported as
SAGEA_API_KEY. - Read Synthesize speech with Sonus for request basics.
- Skim the Sonus TTS model card for streaming formats and sample rates.
Step 1: Open a chunked stream
Request POST https://api.sagea.space/v1/audio/stream with the synthesis body. Audio arrives in chunks so playback starts early.
The -N flag disables curl buffering so chunks hit disk immediately.
Step 2: Measure first-byte timing
Time dispatch to first audio byte. Sonus targets sub-200ms first byte on this endpoint.
If first byte exceeds 200ms, shorten input or prefer mp3 over flac.
Step 3: Pipe chunks to a realtime player
Write chunks to a player pipe as they arrive instead of waiting for the file:
Swap mpv for ffplay -nodisp -autoexit - if needed. Keep 4KB chunks to balance latency and overhead.
Verify
Playback should start almost instantly with no gaps. If it stalls, check the error table below.
| Error | Cause | Fix |
|---|---|---|
401 Unauthorized | API key missing or invalid | Re-export SAGEA_API_KEY and retry |
400 text_too_long | Input exceeds 4096 chars | Stream sentence by sentence under 4096 chars each |
422 voice_not_found | Unknown voice name | Use nova or a valid voice_id starting with vox_ |
429 rate_limit_exceeded | Over 50 req per min on Starter, 500 on Pro | Throttle streams and add backoff |
