Abstract base class for capturing sound data. More...
#include <SoundRecorder.hpp>
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. | |
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:
Moreover, two additionnal virtual functions can be overriden as well if necessary:
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:
Definition at line 42 of file SoundRecorder.hpp.
|
virtual |
destructor
|
protected |
Default constructor.
This constructor is only meant to be called by derived classes.
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).
|
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.
|
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.).
samples | Pointer to the new chunk of recorded samples |
sampleCount | Number of samples pointed by samples |
Implemented in sf::SoundBufferRecorder.
|
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.
Reimplemented in sf::SoundBufferRecorder.
|
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.
sampleRate | Desired capture rate, in number of samples per second |
void sf::SoundRecorder::stop | ( | ) |
Stop the capture.