WAVE recording

Waveform audio recording and playback are similar, and a callback mechanism is also required.

Basic process of WAVE recording

1. The query system uses an input device capable of recording waveform audio, and calls the waveInGetNumDevs function to obtain the number of devices;
2. Check the performance of the waveform audio output device to determine whether the device supports the desired recorded waveform audio format, and call the waveInGetDevCaps function;
3. Open the wave audio input device and call the waveInOpen function;
4. Construct the WAVEHDR structure of the WAVE audio information header and call the waveInPrepareHeader function;
5. Send a buffer to the wave audio device through the lpData parameter of WAVEHDR, and call the waveInAddBuffer function;
6. Call the waveInStart function to start recording;
7. Write the received WAVE data pointed to by lpData in the audio information header structure into the WAV file;
8. Clear the prepared audio information header structure WAVEHDR and release system resources, call the waveInUnprepareHeader function;
9. Close the wave audio input device and call the waveInClose function.

note

1. If the buffer sent by waveInAddBuffer does not return when the wave audio input device is turned off, the call to this function will fail. At this time, the waveInReset function can be called to discard all unused buffers and reset the input device. The waveInReset function will terminate the input and give up the unprocessed buffer and return it to the application. At this time, the dwBytesRecorded parameter in the WAVEHDR structure contains the length of the actual data. You can stop recording by calling the waveInStop function. If you need to continue recording, you can use the waveInStart function to restart recording.
2. Like WAVE playback, WAVE recording also needs to use a double buffer mechanism, otherwise the recorded file will sound to a pause.
3. MSDN has stated that the system call is strictly restricted in the callback function, so it is not appropriate to design schemes arbitrarily, such as directly writing audio data into a file. The usual practice is to design a data block linked list structure for buffering, which mostly involves multi-threaded programming.
4. The system calls the callback function in another thread (not a thread created by yourself), which will affect programs that use the TLS mechanism. If you are using MFC, the return value of functions like AfxGetApp will be incorrect.
5. If the recorded audio needs to be sent over the network, it generally needs to undergo audio processing, such as reducing the amount of data transmission through a compression algorithm, and performing silent detection.

Guess you like

Origin blog.csdn.net/gkzscs/article/details/53978813