Realization of recording wav file with AD inside single chip

1. Hardware design

The simplest electret microphone is used for sound collection, and the signal is amplified and filtered and then input to the internal AD port of the microcontroller. The circuit diagram is as follows, the first stage operational amplifier is used for amplification, and the second stage is low-pass filtering. (The resistance and capacitance parameters are for reference only).

image

After the one-chip computer collects the sound data, it saves the data to the SD card in WAV format.

2. WAV file format description

WAV (Waveform audio format) is a sound encoding format developed by Microsoft and IBM. It conforms to the RIFF (Resource Interchange File Format) file specification. It is used to save audio information resources on the Windows platform and is used by the Windows platform and its applications. Widely supported.

The first four bytes of each WAV file is "RIFF". WAV files are composed of two parts: file header and data body. The file header is divided into RIFF block and FORMAT block. The contents and format of each part of the WAV file are shown in the table below.

RIFF block

image

Where Size is the length of the entire file minus the length of ID and Size

FORMAT block

image

       The audio format can be linear PCM or ADPCM, etc. In this example, linear PCM (uncompressed) is used.

DATA block

image

Among them, Size represents the length of audio data, in bytes.

Audio data.

3. Software design

The human ear can recognize the sound range of 20-20kHz. If you want to collect sound signals with frequencies within 20kHz, the sampling rate must be at least 40kHz, the amount of data is relatively large, and when the resources of the single-chip microcomputer are limited, data processing and storage may not be completed.

Normal people speak (not sing) frequency: male voice 50-500Hz, female voice 100-1kHz. Taking into account the requirements of harmonics and Nyquist sampling theorem, 8k sampling rate can meet the requirements. This is also the sampling rate in many digital phones. The actual test 8k sampling rate can restore the voice more clearly.

The number of sampling bits can be 8bits or 16bits. The internal AD of the microcontroller is 12 bits, occupying 2 bytes, which can be regarded as 16 bits of data. In order to reduce the amount of data, the lower 4 bits of AD are discarded and treated as 8-bit data. Actually testing the effect of 8-bit data is fine.

system initialization

Enable the internal AD of the single-chip microcomputer, trigger AD sampling through timer events to ensure the accuracy of the sampling rate, and enable the DMA channel to reduce the CPU usage rate. Enable the SPI interface to drive the SD card and transplant the FatFs file system. The steps are not described in detail here, you can refer to the article before the publicization.

Programming

AD sampling adopts double buffering method. When DMA finishes buffer 1, set the target address of DMA to buffer 2 to continue sampling, and at the same time save the data in buffer 1 to the file, so that the two buffers are saved alternately to ensure The continuity of sampled data.

The data before the audio data is defined as a structure, initialized and saved at the beginning of the file. The length of the data has not been determined yet, and it needs to be recalculated and written.

//初始化WAV头. void recoder_wav_init(WaveHeader* wavhead) //初始化WAV头              {     wavhead->riff.ChunkID=0X46464952;    //"RIFF"     wavhead->riff.ChunkSize=0;           //还未确定,最后需要计算     wavhead->riff.Format=0X45564157;     //"WAVE"     wavhead->fmt.ChunkID=0X20746D66;     //"fmt "     wavhead->fmt.ChunkSize=0x10;           //大小为16个字节     wavhead->fmt.AudioFormat=0X01;       //0X01,表示PCM;0X11,表示IMA ADPCM     wavhead->fmt.NumOfChannels=1;        //单声道     wavhead->fmt.SampleRate=8000;        //8Khz采样率 采样速率     wavhead->fmt.ByteRate=8000;                                    //字节速率=采样率*通道数*(ADC位数/8)    wavhead->fmt.BlockAlign=1;           //块大小,1个字节为一个块     wavhead->fmt.BitsPerSample=8;       //8位PCM     wavhead->data.ChunkID=0X61746164;    //"data"     wavhead->data.ChunkSize=0;           //数据大小,还需要计算   
    res=f_open(&file_rec,(const TCHAR*)FileName, FA_CREATE_ALWAYS | FA_WRITE);    if(res==FR_OK)    {             res=f_write(&file_rec,(const void*)(&wavhead),sizeof(WaveHeader),&bw);//写入头数据    }}

After each completion of DMA, the data is saved once. It is generally recommended to write in integer multiples of the SD card sector to increase the write speed. The more data written at one time, the faster the average write speed.

res=f_write(&file_rec,Databuf,512,&bw);//写入数据

After recording, recalculate the file length and write:

wavhead.riff.ChunkSize=sectorsize*512+36;       //整个文件的大小-8;wavhead.data.ChunkSize=sectorsize*512;              //数据大小res = f_lseek(&file_rec,0);                                                 //偏移到文件头.res = f_write(&file_rec,(const void*)(&wavhead),sizeof(WaveHeader),&bw);//写入头数据res = f_close(&file_rec);
​​​​​​​Welcome to pay attention to the public account "embedded technology development", you can leave me a message in the background to communicate. If you think this official account is helpful to you, you are also welcome to recommend it to others.

 

image

Guess you like

Origin blog.csdn.net/zhang062061/article/details/113001281