Click here to Skip to main content
11,803,019 members (81,645 online)
Click here to Skip to main content

Voice Recording/Playing back using simple classes

, , 16 Apr 2005 320.8K 10.4K 125
Rate this:
Please Sign up or sign in to vote.
This article introduces some useful classes that wrap the WAVE APIs.


Most of us are familiar with at least one multimedia recording software. Windows has one of the simplest ones: Sound Recorder. In this article I want to introduce some simple but useful classes for recording/playing back of voice that wrap the WAVE APIs.


Here is the list of all the Wave APIs that I have used:


waveInOpen Open the Wave device for recording.
waveInPrepareHeader Prepare the Wave header for recording.
waveInAddBuffer Add buffer
waveInStart Start recording. Recorded data will be saved in a specified buffer.
waveInUnprepareHeader After finishing the record process, should un-prepare Wave header.
waveInClose Close the recording device.


waveOutOpen Open the Wave device for playing.
waveOutPrepareHeader Prepare the Wave header for playing.
waveOutWrite Play the buffer.
waveOutUnprepareHeader Un-prepare the Wave header.
waveOutClose Close the playing device.

As you can see, working with these APIs are straightforward but if you want to skip them and simply do a copy/paste code in your project, my classes are for you.

Here is the definition of the classes:

class CVoiceBase  
    CString m_result;
    MMRESULT res;


    char* buffer;
    WAVEHDR WaveHeader;

    void SetFormat(DWORD nSamplesPerSec,  
            WORD  wBitsPerSample,WORD  nChannels);
    BOOL CopyBuffer(LPVOID lpBuffer, DWORD ntime);
    CString GetLastError();
    void GetMMResult(MMRESULT res);
    void DestroyBuffer();
    BOOL PrepareBuffer(DWORD ntime);
    virtual ~CVoiceBase();

class CVoiceRecording : public CVoiceBase  
    void RecordFinished();
    BOOL IsOpen();
    BOOL Close();
    BOOL Open();    
    BOOL Record();
    HWAVEIN hWaveIn;
    virtual ~CVoiceRecording();

class CVoicePlaying : public CVoiceBase  
    void PlayFinished();
    BOOL IsOpen();
    BOOL Close();
    BOOL Open();
    BOOL Play();

    HWAVEOUT hWaveOut;

    virtual ~CVoicePlaying();

Also, for notifying of events, such as finishing recording/playing, I have used two callback functions:

