Trait sfml::audio::capture::SoundRecorder[][src]

pub trait SoundRecorder {
    fn on_process_samples(&mut self, samples: &[i16]) -> bool;

    fn on_start(&mut self) -> bool { ... }
fn on_stop(&mut self) { ... } }

Trait for processing captured sound data.

SoundRecorder provides a simple interface to access the audio recording capabilities of the computer (the microphone).

As a trait, it only cares about capturing sound samples, the task of making something useful with them is left to the implementer. Note that SFML provides a built-in implementation for saving the captured data to a sound buffer (see SoundBufferRecorder).

Only one method is required to be implemented.

on_process_samples provides the new chunks of audio samples while the capture happens Moreover, two additional methods 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.

You can also control the frequency of the on_process_samples calls, with SoundRecorderDriver::set_processing_interval. 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 is_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 SoundRecorderDriver::set_device with the appropriate device. Otherwise the default capturing device will be used.

By default the recording is in 16-bit mono. Using the SoundRecorderDriver::set_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 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.

Required methods

fn on_process_samples(&mut self, samples: &[i16]) -> bool[src]

Process a new chunk of recorded samples.

This method is called every time a new chunk of recorded data is available. The implementer can then do whatever it wants with it (storing it, playing it, sending it over the network, etc.).

Returns true to continue the capture, or false to stop it.

Loading content...

Provided methods

fn on_start(&mut self) -> bool[src]

Start capturing audio data.

This method may be overridden by an implementer 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.

fn on_stop(&mut self)[src]

Stop capturing audio data.

This method may be overridden by an implementer if something has to be done every time the capture ends. If not, this function can be ignored; the default implementation does nothing.

Loading content...

Implementors

Loading content...