libmp3lame-js icon indicating copy to clipboard operation
libmp3lame-js copied to clipboard

Uncaught RangeError: Invalid array buffer length

Open lightmar opened this issue 10 years ago • 12 comments

Hi,

Thank you for this great plugin. Unfortunately I have a problem which I can't figure out. When I encode to mp3 from microphone, on some computers it works great, however on others when I try to encode to I get the following error: Uncaught RangeError: Invalid array buffer length

Does anyone knows whats the reason and how solve it?

Thanks

lightmar avatar Aug 10 '14 14:08 lightmar

I have this problem too. The error message is caused by trying to create a new ArrayBuffer of size -1 just before the end of the code:

        var nread = Module.ccall('lame_encode_buffer_ieee_float', ...]);
        var arraybuf = new ArrayBuffer(nread);

I get the "Invalid array buffer length" because nread is -1. The transcompiled code is hard to follow, but so far I have learned the following:

  • lame_encode_buffer_ieee_float calls _lame_encode_buffer_template
  • _lame_encode_buffer_template is a knotty little state machine, it uses a switch statement as a sort of JMP or GOTO.
  • case 53 of this switch calls _lame_encode_mp3_frame, which works for a bunch of frames, returning a positive integer, and then at some point returns -1, which causes case 60 to return -1 to nread.
  • _lame_encode_mp3_frame gets this -1 as a return value from _copy_buffer
  • I think this is from case 2 of the state machine in _copy_buffer, not entirely clear to me.

Here's a paste of the function giving that -1.. Will comment again if I get it working for me.

edit: here's a trace after instrumenting it a bit.

awarenetdev avatar Aug 13 '14 01:08 awarenetdev

@lightmar: I have a solution which works for me. I could not get the copy of libmp3lame.js in this repo working for recordings longer than 8 seconds, I suspect this is because of something bad in my input, am sending mono and the number of samples seems to be wrong.

Anyway, this copy works for me, found at: http://audior.ec/recordmp3js/, via this tutorial.

Swapping it in, I no longer experience the issue you describe.

awarenetdev avatar Aug 13 '14 04:08 awarenetdev

Thank you, however I still can't make it work. I just came back to try again after I left it for few months. I have discovered that the error occurs only when a user records microphone with an high sample rate of 192000. When I try it on my mac with 44000 input, it works.

How can I make it work? or Maybe reduce the sample rate somehow?

lightmar avatar Dec 01 '14 15:12 lightmar

I'm having problems also on windows computers, which, as @lightmar has stated, have sample rates of 192000 instead of 44000.

Any approach with this?

BondaCB avatar Dec 02 '14 15:12 BondaCB

I have tried changing the Lame config like this:

Lame.set_in_samplerate(mp3codec, 44100); Lame.set_out_samplerate(mp3codec, 44100); Lame.set_bitrate(mp3codec, 128);

And it did worked, but the problem is that the sound recording was really bad, it sounds something like roaring lions....

lightmar avatar Dec 08 '14 16:12 lightmar

Can anyone point me to how I can increase the length of time I can record..? I suspect it deals with increasing the bufferlength, etc..

Any help is greatly appreciated. Thanks!

chezbro avatar Dec 08 '14 16:12 chezbro

@lightmar , Is this solution helpful?

"Lame.set_in_samplerate(mp3codec, 44100); Lame.set_out_samplerate(mp3codec, 44100); Lame.set_bitrate(mp3codec, 128);"

I am not trying this yet as it works beautifully on linux but has suddenly stopped working on Windows, I'll take your word on it.

abhilashLenka avatar Apr 09 '15 07:04 abhilashLenka

@lightmar I should have taken you seriously on the roaring lions thing, Any idea how to fix it??

abhilashLenka avatar Apr 09 '15 07:04 abhilashLenka

We run into the same issue. Since it has been a while, may be someone already found the solution ? really appreciate you help.

ybelova avatar Feb 05 '16 17:02 ybelova

@lightmar i also get the "roaring lions", did you manage to solve this problem?

michelboudali avatar Aug 29 '16 14:08 michelboudali

@michelboudali, we did not find solution per say, but figured what causing that. On Windows: Try to go Control Panel -> Sound ->Payback-> Advanced and try to change sample rate. We get "Invalid array buffer length" with certain settings and don't get with others.

We do catch that issue by adding onerror handler to worker:

encoderWorker.onerror = function (err) {
    var errorText = 'Sorry, We are having some technical <br/>difficulties to convert your recorder <br/>sound to mp3 format and save.';
    console.log('worker is suffering!', err)
    document.getElementById('recorder_error').innerHTML = errorText;
}

ybelov avatar Sep 07 '16 18:09 ybelov

I can share files with you if you are interested.

ybelov avatar Sep 07 '16 18:09 ybelov