DEV Community

Cover image for Live call subtitles in Flutter: each speaker's own microphone
Frank Milvus
Frank Milvus

Posted on Originally published at frankmilvus.github.io

Live call subtitles in Flutter: each speaker's own microphone

In a language-exchange app, text is the easy half. A message can be translated before you read it, stored, reread, and checked against the original. A call gives you none of that. The other person starts speaking in a language you are still learning, the sentence is gone in four seconds, and the only recovery is the phrase every learner knows: sorry, could you say that again?

Live subtitles close that gap: a CC button on the call screen shows what each participant said, in the reader's own language, while the call continues. The interesting part is not the button. It is the decision about whose microphone gets transcribed — and that decision came from a limit in the WebRTC stack.

The audio you want is the audio you cannot have

The obvious design is the one a hearing person might sketch: take the incoming call audio, run it through speech recognition on the receiver, and display the text. One model, one stream, done.

That design does not survive contact with flutter_webrtc. The package gives you a local stream and a remote stream, and both are handles into the platform's WebRTC pipeline, not raw sample buffers. The remote handle is meant to be rendered by a platform view or an RTCVideoRenderer. Pulling PCM out of it means reaching past the plugin into each platform's audio graph, on Android, iOS, macOS, Windows and Linux, while the call is already using that graph for echo cancellation and mixing. Every platform would be a separate, fragile implementation, and the extraction would compete with the thing keeping the call intelligible.

There is a second reason to avoid it. A live call is the one place where audio is continuous, personal, and expensive to move. Streaming the peer's audio to a recogniser adds a second copy of a conversation that may already be end-to-end encrypted, and no amount of care makes that copy disappear.

So we inverted the problem.

Everyone in the call is already holding a microphone

Each participant's device has a microphone, and it is already capturing that participant's speech for the call. Recognising your own microphone and sending the resulting text is a much smaller problem than extracting the other side's audio:

  • the samples are local, so no new audio leaves the device;
  • the platform's capture API is a normal Flutter plugin, the same one that records voice messages;
  • text is tiny compared with audio, so the relay is cheap even on a weak link;
  • the speaker is known by construction — your device only transcribes you, so there is no speaker-diarisation problem to solve.

The pipeline ends up looking like this:

Speaker A                                          Listener B
---------                                          ----------
microphone (PCM16, 16 kHz)
  -> voice-activity segmenter
  -> one WAV per utterance
  -> existing speech-to-text service
  -> caption shown locally
  -> caption text over the call's signalling channel
                                                    -> translate into B's language
                                                    -> subtitle band on B's screen
Enter fullscreen mode Exit fullscreen mode

Both directions run at once. Each person sees their own words immediately and the other person's words after recognition and translation.

From a stream of samples to a sentence

Speech recognition wants utterances, not a firehose. The capture stream is 16 kHz mono PCM16, delivered in chunks whose boundaries have nothing to do with sentences. A small voice-activity detector cuts it into pieces that a recogniser can handle:

CallCaptionSegmenter(
  onUtterance: (wav) => transcribe(wav),
  endSilenceMs: 700,     // a natural pause ends the utterance
  minUtteranceMs: 400,   // drop coughs and door slams
  maxUtteranceMs: 6000,  // cap a monologue
  preRollMs: 300,        // keep the first syllable
)
Enter fullscreen mode Exit fullscreen mode

The details matter more than the API:

  • Frames, not bytes. The segmenter analyses 20 ms frames and carries any leftover bytes into the next chunk, so a chunk boundary in the middle of a frame does not become a click or a lost syllable.
  • A short pre-roll. It keeps the last 300 ms of audio before speech starts, because the first consonant is usually the quietest part of the sentence.
  • Three voiced frames to start, a real pause to stop. A single loud frame is a door; three in a row is a voice. A pause of 700 ms ends the utterance, and the trailing silence is trimmed before packaging.
  • A hard cap. A speaker who does not pause for six seconds gets cut at six seconds. The transcript is still useful, and one long request cannot hold the queue forever.
  • Short blips are dropped. Anything under 400 ms of voiced audio is unlikely to produce a useful line and likely to produce a hallucinated one.

The output is a complete WAV per utterance, posted to the same speech-to-text service that already transcribes voice messages. No new model, no new server: the only new work is on the client.

One transcription at a time

A conversation produces utterances faster than a batch recogniser returns them. Without a queue discipline, a talkative minute creates a backlog that keeps growing, and every caption arrives further behind the conversation.

The service keeps one request in flight and at most two utterances waiting. When a third arrives, the oldest waiting one is dropped. That is a deliberate trade: a caption that arrives after the topic has moved on has no value, so the system prefers to lose an old line than to fall further behind. A small gap between requests gives the client a chance to breathe and keeps a short burst from turning into a spike.

Two more details keep the band honest:

  • Repeated lines are suppressed. A batch recogniser can repeat the end of one chunk at the start of the next. An identical line arriving within eight seconds is dropped instead of shown twice.
  • Your own captions are not translated. You know what you said; translating it back into the language you just spoke would waste a request and add a confusing second line.

Latency is real and worth stating plainly: this is near-live, not word-by-word. A short sentence usually appears within a couple of seconds. Long sentences wait for the pause at the end, because the recogniser needs an utterance boundary. Subtitles are for following the conversation, not for lip-sync.

Relay text, not audio

The caption leaves the speaker's device as text over the call's existing signalling connection — the same channel that already carries call state and control messages. There is no second transport to secure, and no audio to relay.

Each caption carries a sequence number. The receiver keeps the highest one it has seen and drops anything at or below it, so a retransmitted or out-of-order message cannot make the band jump backwards. Text is capped at 600 characters, which is far beyond a useful spoken line but bounds a pathological transcript.

