• WAV和PCM的关系和区别


    什么是WAV和PCM?

    WAV:wav是一种无损的音频文件格式,WAV符合 PIFF(Resource Interchange File Format)规范。所有的WAV都有一个文件头,这个文件头音频流的编码参数。WAV对音频流的编码没有硬性规定,除了PCM之外,还有几乎所有支持ACM规范的编码都可以为WAV的音频流进行编码。

    PCM:PCM(Pulse Code Modulation----脉码调制录音)。所谓PCM录音就是将声音等模拟信号变成符号化的脉冲列,再予以记录。PCM信号是由[1]、[0]等符号构成的数字信号,而未经过任何编码和压缩处理。与模拟信号比,它不易受传送系统的杂波及失真的影响。动态范围宽,可得到音质相当好的影响效果。

    简单来说:wav是一种无损的音频文件格式,pcm是没有压缩的编码方式。

    WAV和PCM的关系

    WAV可以使用多种音频编码来压缩其音频流,不过我们常见的都是音频流被PCM编码处理的WAV,但这不表示WAV只能使用PCM编码,MP3编码同样也可以运用在WAV中,和AVI一样,只要安装好了相应的Decode,就可以欣赏这些WAV了。在Windows平台下,基于PCM编码的WAV是被支持得最好的音频格式,所有音频软件都能完美支持,由于本身可以达到较高的音质的要求,因此,WAV也是音乐编辑创作的首选格式,适合保存音乐素材。因此,基于PCM编码的WAV被作为了一种中介的格式,常常使用在其他编码的相互转换之中,例如MP3转换成WMA。

    简单来说:pcm是无损wav文件中音频数据的一种编码方式,但wav还可以用其它方式编码。

    将录音写成wav格式的文件

    有时候需要将录音文件保存为wav格式,这需要手动填充wav的文件头信息,整段代码非常简单,大致如下:

     1 private RandomAccessFile fopen(String path) throws IOException {
     2     File f = new File(path);
     3 
     4     if (f.exists()) {
     5         f.delete();
     6     } else {
     7         File parentDir = f.getParentFile();
     8         if (!parentDir.exists()) {
     9             parentDir.mkdirs();
    10         }
    11     }
    12 
    13     RandomAccessFile file = new RandomAccessFile(f, "rw");
    14     // 16K、16bit、单声道
    15     /* RIFF header */
    16     file.writeBytes("RIFF"); // riff id
    17     file.writeInt(0); // riff chunk size *PLACEHOLDER*
    18     file.writeBytes("WAVE"); // wave type
    19 
    20     /* fmt chunk */
    21     file.writeBytes("fmt "); // fmt id
    22     file.writeInt(Integer.reverseBytes(16)); // fmt chunk size
    23     file.writeShort(Short.reverseBytes((short) 1)); // format: 1(PCM)
    24     file.writeShort(Short.reverseBytes((short) 1)); // channels: 1
    25     file.writeInt(Integer.reverseBytes(16000)); // samples per second
    26     file.writeInt(Integer.reverseBytes((int) (1 * 16000 * 16 / 8))); // BPSecond
    27     file.writeShort(Short.reverseBytes((short) (1 * 16 / 8))); // BPSample
    28     file.writeShort(Short.reverseBytes((short) (1 * 16))); // bPSample
    29 
    30     /* data chunk */
    31     file.writeBytes("data"); // data id
    32     file.writeInt(0); // data chunk size *PLACEHOLDER*
    33 
    34     Log.d(TAG, "wav path: " + path);
    35     return file;
    36 }
    37 
    38 private void fwrite(RandomAccessFile file, byte[] data, int offset, int size) throws IOException {
    39     file.write(data, offset, size);
    40     Log.d(TAG, "fwrite: " + size);
    41 }
    42 
    43 private void fclose(RandomAccessFile file) throws IOException {
    44     try {
    45         file.seek(4); // riff chunk size
    46         file.writeInt(Integer.reverseBytes((int) (file.length() - 8)));
    47 
    48         file.seek(40); // data chunk size
    49         file.writeInt(Integer.reverseBytes((int) (file.length() - 44)));
    50 
    51         Log.d(TAG, "wav size: " + file.length());
    52 
    53     } finally {
    54         file.close();
    55     }
    56 }

    参考资料



    作者:张明云
    链接:https://www.jianshu.com/p/1d1f893e53e9
    來源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
  • 相关阅读:
    矩阵快速幂的学习(系统的学习)
    hdu3949(线性基,求第k小的异或和
    牛客网训练赛26D(xor)
    牛客网练习赛26B(简单的dp)
    Carryon的字符串
    string的各种函数(系统学习)
    约瑟夫问题(vector的使用)
    vector的学习(系统的学习)
    CodeForces
    POJ-3624-背包问题
  • 原文地址:https://www.cnblogs.com/ricks/p/9522243.html
Copyright © 2020-2023  润新知