首页 > 解决方案 > 将 8kHz mulaw 转换为 PCM 16kHz

问题描述

我试图以 8kHz mulaw 接收来自 Twilio 的对话流,并且我想将其转换为 16kHz PCM 以进行某些处理(不支持 8kHz mulaw 格式),我尝试了这种方法但没有成功:
- 将字符串有效负载转换为 base64 缓冲区。
- 使用此包将缓冲区转换为 Uint8Array:buffer-to-uint8array
- 使用此 pacakge: alawmulaw 将 Uint8Array 转换为Int16Array
- 然后使用 wav 库编写结果。
按照这个过程,我仍然无法获得有效的音频文件,有人能告诉我我做错了什么吗?或指导我实现这一目标?

标签: node.jsaudiotwiliotwilio-twimltwilio-programmable-voice

解决方案


我很幸运使用 WaveFile 库(https://www.npmjs.com/package/wavefile

const wav = new WaveFile();
wav.fromScratch(1, 8000, '8m', Buffer.from(payload, "base64"));
wav.fromMuLaw();
// You can resample.
wav.toSampleRate(16000);
// You can write this straight to a file (will have the headers)
const results = wav.toBuffer();
// Or you can access the samples without the WAV header
const samples = wav.data.samples;

希望有帮助!


推荐阅读