I'm a bit confused with Javascript Typed Arrays.
What I have are several Float32Array s, that have no concat method. I don't know how many are them in advance, btw. I'd like to concatenate them all inside another Float32Array, but:
var length_now = buffer.length;
for (var i = 0; i < event.frameBuffer.length; i += 1) {
buffer [length_now + i] = event.frameBuffer[i];
}
The only solution I found is to copy the Float32Array in a regular array, that's definitely not what I want. How would you do, stackoverflowers?
Typed arrays are based on array buffers, which cannot be resized dynamically, so writing past the end of the array or using push()
is not possible.
One way to achieve what you want would be to allocate a new Float32Array
, large enough to contain both arrays, and perform an optimized copy:
function Float32Concat(first, second)
{
var firstLength = first.length,
result = new Float32Array(firstLength + second.length);
result.set(first);
result.set(second, firstLength);
return result;
}
That would allow you to write:
buffer = Float32Concat(buffer, event.frameBuffer);
Or if you're trying to join N arrays:
// one-liner to sum the values in an array
function sum(a){
return a.reduce(function(a,b){return a+b;},0);
}
// call this with an array of Uint8Array objects
function bufjoin(bufs){
var lens=bufs.map(function(a){return a.length;});
var aout=new Uint8Array(sum(lens));
for (var i=0;i<bufs.length;++i){
var start=sum(lens.slice(0,i));
aout.set(bufs[i],start); // copy bufs[i] to aout at start position
}
return aout;
}
I had the same issue, you can add the following to the prototype
Float32Array.prototype.concat = function() {
var bytesPerIndex = 4,
buffers = Array.prototype.slice.call(arguments);
// add self
buffers.unshift(this);
buffers = buffers.map(function (item) {
if (item instanceof Float32Array) {
return item.buffer;
} else if (item instanceof ArrayBuffer) {
if (item.byteLength / bytesPerIndex % 1 !== 0) {
throw new Error('One of the ArrayBuffers is not from a Float32Array');
}
return item;
} else {
throw new Error('You can only concat Float32Array, or ArrayBuffers');
}
});
var concatenatedByteLength = buffers
.map(function (a) {return a.byteLength;})
.reduce(function (a,b) {return a + b;}, 0);
var concatenatedArray = new Float32Array(concatenatedByteLength / bytesPerIndex);
var offset = 0;
buffers.forEach(function (buffer, index) {
concatenatedArray.set(new Float32Array(buffer), offset);
offset += buffer.byteLength / bytesPerIndex;
});
return concatenatedArray;
};
now you can simply do
var array1 = new Float32Array(10000000),
array2 = new Float32Array(10000000);
var array3 = array1.concat(array2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With