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
This commit is contained in:
laurentgom 2009-02-20 21:38:59 +00:00
parent 4a3d6b171c
commit e580c8cd63
2 changed files with 40 additions and 36 deletions

View File

@ -64,11 +64,11 @@ void PlayMusic()
// Loop while the music is playing // Loop while the music is playing
while (Music.GetStatus() == sf::Music::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 // Leave some CPU time for other processes
sf::Sleep(0.1f); sf::Sleep(0.1f);
// Display the playing position
std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << Music.GetPlayingOffset() << " sec ";
} }
std::cout << std::endl; std::cout << std::endl;
} }

View File

@ -191,6 +191,7 @@ void SoundStream::Run()
{ {
// Create buffers // Create buffers
ALCheck(alGenBuffers(BuffersCount, myBuffers)); ALCheck(alGenBuffers(BuffersCount, myBuffers));
unsigned int EndBuffer = 0xFFFF;
// Fill the queue // Fill the queue
bool RequestStop = FillQueue(); bool RequestStop = FillQueue();
@ -203,37 +204,15 @@ void SoundStream::Run()
// The stream has been interrupted ! // The stream has been interrupted !
if (Sound::GetStatus() == Stopped) if (Sound::GetStatus() == Stopped)
{ {
// User requested to stop if (!RequestStop)
if (RequestStop)
{ {
if (myLoop) // Just continue
{ Sound::Play();
// 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;
}
} }
else else
{ {
// Streaming is not completed : restart the sound // End streaming
Sound::Play(); myIsStreaming = false;
} }
} }
@ -241,20 +220,45 @@ void SoundStream::Run()
ALint NbProcessed; ALint NbProcessed;
ALCheck(alGetSourcei(Sound::mySource, AL_BUFFERS_PROCESSED, &NbProcessed)); ALCheck(alGetSourcei(Sound::mySource, AL_BUFFERS_PROCESSED, &NbProcessed));
while (NbProcessed-- && !RequestStop) while (NbProcessed--)
{ {
// Pop the first unused buffer from the queue // Pop the first unused buffer from the queue
ALuint Buffer; ALuint Buffer;
ALCheck(alSourceUnqueueBuffers(Sound::mySource, 1, &Buffer)); ALCheck(alSourceUnqueueBuffers(Sound::mySource, 1, &Buffer));
// Retrieve its size and add it to the samples count // Retrieve its size and add it to the samples count
ALint Size; if (Buffer == EndBuffer)
ALCheck(alGetBufferi(Buffer, AL_SIZE, &Size)); {
mySamplesProcessed += Size / sizeof(Int16); // 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 // Fill it and push it back into the playing queue
if (FillAndPushBuffer(Buffer)) if (!RequestStop)
RequestStop = true; {
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 // Leave some time for the other threads if the stream is still playing