dsfml.audio.soundrecorder

$(U SoundRecorder) 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 DSFML provides a built-in specialization for saving the captured data to a sound buffer (see $(SOUNDBUFFERRECORDER_LINK)).

A derived class has only one virtual function to override:

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

$(PARA 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

$(PARA 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

Members

Classes

SoundRecorder
class SoundRecorder

Abstract base class for capturing sound data.

Examples

microphone, external soundcard, webcam mic, ...) you can get a list of all available devices through the getAvailableDevices() function. You can then select a device by calling setDevice() with the appropriate device. Otherwise the default capturing device will be used.

By default the recording is in 16-bit mono. Using the setChannelCount method you can change the number of channels used by the audio capture device to record. Note that you have to decide whether you want to record in mono or stereo before starting the recording.

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.)

1 class CustomRecorder : SoundRecorder
2 {
3    ~this()
4    {
5        // Make sure to stop the recording thread
6        stop();
7    }
8 
9    override bool onStart() // optional
10    {
11        // Initialize whatever has to be done before the capture starts
12        ...
13 
14        // Return true to start playing
15        return true;
16    }
17 
18    bool onProcessSamples(const(short)[] samples)
19    {
20        // Do something with the new chunk of samples (store them, send them, ...)
21        ...
22 
23        // Return true to continue playing
24        return true;
25    }
26 
27    override void onStop() // optional
28    {
29        // Clean up whatever has to be done after the capture ends
30        ...
31    }
32 }
33 
34 // Usage
35 if (CustomRecorder.isAvailable())
36 {
37    auto recorder = new CustomRecorder();
38 
39    if (!recorder.start())
40        return -1;
41 
42    ...
43    recorder.stop();
44 }

See Also

$(SOUNDBUFFERRECORDER_LINK)

Meta