Captions are transient by design:

  • nothing is written to a database;
  • nothing is added to the call log;
  • nothing is pushed to a notification;
  • the on-screen buffer holds the last 30 lines in memory, and the band renders only the last two.

When the call ends, the captions are gone. That is a product decision as much as a privacy one: a call is a conversation, not a document.

Translate on the side that is reading

Only the receiver translates. The speaker's own line is shown as recognised; the arriving line goes through the app's normal translation fallback chain, targeting the language that reader has chosen for messages.

Two refinements make that feel local rather than global:

  • A call-local language override. A globe button beside the CC toggle opens a picker that starts at "Same as messages". Choosing Spanish there changes the subtitle language for this call only. It does not change the language the user reads chat or feed in, so following one call in another language does not quietly rewrite the whole app.
  • A generation counter. When the language changes, the visible captions are cleared and any translation still in flight is discarded. Without that, a slow request from the previous language could land after the switch and paint an old-language line into the new band.

If the translation comes back identical to the original — the speaker was already using the reader's language — the duplicate is suppressed and only one line is shown. When a translation does appear, it carries the same source hint the chat and feed use: Translated · Folcadi AI or Translated · Offline, so the reader knows which engine actually served it.

Do not let one feature eat the budget

Our translation stack offers a paid engine for the highest-quality results, with a usage budget attached. Live captions must never use it.

A busy call can produce hundreds of short utterances. Each one is a translation request. Even at a modest rate, a single long call would exhaust a daily character allowance built for reading messages, and the damage would not stay inside the call: chat and feed translation would start falling back for the rest of the day.

The rule is therefore explicit in the caption path: if the requested engine is the paid one, captions downgrade to the free engine (or the on-device model) before the request is made. The reader still sees a source hint, so the downgrade is visible rather than silent.

The general lesson is not "paid translation is bad". It is that a per-request budget is not a per-feature budget. A feature that can issue requests in a tight loop needs its own ceiling, decided at the feature boundary, not discovered when another surface starts degrading.

Start on answer, stop on mute

Capturing a microphone continuously is not something a call app should do casually, so the lifecycle is conservative:

  • capture starts only when the call is answered;
  • it stops when subtitles are switched off, when the microphone is muted, when the app goes to the background, and when the call ends;
  • it resumes only if the call is still active and subtitles are still on and the microphone is not muted;
  • the feature is off by default, because it records and uploads speech chunks, and a call should not silently start doing that.

The app-lifecycle check has one sharp edge worth copying. Flutter's inactive state fires for the notification shade, Control Centre, and the operating-system permission dialog — not only for a real trip to the background. Treating inactive as "stop capturing" makes the feature flicker off while the user is merely reading a notification. Only resumed counts as foreground; everything else stops capture.

Encrypted calls stay out of it

Live subtitles are not available in encrypted conversations, and the refusal is layered rather than cosmetic.

The call offer carries an encryption flag, so both clients know the conversation is encrypted before the call starts. Both hide the CC button and never start capture. The relay also refuses caption events for an encrypted conversation, so an older or modified client cannot push plaintext captions into an encrypted call through the signalling channel.

That is the honest position. A caption is plaintext by definition — the speaker's phone recognises the words and sends them as text. Offering the feature in an encrypted call, or letting the relay carry it, would quietly turn the encrypted conversation into a partially plaintext one. The feature is better absent than misleading.

The interface: two buttons and two lines

The call chrome gained a CC toggle next to the minimise control, and a globe button that appears beside it only while captions are on. The band above the call controls shows the last two utterances with a speaker label, either Translated only (the default, matching chat) or Original and translation, where the recognised speech sits above the translation.

Settings → Calls carries the same switch, the display-mode choice, and the language picker, so the feature can be configured before a call rather than hunted for during one. The layout is identical for audio and video calls, and the band stays out of system picture-in-picture and the end-of-call card.

The camera stayed on

One fix in the same change had nothing to do with subtitles and everything to do with native resources. Ending a video call on Linux could leave the camera indicator lit. Stopping the media tracks was not enough: a renderer still held a reference to the stream, so the platform kept the device open. Clearing the renderers on call end released it promptly.

The general rule is that a native resource is owned by everything that can still reach it, not only by the object that created it. Stopping the track ends the flow; dropping the last reference releases the device.

What it does not do

Worth being explicit, because "live subtitles" can promise more than it delivers:

  • No group calls. The feature is built for 1:1 audio and video, where each participant has one microphone and one reader.
  • No native streaming recogniser. The app uses the same server-side recogniser as voice messages, which works identically on every platform and in every supported language. An operating-system recogniser could be faster on mobile, but it would open a second microphone client alongside WebRTC and would not cover desktop.
  • No word-by-word karaoke. Utterances are cut at pauses, so text lands in phrases.
  • No stored transcript. If you want a record, this is not it.
  • No captions in encrypted chats. Explained above, and enforced on both sides.

What I would take to another app

Three things generalise beyond call subtitles:

  1. Invert the data flow before you fight the platform. We could have spent weeks extracting remote audio on five platforms. Recognising the microphone we already owned turned the same problem into a client queue and a text relay.
  2. Put the queue in front of the expensive step. One request in flight and a two-item buffer is the difference between a feature that follows a conversation and one that narrates the past.
  3. Give a chatty feature its own budget. A paid engine that is correct for messages can be ruinous for a call. Decide the ceiling where the loop lives.

A call is the hardest surface to make readable, because it refuses to wait. The version that works is the one that accepts the constraint instead of hiding it: each person's phone listens to its own speaker, sends the words, and lets the other side read them.

If you build a messenger with calls, what would you caption first: the speech, the connection state, or the translation itself?


Get Folcadi — a language-exchange messenger with live translation.

Top comments (0)