How does this js code change a 32-bit data stream into a 16-bit data stream?

  /**
   *  32  16 
   * @param {ByteArray} chunk 
   */
  static bit32to16(chunk) {
    const b16 = new Int16Array(chunk.byteLength / 4);
    const dv = new DataView(chunk.buffer);
    for (let i = 0, offset = 0; offset < chunk.byteLength; iPP, offset += 4){
        const v = dv.getFloat32(offset, true);
        b16[i] = v > 0 ? v * 32767 : v * 32768;
    }
    return b16.buffer
  }
The

view is mainly used to manipulate binary data, which is relatively rare in general.
to explain that this method involves a lot of concepts, it is recommended to calm down and understand it when you have time. Use it directly if you don't have time, and you don't have to care about the specific implementation.
this is the information URL: ArrayBuffer introduction .

Menu