Reference Documentation:
An open source code on the 1:github: Click to open the link, because the storage audio needs to write the file header itself, so it is borrowed from its file storage function.
2: A very useful post code: Click to open the link, although a bit small flaws, but there are answers.
3: A Description Class Blog: Click the Open link
Here add some instructions: Windows under the multithreading as far as possible with _beginthreadex instead of createthread, online reason is very detailed. Second, when the recording is turned off, do not buffer the WaveIn device, otherwise it is easy to generate a deadlock. The sample code is as follows:
Create the code that terminates the copy thread: (as indicated in the blog above, you cannot write data to a file in the callback function of the recording, so you need to create a thread and copy it separately)
BOOL Stoprecording (const char *filename, int namesize) {char *filenametrue = (char *) malloc (sizeof (char) * (namesize+1)); strcpy_s (filenametrue,namesize+1, fileName);//stop recording unsigned threadid;haudio_detect_thread = (HANDLE) _beginthreadex ( NULL, 0, &audio_detect, filenametrue, 0, &threadid); WaitForSingleObject (Haudio_detect_thread, INFINITE); CloseHandle (Haudio_detect_thread); return true;}
Replace the buffer module code for the WaveIn device, note that the buffer module should be at least 4, too little to cause noise and other problems
void Processsamples () {if (brecordingstarted) {Waveinunprepareheader (Hwavein, Waveheaders[ibuffidx], sizeof (WAVEHDR) ); Ibuffidx ++;if (Ibuffidx = = BUFFERCOUNT) {ibuffidx = 0;} Waveheaders[ibuffidxt]->dwbufferlength = wavebufsize; waveheaders[ibuffidxt]->dwflags = 0; Waveheaders[ibuffidxt]->dwloops = 0;waveinprepareheader (Hwavein, Waveheaders[ibuffidxt], sizeof (WAVEHDR)); Waveinaddbuffer (Hwavein, Waveheaders[ibuffidxt], sizeof (WAVEHDR)); if (ibuffidxt = = BUFFERCOUNT) {ibuffidxt = 0;}}}
A function that writes data, primarily invoking the code on GitHub
void WriteData (char *name) {//Stop recording printf ("Enter the function inside the write, the name is%s\n", name); brecordingstarted = False;waveinreset ( Hwavein); int stopcode = Waveinstop (Hwavein);p rintf ("The Stopcode is%d\n", stopcode); Coutputfile *file = Null;file = new Cwaveoutputfile;if (!file->create (name, m_waveformin)) {printf ("initialization is wrong \ n");} printf ("Writing data \ n"); File->writedata (M_audiodatapool.lpdata, M_audiodatapool.dwlength);p rintf ("The length of m_ Audiodatapool.length is%d ", m_audiodatapool.dwlength); File->flush ();//Release resources free (name); Audiodatapool.lpdata); m_audiodatapool.lpdata = Null;m_audiodatapool.dwlength = 0;}
Recording Audio under Windows