wav文件格式

http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html

Audio File Format Specifications

File Description: WAVE or RIFF WAVE sound file
File Extension: Commonly.wav, sometimes.wave
File Byte Order: Little-endian

P. Kabal, TSP Lab, ECE, McGill University: Last update:2006-06-19

WAVE Specifications

The WAVE file specifications came from Microsoft. The WAVE file format use RIFF chunks, each chunk consisting of a chunk identifier, chunk length and chunk data.

Data Types

The data in WAVE files can be of many different types. Data format codes are listed in the following:

Wave File Format

Wave files have a master RIFF chunk which includes a WAVE identifier followed by sub-chunks. The data is stored in little-endian byte order.

FieldLengthContents
ckID4Chunk ID: "RIFF"
cksize4Chunk size:4+n
 WAVEID4WAVE ID: "WAVE"
WAVE chunksnWave chunks containing format information and sampled data

Format Chunk

The Format chunk specifies the format of the data. There are 3 variants of the Format chunk for sampled data. These differ in the extensions to the basic Formant chunk.

FieldLengthContents
ckID4Chunk ID: "fmt"
cksize4Chunk size:16or18or40
 wFormatTag2Format code
nChannels2Number of interleaved channels
nSamplesPerSec4Sampling rate (blocks per second)
nAvgBytesPerSec4Data rate
nBlockAlign2Data block size (bytes)
wBitsPerSample2Bits per sample

cbSize2Size of the extension (0or22)

wValidBitsPerSample2Number of valid bits
dwChannelMask4Speaker position mask
SubFormat16GUID, including the data format code

The standard format codes for waveform. data are given below. The references above give many more format codes for compressed data, a good fraction of which are now obsolete.

Format CodePreProcessor SymbolData
0x0001WAVE_FORMAT_PCMPCM
0x0003WAVE_FORMAT_IEEE_FLOATIEEE float
0x0006WAVE_FORMAT_ALAW8-bit ITU-T G.711 A-law
0x0007WAVE_FORMAT_MULAW8-bit ITU-T G.711 µ-law
0xFFFEWAVE_FORMAT_EXTENSIBLEDetermined bySubFormat

PCM Format

The first part of the Format chunk is used to describe PCM data.

  • For PCM data, the Format chunk in the header declares the number of bits/sample in each sample (wBitsPerSample). The original documentation (Revision 1) specified that the number of bits per sample is to be rounded up to the next multiple of 8 bits. This rounded-up value is the container size. This information is redundant in that the container size (in bytes) for each sample can also be determined from the block size divided by the number of channels (nBlockAlign/nChannels).
    • This redundancy has been appropriated to define new formats. For instance,Cool Edituses a format which declares a sample size of 24 bits together with a container size of 4 bytes (32 bits) determined from the block size and number of channels. With this combination, the data is actually stored as 32-bit IEEE floats. The normalization (full scale 223) is however different from the standard float format.
  • PCM data is two's-complement except for resolutions of 1-8 bits, which are represented as offset binary.

Non-PCM Formats

An extended Format chunk is used for non-PCM data. ThecbSizefield gives the size of the extension.

  • For all formats other than PCM, the Format chunkmusthave an extended portion. The extension can be of zero length, but the size field (with value0) must be present.
  • For float data, full scale is 1. The bits/sample would normally be 32 or 64.
  • For the log-PCM formats (µ-law and A-law), the Rev. 3 documentation indicates that the bits/sample field (wBitsPerSample) should be set to 8 bits.
  • The non-PCM formats must have a Fact chunk.

Extensible Format

TheWAVE_FORMAT_EXTENSIBLEformat code indicates that there is an extension to the Format chunk. The extension has one field which declares the number of "valid" bits/sample (wValidBitsPerSample). Another field (dwChannelMask) contains a bits which indicate the mapping from channels to loudspeaker positions. The last field (SubFormat) is a 16-byte globally unique identifier (GUID).

  • With theWAVE_FORMAT_EXTENSIBLEformat, the original bits/sample field (wBitsPerSample) must match the container size (8 * nBlockAlign/nChannels). This means thatwBitsPerSamplemust be a multiple of8. Reduced precision within the container size is now specified bywValidBitsPerSample.
  • The number of valid bits (wValidBitsPerSample) is informational only. The data is correctly represented in the precision of the container size. The number of valid bits can be any value from 1 to the container size in bits.
  • The loudspeaker position mask uses 18 bits, each bit corresponding to a speaker position (e.g. Front Left or Top Back Right), to indicate the channel to speaker mapping. More details are in the document cited above. This field is informational. An all-zero field indicates that channels are mapped to outputs in order: first channel to first output, second channel to second output, etc.
  • The first two bytes of the GUID form. the sub-code specifying the data format code, e.g.WAVE_FORMAT_PCM. The remaining 14 bytes contain a fixed string, "/x00/x00/x00/x00/x10/x00/x80/x00/x00/xAA/x00/x38/x9B/x71".

TheWAVE_FORMAT_EXTENSIBLEformat should be used whenever:

  • PCM data has more than 16 bits/sample.
  • The number of channels is more than 2.
  • The actual number of bits/sample is not equal to the container size.
  • The mapping from channels to speakers needs to be specified.

Fact Chunk

All (compressed) non-PCM formatsmusthave a Fact chunk (Rev. 3 documentation). The chunk contains at least one value, the number of samples in the file.

