@tslfe/ai-sdk
v1.0.11
Published
tsl ai sdk
Readme
@tslfe/ai-sdk
一个功能强大的 AI 语音交互 SDK,支持语音录制、语音识别、语音合成、语音唤醒和智能对话等功能。
特性
- 🎤 语音录制 - 支持实时录音和音频处理
- 🗣️ 语音识别 - 集成多种 ASR 引擎,支持实时语音转文字
- 🔊 语音合成 - 支持多种 TTS 引擎,包括字节跳动、阿里云等
- 🎯 语音唤醒 - 基于 ONNX 模型的离线语音唤醒
- 🤖 智能对话 - 集成多种 LLM 模型,支持流式对话
- 📊 数据可视化 - 内置图表展示功能
- 🎨 音频可视化 - 支持波形图、频谱图等多种可视化效果
安装
npm install @tslfe/ai-sdk快速开始
基础用法
import { createllm, createAudioRecorder } from "@tslfe/ai-sdk";
// 创建LLM实例
const llm = createllm({
application: "your-app-name",
recordConfig: {
type: "sentence",
waveView: {
enable: true,
el: "#wave-container"
}
},
tts: {
enable: true,
type: "bytedance"
}
});
// 监听事件
llm.on("data", (data) => {
console.log("收到数据:", data);
});
llm.on("connected", () => {
console.log("连接成功");
});语音录制
import { createAudioRecorder } from "@tslfe/ai-sdk";
const recorder = createAudioRecorder({});
// 初始化录音
recorder.init(
() => console.log("录音初始化成功"),
(error) => console.error("录音初始化失败:", error)
);
// 开始录音
recorder.start();
// 监听录音事件
recorder.on("audio", ({ text, type }) => {
console.log("音频数据:", text);
});语音唤醒
// 初始化语音唤醒
recorder.initWakeup();
// 监听唤醒事件
recorder.on("wakeup", (isWakeup) => {
if (isWakeup) {
console.log("语音唤醒成功");
}
});API 文档
createllm(config)
创建 LLM 实例,用于智能对话和语音交互。
参数
config.application(string) - 应用名称,必填config.recordConfig(AudioRecordConfig) - 录音配置config.clientId(string) - 客户端 ID,可选config.notifications(string[]) - 通知配置config.tts(object) - 语音合成配置config.wakeup(object) - 语音唤醒配置
返回值
返回 LLMInstance 实例,包含以下方法:
inputMessage(text, type)- 发送文本消息on(event, callback)- 监听事件off(event, callback)- 取消监听close()- 关闭连接
createAudioRecorder(config)
创建音频录制实例。
参数
config.type('sentence' | 'live') - 录音类型config.mode('normal' | 'wakeup') - 录音模式config.waveView(object) - 波形可视化配置config.powerLevelLimit(number) - 音量阈值config.remainTime(number) - 停顿时间
返回值
返回 AudioInstance 实例,包含以下方法:
init(success, fail)- 初始化录音start()- 开始录音stop()- 停止录音close()- 关闭录音on(event, callback)- 监听事件
事件系统
LLM 事件
connected- WebSocket 连接成功data- 接收到数据progress- 处理进度close- 连接关闭
录音事件
start- 开始录音stop- 停止录音audio- 音频数据wakeup- 语音唤醒结果error- 错误事件
配置选项
录音配置 (AudioRecordConfig)
interface AudioRecordConfig {
mode?: "normal" | "wakeup";
type?: "sentence" | "live";
waveView?: {
el?: string;
enable?: boolean;
type?: "FrequencyHistogramView" | "WaveView" | "WaveSurferView";
options?: any;
};
powerLevelLimit?: number;
remainTime?: number;
stopTime?: number;
maximumTime?: number;
}LLM 配置 (LLMConfig)
interface LLMConfig {
recordConfig: AudioRecordConfig;
application: string;
clientId?: string;
notifications?: string[];
autoInitRecorder?: boolean;
notify: 0 | 1;
tts?: {
enable?: boolean;
type?: string;
params?: object;
};
}支持的 TTS 引擎
- 字节跳动 TTS
- 阿里云 TTS
- 自定义 TTS 引擎
开发
安装依赖
yarn install开发模式
yarn serve构建
yarn compile