In Windows, audio functions include multiple types, such as MCI, multimedia ole control, and advanced audio, which are easy to use. However, if you want to write a powerful audio processing program, you must use low-level audio functions and multimedia file I/O to control the input and output of the audio device. Because low-level audio functions can directly interact with the audio driver, the window message or call back function is used to manage recording and playing of audio data blocks, and the control is flexible. The important thing is that low-level audio functions provide a device-independent interface.
Play Digital Audio
You must open the device driver before using the audio device. To ensure reliability, you must determine the capabilities of the system audio device before playing and recording audio. You must disable the audio device after use. The specific process is as follows:
1. query the number and capability of devices
Use waveoutgetnumdevs and waveoutgetdevcaps to obtain the number and capability of waveform output devices. The device can be opened and used only after the device is confirmed to exist.
2. Turn on the waveform output device
Use waveoutopen to enable a specific waveform device for replay. This function opens the device associated with the specified device ID, and returns the handle that opens the waveform device by giving the specified memory handle.
When there are multiple waveform output devices, we recommend that you use the wave_mapper constant as the device ID so that the waveoutopen function automatically selects the device that is most suitable for playing the specified data format.
3. Prepare audio data blocks
Prepare audio data blocks before waveform replay. Data blocks are transmitted to the device driver for replay. The function used is waveoutperpareheader. After using the data block, you must use the waveoutunprepareheader function to clear the preparation for the waveform data block.
4. Send audio data blocks
After the waveform output device is successfully enabled, you can perform waveform replay and use the waveoutwrite function. After calling this function, you must wait until the device driver has used the audio data block before releasing the data block.
5. Manage waveform replay
When playing audio using a low-level audio function, the application must continuously provide data blocks to the device driver until the playing ends. Windows provides two methods to manage waveform replay: window message management and low-level callback function management. In addition, you can pause, restart, and stop playing by using waveoutpause, waveoutrestart, and waveoutreset.
6. Disable waveform devices.
After using the device, you must call the waveoutclose function to disable the waveform output device so that other programs can use the device.
Recording Digital Audio
The method for Recording Digital Audio is basically the same as the playing process. The difference is that the control such as pause and restart are not provided during the recording. The functions used include: waveingetdevcaps, waveinopen, waveinprepareheader, waveinunprepareheader, waveinaddbuffer, waveinreset, waveinstart, waveinstop, and waveinclose.