Click here to Skip to main content
15,887,027 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 401.2K   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

 
Generaloutput wav can't play on Vista Pin
binsonl4-May-07 7:35
binsonl4-May-07 7:35 
GeneralRe: output wav can't play on Vista Pin
Saneesh4-May-07 16:46
Saneesh4-May-07 16:46 
GeneralRe: output wav can't play on Vista Pin
binsonl4-May-07 17:17
binsonl4-May-07 17:17 
GeneralRe: output wav can't play on Vista Pin
GabeON6-May-07 5:07
GabeON6-May-07 5:07 
GeneralRe: output wav can't play on Vista Pin
binsonl7-May-07 2:18
binsonl7-May-07 2:18 
QuestionRe: output wav can't play on Vista Pin
Saneesh16-May-07 12:58
Saneesh16-May-07 12:58 
AnswerRe: output wav can't play on Vista Pin
binsonl16-May-07 20:37
binsonl16-May-07 20:37 
GeneralRe: output wav can't play on Vista Pin
Saneesh17-May-07 4:11
Saneesh17-May-07 4:11 
I checked the MSDN and found that it can be set to 0 for if no extra information is needed. So you are good to go with this code change. See below:

cbSize
Specifies the size, in bytes, of extra format information appended to the end of the WAVEFORMATEX structure. This information can be used by non-PCM formats to store extra attributes for the wFormatTag. If no extra information is required by the wFormatTag, this member must be set to zero. For WAVE_FORMAT_PCM formats, this member is ignored.

Thanks for pointing it out. May be only in Vista the OS part is doing the strict checking. Thats why it is working in all the other platforms.

Saneesh

GeneralRe: output wav can't play on Vista Pin
binsonl17-May-07 8:02
binsonl17-May-07 8:02 
GeneralFormat combo box Pin
chris_mezzanine5-Feb-07 4:14
chris_mezzanine5-Feb-07 4:14 
GeneralRe: Format combo box Pin
Saneesh6-Feb-07 3:27
Saneesh6-Feb-07 3:27 
GeneralRe: Format combo box Pin
chris_mezzanine6-Feb-07 12:22
chris_mezzanine6-Feb-07 12:22 
QuestionCompatible for c++ v6.0? Pin
Newbee0724-Jan-07 4:10
Newbee0724-Jan-07 4:10 
AnswerRe: Compatible for c++ v6.0? Pin
Saneesh24-Jan-07 5:18
Saneesh24-Jan-07 5:18 
GeneralRe: Compatible for c++ v6.0? Pin
Member 870123527-Apr-12 22:56
Member 870123527-Apr-12 22:56 
GeneralRAW PCM file extension. Pin
Newbee079-Jan-07 20:14
Newbee079-Jan-07 20:14 
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 
GeneralRe: I have an strange Problem Pin
Hung In3-Jul-06 19:01
Hung In3-Jul-06 19:01 

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.