UNPKG

web-asr-core

Version:

WebASR Core - Browser-based speech processing with VAD, WakeWord and Whisper - Unified all-in-one version

136 lines 5.29 kB
/** * Whisper 語音辨識服務 * * 提供無狀態的語音轉錄服務,使用 transformers.js 框架進行 Whisper 模型推論。 * 支援本地和遠端模型載入,提供靈活的語音辨識解決方案。 * * @fileoverview Whisper 語音辨識服務實現 * @author WebASRCore Team */ import type { WhisperResources, WhisperOptions, WhisperResult, WhisperLoadOptions } from '../types'; import { ConfigManager } from '../utils/config-manager'; /** * Whisper 事件發射器 * * @description 用於發送語音識別相關事件,外部可以監聽這些事件進行相應處理 * 事件類型: * - 'transcription-start': 轉錄開始 { timestamp: number } * - 'transcription-complete': 轉錄完成 { text: string, duration: number } * - 'processing-error': 處理錯誤 { error: Error, context: string } */ export declare const whisperEvents: EventTarget; /** * 使用 transformers.js 載入 Whisper 模型資源 * * @description 動態載入 Whisper 語音辨識模型,支援 CDN 和 npm 包兩種載入方式 * @param modelPathOrId - Whisper 模型路徑或 HuggingFace 模型 ID(可選,預設使用 ConfigManager 設定) * @param opts - 可選的模型載入配置 * @param config - 可選的配置管理器實例 * @returns Promise<WhisperResources> - Whisper 語音辨識資源 * @throws Error - 當模型載入失敗時拋出錯誤 * * @example * ```typescript * // 使用預設配置 * const resources = await loadWhisperResources(); * * // 使用自訂模型 * const resources = await loadWhisperResources('whisper-base', { * quantized: true, * localBasePath: './models/' * }); * * // 使用自訂配置管理器 * const config = new ConfigManager(); * config.whisper.modelPath = 'Xenova/whisper-large'; * const resources = await loadWhisperResources(undefined, undefined, config); * ``` */ export declare function loadWhisperResources(modelPathOrId?: string, opts?: WhisperLoadOptions, config?: ConfigManager): Promise<WhisperResources>; /** * 使用 Whisper 進行語音轉錄 * * @description 將音訊資料轉錄為文字,支援多語言和時間戳片段 * @param resources - Whisper 語音辨識資源(管道) * @param audio - 音訊資料,格式為 Float32Array(16kHz 單聲道) * @param options - 轉錄選項配置 * @returns Promise<WhisperResult> - 轉錄結果,包含文字和可選的時間戳片段 * @throws Error - 當轉錄失敗時拋出錯誤 * * @example * ```typescript * // 基本轉錄 * const result = await transcribe(resources, audioData); * console.log('轉錄結果:', result.text); * * // 帶時間戳的轉錄 * const result = await transcribe(resources, audioData, { * language: 'zh', * returnSegments: true * }); * ``` */ export declare function transcribe(resources: WhisperResources, audio: Float32Array, options?: WhisperOptions): Promise<WhisperResult>; /** * 將音訊分塊以進行串流轉錄的輔助函數 * * @description 將長音訊分割成較小的重疊塊,以支援串流轉錄處理 * @param audio - 原始音訊資料 * @param chunkSizeSeconds - 每個塊的大小(秒) * @param overlapSeconds - 塊間重疊大小(秒) * @param sampleRate - 音訊採樣率 * @param config - 可選的配置管理器實例 * @returns Float32Array[] - 分割後的音訊塊陣列 * * @example * ```typescript * const chunks = chunkAudioForTranscription(longAudio); * console.log(`分割成 ${chunks.length} 個音訊塊`); * * // 使用自訂參數 * const chunks = chunkAudioForTranscription(longAudio, 20, 3, 16000); * ``` * * @remarks 這是未來增強功能,不屬於 MVP 範圍 */ export declare function chunkAudioForTranscription(audio: Float32Array, chunkSizeSeconds?: number, overlapSeconds?: number, sampleRate?: number, config?: ConfigManager): Float32Array[]; /** * 處理多個音訊塊 * * @description 批次處理多個音訊塊。在 MVP 版本中,簡單地將所有塊串聯後作為一個整體進行轉錄 * @param resources - Whisper 語音辨識資源 * @param chunks - 音訊塊陣列 * @param options - 轉錄選項 * @returns Promise<WhisperResult> - 合併轉錄的結果 * * @example * ```typescript * const chunks = [chunk1, chunk2, chunk3]; * const result = await transcribeChunks(resources, chunks, { language: 'zh' }); * console.log('合併轉錄結果:', result.text); * ``` * * @remarks MVP 版本:將所有塊串聯為一個音訊進行轉錄 */ export declare function transcribeChunks(resources: WhisperResources, chunks: Float32Array[], options?: WhisperOptions): Promise<WhisperResult>; /** * 創建預設的 Whisper 選項 * * @description 從 ConfigManager 創建預設的 Whisper 轉錄選項 * @param config - 可選的配置管理器實例 * @returns WhisperOptions - Whisper 轉錄選項配置 * * @example * ```typescript * // 使用預設配置 * const options = createDefaultWhisperOptions(); * * // 使用自訂配置 * const config = new ConfigManager(); * config.whisper.language = 'en'; * config.whisper.task = 'translate'; * const options = createDefaultWhisperOptions(config); * ``` */ export declare function createDefaultWhisperOptions(config?: ConfigManager): WhisperOptions; //# sourceMappingURL=whisper.d.ts.map