From b9d0295c89ec91a4e1cd8912c3a77fd4a87df28b Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Sun, 11 Aug 2013 19:39:52 +0200 Subject: [PATCH] Added SoundRecorder::setProcessingInterval (#333) --- include/SFML/Audio/SoundRecorder.hpp | 34 ++++++++++++++++++++++++---- src/SFML/Audio/SoundRecorder.cpp | 16 +++++++++---- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/include/SFML/Audio/SoundRecorder.hpp b/include/SFML/Audio/SoundRecorder.hpp index eddf48eaa..843a0b5bf 100644 --- a/include/SFML/Audio/SoundRecorder.hpp +++ b/include/SFML/Audio/SoundRecorder.hpp @@ -30,6 +30,7 @@ //////////////////////////////////////////////////////////// #include #include +#include #include @@ -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 /// @@ -181,10 +200,11 @@ private : //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - Thread m_thread; ///< Thread running the background recording task - std::vector m_samples; ///< Buffer to store captured samples - unsigned int m_sampleRate; ///< Sample rate - bool m_isCapturing; ///< Capturing state + Thread m_thread; ///< Thread running the background recording task + std::vector 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 }; } // namespace sf @@ -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 diff --git a/src/SFML/Audio/SoundRecorder.cpp b/src/SFML/Audio/SoundRecorder.cpp index bf7b03fa1..7d1e859b5 100644 --- a/src/SFML/Audio/SoundRecorder.cpp +++ b/src/SFML/Audio/SoundRecorder.cpp @@ -45,9 +45,10 @@ namespace sf { //////////////////////////////////////////////////////////// SoundRecorder::SoundRecorder() : -m_thread (&SoundRecorder::record, this), -m_sampleRate (0), -m_isCapturing(false) +m_thread (&SoundRecorder::record, this), +m_sampleRate (0), +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