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
This commit is contained in:
LaurentGom 2010-01-21 10:00:31 +00:00
parent e4165be2f9
commit 6341b569db
3 changed files with 44 additions and 31 deletions

View File

@ -402,7 +402,7 @@
> >
</File> </File>
<File <File
RelativePath="..\..\src\SFML\Audio\AudioResource.hpp" RelativePath="..\..\include\SFML\Audio\AudioResource.hpp"
> >
</File> </File>
<File <File
@ -418,7 +418,7 @@
> >
</File> </File>
<File <File
RelativePath="..\..\src\SFML\Audio\Music.hpp" RelativePath="..\..\include\SFML\Audio\Music.hpp"
> >
</File> </File>
<File <File
@ -430,7 +430,7 @@
> >
</File> </File>
<File <File
RelativePath="..\..\src\SFML\Audio\Sound.hpp" RelativePath="..\..\include\SFML\Audio\Sound.hpp"
> >
</File> </File>
<File <File
@ -438,7 +438,7 @@
> >
</File> </File>
<File <File
RelativePath="..\..\src\SFML\Audio\SoundBuffer.hpp" RelativePath="..\..\include\SFML\Audio\SoundBuffer.hpp"
> >
</File> </File>
<File <File
@ -446,7 +446,7 @@
> >
</File> </File>
<File <File
RelativePath="..\..\src\SFML\Audio\SoundBufferRecorder.hpp" RelativePath="..\..\include\SFML\Audio\SoundBufferRecorder.hpp"
> >
</File> </File>
<File <File
@ -478,7 +478,7 @@
> >
</File> </File>
<File <File
RelativePath="..\..\src\SFML\Audio\SoundRecorder.hpp" RelativePath="..\..\include\SFML\Audio\SoundRecorder.hpp"
> >
</File> </File>
<File <File
@ -486,7 +486,7 @@
> >
</File> </File>
<File <File
RelativePath="..\..\src\SFML\Audio\SoundStream.hpp" RelativePath="..\..\include\SFML\Audio\SoundStream.hpp"
> >
</File> </File>
</Files> </Files>

View File

@ -186,12 +186,12 @@ private :
/// Fill a new buffer with audio data, and push it to the /// Fill a new buffer with audio data, and push it to the
/// playing queue /// 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 /// \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 /// Fill the buffers queue with all available buffers
@ -219,6 +219,7 @@ private :
unsigned long myFormat; ///< Format of the internal sound buffers unsigned long myFormat; ///< Format of the internal sound buffers
bool myLoop; ///< Loop flag (true to loop, false to play once) bool myLoop; ///< Loop flag (true to loop, false to play once)
unsigned int mySamplesProcessed; ///< Number of buffers processed since beginning of the stream 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 } // namespace sf

View File

@ -188,9 +188,10 @@ bool SoundStream::GetLoop() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void SoundStream::Run() void SoundStream::Run()
{ {
// Create buffers // Create the buffers
ALCheck(alGenBuffers(BuffersCount, myBuffers)); ALCheck(alGenBuffers(BuffersCount, myBuffers));
unsigned int EndBuffer = 0xFFFF; for (int i = 0; i < BuffersCount; ++i)
myEndBuffers[i] = false;
// Fill the queue // Fill the queue
bool RequestStop = FillQueue(); bool RequestStop = FillQueue();
@ -225,12 +226,21 @@ void SoundStream::Run()
ALuint Buffer; ALuint Buffer;
ALCheck(alSourceUnqueueBuffers(Sound::mySource, 1, &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 // 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 // This was the last buffer: reset the sample count
mySamplesProcessed = 0; mySamplesProcessed = 0;
EndBuffer = 0xFFFF; myEndBuffers[BufferNum] = false;
} }
else else
{ {
@ -242,16 +252,10 @@ void SoundStream::Run()
// Fill it and push it back into the playing queue // Fill it and push it back into the playing queue
if (!RequestStop) if (!RequestStop)
{ {
if (FillAndPushBuffer(Buffer)) if (FillAndPushBuffer(BufferNum))
{ {
// User requested to stop: check if we must loop or really stop // User requested to stop: check if we must loop or really stop
if (myLoop && OnStart()) 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 // Not looping or restart failed: request stop
RequestStop = true; RequestStop = true;
@ -281,18 +285,23 @@ void SoundStream::Run()
/// Fill a new buffer with audio data, and push it to the /// Fill a new buffer with audio data, and push it to the
/// playing queue /// playing queue
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool SoundStream::FillAndPushBuffer(unsigned int Buffer) bool SoundStream::FillAndPushBuffer(unsigned int BufferNum)
{ {
bool RequestStop = false; bool RequestStop = false;
// Acquire audio data // Acquire audio data
Chunk Data = {NULL, 0}; Chunk Data = {NULL, 0};
if (!OnGetData(Data)) if (!OnGetData(Data))
{
myEndBuffers[BufferNum] = true;
RequestStop = true; RequestStop = true;
}
// Create and fill the buffer, and push it to the queue // Create and fill the buffer, and push it to the queue
if (Data.Samples && Data.NbSamples) if (Data.Samples && Data.NbSamples)
{ {
unsigned int Buffer = myBuffers[BufferNum];
// Fill the buffer // Fill the buffer
ALsizei Size = static_cast<ALsizei>(Data.NbSamples) * sizeof(Int16); ALsizei Size = static_cast<ALsizei>(Data.NbSamples) * sizeof(Int16);
ALCheck(alBufferData(Buffer, myFormat, Data.Samples, Size, mySampleRate)); ALCheck(alBufferData(Buffer, myFormat, Data.Samples, Size, mySampleRate));
@ -314,9 +323,12 @@ bool SoundStream::FillQueue()
bool RequestStop = false; bool RequestStop = false;
for (int i = 0; (i < BuffersCount) && !RequestStop; ++i) for (int i = 0; (i < BuffersCount) && !RequestStop; ++i)
{ {
if (FillAndPushBuffer(myBuffers[i])) if (FillAndPushBuffer(i))
{
if (!myLoop || !OnStart())
RequestStop = true; RequestStop = true;
} }
}
return RequestStop; return RequestStop;
} }