a2e-audio-manager
v0.0.5
Published
Real-time audio recording and communication with A2E server to obtain audio emotion data management library|实时音频录制与A2E服务器通信,获取声音表情数据的音频管理库
Maintainers
Readme
A2E Audio Manager
Real-time audio recording and communication with A2E server to obtain audio emotion data management library.
实时音频录制与A2E服务器通信,获取声音表情数据的音频管理库。
Features
- 🎤 Real-time audio recording and compression | 实时音频录制与压缩
- 🤖 Communication with A2E server to get audio emotions (BlendShape) | 与A2E服务器通信获取声音表情
- 📊 Audio power event monitoring | 音频功率事件监听
- 🔊 Audio playback and decoding | 音频播放与解码
- ⚙️ Flexible audio configuration options | 灵活的音频配置选项
Installation
npm install a2e-audio-managerQuick Start
import AudioManager from 'a2e-audio-manager';
// Create instance | 创建实例
const audioManager = new AudioManager('ws://localhost:6006');
// connect to websocket server | 连接服务器
// With channel and uid parameters | 使用频道和用户ID参数
try {
await audioManager.connect('channel-001', 'user-123');
console.log('连接建立成功');
await audioManager.startMic();
} catch (error) {
console.error('连接或录音启动失败:', error);
}
// Listen to events | 监听事件
audioManager.addEventListener(AudioManager.POWER_EVENT, (event) => {
console.log('Power | 功率:', (event as CustomEvent).detail.powerLevel);
});
audioManager.addEventListener(AudioManager.BLEND_SHAPE_EVENT, (event) => {
const { bs } = (event as CustomEvent).detail;
console.log('BlendShape:', bs);
});API Reference
Constructor
new AudioManager(serverUrl: string, audioOptions?: AudioConfigOptions)Parameters:
serverUrl: WebSocket server address (required) | WebSocket服务器地址(必填)audioOptions: Optional audio configuration | 可选的音频配置参数
AudioConfigOptions
interface AudioConfigOptions {
sampleRate?: number; // Default: 16000 | 默认:16000
bitRate?: number; // Default: 16 | 默认:16
frameThreshold?: number; // Default: 10 | 默认:10
silenceThreshold?: number; // Default: 1 | 默认:1
}Methods
| Method | Description | 描述 |
|--------|-------------|------|
| connect(channel?: string, uid?: string) | Initialize WebSocket connection (returns Promise) | 初始化WebSocket连接(返回Promise) |
| startMic() | Start recording | 开始录音 |
| stopMic() | Stop recording | 停止录音 |
| resetAudioServer() | Reset server | 重置服务器 |
| getAudioConfig() | Get current configuration | 获取当前配置 |
| dispose() | Dispose instance | 销毁实例 |
Properties
| Property | Type | Description | 描述 |
|----------|------|-------------|------|
| isConnected | boolean | WebSocket connection status | WebSocket连接状态 |
| isRecording | boolean | Recording status | 录音状态 |
Events
POWER_EVENT | 音频功率事件
detail: {
powerLevel: number; // Audio power value | 音频功率值
bufferSampleRate: number; // Buffer sample rate | 缓冲区采样率
timestamp: number; // Timestamp | 时间戳
}BLEND_SHAPE_EVENT | BlendShape数据事件
detail: {
bs: number[]; // BlendShape array | BlendShape数组
}Advanced Usage | 高级用法
Custom Configuration | 自定义配置
import AudioManager from 'a2e-audio-manager';
import type { AudioConfigOptions } from 'a2e-audio-manager';
const audioOptions: AudioConfigOptions = {
sampleRate: 44100, // Higher quality | 更高质量
bitRate: 16, // 16-bit depth | 16位深度
frameThreshold: 20, // Larger buffer | 更大缓冲区
silenceThreshold: 2 // More sensitive | 更敏感
};
const audioManager = new AudioManager('ws://localhost:6006', audioOptions);
// Connect with async/await | 使用async/await连接
try {
await audioManager.connect('my-channel', 'my-user-id');
console.log('连接成功');
} catch (error) {
console.error('连接失败:', error);
}
// Get current configuration | 获取当前配置
const config = audioManager.getAudioConfig();
console.log('Sample rate | 采样率:', config.sampleRate);Promise-based Connection | 基于Promise的连接
// Using .then()/.catch() | 使用.then()/.catch()
audioManager.connect('channel-001', 'user-123')
.then(() => {
console.log('连接建立成功');
return audioManager.startMic();
})
.then(() => {
console.log('录音开始成功');
})
.catch((error) => {
console.error('操作失败:', error);
});Configuration Guide | 配置指南
Audio Parameters | 音频参数
- sampleRate: Audio sample rate, affects quality and file size | 音频采样率,影响质量和文件大小
- bitRate: Audio bit depth, typically 16-bit | 音频位深度,通常为16位
- frameThreshold: Recording frame threshold, data sent after reaching this count | 录音帧数阈值,达到此数量后发送数据
- silenceThreshold: Silence detection threshold | 静音检测阈值
