在使用UInt8时似乎没有什么可以处理字节序。例如,在处理UInt16时,您可以设置是否需要小端或大端:
dataview.setUint16(byteOffset, value [, littleEndian])
VS
dataview.setUint8(byteOffset, value)
我想这是因为endianness正在处理字节的顺序,如果我一次插入一个字节,那么我需要自己订购它们。
那么我如何自己处理字节序呢?我正在使用此规范创建WAVE文件头:http://soundfile.sapp.org/doc/WaveFormat/
标题的第一部分是big endian中的“ChunkID”,这就是我的工作方式:
dataView.setUint8(0, 'R'.charCodeAt());
dataView.setUint8(1, 'I'.charCodeAt());
dataView.setUint8(2, 'F'.charCodeAt());
dataView.setUint8(3, 'F'.charCodeAt());
标题的第二部分是小端的“ChunkSize”,这就是我的方法:
dataView.setUint8(4, 172);
现在我想,由于那些块的字节序不同,所以我应该在每个块中做一些不同的事情。在这两个实例中我应该做些什么?
干杯!
编辑
我问这个问题,因为我创建的wav文件是无效的(根据https://indiehd.com/auxiliary/flac-validator/)。我怀疑这是因为我没有正确处理字节序。这是完整的波形文件:
const fs = require('fs');
const BITS_PER_BYTE = 8;
const BITS_PER_SAMPLE = 8;
const SAMPLE_RATE = 44100;
const NB_CHANNELS = 2;
const SUB_CHUNK_2_SIZE = 128;
const chunkSize = 36 + SUB_CHUNK_2_SIZE;
const blockAlign = NB_CHANNELS * (BITS_PER_SAMPLE / BITS_PER_BYTE);
const byteRate = SAMPLE_RATE * blockAlign;
const arrayBuffer = new ArrayBuffer(chunkSize + 8)
const dataView = new DataView(arrayBuffer);
// The RIFF chunk descriptor
// ChunkID
dataView.setUint8(0, 'R'.charCodeAt());
dataView.setUint8(1, 'I'.charCodeAt());
dataView.setUint8(2, 'F'.charCodeAt());
dataView.setUint8(3, 'F'.charCodeAt());
// ChunkSize
dataView.setUint8(4, chunkSize);
// Format
dataView.setUint8(8, 'W'.charCodeAt());
dataView.setUint8(9, 'A'.charCodeAt());
dataView.setUint8(10, 'V'.charCodeAt());
dataView.setUint8(11, 'E'.charCodeAt());
// The fmt sub-chunk
// Subchunk1ID
dataView.setUint8(12, 'f'.charCodeAt());
dataView.setUint8(13, 'm'.charCodeAt());
dataView.setUint8(14, 't'.charCodeAt());
// Subchunk1Size
dataView.setUint8(16, 16);
// AudioFormat
dataView.setUint8(20, 1);
// NumChannels
dataView.setUint8(22, NB_CHANNELS);
// SampleRate
dataView.setUint8(24, ((SAMPLE_RATE >> 8) & 255));
dataView.setUint8(25, SAMPLE_RATE & 255);
// ByteRate
dataView.setUint8(28, ((byteRate >> 8) & 255));
dataView.setUint8(29, byteRate & 255);
// BlockAlign
dataView.setUint8(32, blockAlign);
// BitsPerSample
dataView.setUint8(34, BITS_PER_SAMPLE);
// The data sub-chunk
// Subchunk2ID
dataView.setUint8(36, 'd'.charCodeAt());
dataView.setUint8(37, 'a'.charCodeAt());
dataView.setUint8(38, 't'.charCodeAt());
dataView.setUint8(39, 'a'.charCodeAt());
// Subchunk2Size
dataView.setUint8(40, SUB_CHUNK_2_SIZE);
// Data
for (let i = 0; i < SUB_CHUNK_2_SIZE; i++) {
dataView.setUint8(i + 44, i);
}
单个字节(uint8
)没有任何字节序,字节序是字节序列的属性。
根据您链接的规范,ChunkSize占用4个字节的空间 - 首先是最低有效字节(小端)。如果您的值只有一个字节(不大于255),那么您只需将偏移量为4的字节写入。如果4个字节是大端序,则必须在偏移量7处写入字节。
但我建议只使用setUint32
:
dataView.setUint32(0, 0x52494646, false); // RIFF
dataView.setUint32(4, 172 , true);
dataView.setUint32(8, 0x57415645, false) // WAVE