wav格式文件、pcm数据

本文详细介绍了WAV文件格式的结构,包括文件头的具体组成部分及其作用,并提供了生成WAV文件头的示例代码。此外,还讲解了不同位深及声道数的PCM数据组织方式。

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

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

端模式

field name

Size

说明

bigChunkID4文件头标识,一般就是"RIFF" 四个字母
littleChunkSize4整个数据文件的大小,不包括上面ID和Size本身
bigFormat4一般就是"WAVE" 四个字母
bigSubChunk1ID4格式说明块,本字段一般就是"fmt "
littleSubChunk1Size4本数据块的大小,不包括ID和Size字段本身,这里是pcm 16
littleAudioFormat2音频的格式说明,对于pcm 这里是1
littleNumChannels2声道数 
littleSampleRate4采样率
littleByteRate4比特率,每秒所需要的字节数
littleBlockAlign2数据块对齐单元
littleBitsPerSample2采样时模数转换的分辨率,采样位数
bigSubChunk2ID4真正的声音数据块,本字段一般是"data"
littleSubChunk2Size4本数据块的大小,不包括ID和Size字段本身
littleDataN音频的采样数据

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

    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

转载于:https://www.cnblogs.com/sskbskdrin/p/10648585.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值