学习目标:
音频之WAV格式编码解析学习内容:
- 介绍
WAV是最常见的声音文件格式之一,wav文件分为两个部分,第一个部分是wav头文件,第二个部分是PCM编码的音频数据部分。是微软公司专门为Windows开发的一种标准数字音频文件,该文件能记录各种单声道或立体声的声音信息,并能保证声音不失真。但WAV文件有一个致命的缺点,就是它所占用的磁盘空间太大(每分钟的音乐大约需要12兆磁盘空间)。它符合资源互换文件格式(RIFF)规范,用于保存Windows平台的音频信息资源,被Windows平台及其应用程序所广泛支持。 - WAV格式
名称 | 占用字节数 | 计算公式 | 对应16进制 | 解释 |
---|---|---|---|---|
ChunkId | 4 | 固定 | 0x 52 49 46 46 | RIFF标准头部标识 |
ChunkSize | 4 | 4+(8+subchunk1Size)+(8+subchunk2Size) | 0x 00 64 0D 00 | 标识后面整个编码的大小 |
Format | 4 | 固定 | 0x 57 41 56 45 | WAVE标准头部标识 |
Subchunk1 ID | 4 | 固定为 fmt或者chunk | 0x 66 6D 74 20 | 可以为fmt或者chunk |
Subchunk1 Size | 4 | / | 0x 10 00 00 00 | / |
AudioFormat | 2 | / | 0x 01 00 | pcm=1(线性量化) |
NumChannels | 2 | / | 0x 02 00 | 声道数 |
SampleRate | 4 | / | 0x 44 AC 00 00 | 采样率(8000,44100等) |
ByteRate | 4 | sampleRate * NumChannels * BitsPerSample/8 | 0x 10 B1 02 00 | / |
BolckAlign | 2 | numChannels * BitsPerSample/8 | 0x 04 00 | / |
BitsPerSample | 2 | / | 0x 10 00 | 每个采样点的对应位数 |
Subchunk2 ID | 4 | 固定 | 0x 64 61 74 61 | “data”标志位 |
Subchunk2 Size | 4 | numSamples * numChannels * BitsPerSample(音频文件的长度) / 8 | 0x DC 63 0D 00 | pcm音频数据的长度字节数 |
data | / | / | 其他数据的部分 | pcm音频数据 |
注意:
上面表格中的除了固定的和data部分以外的
,我们在使用的时候都需要转换, 举个例子,比如我们想知道采样率
0x 44 AC 00 00
转换
0x 00 00 AC 44
= 44100
为什么会出现上面我们需要转化的情况,因为编码在存储的时候就需要进行一些操作,下面用伪代码进行演示:
//这样就是会实现按字节倒叙存储
header[24] = (byte) (SampleRate & 0xff);
header[25] = (byte) ((SampleRate >> 8) & 0xff);
header[26] = (byte) ((SampleRate >> 16) & 0xff);
header[27] = (byte) ((SampleRate >> 24) & 0xff);
理解了之后,我们就可以分析一段音频数据了,大概你会在心中有以下这么一张图。
图和表格结合看
所以添加 WAV头部 Java代码如下:
/**
* 添加wav文件头
*/
private void writeWaveFileHeader(long totalAudioLen,long totalDataLen, long longSampleRate, int channels, long byteRate){
byte[] header = new byte[44];
//RIFF/WAVE header
header[0] = 'R';
header[1] = 'I';
header[2] = 'F';
header[3] = 'F';
//
header[4] = (byte) (totalDataLen & 0xff);
header[5] = (byte) ((totalDataLen >> 8) & 0xff);
header[6] = (byte) ((totalDataLen >> 16) & 0xff);
header[7] = (byte) ((totalDataLen >> 24) & 0xff);
//WAVE
header[8] = 'W';
header[9] = 'A';
header[10] = 'V';
header[11] = 'E';
// fmt 或 chunk
header[12] = 'f';
header[13] = 'm';
header[14] = 't';
header[15] = ' ';
// 4 bytes: size of 'fmt ' chunk
header[16] = 16;
header[17] = 0;
header[18] = 0;
header[19] = 0;
// format = 1
header[20] = 1;
header[21] = 0;
header[22] = (byte) channels;
header[23] = 0;
//采样率
header[24] = (byte) (longSampleRate & 0xff);
header[25] = (byte) ((longSampleRate >> 8) & 0xff);
header[26] = (byte) ((longSampleRate >> 16) & 0xff);
header[27] = (byte) ((longSampleRate >> 24) & 0xff);
//
header[28] = (byte) (byteRate & 0xff);
header[29] = (byte) ((byteRate >> 8) & 0xff);
header[30] = (byte) ((byteRate >> 16) & 0xff);
header[31] = (byte) ((byteRate >> 24) & 0xff);
// block align
header[32] = (byte) (2 * 16 / 8);
header[33] = 0;
// bits per sample
header[34] = 16;
header[35] = 0;
//data
header[36] = 'd';
header[37] = 'a';
header[38] = 't';
header[39] = 'a';
//音频文件长度
header[40] = (byte) (totalAudioLen & 0xff);
header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
header[43] = (byte) ((totalAudioLen >> 24) & 0xff);
}
另外,我们的data部分的音频数据(pcm裸流数据)摆放的时候也有规则的,规则如下图
pcm = 8位:总的来看是左声道先,右声道后 .....
pcm = 16位:左声道低8位 左声道 高8位 右声道低8位 右声道高8位 ......
以上是个人拙见,欢迎指正,补充!