I'm using a voice capture SDK that returns a pcm16Audio representation of the capture. I need to convert this to wav, then convert the wav to base64 to send to an API.
This is the function in the SDK that returns the pcm16Audio:
async getRecordedAudioPcm16Samples() {
let audio = new Int16Array(this.numRecordedSamples);
let offset = 0;
for (let i = 0; i < this.audioBuffers.length; i++) {
audio.set(this.audioBuffers[i], offset);
offset += this.audioBuffers[i].length;
}
return audio;
}
I believe the way to do this is to use 'audiobuffer-to-wav' from npm then convert the wav to base64:
import toWav from 'audiobuffer-to-wav';
const audio = sdk.getRecordedAudioPcm16Samples();
audio.then((pcm16Audio) => {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
audioContext.decodeAudioData(pcm16Audio.buffer, (buffer) => {
// encode AudioBuffer to WAV
const wav = toWav(buffer);
// convert wav to base64...
I get this error when calling decodeAudioData in Firefox: 'DOMException: The buffer passed to decodeAudioData contains an unknown content type.'
I get this error in Chrome: 'DOMException: Failed to execute 'decodeAudioData' on 'BaseAudioContext': Unable to decode audio data'
Am I on the right track? Thank you for any help!
decodeAudioData()is very limited. It only works with a few file types and support is very inconsistent across browsers. As far as I know there is no browser which decodes raw PCM data.But it can be done manually in a fairly straightforward way. An
AudioBufferwith asampleRateof 48kHz and a length of a second can for example be created like this:You can then copy the samples by filling a
Float32Arraywith values between -1 and 1. A value stored as int16 can usually be converted like this:The last step is to use the
Float32Arrayto copy the samples onto theAudioBuffer. Here is a full example for one second of audio stored asint16.You can then use that
audioBufferand convert it to a wav file with the package you mentioned above.