BOOL CALLBACK VoiceWaveInProc(HWAVEIN hwi,       
            UINT uMsg,         
            DWORD dwInstance,  
            DWORD dwParam1,    
            DWORD dwParam2     

BOOL CALLBACK VoiceWaveOutProc(HWAVEOUT hwi,       
            UINT uMsg,         
            DWORD dwInstance,  
            DWORD dwParam1,    
            DWORD dwParam2     


Just declare two variables of type CVoiceRecording and CVoicePlaying. Use SetFormat, PrepareBuffer, Open and Record or Play member functions to work with them:

CVoiceRecording m_Record;
CVoicePlaying m_Play;

m_Record.PrepareBuffer(10);    //prepare buffer for recording 10 seconds.
m_Play.PrepareBuffer(10);    //prepare buffer for playing of 10 seconds of data

if (m_Record.IsOpen())

//after finishing the record scenario,
//play the buffer, first copy recorded buffer to m_Play buffer
m_Play.CopyBuffer(m_Record.buffer, 10);
if (m_Play.IsOpen())



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


About the Authors

A. Riazi
CEO Misbah3Com
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acqusition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competetion, my articles are:

You can see list of my articles, by clicking here

Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

You may also be interested in...

Comments and Discussions

Questionrecording wave file from voice modem Pin
zendehdel19-Jul-14 22:47
groupzendehdel19-Jul-14 22:47 
GeneralMy vote of 5 Pin
Member 1038781122-Jan-14 18:56
memberMember 1038781122-Jan-14 18:56 
QuestionMMSystem.h Pin
Member 1030607429-Sep-13 23:58
memberMember 1030607429-Sep-13 23:58 
AnswerRe: MMSystem.h Pin
Frans Jan16-Jun-14 23:34
professionalFrans Jan16-Jun-14 23:34 
Questionwriting a wave file to currecnt directory from buffer Pin
Member 87012356-Apr-12 4:26
memberMember 87012356-Apr-12 4:26 
GeneralMy vote of 4 Pin
Member 87012356-Apr-12 4:24
memberMember 87012356-Apr-12 4:24 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 19:50
membermanoj kumar choubey26-Feb-12 19:50 
GeneralMy vote of 3 Pin
zwj72319-Feb-12 22:03
memberzwj72319-Feb-12 22:03 
Generalسلام Pin
mahboob4666822-May-11 8:50
membermahboob4666822-May-11 8:50 
GeneralI fatted this project,now can recording and playback at the same time ,minimal 50MA delay can play normal [modified] Pin
erben16-Jan-11 16:20
membererben16-Jan-11 16:20 
QuestionCould you please write it using VB .net? Pin
lowisdom8-Jun-10 17:08
memberlowisdom8-Jun-10 17:08 
GeneralRecord only internal sound not what ever we talk too (how).. Pin
sunil1501877-Apr-10 19:59
membersunil1501877-Apr-10 19:59 
Generalexecuting the program doesnt play nethn Pin
niki parmar17-Mar-10 8:06
memberniki parmar17-Mar-10 8:06 
GeneralRe: executing the program doesnt play nethn Pin
niki parmar18-Mar-10 0:14
memberniki parmar18-Mar-10 0:14 
GeneralA Little doubt Pin
blaomiao8-Nov-09 6:16
memberblaomiao8-Nov-09 6:16 
GeneralPlay an array Pin
asef31-Oct-09 0:45
memberasef31-Oct-09 0:45 
GeneralTest of script Pin
supertotal3-Aug-09 10:42
membersupertotal3-Aug-09 10:42 
QuestionReg: How many minutes can I buffer I have to save it to a file Pin
Nikhil_777712-Jun-09 5:48
memberNikhil_777712-Jun-09 5:48 
QuestionHow do I make this program record until the user presses a key Pin
Nikhil_77772-Jun-09 8:02
memberNikhil_77772-Jun-09 8:02 
QuestionRe: How do I make this program record until the user presses a key Pin
Nikhil_77773-Jun-09 10:05
memberNikhil_77773-Jun-09 10:05 
Generalcall recording Pin
joshi aniruddha27-Apr-09 0:52
memberjoshi aniruddha27-Apr-09 0:52 
GeneralRe: call recording Pin
vvhhbc23-Dec-09 18:20
membervvhhbc23-Dec-09 18:20 
QuestionI successfully compiled the files. How do I save them to a file !! **Urgent** Pin
Nikhil_77775-Mar-09 11:28
memberNikhil_77775-Mar-09 11:28 
QuestionHas anyone successfully compiled this program ?? Pin
Nikhil_777719-Feb-09 16:41
memberNikhil_777719-Feb-09 16:41 
GeneralAbout License !! Pin
Nikhil_777713-Feb-09 6:08
memberNikhil_777713-Feb-09 6:08 
GeneralRe: About License !! Pin
A. Riazi13-Feb-09 21:28
memberA. Riazi13-Feb-09 21:28 
Generalabout Concurrency Pin
Mahdi Monhi9-Jan-09 8:26
memberMahdi Monhi9-Jan-09 8:26 
General*** Caution when you place sample codes in a function. Pin
Kim Moung Soo13-Jun-08 19:08
memberKim Moung Soo13-Jun-08 19:08 
GeneralVB.NET please Pin
Indra_PR5-Dec-07 2:42
memberIndra_PR5-Dec-07 2:42 
Generalhere's a good one Pin
matt200023-Nov-07 12:26
membermatt200023-Nov-07 12:26 
Generalcompiling errors Pin
doxsi21-Nov-07 2:04
memberdoxsi21-Nov-07 2:04 
Generalrecording doeesnt work Pin
dvieb3-Sep-07 21:40
memberdvieb3-Sep-07 21:40 
Questionneed ur help Pin
rakeshkumarsonu5-Aug-07 20:56
memberrakeshkumarsonu5-Aug-07 20:56 
GeneralI have some question about recordng wave file. Pin
minhye19-Jun-07 22:26
memberminhye19-Jun-07 22:26 
GeneralRaw PCM format output. Pin
Newbee0716-Jan-07 17:45
memberNewbee0716-Jan-07 17:45 
Questionformat of stored voice Pin
whizpig25-Nov-06 0:31
memberwhizpig25-Nov-06 0:31 
Generalrecord and encode GSM 6.10 codec Pin
taolacha11323-Nov-06 18:43
membertaolacha11323-Nov-06 18:43 
GeneralSalam aghaye riazi, Pin
MGA200530-Sep-06 10:53
memberMGA200530-Sep-06 10:53 
QuestionHow to record a voice in MP3 format Pin
lalit_rs1@rediffmail.com30-Aug-06 1:39
memberlalit_rs1@rediffmail.com30-Aug-06 1:39 
GeneralGetting the Record [modified] Pin
crashius4-Aug-06 1:10
membercrashius4-Aug-06 1:10 
QuestionHow can I record sound from wavein and waveout? Pin
Guto Garcia26-Jul-06 10:51
memberGuto Garcia26-Jul-06 10:51 
QuestionWeb Develope Pin
MortezaMacro26-Jun-06 10:03
memberMortezaMacro26-Jun-06 10:03 
AnswerRe: Web Develope Pin
A. Riazi27-Jun-06 0:34
memberA. Riazi27-Jun-06 0:34 
GeneralRe-enterable Pin
Bob McCown22-Jun-06 5:36
memberBob McCown22-Jun-06 5:36 
pttmai15-Jun-06 1:27
memberpttmai15-Jun-06 1:27 
Generalthanks...I wanna Some Answer Pin
nsh_enp6-Jun-06 19:02
membernsh_enp6-Jun-06 19:02 
AnswerRe: thanks...I wanna Some Answer Pin
A. Riazi6-Jun-06 23:38
memberA. Riazi6-Jun-06 23:38 
GeneralRe: thanks...I wanna Some Answer Pin
nsh_enp7-Jun-06 14:35
membernsh_enp7-Jun-06 14:35 
Questionhow to select a recording device? Pin
haitham hamed housin26-May-06 7:55
memberhaitham hamed housin26-May-06 7:55 
AnswerRe: how to select a recording device? Pin
A. Riazi26-May-06 10:00
memberA. Riazi26-May-06 10:00 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151002.1 | Last Updated 17 Apr 2005
Article Copyright 2005 by A. Riazi, Shafiee
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid