Click here to Skip to main content
15,887,027 members
Articles / Mobile Apps / Windows Mobile
Article

Voice Recording/Playing back using simple classes

,
Rate me:
Please Sign up or sign in to vote.
4.62/5 (43 votes)
16 Apr 20051 min read 471.8K   12.2K   130   104
This article introduces some useful classes that wrap the WAVE APIs.

Introduction

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.

WAVE APIs

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

Recording

waveInOpenOpen the Wave device for recording.
waveInPrepareHeaderPrepare the Wave header for recording.
waveInAddBufferAdd buffer
waveInStartStart recording. Recorded data will be saved in a specified buffer.
waveInUnprepareHeaderAfter finishing the record process, should un-prepare Wave header.
waveInCloseClose the recording device.

Playing

waveOutOpenOpen the Wave device for playing.
waveOutPrepareHeaderPrepare the Wave header for playing.
waveOutWritePlay the buffer.
waveOutUnprepareHeaderUn-prepare the Wave header.
waveOutCloseClose 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  
{
public:
    
    CString m_result;
    MMRESULT res;
    enum 
    {
        SPS_8K=8000,
        SPS_11K=11025,
        SPS_22K=22050,
        SPS_44K=44100
    };

    enum
    {
        CH_MONO=1,
        CH_STEREO=2
    };

    char* buffer;
    WAVEHDR WaveHeader;
    WAVEFORMATEX PCMfmt;

    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);
    
    CVoiceBase();
    virtual ~CVoiceBase();
};

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

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

    HWAVEOUT hWaveOut;

    CVoicePlaying();
    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     
                );

Example

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_Record.Open();
    
m_Play.PrepareBuffer(10);    //prepare buffer for playing of 10 seconds of data
m_Play.Open();    

if (m_Record.IsOpen())
{
    m_Record.Record();
}

//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())
{
    m_Play.Play();
}

Enjoy!

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
CEO Solaris Electronics LLC
United Arab Emirates United Arab Emirates
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 Acquisition 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 competition, my articles are:


You can see list of my articles, by clicking here


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalcall recording Pin
joshi aniruddha27-Apr-09 0:52
joshi aniruddha27-Apr-09 0:52 
GeneralRe: call recording Pin
vvhhbc23-Dec-09 18:20
vvhhbc23-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
Nikhil_77775-Mar-09 11:28 
QuestionHas anyone successfully compiled this program ?? Pin
Nikhil_777719-Feb-09 16:41
Nikhil_777719-Feb-09 16:41 
GeneralAbout License !! Pin
Nikhil_777713-Feb-09 6:08
Nikhil_777713-Feb-09 6:08 
GeneralRe: About License !! Pin
Abbas_Riazi13-Feb-09 21:28
professionalAbbas_Riazi13-Feb-09 21:28 
Generalabout Concurrency Pin
DSPCottage9-Jan-09 8:26
DSPCottage9-Jan-09 8:26 
General*** Caution when you place sample codes in a function. Pin
Kim Moung Soo13-Jun-08 19:08
Kim Moung Soo13-Jun-08 19:08 
Recording and playing are asynchronous.
You should put Sleep() after Record() and Play() call when you just test it in a function.

CVoiceRecording m_Record;
CVoicePlaying m_Play;

m_Record.PrepareBuffer(10); //prepare buffer for recording 10 seconds.
m_Record.Open();

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

if (m_Record.IsOpen())
{
m_Record.Record();
Sleep(10*1000); // Wait 10 secord to finish recording
}

//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())
{
m_Play.Play();
Sleep(10*1000); // Wait to finish Playing
}
GeneralVB.NET please Pin
Indra PR5-Dec-07 2:42
Indra PR5-Dec-07 2:42 
Generalhere's a good one Pin
matt200023-Nov-07 12:26
matt200023-Nov-07 12:26 
Generalcompiling errors Pin
doxsi21-Nov-07 2:04
doxsi21-Nov-07 2:04 
Generalrecording doeesnt work Pin
dvieb3-Sep-07 21:40
dvieb3-Sep-07 21:40 
Questionneed ur help Pin
rakeshkumarsonu5-Aug-07 20:56
rakeshkumarsonu5-Aug-07 20:56 
GeneralI have some question about recordng wave file. Pin
minhye19-Jun-07 22:26
minhye19-Jun-07 22:26 
GeneralRaw PCM format output. Pin
Newbee0716-Jan-07 17:45
Newbee0716-Jan-07 17:45 
Questionformat of stored voice Pin
whizpig25-Nov-06 0:31
whizpig25-Nov-06 0:31 
Generalrecord and encode GSM 6.10 codec Pin
taolacha11323-Nov-06 18:43
taolacha11323-Nov-06 18:43 
GeneralSalam aghaye riazi, Pin
Antonom30-Sep-06 10:53
Antonom30-Sep-06 10:53 
QuestionHow to record a voice in MP3 format Pin
lalit_rs1@rediffmail.com30-Aug-06 1:39
lalit_rs1@rediffmail.com30-Aug-06 1:39 
GeneralGetting the Record [modified] Pin
crashius4-Aug-06 1:10
crashius4-Aug-06 1:10 
QuestionHow can I record sound from wavein and waveout? Pin
Guto Garcia26-Jul-06 10:51
Guto Garcia26-Jul-06 10:51 
QuestionWeb Develope Pin
MortezaMacro26-Jun-06 10:03
MortezaMacro26-Jun-06 10:03 
AnswerRe: Web Develope Pin
Abbas_Riazi27-Jun-06 0:34
professionalAbbas_Riazi27-Jun-06 0:34 
GeneralRe-enterable Pin
Bob McCown22-Jun-06 5:36
Bob McCown22-Jun-06 5:36 
GeneralPLEASE HELP ME! Pin
pttmai15-Jun-06 1:27
pttmai15-Jun-06 1: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.