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

View File

@ -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

View File

@ -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<ALsizei>(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;