sf::SoundRecorder Class Reference

Abstract base class for capturing sound data. More...

#include <SoundRecorder.hpp>

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

List of all members.

Public Member Functions

virtual ~SoundRecorder ()
 destructor
void Start (unsigned int sampleRate=44100)
 Start the capture.
void Stop ()
 Stop the capture.
unsigned int GetSampleRate () const
 Get the sample rate.

Static Public Member Functions

static bool IsAvailable ()
 Check if the system supports audio capture.

Protected Member Functions

 SoundRecorder ()
 Default constructor.

Private Types

typedef void(* FuncType )(void *)

Private Member Functions

void Launch ()
 Run the thread.
void Wait ()
 Wait until the thread finishes.
void Terminate ()
 Terminate the thread.

Detailed Description

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:

Moreover, two additionnal virtual functions can be overriden as well if necessary:

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.

Usage example:

 class CustomRecorder : public sf::SoundRecorder
 {
     virtual bool OnStart() // optional
     {
         // Initialize whatever has to be done before the capture starts
         ...

         // Return true to start playing
         return true;
     }

     virtual bool OnProcessSamples(const Int16* samples, std::size_t samplesCount)
     {
         // Do something with the new chunk of samples (store them, send them, ...)
         ...

         // Return true to continue playing
         return true;
     }

     virtual void OnStop() // optional
     {
         // Clean up whatever has to be done after the capture ends
         ...
     }
 }

 // Usage
 if (CustomRecorder::IsAvailable())
 {
     CustomRecorder recorder;
     recorder.Start();
     ...
     recorder.Stop();
 }
See also:
sf::SoundBufferRecorder

Definition at line 41 of file SoundRecorder.hpp.


Constructor & Destructor Documentation

sf::SoundRecorder::~SoundRecorder (  )  [virtual]

destructor

Definition at line 55 of file SoundRecorder.cpp.

sf::SoundRecorder::SoundRecorder (  )  [protected]

Default constructor.

This constructor is only meant to be called by derived classes.

Definition at line 46 of file SoundRecorder.cpp.


Member Function Documentation

unsigned int sf::SoundRecorder::GetSampleRate (  )  const

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

Definition at line 115 of file SoundRecorder.cpp.

bool sf::SoundRecorder::IsAvailable (  )  [static]

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

Definition at line 122 of file SoundRecorder.cpp.

void sf::SoundRecorder::Start ( unsigned int  sampleRate = 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.

Parameters:
sampleRate Desired capture rate, in number of samples per second
See also:
Stop

Definition at line 62 of file SoundRecorder.cpp.

void sf::SoundRecorder::Stop (  ) 

Stop the capture.

See also:
Start

Definition at line 106 of file SoundRecorder.cpp.


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