Click here to Skip to main content
6,935,055 members and growing! (20,429 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Audio     Intermediate

Voice Recording/Playing back using simple classes

By A. Riazi, Shafiee

This article introduces some useful classes that wrap the WAVE APIs.
VC6, Windows, WinMobile, Visual-Studio, MFC, Dev
Posted:17 Apr 2005
Views:157,468
Bookmarked:109 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
28 votes for this article.
Popularity: 5.43 Rating: 3.75 out of 5
8 votes, 28.6%
1
1 vote, 3.6%
2
2 votes, 7.1%
3
3 votes, 10.7%
4
14 votes, 50.0%
5

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

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.

Playing

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  
{
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

About the Authors

A. Riazi


Member
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


Occupation: Software Developer (Senior)
Company: Misbah3Com
Location: Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Shafiee


Member

Occupation: Web Developer
Location: Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Other popular Audio and Video articles:

Article Top
 
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 91 (Total in Forum: 91) (Refresh)FirstPrevNext
Generalexecuting the program doesnt play nethn Pinmemberniki parmar9:06 17 Mar '10  
GeneralRe: executing the program doesnt play nethn Pinmemberniki parmar1:14 18 Mar '10  
GeneralA Little doubt Pinmemberblaomiao7:16 8 Nov '09  
GeneralPlay an array Pinmemberasef1:45 31 Oct '09  
GeneralTest of script Pinmembersupertotal11:42 3 Aug '09  
QuestionReg: How many minutes can I buffer I have to save it to a file PinmemberNikhil_77776:48 12 Jun '09  
QuestionHow do I make this program record until the user presses a key PinmemberNikhil_77779:02 2 Jun '09  
QuestionRe: How do I make this program record until the user presses a key PinmemberNikhil_777711:05 3 Jun '09  
Generalcall recording Pinmemberjoshi aniruddha1:52 27 Apr '09  
GeneralRe: call recording Pinmembervvhhbc19:20 23 Dec '09  
QuestionI successfully compiled the files. How do I save them to a file !! **Urgent** PinmemberNikhil_777712:28 5 Mar '09  
QuestionHas anyone successfully compiled this program ?? PinmemberNikhil_777717:41 19 Feb '09  
GeneralAbout License !! PinmemberNikhil_77777:08 13 Feb '09  
GeneralRe: About License !! PinmemberA. Riazi22:28 13 Feb '09  
Generalabout Concurrency PinmemberMahdi Monhi9:26 9 Jan '09  
General*** Caution when you place sample codes in a function. PinmemberKim Moung Soo20:08 13 Jun '08  
GeneralVB.NET please PinmemberIndra_PR3:42 5 Dec '07  
Generalhere's a good one Pinmembermatt200013:26 23 Nov '07  
Generalcompiling errors Pinmemberdoxsi3:04 21 Nov '07  
Generalrecording doeesnt work Pinmemberdvieb22:40 3 Sep '07  
Questionneed ur help Pinmemberrakeshkumarsonu21:56 5 Aug '07  
GeneralI have some question about recordng wave file. Pinmemberminhye23:26 19 Jun '07  
GeneralRaw PCM format output. PinmemberNewbee0718:45 16 Jan '07  
Questionformat of stored voice Pinmemberwhizpig1:31 25 Nov '06  
Generalrecord and encode GSM 6.10 codec Pinmembertaolacha11319:43 23 Nov '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 17 Apr 2005
Editor: Rinish Biju
Copyright 2005 by A. Riazi, Shafiee
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project