webRTC - record audio with RecordRTC and convert to mp3 using lamejs

2.5k views Asked by At

I am trying to record stream audio using recordRTC. But I would like to convert the output into mp3 in real time. For the conversion I came across lamejs. I found a couple of examples. But I don't know how to use them for what I want.

I get a blob

mediaRecorder = RecordRTC(webcamStream,{type: 'audio', mimeType: 'audio/mpeg-3',});
mediaRecorder.startRecording();

mediaRecorder.stopRecording(function() {
     let blob = mediaRecorder.getBlob();

}

lamejs example that I am trying to fix my needs into.

channels = 1; //1 for mono or 2 for stereo
sampleRate = 44100; //44.1khz (normal mp3 samplerate)
kbps = 128; //encode 128kbps mp3
mp3encoder = new lamejs.Mp3Encoder(channels, sampleRate, kbps);
var mp3Data = [];

samples = new Int16Array(44100); //one second of silence (get your data from the source you have)
sampleBlockSize = 1152; //can be anything but make it a multiple of 576 to make encoders life easier

var mp3Data = [];
for (var i = 0; i < samples.length; i += sampleBlockSize) {
  sampleChunk = samples.subarray(i, i + sampleBlockSize);
  var mp3buf = mp3encoder.encodeBuffer(sampleChunk);
  if (mp3buf.length > 0) {
      mp3Data.push(mp3buf);
  }
}
var mp3buf = mp3encoder.flush();   //finish writing mp3

if (mp3buf.length > 0) {
    mp3Data.push(new Int8Array(mp3buf));
}

var blob = new Blob(mp3Data, {type: 'audio/mp3'});
var url = window.URL.createObjectURL(blob);
console.log('MP3 URl: ', url);

How can I pass the recorded buffer to lamejs in realtime?

1

There are 1 answers

5
Newbie On

Posting the answer in case anyone needs it. But not that there is still some issue with output mp3 file. I will ask a new question for that. Below code will give you idea how to pass recordRTC buffer to lamejs. recordRTC has ondataavailable event. We can use to pass buffer data to lamejs.

var config = {
            recorderType: StereoAudioRecorder,
            mimeType: 'audio/wav',
            timeSlice: 1000, // concatenate intervals based blobs
            ondataavailable: function(e) {
                    mp3encoder = new lamejs.Mp3Encoder(2, 44100, 128); 
                    log(' data available')
                    const leftsamples = new Int16Array(e.data);   //for stereo
                    const rightsamples = new Int16Array(e.data);  //for stereo
                    let mp3Tmp = mp3encoder.encodeBuffer(leftsamples, rightsamples);
                    let fmp3Tmp = mp3encoder.flush();
                    let mp3Ct= fmp3Tmp.length;
                    if(mp3Ct>0) {
                        mp3Data.push(fmp3Tmp);
                    }
            }
        }
        mediaRecorder = RecordRTC(webcamStream,config);



mediaRecorder.stopRecording(function() {
            let fmp3Tmp = mp3encoder.flush();
            if (fmp3Tmp.length > 0) {
                mp3Data.push(new Int8Array(fmp3Tmp));
            }
           blob = new Blob(mp3Data, {type: 'audio/mp3'});
           invokeSaveAsDialog(blob); //download file
        });