Skip to main content
This Python example sends Protobuf messages with rime.v1.binary and writes PCM audio chunks to a file as they arrive. It needs no audio device. The endpoint, synthesis parameters, and lifecycle are the same as in the JSON quickstart.

Prepare the client

Install uv and Python 3.11 or later. Create a Rime API key and set RIME_API_KEY in your server’s environment. Create a project and install the transport client and message definitions:
rime-api supplies generated Protobuf classes and schema files. Your application still uses a standard WebSocket client to connect, authenticate, send messages, and manage retries. The LiveKit plugin uses these same message classes.

Stream audio to a file

Save this as stream_binary.py. It overwrites output.pcm in the current directory. Decode each server message as WebSocketResponse, then read its audio field. The full WebSocket message includes Protobuf metadata and cannot go directly to a player.
Run the script after setting your API key:
A successful run prints a request ID and Saved output.pcm. The file contains raw mono, signed 16-bit little-endian PCM at 24 kHz, with no WAV header. A failed run can leave a partial file. To stream input over time, replace the fixed list in send_sentences with complete sentences from your application. To route audio to a browser, phone system, or player, replace output.write(...) with your audio handler. Empty Protobuf messages still need presence. Call request.end.SetInParent() or request.cancel.SetInParent() to select these payloads. Constructing a request with only a context_id sends no operation.

Run the audio demo

Download the audio demos, extract the archive, and open its coda-audio-demos directory. With RIME_API_KEY set and a local audio output device available, run:
This version plays PCM audio as it arrives and prints Playback complete after the last samples play. The archive includes the playback helper and a README with audio-device setup and troubleshooting.

Generate bindings for another language

The installed package includes schema/rime/text_to_speech.proto and schema/text_to_speech.asyncapi.yaml. Use the Protobuf source to generate messages for your language. The AsyncAPI file describes the JSON encoding. This command copies the installed Protobuf schema to your project:
Generate rime.WebSocketRequest and rime.WebSocketResponse with your language’s Protobuf compiler. Each WebSocket message carries one serialized message. Do not add a gRPC header, length prefix, base64 layer, or custom framing. The schema defines gRPC services too, but this WebSocket client only needs the message types. Use the Coda WebSocket API reference for message fields, connection reuse, cancellation, and errors.