- abstract class SoundRecorder;
- Abstract base class for capturing sound data.
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 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.
Laurent Gomila, Jeremy DeHaan
- void start(uint theSampleRate = 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 stop();
- Stop the capture.
- @property uint sampleRate();
- Get the sample rate in samples per second.
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 bool isAvailable();
- 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 SoundRecorder or one of its derived classes will fail.
True if audio capture is supported, false otherwise
- protected bool onStart();
- 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.
True to the start the capture, or false to abort it.
- protected abstract bool onProcessSamples(const(short) samples);
- 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.).
const(short) samples Array of the new chunk of recorded samples
True to continue the capture, or false to stop it
- protected void onStop();
- 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.