Documentation of SFML 2.1

Warning: this page refers to an old version of SFML. Click here to switch to the latest version.
sf::SoundRecorder Class Referenceabstract

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

#include <SoundRecorder.hpp>

Inheritance diagram for sf::SoundRecorder:
sf::SoundBufferRecorder

Public Member Functions

virtual ~SoundRecorder ()
 destructor
 
void start (unsigned int sampleRate=44100)
 Start the capture.
 
void stop ()
 Stop the capture.
 
unsigned int getSampleRate () const
 Get the sample rate.
 

Static Public Member Functions

static bool isAvailable ()
 Check if the system supports audio capture.
 

Protected Member Functions

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

Detailed Description

Abstract base class for capturing sound data.

sf::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 SFML provides a built-in specialization for saving the captured data to a sound buffer (see sf::SoundBufferRecorder).

A derived class has only one virtual function to override:

  • onProcessSamples provides the new chunks of audio samples while the capture happens

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

  • onStart is called before the capture happens, to perform custom initializations
  • onStop is called after the capture ends, to perform custom cleanup

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.

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 and onStop virtual functions (but not onStart) 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 sf::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;
recorder.start();
...
recorder.stop();
}
See Also
sf::SoundBufferRecorder

Definition at line 42 of file SoundRecorder.hpp.

Constructor & Destructor Documentation

virtual sf::SoundRecorder::~SoundRecorder ( )
virtual

destructor

sf::SoundRecorder::SoundRecorder ( )
protected

Default constructor.

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

Member Function Documentation

unsigned int sf::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 sf::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 sf::SoundRecorder or one of its derived classes will fail.

Returns
True if audio capture is supported, false otherwise
virtual bool sf::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 sf::SoundBufferRecorder.

virtual bool sf::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 sf::SoundBufferRecorder.

virtual void sf::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 sf::SoundBufferRecorder.

void sf::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.

Parameters
sampleRateDesired capture rate, in number of samples per second
See Also
stop
void sf::SoundRecorder::stop ( )

Stop the capture.

See Also
start

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