Use this file to discover all available pages before exploring further.
When you stream a pre-recorded audio file to the Streaming API, you need to send audio at the same pace it was recorded. If you send audio faster than real time, the server receives more data than it can process in sequence, which can cause degraded transcription accuracy, unexpected session closures, or other errors.This guide shows you how to pace audio correctly so that the server processes it as if a person were speaking into a live microphone.
The Streaming API is designed for live audio. It expects audio to arrive at roughly the same rate it was originally spoken. When you stream a pre-recorded file without any pacing, your code reads and sends the entire file in seconds, even if the recording is minutes long. This causes problems:
Unexpected session behavior — Sending audio faster than real time can overwhelm the connection and cause the server to close the session or return errors.
Inaccurate results — The speech model is optimized for real-time input. Audio that arrives too quickly may not be processed the same way as live speech, potentially affecting transcription quality.
Unreliable benchmarks — If you’re evaluating transcription quality, faster-than-real-time streaming produces results that don’t reflect production conditions where audio arrives at normal speed.
If you only need a transcript and don’t need real-time results, use the pre-recorded transcription API instead. It processes audio as fast as possible and is optimized for batch workloads.
The Streaming API accepts raw audio samples. WAV is the simplest format to work with because it contains uncompressed PCM data that you can read directly.Your audio file must be:
Mono (single channel)
16-bit PCM encoding
A sample rate that matches the sample_rate connection parameter
If your file doesn’t meet these requirements, convert it with FFmpeg:
The key to simulating real-time audio is wall-clock pacing. Instead of calling sleep for a fixed duration after each chunk (which accumulates drift from processing time), track elapsed time from the start and sleep only until the next chunk is due.Here’s the difference:Naive approach (not recommended) — Fixed sleep after each send. Processing time adds up, so audio arrives progressively later than real time:
# Don't do this for benchmarkingwhile True: frames = wav_file.readframes(frames_per_chunk) ws.send(frames) time.sleep(chunk_duration) # Drift accumulates over time
Wall-clock approach (recommended) — Calculate when each chunk should be sent based on the start time. This self-corrects any drift:
Python
JavaScript
start_time = time.monotonic()chunks_sent = 0while not stop_event.is_set(): frames = wav_file.readframes(frames_per_chunk) if not frames: break ws.send(frames, websocket.ABNF.OPCODE_BINARY) chunks_sent += 1 # Sleep until the next chunk is due next_chunk_time = start_time + (chunks_sent * CHUNK_DURATION) sleep_duration = next_chunk_time - time.monotonic() if sleep_duration > 0: time.sleep(sleep_duration)
const audioData = readFileSync(AUDIO_FILE); // Raw PCM bytes from WAV data chunkconst bytesPerSample = 2; // 16-bit PCMconst bytesPerChunk = Math.floor(SAMPLE_RATE * CHUNK_DURATION) * bytesPerSample;let offset = 0;let chunksSent = 0;const startTime = Date.now();function sendNextChunk() { if (offset >= audioData.length) { ws.send(JSON.stringify({ type: "Terminate" })); return; } const chunk = audioData.subarray(offset, offset + bytesPerChunk); ws.send(chunk); offset += bytesPerChunk; chunksSent++; // Schedule the next chunk at the correct wall-clock time const nextChunkTime = startTime + chunksSent * CHUNK_DURATION * 1000; const delay = nextChunkTime - Date.now(); setTimeout(sendNextChunk, Math.max(0, delay));}
This approach uses time.monotonic() (Python) or Date.now() (JavaScript) to track elapsed time from the start of streaming. Each chunk is scheduled based on its position in the file, not relative to the previous chunk. If one iteration takes longer than expected, the next chunk is sent sooner to catch up — keeping the overall pace at real time.
After you send all audio, send a Terminate message so the server can flush its buffers and return any remaining transcripts:
Python
JavaScript
ws.send(json.dumps({"type": "Terminate"}))
ws.send(JSON.stringify({ type: "Terminate" }));
The server responds with a Termination message that includes the total audio duration processed. Wait for this message before closing the WebSocket connection so you don’t miss any final transcripts.
The CHUNK_DURATION value controls how much audio you send in each message. Common values:
100ms (0.1) — Good default. Balances network overhead with smooth pacing.
50ms (0.05) — More closely simulates microphone input. Use this if you want behavior closest to a live mic stream.
200ms (0.2) — Fewer network calls, slightly less real-time feel. Acceptable for most benchmarks.
Smaller chunks send more WebSocket messages but more closely approximate continuous microphone input. For benchmarking, 100ms is a good starting point.