From 95c98093e9e4924f7978cc83c0126e5bc0b5e7f2 Mon Sep 17 00:00:00 2001 From: scotth Date: Thu, 28 May 2020 21:06:18 +0930 Subject: [PATCH] Adjustable processing interval in SoundStream #1517 Resurrection of previous PR with updated documentation. Added setter to adjust processing interval in SoundStream for low-latency streams. --- include/SFML/Audio/SoundStream.hpp | 17 ++++++++++++++++- src/SFML/Audio/SoundStream.cpp | 10 ++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/include/SFML/Audio/SoundStream.hpp b/include/SFML/Audio/SoundStream.hpp index f1c7f1f5..6fbaea4d 100644 --- a/include/SFML/Audio/SoundStream.hpp +++ b/include/SFML/Audio/SoundStream.hpp @@ -251,6 +251,20 @@ protected: //////////////////////////////////////////////////////////// virtual Int64 onLoop(); + //////////////////////////////////////////////////////////// + /// \brief Set the processing interval + /// + /// The processing interval controls the period at which the + /// audio buffers are filled by calls to onGetData. A smaller + /// interval may be useful for low-latency streams. Note that + /// the given period is only a hint and the actual period may + /// vary. The default processing interval is 10 ms. + /// + /// \param interval Processing interval + /// + //////////////////////////////////////////////////////////// + void setProcessingInterval(Time interval); + private: //////////////////////////////////////////////////////////// @@ -315,8 +329,9 @@ private: unsigned int m_sampleRate; //!< Frequency (samples / second) Uint32 m_format; //!< Format of the internal sound buffers bool m_loop; //!< Loop flag (true to loop, false to play once) - Uint64 m_samplesProcessed; //!< Number of buffers processed since beginning of the stream + Uint64 m_samplesProcessed; //!< Number of samples processed since beginning of the stream Int64 m_bufferSeeks[BufferCount]; //!< If buffer is an "end buffer", holds next seek position, else NoLoop. For play offset calculation. + Time m_processingInterval; //!< Interval for checking and filling the internal sound buffers. }; } // namespace sf diff --git a/src/SFML/Audio/SoundStream.cpp b/src/SFML/Audio/SoundStream.cpp index 5b6fff02..d8f5f785 100644 --- a/src/SFML/Audio/SoundStream.cpp +++ b/src/SFML/Audio/SoundStream.cpp @@ -51,7 +51,8 @@ m_sampleRate (0), m_format (0), m_loop (false), m_samplesProcessed(0), -m_bufferSeeks () +m_bufferSeeks (), +m_processingInterval(milliseconds(10)) { } @@ -264,6 +265,11 @@ Int64 SoundStream::onLoop() return 0; } +//////////////////////////////////////////////////////////// +void SoundStream::setProcessingInterval(Time interval) +{ + m_processingInterval = interval; +} //////////////////////////////////////////////////////////// void SoundStream::streamData() @@ -384,7 +390,7 @@ void SoundStream::streamData() // Leave some time for the other threads if the stream is still playing if (SoundSource::getStatus() != Stopped) - sleep(milliseconds(10)); + sleep(m_processingInterval); } // Stop the playback