/**
|
* 语音合成工具类
|
* @class SpeechSynthesisUtil
|
* @description 用于处理文字转语音的工具类,包含音频播放、合成等功能
|
*/
|
export declare enum EventType {
|
STATE_CHANGE = "stateChange",
|
SYNTHESIS_START = "synthesisStart",
|
SYNTHESIS_END = "synthesisEnd",
|
AUDIO_PLAY = "audioPlay",
|
AUDIO_END = "audioEnd",
|
ERROR = "error",
|
PROGRESS = "progress",
|
PAUSE = "pause",
|
RESUME = "resume"
|
}
|
interface State {
|
isInterrupted: boolean;
|
isStartPlayQueue: boolean;
|
isPlaying: boolean;
|
isEnded: boolean;
|
isError: boolean;
|
isPaused: boolean;
|
isSynthesizing: boolean;
|
needsUserInteraction: boolean;
|
}
|
interface SpeechSynthesisOptions {
|
API_KEY: string;
|
GroupId: string;
|
MAX_QUEUE_LENGTH?: number;
|
modelConfig?: any;
|
}
|
declare class SpeechSynthesisUtil {
|
private API_KEY;
|
private GroupId;
|
private modelConfig;
|
private audioContext;
|
private audioQueue;
|
private audioTextQueue;
|
private state;
|
private fileManager;
|
private pendingAudioUrl;
|
private eventListeners;
|
private timer;
|
private modelContent;
|
private MAX_QUEUE_LENGTH;
|
private totalAudioCount;
|
constructor(options: SpeechSynthesisOptions);
|
/**
|
* 文字转语音方法
|
* @param text - 需要转换的文本
|
*/
|
textToSpeech(text: string): Promise<void>;
|
/**
|
* 请求语音合成
|
* @param text - 需要合成的文本
|
* @private
|
*/
|
private requestSynthesis;
|
/**
|
* 处理音频响应数据
|
* @param response - 响应数据
|
* @param text - 对应的文本内容
|
* @private
|
*/
|
private processAudioResponse;
|
/**
|
* 写入音频文件
|
* @param filePath - 文件路径
|
* @param buffer - 音频数据
|
* @private
|
*/
|
private writeAudioFile;
|
/**
|
* 开始播放音频队列
|
* @private
|
*/
|
private playAudioQueue;
|
/**
|
* 安全播放方法
|
* @private
|
*/
|
private safePlay;
|
/**
|
* 手动触发播放
|
*/
|
manualPlay(): Promise<void>;
|
/**
|
* 绑定音频事件
|
* @private
|
*/
|
private bindAudioEvents;
|
/**
|
* 处理音频播放开始事件
|
* @private
|
*/
|
private handleAudioPlay;
|
/**
|
* 处理音频播放结束事件
|
* @private
|
*/
|
private handleAudioEnd;
|
/**
|
* 处理音频错误事件
|
* @private
|
*/
|
private handleAudioError;
|
/**
|
* 销毁音频上下文
|
* @private
|
*/
|
private destroyAudioContext;
|
/**
|
* 设置状态
|
* @private
|
*/
|
private setState;
|
/**
|
* 获取当前状态
|
*/
|
getState(): State;
|
/**
|
* 重置所有状态
|
*/
|
reset(): void;
|
/**
|
* 添加事件监听
|
*/
|
on(event: EventType, callback: Function): void;
|
/**
|
* 移除事件监听
|
*/
|
off(event: EventType, callback: Function): void;
|
/**
|
* 触发事件
|
* @private
|
*/
|
private emit;
|
/**
|
* 开始计时
|
* @private
|
*/
|
private startTimer;
|
/**
|
* 结束计时并返回时(毫秒)
|
* @private
|
*/
|
private endTimer;
|
/**
|
* 批量处理文本
|
*/
|
processText(text: string): Promise<void>;
|
/**
|
* 处理文本分段
|
* @private
|
*/
|
private processContent;
|
/**
|
* 强制处理剩余文本
|
*/
|
flushRemainingText(): Promise<void>;
|
/**
|
* 重置文本处理状态
|
*/
|
resetTextProcessor(): void;
|
/**
|
* 暂停播放
|
*/
|
pause(): void;
|
/**
|
* 恢复播放
|
*/
|
resume(): void;
|
/**
|
* 切换播放/暂停状态
|
*/
|
togglePlay(): void;
|
/**
|
* 如果需要,开始播放音频
|
* @private
|
*/
|
private startPlayingIfNeeded;
|
/**
|
* 处理错误
|
* @private
|
*/
|
private handleError;
|
private calculateProgress;
|
/**
|
* 判断是否是合适的分段点
|
* @private
|
*/
|
private isGoodSplitPoint;
|
}
|
export default SpeechSynthesisUtil;
|