Recording Audio under Windows

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.