Providers

Soniox

Soniox through the cache: in a LiveKit agent with our plugin or LiveKit's Soniox plugin, or with Soniox's own REST and real-time WebSocket APIs and Soniox's SDK, changing only the base URLs and key.

View as Markdown

With our LiveKit plugin#

Soniox’s text to speech with every setting of LiveKit’s Soniox plugin. The router calls Soniox’s US endpoint: store a key from a Soniox project in the US region.

pip install livekit-plugins-tts-cache-router
agent.py
from livekit.plugins import tts_cache_router as tcr# The API key comes from TTS_CACHE_ROUTER_API_KEY.tts = tcr.TTS(    model="soniox/tts-rt-v2",    voice="Adrian",    language="en",)

Pass tts to AgentSession(tts=tts). Set TTS_CACHE_ROUTER_API_KEY in the agent’s environment, and store your Soniox key under Provider keys. TTS’s own arguments are on the plugin’s page.

Options#

tcr.TTS(...)’s settings for Soniox, in the router’s names: model, voice, language and speed as arguments, and the rest in the options dict (options={"name": value}). The router checks them; the languages per model are in Models and options.

modelstringRequired

One of soniox/tts-rt-v2

Which model synthesizes the replies, with its provider.
voicestringRequired
A voice of the chosen model, passed to Soniox as given. See models and voices.
languagestringRequired
A Soniox language code from languages, passed through as given.
speednumberDefault 1.0

Range 0.7 – 1.3

Speech speed. 1.0 is normal speed.
sample_rateintDefault 24000

One of 8000, 16000, 24000, 32000, 44100, 48000

Of the audio you receive, in Hz.
options.reduce_silenceboolean | nullDefault null
Shorten the pauses between words. Null: Soniox's default (false).

Changing settings#

Replies that start afterwards use the new settings:

Python
tts.update_options(voice="Nina", speed=1.1)

With LiveKit's Soniox plugin#

Keep livekit-plugins-soniox and point it at the router: your router API key in place of your Soniox key, and the router’s address.

pip install livekit-plugins-soniox
agent.py
import osfrom livekit.plugins import sonioxtts = soniox.TTS(    model="tts-rt-v2",    voice="Adrian",    language="en",    api_key=os.environ["TTS_CACHE_ROUTER_API_KEY"],    websocket_url="wss://api.media.ttscacherouter.dev/soniox/tts-websocket",)

Pass model="tts-rt-v2" and a voice explicitly: the plugin defaults to tts-rt-v1-preview, a deprecated alias, and Maya, a voice tts-rt-v2 doesn’t have.

Each sentence is a take of its own, so the pause between two sentences is shorter (about 0.2 s) than when the plugin streams a whole reply to Soniox (about 0.5 s), and the first sentence starts as soon as it is complete.

The router splits the reply into sentences and caches each one, with a few differences from Soniox’s own messages.

Requests, responses and errors keep Soniox’s own shape; the endpoints are below.

Which plugin to choose#

Both use your org's cache and your stored provider key, and sound the same.

  • Our plugin reports only the characters Soniox billed in TTSMetrics (cache hits count zero), and switches provider with update_options.
  • Soniox’s plugin is the smallest change to an agent that already uses it. Its metrics count every character, cached or not; Usage has the real split.

Soniox's Python SDK#

Soniox’s text-to-speech API, mirrored under https://api.media.ttscacherouter.dev/soniox. Point Soniox’s SDK at the router and use your router API key as the API key.

import osfrom soniox import SonioxClientfrom soniox.types import RealtimeTTSConfigclient = SonioxClient(    api_key=os.environ["TTS_CACHE_ROUTER_API_KEY"],    tts_api_base_url="https://api.media.ttscacherouter.dev/soniox",    tts_websocket_base_url="wss://api.media.ttscacherouter.dev/soniox/tts-websocket",)config = RealtimeTTSConfig(    stream_id="reply-1",    model="tts-rt-v2",    language="en",    voice="Adrian",    audio_format="pcm_s16le",    sample_rate=24000,)with client.realtime.tts.connect(config=config) as session:    session.send_text_chunk("Your order has shipped. ")    session.send_text_chunk("It arrives on Friday.", text_end=True)    audio = b"".join(session.receive_audio_chunks())with open("speech.pcm", "wb") as f:    f.write(audio)

Requests go to Soniox with the Soniox key stored under Provider keys. The router calls Soniox’s US endpoint, so store a key from a Soniox project in the US region. For a LiveKit agent, see LiveKit’s Soniox plugin above.

WebSocket#

wss://api.media.ttscacherouter.dev/soniox/tts-websocket speaks Soniox’s real-time protocol: up to five streams on a connection, each a config message with its stream_id, text messages and text_end, answered with audio messages (the last with audio_end) and terminated.

Send your router API key in Authorization: Bearer, or, as LiveKit’s Soniox plugin 1.8.5 does, as the api_key of each config. cancel ends a stream with terminated, and {"keep_alive": true} keeps an idle connection open: one with no stream and no message for 40 seconds is closed (code 1001), and a stream with no text for 5 seconds and no text_end ends with request_timeout, as at Soniox.

