Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
realrecordzLab opened this issue Jan 3, 2021 · 3 comments

Comments

@realrecordzLab
Copy link

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?

@kauly
Copy link

kauly commented Jan 8, 2021

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

 const buf = await blob.arrayBuffer();

@realrecordzLab
Copy link
Author

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

@kauly
Copy link

kauly commented Jan 9, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants