web-asr-core
Version:
WebASR Core - Browser-based speech processing with VAD, WakeWord and Whisper - Unified all-in-one version
226 lines • 5.05 kB
TypeScript
/**
* SpeechService - Web Speech API 事件驅動服務(Event Architecture v2)
*
* @description 提供語音合成(TTS)和語音識別(STT)的事件驅動服務
* 整合 Web Speech API 的純函數實作與事件系統
*
* @module SpeechService
*/
import { EventEmitter } from '../core/EventEmitter';
import { ConfigManager } from '../utils/config-manager';
import type { TTSState, TTSParams, STTState, STTParams } from './speech';
/**
* Speech 服務事件定義
*/
export interface SpeechEvents {
ready: {
ttsSupported: boolean;
sttSupported: boolean;
voices: Array<{
name: string;
lang: string;
}>;
timestamp: number;
};
error: {
type: 'tts' | 'stt';
error: Error | string;
context: string;
timestamp: number;
};
'tts-start': {
text: string;
voice: string | null;
rate: number;
pitch: number;
volume: number;
timestamp: number;
};
'tts-end': {
text: string;
duration: number;
timestamp: number;
};
'tts-pause': {
text: string;
position: number;
timestamp: number;
};
'tts-resume': {
text: string;
position: number;
timestamp: number;
};
'tts-boundary': {
text: string;
charIndex: number;
charLength: number;
word: string;
timestamp: number;
};
'tts-mark': {
text: string;
mark: string;
timestamp: number;
};
'stt-start': {
language: string;
continuous: boolean;
timestamp: number;
};
'stt-result': {
transcript: string;
isFinal: boolean;
confidence: number;
alternatives?: Array<{
transcript: string;
confidence: number;
}>;
timestamp: number;
};
'stt-end': {
finalTranscript: string;
duration: number;
timestamp: number;
};
'stt-nomatch': {
timestamp: number;
};
'stt-speechstart': {
timestamp: number;
};
'stt-speechend': {
timestamp: number;
};
'stt-audiostart': {
timestamp: number;
};
'stt-audioend': {
timestamp: number;
};
}
/**
* SpeechService - Web Speech API 事件驅動服務
*
* @example
* ```typescript
* const speechService = new SpeechService();
*
* // TTS 使用範例
* speechService.on('tts-start', ({ text }) => {
* console.log(`開始說話: ${text}`);
* });
*
* speechService.on('tts-boundary', ({ word }) => {
* console.log(`當前單字: ${word}`);
* });
*
* await speechService.speak('你好,世界!', {
* voice: 'zh-TW',
* rate: 1.2
* });
*
* // STT 使用範例
* speechService.on('stt-result', ({ transcript, isFinal }) => {
* console.log(`識別結果: ${transcript} (${isFinal ? '最終' : '暫時'})`);
* });
*
* await speechService.startListening({
* language: 'zh-TW',
* continuous: true
* });
* ```
*/
export declare class SpeechService extends EventEmitter<SpeechEvents> {
private config;
private ttsResources;
private ttsState;
private currentUtterance;
private ttsStartTime;
private sttResources;
private sttState;
private sttStartTime;
constructor(config?: ConfigManager);
/**
* 初始化服務
*/
initialize(): Promise<void>;
/**
* 設定 STT 事件處理器
*/
private setupSTTEventHandlers;
/**
* 開始說話(TTS)
*
* @param text 要說的文字
* @param params TTS 參數
*/
speak(text: string, params?: Partial<TTSParams>): Promise<void>;
/**
* 暫停說話
*/
pause(): void;
/**
* 恢復說話
*/
resume(): void;
/**
* 停止說話
*/
stop(): void;
/**
* 開始語音識別
*
* @param params STT 參數
*/
startListening(params?: STTParams): Promise<void>;
/**
* 停止語音識別
*/
stopListening(): void;
/**
* 取得可用語音列表
*
* @param language 語言篩選
* @returns 語音列表
*/
getVoices(language?: string): Array<{
name: string;
lang: string;
localService: boolean;
}>;
/**
* 設定預設語音
*
* @param voice 語音名稱或語言代碼
*/
setDefaultVoice(voice: string): void;
/**
* 設定預設語言(STT)
*
* @param language 語言代碼
*/
setDefaultLanguage(language: string): void;
/**
* 取得 TTS 狀態
*/
getTTSState(): TTSState;
/**
* 取得 STT 狀態
*/
getSTTState(): STTState;
/**
* 檢查支援狀態
*/
getSupport(): {
tts: boolean;
stt: boolean;
details: any;
};
/**
* 清理資源
*/
dispose(): void;
}
export default SpeechService;
//# sourceMappingURL=SpeechService.d.ts.map