Skip to main content
This walkthrough shows feeding sentences from a long document into a single WebSocket context, receiving PCM audio chunks as they’re generated, and writing the result to a single WAV file at the end.

Setup

Install the SDK and have KOVA_API_KEY set in your environment.

Example

What’s happening

  1. Open one context for the whole document. Reusing a context means the voice and format stay consistent across the entire output, with no warm-up overhead between sentences.
  2. Send each sentence as a separate send_text frame with trailing whitespace so the model treats them as sequential prose, not concatenated tokens.
  3. Audio streams back incrementally — by the time you’ve sent the third sentence, audio for the first is already arriving.
  4. flush with a sentinel flush_id lets you know when the server has finished generating the last sentence — you wait for the flush_completed whose flush_id matches the one you sent. This matters: close_context emits its own flush_completed with the synthetic id "doc:close", so a loop that breaks on any flush_completed would stop at the wrong moment.
  5. Assemble PCM and write a single WAV file at the end. Python uses stdlib wave; JS uses the exported pcm16ToWavBytes primitive + node:fs. The SDKs don’t currently ship a higher-level writePcm16WavFile helper — if one is added later, this page should be updated to prefer it.

Variations

  • Multiple parallel contexts: open ctx-narration and ctx-sfx concurrently with different voices. Demultiplex audio_chunk by context_id on the client.
  • Different output format: swap encoding: "pcm" for encoding: "mp3" and skip the WAV-header step.
  • Without timestamps: omit timestamps: true from start_context to skip word-timing frames. When enabled, remember that timestamp frames arrive incrementally — append them rather than overwriting.

Why one context beats repeated HTTP calls

This whole document costs one concurrency slot for the duration of the session. The same document sent as one POST /v1/tts per sentence would consume a slot per request, and you’d hit the per-key limit of 9 at nine sentences in flight.