expo-speech-recognition 0.2.20 → 0.2.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +113 -43
  2. package/android/src/main/java/expo/modules/speechrecognition/ExpoSpeechRecognitionModule.kt +4 -0
  3. package/android/src/main/java/expo/modules/speechrecognition/ExpoSpeechService.kt +11 -2
  4. package/build/ExpoSpeechRecognitionModule.d.ts +2 -40
  5. package/build/ExpoSpeechRecognitionModule.d.ts.map +1 -1
  6. package/build/ExpoSpeechRecognitionModule.js +1 -0
  7. package/build/ExpoSpeechRecognitionModule.js.map +1 -1
  8. package/build/ExpoSpeechRecognitionModule.types.d.ts +65 -43
  9. package/build/ExpoSpeechRecognitionModule.types.d.ts.map +1 -1
  10. package/build/ExpoSpeechRecognitionModule.types.js.map +1 -1
  11. package/build/ExpoSpeechRecognitionModule.web.d.ts.map +1 -1
  12. package/build/ExpoSpeechRecognitionModule.web.js +5 -4
  13. package/build/ExpoSpeechRecognitionModule.web.js.map +1 -1
  14. package/build/ExpoWebSpeechRecognition.d.ts +1 -1
  15. package/build/ExpoWebSpeechRecognition.d.ts.map +1 -1
  16. package/build/ExpoWebSpeechRecognition.js +2 -2
  17. package/build/ExpoWebSpeechRecognition.js.map +1 -1
  18. package/build/index.d.ts +14 -1
  19. package/build/index.d.ts.map +1 -1
  20. package/build/index.js +4 -0
  21. package/build/index.js.map +1 -1
  22. package/build/useSpeechRecognitionEvent.d.ts +1 -1
  23. package/build/useSpeechRecognitionEvent.d.ts.map +1 -1
  24. package/build/useSpeechRecognitionEvent.js +1 -1
  25. package/build/useSpeechRecognitionEvent.js.map +1 -1
  26. package/ios/ExpoSpeechRecognitionModule.swift +6 -2
  27. package/ios/ExpoSpeechRecognizer.swift +0 -1
  28. package/package.json +1 -1
  29. package/src/ExpoSpeechRecognitionModule.ts +3 -1
  30. package/src/ExpoSpeechRecognitionModule.types.ts +76 -45
  31. package/src/ExpoSpeechRecognitionModule.web.ts +8 -12
  32. package/src/ExpoWebSpeechRecognition.ts +6 -9
  33. package/src/index.ts +16 -0
  34. package/src/useSpeechRecognitionEvent.ts +2 -2
@@ -279,7 +279,11 @@ public class ExpoSpeechRecognitionModule: Module {
279
279
  }
280
280
 
281
281
  Function("supportsRecording") { () -> Bool in
282
- let recognizer: SFSpeechRecognizer? = SFSpeechRecognizer()
282
+ return true
283
+ }
284
+
285
+ Function("isRecognitionAvailable") { () -> Bool in
286
+ let recognizer = SFSpeechRecognizer()
283
287
  return recognizer?.isAvailable ?? false
284
288
  }
285
289
 
