Wav file structure Parsing

Source: Internet
Author: User
Wave is the standard Windows file format used for recording. The file extension is "WAV" and the data format is PCM or compressed.

The WAV file format is a standard for Audio Digital Storage jointly developed by Microsoft and IBM. It adopts the riff File Format Structure and is very similar to the AIFF and IFF formats. Complies with the piff resource interchange file format specification. All WAV Files have a file header, which is the encoding parameter of the audio stream.

Wav files, as the most classic windows multimedia audio format, are widely used. They use three parameters to represent sound: Sampling digits, sampling frequencies, and number of audio channels.

Channels are classified into single-channel and stereo sound. Generally, the sampling frequency is 11025Hz (11 kHz), 22050Hz (22 kHz), and 44100Hz (44 kHz. Capacity of WAV Files = (sampling frequency × number of sampling digits × audio channel) × time/8 (1 byte = 8 bit ).

Wav has no hard coding rules for audio streams. Apart from PCM, almost all codes supporting ACM specifications can be encoded for WAV audio streams. Multimedia Applications use a variety of data, including bitmap, audio data, video data, and peripheral device control information. Riff provides a way to store these types of data. The data types contained in a riff file are identified by the file extension. The data stored in a riff file includes:
Audio/Video staggered format data (. avi), waveform format data (. wav), bitmap format data (. RDI), MIDI format data (. rmi), color palette format (. PAL), multimedia movies (. rmn), animation cursor (. ani), other riff files (. BND ).

Wave files can store a large amount of data in a format. Generally, the audio encoding method is pulse encoding modulation (PCM ). The WAV format is derived from the Windows/Intel environment and therefore stored in little-Endian byte sequence.

As one of the acoustic file formats used in multimedia, the wave file is in the riff format as the standard. Riff is short for resource interchange file format. The first four bytes of each wave file are "riff ". A wave file consists of a file header and a data body. The file header is divided into two parts: Riff/WAV file ID segment and audio data format description segment. For the content and format of each part of the wave file, see the appendix.

There are two types of common audio files, which correspond to single-channel (11.025khz sampling rate, 8-bit sampling value) and dual-channel (44.1khz sampling rate and 16-bit sampling value ). Sampling Rate refers to the number of times the audio signal is sampled per unit time during the "modulo → count" conversion process. The sampling value refers to the integral value of the sound analog signal during each sampling period.

For single-channel audio files, the sample data is short int 00h-ffh. For two-channel stereo audio files, the sample data is a 16-bit integer (INT ), the 8-bit high and the 8-bit low represent both the left and right channels.

The data block of the wave file contains samples in Pulse-Coded Modulation (PCM) format. A wave file is composed of samples. In a single channel wave file, channel 0 indicates the left channel, and channel 1 indicates the right channel. In a multi-channel wave file, samples are generated alternately.

Each sample value of the wave file is included in an integer I, and the length of I is the minimum number of bytes required to hold the specified sample length. First, store low valid bytes, indicating that the bit of the sample amplitude is placed on the high valid bit of I, and the remaining position is 0, so that the data format of the 8-bit and 16-bit PCM waveform samples is as follows. As one of the acoustic file formats used in multimedia, the wave file is in the riff format as the standard.
Riff is the abbreviation of resource interchange file format. The first four
The byte is "riff ".

A wave file is composed of several chunks. According to the location where the file appears, including: Riff Wave
Chunk, format chunk, fact chunk (optional), data chunk. For details, see:

------------------------------------------------
| Riff wave chunk |
| Id = 'riff' |
| Rifftype = 'wave '|
------------------------------------------------
| Format chunk |
| Id = 'fmt' |
------------------------------------------------
| Fact chunk (optional) |
| Id = 'fact' |
------------------------------------------------
| Data chunk |
| Id = 'data' |
------------------------------------------------
Figure 1 example of chunk in WAV format

Riff wave chunk
========================================
| Bytes | specific content |
========================================
| ID | 4 bytes | 'riff' |
----------------------------------
| Size | 4 bytes |
----------------------------------
| Type | 4 bytes | 'wave '|
----------------------------------
Figure 2 riff wave chunk

Use 'riff' as the marker, followed by the size field. The size is the size of the whole WAV file minus the ID.
And the number of bytes occupied by size, that is, filelen-8 = size. Then the type field, which is 'wave ', table
Shows a WAV file.
The structure is defined as follows:
Struct riff_header
{
Char szriffid [4]; // 'R', 'I', 'F', 'F'
DWORD dwriffsize;
Char szriffformat [4]; // 'w', 'A', 'V', 'E'
};

Format chunk
========================================================== ======================================
| Number of bytes | specific content |
========================================================== ======================================
| ID | 4 bytes | 'fmt' |
--------------------------------------------------------------------
| Size | 4 bytes | if the value is 16 or 18, the last information is appended. |
------------------------------------------------------------------------
| Formattag | 2 bytes | encoding method, generally 0x0001 |
-------------------------------------------------------------------- |
| Channels | 2 bytes | Number of audio channels; values: 1 (Single Channel) and 2 (dual channel) |
-------------------------------------------------------------------- |
| Samplespersec | 4 bytes | sampling frequency |
-------------------------------------------------------------------- |
| Avgbytespersec | 4 bytes | number of required bytes per second ||==> wave_format
-------------------------------------------------------------------- |
| Blockalign | 2 bytes | data block alignment unit (the number of bytes required for each sample) |
-------------------------------------------------------------------- |
| Bitspersample | 2 bytes | Number of BITs required for each sample |
-------------------------------------------------------------------- |
| 2 bytes | additional information (optional, which can be determined by size) |
------------------------------------------------------------------------
Figure 3 Format chunk

Use 'fmt' as the flag. Generally, the size is 16, and the last additional information does not exist. If the size is 18
Then, two additional bytes are added. These two bytes are contained in the WAV format made mainly by some software.
Additional information.
The structure is defined as follows:
Struct wave_format
{
Word wformattag;
Word wchannels;
DWORD dwsamplespersec;
DWORD dwavgbytespersec;
Word wblockalign;
Word wbitspersample;
};
Struct fmt_block
{
Char szfmtid [4]; // 'F', 'M','t ',''
DWORD dwfmtsize;
Wave_format wavformat;
};

Fact chunk
========================================
| Bytes | specific content |
========================================
| ID | 4 bytes | 'fact' |
----------------------------------
| Size | 4 bytes | the value is 4. |
----------------------------------
| Data | 4 bytes |
----------------------------------
Figure 4 fact chunk

Fact Chunk is an optional field. Generally, when a WAV file is converted from some software, it contains the chunk.
The structure is defined as follows:
Struct fact_block
{
Char szfactid [4]; // 'F', 'A', 'C', 'T'
DWORD dwfactsize;
};

Data chunk
========================================
| Bytes | specific content |
========================================
| ID | 4 bytes | 'data' |
----------------------------------
| Size | 4 bytes |
----------------------------------
| Data |
----------------------------------
Figure 5 Data chunk

The data Chunk is the place where the wav data is actually stored. 'data' is used as the chunk identifier. Then
Data size. Followed by WAV data. Based on the number of channels in the format Chunk and the number of sampling bits,
The bit location of Wav data can be divided into the following forms:
---------------------------------------------------------------------
| Mono | sampling 1 | sampling 2 | sampling 3 | sampling 4 |
| --------------------------------------------------------
| 8-bit quantization | channel 0 | channel 0 | channel 0 | channel 0 |
---------------------------------------------------------------------
| Dual-channel | sampling 1 | sampling 2 |
| --------------------------------------------------------
| 8-bit quantization | channel 0 (left) | channel 1 (right) | channel 0 (left) | channel 1 (right) |
---------------------------------------------------------------------
| Sampling 1 | sampling 2 |
| Single channel | --------------------------------------------------------
| 16-bit quantization | channel 0 | channel 0 | channel 0 | channel 0 |
| (Low byte) | (high byte) |
---------------------------------------------------------------------
| Sample 1 |
| Dual-channel | --------------------------------------------------------
| 16-bit quantization | channel 0 (left) | channel 0 (left) | channel 1 (right) | channel 1 (right) |
| (Low byte) | (high byte) |
---------------------------------------------------------------------
Figure 6 bit location arrangement of Wav data

The data chunk header structure is defined as follows:
Struct data_block
{
Char szdataid [4]; // 'D', 'A', 't', 'A'
DWORD dwdatasize;
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.