a voice test

2017.12.13

 

      A little homework on speech.

      As a result of work, I started to get in touch with a lot of things about speech, and one of the main feelings is that the knowledge has grown. Below is a summary of the completed work. 

       wav and pcm files are the two most common audio files, let's understand the difference between them.

       WAV: is a lossless audio file format that conforms to the PIFF specification. All wavs have a file header, the encoding parameters of this file header audio stream.

       PCM: Pulse Modulation Recording. That is, the analog signal such as sound is converted into a symbolized pulse train, and then recorded. A PCM signal is a digital signal composed of symbols such as [1] and [0] without any encoding and compression processing.

       The relationship between WAV and PCM

       pcm is an encoding method for audio data in a lossless wav file, but wav can also be encoded in other ways.

       Format of wav audio file:

       The wav file consists of two parts: the file header and the sampled data.

The file header is further divided into RIFF, WAVE file identification segment and sound format description segment. The starting address of each segment is demarcated by RIFF identifier and waveform format identifier (FMT).

       

        This picture is the meaning of the first 44 bytes of WAV. Generally, these 44 bytes are removed when calculating speech. If you want to know more, click the link: http://soundfile.sapp.org/doc/WaveFormat/

        Here is this little exercise:

         1. Given a wav file, it can read the audio sampling rate, sampling accuracy, number of channels and other information.

        2. Calculate the size of the voice file on the basis of 1, and also calculate the audio playback time.

        

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

struct WAV_Format { //Define a structure WAV file
	uint32_t ChunkID; /* "RIFF" resource swap file flag*/
	uint32_t ChunkSize; /* The total number of bytes from the next address to the end of the file*/
	uint32_t Format; /* "WAVE file flag" */

	/* 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, by tc number of channels, single channel is 1, dual channel is 2 */
	uint32_t SampleRate; /* 8000, 44100, by tc sampling rate*/
	uint32_t ByteRate; /* = SampleRate * NumChannels * BitsPerSample/8 Waveform data transfer rate */
	uint16_t BlockAlign;	/* = NumChannels * BitsPerSample/8 */	
	uint16_t BitsPerSample;	/* 8bits, 16bits, by tc */

	/* sub-chunk "data" */
	uint32_t Subchunk2ID;	/* "data" */
	uint32_t Subchunk2Size;	/* data size */

	uint32_t FileSize;
	uint32_t Time;//Play time
};

int main(void)
{
	FILE *fp = NULL;
	struct WAV_Format wav;

	fp = fopen("C:\\Users\\admin\\Desktop\\female.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);//Block size
	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);

	wav.FileSize = (96000*wav.NumChannels*6)/8;
	printf("The amount of data is:\t%d B\n",wav.FileSize);
	wav.Time= wav.Subchunk2Size / wav.ByteRate;
	printf("The audio playback time is:\t%d seconds\n",wav.Time);

	fclose(fp);

	return 0;
}

      The result is as follows:

       

 

        In addition, why use uint32_t to define in the text, mainly can effectively maintain the code.

        You can refer to the blog: http://blog.csdn.net/kiddy19850221/article/details/6655066

        over, go to the gym.

        
 
 
  
       
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326298567&siteId=291194637