Convert pcm to wav format-1-Wav Format

本文详细介绍了WAV文件格式,包括其在RIFF标准下的结构、主要数据块(fmt和data)参数、chunksize的计算方法,以及如何将PCM数据转换为WAV文件。重点讲解了WAV文件的构成和PCM数据的处理过程。
摘要由CSDN通过智能技术生成

wav 文件格式介绍

The WAVE file format is a subset of Microsoft's RIFF specification for the storage of multimedia files. A RIFF file starts out with a file header followed by a sequence of data chunks. A WAVE file is often just a RIFF file with a single "WAVE" chunk which consists of two sub-chunks -- a "fmt " chunk specifying the data format and a "data" chunk containing the actual sample data. Call this form the "Canonical form". 

RIFF文件一般以一个文件头开始,后面跟着一系列的数据。WAV文件一般是一个RIFF文件,外加一个包含两个子数据块(fmt 数据库和data数据库)的WAV数据块组成的。我们把这个形式作为规范形式。

wav文件格式数据组成

wav文件格式参数介绍

OffsetSizeNameDescription
04
 ChunkID

Contains the letters "RIFF" in ASCII form(0x52494646 big-endian form).

包含 字符串"RIFF",二进制文件中表现为“52 49 46 46”“

44
ChunkSize

36 + SubChunk2Size, or more precisely:4 + (8 + SubChunk1Size) + (8 + SubChunk2Size)

This is the size of the rest of the chunk

following this number.  This is the size of the entire file in bytes minus 8 bytes for the two fields not included in this count:ChunkID and ChunkSize.

ChunkSize的计算方式

84
Format

Contains the letters "WAVE"(0x57415645 big-endian form).

包含字符串WAV,二进制文件中表现为"57 41 56 45"

124
Subchunk1ID

Contains the letters "fmt "(0x666d7420 big-endian form).

包含字符串fmt,二进制文件中表现为"66 6d 74 20"

164
 Subchunk1Size

16 for PCM.  This is the size of the rest of the Subchunk which follows this number.

固定值,为后续fmt子数据块数据的size

204
AudioFormat      

PCM = 1 (i.e. Linear quantization)Values other than 1 indicate some form of compression.

222
NumChannels
Mono = 1, Stereo = 2, etc
242
SampleRate
8000, 44100, etc.
284
ByteRate 
== SampleRate * NumChannels * BitsPerSample/8
324
BlockAlign

 == NumChannels * BitsPerSample/8 The number of bytes for one sample including all channels.

342
 BitsPerSample
8 bits = 8, 16 bits = 16, etc.
362
Subchunk2ID

Contains the letters "data"(0x64617461 big-endian form).

包含字符串data,二进制文件中表现为"64 61 74 61"

404
Subchunk2Size

== NumSamples * NumChannels * BitsPerSample/8

This is the number of bytes in the data.You can also think of this as the size of the read of the subchunk following this number.

这个size实际就是pcm data的数据

444
 Data
The actual sound data

Wav二进制文件分析

下面是一个72bytes的wav 文件

二进制数据

52 49 46 46 24 08 00 00 57 41 56 45 66 6d 74 20 10 00 00 00 01 00 02 00 
22 56 00 00 88 58 01 00 04 00 10 00 64 61 74 61 00 08 00 00 00 00 00 00 
24 17 1e f3 3c 13 3c 14 16 f9 18 f9 34 e7 23 a6 3c f2 24 f2 11 ce 1a 0d 

数据分析

可对比wav文件参数介绍部分内容。

需要介绍的是ChunkSize的计算方式。

我们可以看到二进制中ChunkSize表示为 “24 08 00 00”,在换算时应该列为 08 24 ->对应10进制就为图中的2048,相同方式可以得到SubChunk1Size以及SubChunk2Size.

PCM 数据格式文件转化为WAV 文件格式

根据前面WAV 格式文件分析,如果已经拿到了PCM的数据,想要将生成的数据保存为WAV 文件格式,只需根据WAV文件格式需求,为PCM加一个file header,加在数据头部即可。

参考链接:Microsoft WAVE soundfile format

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值