@mazka/react-speech-to-text
Version:
A powerful, TypeScript-first React hook for speech recognition using the Web Speech API. This library provides a simple yet comprehensive interface for converting speech to text in React applications.
146 lines (142 loc) • 5.58 kB
TypeScript
interface SpeechRecognition extends EventTarget {
continuous: boolean;
grammars: SpeechGrammarList;
interimResults: boolean;
lang: string;
maxAlternatives: number;
serviceURI: string;
onaudioend: ((this: SpeechRecognition, ev: Event) => void) | null;
onaudiostart: ((this: SpeechRecognition, ev: Event) => void) | null;
onend: ((this: SpeechRecognition, ev: Event) => void) | null;
onerror: ((this: SpeechRecognition, ev: SpeechRecognitionErrorEvent) => void) | null;
onnomatch: ((this: SpeechRecognition, ev: SpeechRecognitionEvent) => void) | null;
onresult: ((this: SpeechRecognition, ev: SpeechRecognitionEvent) => void) | null;
onsoundend: ((this: SpeechRecognition, ev: Event) => void) | null;
onsoundstart: ((this: SpeechRecognition, ev: Event) => void) | null;
onspeechend: ((this: SpeechRecognition, ev: Event) => void) | null;
onspeechstart: ((this: SpeechRecognition, ev: Event) => void) | null;
onstart: ((this: SpeechRecognition, ev: Event) => void) | null;
abort(): void;
start(): void;
stop(): void;
}
interface SpeechRecognitionStatic {
new (): SpeechRecognition;
prototype: SpeechRecognition;
}
interface SpeechRecognitionEvent extends Event {
readonly resultIndex: number;
readonly results: SpeechRecognitionResultList;
readonly interpretation: unknown;
readonly emma: Document | null;
}
interface SpeechRecognitionErrorEvent extends Event {
readonly error: SpeechRecognitionErrorCode;
readonly message: string;
}
type SpeechRecognitionErrorCode = "no-speech" | "aborted" | "audio-capture" | "network" | "not-allowed" | "service-not-allowed" | "bad-grammar" | "language-not-supported";
interface SpeechRecognitionResultList {
readonly length: number;
item(index: number): SpeechRecognitionResult;
[index: number]: SpeechRecognitionResult;
}
interface SpeechRecognitionResult {
readonly isFinal: boolean;
readonly length: number;
item(index: number): SpeechRecognitionAlternative;
[index: number]: SpeechRecognitionAlternative;
}
interface SpeechRecognitionAlternative {
readonly transcript: string;
readonly confidence: number;
}
interface SpeechGrammarList {
readonly length: number;
item(index: number): SpeechGrammar;
addFromURI(src: string, weight?: number): void;
addFromString(string: string, weight?: number): void;
[index: number]: SpeechGrammar;
}
interface SpeechGrammar {
src: string;
weight: number;
}
declare global {
interface Window {
SpeechRecognition?: SpeechRecognitionStatic;
webkitSpeechRecognition?: SpeechRecognitionStatic;
mozSpeechRecognition?: SpeechRecognitionStatic;
msSpeechRecognition?: SpeechRecognitionStatic;
SpeechGrammarList?: {
new (): SpeechGrammarList;
prototype: SpeechGrammarList;
};
webkitSpeechGrammarList?: {
new (): SpeechGrammarList;
prototype: SpeechGrammarList;
};
}
}
interface SpeechToTextResult {
transcript: string;
confidence: number;
isFinal: boolean;
timestamp: Date;
}
interface SpeechToTextStateError {
code: string;
message: string;
name: string;
browserInfo?: {
browserName: string;
reason: string;
};
}
interface SpeechToTextState {
isListening: boolean;
isSupported: boolean;
transcript: string;
interimTranscript: string;
finalTranscript: string;
results: SpeechToTextResult[];
error: SpeechToTextStateError | null;
isInitializing: boolean;
isPaused: boolean;
isAutoStopping: boolean;
lastSpeechTimestamp: Date | null;
}
interface SpeechToTextOptions {
continuous?: boolean;
interimResults?: boolean;
maxAlternatives?: number;
language?: string;
autoStopOnSilence?: {
enabled: boolean;
silenceDuration?: number;
onAutoStop?: (transcript: string) => void;
};
}
interface SpeechToTextActions {
startListening: (options?: Partial<SpeechToTextOptions>) => void;
stopListening: () => void;
pauseListening: () => void;
resumeListening: () => void;
abortListening: () => void;
resetTranscript: () => void;
clearError: () => void;
}
interface UseSpeechToTextReturn extends SpeechToTextState, SpeechToTextActions {
}
/**
* Custom hook for speech-to-text functionality using the Web Speech API.
* It provides methods to start, stop, and manage speech recognition,
* as well as handling browser compatibility and errors.
* This hook is designed to be used in React applications
* and provides a simple interface for integrating speech recognition.
* @param initialOptions - Optional initial configuration for speech recognition.
* It can include options like language, continuous mode, interim results, etc.
* @returns An object containing the current state of speech recognition,
* methods to control the recognition process, and error handling.
*/
declare const useSpeechToText: (initialOptions?: Partial<SpeechToTextOptions>) => UseSpeechToTextReturn;
export { type SpeechGrammar, type SpeechGrammarList, type SpeechRecognition, type SpeechRecognitionAlternative, type SpeechRecognitionErrorCode, type SpeechRecognitionErrorEvent, type SpeechRecognitionEvent, type SpeechRecognitionResult, type SpeechRecognitionResultList, type SpeechRecognitionStatic, type SpeechToTextActions, type SpeechToTextOptions, type SpeechToTextResult, type SpeechToTextState, type SpeechToTextStateError, type UseSpeechToTextReturn, useSpeechToText };