CWaveForm






3.46/5 (7 votes)
Sep 7, 2001
2 min read

192995

2414
A simple wave form API wrapper class
Introduction
This wrapper class implements standard waveform APIs in object oriented way. It supports following features :
- Supports both wave_in and wave_out object in one class. You can indicate which type of stream to use by setting Direction.
- Use my own private stream device interface to abstract the real world stream behavior. This interface is still under development.
- All memory allocation and deallocation is managed by theobject itself. That is, the caller never worries about memory. This is achieved by using
IIDataSource
&IIDataSink
interface defined by myself. - The use of callback functions instead of callback windows. This is especially suitable for drawing intensive programs.
- The use of the private
IIAdvise
interface, through which you can notify the client by window, thread ID, event and callback function. Very flexible. In my own project I use a separate thread to process the wave data. - Support volume control on wave_in and wave_out stream.
- The use of
AddRef()
/Release()
to manage the object life cycle. (Similar to COM)
The package includes three files.
- CWaveForm.cpp
- CWaveForm.h
- Interface.h
How to use my classes:
- Create a wave object
new CWaveForm(...)
This contractor has couples parameters. Stream direction (in or out), wave in device id, wave out device id, wave in format, wave out format, wave in volume, wave out volume. Remember to call
AddRef()
- Wave device operation sequence :
open()->start()->stop()->close()
. - Whenever wave in data is incoming, the object itself will invoke, that is, callback to the client. Then client calls the
Read(ppData)
function to get data buffer. After processing, client callsReadDone(pData)
to return data buffer. - Whenever the client wants to send data to the wave out device, it can call
Write()
twice. The first call should provides an empty pointer, which will be filled with a valid buffer address. Then the client can write any data to this buffer, then callWrite()
again to queue this buffer back to wave out object. - Please call
Release()
to free wave object.
Example
- Create new wave object:
WaveForm = new CWaveForm ( WAVE_FORM_FLOW_BOTH, // Support both WAVE_IN and WAVE_OUT m_waveInDeviceID, m_waveOutDeviceID, &m_waveInFormatEx, &m_waveOutFormatEx, m_waveInVolume, m_waveOutVolume ); WaveForm->AddRef(); // For wave_in. WaveForm->AddRef(); // For wave_out.
- Client advises call back interface:
WaveForm->Advise ( (DWORD)m_hThread, // I use seperate THREAD to processing wave data. 0, // dwInstance is 0 WM_STREAM_NOTIFY, // My private window message mask CALLBACK_THREAD // Callback type. );
- Whenever wave in data is coming:
// Notify client. Invoke(WM_STREAM_NOTIFY, SUB_EVENT_WAVE_IN, 0);
- Client reading data:
PWAVEFRAME pWaveFrame; WaveForm->Read(&pWaveFrame); // Get wave in data. . . // Client processing wave in data. . . WaveForm->ReadDone(pWaveFrame); // Return wave in data.
- Client write data to wave out device:
PWAVEFRAME pWaveFrame = NULL; WaveForm->Write(&pWaveFrame); // Ask wave out object to allocate memory. . . // Client fill up this buffer with any data. . . WaveForm->Write(pWaveFrame); // Write data to wave out device.
- After using wave object:
WaveForm->Release(); // For wave_in. WaveForm->Release(); // For wave_out.