wav格式文件是常见的录音文件,是声音波形文件格式之一,wav 文件由文件头和数据体两部分组成。

文件头是我们在做录音保存到文件的时候,要存储的文件的说明信息,播放器要通过文件头的相关信息去读取数据播放文件,下面是wav文件头的格式说明。

端模式

field name

Size

说明

big ChunkID 4 文件头标识,一般就是"RIFF" 四个字母
little ChunkSize 4 整个数据文件的大小,不包括上面ID和Size本身
big Format 4 一般就是"WAVE" 四个字母
big SubChunk1ID 4 格式说明块,本字段一般就是"fmt "
little SubChunk1Size 4 本数据块的大小,不包括ID和Size字段本身,这里是pcm 16
little AudioFormat 2 音频的格式说明,对于pcm 这里是1
little NumChannels 2 声道数 
little SampleRate 4 采样率
little ByteRate 4 比特率,每秒所需要的字节数
little BlockAlign 2 数据块对齐单元
little BitsPerSample 2 采样时模数转换的分辨率,采样位数
big SubChunk2ID 4 真正的声音数据块,本字段一般是"data"
little SubChunk2Size 4 本数据块的大小,不包括ID和Size字段本身
little Data N 音频的采样数据

下面贴上一段代码可对应着看

    private static byte[] getWaveFileHeader(int totalDataLen, int sampleRate, int channelCount, int bits) {
        byte[] header = new byte[44];
        // RIFF/WAVE header
        header[0] = 'R';
        header[1] = 'I';
        header[2] = 'F';
        header[3] = 'F';

        int fileLength = totalDataLen + 36;
        header[4] = (byte) (fileLength & 0xff);
        header[5] = (byte) (fileLength >> 8 & 0xff);
        header[6] = (byte) (fileLength >> 16 & 0xff);
        header[7] = (byte) (fileLength >> 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;

        // pcm format = 1
        header[20] = 1;
        header[21] = 0;
        header[22] = (byte) channelCount;
        header[23] = 0;

        header[24] = (byte) (sampleRate & 0xff);
        header[25] = (byte) (sampleRate >> 8 & 0xff);
        header[26] = (byte) (sampleRate >> 16 & 0xff);
        header[27] = (byte) (sampleRate >> 24 & 0xff);

        int byteRate = sampleRate * bits * channelCount / 8;
        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) (channelCount * bits / 8);
        header[33] = 0;
        // bits per sample
        header[34] = (byte) bits;
        header[35] = 0;
        //data
        header[36] = 'd';
        header[37] = 'a';
        header[38] = 't';
        header[39] = 'a';
        header[40] = (byte) (totalDataLen & 0xff);
        header[41] = (byte) (totalDataLen >> 8 & 0xff);
        header[42] = (byte) (totalDataLen >> 16 & 0xff);
        header[43] = (byte) (totalDataLen >> 24 & 0xff);
        return header;
    }

有了头信息,下面就是pcm数据了,

pcm数据也有一定的格式

1、8位单通道

数据块1 数据块2 ...
数据1 数据2 ...

2、8位双通道

数据块1 数据块2 ...
声道1数据1 声道2数据1 声道1数据2 声道2数据2 ...

3、16位单通道

数据块1 数据块2 ...
数据1低字节 数据1高字节 数据2低字节 数据2高字节 ...

4、16位双通道

数据块1 数据块2 ...
声道1低字节 声道1高字节 声道2低字节 声道2高字节 声道1低字节 声道1高字节 声道2低字节 声道2高字节 ...

pcm数据按照上面格式读取即可。

参考:

  https://www.cnblogs.com/liyiwen/archive/2010/04/19/1715715.html

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!