SOUND RECORDING CLASS FOR WINDOWS

This class encapsulates Windows API funcions for recording waveform, a callback mechanism and a buffering and enables easy access to raw waveform. Data is read from the class instance as from a file.

How to add the class to your project?

You can simply add the cpp file and include a header to your main file.
#include <wfsource.h>
This header contain a pragma for linking the MMSYSTEM.DLL dynamic library. It is done through a WINMM.LIB import library. If your compiler does not have any, you must create one:

CBuilder:      
      implib winmm.lib mmsystem.dll

Functions:

CWFSource()
Standard constructor.

~CWFSource()
Standard destructor.

bool isAvailable(char* ret_whay_not = 0);

Tests if a sound recording device is available. If not the return string will contain a text whay not. It can contain two messages: "no device exists" or "can not open device".

void open()
Opens a sound recording device.

Note: this function can throw two types of exceptions:  wf_error, std::bad_alloc.

void close()
Stops recording and closes the sound recording device.

void read(char *buff, int n)
Reads n bytes from a sound device to an output buffer. The reading is performed through an internal buffer. If the buffer is free, it starts recording. The calling of reading can be random but if the interval between two calls is not too long and the internal buffer does not overflow, the recording is not stoped.

Note: this function can throw the wf_error exception.

void setFormat(int sf, int ch, int bps) 
Sets a format of recorded data. Number of channels can be 1 or 2, bits per sample is 8 r 16.

Exceptions:

std::bad_alloc
This exception is thrown when memory allocation error occured.

wf_error
This exception is trown if a error in opening/closing recording device or during recording occured. You can call a wf_error::what() function to get error message. It can be one of following:

Can not open sound device.
Can not prepare a sound buffer.
Can not add a buffer.
A sound device is not open.
Can not start recording.
A CWFS_BUFFERLENGTH constant must be multiplication of a CWFS_FRAMELENGTH constant.

Example:

#include "wfsource.h"

int main()
{
    int N = 40;
    short buffer[N];
    CWFSource WFS;

    try
    {
        WFS.setFormat(8000, 1, 16);
        WFS.open();
        WFS.read((char *)buffer, N * sizeof(short));
        WFS.close();
    }
    catch(wf_error &err)
    {
        printf("ERROR: %s\n", err.what());
        return 1;
    }
    return 0;
}

Download:

wfsource.zip