Skip to content

feat(mobile): transcribe dictation on iPhone with Apple's on-device speech model - #39410

Merged
AmoabaKelvin merged 4 commits into
mainfrom
mobile-ondevice-ios
Nov 7, 2026
Merged

AmoabaKelvin merged 4 commits into
mainfrom
mobile-ondevice-ios

Conversation

@AmoabaKelvin

@AmoabaKelvin AmoabaKelvin commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

ELI5

On an iPhone with iOS 26 or later, dictation can now turn speech into text on the phone itself, using Apple's built-in speech model, instead of sending the audio to the paired computer. This PR is the iPhone half of that: the native code behind the on-device contract that #39386 defines.

Files Added Deleted Net
Test 0 0 0 0
Prod 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​547 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​546

What Changed

Builds on #39386 (merged), which defines the contract in mobile/packages/expo-two-way-audio/src/speech.ts and wires the page, the shell and the Voice setting to it. This PR only adds Swift in mobile/packages/expo-two-way-audio/ios:

  • speechAvailability(locale) answers ready, needs-download or unsupported with the resolved BCP-47 locale. It never prompts and never downloads.
  • speechDownload(locale) reserves the language with AssetInventory and installs its assets, reporting onSpeechDownloadProgress. It runs only when called; nothing downloads by itself.
  • speechStart(locale, 16000) answers after the locale and asset check (~0.1 s). The model warms up afterwards, and PCM fed in the meantime waits in an unbounded native queue. speechFeed copies the 16-bit PCM the mic already delivers. speechFinish drains every fed sample and returns the whole text. speechCancel stops the live session, or one that is still finishing.
  • onSpeechText {revision, committed, tentative}: volatile results become tentative and final results are appended to committed. onSpeechError fires when a live session dies.
  • The engine is SpeechAnalyzer. It uses SpeechTranscriber where the hardware supports it (isAvailable), and otherwise DictationTranscriber, the keyboard-dictation model, which older iPhones run. Audio passes through AVAudioConverter only when the analyzer wants a format other than the one fed in.
  • Before iOS 26, every function exists and answers unsupported, so the page uses the computer.

The existing capture code (AudioEngine, toggleRecording, onMicrophoneData) is unchanged. The recognizer never opens the mic.

Why

Feeding PCM from JS, rather than tapping the engine natively, was the contract owner's call. It keeps one owner of the mic and lets the page keep the audio for a fallback to the computer. Things these choices fixed, found while testing:

  • supportedLocale(equivalentTo:) gives a different region on each call for a bare language or an unsupported region. "en" came back as en-NZ, en-IE, en-GB or en-US. So availability, download and start could each pick a different locale. Locales are now picked from supportedLocales in this order: the requested region, the regions of the phone's own languages, the phone's region, then the language's usual region. The result is stable.
  • Without a reservation, the download failed with "not subscribed to assistant.en_US". The code now calls AssetInventory.reserve first. At the per-app limit it releases another variant of the same language first.
  • The simulator reports the assets as installed but has no audio format it can run them with ("Model not available"). An installed locale with no compatible audio format now reads unsupported, so a device that can't run the model goes to the computer instead of failing after start.
  • The implementation handles two traps: wait for the result collector before finishing, so the last words aren't lost, and treat an unsupported locale as its own answer.

Linked Issue

Part of the dictation plan, Wave 2 (W2.2). No GitHub issue.

Visual Proof

The phone's Voice setting ("Transcribe on", from #39386) running on this native module in an iOS 27 simulator. The simulator can download Apple's speech assets but can't run the model, so the last state is what it honestly reports.

Language that needs a download (en-GB) Downloading, progress from onSpeechDownloadProgress Installed, but this simulator can't run the model

