I am using Recorder.js to record audio from mic. That library can encode the PCM data in WAV and I can successfully play it back using <audio>
. But, the resulting WAV data is too huge (~38MB for a 5 minute recording). I tried using libmp3lame.js available from Speech-to-Server.
In recorderWorker.js
, I am importing the Lame script:
importScripts(libmp3lame.js);
Then, I changed the exportWAV()
function to encode the PCM buffers into MP3 instead of WAV.
function exportWAV(type){
var bufferL = mergeBuffers(recBuffersL, recLength);
var bufferR = mergeBuffers(recBuffersR, recLength);
//var interleaved = interleave(bufferL, bufferR);
console.log(Start MP3 encoding);
var mp3codec = Lame.init();
Lame.set_mode(mp3codec, Lame.JOINT_STEREO);
Lame.set_num_channels(mp3codec, 2);
Lame.set_out_samplerate(mp3codec, sampleRate);
Lame.set_bitrate(mp3codec, 128);
Lame.init_params(mp3codec);
var mp3data = Lame.encode_buffer_ieee_float(mp3codec, bufferL, bufferR);
audioBlob = new Blob([mp3data.data], { type: audio/mp3 });
console.log(Done MP3 encoding);
this.postMessage(audioBlob);
}
But, the Lame.encode_buffer_ieee_float
method is throwing this error:
Uncaught RangeError: Invalid array buffer length
Here PCM data in bufferL
and bufferR
are Float32Array. I can not find out what exactly Lame.encode_buffer_ieee_float expects as input.
The offending line in Lame.encode_buffer_ieee_float
that is throwing the error is:
var arraybuf = new ArrayBuffer(nread);
I put a breakpoint and checked the value of nread
. It is -1.
So, my question is how do I use the Lame MP3 JavaScript library in this situation? Thank you.