@lakshmiprasanth/react-voice-to-text
Version:
A modern React package for voice-to-text conversion with real-time speech recognition and file upload support
192 lines (186 loc) • 5.78 kB
TypeScript
/**
* Configuration options for speech recognition engines
*/
interface SpeechRecognitionConfig {
/** Language code (e.g., 'en-US', 'es-ES') */
language?: string;
/** Sample rate for audio processing */
sampleRate?: number;
/** Enable continuous recognition */
continuous?: boolean;
/** Return interim results */
interimResults?: boolean;
/** Maximum number of alternatives to return */
maxAlternatives?: number;
/** Confidence threshold (0-1) */
confidenceThreshold?: number;
/** Custom vocabulary or phrases to improve recognition */
phrases?: string[];
/** Audio encoding format */
encoding?: 'LINEAR16' | 'FLAC' | 'MULAW' | 'AMR' | 'AMR_WB' | 'OGG_OPUS' | 'SPEEX_WITH_HEADER_BYTE';
}
/**
* Speech recognition result
*/
interface SpeechRecognitionResult {
/** Transcribed text */
transcript: string;
/** Confidence score (0-1) */
confidence: number;
/** Whether this is a final result */
isFinal: boolean;
/** Alternative transcriptions */
alternatives?: Array<{
transcript: string;
confidence: number;
}>;
/** Timestamp information */
timestamp?: {
start: number;
end: number;
};
}
/**
* Engine-specific configuration
*/
interface EngineConfig {
/** Engine type */
engine: 'web-speech' | 'vosk' | 'google-cloud' | 'whisper';
/** API key (for cloud services) */
apiKey?: string;
/** Model path (for offline engines like Vosk) */
modelPath?: string;
/** Project ID (for Google Cloud) */
projectId?: string;
/** Custom endpoint URL */
endpoint?: string;
}
/**
* Voice-to-text converter options
*/
interface VoiceToTextOptions {
/** Default engine configuration */
defaultEngine?: EngineConfig;
/** Default speech recognition configuration */
defaultRecognitionConfig?: SpeechRecognitionConfig;
/** Enable automatic engine fallback */
enableFallback?: boolean;
/** Engine priority order for fallback */
enginePriority?: Array<EngineConfig['engine']>;
/** Debug mode */
debug?: boolean;
}
/**
* Browser compatibility information
*/
interface BrowserSupport {
/** Whether Web Speech API is supported */
webSpeechAPI: boolean;
/** Whether MediaRecorder API is supported */
mediaRecorder: boolean;
/** Whether getUserMedia is supported */
getUserMedia: boolean;
/** Browser name and version */
browser: {
name: string;
version: string;
};
}
interface UseVoiceToTextOptions extends VoiceToTextOptions {
onResult?: (result: SpeechRecognitionResult) => void;
onError?: (error: string) => void;
onStart?: () => void;
onStop?: () => void;
}
interface UseVoiceToTextReturn {
isInitialized: boolean;
isRecording: boolean;
isProcessing: boolean;
results: SpeechRecognitionResult[];
error: string | null;
browserSupport: BrowserSupport | null;
startRecording: (options?: Partial<SpeechRecognitionConfig>) => Promise<void>;
stopRecording: () => Promise<void>;
clearResults: () => void;
clearError: () => void;
updateConfig: (config: Partial<VoiceToTextOptions>) => void;
getConfig: () => VoiceToTextOptions;
}
declare const useVoiceToText: (options?: UseVoiceToTextOptions) => UseVoiceToTextReturn;
interface UseVoiceRecorderOptions {
language?: string;
continuous?: boolean;
interimResults?: boolean;
onResult?: (result: any) => void;
onError?: (error: string) => void;
onStart?: () => void;
onStop?: () => void;
}
interface UseVoiceRecorderReturn {
isInitialized: boolean;
isRecording: boolean;
isProcessing: boolean;
results: any[];
error: string | null;
startRecording: () => Promise<void>;
stopRecording: () => Promise<void>;
clearResults: () => void;
clearError: () => void;
}
declare const useVoiceRecorder: (options?: UseVoiceRecorderOptions) => UseVoiceRecorderReturn;
interface UseFileUploadOptions {
acceptedFormats?: string[];
maxFileSize?: number;
onFileSelect?: (file: File) => void;
onConvert?: (file: File, language: string) => Promise<any>;
onError?: (error: string) => void;
}
interface UseFileUploadReturn {
selectedFile: File | null;
isConverting: boolean;
results: any[];
error: string | null;
selectFile: (file: File) => void;
convertFile: (language?: string) => Promise<void>;
clearFile: () => void;
clearResults: () => void;
clearError: () => void;
validateFile: (file: File) => {
isValid: boolean;
error?: string;
};
}
declare const useFileUpload: (options?: UseFileUploadOptions) => UseFileUploadReturn;
interface UseSpeechRecognitionOptions {
language?: string;
continuous?: boolean;
interimResults?: boolean;
maxAlternatives?: number;
onResult?: (event: any) => void;
onError?: (event: any) => void;
onStart?: () => void;
onEnd?: () => void;
onAudioStart?: () => void;
onAudioEnd?: () => void;
onSoundStart?: () => void;
onSoundEnd?: () => void;
onSpeechStart?: () => void;
onSpeechEnd?: () => void;
onNoMatch?: () => void;
onNomatch?: () => void;
}
interface UseSpeechRecognitionReturn {
isSupported: boolean;
isListening: boolean;
transcript: string;
finalTranscript: string;
interimTranscript: string;
error: string | null;
start: () => void;
stop: () => void;
abort: () => void;
reset: () => void;
updateConfig: (config: Partial<UseSpeechRecognitionOptions>) => void;
}
declare const useSpeechRecognition: (options?: UseSpeechRecognitionOptions) => UseSpeechRecognitionReturn;
export { useFileUpload, useSpeechRecognition, useVoiceRecorder, useVoiceToText };