鍍金池/ 問答/Linux  HTML/ 請問這段js代碼是怎么把32位的數(shù)據(jù)流變成16位的數(shù)據(jù)流的

請問這段js代碼是怎么把32位的數(shù)據(jù)流變成16位的數(shù)據(jù)流的

  /**
   * 將 32 位的數(shù)據(jù)轉(zhuǎn)為 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; i++, offset += 4){
        const v = dv.getFloat32(offset, true);
        b16[i] = v > 0 ? v * 32767 : v * 32768;
    }
    return b16.buffer
  }
回答
編輯回答
別逞強

視圖主要用來操作二進制數(shù)據(jù),在平常比較少見。
要說明這個方法會涉及很多概念,有時間建議靜下心了解下。沒時間就直接用,不必在乎具體實現(xiàn)。
這是資料網(wǎng)址:ArrayBuffer 介紹。

2018年6月20日 23:46