The router splits each stream’s text into sentences and serves each sentence from the cache or synthesizes it at Soniox, so the messages are Soniox’s with these differences:

  • Audio for a sentence starts once the sentence is complete, and the last sentence is spoken without waiting for text_end. Soniox holds a stream’s text until more text or text_end arrives.
  • Each sentence is a take of its own, so the pause between two sentences is shorter (about 0.2 s) than inside one Soniox stream (about 0.5 s).
  • A config’s values (voice, language, rate) are checked by Soniox when its first sentence is synthesized, so an error for a bad one arrives then, not right after the config.
  • A stream with no speakable text gets audio_end on an empty audio.
  • return_timestamps and the aac, opus and flac formats are refused with an error for that stream: use the REST endpoint for them. With wav and mp3, a stream starts with one WAV header or ID3 tag, as from Soniox.
  • Each sentence is a stream of its own on the router’s connection to Soniox, one at a time per stream of yours: the same Soniox concurrency as your stream would use directly.

REST#

  • POST /soniox/tts: the audio in the format the body asks for, streamed with Soniox’s own Content-Type. The router splits the text into sentences and caches each one (see What is cached).
  • GET /soniox/v1/voices (your cloned voices) and GET /soniox/v1/tts-models (the models and their voices): Soniox’s account API at api.soniox.com, never cached. With Soniox’s SDK, set its api_base_urlto https://api.media.ttscacherouter.dev/soniox/v1. GET /v1/voices lists both in the router’s own shape.

Any other path under /soniox answers 404.

Shell
curl "https://api.media.ttscacherouter.dev/soniox/tts" \  -H "Authorization: Bearer $TTS_CACHE_ROUTER_API_KEY" \  -H "Content-Type: application/json" \  -d '{"model": "tts-rt-v2", "language": "en", "voice": "Adrian", "audio_format": "wav", "text": "Your order has shipped."}' \  --output speech.wav

What is cached#

  • REST: each sentence. The router splits text at sentence ends and at the opening sentence’s first comma, and sends each piece to Soniox as a request of its own with your body’s settings. A sentence your org has synthesized before with the same settings comes from the cache, whatever text surrounds it. A piece with nothing to speak is skipped.
  • The sentences reach you as one response in your format, streamed as Soniox streams it: raw samples (pcm_s16le, pcm_f32le, pcm_mulaw, pcm_alaw) and MP3 frames one after another, under one WAV header for wav and one ID3 tag for mp3, as from Soniox.
  • A field you leave out counts as Soniox’s default for it: a sample_rate of 24000 (8000 for pcm_mulaw and pcm_alaw), a bitrate of 128000 for mp3 and aac and 64000 for opus, speed 1.0 and reduce_silence off. A client_reference_id is sent to Soniox with each sentence and is part of the cache key, so leave it out to share sentences across requests. /v1/audio/speech sends Soniox the same REST request, so a sentence here shares its cache for the same model, voice, language, format and settings. /v1/stream, the LiveKit plugins and the WebSocket use Soniox’s WebSocket and keep a cache of their own.
  • Prefer pcm_s16le, pcm_f32le, pcm_mulaw, pcm_alaw, wav or mp3 over flac, opus or aac. Soniox makes each FLAC, Opus (Ogg) or AAC (MP4) take a file of its own, so their sentences don’t join into one: the whole text goes to Soniox as one request and is cached as one entry. Only the same text with the same settings replays it, so a sentence your agent repeats inside other replies is synthesized and paid for every time.
  • Only a complete take is cached: a 200 in the format you asked for whose body Soniox ended cleanly. Errors are never cached.
  • If your client disconnects mid-request, the sentence Soniox is synthesizing finishes and is cached; the sentences after it aren’t sent.
  • A request the router doesn’t read as one Soniox accepts (a missing required field, a field Soniox’s reference doesn’t list, which Soniox ignores, a value of the wrong type, an audio_format Soniox doesn’t make, text with nothing to speak or over 5,000 bytes) goes to Soniox as you sent it, isn’t cached, and gets Soniox’s own answer.
  • WebSocket: each sentence, so a sentence you have sent before with the same config comes from the cache whatever came around it. Sentences share the cache with the router’s own /v1/stream when the settings are the same.
  • Audio is kept for your org’s retention, counted from its last use. At retention 0, nothing is cached and every sentence goes to Soniox.

Limits and timeouts#

  • A REST request body is at most 80,000 bytes; Soniox takes at most 5,000 bytes of text.
  • Each sentence’s first byte within 10 seconds, then 10 seconds at most between chunks. A request sent to Soniox as is gets the same.
  • Each REST request uses one connection to Soniox from your org’s pool and synthesizes one sentence at a time, so it uses as much of Soniox’s concurrency on your key as calling it directly. Each sentence Soniox synthesizes is a request of its own, so it counts against Soniox’s requests-per-minute limit; a sentence from the cache makes no request to Soniox.

Errors#

Errors arrive as Soniox sends them, so your client's error handling keeps working.

  • A missing or wrong router key gets Soniox’s own refusal: HTTP 401 unauthenticated over REST; on the WebSocket, that error message right after the connection opens, then a close (with the key in a config, an error for that stream). An org with no Soniox key stored gets the same, with a message that links to the dashboard.
  • Errors from Soniox, such as an unknown voice (400 invalid_request), a speed out of range or your Soniox limits (429 limit_exceeded), reach you with Soniox’s status and body; on the WebSocket they end only their stream.
  • The router’s own come in Soniox’s shape, with a request_id of null: 402 insufficient_credits when your org is out of credit, 400 invalid_request for a body over 80,000 bytes, 502 service_unavailable when Soniox can’t be reached and 504 request_timeout when it sends nothing in time.
  • A REST response begins with the first sentence’s audio, so a later sentence that fails breaks the response off before its end, as a dropped connection to Soniox would.
{"stream_id": "", "error_code": 401, "error_type": "unauthenticated", "error_message": "Incorrect API key provided.", "more_info": null, "request_id": null}