I have this code from the lamejs example:
let channels = 1; //1 for mono or 2 for stereo
let sampleRate = 44100; //44.1khz (normal mp3 samplerate)
let kbps = 128; //encode 128kbps mp3
let mp3encoder = new lamejs.Mp3Encoder(channels, sampleRate, kbps);
let mp3Data;
********** this line is stumping me: **********
let samples = new Int16Array(44100); //one second of silence (get your data from the source you have)
let sampleBlockSize = 1152; //can be anything but make it a multiple of 576 to make encoders life easier
let mp3buf;
mp3Data = [];
for (let i = 0; i < samples.length; i += sampleBlockSize) {
let sampleChunk = samples.subarray(i, i + sampleBlockSize);
mp3buf = mp3encoder.encodeBuffer(sampleChunk);
if (mp3buf.length > 0) {
mp3Data.push(mp3buf);
}
}
mp3buf = mp3encoder.flush(); //finish writing mp3
if (mp3buf.length > 0) {
mp3Data.push(new Int8Array(mp3buf));
}
let audioBlob = new Blob(mp3Data, { type: "audio/mp3" });
I am not sure how to add my own source/samples: I am using the browser mediarecorder:
mediaRecorder.ondataavailable = (e) => {
chunks.push(e.data);
};
I have tried to use chunks
as the source/samples but I am not knowledgable enough about audio in general so can anyone help me on how to format the data and the underlying reason? I am just so lost as to where to start. Thanks in advance.