Integrations
Echo cancellation
Configure microphone capture and playback routing to reduce speaker echo in browser clients using the Realtime API.
Use acoustic echo cancellation (AEC) when your application plays agent audio through speakers while the microphone is open. Speaker audio can enter the microphone, appear in the user's transcript, and trigger an interruption while the agent is still speaking.
The Realtime API does no server-side echo cancellation. Configure AEC in your browser client. An echo canceller uses a reference signal, a copy of the audio being played, to identify that audio in the microphone input. Requesting AEC is the first step; playback routing and the browser's implementation also affect the result.
This guide covers browser clients, including mobile browsers, using WebRTC or WebSocket. Native mobile applications need their platform's audio capture and playback configuration.
Enable echo cancellation on the microphone
Request echo cancellation when you acquire the microphone:
const stream = await navigator.mediaDevices.getUserMedia({
audio: {
channelCount: 1,
echoCancellation: true,
noiseSuppression: true
}
});Send audio from this stream through your existing capture path: add its track to your WebRTC connection, or pass it to your WebSocket client's PCM encoder. Keep the sample rate and encoding required by your session.
noiseSuppression requests background noise reduction. It serves a different purpose from AEC. The browser decides which playback sources to cancel when echoCancellation is true; see the media capture specification. A track reporting echoCancellation: true in getSettings() does not measure how much echo remains.
Check the playback path
Keep one playback path for each agent audio stream. Test the capture and playback paths together on the browsers and devices your application supports.
WebRTC sessions
Play the remote audio track through an <audio> element, as shown in the WebRTC quickstart. This keeps playback on the browser's WebRTC media path:
Loading diagram…
flowchart LR
A["Remote track"] --> B["audio element"] --> C["Speaker"]If echo persists, check whether your application also plays the agent audio through a separate Web Audio graph. Test with only the remote track playing through the media element.
WebSocket sessions
WebSocket clients decode and schedule received audio themselves. If direct playback through AudioContext.destination produces echo, try routing the final playback node through a MediaStreamAudioDestinationNode into an <audio> element.
Loading diagram…
flowchart LR
A["Decoded PCM"] --> B["Output stream"] --> C["audio element"] --> D["Speaker"]Use the existing playback AudioContext, named ctx below. Create one destination and one playback element per session before requesting the first reply. Keep them in your session state so the code that schedules audio chunks can use them. Start or resume playback from your application's start button:
const playback = ctx.createMediaStreamDestination();
const speaker = document.createElement('audio');
speaker.autoplay = true;
speaker.srcObject = playback.stream;
document.body.appendChild(speaker);
await ctx.resume();
await speaker.play();Connect the final node of your playback graph to playback. For example, replace src.connect(ctx.destination) with src.connect(playback) in the WebSocket quickstart. If you use a gain or analyser node after src, change that node's output connection instead. Keep your existing decoding, buffering, and scheduling.
Remove the previous connection to ctx.destination so audio plays once. If play() rejects, show a control that retries playback from a user action. On session teardown, stop queued audio and microphone tracks, disconnect the graph, clear speaker.srcObject, remove the element, and close any audio contexts owned by that session.
This routing is an approach to test when troubleshooting echo. It does not guarantee cancellation on every browser, operating system, or output device.
Consider a local WebRTC loopback
If echo persists with WebSocket playback, a local WebRTC loopback is another approach to test. Two RTCPeerConnection objects exchange audio within the page so playback arrives as a remote WebRTC track, which the browser can use as an echo reference. The connection to the Realtime API remains WebSocket.
Wire the streams as follows:
| Stream | Route |
|---|---|
| Agent playback | Playback destination stream → peer A → peer B → <audio> |
| Microphone | Captured microphone stream → peer B → peer A → PCM encoder → WebSocket |
Encode the microphone stream received by peer A. Keep the raw microphone stream as the input to peer B.
Before switching to loopback, wait for audio tracks on both receiving peers. Use a setup timeout and restore the direct capture and media-element playback paths if setup fails. Close both peers on failure or session teardown. The browser's RTCPeerConnection reference covers track delivery and connection setup.
Enable this path only on browser and device combinations you have tested.
How the realtime.ai demo handles echo and greetings
The demo requests echoCancellation: true, noiseSuppression: true, and autoGainControl: true when it opens the microphone. On mobile it also requests latency: { ideal: 0.01 }, a preference for 10 ms capture latency. These are browser capture preferences for reference when testing your own client.
With loopback active, the demo plays the remote track through an <audio> element. When loopback is unavailable or setup fails on mobile, the demo uses its direct AudioContext path and sends the playback stream to the same element. It uses this direct path on iPhone Safari.
On speakerphone, the microphone can pick up the greeting itself. On iPhone Safari, turn detection can also report speech in the first second or two as the microphone starts and its gain adjusts, even if the user is silent. The demo lets the greeting play before it starts responding to speech:
- Before requesting the greeting, it sets
create_response: falseandinterrupt_response: falseundersession.audio.input.turn_detection. Speech detection stays on. - For each
input_audio_buffer.speech_startedwhile the greeting is pending or playing, it sendsinput_audio_buffer.clear. This also discards real user speech buffered during the greeting. - When the player finishes the greeting, it clears the input buffer again and sends
session.updateto restorecreate_response: trueand the session's choseninterrupt_responsevalue. It uses playback completion because audio may still be queued whenresponse.donearrives. A fallback timeout restores the settings if the playback callback never arrives.
Verify echo reduction and interruptions
Test with microphone capture enabled throughout agent playback:
- Let a reply play while you stay silent. Check for echoed words in input transcripts and unexpected
input_audio_buffer.speech_startedevents. - Speak while the reply is playing. Check that your speech is captured and that interruption handling still stops the reply as intended.
- Repeat at normal and high speaker volumes, then with headphones or Bluetooth. Repeat after changing the audio route or restarting a session.
- Record the browser, operating system, output device, and active playback path alongside failures. Compare direct playback and loopback where available.
FAQ
On iPhone speakerphone, the agent hears itself and cuts off its own reply. What should I do?
Let the agent speak while you stay silent. If its words appear in the user transcript, repeat the test with headphones. If the echoed words disappear, the microphone is likely picking up the speaker. With interrupt_response: true, turn detection can treat that audio as user speech and interrupt the reply. A speech_started event on its own can also come from background noise or microphone startup.
- Check playback. For WebRTC, play the remote track through a single
<audio>element. For WebSocket, follow the decoded audio to its output. In both cases, check for a second connection playing the same audio throughAudioContext.destination. - Enable echo cancellation. Request
echoCancellation: truewhen opening the microphone. The realtime.ai demo also requestsnoiseSuppressionandautoGainControl. On mobile, it useslatency: { ideal: 0.01 }as a capture latency preference. - If only the greeting cuts off, follow the demo’s greeting sequence. Restore normal turn handling when the greeting finishes playing. If callers can wait, you can briefly mute outgoing microphone audio during the greeting. Callers cannot interrupt while muted.
- Retest at the speaker volumes your callers use. Stay silent through one reply, then speak during the next to confirm that intentional interruptions still work.
I hear the assistant twice. How can I tell if the microphone is picking up echo?
Play one reply while you stay silent. If the assistant’s words appear in the user transcript, repeat the test with headphones. If the words disappear, the microphone was likely picking up the speaker. If you still hear two copies with microphone input muted, check whether your app plays the same audio twice, for example through both an <audio> element and AudioContext.destination. Use a single playback path.
Why does a user turn start when nobody spoke?
A phantom turn happens when turn detection interprets microphone input as speech. On speakerphone, the microphone may pick up the assistant’s voice. In a noisy room, expect occasional false starts from nearby voices or background noise even with echo cancellation enabled. Repeat the test in a quiet room with the same device and speaker volume to check whether background noise contributed.
For noisy calls, you can deploy the streaming variant of the open-source Inworld Voice Isolator on your voice server before forwarding microphone audio to the Realtime API. It can isolate the foreground speaker from background noise and quieter voices. If two people speak at similar volumes, both voices may come through.
Can I mute the microphone while the call starts?
Yes. If callers can wait until the greeting ends, try briefly muting the audio you send while keeping microphone capture and assistant playback running. This gives the browser’s echo canceller time to adapt to the speakerphone while keeping startup noise out of the session. Restore outgoing audio when the greeting finishes playing. Callers cannot speak to the assistant or interrupt it while muted.
On WebSocket, you can keep capturing audio and pause sending microphone chunks. On WebRTC, if your client processes the microphone before sending it, mute the outgoing audio after capture. Test on your target devices because the time echo cancellation takes to settle varies.
Does disabling interruptions remove echo?
No. Setting interrupt_response: false changes how the server responds to detected speech. The microphone can still capture the assistant’s voice, and echoed words can still appear in the transcript. Check microphone capture and playback routing if you see those words.