Synchronized with trunk

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1369 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2010-01-21 10:13:44 +00:00
commit 4220795de4
2 changed files with 39 additions and 20 deletions

View File

@ -244,12 +244,12 @@ private :
/// consumed; it fills it again and inserts it back into the /// consumed; it fills it again and inserts it back into the
/// playing queue. /// playing queue.
/// ///
/// \param buffer Handle of the buffer to fill /// \param buffer Number of the buffer to fill (in [0, BuffersCount])
/// ///
/// \return True if the stream source has requested to stop, false otherwise /// \return True if the stream source has requested to stop, false otherwise
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool FillAndPushBuffer(unsigned int buffer); bool FillAndPushBuffer(unsigned int bufferNum);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Fill the audio buffers and put them all into the playing queue /// \brief Fill the audio buffers and put them all into the playing queue
@ -278,13 +278,14 @@ private :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool myIsStreaming; ///< Streaming state (true = playing, false = stopped) bool myIsStreaming; ///< Streaming state (true = playing, false = stopped)
unsigned int myBuffers[BuffersCount]; ///< Sound buffers used to store temporary audio data unsigned int myBuffers[BuffersCount]; ///< Sound buffers used to store temporary audio data
unsigned int myChannelsCount; ///< Number of channels (1 = mono, 2 = stereo, ...) unsigned int myChannelsCount; ///< Number of channels (1 = mono, 2 = stereo, ...)
unsigned int mySampleRate; ///< Frequency (samples / second) unsigned int mySampleRate; ///< Frequency (samples / second)
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

@ -186,9 +186,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();
@ -223,12 +224,21 @@ void SoundStream::Run()
ALuint buffer; ALuint buffer;
ALCheck(alSourceUnqueueBuffers(mySource, 1, &buffer)); ALCheck(alSourceUnqueueBuffers(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
{ {
@ -240,15 +250,13 @@ 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) if (myLoop)
{ {
// Looping: restart and mark the current buffer as the last one // Looping: restart the stream source
// (to know when to reset the sample count)
OnSeek(0); OnSeek(0);
endBuffer = buffer;
} }
else else
{ {
@ -277,18 +285,23 @@ void SoundStream::Run()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
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));
@ -308,8 +321,13 @@ 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))
requestStop = true; {
if (myLoop)
OnSeek(0);
else
requestStop = true;
}
} }
return requestStop; return requestStop;