1

I need to loop over a binary file via an arrayBufferand retrieve sets of 1024 floating points. I'm doing this:

// chunk_size = 1024
// chunk_len = 48
// response_buffer = ArrayBuffer
// response_buffer.byteLength = 49152
for (i = chunk_len; i > 0; i -= 1) {
    switch (i) {
        case (chunk_len):

            // create view from last chunk of 1024 out of 49152 total
            float_buffer = new Float32Array(response_buffer, ((i - 1) * chunk_size));

            // add_data(net_len, float_buffer);
            break;

        case 0:
            break;

        default:
            float_buffer = new Float32Array(response_buffer, ((i - 1) * chunk_size)), chunk_size);
            //add_data(net_len, float_buffer);
            break;
    }
}

My problem is if I call this on the first run for the end of my buffer:

// i - 1 = 47 * chunk_size
new Float32Array(response_buffer, ((i - 1) * chunk_size));

the same statement fails on the next run where I call:

new Float32Array(response_buffer, ((i - 1) * chunk_size), 1024);

Although I can read here, that I can do this:

Float32Array Float32Array(
    ArrayBuffer buffer,
    optional unsigned long byteOffset,
    optional unsigned long length
);

Question:
Why is my loop failing after declaring the first Float32Array view on my response_offer ArrayBuffer?

frequent
  • 27,643
  • 59
  • 181
  • 333

2 Answers2

1

I think you have an extra ")" in the first line of your "default" case.

float_buffer = new Float32Array(response_buffer, ((i - 1) * chunk_size)), chunk_size);

Should be:

float_buffer = new Float32Array(response_buffer, ((i - 1) * chunk_size), chunk_size);
JVNick
  • 92
  • 4
0

So. Finally understand... maybe this helps the next person wondering:

First off - I was all wrong in trying to read my data, which is 4 byte single format.

  • If I have an arrayBuffer with byteLength = 49182 this means there are that many entries in my array.
  • Since my array is 4 byte single format, I found out with some SO-help and searching that this is readable with getFloat32 AND that 4 entries comprise one "real" value
  • My data contains 3 measurements a 4000 data points stored in units of 1024 column by column.
  • So if I have 12000 data-points and 49182/4 = 12288 datapoints, I will have 288 empty data points at the end of my data structure.

  • So my binary data should be stored like this:

          0 -  1024    a
       1025 -  2048    a
       2049 -  3072    a
       3072 -  4000   [a
       4001 -  4096    b]
       4097 -  5120    b
       5121 -  6144    b
       6145 -  7168    b
       7169 -  8000   [b
       8001 -  8192    c]
       8193 -  9216    c
       9217 - 10240    c
      10240 - 11264    c
      11264 - 12000   [c
      12000 - 12288    0]
    
  • My final snippet will contain 288 empty results, because 3x4000 datapoints in 1024 chunks will return some empty results

  • To read, I found a nice snippet here (dynamic high range rendering), which helped me to this:

      // ...
      raw_data = ...
      data = new DataView(raw_data);
    
      ...
      tmp_data = new Float32Array(byte_len / Float32Array.BYTES_PER_ELEMENT);
      len = tmp_data.length;
    
      // Incoming data is raw floating point values with little-endian byte ordering.
      for (i = 0; i < len; i += 1) {
        tmp_data[i] = data.getFloat32(i * Float32Array.BYTES_PER_ELEMENT, true);
      }
    
  • Now I have a single array with which I can work and build my processing structure.

Community
  • 1
  • 1
frequent
  • 27,643
  • 59
  • 181
  • 333