音频——WAV 格式详解

这篇具有很好参考价值的文章主要介绍了音频——WAV 格式详解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

WAV 文件格式解析

概述

wav 文件支持多种不同的比特率、采样率、多声道音频。

WAV 文件格式是 Microsoft 的 RIFF 规范的一个子集,用于存储多媒体文件。RIFF(resource interchange file format 资源互换文件格式,以 chunk(块) 为单位组织文件)格式文件。在 windows 上,大部分多媒体文件都是 RIFF 文件。wav 文件由若干个 RIFF chunk 构成,分别为: RIFF WAVE Chunk,Format Chunk,Fact Chunk(可选),Data Chunk。另外,文件中还可能包含一些可选的区块,如:Fact chunk、Cue points chunk、Playlist chunk、Associated data list chunk 等。具体格式如下:

wav文件格式,音频,音视频,microsoft

块解析

wav 文件都是由 chunk 组成,chunk 的格式如下

size 内容 含义
4 bytes ID 如:RIFF,fmt,data
4 bytes chund size 如标准的 fmt chunk 为 16 字节
N bytes data chunk 的内容

RIFF chunk

typedef struct
{
    char ChunkID[4]; //'R','I','F','F'
    unsigned int ChunkSize;
    char Format[4];  //'W','A','V','E'
}riff_chunk;
size 内容 含义
4 bytes ChunkID RIFF
4 bytes ChunkSize 从下一个字段首地址开始到文件末尾的总字节数。该字段的数值加 8 为当前文件的实际长度
4 bytes Format WAVE

其中 ChunkSize 代表的是整个 file_size 的大小减去 ChunkID 和 ChunkSize 的大小,即 file_size=ChunkSize+8。

fmt chunk

typedef struct
{
    char FmtID[4];
    unsigned int FmtSize;
    unsigned short FmtTag;
    unsigned short FmtChannels;
    unsigned int SampleRate;
    unsigned int ByteRate;
    unsigned short BlockAilgn;
    unsigned short BitsPerSample;
}fmt_chunk;
size 内容 含义
4 bytes FmtID fmt
4 bytes FmtSize fmt chunk 的大小,一般有 16/18/20/22/40 字节 (也有超过 40 字节的情况,如果不知道后面部分的含义,直接跳过即可),超过 16 字节部分为扩展块
2 bytes FmtTag 编码格式代码,其值见下 常见编码格式 表,如果上述取值为 16,则此值通常为 1,代表该音频的编码方式是 PCM 编码
2 bytes FmtChannels 声道数目,1 代表单声道,2 代表双声道
4 bytes SampleRate 采样频率,8/11.025/12/16/22.05/24/32/44.1/48/64/88.2/96/176.4/192 kHZ
4 bytes ByteRate 传输速率,每秒的字节数,计算公式为:SampleRate * FmtChannels * BitsPerSample/8
2 bytes BlockAilgn 块对齐,告知播放软件一次性需处理多少字节,公式为: BitsPerSample*FmtChannels/8
2 bytes BitsPerSample 采样位数,一般有8/16/24/32/64,值越大,对声音的还原度越高

常见编码格式

格式编码 格式名称 fmt 块长度 fact 块
0x01 PCM / 非压缩格式 16
0x02 Microsoft ADPCM 18
0x03 IEEE float 18
0x06 ITU G.711 a-law 18
0x07 ITU G.711 μ-law 18
0x031 GSM 6.10 20
0x040 ITU G.721 ADPCM
0xFFFE 见子格式块中的编码格式 40

data chunk

struct DATA_CHUNK
{
    char DataID[4]; //'d','a','t','a'
    unsigned int DataSize;
};
size 内容 含义
4 bytes DataID data
4 bytes DataSize 原始音频数据的大小

示例分析

Linux 平台下使用 mediainfo 分析 wav 文件

安装

 sudo apt-get install mediainfo

解析

执行命令:mediainfo test.wav

General
Complete name                            : test.wav
Format                                   : Wave
File size                                : 1.35 MiB
Duration                                 : 8 s 0 ms
Overall bit rate mode                    : Constant
Overall bit rate                         : 1 411 kb/s

