CWave

CWave - Play and record wave files using MCI.
This class is a wrapper around the Microsoft MCI Wave API.
I wrote this class a few years ago, just to get to know the Microsoft’s multimedia API a little better.
The CWave class is pretty straight forward and allows you to easily play, record and save wave files. I’ve included some other usefull methods to change for example position, length and parameters.

The following methods are available:

BOOL Load(LPCTSTR lpszFileName);
Load specified wave file into memory.

BOOL Play(CWnd *pWnd, DWORD dwFrom, DWORD dwTo);
Play previous loaded or recorded wave file.
Specify a CWnd pointer for the window where notification message will be send to.
Use dwFrom and dwTo to set the range to play.

BOOL Record(CWnd *pWnd);
Start recording a wave file.
Specify a CWnd pointer for the window where notification message will be send to.

BOOL Stop();
Stop playing or recording current wave file.

BOOL Pause();
Pause playing or resume previous pause.

BOOL Save(LPCTSTR lpszFileName);
Save wave file (in memory) with specified name.

BOOL Close();
Close wave file.

void SetCurPos(DWORD dwPosition);
Set position in current wave to specified value.

DWORD GetCurPos();
Get current position.

BOOL SetParameters(WORD nChannels, DWORD nSamplesPerSec, WORD wBitsPerSample);
Set number of channels, samples per second and bits per sample for (next) recording session.

BOOL GetParameters(WORD &nChannels, DWORD &nSamplesPerSec, WORD &wBitsPerSample);
Get number of channels, samples per second and bits per sample of current wave.

DWORD GetLength();
Get total length (in milliseconds) of current wave.

DWORD GetStatusLevel();
Get current input level of wave device.

BOOL Delete(DWORD dwFrom, DWORD dwTo);
Delete the specified part of the wave


How to play a wave file?
// Load wave into memory
m_Wave.Load("example.wav");
// start playing
m_Wave.Play(this);

Add notification handler for stop notification:

ON_MESSAGE(MM_MCINOTIFY, OnMCINotify)

...

LRESULT CRecorderDlg::OnMCINotify(WPARAM wParam, LPARAM lParam)
{
  // notification that playing has been stopped/aborted
  return 0L;
}


How to record a new wave file?

// Open the wave device with an empty element name:
m_Wave.Load("");
// set recording parameters to stereo, 44100 Hz samplerate and 16 bits resolution
m_Wave.SetParameters(2, 44100, 16);
// start recording
m_Wave.Record(this);

....

// call stop to abort recording
m_Wave.Stop();
// save wave file
m_Wave.Save("example.wav");


Wanna know more? Just check out the example below...


Download demo executable

Download source code
This class is part of the Pablo Software Solutions MFC Extension Package - Classes Edition

 

[Home] [Products] [Source Code] [Downloads]

© 2015 - Pablo Software Solutions
All rights reserved.