# 📝 如何准备测试音频数据 ## 你需要提供的数据格式 ### PCM格式要求(重要!) ``` ✅ 采样率: 16000 Hz ✅ 位深度: 16 bit (有符号整数,Signed Integer) ✅ 声道数: 1 (单声道,Mono) ✅ 字节序: Little Endian (小端序) ✅ 格式: 纯PCM数据(无任何文件头,不是WAV) ``` ### 数据大小计算 ``` 数据大小(bytes)= 采样率 × 时长(秒)× 2 示例: - 1秒音频 = 16000 × 1 × 2 = 32,000 bytes ≈ 31.25 KB - 3秒音频 = 16000 × 3 × 2 = 96,000 bytes ≈ 93.75 KB - 5秒音频 = 16000 × 5 × 2 = 160,000 bytes ≈ 156.25 KB ``` ## 方式一:使用 FFmpeg 转换(推荐) ### 1. 从MP3/WAV/M4A等格式转换 ```bash ffmpeg -i input.mp3 -f s16le -acodec pcm_s16le -ar 16000 -ac 1 output.pcm ``` **参数说明:** - `-i input.mp3`: 输入文件(可以是任何音频格式) - `-f s16le`: 输出格式为16位小端序PCM - `-acodec pcm_s16le`: 使用16位PCM编码 - `-ar 16000`: 采样率16000 Hz - `-ac 1`: 单声道 ### 2. 验证生成的PCM文件 ```bash # 查看文件大小 ls -lh output.pcm # 计算时长(秒)= 文件大小(bytes)/ 32000 # 例如:96000 bytes / 32000 = 3 秒 ``` ### 3. 转换为base64 ```bash # Linux/Mac base64 output.pcm > output_base64.txt # 或者一行输出(适合小文件) base64 output.pcm | tr -d '\n' > output_base64.txt # Windows PowerShell [Convert]::ToBase64String([IO.File]::ReadAllBytes("output.pcm")) > output_base64.txt ``` ## 方式二:在线录音并导出 ### 1. 使用Audacity(免费开源) 1. 打开Audacity 2. 点击红色按钮录音 3. 录制3-5秒的测试语音(说点什么都可以) 4. 点击停止 5. **设置项目采样率**:左下角设置为 `16000 Hz` 6. **转换为单声道**:轨道 → 混音 → 混音立体声为单声道 7. **导出**: - 文件 → 导出 → 导出音频 - 文件类型选择:`其他未压缩文件` - 头部:`RAW (header-less)` - 编码:`Signed 16-bit PCM` - 保存为 `test.pcm` ### 2. 使用Python脚本录音 ```python import pyaudio import wave import struct # 配置 RATE = 16000 CHANNELS = 1 FORMAT = pyaudio.paInt16 RECORD_SECONDS = 3 # 录音 audio = pyaudio.PyAudio() stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=1024) print("录音中... (3秒)") frames = [] for i in range(0, int(RATE / 1024 * RECORD_SECONDS)): data = stream.read(1024) frames.append(data) print("录音完成") stream.stop_stream() stream.close() audio.terminate() # 保存为PCM with open('output.pcm', 'wb') as f: f.write(b''.join(frames)) print("已保存为 output.pcm") ``` ## 如何使用生成的数据 ### 选项A:使用base64(推荐,适合小文件) 1. **转换为base64**(见上面的命令) 2. **打开** `webUI/src/utils/config.js` 3. **填入base64数据**: ```javascript TEST_MODE: { enabled: true, testAudioBase64: 'AAEAAgADAAQABQAG...', // 👈 粘贴你的base64字符串 testAudioPath: '', } ``` **完整示例:** ```javascript TEST_MODE: { enabled: true, testAudioBase64: 'AAEAAgADAAQABQAGAAcACA...(很长的base64字符串)...==', testAudioPath: '', } ``` ### 选项B:使用文件路径(适合大文件) 1. **将PCM文件放入项目**: ``` webUI/src/static/test_audio.pcm ``` 2. **配置路径**: ```javascript TEST_MODE: { enabled: true, testAudioBase64: '', // 留空 testAudioPath: '/static/test_audio.pcm', // 👈 文件路径 } ``` ## 快速测试数据示例 ### 生成一个简单的测试文件(Python) ```python import struct # 生成3秒的简单正弦波(200Hz) sample_rate = 16000 duration = 3 frequency = 200 with open('test.pcm', 'wb') as f: for i in range(sample_rate * duration): t = i / sample_rate # 正弦波,振幅8000 sample = int(8000 * (2 * 3.14159 * frequency * t) ** 0.5) sample = max(-32768, min(32767, sample)) f.write(struct.pack('