Prerequisites
- A Telnyx API key.
- A mono 16-bit PCM WAV file with a sample rate from 8 kHz to 48 kHz. To convert a file with
ffmpeg:
-ar 8000. Do not upsample 8 kHz audio to a higher rate.
How the examples work
- Read the sample rate from the WAV header.
- Connect with
format=l16, the samesample_rate, andchannels=1. - Send the audio in 20 ms binary chunks, then send
{"type": "end_of_stream"}. - Read messages until the server closes the connection. Keep the binary frames and print JSON errors.
- On close
1000, write the denoised audio todenoised.wav. - On close
1001,1011, or1013, or HTTP503, the failure is temporary. Wait, then send the file again on a new connection, with exponential backoff. Any other close is an error.
1008. Split longer audio into parts and send each part in its own session.
See End of stream for why the examples send end_of_stream instead of only closing the connection.
Code
Real-time streams
For live audio, such as a microphone or a media stream, send each chunk as soon as you capture it and play or forward each binary response as soon as it arrives. Keep chunks small (10–40 ms) for low latency. When the stream ends, send{"type": "end_of_stream"} and keep reading until the server closes the connection.
Sessions are limited to 590 seconds. At the limit, the server returns the remaining audio and closes with 1008 and a max_session_duration error. Reconnect to continue. See Session duration.