Loading...
Searching...
No Matches

Specialized SoundRecorder which stores the captured audio data into a sound buffer. More...

#include <SFML/Audio/SoundBufferRecorder.hpp>

Inheritance diagram for sf::SoundBufferRecorder:
sf::SoundRecorder

Public Member Functions

 ~SoundBufferRecorder () override
 destructor
 
const SoundBuffergetBuffer () const
 Get the sound buffer containing the captured audio data.
 
bool start (unsigned int sampleRate=44100)
 Start the capture.
 
void stop ()
 Stop the capture.
 
unsigned int getSampleRate () const
 Get the sample rate.
 
bool setDevice (const std::string &name)
 Set the audio capture device.
 
const std::string & getDevice () const
 Get the name of the current audio capture device.
 
void setChannelCount (unsigned int channelCount)
 Set the channel count of the audio capture device.
 
unsigned int getChannelCount () const
 Get the number of channels used by this recorder.
 
const std::vector< SoundChannel > & getChannelMap () const
 Get the map of position in sample frame to sound channel.
 

Static Public Member Functions

static std::vector< std::string > getAvailableDevices ()
 Get a list of the names of all available audio capture devices.
 
static std::string getDefaultDevice ()
 Get the name of the default audio capture device.
 
static bool isAvailable ()
 Check if the system supports audio capture.
 

Protected Member Functions

bool onStart () override
 Start capturing audio data.
 
bool onProcessSamples (const std::int16_t *samples, std::size_t sampleCount) override
 Process a new chunk of recorded samples.
 
void onStop () override
 Stop capturing audio data.
 

Detailed Description

Specialized SoundRecorder which stores the captured audio data into a sound buffer.

sf::SoundBufferRecorder allows to access a recorded sound through a sf::SoundBuffer, so that it can be played, saved to a file, etc.

It has the same simple interface as its base class (start(), stop()) and adds a function to retrieve the recorded sound buffer (getBuffer()).

As usual, don't forget to call the isAvailable() function before using this class (see sf::SoundRecorder for more details about this).

Usage example:

{
// Record some audio data
if (!recorder.start())
{
// Handle error...
}
...
recorder.stop();
// Get the buffer containing the captured audio data
const sf::SoundBuffer& buffer = recorder.getBuffer();
// Save it to a file (for example...)
if (!buffer.saveToFile("my_record.ogg"))
{
// Handle error...
}
}
Specialized SoundRecorder which stores the captured audio data into a sound buffer.
const SoundBuffer & getBuffer() const
Get the sound buffer containing the captured audio data.
Storage for audio samples defining a sound.
bool saveToFile(const std::filesystem::path &filename) const
Save the sound buffer to an audio file.
bool start(unsigned int sampleRate=44100)
Start the capture.
void stop()
Stop the capture.
static bool isAvailable()
Check if the system supports audio capture.
See also
sf::SoundRecorder

Definition at line 48 of file SoundBufferRecorder.hpp.

Constructor & Destructor Documentation

◆ ~SoundBufferRecorder()

sf::SoundBufferRecorder::~SoundBufferRecorder ( )
override

destructor

Member Function Documentation

◆ getAvailableDevices()

static std::vector< std::string > sf::SoundRecorder::getAvailableDevices ( )
staticnodiscardinherited

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

◆ getBuffer()

const SoundBuffer & sf::SoundBufferRecorder::getBuffer ( ) const
nodiscard

Get the sound buffer containing the captured audio data.

The sound buffer is valid only after the capture has ended. This function provides a read-only access to the internal sound buffer, but it can be copied if you need to make any modification to it.

Returns
Read-only access to the sound buffer

◆ getChannelCount()

unsigned int sf::SoundRecorder::getChannelCount ( ) const
nodiscardinherited

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
setChannelCount

◆ getChannelMap()

const std::vector< SoundChannel > & sf::SoundRecorder::getChannelMap ( ) const
nodiscardinherited

Get the map of position in sample frame to sound channel.

This is used to map a sample in the sample stream to a position during spatialization.

Returns
Map of position in sample frame to sound channel

◆ getDefaultDevice()

static std::string sf::SoundRecorder::getDefaultDevice ( )
staticnodiscardinherited

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

◆ getDevice()

const std::string & sf::SoundRecorder::getDevice ( ) const
nodiscardinherited

Get the name of the current audio capture device.

Returns
The name of the current audio capture device

◆ getSampleRate()

unsigned int sf::SoundRecorder::getSampleRate ( ) const
nodiscardinherited

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

◆ isAvailable()

static bool sf::SoundRecorder::isAvailable ( )
staticnodiscardinherited

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

◆ onProcessSamples()

bool sf::SoundBufferRecorder::onProcessSamples ( const std::int16_t * samples,
std::size_t sampleCount )
nodiscardoverrideprotectedvirtual

Process a new chunk of recorded samples.

Parameters
samplesPointer to the new chunk of recorded samples
sampleCountNumber of samples pointed by samples
Returns
true to continue the capture, or false to stop it

Implements sf::SoundRecorder.

◆ onStart()

bool sf::SoundBufferRecorder::onStart ( )
nodiscardoverrideprotectedvirtual

Start capturing audio data.

Returns
true to start the capture, or false to abort it

Reimplemented from sf::SoundRecorder.

◆ onStop()

void sf::SoundBufferRecorder::onStop ( )
overrideprotectedvirtual

Stop capturing audio data.

Reimplemented from sf::SoundRecorder.

◆ setChannelCount()

void sf::SoundRecorder::setChannelCount ( unsigned int channelCount)
inherited

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.

Parameters
channelCountNumber of channels. Currently only mono (1) and stereo (2) are supported.
See also
getChannelCount

◆ setDevice()

bool sf::SoundRecorder::setDevice ( const std::string & name)
nodiscardinherited

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.

Parameters
nameThe name of the audio capture device
Returns
true, if it was able to set the requested device
See also
getAvailableDevices, getDefaultDevice

◆ start()

bool sf::SoundRecorder::start ( unsigned int sampleRate = 44100)
nodiscardinherited

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. You can select which capture device will be used by passing the name to the setDevice() 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 getAvailableDevices().

Parameters
sampleRateDesired capture rate, in number of samples per second
Returns
true, if start of capture was successful
See also
stop, getAvailableDevices

◆ stop()

void sf::SoundRecorder::stop ( )
inherited

Stop the capture.

See also
start

The documentation for this class was generated from the following file: