Click here to Skip to main content
15,881,803 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 471K   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

 
GeneralUsing a Wave Stream Pin
Yossef30-Nov-05 22:44
Yossef30-Nov-05 22:44 
GeneralRe: Using a Wave Stream Pin
Abbas_Riazi1-Dec-05 3:27
professionalAbbas_Riazi1-Dec-05 3:27 
GeneralOk, e.g. the microphone Pin
Yossef1-Dec-05 10:57
Yossef1-Dec-05 10:57 
GeneralRe: Ok, e.g. the microphone Pin
Abbas_Riazi1-Dec-05 20:09
professionalAbbas_Riazi1-Dec-05 20:09 
GeneralSaving the file Pin
yasar7921-Nov-05 1:52
yasar7921-Nov-05 1:52 
AnswerRe: Saving the file Pin
Abbas_Riazi21-Nov-05 3:00
professionalAbbas_Riazi21-Nov-05 3:00 
GeneralRecording dosen't work Pin
triplebit4-Nov-05 4:10
triplebit4-Nov-05 4:10 
GeneralRe: Recording dosen't work Pin
dvieb3-Sep-07 21:38
dvieb3-Sep-07 21:38 
QuestionCan I use C# Pin
Kumar Muthu29-Oct-05 0:19
Kumar Muthu29-Oct-05 0:19 
AnswerRe: Can I use C# Pin
Abbas_Riazi29-Oct-05 1:36
professionalAbbas_Riazi29-Oct-05 1:36 
Generalrecording is not working Pin
Anonymous20-Oct-05 20:12
Anonymous20-Oct-05 20:12 
GeneralRe: recording is not working Pin
Abbas_Riazi21-Oct-05 16:31
professionalAbbas_Riazi21-Oct-05 16:31 
GeneralNon standard PCM formats Pin
parallax11-Aug-05 7:23
parallax11-Aug-05 7:23 
GeneralRe: Non standard PCM formats Pin
Abbas_Riazi14-Aug-05 5:20
professionalAbbas_Riazi14-Aug-05 5:20 
GeneralRe: Non standard PCM formats Pin
parallax16-Aug-05 21:13
parallax16-Aug-05 21:13 
GeneralRe: Non standard PCM formats Pin
eshFire22-Sep-05 10:55
eshFire22-Sep-05 10:55 
Generalmpeg audio Pin
shubham20043-Jul-05 18:56
shubham20043-Jul-05 18:56 
GeneralRe: mpeg audio Pin
Abbas_Riazi3-Jul-05 19:56
professionalAbbas_Riazi3-Jul-05 19:56 
GeneralEnvironment problems Pin
paests8-Jun-05 2:54
paests8-Jun-05 2:54 
GeneralRe: Environment problems Pin
Abbas_Riazi8-Jun-05 2:58
professionalAbbas_Riazi8-Jun-05 2:58 
GeneralSelecting input source Pin
PoorNewB2-May-05 5:06
PoorNewB2-May-05 5:06 
GeneralAudio format.. Pin
mpallavi28-Apr-05 19:59
mpallavi28-Apr-05 19:59 
GeneralRe: Audio format.. Pin
Abbas_Riazi28-Apr-05 21:51
professionalAbbas_Riazi28-Apr-05 21:51 
QuestionGSM streaming? Pin
beichuang26-Apr-05 23:51
beichuang26-Apr-05 23:51 
GeneralIncorrect usage of CALLBACK functions ( in / out ) Pin
zenith__17-Apr-05 20:13
zenith__17-Apr-05 20:13 

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.