Reputation: 48
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 ?
Upvotes: 0
Views: 4714
Reputation: 114
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;
Hope that helps!
Upvotes: 8