[Transferred from: http://blog.csdn.net/zhi_qiu_yi_ye/article/details/6663133]
Using the wave API for Windows Audio programming can maintain a great degree of freedom, and it is similar to the OSS programming mode in Linux. Next, I will briefly introduce the function calling sequence. For detailed usage, see msdn (vs2008 ).
Waveinopen-> waveinprepareheader-> waveinaddbuffer-> waveinstart-> waveinstop-> waveinreset-> waveinunprepareheader-> waveinclose
The callback function of the recording only needs to process the wim_data message, copy the data, and call waveinaddbuffer again to add the cache to the queue.
Waveoutopen-> waveoutprepareheader-> waveoutwrite-> waveoutreset-> waveoutunprepareheader-> waveoutclose
The following is the source code of a Win32 console program. In the vs2008 environment, create an empty Win32 console project and add this file for compilation. The function is to record and play back the program for 5 seconds. Do not select the Unicode Character Set. Otherwise, the device name may contain garbled characters.
# Include "stdafx. H "/// waveapi. CPP // by: Zhiqiu yiye // # include <windows. h> # include <stdio. h> # include <stdlib. h> # include <mmsystem. h> # pragma comment (Lib, "winmm. lib ") # define buffer_size (44100*16*2/8*5) // recording sound length # define fragment_size 1024 // cache size # define fragment_num 4 // Number of cache areas static unsigned char buffer [buffer_size] = {0 }; static int buf_count = 0; // Function Definition void callback waveinproc (hwavein Hwi, uint umsg, dwor D_ptr dwinstance, dword_ptr dwparam1, dword_ptr dwparam2); void callback (jwhwo, uint umsg, dword_ptr dwinstance, dword_ptr dwparam1, dword_ptr dwparam2); // entry int main () {/* recording * // deviceint nreturn = waveingetnumdevs (); printf ("Number of input devices: % d \ n", nreturn); For (INT I = 0; I <nreturn; I ++) {waveincaps WIC; waveingetdevcaps (I, & WIC, sizeof (waveincaps); printf ("# % d \ t device name: % s \ n ", I, WIC. szpname );} // Openhwavein hwavein; waveformatex wavform; wavform. wformattag = wave_format_pcm; wavform. nchannels = 2; wavform. nsamplespersec = 44100; wavform. navgbytespersec = 44100*16*2/8; wavform. nblockalign = 4; wavform. wbitspersample = 16; wavform. cbsize = 0; waveinopen (& signature, wave_mapper, & wavform, (dword_ptr) waveinproc, 0, callback_function); waveincaps WIC; fill (uint_ptr) hwavein, & WIC, sizeof (Waveincaps); printf ("opened input device: % s \ n", WIC. szpname); // prepare bufferstatic wavehdr wh [fragment_num]; for (INT I = 0; I <fragment_num; I ++) {wh [I]. lpdata = new char [fragment_size]; wh [I]. dwbufferlength = fragment_size; wh [I]. dwbytesrecorded = 0; wh [I]. dwuser = NULL; wh [I]. dwflags = 0; wh [I]. dwloops = 1; wh [I]. lpnext = NULL; wh [I]. reserved = 0; waveinprepareheader (hwavein, & wh [I], sizeof (wavehdr); waveinaddbuffer (Hwavein, & wh [I], sizeof (wavehdr);} // recordprintf ("start to record... \ n "); buf_count = 0; waveinstart (hwavein); While (buf_count <buffer_size) {sleep (1);} printf (" record over! \ N "); // cleanwaveinstop (hwavein); waveinreset (hwavein); For (INT I = 0; I <fragment_num; I ++) {waveinunprepareheader (hwavein, & wh [I], sizeof (wavehdr); Delete wh [I]. lpdata;} waveinclose (hwavein); System ("pause"); printf ("\ n");/* Playing * // devicenreturn = waveoutgetnumdevs (); printf ("\ n number of output devices: % d \ n", nreturn); For (INT I = 0; I <nreturn; I ++) {waveoutcaps WOC; waveoutgetdevcaps (I, & WOC, sizeof (waveoutcaps); printf ("# % D \ t device name: % s \ n ", I, WIC. szpname);} // openhwaveout hwaveout; contains (& hwaveout, wave_mapper, & wavform, (dword_ptr) values, 0, callback_function); contains WOC; contains (uint_ptr) hwaveout, & WOC, sizeof (waveoutcaps); printf ("On output device: % s \ n", WIC. szpname); // prepare bufferwavehdr wavhdr; wavhdr. lpdata = (lpstr) buffer; wavhdr. dwbufferlength = buffer_size; wavhdr. dwflags = 0; wavhdr. dwloops = 0; waveoutprepareheader (hwaveout, & wavhdr, sizeof (wavehdr); // playprintf ("start to play... \ n "); buf_count = 0; waveoutwrite (hwaveout, & wavhdr, sizeof (wavehdr); While (buf_count <buffer_size) {sleep (1 );} // cleanwaveoutreset (hwaveout); waveoutunprepareheader (hwaveout, & wavhdr, sizeof (wavehdr); waveoutclose (hwaveout); printf ("play over! \ N "); Return 0;} // recording callback function void callback waveinproc (hwavein Hwi, uint umsg, dword_ptr dwinstance, dword_ptr dwparam1, dword_ptr dwparam2) {lpwavehdr pwh = (lpwavehdr) dwparam1; If (wim_data = umsg) & (buf_count <buffer_size) {int temp = buffer_size-buf_count; temp = (temp> pwh-> dwbytesrecorded )? Pwh-> dwbytesrecorded: temp; memcpy (buffer + buf_count, pwh-> lpdata, temp); buf_count + = temp; waveinaddbuffer (HWI, pwh, sizeof (wavehdr ));}} // void callback (hwaveout hwo, uint umsg, dword_ptr dwinstance, dword_ptr dwparam1, dword_ptr dwparam2) {If (callback = umsg) {buf_count = buffer_size ;}}