zhuker / lamejs

mp3 encoder in javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lamejs.WavHeader.readHeader - Uncaught (in promise) TypeError: Cannot read property 'dataOffset' of undefined

realrecordzLab opened this issue · comments

I'm trying to convert some blobs chunks that will be fetched from a remote source into mp3 using this library. The problem is that the lamejs.WavHeader.readHeader seems not working. In the custom chrome extension I'm making I have this code

const processChunks = async (blobChunks) => {
  console.log('chunk stream log', blobChunks);
  let audioBuffer = [];
  blobChunks.forEach( (chunk) => {
    let wav = lamejs.WavHeader.readHeader(new DataView(chunk));
    console.log(wav);
    let sample = new Int16Array(chunk, wav.dataOffset, wav.dataLen / 2);
    audioBuffer.push(sample);
  })

  sampleBlockSize = 1152;

  for(var i = 0; i < audioBuffer.length; i += sampleBlockSize){
    sampleChunk = audioBuffer.subarray(i, i + sampleBlockSize);
    let mp3buf = mp3encoder.encodeBuffer(sampleChunk);
    if(mp3buf.length > 0){
      mp3Data.push(mp3buf);
    }
  }
  // see if there's any data left
  let mp3buf = mp3encoder.flush();
  if(mp3buf.length > 0){
    mp3Data.push(mp3buf);
  }
  // console.log('processed stream as mp3', mp3Data);
  const output = new Blob([mp3Data], {type: 'audio/mp3'});
  const fileURL = URL.createObjectURL(output);
  chrome.downloads.download({
    saveAs: true,
    url: fileURL
  }, (downloadId) => {
    console.log(downloadId);
    URL.revokeObjectURL(fileURL);
  });
} 

But I have no success. Any help?

commented

Are you converting the blob to a arrayBuffer? Here only work with that.

 const buf = await blob.arrayBuffer();

Are you converting the blob to a arrayBuffer? Here only work with that.

 const buf = await blob.arrayBuffer();

No, I'm using arrayBuffer() directly on an ajax response then I pass it to the function posted above

commented

Are you shure that yours arraybuffer is a valid wav file?