Testing

  • The engine on real speech (macOS 26+, the same OnDeviceSpeechSession.swift and the same Speech framework). A say clip fed as 16-bit PCM in 1 KB chunks every 32 ms, the same shape speechFeed receives. The first chunks were fed before ready resolved. Tentative words arrive about 1 s behind the speech, then each final is committed:
    committed="" | tentative="Please open the pool request and run the tests on the main branch before lunch"
    committed="Please open the pool request and run the tests on the main branch before lunch." | tentative=""
    FINAL (finish took 0.46s)
    
    A three-sentence clip with pauses commits three finals with their spacing intact ("First, we fix the login bog. Then we ship the release on Friday. Thanks, everyone."). finish returns in 0.02–0.46 s after the last chunk.
  • The module in the app on an iOS 27 simulator (dev build from this branch, temporary probe route, not committed):
    • hasNativeSpeech() is true.
    • speechAvailability: xx-XX gives unsupported, de-DE gives needs-download, and en resolves to en-US.
    • speechStart('xx-XX') gives {started:false, reason:'unsupported'}. speechFinish with no session rejects.
    • A real speechDownload('en'): 456 MB with progress events from 0 to 1, then true. After it, en reads unsupported, because the simulator can't run the model.
    • The mic still records PCM through onMicrophoneData (~32 KB/s) and stops on toggleRecording(false). The computer path is unaffected.
  • Not covered: a spoken clip transcribed in the iOS simulator. Apple's on-device models don't run there (SpeechTranscriber.isAvailable is false on a stock simulator too, and DictationTranscriber fails with "Model not available"). I had no physical iPhone, so the recognition path on a device was exercised only through the identical engine code on macOS.
  • Before iOS 26: checked by reading the code. Every function is behind #available(iOS 26.0, *), and no older-iOS runtime is installed here.
  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below: no tests added. The behaviour is Apple's framework, which we can't run in CI, and the package has no Swift test harness in CI.
  • Every added test fails without this change and avoids the test anti-patterns

Risk

The riskiest part is the session state in OnDeviceSpeechController: start, finish and cancel racing with speechFeed on the JS thread. A generation counter under an NSLock guards it, and two review rounds went through it. If it went wrong, a user would see on-device dictation fail and the page fall back to the computer, which keeps the audio. Capture itself isn't touched. On a device that can't run the models, availability says unsupported and the computer path runs as it does today. This is native code, so it ships with the next store release. Older shells answer unsupported through the contract's feature check. To back it out, revert the PR.

Level: Medium

AI Disclosure

Review

Two review rounds (Claude). Fixed:

  • a cancel during finish left the finishing session running;
  • text events were emitted outside the lock;
  • no cancel ran on module destroy;
  • the pump kept converting its backlog after a cancel;
  • the locale lookup wasn't deterministic;
  • the reservation release picked an arbitrary locale.

Not fixed (P3s): flushing the converter's tail for input that isn't 16 kHz (the input is always 16 kHz), and a narrow "busy" vs "failed" reason when a session dies before speechStart returns.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Security: the PCM comes from our own shell. Locale strings only feed Locale(identifier:).
  • Cross-platform: iOS only. Android is a separate worker and PR.
  • Remote SSH: not affected.
  • Mobile: no wire change. The contract and the feature check are in feat(mobile): transcribe dictation on the phone, with the computer as fallback #39386.
  • Backwards compatibility: the contract's typeof check treats older shells as unsupported.
  • Performance: per 32 ms chunk, one copy into a buffer and one queue push. Conversion runs off the JS thread.

For Kelvin

  • The on-device language download is large (en-US is 456,199,168 bytes, ~456 MB; it took ~25 min on the simulator). Rule: it never starts automatically. speechStart answers needs-download, and only an explicit speechDownload from the phone's dictation setting downloads. The setting should show the size. Until the language is installed, Automatic uses the computer.
  • Devices without SpeechTranscriber support use Apple's DictationTranscriber (the keyboard-dictation model). Coordinator approved.
  • A bare language picks the region from the phone's own languages, then the phone's region, then the language's usual region ("en" gives en-US, "pt" gives pt-BR, "zh-TW" gives zh-TW).
  • When the per-app limit on reserved languages is reached, downloading a new one releases another reservation, a variant of the same language first.
  • No new user-facing strings and no new permission. Apple's "Asking Permission to Use Speech Recognition" page says the authorization and NSSpeechRecognitionUsageDescription apply only to the server-based SFSpeechRecognizer, and that SpeechAnalyzer transcriber modules don't send audio to Apple's servers. In my macOS run, a process whose SFSpeechRecognizer.authorizationStatus() was notDetermined transcribed normally. The only permission prompt is the existing microphone one.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@AmoabaKelvin
AmoabaKelvin changed the base branch from mobile-ondevice-seam to main November 7, 2026 08:49
@coderabbitai

coderabbitai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 6 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2affefaa-3bf0-4361-b192-00409dcca623
Commits

Reviewing files that changed from the base of the PR and between ffaf887 and 23a49cc.

Files selected for processing: 3

  • mobile/packages/expo-two-way-audio/ios/ExpoTwoWayAudioModule.swift
  • mobile/packages/expo-two-way-audio/ios/OnDeviceSpeechController.swift
  • mobile/packages/expo-two-way-audio/ios/OnDeviceSpeechSession.swift
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant