voice-to-text-converter
Version:
A modern, lightweight Node.js package for speech-to-text conversion with support for multiple engines
179 lines • 5.5 kB
TypeScript
import { EventEmitter } from 'events';
/**
* Configuration options for speech recognition engines
*/
export 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
*/
export 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;
};
}
/**
* Audio input source configuration
*/
export interface AudioInputConfig {
/** Audio source type */
source: 'microphone' | 'file' | 'stream';
/** File path (for file source) */
filePath?: string;
/** Audio stream (for stream source) */
audioStream?: NodeJS.ReadableStream;
/** Device ID (for microphone source) */
deviceId?: string;
/** Recording duration in milliseconds (for microphone) */
duration?: number;
}
/**
* Engine-specific configuration
*/
export 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;
}
/**
* Error types that can occur during speech recognition
*/
export declare enum SpeechRecognitionErrorType {
NO_SPEECH = "no-speech",
ABORTED = "aborted",
AUDIO_CAPTURE = "audio-capture",
NETWORK = "network",
NOT_ALLOWED = "not-allowed",
SERVICE_NOT_ALLOWED = "service-not-allowed",
BAD_GRAMMAR = "bad-grammar",
LANGUAGE_NOT_SUPPORTED = "language-not-supported",
ENGINE_ERROR = "engine-error",
INVALID_CONFIG = "invalid-config"
}
/**
* Speech recognition error
*/
export declare class SpeechRecognitionError extends Error {
type: SpeechRecognitionErrorType;
originalError?: Error | undefined;
constructor(type: SpeechRecognitionErrorType, message: string, originalError?: Error | undefined);
}
/**
* Events emitted by speech recognition engines
*/
export interface SpeechRecognitionEvents {
'start': () => void;
'result': (result: SpeechRecognitionResult) => void;
'end': () => void;
'error': (error: SpeechRecognitionError) => void;
'audiostart': () => void;
'audioend': () => void;
'soundstart': () => void;
'soundend': () => void;
'speechstart': () => void;
'speechend': () => void;
}
/**
* Base interface for speech recognition engines
*/
export interface ISpeechRecognitionEngine extends EventEmitter {
/** Start speech recognition */
start(audioConfig: AudioInputConfig, recognitionConfig?: SpeechRecognitionConfig): Promise<void>;
/** Stop speech recognition */
stop(): Promise<void>;
/** Abort speech recognition */
abort(): Promise<void>;
/** Check if engine is available */
isAvailable(): boolean;
/** Get supported languages */
getSupportedLanguages(): string[];
/** Process audio file directly */
processFile(filePath: string, config?: SpeechRecognitionConfig): Promise<SpeechRecognitionResult[]>;
/** Process audio stream */
processStream(stream: NodeJS.ReadableStream, config?: SpeechRecognitionConfig): Promise<SpeechRecognitionResult[]>;
}
/**
* Voice-to-text converter options
*/
export 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;
}
/**
* Microphone recording options
*/
export interface MicrophoneOptions {
/** Recording duration in milliseconds */
duration?: number;
/** Device ID to use */
deviceId?: string;
/** Sample rate */
sampleRate?: number;
/** Number of channels */
channels?: number;
/** Bit depth */
bitDepth?: number;
/** Audio format */
format?: 'wav' | 'raw';
}
/**
* Browser compatibility information
*/
export 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;
};
}
//# sourceMappingURL=index.d.ts.map