mido
mido

Reputation: 25034

Conversion Error : ArrayBuffer to Int16Array

this should be straight forward, but I am not sure why I am getting the error, I am using constructor with ArrayBuffer as parameter as shown in mdn, but I am getting the error as invalid arguments, (p.s with dataview I have checked, the data is Int16 only)

the code is:

        var view= DataView(arrayBuf);
        console.log('arrayBuf.byteLength : '+arrayBuf.byteLength);
        console.log('data at 0 : '+view.getInt16(0));
        console.log('data at 1 : '+view.getInt16(1));
        var int16arry = new Int16Array(arrayBuf);

the console output is:

"arrayBuf.byteLength : 117" 
"data at 0 : 22720" 
"data at 1 : -16315"
Error: invalid arguments

what is my mistake?

Upvotes: 2

Views: 4386

Answers (1)

Snellface
Snellface

Reputation: 608

The short answer is that your arrayBuffer is in the wrong size. You can use:

var int16Array = new Int16Array(arrayBuf, 0, Math.floor(arrayBuf.byteLength / 2));

to hack away the problem.

Case specific comment:
I have tried reading the source for your library but i am unable to see why you are getting that extra byte (or what is missing).
The data you are getting is supposed to be 16 bit ints but for some reason you have other data there that take up an uneven amount of bytes, and according to the source as far as i can tell there should be some doubles (javascript floats) in there as well, meaning that "hacking" away the problem might not work.

Upvotes: 2

Related Questions