From e580c8cd63ccc97c47e17ea039e77571795ff7bb Mon Sep 17 00:00:00 2001 From: laurentgom Date: Fri, 20 Feb 2009 21:38:59 +0000 Subject: [PATCH] sf::SoundStream (and sf::Music) is now able to loop seamlessly git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1018 4e206d99-4929-0410-ac5d-dfc041789085 --- samples/sound/Sound.cpp | 6 +-- src/SFML/Audio/SoundStream.cpp | 70 ++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/samples/sound/Sound.cpp b/samples/sound/Sound.cpp index 94314d3c4..c50b0c542 100644 --- a/samples/sound/Sound.cpp +++ b/samples/sound/Sound.cpp @@ -64,11 +64,11 @@ void PlayMusic() // Loop while the music is playing while (Music.GetStatus() == sf::Music::Playing) { - // Display the playing position - std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << Music.GetPlayingOffset() << " sec "; - // Leave some CPU time for other processes sf::Sleep(0.1f); + + // Display the playing position + std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << Music.GetPlayingOffset() << " sec "; } std::cout << std::endl; } diff --git a/src/SFML/Audio/SoundStream.cpp b/src/SFML/Audio/SoundStream.cpp index 991a0bca1..f5b87c430 100644 --- a/src/SFML/Audio/SoundStream.cpp +++ b/src/SFML/Audio/SoundStream.cpp @@ -191,6 +191,7 @@ void SoundStream::Run() { // Create buffers ALCheck(alGenBuffers(BuffersCount, myBuffers)); + unsigned int EndBuffer = 0xFFFF; // Fill the queue bool RequestStop = FillQueue(); @@ -203,37 +204,15 @@ void SoundStream::Run() // The stream has been interrupted ! if (Sound::GetStatus() == Stopped) { - // User requested to stop - if (RequestStop) + if (!RequestStop) { - if (myLoop) - { - // The stream is in loop mode : restart it - if (OnStart()) - { - mySamplesProcessed = 0; - ClearQueue(); - RequestStop = FillQueue(); - Sound::Play(); - } - else - { - // Restart failed : finish the streaming loop - myIsStreaming = false; - break; - } - } - else - { - // The stream is not in loop mode : finish the streaming loop - myIsStreaming = false; - break; - } + // Just continue + Sound::Play(); } else { - // Streaming is not completed : restart the sound - Sound::Play(); + // End streaming + myIsStreaming = false; } } @@ -241,20 +220,45 @@ void SoundStream::Run() ALint NbProcessed; ALCheck(alGetSourcei(Sound::mySource, AL_BUFFERS_PROCESSED, &NbProcessed)); - while (NbProcessed-- && !RequestStop) + while (NbProcessed--) { // Pop the first unused buffer from the queue ALuint Buffer; ALCheck(alSourceUnqueueBuffers(Sound::mySource, 1, &Buffer)); // Retrieve its size and add it to the samples count - ALint Size; - ALCheck(alGetBufferi(Buffer, AL_SIZE, &Size)); - mySamplesProcessed += Size / sizeof(Int16); + if (Buffer == EndBuffer) + { + // This was the last buffer: reset the sample count + mySamplesProcessed = 0; + EndBuffer = 0xFFFF; + } + else + { + ALint Size; + ALCheck(alGetBufferi(Buffer, AL_SIZE, &Size)); + mySamplesProcessed += Size / sizeof(Int16); + } // Fill it and push it back into the playing queue - if (FillAndPushBuffer(Buffer)) - RequestStop = true; + if (!RequestStop) + { + if (FillAndPushBuffer(Buffer)) + { + // 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 + { + // Not looping or restart failed: request stop + RequestStop = true; + } + } + } } // Leave some time for the other threads if the stream is still playing