mstf-kit
Version:
一个现代化的 JavaScript/TypeScript 工具库,提供了丰富的常用工具函数
138 lines (137 loc) • 4.06 kB
TypeScript
/**
* 新版流式音频处理器 V2
* 支持有头部和无头部两种流式音频格式
* 参考 audioRequest 的处理方式,提供更灵活的配置
*/
/**
* 音频数据格式类型
*/
export type AudioDataFormat = 'with-header' | 'without-header' | 'auto';
/**
* 流式音频处理选项
*/
export interface StreamAudioV2Options {
/** 是否自动播放音频 */
autoPlay?: boolean;
/** 是否返回完整的Blob */
returnBlob?: boolean;
/** MIME类型 */
mimeType?: string;
/** 音频数据字段路径,例如'data.audio'或'audio' */
audioField?: string;
/**
* 音频数据格式
* - 'with-header': 每个数据块都包含完整的音频头部(如WAV头)
* - 'without-header': 只有第一个数据块包含头部,后续只有音频数据
* - 'auto': 自动检测(默认)
*/
dataFormat?: AudioDataFormat;
/** 是否启用调试日志 */
debug?: boolean;
/** 收到音频数据回调 */
onAudioData?: (audioBlob: Blob) => void;
/** 错误回调 */
onError?: (error: Error) => void;
/** 完成回调 */
onComplete?: (blob?: Blob) => void;
/** 音频开始播放回调 */
onStart?: () => void;
/** 音频播放结束回调 */
onEnded?: () => void;
/** 原始数据块回调 */
onRawChunk?: (chunk: string) => void;
}
/**
* 音频控制接口
*/
export interface StreamAudioV2Control {
/** 暂停播放 */
pause: () => void;
/** 恢复播放 */
resume: () => void;
/** 中止处理 */
abort: () => void;
/** 是否正在播放 */
isPlaying: () => boolean;
/** 完整音频Blob (如果returnBlob为true) */
finalBlob?: Promise<Blob>;
}
/**
* 处理流式音频响应 V2
*
* @param reader 响应流的Reader对象
* @param options 处理选项
* @returns 控制对象
*
* @example
* ```typescript
* // 处理有头部的流式音频
* const response = await fetch('/api/audio/stream');
* const reader = response.body.getReader();
*
* const { pause, resume, abort, isPlaying } = await processStreamAudioV2(reader, {
* autoPlay: true,
* dataFormat: 'with-header', // 每个块都有头部
* audioField: 'data.audio',
* onAudioData: (blob) => {
* console.log('收到音频块:', blob.size);
* }
* });
*
* // 处理无头部的流式音频
* const { pause, resume, abort } = await processStreamAudioV2(reader, {
* autoPlay: true,
* dataFormat: 'without-header', // 只有第一个块有头部
* onStart: () => console.log('开始播放'),
* onEnded: () => console.log('播放结束')
* });
*
* // 自动检测格式
* const control = await processStreamAudioV2(reader, {
* autoPlay: true,
* dataFormat: 'auto', // 自动检测
* debug: true
* });
* ```
*/
export declare function processStreamAudioV2(reader: ReadableStreamDefaultReader<Uint8Array>, options?: StreamAudioV2Options): Promise<StreamAudioV2Control>;
/**
* 创建 Axios 配置用于流式音频请求 V2
*
* @param options 处理选项
* @returns axios配置和控制对象
*
* @example
* ```typescript
* import axios from 'axios';
* import { createAxiosAudioOptionsV2 } from 'mstf-kit';
*
* const { axiosConfig, pause, resume, abort } = createAxiosAudioOptionsV2({
* autoPlay: true,
* dataFormat: 'with-header',
* audioField: 'data.audio',
* onStart: () => console.log('开始播放')
* });
*
* const response = await axios.post('/api/audio/stream', data, axiosConfig);
* ```
*/
export declare function createAxiosAudioOptionsV2(options?: StreamAudioV2Options): {
axiosConfig: {
responseType: string;
onDownloadProgress: (progressEvent: any) => void;
};
pause: () => void;
resume: () => void;
abort: () => void;
isPlaying: () => boolean;
finalBlob?: Promise<Blob>;
};
/**
* 导出默认对象
*/
declare const _default: {
processStreamAudioV2: typeof processStreamAudioV2;
createAxiosAudioOptionsV2: typeof createAxiosAudioOptionsV2;
};
export default _default;