@lakshmiprasanth/react-voice-to-text
Version:
A modern React package for voice-to-text conversion with real-time speech recognition and file upload support
268 lines (258 loc) • 8.19 kB
TypeScript
import React$1, { ReactNode } from 'react';
/**
* 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;
};
}
/**
* Props for VoiceRecorder component
*/
interface VoiceRecorderProps {
/** CSS class name */
className?: string;
/** Whether to show language selector */
showLanguageSelector?: boolean;
/** Whether to show results display */
showResults?: boolean;
/** Whether to show recording controls */
showControls?: boolean;
/** Default language */
language?: string;
/** Enable continuous recording */
continuous?: boolean;
/** Show interim results */
interimResults?: boolean;
/** Callback when result is received */
onResult?: (result: SpeechRecognitionResult) => void;
/** Callback when error occurs */
onError?: (error: string) => void;
/** Callback when recording starts */
onStart?: () => void;
/** Callback when recording stops */
onStop?: () => void;
/** Child components */
children?: React.ReactNode;
}
/**
* Props for FileUpload component
*/
interface FileUploadProps {
/** CSS class name */
className?: string;
/** Callback when file is selected */
onFileSelect?: (file: File) => void;
/** Callback when file is converted */
onConvert?: (file: File, language: string) => Promise<void>;
/** Callback when error occurs */
onError?: (error: string) => void;
/** Accepted file formats */
acceptedFormats?: string[];
/** Maximum file size in bytes */
maxFileSize?: number;
/** Whether to show language selector */
showLanguageSelector?: boolean;
/** Default language */
language?: string;
/** Enable drag and drop */
dragAndDrop?: boolean;
/** Allow multiple file selection */
multiple?: boolean;
/** Whether component is disabled */
disabled?: boolean;
/** Child components */
children?: React.ReactNode;
}
/**
* Props for ResultsDisplay component
*/
interface ResultsDisplayProps {
/** Array of recognition results */
results: SpeechRecognitionResult[];
/** Error message */
error?: string | null;
/** Callback to clear results */
onClear?: () => void;
/** Whether to show clear button */
showClearButton?: boolean;
/** Whether to show confidence scores */
showConfidence?: boolean;
/** Whether to show status (final/interim) */
showStatus?: boolean;
/** Maximum number of results to display */
maxResults?: number;
/** CSS class name */
className?: string;
/** Text to show when no results */
emptyText?: string;
}
/**
* Props for LanguageSelector component
*/
interface LanguageSelectorProps {
/** Selected language value */
value?: string;
/** Callback when language changes */
onChange?: (language: string) => void;
/** Whether component is disabled */
disabled?: boolean;
/** CSS class name */
className?: string;
/** Whether to show label */
showLabel?: boolean;
/** Label text */
label?: string;
/** Placeholder text */
placeholder?: string;
}
/**
* Props for RecordingControls component
*/
interface RecordingControlsProps {
/** Whether currently recording */
isRecording: boolean;
/** Whether currently processing */
isProcessing: boolean;
/** Callback to start recording */
onStart?: () => void;
/** Callback to stop recording */
onStop?: () => void;
/** Whether component is disabled */
disabled?: boolean;
/** CSS class name */
className?: string;
/** Whether to show status */
showStatus?: boolean;
/** Text for start button */
startText?: string;
/** Text for stop button */
stopText?: string;
/** Text for processing state */
processingText?: string;
}
interface VoiceToTextContextType {
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;
}
interface VoiceToTextProviderProps {
children: ReactNode;
options?: VoiceToTextOptions;
onResult?: (result: SpeechRecognitionResult) => void;
onError?: (error: string) => void;
onStart?: () => void;
onStop?: () => void;
}
declare const VoiceToTextProvider: React$1.FC<VoiceToTextProviderProps>;
declare const useVoiceToTextContext: () => VoiceToTextContextType;
declare const VoiceRecorder: React$1.FC<VoiceRecorderProps>;
declare const FileUpload: React$1.FC<FileUploadProps>;
interface VoiceToTextConverterProps {
className?: string;
showFileUpload?: boolean;
showVoiceRecorder?: boolean;
showResults?: boolean;
defaultLanguage?: string;
onResult?: (result: SpeechRecognitionResult) => void;
onError?: (error: string) => void;
onStart?: () => void;
onStop?: () => void;
children?: React$1.ReactNode;
}
declare const VoiceToTextConverter: React$1.FC<VoiceToTextConverterProps>;
declare const ResultsDisplay: React$1.FC<ResultsDisplayProps>;
declare const LanguageSelector: React$1.FC<LanguageSelectorProps>;
declare const RecordingControls: React$1.FC<RecordingControlsProps>;
export { FileUpload, LanguageSelector, RecordingControls, ResultsDisplay, VoiceRecorder, VoiceToTextConverter, VoiceToTextProvider, useVoiceToTextContext };
export type { FileUploadProps, LanguageSelectorProps, RecordingControlsProps, ResultsDisplayProps, VoiceRecorderProps };