React Native plugin for KeenASR offline speech recognition SDK (iOS & Android).
KeenASR is an offline speech recognition SDK for iOS and Android. For a general overview of SDK concepts, FAQ, and support, please see the KeenASR documentation. This React Native plugin provides a TypeScript API that wraps the native SDK, giving you on-device speech recognition without requiring a network connection.
import KeenASR, { onFinalResponse } from 'keenasr-react-native';
// 1. Initialize with an ASR bundle from app resources
await KeenASR.initialize('keenA1m-nnet3chain-en-us');
// 2. Create a decoding graph with expected phrases
await KeenASR.createDecodingGraphFromPhrases('greetings', [
'YES', 'NO', 'HELLO', 'GOODBYE',
]);
// 3. Prepare the recognizer with the 'greetings' graph
await KeenASR.prepareForListening('greetings');
// 4. Subscribe to results
const unsubscribe = onFinalResponse((response) => {
console.log('Recognized:', response.asrResult.text);
// do something with the response/result
// Always release the response to free native resources
response.release();
});
// 5. Start listening
await KeenASR.startListening();
An ASR bundle is a language-specific asset containing a pre-trained speech recognition model. The plugin ships with a default ASR bundle that is automatically included in your app at build time.
To initialize with the bundled model, pass its name to initialize:
await KeenASR.initialize('keenA1m-nnet3chain-en-us');
To use a different ASR bundle:
android/app/src/main/assets/.Then pass its name to initialize. Alternatively, if the bundle is downloaded at runtime (e.g., to the app's documents directory), use initializeWithPath with the full filesystem path.
A decoding graph defines what the recognizer can recognize. Create one from a list of phrases using createDecodingGraphFromPhrases or createContextualDecodingGraphFromPhrases. Graphs are persisted on the device filesystem and can be referenced by name and reused across sessions.
The recognizer progresses through these states (see RecognizerState):
Listening stops in one of three ways:
Results are delivered through two event listeners:
Each ASRResponse holds a reference to native resources. The caller owns the response and must call release() when done. Failing to release will leak native memory.
const unsubscribe = onFinalResponse((response) => {
// Use the response
const text = response.asrResult.cleanText;
// Save audio/JSON if needed
await response.saveAudioFile('/path/to/dir');
await response.saveJsonFile('/path/to/dir');
// Or queue for Dashboard upload
await response.queueForUpload();
// Always release when done
response.release();
});
VAD parameters control when the recognizer automatically stops listening. See VadParameter for all options.
import { VadParameter } from 'keenasr-react-native';
await KeenASR.setVADParameter(VadParameter.TimeoutForNoSpeech, 5.0);
await KeenASR.setVADParameter(VadParameter.TimeoutEndSilenceForGoodMatch, 0.5);
VAD parameters can be set at any time, including while the recognizer is listening. For more details, see Start and Stop Listening.
When computeGoP is set to true in prepareForListening, the final result includes phoneme-level pronunciation scores (0–1) in each word's phones array. This is useful for pronunciation assessment applications. The ASR bundle must support GoP scoring.
await KeenASR.prepareForListening('greetings', true); // computeGoP = true
onFinalResponse((response) => {
for (const word of response.asrResult.words) {
if (word.phones) {
for (const phone of word.phones) {
console.log(`${phone.text}: ${phone.pronunciationScore}`);
}
}
}
response.release();
});
To upload recognition responses to the KeenASR Dashboard for analysis, start the uploader with startDataUploader and queue responses via queueForUpload():
// Start the uploader once during app setup
await KeenASR.startDataUploader('YOUR_APP_KEY');
// Queue individual responses for upload
onFinalResponse((response) => {
response.queueForUpload();
response.release();
});
| Feature | iOS | Android |
|---|---|---|
| Initialization | Synchronous | Asynchronous |
| Status | Implemented | Pending |
For platform-specific details, see the native SDK introduction docs:
To fully release the SDK and all resources:
await KeenASR.teardown();
After teardown, the SDK can be re-initialized by calling initialize again.