Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
cpp3ds::SoundRecorder Class Referenceabstract

Abstract base class for capturing sound data. More...

#include <SoundRecorder.hpp>

Inheritance diagram for cpp3ds::SoundRecorder:
cpp3ds::AlResource cpp3ds::SoundBufferRecorder

Public Member Functions

virtual ~SoundRecorder ()
 destructor More...
 
bool start (unsigned int sampleRate=44100)
 Start the capture. More...
 
void stop ()
 Stop the capture. More...
 
unsigned int getSampleRate () const
 Get the sample rate. More...
 
bool setDevice (const std::string &name)
 Set the audio capture device. More...
 
const std::string & getDevice () const
 Get the name of the current audio capture device. More...
 

Static Public Member Functions

static std::vector< std::string > getAvailableDevices ()
 Get a list of the names of all availabe audio capture devices. More...
 
static std::string getDefaultDevice ()
 Get the name of the default audio capture device. More...
 
static bool isAvailable ()
 Check if the system supports audio capture. More...
 

Protected Member Functions

 SoundRecorder ()
 Default constructor. More...
 
void setProcessingInterval (cpp3ds::Time interval)
 Set the processing interval. More...
 
virtual bool onStart ()
 Start capturing audio data. More...
 
virtual bool onProcessSamples (const Int16 *samples, std::size_t sampleCount)=0
 Process a new chunk of recorded samples. More...
 
virtual void onStop ()
 Stop capturing audio data. More...
 

Detailed Description

Abstract base class for capturing sound data.

cpp3ds::SoundBuffer provides a simple interface to access the audio recording capabilities of the computer (the microphone).

As an abstract base class, it only cares about capturing sound samples, the task of making something useful with them is left to the derived class. Note that cpp3ds provides a built-in specialization for saving the captured data to a sound buffer (see cpp3ds::SoundBufferRecorder).

A derived class has only one virtual function to override:

Moreover, two additionnal virtual functions can be overriden as well if necessary:

A derived class can also control the frequency of the onProcessSamples calls, with the setProcessingInterval protected function. The default interval is chosen so that recording thread doesn't consume too much CPU, but it can be changed to a smaller value if you need to process the recorded data in real time, for example.

The audio capture feature may not be supported or activated on every platform, thus it is recommended to check its availability with the isAvailable() function. If it returns false, then any attempt to use an audio recorder will fail.

If you have multiple sound input devices connected to your computer (for example: microphone, external soundcard, webcam mic, ...) you can get a list of all available devices throught the getAvailableDevices() function. You can then select a device by calling setDevice() with the appropiate device. Otherwise the default capturing device will be used.

It is important to note that the audio capture happens in a separate thread, so that it doesn't block the rest of the program. In particular, the onProcessSamples virtual function (but not onStart and not onStop) will be called from this separate thread. It is important to keep this in mind, because you may have to take care of synchronization issues if you share data between threads.

Usage example:

class CustomRecorder : public cpp3ds::SoundRecorder
{
virtual bool onStart() // optional
{
// Initialize whatever has to be done before the capture starts
...
// Return true to start playing
return true;
}
virtual bool onProcessSamples(const Int16* samples, std::size_t sampleCount)
{
// Do something with the new chunk of samples (store them, send them, ...)
...
// Return true to continue playing
return true;
}
virtual void onStop() // optional
{
// Clean up whatever has to be done after the capture ends
...
}
}
// Usage
if (CustomRecorder::isAvailable())
{
CustomRecorder recorder;
if (!recorder.start())
return -1;
...
recorder.stop();
}
See also
cpp3ds::SoundBufferRecorder

Definition at line 49 of file SoundRecorder.hpp.

Constructor & Destructor Documentation

virtual cpp3ds::SoundRecorder::~SoundRecorder ( )
virtual

destructor

cpp3ds::SoundRecorder::SoundRecorder ( )
protected

Default constructor.

This constructor is only meant to be called by derived classes.

Member Function Documentation

