From 6341b569dbcfe53e1522ecdbf83ac7fdda1ce0db Mon Sep 17 00:00:00 2001 From: LaurentGom Date: Thu, 21 Jan 2010 10:00:31 +0000 Subject: [PATCH] FS#139 - Fix tiny musics ignoring the "loop" flag git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1368 4e206d99-4929-0410-ac5d-dfc041789085 --- build/vc2008/sfml-audio.vcproj | 14 +++++----- include/SFML/Audio/SoundStream.hpp | 19 +++++++------- src/SFML/Audio/SoundStream.cpp | 42 +++++++++++++++++++----------- 3 files changed, 44 insertions(+), 31 deletions(-) diff --git a/build/vc2008/sfml-audio.vcproj b/build/vc2008/sfml-audio.vcproj index 3c4c0bdf5..b3f14c6b8 100644 --- a/build/vc2008/sfml-audio.vcproj +++ b/build/vc2008/sfml-audio.vcproj @@ -402,7 +402,7 @@ > diff --git a/include/SFML/Audio/SoundStream.hpp b/include/SFML/Audio/SoundStream.hpp index 6df0c57bc..67b2babed 100644 --- a/include/SFML/Audio/SoundStream.hpp +++ b/include/SFML/Audio/SoundStream.hpp @@ -186,12 +186,12 @@ private : /// Fill a new buffer with audio data, and push it to the /// playing queue /// - /// \param Buffer : Buffer to fill + /// \param Buffer : Number of the buffer to fill (in [0, BuffersCount]) /// /// \return True if the derived class has requested to stop /// //////////////////////////////////////////////////////////// - bool FillAndPushBuffer(unsigned int Buffer); + bool FillAndPushBuffer(unsigned int BufferNum); //////////////////////////////////////////////////////////// /// Fill the buffers queue with all available buffers @@ -212,13 +212,14 @@ private : //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - bool myIsStreaming; ///< Streaming state (true = playing, false = stopped) - unsigned int myBuffers[BuffersCount]; ///< Sound buffers used to store temporary audio data - unsigned int myChannelsCount; ///< Number of channels (1 = mono, 2 = stereo, ...) - unsigned int mySampleRate; ///< Frequency (samples / second) - unsigned long myFormat; ///< Format of the internal sound buffers - bool myLoop; ///< Loop flag (true to loop, false to play once) - unsigned int mySamplesProcessed; ///< Number of buffers processed since beginning of the stream + bool myIsStreaming; ///< Streaming state (true = playing, false = stopped) + unsigned int myBuffers[BuffersCount]; ///< Sound buffers used to store temporary audio data + unsigned int myChannelsCount; ///< Number of channels (1 = mono, 2 = stereo, ...) + unsigned int mySampleRate; ///< Frequency (samples / second) + unsigned long myFormat; ///< Format of the internal sound buffers + bool myLoop; ///< Loop flag (true to loop, false to play once) + unsigned int mySamplesProcessed; ///< Number of buffers processed since beginning of the stream + bool myEndBuffers[BuffersCount]; ///< Each buffer is marked as "end buffer" or not, for proper duration calculation }; } // namespace sf diff --git a/src/SFML/Audio/SoundStream.cpp b/src/SFML/Audio/SoundStream.cpp index 4fb2ff249..bd776edf4 100644 --- a/src/SFML/Audio/SoundStream.cpp +++ b/src/SFML/Audio/SoundStream.cpp @@ -188,9 +188,10 @@ bool SoundStream::GetLoop() const //////////////////////////////////////////////////////////// void SoundStream::Run() { - // Create buffers + // Create the buffers ALCheck(alGenBuffers(BuffersCount, myBuffers)); - unsigned int EndBuffer = 0xFFFF; + for (int i = 0; i < BuffersCount; ++i) + myEndBuffers[i] = false; // Fill the queue bool RequestStop = FillQueue(); @@ -225,12 +226,21 @@ void SoundStream::Run() ALuint Buffer; ALCheck(alSourceUnqueueBuffers(Sound::mySource, 1, &Buffer)); + // Find its number + unsigned int BufferNum = 0; + for (int i = 0; i < BuffersCount; ++i) + if (myBuffers[i] == Buffer) + { + BufferNum = i; + break; + } + // Retrieve its size and add it to the samples count - if (Buffer == EndBuffer) + if (myEndBuffers[BufferNum]) { // This was the last buffer: reset the sample count mySamplesProcessed = 0; - EndBuffer = 0xFFFF; + myEndBuffers[BufferNum] = false; } else { @@ -242,16 +252,10 @@ void SoundStream::Run() // Fill it and push it back into the playing queue if (!RequestStop) { - if (FillAndPushBuffer(Buffer)) + if (FillAndPushBuffer(BufferNum)) { // User requested to stop: check if we must loop or really stop - if (myLoop && OnStart()) - { - // Looping: mark the current buffer as the last one - // (to know when to reset the sample count) - EndBuffer = Buffer; - } - else + if (!myLoop || !OnStart()) { // Not looping or restart failed: request stop RequestStop = true; @@ -281,18 +285,23 @@ void SoundStream::Run() /// Fill a new buffer with audio data, and push it to the /// playing queue //////////////////////////////////////////////////////////// -bool SoundStream::FillAndPushBuffer(unsigned int Buffer) +bool SoundStream::FillAndPushBuffer(unsigned int BufferNum) { bool RequestStop = false; // Acquire audio data Chunk Data = {NULL, 0}; if (!OnGetData(Data)) + { + myEndBuffers[BufferNum] = true; RequestStop = true; + } // Create and fill the buffer, and push it to the queue if (Data.Samples && Data.NbSamples) { + unsigned int Buffer = myBuffers[BufferNum]; + // Fill the buffer ALsizei Size = static_cast(Data.NbSamples) * sizeof(Int16); ALCheck(alBufferData(Buffer, myFormat, Data.Samples, Size, mySampleRate)); @@ -314,8 +323,11 @@ bool SoundStream::FillQueue() bool RequestStop = false; for (int i = 0; (i < BuffersCount) && !RequestStop; ++i) { - if (FillAndPushBuffer(myBuffers[i])) - RequestStop = true; + if (FillAndPushBuffer(i)) + { + if (!myLoop || !OnStart()) + RequestStop = true; + } } return RequestStop;