@@ -419,7 +423,7 @@ public class ExpoSpeechRecognitionModule: Module {
419
423
 
420
424
  if isFinal && results.isEmpty {
421
425
  // https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition/nomatch_event
422
- // The nomatch event of the Web Speech API is fired22A3351
426
+ // The nomatch event of the Web Speech API is fired
423
427
  // when the speech recognition service returns a final result with no significant recognition.
424
428
  sendEvent("nomatch")
425
429
  return
@@ -23,7 +23,6 @@ enum RecognizerError: Error {
23
23
  }
24
24
  }
25
25
 
26
- /// A helper for transcribing speech to text using SFSpeechRecognizer and AVAudioEngine.
27
26
  actor ExpoSpeechRecognizer: ObservableObject {
28
27
 
29
28
  private var options: SpeechRecognitionOptions?
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-speech-recognition",
3
- "version": "0.2.20",
3
+ "version": "0.2.21",
4
4
  "description": "Speech Recognition for React Native Expo projects",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -11,7 +11,7 @@ import type { ExpoSpeechRecognitionModuleType } from "./ExpoSpeechRecognitionMod
11
11
  const ExpoSpeechRecognitionNativeModule =
12
12
  requireNativeModule<ExpoSpeechRecognitionModuleType>("ExpoSpeechRecognition");
13
13
 
14
- export const ExpoSpeechRecognitionModule = {
14
+ export const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType = {
15
15
  ...ExpoSpeechRecognitionNativeModule,
16
16
  // Avoid any function bindings when calling the native module
17
17
  stop: () => ExpoSpeechRecognitionNativeModule.stop(),
@@ -31,6 +31,8 @@ export const ExpoSpeechRecognitionModule = {
31
31
  ExpoSpeechRecognitionNativeModule.supportsOnDeviceRecognition(),
32
32
  supportsRecording: () =>
33
33
  ExpoSpeechRecognitionNativeModule.supportsRecording(),
34
+ isRecognitionAvailable: () =>
35
+ ExpoSpeechRecognitionNativeModule.isRecognitionAvailable(),
34
36
  };
35
37
 
36
38
  export const ExpoSpeechRecognitionModuleEmitter = new EventEmitter(
@@ -14,6 +14,48 @@ import type {
14
14
  export type AVAudioSessionCategoryValue =
15
15
  (typeof AVAudioSessionCategory)[keyof typeof AVAudioSessionCategory];
16
16
 
17
+ export type ExpoSpeechRecognitionResult = {
18
+ transcript: string;
19
+ /**
20
+ * Value ranging between between 0.0, 1.0, and -1 (unavailable) indicating transcript confidence.
21
+ */
22
+ confidence: number;
23
+ /**
24
+ * An array of transcription segments that represent the parts of the transcription, as identified by the speech recognizer.
25
+ *
26
+ * Notes for Android:
27
+ *
28
+ * - This is only available for SDK 34+ (Android 14+)
29
+ * - This is only verified to work with the `com.google.android.as` service (using on device speech recognition)
30
+ * - Segments are only available during the final result
31
+ * - The segment parts are split up by words.
32
+ * - The segments are only available for the first transcript
33
+ * - Segment confidences currently return as -1 (unavailable)
34
+ *
35
+ * Notes for iOS:
36
+ *
37
+ * - The confidence value will be 0 on partial results
38
+ */
39
+ segments: ExpoSpeechRecognitionResultSegment[];
40
+ };
41
+
42
+ export type ExpoSpeechRecognitionResultSegment = {
43
+ /** The start timestamp of the utterance, e.g. 1000 */
44
+ startTimeMillis: number;
45
+ /** The end timestamp of the utterance, e.g. 1500 */
46
+ endTimeMillis: number;
47
+ /** The text portion of the transcript, e.g. "Hello world" */
48
+ segment: string;
49
+ /** Value ranging between between 0.0, 1.0, and -1 (unavailable) indicating the confidence of the specific segment */
50
+ confidence: number;
51
+ };
52
+
53
+ /** Fired when there's a speech result. The result may be partial or final */
54
+ export type ExpoSpeechRecognitionResultEvent = {
55
+ isFinal: boolean;
56
+ results: ExpoSpeechRecognitionResult[];
57
+ };
58
+
17
59
  export type ExpoSpeechRecognitionErrorCode =
18
60
  /** The user called `ExpoSpeechRecognitionModule.abort()`. */
19
61
  | "aborted"
@@ -35,53 +77,23 @@ export type ExpoSpeechRecognitionErrorCode =
35
77
  /** The recognizer is busy and cannot accept any new recognition requests. */
36
78
  | "busy"
37
79
  /** (Android only) An unknown client-side error occurred. */
38
- | "client";
80
+ | "client"
81
+ /** (Android) No speech input. */
82
+ | "speech-timeout"
83
+ /** (Android) Unknown error */
84
+ | "unknown";
85
+
86
+ export type ExpoSpeechRecognitionErrorEvent = {
87
+ error: ExpoSpeechRecognitionErrorCode;
88
+ message: string;
89
+ };
39
90
 
40
91
  /**
41
92
  * Events that are dispatched from the native side
42
93
  */
43
94
  export type ExpoSpeechRecognitionNativeEventMap = {
44
- /** Fired when there's a speech result. The result may be partial or final */
45
- result: {
46
- isFinal: boolean;
47
- results: {
48
- transcript: string;
49
- /**
50
- * Value ranging between between 0.0, 1.0, and -1 (unavailable) indicating transcript confidence.
51
- */
52
- confidence: number;
53
- /**
54
- * An array of transcription segments that represent the parts of the transcription, as identified by the speech recognizer.
55
- *
56
- * Notes for Android:
57
- *
58
- * - This is only available for SDK 34+ (Android 14+)
59
- * - This is only verified to work with the `com.google.android.as` service (using on device speech recognition)
60
- * - Segments are only available during the final result
61
- * - The segment parts are split up by words.
62
- * - The segments are only available for the first transcript
63
- * - Segment confidences currently return as -1 (unavailable)
64
- *
65
- * Notes for iOS:
66
- *
67
- * - The confidence value will be 0 on partial results
68
- */
69
- segments: {
70
- /** The start timestamp of the utterance, e.g. 1000 */
71
- startTimeMillis: number;
72
- /** The end timestamp of the utterance, e.g. 1500 */
73
- endTimeMillis: number;
74
- /** The text portion of the transcript, e.g. "Hello world" */
75
- segment: string;
76
- /** Value ranging between between 0.0, 1.0, and -1 (unavailable) indicating the confidence of the specific segment */
77
- confidence: number;
78
- }[];
79
- }[];
80
- };
81
- error: {
82
- error: ExpoSpeechRecognitionErrorCode;
83
- message: string;
84
- };
95
+ result: ExpoSpeechRecognitionResultEvent;
96
+ error: ExpoSpeechRecognitionErrorEvent;
85
97
  start: null;
86
98
  speechstart: null;
87
99
  speechend: null;
@@ -132,7 +144,13 @@ export type ExpoSpeechRecognitionOptions = {
132
144
  * On iOS, this configures [`SFSpeechRecognitionRequest.contextualStrings`](https://developer.apple.com/documentation/speech/sfspeechrecognitionrequest/1649391-contextualstrings).
133
145
  */
134
146
  contextualStrings?: string[];
135
- /** [Default: false] Continuous recognition. Note: if false on iOS, recognition will run until no speech is detected for 3 seconds */
147
+ /**
148
+ * [Default: false] Continuous recognition.
149
+ *
150
+ * Not supported on Android 12 and below.
151
+ *
152
+ * If false on iOS, recognition will run until no speech is detected for 3 seconds.
153
+ */
136
154
  continuous?: boolean;
137
155
  /** [Default: false] Prevent device from sending audio over the network. Only enabled if the device supports it.
138
156
  *
@@ -527,7 +545,10 @@ export interface ExpoSpeechRecognitionModuleType extends NativeModule {
527
545
  *
528
546
  * @returns empty string if no service is found, or not Android
529
547
  */
530
- getDefaultRecognitionService(): { packageName: string };
548
+ getDefaultRecognitionService(): {
549
+ /** e.g. "com.google.android.tts" or "com.google.android.googlequicksearchbox" */
550
+ packageName: string;
551
+ };
531
552
  /**
532
553
  * [Android only] Returns the default voice recognition service on the device.
533
554
  *
@@ -535,7 +556,10 @@ export interface ExpoSpeechRecognitionModuleType extends NativeModule {
535
556
  *
536
557
  * @returns empty string if no service is found, or not Android
537
558
  */
538
- getAssistantService(): { packageName: string };
559
+ getAssistantService(): {
560
+ /** e.g. "com.google.android.googlequicksearchbox" or "com.samsung.android.bixby.agent" */
561
+ packageName: string;
562
+ };
539
563
  /**
540
564
  * Whether the on-device speech recognition is available on the device.
541
565
  */
@@ -546,6 +570,13 @@ export interface ExpoSpeechRecognitionModuleType extends NativeModule {
546
570
  * This mostly applies to Android devices, to check if it's greater than Android 13.
547
571
  */
548
572
  supportsRecording(): boolean;
573
+ /**
574
+ * Whether on-device speech recognition is available.
575
+ *
576
+ * If this method returns false, `start()` will fail and emit an error event with the code `service-not-allowed` or `language-not-supported`.
577
+ */
578
+ isRecognitionAvailable(): boolean;
579
+
549
580
  /**
550
581
  * Downloads the offline model for the specified locale.
551
582
  * Note: this is only supported on Android 13 and above.
@@ -82,31 +82,19 @@ export const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType = {
82
82
  );
83
83
  },
84
84
  getSpeechRecognitionServices: () => {
85
- console.warn(
86
- "getSpeechRecognitionServices is not supported on web. Returning an empty array.",
87
- );
88
85
  return [] as string[];
89
86
  },
90
87
  getDefaultRecognitionService: () => {
91
- console.warn(
92
- "getDefaultRecognitionService is not supported on web. Returning an empty object.",
93
- );
94
88
  return {
95
89
  packageName: "",
96
90
  };
97
91
  },
98
92
  getAssistantService: () => {
99
- console.warn(
100
- "getAssistantService is not supported on web. Returning an empty object.",
101
- );
102
93
  return {
103
94
  packageName: "",
104
95
  };
105
96
  },
106
97
  supportsOnDeviceRecognition: () => {
107
- console.warn(
108
- "supportsOnDeviceRecognition is not supported on web. Returning false.",
109
- );
110
98
  return false;
111
99
  },
112
100
  supportsRecording: () => {
@@ -137,6 +125,13 @@ export const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType = {
137
125
  setAudioSessionActiveIOS: () => {
138
126
  console.warn("setAudioSessionActiveIOS is not supported on web.");
139
127
  },
128
+ isRecognitionAvailable: () => {
129
+ const hasSpeechRecognitionAPI =
130
+ typeof webkitSpeechRecognition !== "undefined" ||
131
+ typeof SpeechRecognition !== "undefined";
132
+
133
+ return hasSpeechRecognitionAPI;
134
+ },
140
135
  };
141
136
 
142
137
  /**
@@ -176,6 +171,7 @@ const webToNativeEventMap: {
176
171
  start: (ev) => null,
177
172
  soundend: (ev) => null,
178
173
  };
174
+
179
175
  export const ExpoSpeechRecognitionModuleEmitter = {
180
176
  _nativeListeners: new Map() as Map<string, Set<(event: any) => void>>,
181
177
  _clientListeners: new Map() as Map<
@@ -210,7 +210,7 @@ export class ExpoWebSpeechRecognition implements SpeechRecognition {
210
210
  androidRecognitionServicePackage: ExpoSpeechRecognitionOptions["androidRecognitionServicePackage"];
211
211
 
212
212
  // keyed by listener function
213
- #subscriptionMap: Map<Function, Subscription[]> = new Map();
213
+ #subscriptionMap: Map<SpeechListener<any>, Subscription[]> = new Map();
214
214
 
215
215
  start() {
216
216
  ExpoSpeechRecognitionModule.requestPermissionsAsync().then(() => {
@@ -354,10 +354,7 @@ export class ExpoWebSpeechRecognition implements SpeechRecognition {
354
354
 
355
355
  addEventListener<K extends keyof SpeechRecognitionEventMap>(
356
356
  type: K,
357
- listener: (
358
- this: SpeechRecognition,
359
- ev: SpeechRecognitionEventMap[K],
360
- ) => any,
357
+ listener: SpeechListener<K>,
361
358
  options?: boolean | AddEventListenerOptions,
362
359
  ): void {
363
360
  const once = typeof options === "object" && options.once;
@@ -368,9 +365,9 @@ export class ExpoWebSpeechRecognition implements SpeechRecognition {
368
365
  ? (((ev) => {
369
366
  listener.call(this, ev);
370
367
  // remove the listeners from the map
371
- this.#subscriptionMap.get(listener)?.forEach((sub) => {
368
+ for (const sub of this.#subscriptionMap.get(listener) ?? []) {
372
369
  sub.remove();
373
- });
370
+ }
374
371
  this.#subscriptionMap.delete(listener);
375
372
  }) as SpeechListener<K>)
376
373
  : listener;
@@ -443,8 +440,8 @@ export class ExpoWebSpeechGrammarList implements SpeechGrammarList {
443
440
  }
444
441
 
445
442
  export class ExpoWebSpeechGrammar implements SpeechGrammar {
446
- src: string = "";
447
- weight: number = 1;
443
+ src = "";
444
+ weight = 1;
448
445
 
449
446
  constructor(src: string, weight?: number) {
450
447
  this.src = src;
package/src/index.ts CHANGED
@@ -51,6 +51,18 @@ export const getAudioSessionCategoryAndOptionsIOS =
51
51
  export const setAudioSessionActiveIOS =
52
52
  ExpoSpeechRecognitionModule.setAudioSessionActiveIOS;
53
53
 
54
+ export const androidTriggerOfflineModelDownload =
55
+ ExpoSpeechRecognitionModule.androidTriggerOfflineModelDownload;
56
+
57
+ export const isRecognitionAvailable =
58
+ ExpoSpeechRecognitionModule.isRecognitionAvailable;
59
+
60
+ export const getDefaultRecognitionService =
61
+ ExpoSpeechRecognitionModule.getDefaultRecognitionService;
62
+
63
+ export const getAssistantService =
64
+ ExpoSpeechRecognitionModule.getAssistantService;
65
+
54
66
  export const addSpeechRecognitionListener = <
55
67
  T extends keyof ExpoSpeechRecognitionNativeEventMap,
56
68
  >(
@@ -71,4 +83,8 @@ export {
71
83
  type IOSTaskHintValue,
72
84
  type SetCategoryOptions,
73
85
  type ExpoSpeechRecognitionErrorCode,
86
+ type ExpoSpeechRecognitionErrorEvent,
87
+ type ExpoSpeechRecognitionResultEvent,
88
+ type ExpoSpeechRecognitionResult,
89
+ type ExpoSpeechRecognitionResultSegment,
74
90
  } from "./ExpoSpeechRecognitionModule.types";
@@ -1,6 +1,6 @@
1
1
  import { useEffect, useRef } from "react";
2
2
  import { ExpoSpeechRecognitionModuleEmitter } from "./ExpoSpeechRecognitionModule";
3
- import { ExpoSpeechRecognitionNativeEventMap } from "./ExpoSpeechRecognitionModule.types";
3
+ import type { ExpoSpeechRecognitionNativeEventMap } from "./ExpoSpeechRecognitionModule.types";
4
4
 
5
5
  /**
6
6
  * This hook allows you to listen to native events emitted by the `ExpoSpeechRecognitionModule`.
@@ -26,5 +26,5 @@ export function useSpeechRecognitionEvent<
26
26
  handler,
27
27
  );
28
28
  return subscription.remove;
29
- }, [eventName, listenerRef]);
29
+ }, [eventName]);
30
30
  }