WAV Audio File Header parsing

Source: Internet
Author: User

1. WAV Overview wav is A sound file format developed by Microsoft Corporation (Microsoft) that complies with the riff (Resource Interchange File format) files specification, The audio information resource for saving the Windows platform is widely supported by the Windows platform and its applications, which also supports a variety of compression operations, such as Msadpcm,ccitt A Law, and supports a variety of audio numbers, sampling frequencies and channels, The standard formatted WAV file is the same as the CD format, 44.1K sampling frequency, 16-bit quantization number, so the sound file quality and CD-like! The WAV Open tool is a media player for Windows . typically three parameters are used to represent sound, quantify the number of bits, sample frequency, and sample point amplitude. Quantization bits are divided into 8-bit, 16-bit, 24-bit three-channel channels with mono and stereo, mono amplitude data is n*1 matrix point, Stereo is n*2 matrix point, sampling frequency is generally 11025Hz (11kHz), 22050Hz (22kHz) and 44100Hz (44kHz) Three kinds, but despite the excellent sound quality, the compressed file size is too large! Relative to other audio formats is a disadvantage, its file size is calculated as: WAV format file occupied capacity (B) = (sampling frequency x quantization digit x channel) x Time/8 (byte = 8bit) each minute the size of the WAV format audio file is 10MB, its size does not vary with the volume size and sharpness and change. WAV-enabled phones are primarily smartphones, such as Sony Ericsson P910 and Nokia N90, and phones with Windows Moblie Dopod, as well as Microsoft's Windows Phone series phones, while other non-smartphone products If the promotion supports WAV format, most of them are only mono-supported. 2. Format parsing wave files are a very simple type of riff file, and its format is "WAVE". The RIFF block contains two sub-blocks, the IDs of the two sub-blocks are "FMT" and "data", wherein the "FMT" sub-block consists of the structure Pcmwaveformat, the size of which is Sizeofof (Pcmwaveformat), The data composition is the data in the Pcmwaveformat structure. the entire head length is 44byte.

Marker (RIFF)

The length of all remaining data

Format type ("WAVE")

"FMT"

Length of Pcmwaveformat

Pcmwaveformat

"Data"

Sound data size

Sound data


