wav 文件格式介绍
The WAVE file format is a subset of Microsoft's RIFF specification for the storage of multimedia files. A RIFF file starts out with a file header followed by a sequence of data chunks. A WAVE file is often just a RIFF file with a single "WAVE" chunk which consists of two sub-chunks -- a "fmt " chunk specifying the data format and a "data" chunk containing the actual sample data. Call this form the "Canonical form".
RIFF文件一般以一个文件头开始,后面跟着一系列的数据。WAV文件一般是一个RIFF文件,外加一个包含两个子数据块(fmt 数据库和data数据库)的WAV数据块组成的。我们把这个形式作为规范形式。
wav文件格式数据组成
wav文件格式参数介绍
Offset | Size | Name | Description |
0 | 4 | ChunkID | Contains the letters "RIFF" in ASCII form(0x52494646 big-endian form). 包含 字符串"RIFF",二进制文件中表现为“52 49 46 46”“ |
4 | 4 | ChunkSize | 36 + SubChunk2Size, or more precisely:4 + (8 + SubChunk1Size) + (8 + SubChunk2Size) This is the size of the rest of the chunk following this number. This is the size of the entire file in bytes minus 8 bytes for the two fields not included in this count:ChunkID and ChunkSize. ChunkSize的计算方式 |
8 | 4 | Format | Contains the letters "WAVE"(0x57415645 big-endian form). 包含字符串WAV,二进制文件中表现为"57 41 56 45" |
12 | 4 | Subchunk1ID | Contains the letters "fmt "(0x666d7420 big-endian form). 包含字符串fmt,二进制文件中表现为"66 6d 74 20" |
16 | 4 | Subchunk1Size | 16 for PCM. This is the size of the rest of the Subchunk which follows this number. 固定值,为后续fmt子数据块数据的size |
20 | 4 | AudioFormat | PCM = 1 (i.e. Linear quantization)Values other than 1 indicate some form of compression. |
22 | 2 | NumChannels | Mono = 1, Stereo = 2, etc |
24 | 2 | SampleRate | 8000, 44100, etc. |
28 | 4 | ByteRate | == SampleRate * NumChannels * BitsPerSample/8 |
32 | 4 | BlockAlign | == NumChannels * BitsPerSample/8 The number of bytes for one sample including all channels. |
34 | 2 | BitsPerSample | 8 bits = 8, 16 bits = 16, etc. |
36 | 2 | Subchunk2ID | Contains the letters "data"(0x64617461 big-endian form). 包含字符串data,二进制文件中表现为"64 61 74 61" |
40 | 4 | Subchunk2Size | == NumSamples * NumChannels * BitsPerSample/8 This is the number of bytes in the data.You can also think of this as the size of the read of the subchunk following this number. 这个size实际就是pcm data的数据 |
44 | 4 | Data | The actual sound data |
Wav二进制文件分析
下面是一个72bytes的wav 文件
二进制数据
52 49 46 46 24 08 00 00 57 41 56 45 66 6d 74 20 10 00 00 00 01 00 02 00 22 56 00 00 88 58 01 00 04 00 10 00 64 61 74 61 00 08 00 00 00 00 00 00 24 17 1e f3 3c 13 3c 14 16 f9 18 f9 34 e7 23 a6 3c f2 24 f2 11 ce 1a 0d |
数据分析
可对比wav文件参数介绍部分内容。
需要介绍的是ChunkSize的计算方式。
我们可以看到二进制中ChunkSize表示为 “24 08 00 00”,在换算时应该列为 08 24 ->对应10进制就为图中的2048,相同方式可以得到SubChunk1Size以及SubChunk2Size.
PCM 数据格式文件转化为WAV 文件格式
根据前面WAV 格式文件分析,如果已经拿到了PCM的数据,想要将生成的数据保存为WAV 文件格式,只需根据WAV文件格式需求,为PCM加一个file header,加在数据头部即可。