将 8kHz mulaw 转换为 PCM 16kHz [英] Converting 8kHz mulaw to PCM 16kHz

查看:45
本文介绍了将 8kHz mulaw 转换为 PCM 16kHz的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im trying to receive a conversation streaming from Twilio in 8kHz mulaw and I want to convert it to 16kHz PCM for some processing ( that doesnt support 8kHz mulaw format), I tried this method but without success :
- convert the string payload to base64 buffer.
- convert the buffer to Uint8Array with this package: buffer-to-uint8array.
- convert the Uint8Array to Int16Array with this pacakge: alawmulaw.
- then use wav library to write the results.
I am still unable to get a valid audio file following this process, Can someone tell me what i am doing wrong ? or guide me to achieve this ?

推荐答案

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

I've had good luck using the WaveFile lib (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;

希望有帮助!

这篇关于将 8kHz mulaw 转换为 PCM 16kHz的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