3, WAV head structure definition
/* RIFF WAVE file struct. * For details see WAVE file Format Documentation  * (for example at http://www.wotsit.org). */typedef struct Wav_header _s{char   rifftype[4];//4byte, resource Exchange file flag: riffunsigned int   riffsize;//4byte, total bytes from next address to end of file char   Wavetype [4];//4byte,wav File flag: Wavechar   formattype[4];//4byte, waveform file flag: FMT (last character space) unsigned intformatsize;//4byte, audio properties ( Compressioncode,numchannels,samplerate,bytespersecond,blockalign,bitspersample) The number of bytes unsigned Shortcompressioncode;//2byte, type of format (1-linear PCM-WAVE_FORMAT_PCM,WAVEFORMAT_ADPCM) unsigned short numchannels;//2byte, Number of channels unsigned int   samplerate;//4byte, sample rate unsigned int   bytespersecond;//4byte, transfer rate unsigned short blockalign;// 2byte, the alignment of the data block, that is, the length unsigned short bitspersample;//2byte, sampling precision-pcm bit width char   datatype[4];//4byte, Data flag: dataunsigned int   Datasize;//4byte, the total number of bytes from the next address to the end of the file, i.e. PCM data length}wav_header other than the WAV HEADER;
4. Example of the head resolverwav.h
#ifndef __wav_h__#define __wav_h__#define Debug (fmt ...) do {printf ("[%s::%d]", __func__, __line__);p rintf (FMT);} while (0)/* RIFF WAVE file struct. * For details see WAVE file Format documentation * (for example at http://www.wotsit.org). */typedef struct Wav_header_s{char rifftype[4];//4byte, resource Exchange file flag: riffunsigned int Riffsize;//4byte, Total bytes from next address to end of file char wavetype[4];//4byte,wave file flag: Wavechar formattype[4];//4byte, waveform file flag: fmtunsigned int formatsize ;//4byte, the number of bytes in the audio properties (compressioncode,numchannels,samplerate,bytespersecond,blockalign,bitspersample) unsigned Shortcompressioncode;//2byte, encoded format (1-linear PCM-WAVE_FORMAT_PCM,WAVEFORMAT_ADPCM) unsigned short numchannels;//2byte, Number of channels unsigned int samplerate;//4byte, sample rate unsigned int bytespersecond;//4byte, transfer rate unsigned short blockalign;//2byte, Data block alignment unsigned short bitspersample;//2byte, sample precision char Datatype[4];//4byte, data marker: dataunsigned int datasize;//4byte, The total number of bytes from the next address to the end of the file, i.e. PCM data length}wav_header;typedef struct wav_info_s{wav other than the WAV HEADER_header HEADER;  FILE *FP; unsigned int channelmask;} Wav_info; #endif
wav.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "wav.h"/* Func:endian Judge * Return:0-big-endian othes-<span style= "Color:rgb (51, 51, 51); font-family: ' Courier New '; font-size:12px; line-height:24px; text-indent:24px; " >little</span>-endian */int Is_little_endian (void) {int __dummy = 1;return (* (unsigned char*) (& (__dummy) ) ) );}  unsigned int readheader (void *dst, signed int size, signed int nmemb, FILE *fp) {unsigned int n, s0, S1, err;unsigned Char TMP, *ptr;if (err = fread (DST, size, NMEMB, fp))! = Nmemb) {return err;} if (!is_little_endian () && size > 1) {//debug ("Big-endian \ n");p tr = (unsigned char*) dst;for (n=0; n<nmemb;    n++) {for (s0=0, s1=size-1; s0 < S1; s0++, s1--) {tmp = PTR[S0];    PTR[S0] = ptr[s1];  PTR[S1] = tmp; } ptr + = size;}} Else{//debug ("<span style=" Color:rgb (Wuyi, Wuyi, Wuyi); font-family: ' Courier New '; font-size:12px; line-height:24px; text-indent:24px; " >little</span>-endian \ n ");} return err;} void Dumpwavinfo (Wav_info wavinfo) {debug ("compressioncode:%d \ n", WavInfo.header.compressionCode);d ebug (" numchannels:%d \ n ", WavInfo.header.numChannels);d ebug (" samplerate:%d \ n ", wavInfo.header.sampleRate);d ebug (" bytespersecond:%d \ n ", WavInfo.header.bytesPerSecond);d ebug (" blockalign:%d \ n ", wavInfo.header.blockAlign);d ebug (" bitspersample:%d \ n ", wavInfo.header.bitsPerSample);}    int Wavinputopen (Wav_info *pwav, const char *filename) {signed int offset;    Wav_info *wav = pwav;      if (wav = = NULL) {Debug ("Unable to allocate WAV struct.\n");    Goto error;    } WAV-&GT;FP = fopen (filename, "RB"); if (WAV-&GT;FP = = NULL) {Debug ("Unable to open WAV file.      %s\n ", filename);    Goto error;  }/* RIFF marker */if (fread (& (Wav->header.rifftype), 1, 4, WAV-&GT;FP)! = 4) {debug ("couldn ' t read riff_id\n");  Goto error; /* Bad error "couldn ' t read riff_id" */}if (strncmp ("RIFF", Wav->header.rifftype, 4)} {Debug ("RIFF Descriptor not Found.\n "); Goto error;} Debug ("Find RIFF \ n");/* Read RIFF size. Ignored.    */Readheader (& (Wav->header.riffsize), 4, 1, WAV-&GT;FP);    Debug ("wav->header.riffsize:%d \ n", wav->header.riffsize); /* Wave marker to determine */if (fread (&wav->header.wavetype, 1, 4, WAV-&GT;FP)!=4) {debug ("couldn ' t read format\n      ");  Goto error; /* Bad error "couldn ' t read format" */} if (STRNCMP ("Wave", Wav->header.wavetype, 4)) {Debug ("Wave Chu      NK ID not found.\n ");    Goto error;      } debug ("Find WAVE \ n");/* FMT designator judgment */if (Fread (& (Wav->header.formattype), 1, 4, WAV-&GT;FP)! = 4) {      Debug ("couldn ' t read format_id\n");  Goto error; /* Bad error "couldn ' t read format_id" */} if (strncmp ("FMT", Wav->header.formattype, 3)) {Debug ("Fmt     Chunk format not found.\n ");    Goto error;   } debug ("Find FMT \ n");  Readheader (&wav->header.formatsize, 4, 1, WAV-&GT;FP); Ignored Debug ("Wav->header.formatsize:%d \ n ", wav->header.formatsize);/* Read Info */readheader (& (Wav->header.compressioncode), 2, 1, WAV-&GT;FP); Readheader (& (Wav->header.numchannels), 2, 1, WAV-&GT;FP); Readheader (& (wav-> Header.samplerate), 4, 1, WAV-&GT;FP); Readheader (& (Wav->header.bytespersecond), 4, 1, WAV-&GT;FP); ReadHeader ( & (Wav->header.blockalign), 2, 1, WAV-&GT;FP); Readheader (& (Wav->header.bitspersample), 2, 1, WAV-&GT;FP   ); offset = wav->header.formatsize-16; /* WAV Format Extensible */if (Wav->header.compressioncode = = 0xFFFE) {static const unsigned char guidpcm[16] = {0 X01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}; unsigned short extraformatbytes, validbitspersample; unsigned char guid[16]; signed int i; /* Read Extra bytes */readheader (& (Extraformatbytes), 2, 1, WAV-&GT;FP); Offset-= 2;if (extraformatbytes >=) {Readheader (& (Validbitspersample), 2, 1, WAV-&GT;FP); Readheader (& (Wav->channelmask), 4, 1, WAV-&GT;FP); Readheader (& GUID), 1, WAV-&GT;FP);/* Check for PCM guid */for (i = 0; I & Lt 16; i++) if (guid[i]! = Guidpcm[i]) break;if (i = =) Wav->header.compressioncode = 0x01;offset-= 22;}}    Debug ("wav->header.compressioncode:%d \ n", Wav->header.compressioncode); /* Skip Rest of the FMT header if any.    */for (; offset > 0; offset--) {fread (&wav->header.formatsize, 1, 1, WAV-&GT;FP); } #if 1 Do {/* Read data chunk ID */if (fread (Wav->header.datatype, 1, 4, WAV-&GT;FP)! = 4) {Debug ("unable      To read data chunk id.\n "), free (WAV); goto error;} /* Read chunk length. */Readheader (&offset, 4, 1, WAV-&GT;FP);/* Check for data chunk signature. */if (strncmp ("Data", Wav->header.datatype, 4) = = 0) {Debug ("Find data \ n"); wav->header.datasize = Offset;break;} /* Jump over non data chunk.    */for (; offset > 0; offset--) {fread (& (Wav->header.datasize), 1, 1, WAV-&GT;FP);} } while (!feof (wAV-&GT;FP));    Debug ("wav->header.datasize:%d \ n", wav->header.datasize);        #endif/* Return success */return 0;/* Error path */error:if (WAV) {if (WAV-&GT;FP) {        Fclose (WAV-&GT;FP);      WAV-&GT;FP = NULL;    }//free (WAV); } return-1; } #if 0int Main (int Argc,char **argv) {wav_info Wavinfo;char filename[128];if (argc<2 | | strlen (&argv[1][0]) >= sizeof (fileName) {debug ("Argument Error!!! \ n "); return-1;} Debug ("Size:%d \ n", sizeof (Wav_header)); strcpy (filename,argv[1]); Wavinputopen (&wavinfo, fileName); return 0;} #endif

attached: Fiff file Knowledge points1. Introduction RIFF is all called the Resource Interchange File format ( resourcesinterchange fileformat), RIFF Files are a file structure that is followed by most multimedia files in the Windows environment, the data types contained in the riff file are identified by the file's extension, and the data that can be stored in the riff file includes: Audio Video interleaved Format data (. AVI) waveform format data (. WAV) bitmap Format data (. RDI) MIDI Format data (. RMI) palette format (. PAL) Multimedia Movies (. RMN) animation cursor (. ANI) other riff files (. BND)2. CHUNKChunk is the basic unit that makes up the riff file, and its basic structure is as follows:struct chunk{u32 ID;/* block flag * /u32 size;/* Block sizes * /U8 Dat[size]; */* block content * /};The ID consists of 4 ASCII characters that identify the data contained in the block. such as: ' RIFF ', ' LIST ', ' fmt ', ' data ', ' WAV ', ' AVI ' and so on, since this file structure was originally defined by Microsoft and IBM for PC, the RIFF file is in accordance with Little-endian[2] byte-order write. a size (block size) is the length of the data stored in the database field, and the size of the ID and Size field is not included within the value. The data contained in DAT (block content) is arranged in Word (word), and if the data structure length is odd, an empty (null) byte is added at the end. There are only two types of blocks in the chunk block: ' RIFF ' and ' LIST ' types can contain other blocks, while other blocks can contain only data. the chunk structure of the ' RIFF ' and ' LIST ' types is as followsstructchunk{u32 ID;/* block flag * /u32 size;/* Block sizes * // * dat at this time = type + Restdat * /u32 type; * * Types * /U8 restdat[size]/* dat the remaining data except for the Type4 word * /};As you can see, ' RIFF ' and ' LIST ' are also chunk, except that its DAT consists of two parts, type and Restdat. Type, consisting of 4 ASCII characters, represents the type of the riff file, such as ' WAV ', ' AVI ', or ' list ' block type, such as an AVI file listing in ' Hdrl ', ' movi '. data remaining in Restdat,dat except Type4, including block content, containing several chunk and ' LIST '2.1 FOURCC a FOURCC( fourcharacter code) is a 4-byte data, typically representing 4 ASCII characters. In the RIFF file format, FOURCC is very common, the ID member in Structchunk, ' list ', the type member of ' RIFF ', and the starting identifier are all expressed in FOURCC . FOURCC is generally four characters, such as ' ABCD ' in this form, or three characters can contain a space, such as ' abc ' in this form. the filedata part of the riff file consists of several ' list ' and chunk, and the ' list ' Listdata can be made up of several ' list ' and chunk, meaning ' list ' can be nested. ' RIFF ', FileType, ' LIST ', listtype,chunkid are all FOURCC, even if the type is identified with a 4-byte ASIIC character. Filesize,listsize,chunksize is a little-endian32-bit positive integer that represents the size of type (only ' RIFF ', ' LIST ' chunk have type) +data together, Note that it is Little-endian represented, such as: 0x00123456, storage address from low to high, storage in Little-endian system represented as 0x56341200 (bytes from low to high storage), While the Big-endian is 0x00123456 (bytes are stored from high to low). 32bit integer 0x00123456 storage address low--------->; high Little-endian (bytes from low to high storage) 56341200big-endian (bytes from high to low) 00123456


WAV Audio File Header parsing

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.