Click here to Skip to main content
15,888,033 members
Articles / Programming Languages / C++
Article

Simple audio recording program

Rate me:
Please Sign up or sign in to vote.
4.18/5 (31 votes)
12 Jul 20071 min read 402K   18.5K   83   109
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


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

Comments and Discussions

 
GeneralRe: RAW PCM file extension. Pin
Saneesh15-Jan-07 7:36
Saneesh15-Jan-07 7:36 
QuestionTwo sound cards Pin
Baracat15-Oct-06 20:09
Baracat15-Oct-06 20:09 
AnswerRe: Two sound cards Pin
Saneesh16-Oct-06 8:49
Saneesh16-Oct-06 8:49 
GeneralRe: Two sound cards Pin
Conmen.Tsai7-Mar-07 20:00
Conmen.Tsai7-Mar-07 20:00 
GeneralRe: Two sound cards Pin
Saneesh8-Mar-07 8:08
Saneesh8-Mar-07 8:08 
GeneralI have an strange Problem Pin
Hung In27-Jun-06 1:28
Hung In27-Jun-06 1:28 
GeneralRe: I have an strange Problem Pin
Saneesh27-Jun-06 4:07
Saneesh27-Jun-06 4:07 
GeneralRe: I have an strange Problem [modified] Pin
Hung In27-Jun-06 15:56
Hung In27-Jun-06 15:56 
OK, I will show you the details here:
My PC has 2 sound devices: RealTek (on-board), SetB4 (USB-Phone)

- In CONTROL_PANEL/SOUND, at Tab Audio, If I Set default devices in turn are RealTek-RealTek or SetB4-SetB4 then no problem occurs!

- But if I set default devices are RealTek-SetB4 or SetB4-RealTek then problem occurs!

The program wrote the log when it was running as follow:

(When set SetB4-RealTek in Control Panel/Sound)

+ Add device 0, name = Realtek AC97 Audio
+ Add device 1, name = SetB4
+ SET device index = 0, name = Realtek AC97 Audio => Set device to play
+ Play sound on device = 0 (Use waveOutOpen to open "index" device)
=> But now I hear the sound on USB-Phone!

And in other case the sound is heared from RealTek, though i choosed USB:
+ Add device 0 name = Realtek AC97 Audio
+ Add device 1 name = SetB4
+ SET device index = 1, name = SetB4
+ Play sound on device = 1
=> Sound was heared from RealTek

I think changing default device in Windows makes the change in index of playing device, though it does not change the order of device when using GetNumDevs to get from the system! => Device Order in system is not the same as playing-device order??? Dead | X|

So can you help me explain this again?
D'Oh! | :doh:
THis is excel file describing this problem in detail: http://n.domaindlx.com/hunginday/setVolume.xls



-- modified at 23:51 Tuesday 27th June, 2006
GeneralRe: I have an strange Problem Pin
Hung In3-Jul-06 19:01
Hung In3-Jul-06 19:01 
GeneralError when free memory [modified] Pin
Hung In20-Jun-06 16:26
Hung In20-Jun-06 16:26 
GeneralRe: Error when free memory Pin
Saneesh22-Jun-06 16:45
Saneesh22-Jun-06 16:45 
GeneralRe: Error when free memory Pin
Hung In22-Jun-06 17:34
Hung In22-Jun-06 17:34 
QuestionPlayer? Pin
Hung In24-May-06 20:47
Hung In24-May-06 20:47 
AnswerRe: Player? Pin
Saneesh25-May-06 10:48
Saneesh25-May-06 10:48 
GeneralAbout wave audio control Pin
hslings16-May-06 0:56
hslings16-May-06 0:56 
General# of buffers is hardcoded to 3 in UnprepareBuffers Pin
codeproject4sba8-Apr-06 2:27
codeproject4sba8-Apr-06 2:27 
GeneralRe: # of buffers is hardcoded to 3 in UnprepareBuffers Pin
Saneesh8-Apr-06 4:41
Saneesh8-Apr-06 4:41 
QuestionVC6: works fine. Now, how to add a VU meter? Pin
fabrizio.carraro23-Jan-06 3:43
fabrizio.carraro23-Jan-06 3:43 
AnswerRe: VC6: works fine. Now, how to add a VU meter? Pin
Saneesh23-Jan-06 8:00
Saneesh23-Jan-06 8:00 
Questioncannot compile with VC6 Pin
fabrizio.carraro20-Jan-06 6:35
fabrizio.carraro20-Jan-06 6:35 
AnswerRe: cannot compile with VC6 Pin
Saneesh20-Jan-06 12:25
Saneesh20-Jan-06 12:25 
GeneralRe: cannot compile with VC6 Pin
Saneesh20-Jan-06 12:28
Saneesh20-Jan-06 12:28 
QuestionCould I contact you via e-mail? Pin
louisphilip19-Jan-06 14:25
louisphilip19-Jan-06 14:25 
AnswerRe: Could I contact you via e-mail? Pin
Saneesh19-Jan-06 14:41
Saneesh19-Jan-06 14:41 
Generalaudio device input listbox Pin
jan marco5-Jan-06 21:14
jan marco5-Jan-06 21:14 

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.