xfyun-sdk
Version:
科大讯飞语音识别 SDK,支持浏览器中实时语音听写功能
78 lines (77 loc) • 2.5 kB
TypeScript
import { Logger } from './logger';
/**
* 判断是否在浏览器环境
*/
export declare function isBrowser(): boolean;
/**
* 将字符串转换为 Base64 (浏览器/Node.js 兼容)
* @remarks
* 此函数处理 Unicode 字符,确保在不同环境下都能正确编码
*/
export declare function toBase64(str: string, encoding?: 'utf-8' | 'binary'): string;
/**
* 生成科大讯飞API请求URL
* @param apiKey 接口密钥
* @param apiSecret 接口密钥对应的secret
* @param host 请求的服务器地址
* @param path API 路径,默认 /v2/iat
* @returns 带有签名的完整URL
*/
export declare function generateAuthUrl(apiKey: string, apiSecret: string, host?: string, path?: string): string;
/**
* 检测浏览器是否支持 MediaRecorder API
* @returns 支持的 MIME 类型字符串
*/
export declare function detectSupportedMimeType(): string;
/**
* 创建 AudioContext(兼容 webkit 前缀)
* @param sampleRate 可选采样率
* @returns AudioContext 实例
*
* @warning ⚠️ 重要资源管理提示
*
* AudioContext 是重要的浏览器资源,调用方必须在不再使用时显式调用 `close()` 方法:
*
* ```typescript
* const audioContext = createAudioContext(16000);
*
* // ... 使用 audioContext ...
*
* // 使用完毕后务必调用
* audioContext.close();
*
* // 最佳实践:在组件销毁或清理时调用
* function cleanup() {
* if (audioContext) {
* audioContext.close();
* audioContext = null;
* }
* }
* ```
*
* 未能正确关闭 AudioContext 可能导致:
* - 浏览器音频设备无法释放
* - 内存泄漏
* - 其他音频应用无法使用音频设备
*
* 在 xfyun-sdk 中,所有使用 createAudioContext 的类都在 `destroy()` 方法中正确调用了 `close()`。
*/
export declare function createAudioContext(sampleRate?: number): AudioContext;
/**
* 计算音频音量
* @param array 音频数据
* @returns 音量值
*/
export declare function calculateVolume(array: Float32Array): number;
/**
* Convert ArrayBuffer to Base64 string (handles large buffers)
* @param buffer - ArrayBuffer data
* @returns Base64 string
*/
export declare function arrayBufferToBase64(buffer: ArrayBuffer): string;
/**
* 解析科大讯飞返回的结果
* @param result 科大讯飞返回的识别结果
* @param logger 可选的日志记录器,若不提供则使用 console.error
*/
export declare function parseXfyunResult(result: unknown, logger?: Logger): string;