@gmessier/nitro-speech 0.4.1 → 0.4.2

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.
package/README.md CHANGED
@@ -155,7 +155,8 @@ Because of that, treat it as a **single session owner** setup hook: use it once
155
155
  import { useRecognizer } from '@gmessier/nitro-speech';
156
156
 
157
157
  function MyComponent() {
158
- const {
158
+ const {
159
+ prewarm,
159
160
  startListening,
160
161
  stopListening,
161
162
  resetAutoFinishTime,
@@ -261,6 +262,7 @@ If you need to call recognizer methods from other components without prop drilli
261
262
  ```typescript
262
263
  import { RecognizerRef } from '@gmessier/nitro-speech';
263
264
 
265
+ RecognizerRef.prewarm({ locale: 'en-US' });
264
266
  RecognizerRef.startListening({ locale: 'en-US' });
265
267
  RecognizerRef.addAutoFinishTime(5000);
266
268
  RecognizerRef.resetAutoFinishTime();
@@ -357,12 +359,20 @@ function MyComponent() {
357
359
 
358
360
  `SpeechRecognizer` is the hybrid object. It gives direct access to callbacks and control methods, but it is unsafe to orchestrate the full session directly from it.
359
361
 
362
+ **Warning**: Since it reflects the original hybrid object, its API may change in the future.
363
+
360
364
  ```typescript
361
- import { SpeechRecognizer, speechRecognizerVolumeChangeHandler } from '@gmessier/nitro-speech';
365
+ import {
366
+ SpeechRecognizer,
367
+ speechRecognizerVolumeChangeHandler,
368
+ speechRecognizerActiveStateHandler,
369
+ } from '@gmessier/nitro-speech';
362
370
 
363
371
  // Set up callbacks
364
372
  SpeechRecognizer.onReadyForSpeech = () => {
365
373
  console.log('Listening...');
374
+ // Add speechRecognizerActiveStateHandler to enable useRecognizerIsActive hook manually
375
+ speechRecognizerActiveStateHandler(true);
366
376
  };
367
377
 
368
378
  SpeechRecognizer.onResult = (textBatches) => {
@@ -371,6 +381,8 @@ SpeechRecognizer.onResult = (textBatches) => {
371
381
 
372
382
  SpeechRecognizer.onRecordingStopped = () => {
373
383
  console.log('Stopped');
384
+ // Add speechRecognizerActiveStateHandler to enable useRecognizerIsActive hook manually
385
+ speechRecognizerActiveStateHandler(false);
374
386
  };
375
387
 
376
388
  SpeechRecognizer.onAutoFinishProgress = (timeLeftMs) => {
@@ -387,10 +399,27 @@ SpeechRecognizer.onPermissionDenied = () => {
387
399
 
388
400
  SpeechRecognizer.onVolumeChange = (volume) => {
389
401
  console.log('new volume: ', volume);
402
+ // Add speechRecognizerVolumeChangeHandler to enable useVoiceInputVolume hook manually
403
+ speechRecognizerVolumeChangeHandler(volume);
390
404
  };
391
- // OR use speechRecognizerVolumeChangeHandler to enable useVoiceInputVolume hook manually
392
- SpeechRecognizer.onVolumeChange = speechRecognizerVolumeChangeHandler
393
405
 
406
+ // Prepare resources, download assets, confirms locale availability
407
+ SpeechRecognizer.prewarm({
408
+ locale: 'en-US',
409
+ // ... your config to prepare
410
+ });
411
+ // OR `await` if you want to react to the success
412
+ await SpeechRecognizer.prewarm({
413
+ locale: 'en-US',
414
+ // ... your config to prepare
415
+ });
416
+ // OR from worklet (only sync)
417
+ scheduleOnRuntime(workletRuntime, () => {
418
+ SpeechRecognizer.prewarm({
419
+ locale: 'en-US',
420
+ // ... your config to prepare
421
+ });
422
+ });
394
423
 
395
424
  // Start listening
396
425
  SpeechRecognizer.startListening({
@@ -1,10 +1,11 @@
1
- import { recognizerAddAutoFinishTime, recognizerGetSupportedLocalesIOS, recognizerGetIsActive, recognizerResetAutoFinishTime, recognizerStartListening, recognizerStopListening, recognizerUpdateConfig, recognizerGetVoiceInputVolume, } from './methods';
1
+ import { recognizerAddAutoFinishTime, recognizerGetSupportedLocalesIOS, recognizerGetIsActive, recognizerResetAutoFinishTime, recognizerStartListening, recognizerStopListening, recognizerUpdateConfig, recognizerGetVoiceInputVolume, recognizerPrewarm, } from './methods';
2
2
  /**
3
3
  * Safe cross-component reference to the Speech Recognizer methods.
4
4
  *
5
5
  * All methods support worklets and UI thread calls
6
6
  */
7
7
  export const RecognizerRef = {
8
+ prewarm: recognizerPrewarm,
8
9
  startListening: recognizerStartListening,
9
10
  stopListening: recognizerStopListening,
10
11
  resetAutoFinishTime: recognizerResetAutoFinishTime,
@@ -3,6 +3,7 @@
3
3
  *
4
4
  * Direct access to the all Speech Recognizer methods and callbacks.
5
5
  *
6
- * @note unsafe, might lead to race conditions
6
+ * @note Unsafe, might lead to race conditions
7
+ * @warning Since it reflects the original hybrid object, its API may change in the future.
7
8
  */
8
9
  export declare const SpeechRecognizer: import("./types").RecognizerSpec;
@@ -4,6 +4,7 @@ import { NitroSpeech } from '../NitroSpeech';
4
4
  *
5
5
  * Direct access to the all Speech Recognizer methods and callbacks.
6
6
  *
7
- * @note unsafe, might lead to race conditions
7
+ * @note Unsafe, might lead to race conditions
8
+ * @warning Since it reflects the original hybrid object, its API may change in the future.
8
9
  */
9
10
  export const SpeechRecognizer = NitroSpeech.recognizer;
@@ -1,9 +1,10 @@
1
- import type { SpeechRecognitionConfig } from './types';
2
- export declare const recognizerStartListening: (params: SpeechRecognitionConfig) => void;
1
+ import type { MutableSpeechRecognitionConfig, SpeechRecognitionConfig } from './types';
2
+ export declare const recognizerPrewarm: (params?: SpeechRecognitionConfig) => Promise<void>;
3
+ export declare const recognizerStartListening: (params?: SpeechRecognitionConfig) => void;
3
4
  export declare const recognizerStopListening: () => void;
4
5
  export declare const recognizerResetAutoFinishTime: () => void;
5
6
  export declare const recognizerAddAutoFinishTime: (additionalTimeMs?: number) => void;
6
- export declare const recognizerUpdateConfig: (newConfig: SpeechRecognitionConfig, resetAutoFinishTime?: boolean) => void;
7
+ export declare const recognizerUpdateConfig: (newConfig?: MutableSpeechRecognitionConfig, resetAutoFinishTime?: boolean) => void;
7
8
  export declare const recognizerGetIsActive: () => boolean;
8
9
  export declare const recognizerGetVoiceInputVolume: () => import("./types").VolumeChangeEvent;
9
10
  export declare const recognizerGetSupportedLocalesIOS: () => string[];
@@ -1,4 +1,8 @@
1
1
  import { SpeechRecognizer } from './SpeechRecognizer';
2
+ export const recognizerPrewarm = (params) => {
3
+ 'worklet';
4
+ return SpeechRecognizer.prewarm(params);
5
+ };
2
6
  export const recognizerStartListening = (params) => {
3
7
  'worklet';
4
8
  SpeechRecognizer.startListening(params);
@@ -1,6 +1,6 @@
1
1
  import type { Recognizer as RecognizerSpec } from '../specs/Recognizer.nitro';
2
- import type { SpeechRecognitionConfig } from '../specs/SpeechRecognitionConfig';
2
+ import type { MutableSpeechRecognitionConfig, SpeechRecognitionConfig } from '../specs/SpeechRecognitionConfig';
3
3
  import type { VolumeChangeEvent } from '../specs/VolumeChangeEvent';
4
4
  type RecognizerCallbacks = Pick<RecognizerSpec, 'onReadyForSpeech' | 'onRecordingStopped' | 'onResult' | 'onAutoFinishProgress' | 'onError' | 'onPermissionDenied' | 'onVolumeChange'>;
5
- type RecognizerMethods = Pick<RecognizerSpec, 'startListening' | 'stopListening' | 'resetAutoFinishTime' | 'addAutoFinishTime' | 'updateConfig' | 'getIsActive' | 'getVoiceInputVolume' | 'getSupportedLocalesIOS'>;
6
- export type { RecognizerSpec, SpeechRecognitionConfig, VolumeChangeEvent, RecognizerCallbacks, RecognizerMethods, };
5
+ type RecognizerMethods = Pick<RecognizerSpec, 'prewarm' | 'startListening' | 'stopListening' | 'resetAutoFinishTime' | 'addAutoFinishTime' | 'updateConfig' | 'getIsActive' | 'getVoiceInputVolume' | 'getSupportedLocalesIOS'>;
6
+ export type { RecognizerSpec, SpeechRecognitionConfig, MutableSpeechRecognitionConfig, VolumeChangeEvent, RecognizerCallbacks, RecognizerMethods, };
@@ -1,5 +1,5 @@
1
1
  import { useEffect } from 'react';
2
- import { recognizerResetAutoFinishTime, recognizerAddAutoFinishTime, recognizerUpdateConfig, recognizerGetIsActive, recognizerGetSupportedLocalesIOS, recognizerStartListening, recognizerStopListening, recognizerGetVoiceInputVolume, } from './methods';
2
+ import { recognizerResetAutoFinishTime, recognizerAddAutoFinishTime, recognizerUpdateConfig, recognizerGetIsActive, recognizerGetSupportedLocalesIOS, recognizerStartListening, recognizerStopListening, recognizerGetVoiceInputVolume, recognizerPrewarm, } from './methods';
3
3
  import { SpeechRecognizer } from './SpeechRecognizer';
4
4
  import { speechRecognizerVolumeChangeHandler } from './useVoiceInputVolume';
5
5
  import { speechRecognizerActiveStateHandler } from './useRecognizerIsActive';
@@ -59,6 +59,7 @@ export const useRecognizer = (callbacks, destroyDeps = []) => {
59
59
  // eslint-disable-next-line react-hooks/exhaustive-deps
60
60
  }, [...destroyDeps]);
61
61
  return {
62
+ prewarm: recognizerPrewarm,
62
63
  startListening: recognizerStartListening,
63
64
  stopListening: recognizerStopListening,
64
65
  resetAutoFinishTime: recognizerResetAutoFinishTime,
package/lib/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- export * from './Recognizer/types';
2
1
  export { useRecognizer } from './Recognizer/useRecognizer';
3
2
  export { useVoiceInputVolume, speechRecognizerVolumeChangeHandler, } from './Recognizer/useVoiceInputVolume';
4
3
  export { useRecognizerIsActive, speechRecognizerActiveStateHandler, } from './Recognizer/useRecognizerIsActive';
5
4
  export { SpeechRecognizer } from './Recognizer/SpeechRecognizer';
6
5
  export { RecognizerRef } from './Recognizer/RecognizerRef';
7
6
  export { NitroSpeech } from './NitroSpeech';
7
+ export { type RecognizerSpec, type SpeechRecognitionConfig, type MutableSpeechRecognitionConfig, type VolumeChangeEvent, type RecognizerCallbacks, type RecognizerMethods, } from './Recognizer/types';
package/lib/index.js CHANGED
@@ -1,7 +1,7 @@
1
- export * from './Recognizer/types';
2
1
  export { useRecognizer } from './Recognizer/useRecognizer';
3
2
  export { useVoiceInputVolume, speechRecognizerVolumeChangeHandler, } from './Recognizer/useVoiceInputVolume';
4
3
  export { useRecognizerIsActive, speechRecognizerActiveStateHandler, } from './Recognizer/useRecognizerIsActive';
5
4
  export { SpeechRecognizer } from './Recognizer/SpeechRecognizer';
6
5
  export { RecognizerRef } from './Recognizer/RecognizerRef';
7
6
  export { NitroSpeech } from './NitroSpeech';
7
+ export {} from './Recognizer/types';
@@ -8,9 +8,24 @@ export interface Recognizer extends HybridObject<{
8
8
  /**
9
9
  * Prepare the speech recognition engine and the model for the given parameters.
10
10
  *
11
- * Omit the {@linkcode Promise} result if want to run synchronously.
11
+ * @notes
12
+ * - Not required, {@linkcode startListening} will prewarm automatically.
13
+ *
14
+ * - Omit the {@linkcode Promise} result if you want to run synchronously.
12
15
  * {@linkcode startListening} will start and resolve it automatically.
13
- * Only `await` if run beforehand and want to react on the success
16
+ *
17
+ * - Only `await` if run beforehand and you want to react to the success
18
+ *
19
+ * @worklet **Do not** use `await` on Worklet or UI runtimes. It will not work properly.
20
+ *
21
+ * You can run it synchronously:
22
+ * ```typescript
23
+ * scheduleOnRuntime(workletRuntime, () => {
24
+ * RecognizerRef.prewarm({
25
+ * // your config to prepare
26
+ * });
27
+ * });
28
+ * ```
14
29
  */
15
30
  prewarm(defaultParams?: SpeechRecognitionConfig): Promise<void>;
16
31
  /**
@@ -24,9 +39,9 @@ export interface Recognizer extends HybridObject<{
24
39
  */
25
40
  startListening(params?: SpeechRecognitionConfig): void;
26
41
  /**
27
- * Stops the speech recognition. if not started, does nothing.
42
+ * Stops the speech recognition. If not started, does nothing.
28
43
  *
29
- * Not a sync operation for android, delay about 250ms to polish the result.
44
+ * Not a sync operation for Android, delay about 250ms to polish the result.
30
45
  *
31
46
  * Use {@linkcode onRecordingStopped} to handle the stop event.
32
47
  */
@@ -78,8 +93,6 @@ export interface Recognizer extends HybridObject<{
78
93
  * Fires every {@linkcode SpeechRecognitionConfig.autoFinishProgressIntervalMs} or 1000ms
79
94
  *
80
95
  * Time left in milliseconds until the timer stops.
81
- *
82
- * @note not implemented for Android yet.
83
96
  */
84
97
  onAutoFinishProgress?: (timeLeftMs: number) => void;
85
98
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gmessier/nitro-speech",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "React Native Real-time Speech Recognition Library powered by Nitro Modules",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",
@@ -8,6 +8,7 @@ import {
8
8
  recognizerStopListening,
9
9
  recognizerUpdateConfig,
10
10
  recognizerGetVoiceInputVolume,
11
+ recognizerPrewarm,
11
12
  } from './methods'
12
13
 
13
14
  /**
@@ -16,6 +17,7 @@ import {
16
17
  * All methods support worklets and UI thread calls
17
18
  */
18
19
  export const RecognizerRef: RecognizerMethods = {
20
+ prewarm: recognizerPrewarm,
19
21
  startListening: recognizerStartListening,
20
22
  stopListening: recognizerStopListening,
21
23
  resetAutoFinishTime: recognizerResetAutoFinishTime,
@@ -5,6 +5,7 @@ import { NitroSpeech } from '../NitroSpeech'
5
5
  *
6
6
  * Direct access to the all Speech Recognizer methods and callbacks.
7
7
  *
8
- * @note unsafe, might lead to race conditions
8
+ * @note Unsafe, might lead to race conditions
9
+ * @warning Since it reflects the original hybrid object, its API may change in the future.
9
10
  */
10
11
  export const SpeechRecognizer = NitroSpeech.recognizer
@@ -1,7 +1,15 @@
1
1
  import { SpeechRecognizer } from './SpeechRecognizer'
2
- import type { SpeechRecognitionConfig } from './types'
2
+ import type {
3
+ MutableSpeechRecognitionConfig,
4
+ SpeechRecognitionConfig,
5
+ } from './types'
3
6
 
4
- export const recognizerStartListening = (params: SpeechRecognitionConfig) => {
7
+ export const recognizerPrewarm = (params?: SpeechRecognitionConfig) => {
8
+ 'worklet'
9
+ return SpeechRecognizer.prewarm(params)
10
+ }
11
+
12
+ export const recognizerStartListening = (params?: SpeechRecognitionConfig) => {
5
13
  'worklet'
6
14
  SpeechRecognizer.startListening(params)
7
15
  }
@@ -22,7 +30,7 @@ export const recognizerAddAutoFinishTime = (additionalTimeMs?: number) => {
22
30
  }
23
31
 
24
32
  export const recognizerUpdateConfig = (
25
- newConfig: SpeechRecognitionConfig,
33
+ newConfig?: MutableSpeechRecognitionConfig,
26
34
  resetAutoFinishTime?: boolean
27
35
  ) => {
28
36
  'worklet'
@@ -1,5 +1,8 @@
1
1
  import type { Recognizer as RecognizerSpec } from '../specs/Recognizer.nitro'
2
- import type { SpeechRecognitionConfig } from '../specs/SpeechRecognitionConfig'
2
+ import type {
3
+ MutableSpeechRecognitionConfig,
4
+ SpeechRecognitionConfig,
5
+ } from '../specs/SpeechRecognitionConfig'
3
6
  import type { VolumeChangeEvent } from '../specs/VolumeChangeEvent'
4
7
 
5
8
  type RecognizerCallbacks = Pick<
@@ -15,6 +18,7 @@ type RecognizerCallbacks = Pick<
15
18
 
16
19
  type RecognizerMethods = Pick<
17
20
  RecognizerSpec,
21
+ | 'prewarm'
18
22
  | 'startListening'
19
23
  | 'stopListening'
20
24
  | 'resetAutoFinishTime'
@@ -28,6 +32,7 @@ type RecognizerMethods = Pick<
28
32
  export type {
29
33
  RecognizerSpec,
30
34
  SpeechRecognitionConfig,
35
+ MutableSpeechRecognitionConfig,
31
36
  VolumeChangeEvent,
32
37
  RecognizerCallbacks,
33
38
  RecognizerMethods,
@@ -8,6 +8,7 @@ import {
8
8
  recognizerStartListening,
9
9
  recognizerStopListening,
10
10
  recognizerGetVoiceInputVolume,
11
+ recognizerPrewarm,
11
12
  } from './methods'
12
13
  import type { RecognizerCallbacks, RecognizerMethods } from './types'
13
14
  import { SpeechRecognizer } from './SpeechRecognizer'
@@ -75,6 +76,7 @@ export const useRecognizer = (
75
76
  }, [...destroyDeps])
76
77
 
77
78
  return {
79
+ prewarm: recognizerPrewarm,
78
80
  startListening: recognizerStartListening,
79
81
  stopListening: recognizerStopListening,
80
82
  resetAutoFinishTime: recognizerResetAutoFinishTime,
package/src/index.ts CHANGED
@@ -1,4 +1,3 @@
1
- export * from './Recognizer/types'
2
1
  export { useRecognizer } from './Recognizer/useRecognizer'
3
2
  export {
4
3
  useVoiceInputVolume,
@@ -11,3 +10,11 @@ export {
11
10
  export { SpeechRecognizer } from './Recognizer/SpeechRecognizer'
12
11
  export { RecognizerRef } from './Recognizer/RecognizerRef'
13
12
  export { NitroSpeech } from './NitroSpeech'
13
+ export {
14
+ type RecognizerSpec,
15
+ type SpeechRecognitionConfig,
16
+ type MutableSpeechRecognitionConfig,
17
+ type VolumeChangeEvent,
18
+ type RecognizerCallbacks,
19
+ type RecognizerMethods,
20
+ } from './Recognizer/types'
@@ -12,9 +12,24 @@ export interface Recognizer extends HybridObject<{
12
12
  /**
13
13
  * Prepare the speech recognition engine and the model for the given parameters.
14
14
  *
15
- * Omit the {@linkcode Promise} result if want to run synchronously.
15
+ * @notes
16
+ * - Not required, {@linkcode startListening} will prewarm automatically.
17
+ *
18
+ * - Omit the {@linkcode Promise} result if you want to run synchronously.
16
19
  * {@linkcode startListening} will start and resolve it automatically.
17
- * Only `await` if run beforehand and want to react on the success
20
+ *
21
+ * - Only `await` if run beforehand and you want to react to the success
22
+ *
23
+ * @worklet **Do not** use `await` on Worklet or UI runtimes. It will not work properly.
24
+ *
25
+ * You can run it synchronously:
26
+ * ```typescript
27
+ * scheduleOnRuntime(workletRuntime, () => {
28
+ * RecognizerRef.prewarm({
29
+ * // your config to prepare
30
+ * });
31
+ * });
32
+ * ```
18
33
  */
19
34
  prewarm(defaultParams?: SpeechRecognitionConfig): Promise<void>
20
35
 
@@ -30,9 +45,9 @@ export interface Recognizer extends HybridObject<{
30
45
  startListening(params?: SpeechRecognitionConfig): void
31
46
 
32
47
  /**
33
- * Stops the speech recognition. if not started, does nothing.
48
+ * Stops the speech recognition. If not started, does nothing.
34
49
  *
35
- * Not a sync operation for android, delay about 250ms to polish the result.
50
+ * Not a sync operation for Android, delay about 250ms to polish the result.
36
51
  *
37
52
  * Use {@linkcode onRecordingStopped} to handle the stop event.
38
53
  */
@@ -94,8 +109,6 @@ export interface Recognizer extends HybridObject<{
94
109
  * Fires every {@linkcode SpeechRecognitionConfig.autoFinishProgressIntervalMs} or 1000ms
95
110
  *
96
111
  * Time left in milliseconds until the timer stops.
97
- *
98
- * @note not implemented for Android yet.
99
112
  */
100
113
  onAutoFinishProgress?: (timeLeftMs: number) => void
101
114
  /**