Added SoundRecorder::setProcessingInterval (#333)

This commit is contained in:
Laurent Gomila 2013-08-11 19:39:52 +02:00
parent 5624948042
commit b9d0295c89
2 changed files with 42 additions and 8 deletions

View File

@ -30,6 +30,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Audio/Export.hpp>
#include <SFML/System/Thread.hpp>
#include <SFML/System/Time.hpp>
#include <vector>
@ -109,6 +110,24 @@ protected :
////////////////////////////////////////////////////////////
SoundRecorder();
////////////////////////////////////////////////////////////
/// \brief Set the processing interval
///
/// The processing interval controls the period
/// between calls to the onProcessSamples 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.
///
/// \param interval Processing interval
///
////////////////////////////////////////////////////////////
void setProcessingInterval(sf::Time interval);
////////////////////////////////////////////////////////////
/// \brief Start capturing audio data
///
@ -184,6 +203,7 @@ private :
Thread m_thread; ///< Thread running the background recording task
std::vector<Int16> m_samples; ///< Buffer to store captured samples
unsigned int m_sampleRate; ///< Sample rate
sf::Time m_processingInterval; ///< Time period between calls to onProcessSamples
bool m_isCapturing; ///< Capturing state
};
@ -213,6 +233,12 @@ private :
/// \li onStart is called before the capture happens, to perform custom initializations
/// \li onStop is called after the capture ends, to perform custom cleanup
///
/// 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

View File

@ -47,7 +47,8 @@ namespace sf
SoundRecorder::SoundRecorder() :
m_thread (&SoundRecorder::record, this),
m_sampleRate (0),
m_isCapturing(false)
m_isCapturing (false),
m_processingInterval(sf::milliseconds(100))
{
priv::ensureALInit();
}
@ -128,6 +129,13 @@ bool SoundRecorder::isAvailable()
}
////////////////////////////////////////////////////////////
void SoundRecorder::setProcessingInterval(sf::Time interval)
{
m_processingInterval = interval;
}
////////////////////////////////////////////////////////////
bool SoundRecorder::onStart()
{
@ -152,7 +160,7 @@ void SoundRecorder::record()
processCapturedSamples();
// Don't bother the CPU while waiting for more captured data
sleep(milliseconds(100));
sleep(m_processingInterval);
}
// Capture is finished : clean up everything