Skip to content

abstract class SF::SoundRecorder
inherits Reference #

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:

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

Moreover, two additional virtual functions can be overridden as well if necessary:

  • on_start is called before the capture happens, to perform custom initializations
  • on_stop is called after the capture ends, to perform custom cleanup

A derived class can also control the frequency of the on_process_samples calls, with the processing_interval= 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 available?() 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 through the available_devices() function. You can then select a device by calling device=() with the appropriate device. Otherwise the default capturing device will be used.

By default the recording is in 16-bit mono. Using the channel_count= 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 on_process_samples virtual function (but not on_start and not on_stop) 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. Another thing to bear in mind is that you must call stop() in the destructor of your derived class, so that the recording thread finishes before your object is destroyed.

Usage example:

class CustomRecorder < SF::SoundRecorder
  def finalize
    # Make sure to stop the recording thread
    stop
  end

  def on_start # optional
    # Initialize whatever has to be done before the capture starts
    # [...]

    # Return true to start playing
    true
  end

  def on_process_samples(samples)
    # Do something with the new chunk of samples (store them, send them, ...)
    # [...]

    # Return true to continue playing
    true
  end

  def on_stop # optional
    # Clean up whatever has to be done after the capture ends
    # [...]
  end
end

# Usage
if CustomRecorder.available?
  recorder = CustomRecorder.new

  if !recorder.start
    return -1
  end

  # [...]
  recorder.stop
end

See also: SF::SoundBufferRecorder

Included modules

SF::AlResource

Direct known subclasses

SF::SoundBufferRecorder

Class methods#

.available? : Bool#

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

View source

.available_devices : Array(String)#

Get a list of the names of all available audio capture devices

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

Returns: A vector of strings containing the names

View source

.default_device : String#

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

View source

Methods#

#channel_count : Int32#

Get the number of channels used by this recorder

Currently only mono and stereo are supported, so the value is either 1 (for mono) or 2 (for stereo).

Returns: Number of channels

See also: channel_count=

View source

#channel_count=(channel_count : Int)#

Set the channel count of the audio capture device

This method allows you to specify the number of channels used for recording. Currently only 16-bit mono and 16-bit stereo are supported.

  • channel_count - Number of channels. Currently only mono (1) and stereo (2) are supported.

See also: channel_count

View source

#device : String#

Get the name of the current audio capture device

Returns: The name of the current audio capture device

View source

#device=(name : String) : Bool#

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.

  • name - The name of the audio capture device

Returns: True, if it was able to set the requested device

See also: available_devices, default_device

View source

#finalize#

destructor

View source

abstract #on_process_samples(samples : Array(Int16) | Slice(Int16)) : Bool#

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
  • sample_count - Number of samples pointed by samples

Returns: True to continue the capture, or false to stop it

View source

#on_start : Bool#

Start capturing audio data

This virtual function may be overridden 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

View source

#on_stop#

Stop capturing audio data

This virtual function may be overridden 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.

View source

#processing_interval=(interval : Time)#

Set the processing interval

The processing interval controls the period between calls to the on_process_samples 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.

  • interval - Processing interval
View source

#sample_rate : Int32#

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

View source

#start(sample_rate : Int = 44100) : Bool#

Start the capture

The sample_rate 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 device=() 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 available_devices().

  • sample_rate - Desired capture rate, in number of samples per second

Returns: True, if start of capture was successful

See also: stop, available_devices

View source

#stop#

Stop the capture

See also: start

View source