MFC Multithreaded Classes for Recording, Playing, and Saving (WAV Files)


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

The following classes, CRecordSound and CPlaySound, record sound and play PCM sound simultaneously. An example dialog-based program is provided that records, saves to disk, and then echos sound. This example also contains two more classes, CWriteSoundFile and CPlayMMSound. CWriteSoundFile receives sound buffers from CRecordSound and writes them to a WAV disk file. CPlayMMSound opens these WAV files and plays them to the sound device. All sound files are currently 8-bit PCM samples. Be careful with the sample application because it records a PCM WAV file. The PCM WAV file can grow at 8 kilobytes a second, so keep the recorded samples short.

With these classes, sound can be both recorded and played at the same time. Continuous sound can be monitored and, upon appropriate queues, sound can be played back. Simple sound recognition could also, for instance, be put into CRecordSound. The multithreading allows other actions to take place while sound recording and playback go on. As well, recorded sound can be saved to WAV files and played back.

CRecordSound can be invoked by the following code:

m_pRecordSound = new CRecordSound();

CPlaySound can be invoked by the following code:

m_pPlaySound = new CPlaySound();

To initiate sound recording from CRecordSound, do the following:

m_RecordThread->PostThreadMessage(WM_RECORDSOUND_STARTRECORDING, 0, 0L);

To stop recording sound:


Similar calls begin the playing and stopping of sound:



The CRecordSound class ferries sound blocks that have been recorded over to the CPlaySound class. The CRecordSound class is notified of the CPlaySound thread via the following:


For both CRecordSound and CPlaySound, the virtual member ProcessSoundData(short int* sound, DWORD dwSamples) can be overridden to get access to the actual sound data.

CWriteSoundFile has the following messages associated with it:




To write a WAV file, you must provide a WRITESOUNDFILE structure. The WRITESOUNDFILE structure has the following definition:

typedef struct writesoundfile_tag {
   char lpszFileName[MAX_PATH];
   WAVEFORMATEX waveFormatEx;
   TCHAR buffer[100];

You must provide the filename, and then the WAVEFORMATEX structure that defines the file to be written. With non-PCM formats, there is extra style-specific information at the end of the structure, hence the 100 bytes of buffer space.

This class receives WAVEHDR blocks created by CRecordSound. In the example application, pushing the "Start Recording" button will echo sound and save it to a "sound.wav" file at the same time.

CPlayMMSound will read a WAV file and play it to the sound device. It uses a pointer to a CPlaySound thread to achieve this. Its messages are:




The WM_PLAYMMSOUND_PLAYFILE messages open a WAV file for processing. It automatically sends off a worker thread to play the file.

You must provide a CPlaySound thread for this to work. That is the job of the WM_PLAYMMSOUND_PLAYSOUNDPTR message. You can stop the play at any time by the WM_PLAYMMSOUND_CLOSEFILE message. Currently, CPlaySound is set to play around 1.5 seconds ahead of any sound actually heard.

About the Author

Paul Cheffers

C++, PERL, and web writer. have baudot code emulator (TTY) on web for deaf at http://www.pctty.com have an interest in encrypted email. worked on old Boston University Custom Operating System in the 80s called VPS/VM on the old IBM mainframes.



  • Pls advise me how to decrease the echo function

    Posted by newer1983 on 06/03/2009 12:24pm

    Hi paulcheffers,
    your application is wonderful!
    I'm a begining programmer, and I'm researching about recording and playing a sound file by Visual C++.
    I want to record my voice without echo function.
    Pls advise me how to turn of echo function in your echo.
    Thanks so much!

  • Will not compile using VC 7.0

    Posted by Mike Pliam on 04/09/2005 05:20pm

    Get a bunch of compiler errors like:
    c:\Documents and Settings\MPliam\My Documents\Visual CPP 7.0\AUDIO\Echo\recordsound1.cpp(75): error C2440: 'static_cast' : cannot convert from 'LRESULT (__thiscall CRecordSound1::* )(WPARAM,LPARAM)' to 'void (__thiscall CWinThread::* )(WPARAM,LPARAM)'

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date