Repository navigation
feat(mobile): transcribe dictation on iPhone with Apple's on-device speech model - #39410
Merged
Merged
Conversation
Contributor
|
Warning Review limit reachedYou'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. View limit details
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. |
AmoabaKelvin
force-pushed
the
mobile-ondevice-ios
branch
from
November 7, 2026 08:50
4b25f42 to
141178a
Compare
10 of 11 tasks
…op a finishing session on cancel
…eport the simulator unsupported
…rsion when formats match
AmoabaKelvin
force-pushed
the
mobile-ondevice-ios
branch
from
November 7, 2026 09:39
141178a to
23a49cc
Compare
This was referenced Nov 7, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
What Changed
Builds on #39386 (merged), which defines the contract in
mobile/packages/expo-two-way-audio/src/speech.tsand wires the page, the shell and the Voice setting to it. This PR only adds Swift inmobile/packages/expo-two-way-audio/ios:speechAvailability(locale)answersready,needs-downloadorunsupportedwith the resolved BCP-47 locale. It never prompts and never downloads.speechDownload(locale)reserves the language withAssetInventoryand installs its assets, reportingonSpeechDownloadProgress. 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.speechFeedcopies the 16-bit PCM the mic already delivers.speechFinishdrains every fed sample and returns the whole text.speechCancelstops the live session, or one that is still finishing.onSpeechText {revision, committed, tentative}: volatile results becometentativeand final results are appended tocommitted.onSpeechErrorfires when a live session dies.SpeechAnalyzer. It usesSpeechTranscriberwhere the hardware supports it (isAvailable), and otherwiseDictationTranscriber, the keyboard-dictation model, which older iPhones run. Audio passes throughAVAudioConverteronly when the analyzer wants a format other than the one fed in.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 fromsupportedLocalesin 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.AssetInventory.reservefirst. At the per-app limit it releases another variant of the same language first.unsupported, so a device that can't run the model goes to the computer instead of failing after start.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.
onSpeechDownloadProgressTesting
OnDeviceSpeechSession.swiftand the same Speech framework). Asayclip fed as 16-bit PCM in 1 KB chunks every 32 ms, the same shapespeechFeedreceives. The first chunks were fed beforereadyresolved. Tentative words arrive about 1 s behind the speech, then each final is committed:finishreturns in 0.02–0.46 s after the last chunk.hasNativeSpeech()is true.speechAvailability:xx-XXgives unsupported,de-DEgives needs-download, andenresolves to en-US.speechStart('xx-XX')gives{started:false, reason:'unsupported'}.speechFinishwith no session rejects.speechDownload('en'): 456 MB with progress events from 0 to 1, thentrue. After it,enreads unsupported, because the simulator can't run the model.onMicrophoneData(~32 KB/s) and stops ontoggleRecording(false). The computer path is unaffected.SpeechTranscriber.isAvailableis false on a stock simulator too, andDictationTranscriberfails 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.#available(iOS 26.0, *), and no older-iOS runtime is installed here.Risk
The riskiest part is the session state in
OnDeviceSpeechController: start, finish and cancel racing withspeechFeedon the JS thread. A generation counter under anNSLockguards 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:
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
speechStartreturns.Agent skill upstream boundary
Notes
Locale(identifier:).For Kelvin
speechStartanswersneeds-download, and only an explicitspeechDownloadfrom the phone's dictation setting downloads. The setting should show the size. Until the language is installed, Automatic uses the computer.SpeechTranscribersupport use Apple'sDictationTranscriber(the keyboard-dictation model). Coordinator approved.NSSpeechRecognitionUsageDescriptionapply only to the server-basedSFSpeechRecognizer, and that SpeechAnalyzer transcriber modules don't send audio to Apple's servers. In my macOS run, a process whoseSFSpeechRecognizer.authorizationStatus()wasnotDeterminedtranscribed normally. The only permission prompt is the existing microphone one.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)