Audio
Format                                   : PCM
Format settings                          : Little / Signed
Codec ID                                 : 1
Duration                                 : 8 s 0 ms
Bit rate mode                            : Constant
Bit rate                                 : 1 411.2 kb/s
Channel(s)                               : 2 channels
Sampling rate                            : 44.1 kHz
Bit depth                                : 16 bits
Stream size                              : 1.35 MiB (100%)

查看文件大小

 ls test.wav -l
-rw-rw-r-- 1 tyustli tyustli 1411248 729 15:03 test.wav

hd 工具查看原始数据文章来源地址https://www.toymoban.com/news/detail-698052.html

hd test.wav -n 128
00000000  52 49 46 46 a8 88 15 00  57 41 56 45 66 6d 74 20  |RIFF....WAVEfmt |
00000010  10 00 00 00 01 00 02 00  44 ac 00 00 10 b1 02 00  |........D.......|
00000020  04 00 10 00 64 61 74 61  84 88 15 00 0e fc fa fe  |....data........|
00000030  6c fb 8a fe c2 fa 19 fe  1e fa b5 fd 85 f9 64 fd  |l.............d.|
00000040  f5 f8 20 fd 73 f8 df fc  09 f8 92 fc bb f7 28 fc  |.. .s.........(.|
00000050  83 f7 9d fb 5f f7 fe fa  46 f7 55 fa 2e f7 a3 f9  |...._...F.U.....|
00000060  23 f7 fa f8 36 f7 6b f8  57 f7 ef f7 84 f7 83 f7  |#...6.k.W.......|
00000070  c7 f7 2d f7 1e f8 eb f6  88 f8 c3 f6 0b f9 c5 f6  |..-.............|
  • 52 49 46 46:对应的 ASCII 字符为:RIFF
  • a8 88 15 00:ChunkSize,对应的十六进制是 0x1588a8=1411240 +8 和上面的文件大小一致
  • 57 41 56 45:对应的 ASCII 字符为 WAVE
  • 66 6d 74 20:对应的 ASCII 字符为 fmt
  • 10 00 00 00:FmtSize 0x10=16 代表PCM编码方式
  • 01 00:对应为1,代表PCM编码方式
  • 02 00:通道个数,通道数为2
  • 44 ac 00 00:采样频率 0xac44=44100=44.1KHz
  • 10 b1 02 00:每秒所需的字节数,转化为十六进制为: 0x2b110=176400 通过此值可以计算该音频的时长:1411240/176400 = 8s
  • 04 00:数据对齐单位
  • 10 00:采样位数 0x10=16
  • 64 61 74 61:对应的 ASCII 字符为 data
  • 84 88 15 00:对应该音频的raw数据的大小,转化为十六进制为 0x158884=1411204,此值等于 1411248-44

代码解析

/*
 * Change Logs:
 * Date           Author       Notes
 * 2022-08-26     tyustli      first version
 */

#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>

typedef struct {
    char ChunkID[4]; //'R','I','F','F'
    unsigned int ChunkSize;
    char Format[4]; //'W','A','V','E'
} riff_chunk;

typedef struct {
    char FmtID[4];
    unsigned int FmtSize;
    unsigned short FmtTag;
    unsigned short FmtChannels;
    unsigned int SampleRate;
    unsigned int ByteRate;
    unsigned short BlockAilgn;
    unsigned short BitsPerSample;
} fmt_chunk;

typedef struct {
    char DataID[4]; //'d','a','t','a'
    unsigned int DataSize;
} data_chunk;


typedef struct {
    riff_chunk riff_region;
    fmt_chunk fmt_region;
    data_chunk data_region;
} wav_struct;

static void *map_file(const char *path);
static void data_dump(wav_struct *data);

int main(int argc, char *argv[]) {

    assert(sizeof(wav_struct) == 44); /* defensive */

    if (argc < 2) {
        printf("usage: %s file_path\r\n", argv[0]);
        exit(-1);
    }

    /* map file */
    wav_struct *map_data = map_file(argv[1]);

    /* data dump */
    data_dump(map_data);

    /* munmap file */
    munmap(map_data, map_data->riff_region.ChunkSize + 8);

    return 1;
}

static void *map_file(const char *path) {

    assert(path != NULL);

    int fd = open(path, O_RDWR);
    if (fd == -1) {
        goto __release;
    }

#if 0
    off_t size = lseek(fd, 0, SEEK_END);
    if (size == -1) {
        goto __release;
    }
#endif

    /* get file size */
    struct stat stat;
    int ret = fstat(fd, &stat);
    if (ret == -1) {
        goto __release;
    }

    size_t size = stat.st_size;

    /* map file */
    void *file_data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
    if(file_data == (void *)-1) {
        goto __release;
    }
    close(fd);

    return file_data;

    /* error handle */
__release:
    perror("map file");
    if (fd > 0) {
        close(fd);
    }
    exit(1);
}

static void data_dump(wav_struct *data) {

    /* riff chunkid */
    printf("riff chunk id          :");
    for(int i = 0; i < 4; i++) {
        printf("%c", data->riff_region.ChunkID[i]);
    }
    printf("\r\n");

    /* file size */
    printf("wav file size          :%d\r\n", data->riff_region.ChunkSize + 8);

    /* riff Format */
    printf("riff format id         :");
    for(int i = 0; i < 4; i++) {
        printf("%c", data->riff_region.Format[i]);
    }
    printf("\r\n");

    /* fmt chunkid */
    printf("fmt chunk id           :");
    for(int i = 0; i < 4; i++) {
        printf("%c", data->fmt_region.FmtID[i]);
    }
    printf("\r\n");

    printf("FmtChannels            :%d(1 单声道, 2 双声道)\r\n", data->fmt_region.FmtChannels);
    printf("FmtTag                 :%d(1 PCM 编码)\r\n", data->fmt_region.FmtTag);
    printf("SampleRate             :%d\r\n", data->fmt_region.SampleRate);
    printf("ByteRate               :%d\r\n", data->fmt_region.ByteRate);
    printf("BitsPerSample          :%d\r\n", data->fmt_region.BitsPerSample);

    /* data chunkid */
    printf("fmt chunk id           :");
    for(int i = 0; i < 4; i++) {
        printf("%c", data->data_region.DataID[i]);
    }
    printf("\r\n");
}
/**
 * 编译:gcc wav_parse.c
 * 运行:./a.out sample.wav
 * 结果:
 * riff chunk id          :RIFF
 * wav file size          :497904
 * riff format id         :WAVE
 * fmt chunk id           :fmt 
 * FmtChannels            :2(1 单声道, 2 双声道)
 * FmtTag                 :1(1 PCM 编码)
 * SampleRate             :44100
 * ByteRate               :176400
 * BitsPerSample          :16
 * fmt chunk id           :data
 * 
*/

/*************** end of file ***************/

到了这里,关于音频——WAV 格式详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 音频格式(一)PCM和WAV

            想要了解音频首先要了解它的构造,知道它怎么从声音变成文件,又怎么从文件变成声音。文件格式根据需求和技术的进步有了不同的版本,不同的文件格式有其不同的文件构造。我们先从最原始的两种音频文件入手,讲一讲常见的音频文件格式。首先是PCM和WAV   

    2023年04月24日
    浏览(26)
  • 音视频剪辑|FFMPEG|windows10下的音视频格式转换,遮挡填充,GIF动图制作,背景音频抽取,替换

    最近对于音视频和图像的处理问题比较感兴趣,但发现很多目前需要的功能要么需要付费但不会过于麻烦,要么比较麻烦,很可能某个功能实现需要安装很多软件 例如,视频转GIF动图,该功能的实现要么使用Photoshop全家桶,要么找在线网站,或者是wps充会员,或者找其它方法

    2024年02月20日
    浏览(45)
  • 【Unity】流式播放远端音频:WAV格式音频篇(一)

    先了解一下wav的格式: 参考1:【音频】WAV 格式详解_tyustli的博客-CSDN博客_wav文件格式详解 wav 文件支持多种不同的比特率、采样率、多声道音频。WAV 文件格式是 Microsoft 的 RIFF 规范的一个子集,用于存储多媒体文件。RIFF(resource interchange file format 资源互换文件格式,以 chu

    2024年02月06日
    浏览(38)
  • 将音频格式从flac转到wav的两种方法

    最近在智能语音中用到了数据集cn-celeb。这个数据集的音频格式是flac,而在做数据增强(augmentation)以及模型训练时用的数据格式是wav,因此需要把音频格式从flac转到wav。我在ubuntu下摸索了一番,找到了两种方法。   1,基于Linux下的sox命令写shell脚本实现 SoX​​(即 Sound eXch

    2024年02月03日
    浏览(29)
  • mp3怎样才能转换成wav格式?音频互相转换的方法

    一,什么是WAV WAV,全称为波形音频文件(Waveform Audio File Format),是一种由微软公司和IBM公司联合开发的音频文件格式。自1991年问世以来,WAV格式因其无损的音频质量和广泛的兼容性,成为了多媒体应用中不可或缺的一部分。本文将从WAV格式的定义、特点、应用以及与其他音

    2024年04月14日
    浏览(34)
  • 【音视频|PCM】PCM格式详解

    😁博客主页😁:🚀https://blog.csdn.net/wkd_007🚀 🤑博客内容🤑:🍭嵌入式开发、Linux、C语言、C++、数据结构、音视频🍭 🤣本文内容🤣:🍭介绍数字音频的PCM格式🍭 😎金句分享😎:🍭子曰:君子不器。 ——《论语·为政篇》。意思是,君子不应像器具那样,只有一种用

    2024年02月08日
    浏览(28)
  • 【音频】python读取写入wav文件

    使用librosa库: librosa.load(文件路径,采样率sampling rate) 直接用librosa的函数 librosa.output.write_wav(\\\'output.wav\\\', y, sr) 会报错: \\\"AttributeError: module \\\'librosa\\\' has no attribute \\\'output\\\'\\\" 。 这是因为librosa库在0.8.0版本后把这个函数删掉了,我们可以用soundfile库的soundfile.write()函数达到同样的目的

    2024年02月03日
    浏览(33)
  • Python环境下基于自适应滤波器的音频信号(wav格式)降噪方法

    Python的集成环境我一般使用的是 Winpython , Winpytho 脱胎于pythonxy,面向 科学计算 , 兼顾数据分析与挖掘 ;Anaconda 主要面向数据分析与挖掘方面 ,在大数据处理方面有自己特色的一些包; Winpytho 强调 便携性 ,被做成绿色软件,不写入注册表,安装其实就是解压到某个文件夹

    2024年01月18日
    浏览(29)
  • 【音视频原理】音视频 “ 采样 - 编码 - 封装 过程 “ 和 “ 解封装 - 解码 - 播放 过程 “ 分析 ( 视频采集处理流程 | 音频采集处理流程 | 音视频文件解封装播放流程 )

    本篇文件主要分析 音视频文件 是怎么产生的 , 以及 音视频文件是如何播放的 ; 视频文件从录像到生成文件的全过程 : 采集图像帧 : 摄像头 硬件 负责 采集画面 , 采集的 初始画面 称为 \\\" 图像帧 \\\" , 一秒钟 采集 的 图像帧 数量 称为 \\\" 帧率 \\\" , 如 : 60 帧 就是 一秒钟采集 60 个画

    2024年02月11日
    浏览(51)
  • 【音视频处理】音频编码AAC详解,低码率提高音质?

    大家好,欢迎来到停止重构的频道。 本期我们介绍 音频编码格式AAC 。 AAC是音频最常用的编码格式之一 ,几乎所有的播放器都支持这个编码格式。 其他音频编码格式都是类似的,只是某些细节存在差别,如压缩算法、某些音频参数存在限制等。 我们按这样的顺序讨论 :

    2024年02月09日
    浏览(64)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包