UNPKG

xfyun-sdk

Version:

科大讯飞语音识别 SDK,支持浏览器中实时语音听写功能

278 lines (277 loc) 7.87 kB
/** * 科大讯飞语音 SDK 类型定义 */ /** ASR 识别状态 */ export type RecognizerState = 'idle' | 'connecting' | 'connected' | 'recording' | 'stopped' | 'error'; /** * ASR 配置选项 * * 包含语音识别所需的所有配置参数。 */ export interface XfyunASROptions { appId: string; apiKey: string; apiSecret: string; language?: 'zh_cn' | 'en_us'; domain?: 'iat' | 'medical' | 'assistant'; accent?: 'mandarin' | 'cantonese'; vadEos?: number; maxAudioSize?: number; autoStart?: boolean; hotWords?: string[]; punctuation?: boolean | string; audioFormat?: string; reconnectAttempts?: number; reconnectInterval?: number; enableReconnect?: boolean; logLevel?: 'debug' | 'info' | 'warn' | 'error'; } /** * ASR 事件处理函数接口 * * 定义语音识别过程中可注册的事件回调。 */ export interface ASREventHandlers { onStart?: () => void; onStop?: () => void; onRecognitionResult?: (text: string, isEnd: boolean) => void; onProcess?: (volume: number) => void; onError?: (error: XfyunError) => void; onStateChange?: (state: RecognizerState) => void; } /** * 讯飞 API 错误对象 */ export interface XfyunError { code: number; message: string; data?: unknown; } /** WebSocket 请求结构 */ export interface XfyunWebsocketRequest { common?: { app_id: string; }; business?: { language?: string; domain?: string; accent?: string; vad_eos?: number; dwa?: string; pd?: string; ptt?: number; rlang?: string; vinfo?: number; nunum?: number; speex_size?: number; nbest?: number; wbest?: number; nlu?: string; hotwords?: string; punctuation?: string; }; data?: { status: number; format: string; encoding: string; audio?: string; }; } /** WebSocket 响应结构 */ export interface XfyunWebsocketResponse { code: number; message: string; sid?: string; data?: { result?: { ws: Array<{ bg: number; cw: Array<{ w: string; sc: number; }>; }>; sn: number; ls: boolean; bg: number; ed: number; }; status: number; }; } /** TTS 音频格式类型 */ export type TTSAudioFormat = 'mp3' | 'wav' | 'pcm'; /** * TTS 音色名称类型 * * 包含讯飞 TTS 支持的所有音色选项。 */ export type TTSVoiceName = 'xiaoyan' | 'aisjiuxu' | 'aisxping' | 'aisjinger' | 'aisbabyxu' | 'aisxiaoyuan' | 'aisxingchen' | 'aisdengdeng' | 'aisyaoyao' | 'aismall' | 'aisxiaofeng' | 'aisnan' | 'aisxiaosong' | 'aisxiaoyong' | 'aisxiaowang' | 'aisxiaole' | 'aisxiaoy' | 'aisxiaolin' | 'aisxiaoming' | 'aisxiaogang' | 'aisdarong' | 'aisnvpeach' | 'aisxiaowuma' | 'aisxiaorong' | 'aischanghong' | 'aisxiaoyaxi' | 'aisjiuyuan' | 'aisxiaoxian' | 'aisxiaomao' | 'aisxiaoli' | 'aisxiaokan' | 'aisxiaoning' | 'aismary' | 'aisxiaowawa' | 'aisxiaoxue' | 'aisxiaoyan'; /** TTS 合成器状态类型 */ export type SynthesizerState = 'idle' | 'connecting' | 'connected' | 'synthesizing' | 'stopped' | 'error'; /** TTS 错误对象 */ export interface TTSError { code: number; message: string; data?: unknown; } /** * TTS 事件处理函数接口 * * 定义语音合成过程中可注册的事件回调。 */ export interface TTSEventHandlers { onStart?: () => void; onEnd?: () => void; onStop?: () => void; onAudioData?: (audioData: ArrayBuffer) => void; onProgress?: (current: number, total: number) => void; onError?: (error: TTSError) => void; onStateChange?: (state: SynthesizerState) => void; } /** * TTS 配置选项 * * 包含语音合成所需的所有配置参数。 */ export interface XfyunTTSOptions { appId: string; apiKey: string; apiSecret: string; voice_name?: TTSVoiceName | string; speed?: number; pitch?: number; volume?: number; accent?: string; audioFormat?: TTSAudioFormat; sampleRate?: number; autoStart?: boolean; enableCache?: boolean; logLevel?: 'debug' | 'info' | 'warn' | 'error'; } /** 翻译模式类型 */ export type TranslatorType = 'asr' | 'text'; /** 源语言代码 */ export type SourceLanguage = 'cn' | 'en' | 'ja' | 'ko' | 'fr' | 'es' | 'it' | 'de' | 'pt' | 'vi' | 'id' | 'ms' | 'ru' | 'ar' | 'hi' | 'th'; /** 目标语言代码 */ export type TargetLanguage = 'cn' | 'en' | 'ja' | 'ko' | 'fr' | 'es' | 'it' | 'de' | 'pt' | 'vi' | 'id' | 'ms' | 'ru' | 'ar' | 'hi' | 'th'; /** 翻译器状态类型 */ export type TranslatorState = 'idle' | 'connecting' | 'connected' | 'translating' | 'stopped' | 'error'; /** 翻译错误对象 */ export interface TranslatorError { code: number; message: string; data?: unknown; } /** 翻译结果对象 */ export interface TranslationResult { sourceLanguage: SourceLanguage; targetLanguage: TargetLanguage; sourceText: string; targetText: string; isFinal: boolean; confidence?: number; } /** * 翻译事件处理函数接口 * * 定义语音/文本翻译过程中可注册的事件回调。 */ export interface TranslatorEventHandlers { onStart?: () => void; onEnd?: () => void; onStop?: () => void; onResult?: (result: TranslationResult) => void; onError?: (error: TranslatorError) => void; onStateChange?: (state: TranslatorState) => void; } /** * 翻译器配置选项 * * 包含语音/文本翻译所需的所有配置参数。 */ export interface XfyunTranslatorOptions { appId: string; apiKey: string; apiSecret: string; type?: TranslatorType; from?: SourceLanguage; to?: TargetLanguage; domain?: 'iner' | 'video' | 'command' | 'doc' | 'phonecall' | 'medical'; autoStart?: boolean; vadEos?: number; sampleRate?: number; logLevel?: 'debug' | 'info' | 'warn' | 'error'; } /** * 离线 ASR 配置选项 * * 用于无网络情况下的语音识别引擎配置。 */ export interface OfflineASROptions { /** 引擎类型,固定为 'smsys' */ engine?: 'smsys'; /** 语言 */ language?: 'zh_cn' | 'en_us'; /** 领域 */ domain?: 'iat' | 'search' | 'commands'; /** 采样率 */ sampleRate?: 8000 | 16000; /** 垂直领域(可选) */ nbest?: number; /** 识别结果候选数 */ wbest?: number; } /** * 声纹识别配置选项 * @description 声纹识别( Speaker Verification / Identification ) */ export interface SpeakerVerifyOptions { /** 讯飞应用 ID */ appId: string; /** 讯飞 API Key */ apiKey: string; /** 讯飞 API Secret */ apiSecret: string; /** 声纹场景: verify=验证, identify=识别 */ scene: 'verify' | 'identify'; /** 声纹模型: 21360=一比一验证, 21361=一比N识别 */ engine_type: '21360' | '21361'; /** 用户 ID(identify 模式必填) */ user_id?: string; /** 音频格式 */ audioFormat?: 'wav' | 'pcm' | 'opus'; /** 采样率 */ sampleRate?: number; /** 日志级别 */ logLevel?: 'debug' | 'info' | 'warn' | 'error'; } /** * 声纹识别结果 */ export interface SpeakerVerifyResult { /** 是否验证通过 */ success: boolean; /** 置信度 0-100 */ score: number; /** 识别到的用户 ID(identify 模式) */ user_id?: string; /** 错误信息 */ message?: string; } /** * 声纹注册结果 */ export interface SpeakerRegisterResult { success: boolean; message: string; user_id?: string; } /** * 检测是否在浏览器环境 */ export declare function isBrowser(): boolean; /** * 获取 SDK 版本 */ export declare const SDK_VERSION: string;