static std::vector<std::string> cpp3ds::SoundRecorder::getAvailableDevices ( )
static

Get a list of the names of all availabe audio capture devices.

This function returns a vector of strings, containing the names of all availabe audio capture devices.

Returns
A vector of strings containing the names
static std::string cpp3ds::SoundRecorder::getDefaultDevice ( )
static

Get the name of the default audio capture device.

This function returns the name of the default audio capture device. If none is available, an empty string is returned.

Returns
The name of the default audio capture device
const std::string& cpp3ds::SoundRecorder::getDevice ( ) const

Get the name of the current audio capture device.

Returns
The name of the current audio capture device
unsigned int cpp3ds::SoundRecorder::getSampleRate ( ) const

Get the sample rate.

The sample rate defines the number of audio samples captured per second. The higher, the better the quality (for example, 44100 samples/sec is CD quality).

Returns
Sample rate, in samples per second
static bool cpp3ds::SoundRecorder::isAvailable ( )
static

Check if the system supports audio capture.

This function should always be called before using the audio capture features. If it returns false, then any attempt to use cpp3ds::SoundRecorder or one of its derived classes will fail.

Returns
True if audio capture is supported, false otherwise
virtual bool cpp3ds::SoundRecorder::onProcessSamples ( const Int16 *  samples,
std::size_t  sampleCount 
)
protectedpure virtual

Process a new chunk of recorded samples.

This virtual function is called every time a new chunk of recorded data is available. The derived class can then do whatever it wants with it (storing it, playing it, sending it over the network, etc.).

Parameters
samplesPointer to the new chunk of recorded samples
sampleCountNumber of samples pointed by samples
Returns
True to continue the capture, or false to stop it

Implemented in cpp3ds::SoundBufferRecorder.

virtual bool cpp3ds::SoundRecorder::onStart ( )
protectedvirtual

Start capturing audio data.

This virtual function may be overriden by a derived class if something has to be done every time a new capture starts. If not, this function can be ignored; the default implementation does nothing.

Returns
True to start the capture, or false to abort it

Reimplemented in cpp3ds::SoundBufferRecorder.

virtual void cpp3ds::SoundRecorder::onStop ( )
protectedvirtual

Stop capturing audio data.

This virtual function may be overriden by a derived class if something has to be done every time the capture ends. If not, this function can be ignored; the default implementation does nothing.

Reimplemented in cpp3ds::SoundBufferRecorder.

bool cpp3ds::SoundRecorder::setDevice ( const std::string &  name)

Set the audio capture device.

This function sets the audio capture device to the device with the given name. It can be called on the fly (i.e: while recording). If you do so while recording and opening the device fails, it stops the recording.

Parameters
nameThe name of the audio capture device
Returns
True, if it was able to set the requested device
See also
getAvailableDevices, getDefaultDevice
void cpp3ds::SoundRecorder::setProcessingInterval ( cpp3ds::Time  interval)
protected

Set the processing interval.

The processing interval controls the period between calls to the onProcessSamples function. You may want to use a small interval if you want to process the recorded data in real time, for example.

Note: this is only a hint, the actual period may vary. So don't rely on this parameter to implement precise timing.

The default processing interval is 100 ms.

Parameters
intervalProcessing interval
bool cpp3ds::SoundRecorder::start ( unsigned int  sampleRate = 44100)

Start the capture.

The sampleRate parameter defines the number of audio samples captured per second. The higher, the better the quality (for example, 44100 samples/sec is CD quality). This function uses its own thread so that it doesn't block the rest of the program while the capture runs. Please note that only one capture can happen at the same time. You can select which capture device will be used, by passing the name to the setDevice() method. If none was selected before, the default capture device will be used. You can get a list of the names of all available capture devices by calling getAvailableDevices().

Parameters
sampleRateDesired capture rate, in number of samples per second
Returns
True, if start of capture was successful
See also
stop, getAvailableDevices
void cpp3ds::SoundRecorder::stop ( )

Stop the capture.

See also
start

The documentation for this class was generated from the following file: