Skip to content

Architecture

Speech recognizers differ a lot: in threads, in errors, in who decides that a phrase is over. This document describes how expo-stt makes them behave the same, and why it is built this way.

Diagram source: expo-stt.en.drawio. It opens in app.diagrams.net, draw.io desktop, or the Draw.io extension of VS Code.

Architecture

Layer Responsibility Knows nothing about
useHoldToTalk The gesture: press, slide, release Recognizers
useSpeechRecognition, listen Turn events into React state, or into one promise Platforms
controller Owns the one session, asks for permission, delivers events to their owner Audio
ExpoSttModule Bridge to JavaScript, permissions, app lifecycle Rules of a session
SpeechSession The rules: states, order of events, what an error means Audio, the platform
SpeechEngine Drives the recognizer and the microphone, owns the timers JavaScript

On web, the engine is part of ExpoSttModule.web.ts.

Everything else follows from these rules, which hold on every platform:

  1. A session that emits start emits exactly one end.
  2. error comes right before the end whose reason is error.
  3. A session that fails before start emits nothing: start() rejects.
  4. An ended session ignores everything.
  5. The recognizer is released before the last events are emitted, so a listener can start again from end.

States of a session

SpeechSession implements them without any platform API. It exists three times, in TypeScript, Kotlin and Swift, with the same test cases. A shared implementation would need C++ for about 150 lines of code: the duplication is cheaper, as long as the tests stay the same.

Silence is a good example of what the rules unify. The same silence means no-speech when the recognizer gave up, completed after stop(), and nothing at all in a continuous session.

listen()

listen() is a session with an owner of its own. The controller compares that owner with the one of the running session, so a listen() never sees the events of a hook, and two hooks never see each other’s.

There is one subscription per event to the native module, inside the controller. It clears the session before it delivers end. With a subscription per listener, the order would depend on who subscribed first, and restarting from end would sometimes fail with busy.

Continuous session

Recognizers end after a phrase. A continuous session starts the next one by itself, and to JavaScript it stays one session with one start and one end.

Between two phrases
iOS The microphone stays open, a new request replaces the old one
Android A new SpeechRecognizer. If the service is still busy, up to 3 more attempts
Web A new SpeechRecognition

The pause of 150 ms prevents a busy loop when a recognizer ends at once.

Two timers make up for recognizers that do not decide or do not answer. silenceTimeout starts again with every interim result and ends the phrase. After that, the recognizer has 5 seconds for its final result: then the last interim result becomes final.

Hold to talk

A press is an object of its own, from the touch until the session is over. This outlives the touch on purpose: after the release there are still words to come (releaseDelay), then a final result. A new press in the meantime is ignored.

The reasons for onCancel are checked in this order: slid away, shorter than minDuration, microphone not open yet. A quick tap is too-short even when the microphone had no chance to open, because that is what the user can do something about.

Failures and races

Every recognizer error is mapped to one of ten codes, in SpeechEngine or webSpeech.ts. An app can handle them without knowing the platform.

The permission dialog is the hardest race: it can stay open for minutes, while the user leaves the screen. Until the native session exists, stop() and abort() only mark the session as cancelled.

Session and engine Other threads
iOS Main queue The audio thread only touches AudioSink, which has a lock
Android Main thread, as SpeechRecognizer requires None
Web The only one None
  • AudioSessionManager saves the category, mode and options of the app’s audio session, and puts them back. Deactivating the session lets other apps resume their audio.
  • The format of the microphone is checked before the tap is installed. An invalid one raises an Objective-C exception, which Swift cannot catch.
  • iOS listens forever: the engine ends a phrase in which nothing is said after 8 seconds.
  • Microphone and speech recognition are one permission to JavaScript, with a requester of its own, so that it does not replace the microphone requester of another module.
  • Recognition happens in another process, which may never answer: a start that takes longer than 10 seconds fails with service-unavailable.
  • Error codes are numbers in the source, because some constants are newer than the minimum SDK.
  • The permission and the <queries> entry are in the manifest of the library, so there is no config plugin for Android.
  • The volume is measured by volumeMeter.ts from a second stream, only while something listens to it, and not on phones, which cannot share the microphone.
  • Chrome and Edge work right away. Firefox asks to download a language pack first.
What Where
Rules of a session SpeechSession-test.ts, SpeechSessionTest.kt, SpeechSessionTests.swift
Web engine, with a fake recognizer ExpoSttModule.web-test.ts
Controller, listen, useSpeechRecognition useSpeechRecognition-test.tsx
Gesture useHoldToTalk-test.tsx
Config plugin withSpeechRecognition-test.ts
Demos in a browser website/scripts/screenshots.mjs

The native engines have no automated tests: they need a recognizer and a microphone. CI compiles them. The Swift tests are not run in CI yet.

Alternative Why not
Google’s dialog on Android (ACTION_RECOGNIZE_SPEECH), as in 0.x No interim results, no stop, no UI of your own
SpeechAnalyzer on iOS Newer than the minimum iOS version. A candidate for a second engine behind the same session
Several sessions at once There is one microphone, and recognizers refuse a second client
Returning transcripts only Apps would guess what a missing one means: silence, an error, or a cancellation
  • Android: words spoken while the next phrase starts may be lost, and some services play a sound at each start.
  • Android: a phone call is only noticed when it sends the app to the background.
  • iOS: some error codes are undocumented and may change. Unknown ones become unknown.
  • Web: a second stream for the volume may compete with the recognizer on some systems.
  • Changing the recognizer’s language requires a new session.