Skip to content

Commit

Permalink
feat: export speech event types
Browse files Browse the repository at this point in the history
  • Loading branch information
jamsch committed Sep 29, 2024
1 parent 3d67629 commit 6ac775a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 57 deletions.
16 changes: 9 additions & 7 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@ import {
ExpoSpeechRecognitionModule,
getSpeechRecognitionServices,
getSupportedLocales,
type ExpoSpeechRecognitionOptions,
type AndroidIntentOptions,
useSpeechRecognitionEvent,
type AudioEncodingAndroidValue,
TaskHintIOS,
AVAudioSessionCategory,
type AVAudioSessionCategoryValue,
AVAudioSessionCategoryOptions,
type AVAudioSessionCategoryOptionsValue,
type SetCategoryOptions,
AVAudioSessionMode,
type AVAudioSessionModeValue,
ExpoWebSpeechRecognition,
} from "expo-speech-recognition";
import type {
AudioEncodingAndroidValue,
AndroidIntentOptions,
AVAudioSessionCategoryValue,
AVAudioSessionModeValue,
AVAudioSessionCategoryOptionsValue,
ExpoSpeechRecognitionOptions,
SetCategoryOptions,
} from "expo-speech-recognition";
import { useEffect, useMemo, useRef, useState } from "react";
import {
OptionButton,
Expand Down
2 changes: 1 addition & 1 deletion src/ExpoSpeechRecognitionModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { ExpoSpeechRecognitionModuleType } from "./ExpoSpeechRecognitionMod
const ExpoSpeechRecognitionNativeModule =
requireNativeModule<ExpoSpeechRecognitionModuleType>("ExpoSpeechRecognition");

export const ExpoSpeechRecognitionModule = {
export const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType = {
...ExpoSpeechRecognitionNativeModule,
// Avoid any function bindings when calling the native module
stop: () => ExpoSpeechRecognitionNativeModule.stop(),
Expand Down
100 changes: 57 additions & 43 deletions src/ExpoSpeechRecognitionModule.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,48 @@ import type {
export type AVAudioSessionCategoryValue =
(typeof AVAudioSessionCategory)[keyof typeof AVAudioSessionCategory];

export type ExpoSpeechRecognitionResult = {
transcript: string;
/**
* Value ranging between between 0.0, 1.0, and -1 (unavailable) indicating transcript confidence.
*/
confidence: number;
/**
* An array of transcription segments that represent the parts of the transcription, as identified by the speech recognizer.
*
* Notes for Android:
*
* - This is only available for SDK 34+ (Android 14+)
* - This is only verified to work with the `com.google.android.as` service (using on device speech recognition)
* - Segments are only available during the final result
* - The segment parts are split up by words.
* - The segments are only available for the first transcript
* - Segment confidences currently return as -1 (unavailable)
*
* Notes for iOS:
*
* - The confidence value will be 0 on partial results
*/
segments: ExpoSpeechRecognitionResultSegment[];
};

export type ExpoSpeechRecognitionResultSegment = {
/** The start timestamp of the utterance, e.g. 1000 */
startTimeMillis: number;
/** The end timestamp of the utterance, e.g. 1500 */
endTimeMillis: number;
/** The text portion of the transcript, e.g. "Hello world" */
segment: string;
/** Value ranging between between 0.0, 1.0, and -1 (unavailable) indicating the confidence of the specific segment */
confidence: number;
};

/** Fired when there's a speech result. The result may be partial or final */
export type ExpoSpeechRecognitionResultEvent = {
isFinal: boolean;
results: ExpoSpeechRecognitionResult[];
};

export type ExpoSpeechRecognitionErrorCode =
/** The user called `ExpoSpeechRecognitionModule.abort()`. */
| "aborted"
Expand All @@ -37,51 +79,17 @@ export type ExpoSpeechRecognitionErrorCode =
/** (Android only) An unknown client-side error occurred. */
| "client";

export type ExpoSpeechRecognitionErrorEvent = {
error: ExpoSpeechRecognitionErrorCode;
message: string;
};

/**
* Events that are dispatched from the native side
*/
export type ExpoSpeechRecognitionNativeEventMap = {
/** Fired when there's a speech result. The result may be partial or final */
result: {
isFinal: boolean;
results: {
transcript: string;
/**
* Value ranging between between 0.0, 1.0, and -1 (unavailable) indicating transcript confidence.
*/
confidence: number;
/**
* An array of transcription segments that represent the parts of the transcription, as identified by the speech recognizer.
*
* Notes for Android:
*
* - This is only available for SDK 34+ (Android 14+)
* - This is only verified to work with the `com.google.android.as` service (using on device speech recognition)
* - Segments are only available during the final result
* - The segment parts are split up by words.
* - The segments are only available for the first transcript
* - Segment confidences currently return as -1 (unavailable)
*
* Notes for iOS:
*
* - The confidence value will be 0 on partial results
*/
segments: {
/** The start timestamp of the utterance, e.g. 1000 */
startTimeMillis: number;
/** The end timestamp of the utterance, e.g. 1500 */
endTimeMillis: number;
/** The text portion of the transcript, e.g. "Hello world" */
segment: string;
/** Value ranging between between 0.0, 1.0, and -1 (unavailable) indicating the confidence of the specific segment */
confidence: number;
}[];
}[];
};
error: {
error: ExpoSpeechRecognitionErrorCode;
message: string;
};
result: ExpoSpeechRecognitionResultEvent;
error: ExpoSpeechRecognitionErrorEvent;
start: null;
speechstart: null;
speechend: null;
Expand Down Expand Up @@ -533,15 +541,21 @@ export interface ExpoSpeechRecognitionModuleType extends NativeModule {
*
* @returns empty string if no service is found, or not Android
*/
getDefaultRecognitionService(): { packageName: string };
getDefaultRecognitionService(): {
/** e.g. "com.google.android.tts" or "com.google.android.googlequicksearchbox" */
packageName: string;
};
/**
* [Android only] Returns the default voice recognition service on the device.
*
* e.g. "com.google.android.googlequicksearchbox" or "com.samsung.android.bixby.agent"
*
* @returns empty string if no service is found, or not Android
*/
getAssistantService(): { packageName: string };
getAssistantService(): {
/** e.g. "com.google.android.googlequicksearchbox" or "com.samsung.android.bixby.agent" */
packageName: string;
};
/**
* Whether the on-device speech recognition is available on the device.
*/
Expand Down
8 changes: 4 additions & 4 deletions src/ExpoWebSpeechRecognition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,9 @@ export class ExpoWebSpeechRecognition implements SpeechRecognition {
? (((ev) => {
listener.call(this, ev);
// remove the listeners from the map
this.#subscriptionMap.get(listener)?.forEach((sub) => {
for (const sub of this.#subscriptionMap.get(listener) ?? []) {
sub.remove();
});
}
this.#subscriptionMap.delete(listener);
}) as SpeechListener<K>)
: listener;
Expand Down Expand Up @@ -443,8 +443,8 @@ export class ExpoWebSpeechGrammarList implements SpeechGrammarList {
}

export class ExpoWebSpeechGrammar implements SpeechGrammar {
src: string = "";
weight: number = 1;
src = "";
weight = 1;

constructor(src: string, weight?: number) {
this.src = src;
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ export {
type IOSTaskHintValue,
type SetCategoryOptions,
type ExpoSpeechRecognitionErrorCode,
type ExpoSpeechRecognitionErrorEvent,
type ExpoSpeechRecognitionResultEvent,
type ExpoSpeechRecognitionResult,
type ExpoSpeechRecognitionResultSegment,
} from "./ExpoSpeechRecognitionModule.types";
4 changes: 2 additions & 2 deletions src/useSpeechRecognitionEvent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useRef } from "react";
import { ExpoSpeechRecognitionModuleEmitter } from "./ExpoSpeechRecognitionModule";
import { ExpoSpeechRecognitionNativeEventMap } from "./ExpoSpeechRecognitionModule.types";
import type { ExpoSpeechRecognitionNativeEventMap } from "./ExpoSpeechRecognitionModule.types";

/**
* This hook allows you to listen to native events emitted by the `ExpoSpeechRecognitionModule`.
Expand All @@ -26,5 +26,5 @@ export function useSpeechRecognitionEvent<
handler,
);
return subscription.remove;
}, [eventName, listenerRef]);
}, [eventName]);
}

0 comments on commit 6ac775a

Please sign in to comment.