Don McCurdy
Don McCurdy

Reputation: 12000

"RangeError: Invalid typed array length" for seemingly-valid inputs

I have the following snippet:

new Uint16Array( arraybuffer, 0, 18108 );

I know that arraybuffer is an instance of ArrayBuffer, and that arraybuffer.byteLength is 31984. The content of the arraybuffer is a black box to me. Because the buffer's byteLength is > 18108, I expect this to just work. Instead, I get the following errors:

Chrome:

RangeError: Invalid typed array length

Firefox:

TypeError: invalid arguments

What might cause this to fail, or how can I inspect an ArrayBuffer I can't open?

Upvotes: 11

Views: 14786

Answers (4)

Jasperan
Jasperan

Reputation: 3895

I got this error. In my case, I was passing a null file to a library function. Make sure that you're not dealing with a null case if you're getting this error for seemingly no reason.

Upvotes: 1

A_____ A_______
A_____ A_______

Reputation: 376

I also got same problem when was trying to open 200 files and get some data from their ArrayBuffer parts. 20 files always cause error RangeError: Invalid typed array length: 262144 so i solved it using ArrayBuffer.slice(startByte, endByte) So this

new Int16Array( arraybuffer, byteOffset, byteLength/2 )

changed to this

new Int16Array( arraybuffer.slice( byteOffset, byteOffset + byteLength ) )

but slice creates new ArrayBuffer so u can't change arraybuffer by which this array was created

Upvotes: 0

jecs89
jecs89

Reputation: 49

I got this error recently, so I had used ffmpeg to create wavfiles so I supposed this was the right format. Then, you can try open with Python librosa or scipy and check, my problem was ffmpeg produced wav files but mp3layer format, and I needed to transform to pcm format.

Upvotes: 0

Don McCurdy
Don McCurdy

Reputation: 12000

Well, I misunderstood the TypedArray / Uint16Array constructor. The second argument is a byteOffset, but the third argument is not byte length: It is length in elements.

From TypedArray docs:

length

When called with a length argument, an internal array buffer is created in memory of size length multiplied by BYTES_PER_ELEMENT bytes containing 0 value.

Since Uint16Array.BYTES_PER_ELEMENT is 2, the arraybuffer would need to be at least 2 * 18108 bytes long, which it is not.

Upvotes: 15

Related Questions