FieldLengthContents
ckID4Chunk ID: "fact"
cksize4Chunk size: minimum4
 dwSampleLength4Number of samples (per channel)
  • The Rev. 3 documentation states that the Fact chunk "is required for all new new WAVE formats", but "is not required  for the standardWAVE_FORMAT_PCMfiles". One presumes that files with IEEE float data (introduced after the Rev. 3 documention) need a Fact chunk.
  • The number of samples field is redundant for sampled data, since the Data chunk indicates the length of the data. The number of samples can be determined from the length of the data and the container size as determined from the Format chunk.
  • Their is an ambiguity as to the meaning of "number of samples" for multichannel data. The implication in the Rev. 3 documentation is that it should be interpreted to be "number of samples per channel". The statement in the Rev. 3 documentation is:
    "The <nSamplesPerSec> field from the wave format header is used in conjunction with the <dwSampleLength> field to determine the length of the data in seconds."
    With no mention of the number of channels in this computation, this implies thatdwSampleLengthis the number of samples per channel.
  • There is a question as to whether the Fact chunk should be used for (including those with PCM)WAVE_FORMAT_EXTENSIBLEfiles. One example of aWAVE_FORMAT_EXTENSIBLEwith PCM data from Microsoft, does not have a Fact chunk.

Data Chunk

The Data chunk contains the sampled data.

FieldLengthContents
ckID4Chunk ID: "data"
cksize4Chunk size:n
 sampled datanSamples
pad byte0or1Padding byte ifnis odd

 

Examples

Consider sampled data with the following parameters,

  • Ncchannels
  • The total number of blocks isNs. Each block consists ofNcsamples.
  • Sampling rateF(blocks per second)
  • Each sample isMbytes long

PCM Data

FieldLengthContents
ckID4Chunk ID: "RIFF"
cksize4Chunk size:4 + 24 +
(8 +M*Nc* Ns+ (0
or1))
 WAVEID4WAVE ID: "WAVE"

ckID4Chunk ID: "fmt"
cksize4Chunk size:16
 wFormatTag2WAVE_FORMAT_PCM
nChannels2Nc
nSamplesPerSec4F
nAvgBytesPerSec4F*M*Nc
nBlockAlign2M*Nc
wBitsPerSample2rounds up to8 *M

ckID4Chunk ID: "data"
cksize4Chunk size:M*Nc* Ns
 sampled dataM*Nc*NsNc*Nschannel-interleavedM-byte samples
pad0or1Padding byte ifM*Nc*Nsis odd
Notes
  • WAVE files often have information chunks that precede or follow the sound data (Data chunk). Some programs (naively) assume that for PCM data, the file header is exactly 44 bytes long and that the rest of the file contains sound data. This is not a safe assumption.

Non-PCM Data

FieldLengthContents
ckID4Chunk ID: "RIFF"
cksize4Chunk size:4 + 26 + 12 +
(8 +M*Nc*Ns+ (0
or1))
 WAVEID4WAVE ID: "WAVE"

ckID4Chunk ID: "fmt"
cksize4Chunk size:18
 wFormatTag2Format code
nChannels2Nc
nSamplesPerSec4F
nAvgBytesPerSec4F*M*Nc
nBlockAlign2M*Nc
wBitsPerSample28 *M(float data) or16(log-PCM data)
cbSize2Size of the extension:0

ckID4Chunk ID: "fact"
cksize4Chunk size:4
 dwSampleLength4Nc*Ns

ckID4Chunk ID: "data"
cksize4Chunk size:M*Nc*Ns
 sampled dataM*Nc*NsNc*Nschannel-interleavedM-byte samples
pad0or1Padding byte ifM*Nc*Nsis odd
  • MicrosoftWindows Media Playerwill not play non-PCM data (e.g. µ-law data) if the Format chunk does not have the extension size field (cbSize) or a Fact chunk is not present.

Extensible Format

FieldLengthContents
ckID4Chunk ID: "RIFF"
cksize4Chunk size:4 + 48 + 12 +
(8 +M*Nc*Ns+ (0
or1))
 WAVEID4WAVE ID, "WAVE"

ckID4Chunk ID: "fmt"
cksize4Chunk size:40
 wFormatTag2WAVE_FORMAT_EXTENSIBLE
nChannels2Nc
nSamplesPerSec4F
nAvgBytesPerSec4F*M*Nc
nBlockAlign2M*Nc
wBitsPerSample28 *M
cbSize2Size of the extension:22
wValidBitsPerSample2at most8 *M
dwChannelMask4Speaker position mask:0
SubFormat16GUID (first two bytes are the data format code)

ckID4Chunk ID: "fact"
cksize4Chunk size:4
 dwSampleLength4Nc*Ns

ckID4Chunk ID: "data"
cksize4Chunk size:M*Nc*Ns
 sampled dataM*Nc*NsNc*Nschannel-interleavedM-byte samples
pad0or1Padding byte ifM*Nc*Nsis odd
  • The Fact chunk can be omitted if the sampled data is in PCM format.
  • MicrosoftWindows Media Playerenforces the use of theWAVE_FORMAT_EXTENSIBLEformat code. For instance a file with 24-bit data declared as a standardWAVE_FORMAT_PCMformat code will not play, but a file with 24-bit data declared as aWAVE_FORMAT_EXTENSIBLEfile with aWAVE_FORMAT_PCMsubcode can be played.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值