From f0ce1bff3eddbe9d081585abe1aaa2810371b692 Mon Sep 17 00:00:00 2001
From: Chris Robinson <chris.kcat@gmail.com>
Date: Tue, 11 May 2010 11:59:41 -0700
Subject: Simplify and fix applying a source offset

---
 OpenAL32/alSource.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)

diff --git a/OpenAL32/alSource.c b/OpenAL32/alSource.c
index d7a6d16c..afea8dfb 100644
--- a/OpenAL32/alSource.c
+++ b/OpenAL32/alSource.c
@@ -1908,6 +1908,7 @@ static ALboolean ApplyOffset(ALsource *Source)
     ALbufferlistitem    *BufferList;
     ALbuffer            *Buffer;
     ALint                lBufferSize, lTotalBufferSize;
+    ALint                BuffersPlayed;
     ALint                lByteOffset;
 
     // Get true byte offset
@@ -1920,7 +1921,7 @@ static ALboolean ApplyOffset(ALsource *Source)
     // Sort out the queue (pending and processed states)
     BufferList = Source->queue;
     lTotalBufferSize = 0;
-    Source->BuffersPlayed = 0;
+    BuffersPlayed = 0;
 
     while(BufferList)
     {
@@ -1930,19 +1931,21 @@ static ALboolean ApplyOffset(ALsource *Source)
         if(lTotalBufferSize+lBufferSize <= lByteOffset)
         {
             // Offset is past this buffer so increment BuffersPlayed
-            Source->BuffersPlayed++;
+            BuffersPlayed++;
         }
         else if(lTotalBufferSize <= lByteOffset)
         {
+
             // Offset is within this buffer
             // Set Current Buffer
             Source->Buffer = BufferList->buffer;
+            Source->BuffersPlayed = BuffersPlayed;
 
             // SW Mixer Positions are in Samples
             Source->position = (lByteOffset - lTotalBufferSize) /
                                 aluBytesFromFormat(Buffer->format) /
                                 aluChannelsFromFormat(Buffer->format);
-            break;
+            return AL_TRUE;
         }
 
         // Increment the TotalBufferSize
@@ -1951,8 +1954,8 @@ static ALboolean ApplyOffset(ALsource *Source)
         // Move on to next buffer in the Queue
         BufferList = BufferList->next;
     }
-
-    return AL_TRUE;
+    // Offset is out of range of the buffer queue
+    return AL_FALSE;
 }
 
 
@@ -1970,7 +1973,6 @@ static ALint GetByteOffset(ALsource *Source)
     ALfloat  BufferFreq;
     ALint    Channels, Bytes;
     ALint    ByteOffset = -1;
-    ALint    TotalBufferDataSize;
 
     // Find the first non-NULL Buffer in the Queue
     BufferList = Source->queue;
@@ -2017,19 +2019,6 @@ static ALint GetByteOffset(ALsource *Source)
     // Clear Offset
     Source->lOffset = 0;
 
-    TotalBufferDataSize = 0;
-    BufferList = Source->queue;
-    while(BufferList)
-    {
-        if(BufferList->buffer)
-            TotalBufferDataSize += BufferList->buffer->size;
-        BufferList = BufferList->next;
-    }
-
-    // Finally, if the ByteOffset is beyond the length of all the buffers in
-    // the queue, return -1
-    if(ByteOffset >= TotalBufferDataSize)
-        return -1;
     return ByteOffset;
 }
 
-- 
cgit v1.2.3