Click here to Skip to main content
13,349,502 members (51,968 online)
Click here to Skip to main content
Add your own
alternative version

Stats

312.5K views
15.4K downloads
78 bookmarked
Posted 1 Jan 2006

Simple audio recording program

, 12 Jul 2007
Rate this:
Please Sign up or sign in to vote.
Simplest way to record sound...

Sample Image - SoundRecord.jpg

Introduction

This sample program make use of waveInxxxx functions to read from an audio input device like a sound card and write it to a .wav file using mmioxxx functions. It is one of the simplest way to record sound with all the format capabilities supported by the hardware device

Initially, by using waveInGetNumDevs, we can find all the devices and then by using waveInGetDevCaps, we can find the formats it is supported.

To start recording, first select a device and then open the device with waveInOpen API call with the specified format (i.e. 16 bit etc). The output file is also prepared with all the headers filled in properly. Here i am using only PCM format which is easy. If you have more codecs installed on your PC, you can change that one also. But be sure that you supplied the proper values in the WAVEFORMATEX structure.

waveIn needs buffers to start recording. It is better to give more buffers for buffering to avoid any delays. I have used 3 here, but you can add more if your PC needs more time in writing to the HDD and thus causing the delay. Once the buffers are added in the queue, waveInStart will start recording. For each data chunk, the callback function specified in the waveinOpen will get the buffers. Just write it to the file and mark the buffer again for recording by calling the same addbuffer fn.

Once done with recording, adjust the chunk headers using the mmioascend APIs. Thats it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Saneesh
Software Developer (Senior)
United States United States
I am from the beautiful sea side town called Kochi ( cochin), Kerala, South India.

You may also be interested in...

Comments and Discussions

 
BugWorks, but is beginner code and several issues to be fixed Pin
Elmue13-Oct-14 16:30
memberElmue13-Oct-14 16:30 
Hello

Your program works, but the code shows your lack of programming experience.

1.)
You have mixed the demo code with the wave class.
This is a typical beginner error.

If someone wants to reuse your code he has to rewrite everything.

The correct design would be to write a class that handles all the functions in WinMM like opening a device, opening a WAV file, start recording, stop recording, and close all.

And apart from that you should have written a demo that shows how to use that class.
This would have been reusable code.

But you read the text of the ComboBox and parse it with sscanf in the same function as you open the audio device.
This makes your code useless for someone who needs a class for sound recording without your GUI.
I had to rewrite everything and clean up your code.


2.)
The second thing is that you did not understand how Microsoft designed the callback.
You have implemented a thread that is TOTALLY useless.
The API waveInOpen() takes a callback that you use to write the WAV buffer to a file.
waveInOpen() is designed as an asynchronous function which has the sense that you do NOT need a thread as you would need for blocking functions.

Your thread does nothing else than wait until the user aborts it. There is no code in it.
This is complete nonsense.

Microsoft already did that work for us: waveInOpen() starts a new background thread.

3.)
Your error handling is sloppy.
In UnPrepareBuffers() you ignore error WAVERR_STILLPLAYING that is always returned by waveInUnprepareHeader(). There is a logical error in your program:
In ProcessHeader() you must not call waveInAddBuffer() after waveInStop() has been called. Set a flag before calling waveInStop() and when it is set do not call waveInAddBuffer() anymore.

Additionally you must call waveInReset() before calling UnprepareBuffers().

4.)
You use waveInGetErrorText() to translate error codes into error messages. This function is quite limited. It does not translate for example MMIOERR_CANNOTWRITE. Use mciGetErrorString() instead which translates all MCI errors.

5.)
Just for information: Most sound cards support a sample rate range from 8kHz to 192kHz.
This can be passed as integer to waveInOpen() which is not limited to the few values displayed in your comobobox (11,22,44,96 kHz)

modified 13-Oct-14 23:54pm.

GeneralRe: Works, but is beginner code and several issues to be fixed Pin
dalkeeper4-Mar-15 9:32
memberdalkeeper4-Mar-15 9:32 
GeneralMy vote of 3 Pin
glp332924-Nov-13 22:59
memberglp332924-Nov-13 22:59 
QuestionWhere/How does it write? Pin
Member 923082025-Jul-12 5:56
memberMember 923082025-Jul-12 5:56 
AnswerRe: Where/How does it write? Pin
Saneesh26-Jul-12 4:52
memberSaneesh26-Jul-12 4:52 
GeneralMy vote of 5 Pin
wserra6-Jul-12 22:06
memberwserra6-Jul-12 22:06 
GeneralMy vote of 2 Pin
arjo12930-Jun-12 15:59
memberarjo12930-Jun-12 15:59 
GeneralRe: My vote of 2 Pin
wserra6-Jul-12 22:08
memberwserra6-Jul-12 22:08 
GeneralRe: My vote of 2 Pin
Saneesh8-Jul-12 13:40
memberSaneesh8-Jul-12 13:40 
GeneralRe: My vote of 2 Pin
wserra8-Jul-12 18:00
memberwserra8-Jul-12 18:00 
GeneralCan you help? Pin
Member 779661522-Apr-11 20:44
memberMember 779661522-Apr-11 20:44 
GeneralWon't compile using Visual Studio 2010 (SP1) Pin
Michael B Pliam13-Apr-11 10:58
memberMichael B Pliam13-Apr-11 10:58 
GeneralRe: Won't compile using Visual Studio 2010 (SP1) Pin
Saneesh23-Apr-11 5:12
memberSaneesh23-Apr-11 5:12 
AnswerRe: Won't compile using Visual Studio 2010 (SP1) Pin
tom tom tom tom tom5-Oct-11 1:37
membertom tom tom tom tom5-Oct-11 1:37 
GeneralRe: Won't compile using Visual Studio 2010 (SP1) Pin
Saneesh5-Oct-11 5:45
memberSaneesh5-Oct-11 5:45 
Generalsource code Pin
sunilpatilgs16-Mar-11 20:01
membersunilpatilgs16-Mar-11 20:01 
Generalgetting waveform n samples Pin
sunilpatilgs11-Mar-11 0:28
membersunilpatilgs11-Mar-11 0:28 
GeneralRe: getting waveform n samples Pin
Saneesh11-Mar-11 6:58
memberSaneesh11-Mar-11 6:58 
GeneralRe: getting waveform n samples Pin
peppelisme5-Jun-11 8:17
memberpeppelisme5-Jun-11 8:17 
Questionhow to display wave form Pin
sunilpatilgs8-Mar-11 19:00
membersunilpatilgs8-Mar-11 19:00 
AnswerSanesh can you contact me? Pin
Member 77377838-Mar-11 22:16
memberMember 77377838-Mar-11 22:16 
GeneralRe: Sanesh can you contact me? Pin
Saneesh9-Mar-11 5:15
memberSaneesh9-Mar-11 5:15 
AnswerRe: how to display wave form Pin
Saneesh9-Mar-11 5:15
memberSaneesh9-Mar-11 5:15 
GeneralMy vote of 5 Pin
boyewa13-Sep-10 8:30
memberboyewa13-Sep-10 8:30 
Generalwriting audio to serial port Pin
abhishek50500426-Mar-10 21:27
memberabhishek50500426-Mar-10 21:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 12 Jul 2007
Article Copyright 2006 by Saneesh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid