zoukankan      html  css  js  c++  java
  • wav格式文件、pcm数据

    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

  • 相关阅读:
    Mysql有什么办法批量去掉某个字段字符中的空格
    Mysql order by 排序 varchar 类型数据
    JS将人民币小写金额转换为大写
    禁止表单输入中文(禁止粘贴)
    abp vnext 开发快速入门 3 实现权限控制
    abp vnext 开发快速入门 2 实现基本增删改查
    abp vnext 开发快速入门 1 认识框架
    从零开始一起学Blazor WebAssembly 开发(4)
    发布一个自己做的图片转Base64的软件,Markdown写文章时能用到
    一起学Blazor WebAssembly 开发(3)
  • 原文地址:https://www.cnblogs.com/sskbskdrin/p/10648585.html
Copyright © 2011-2022 走看看