当前位置:网站首页>Format analysis and explanation of wav file

Format analysis and explanation of wav file

2022-06-25 09:04:00 u013250861

WAV File is in PC It is very common on the platform 、 The most classic multimedia audio file , As early as 1991 year 8 The moon appears in Windows 3.1 On the operating system , The file extension is WAV, yes WaveFom Abbreviation , Also known as waveform file , It can directly store sound waveform , The restored waveform curve is very realistic .

WAV File format abbreviation WAV Format is a digital audio format that stores sound waveforms , It's Microsoft and IBM Jointly designed , After many revisions , Can be used for Windows,Macintosh,Linix And many other operating systems , Details are as follows .

One 、 Basic knowledge of waveform file

1、 Stored procedure of waveform file

The sound from the sound source is converted into a continuously changing electrical signal through the microphone , After zooming in 、 After anti aliasing filtering , Sample at a fixed frequency , Each sample is the amplitude value of electrical signal detected in a sampling period ; Next, it is quantized from an analog electrical signal into an integral value represented by a binary number ; Finally encoded and stored as audio stream data .

Some applications save storage space , Before storage , And compress the sampling data first .

2、WAV Coding of documents

The coding includes two aspects , One is to store data in a certain format , The second is to compress data with certain algorithm .

WAV The format has no hard rules for the encoding of audio stream , Supports uncompressed PCM(Puls Code Modulation) Pulse code modulation format , It also supports Microsoft adaptive sub pulse code modulation of compression type Microsoft ADPCM(Adaptive Differential Puls Code Modulation)、 The International Telegraph Union (International Telegraph Union) The voice compression standard ITUG.711 a-law、ITU G.711-law、IMA ADPCM、ITU G.723 ADPCM (Yamaha)、GSM 6.10、ITU G.721 ADPCM Coding and other compression algorithms .MP3 Coding can also be used in WAV in , Just install the corresponding Decode, You can play WAV Medium MP3 music .
 Insert picture description here
The parsing code is as follows :

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
 
struct WAV_Format {
    
	uint32_t ChunkID;	/* "RIFF" */
	uint32_t ChunkSize;	/* 36 + Subchunk2Size */
	uint32_t Format;	/* "WAVE" */
 
	/* sub-chunk "fmt" */
	uint32_t Subchunk1ID;	/* "fmt " */
	uint32_t Subchunk1Size;	/* 16 for PCM */
	uint16_t AudioFormat;	/* PCM = 1*/
	uint16_t NumChannels;	/* Mono = 1, Stereo = 2, etc. */
	uint32_t SampleRate;	/* 8000, 44100, etc. */
	uint32_t ByteRate;	/* = SampleRate * NumChannels * BitsPerSample/8 */
	uint16_t BlockAlign;	/* = NumChannels * BitsPerSample/8 */	
	uint16_t BitsPerSample;	/* 8bits, 16bits, etc. */
 
	/* sub-chunk "data" */
	uint32_t Subchunk2ID;	/* "data" */
	uint32_t Subchunk2Size;	/* data size */
}; 
 
int main(void)
{
    
	FILE *fp = NULL;
	struct WAV_Format wav;
 
	fp = fopen("test.wav", "rb");
	if (!fp) {
    
		printf("can't open audio file\n");
		exit(1);
	}
 
	fread(&wav, 1, sizeof(struct WAV_Format), fp);
 
	printf("ChunkID \t%x\n", wav.ChunkID);
	printf("ChunkSize \t%d\n", wav.ChunkSize);
	printf("Format \t\t%x\n", wav.Format);
	printf("Subchunk1ID \t%x\n", wav.Subchunk1ID);
	printf("Subchunk1Size \t%d\n", wav.Subchunk1Size);
	printf("AudioFormat \t%d\n", wav.AudioFormat);
	printf("NumChannels \t%d\n", wav.NumChannels);
	printf("SampleRate \t%d\n", wav.SampleRate);
	printf("ByteRate \t%d\n", wav.ByteRate);
	printf("BlockAlign \t%d\n", wav.BlockAlign);
	printf("BitsPerSample \t%d\n", wav.BitsPerSample);
	printf("Subchunk2ID \t%x\n", wav.Subchunk2ID);
	printf("Subchunk2Size \t%d\n", wav.Subchunk2Size);
 
	fclose(fp);
 
	return 0;
}

ByteRate The calculation method is SampleRate * NumChannels * BitsPerSample/8, So what this formula calculates is 1 Second audio data size , Unit is Byte, With 44100、16bit、2 Channel, for example , Then the calculated value is 176400, According to the total audio data size , It can be concluded that the calculation formula of playing time is Subchunk2Size / ByteRate.

surface 1

Block identification (4Bytes)
Block length (4Bytes)
data



Reference material :
wav File format analysis and detailed explanation
wav Audio file format analysis

原网站

版权声明
本文为[u013250861]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250736418033.html