From 921e7ac3e9a6ef7cb17dec920f44169658d93c6f Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 22 Oct 2009 09:31:26 -0700 Subject: [PATCH] Store the buffer handle directly in the source and buffer queue list --- Alc/ALu.c | 14 +++-- OpenAL32/Include/alSource.h | 6 +-- OpenAL32/alSource.c | 124 +++++++++++++++++++++++--------------------- 3 files changed, 73 insertions(+), 71 deletions(-) diff --git a/Alc/ALu.c b/Alc/ALu.c index 7d5d742b..d69285f1 100644 --- a/Alc/ALu.c +++ b/Alc/ALu.c @@ -853,16 +853,14 @@ another_source: ALuint DataSize = 0; ALuint DataPosInt = 0; ALuint DataPosFrac = 0; - ALuint Buffer; ALbuffer *ALBuffer; ALuint Channels, Bytes; ALuint BufferSize; ALfloat Pitch; /* Get buffer info */ - if(!(Buffer = ALSource->ulBufferID)) + if(!(ALBuffer = ALSource->Buffer)) goto skipmix; - ALBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(Buffer); Data = ALBuffer->data; Channels = aluChannelsFromFormat(ALBuffer->format); @@ -960,7 +958,7 @@ another_source: if(BufferListItem->next) { - NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(BufferListItem->next->buffer); + NextBuf = BufferListItem->next->buffer; if(NextBuf && NextBuf->data) { ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*Bytes)); @@ -969,7 +967,7 @@ another_source: } else if(ALSource->bLooping) { - NextBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(ALSource->queue->buffer); + NextBuf = ALSource->queue->buffer; if(NextBuf && NextBuf->data) { ulExtraSamples = min(NextBuf->size, (ALint)(ALBuffer->padding*Channels*Bytes)); @@ -1122,7 +1120,7 @@ another_source: skipmix: /* Handle looping sources */ - if(!Buffer || DataPosInt >= DataSize) + if(!ALBuffer || DataPosInt >= DataSize) { /* Queueing */ if(ALSource->queue) @@ -1138,7 +1136,7 @@ another_source: BufferListItem = BufferListItem->next; } if(BufferListItem) - ALSource->ulBufferID = BufferListItem->buffer; + ALSource->Buffer = BufferListItem->buffer; ALSource->position = DataPosInt-DataSize; ALSource->position_fraction = DataPosFrac; ALSource->BuffersPlayed++; @@ -1171,7 +1169,7 @@ another_source: BufferListItem->bufferstate = PENDING; BufferListItem = BufferListItem->next; } - ALSource->ulBufferID = ALSource->queue->buffer; + ALSource->Buffer = ALSource->queue->buffer; if(ALSource->BuffersInQueue == 1) ALSource->position = DataPosInt%DataSize; diff --git a/OpenAL32/Include/alSource.h b/OpenAL32/Include/alSource.h index 65928fc9..7e22584d 100644 --- a/OpenAL32/Include/alSource.h +++ b/OpenAL32/Include/alSource.h @@ -24,7 +24,7 @@ extern "C" { typedef struct ALbufferlistitem { - ALuint buffer; + struct ALbuffer *buffer; ALuint bufferstate; ALuint flag; struct ALbufferlistitem *next; @@ -49,12 +49,12 @@ typedef struct ALsource ALboolean bLooping; ALenum DistanceModel; - ALuint ulBufferID; - ALenum state; ALuint position; ALuint position_fraction; + struct ALbuffer *Buffer; + struct ALbufferlistitem *queue; // Linked list of buffers in queue ALuint BuffersInQueue; // Number of buffers in queue ALuint BuffersPlayed; // Number of buffers played on this loop diff --git a/OpenAL32/alSource.c b/OpenAL32/alSource.c index 6518a685..6d5e1dd9 100644 --- a/OpenAL32/alSource.c +++ b/OpenAL32/alSource.c @@ -142,8 +142,8 @@ ALAPI ALvoid ALAPIENTRY alDeleteSources(ALsizei n, const ALuint *sources) { ALBufferList = ALSource->queue; // Decrement buffer's reference counter - if(ALBufferList->buffer != 0) - ((ALbuffer*)(ALTHUNK_LOOKUPENTRY(ALBufferList->buffer)))->refcount--; + if(ALBufferList->buffer != NULL) + ALBufferList->buffer->refcount--; // Update queue to point to next element in list ALSource->queue = ALBufferList->next; // Release memory allocated for buffer list item @@ -524,14 +524,16 @@ ALAPI ALvoid ALAPIENTRY alSourcei(ALuint source,ALenum eParam,ALint lValue) { if(alIsBuffer(lValue)) { + ALbuffer *buffer = NULL; + // Remove all elements in the queue while(pSource->queue != NULL) { pALBufferListItem = pSource->queue; pSource->queue = pALBufferListItem->next; // Decrement reference counter for buffer - if (pALBufferListItem->buffer) - ((ALbuffer*)(ALTHUNK_LOOKUPENTRY(pALBufferListItem->buffer)))->refcount--; + if(pALBufferListItem->buffer) + pALBufferListItem->buffer->refcount--; // Release memory for buffer list item free(pALBufferListItem); // Decrement the number of buffers in the queue @@ -541,12 +543,14 @@ ALAPI ALvoid ALAPIENTRY alSourcei(ALuint source,ALenum eParam,ALint lValue) // Add the buffer to the queue (as long as it is NOT the NULL buffer) if(lValue != 0) { + buffer = (ALbuffer*)(ALTHUNK_LOOKUPENTRY(lValue)); + // Source is now in STATIC mode pSource->lSourceType = AL_STATIC; // Add the selected buffer to the queue pALBufferListItem = malloc(sizeof(ALbufferlistitem)); - pALBufferListItem->buffer = lValue; + pALBufferListItem->buffer = buffer; pALBufferListItem->bufferstate = PENDING; pALBufferListItem->flag = 0; pALBufferListItem->next = NULL; @@ -555,7 +559,7 @@ ALAPI ALvoid ALAPIENTRY alSourcei(ALuint source,ALenum eParam,ALint lValue) pSource->BuffersInQueue = 1; // Increment reference counter for buffer - ((ALbuffer*)(ALTHUNK_LOOKUPENTRY(lValue)))->refcount++; + buffer->refcount++; } else { @@ -565,7 +569,7 @@ ALAPI ALvoid ALAPIENTRY alSourcei(ALuint source,ALenum eParam,ALint lValue) } // Update AL_BUFFER parameter - pSource->ulBufferID = lValue; + pSource->Buffer = buffer; } else alSetError(AL_INVALID_VALUE); @@ -1050,7 +1054,7 @@ ALAPI ALvoid ALAPIENTRY alGetSourcei(ALuint source, ALenum eParam, ALint *plValu break; case AL_BUFFER: - *plValue = pSource->ulBufferID; + *plValue = (pSource->Buffer ? pSource->Buffer->buffer : 0); break; case AL_SOURCE_STATE: @@ -1302,7 +1306,7 @@ ALAPI ALvoid ALAPIENTRY alSourcePlayv(ALsizei n, const ALuint *pSourceList) ALBufferList = pSource->queue; while(ALBufferList) { - if(ALBufferList->buffer != 0 && ((ALbuffer*)ALTHUNK_LOOKUPENTRY(ALBufferList->buffer))->size) + if(ALBufferList->buffer != NULL && ALBufferList->buffer->size) { bPlay = AL_TRUE; break; @@ -1324,7 +1328,7 @@ ALAPI ALvoid ALAPIENTRY alSourcePlayv(ALsizei n, const ALuint *pSourceList) pSource->position_fraction = 0; pSource->BuffersPlayed = 0; - pSource->ulBufferID = pSource->queue->buffer; + pSource->Buffer = pSource->queue->buffer; // Make sure all the Buffers in the queue are marked as PENDING ALBufferList = pSource->queue; @@ -1541,7 +1545,7 @@ ALAPI ALvoid ALAPIENTRY alSourceRewindv(ALsizei n, const ALuint *sources) ALBufferListItem = ALBufferListItem->next; } if(Source->queue) - Source->ulBufferID = Source->queue->buffer; + Source->Buffer = Source->queue->buffer; } Source->lOffset = 0; } @@ -1593,8 +1597,8 @@ ALAPI ALvoid ALAPIENTRY alSourceQueueBuffers( ALuint source, ALsizei n, const AL { if (ALBufferList->buffer) { - iFrequency = ((ALbuffer*)(ALTHUNK_LOOKUPENTRY(ALBufferList->buffer)))->frequency; - iFormat = ((ALbuffer*)(ALTHUNK_LOOKUPENTRY(ALBufferList->buffer)))->format; + iFrequency = ALBufferList->buffer->frequency; + iFormat = ALBufferList->buffer->format; break; } ALBufferList = ALBufferList->next; @@ -1602,30 +1606,27 @@ ALAPI ALvoid ALAPIENTRY alSourceQueueBuffers( ALuint source, ALsizei n, const AL for(i = 0; i < n; i++) { - if(alIsBuffer(buffers[i])) + ALbuffer *buffer; + + if(!alIsBuffer(buffers[i])) { - if(buffers[i]) - { - if((iFrequency == -1) && (iFormat == -1)) - { - iFrequency = ((ALbuffer*)(ALTHUNK_LOOKUPENTRY(buffers[i])))->frequency; - iFormat = ((ALbuffer*)(ALTHUNK_LOOKUPENTRY(buffers[i])))->format; - } - else - { - if((iFrequency != ((ALbuffer*)(ALTHUNK_LOOKUPENTRY(buffers[i])))->frequency) || - (iFormat != ((ALbuffer*)(ALTHUNK_LOOKUPENTRY(buffers[i])))->format)) - { - alSetError(AL_INVALID_OPERATION); - bBuffersValid = AL_FALSE; - break; - } - } - } + alSetError(AL_INVALID_NAME); + bBuffersValid = AL_FALSE; + break; } - else + if(!buffers[i]) + continue; + + buffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(buffers[i]); + if(iFrequency == -1 && iFormat == -1) { - alSetError(AL_INVALID_NAME); + iFrequency = buffer->frequency; + iFormat = buffer->format; + } + else if(iFrequency != buffer->frequency || + iFormat != buffer->format) + { + alSetError(AL_INVALID_OPERATION); bBuffersValid = AL_FALSE; break; } @@ -1633,33 +1634,39 @@ ALAPI ALvoid ALAPIENTRY alSourceQueueBuffers( ALuint source, ALsizei n, const AL if(bBuffersValid) { + ALbuffer *buffer = NULL; + // Change Source Type ALSource->lSourceType = AL_STREAMING; + if(buffers[0]) + buffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(buffers[0]); + // All buffers are valid - so add them to the list ALBufferListStart = malloc(sizeof(ALbufferlistitem)); - ALBufferListStart->buffer = buffers[0]; + ALBufferListStart->buffer = buffer; ALBufferListStart->bufferstate = PENDING; ALBufferListStart->flag = 0; ALBufferListStart->next = NULL; // Increment reference counter for buffer - if(buffers[0]) - ((ALbuffer*)(ALTHUNK_LOOKUPENTRY(buffers[0])))->refcount++; + if(buffer) buffer->refcount++; ALBufferList = ALBufferListStart; for(i = 1; i < n; i++) { + if(buffers[i]) + buffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(buffers[i]); + ALBufferList->next = malloc(sizeof(ALbufferlistitem)); - ALBufferList->next->buffer = buffers[i]; + ALBufferList->next->buffer = buffer; ALBufferList->next->bufferstate = PENDING; ALBufferList->next->flag = 0; ALBufferList->next->next = NULL; // Increment reference counter for buffer - if(buffers[i]) - ((ALbuffer*)(ALTHUNK_LOOKUPENTRY(buffers[i])))->refcount++; + if(buffer) buffer->refcount++; ALBufferList = ALBufferList->next; } @@ -1668,7 +1675,7 @@ ALAPI ALvoid ALAPIENTRY alSourceQueueBuffers( ALuint source, ALsizei n, const AL { ALSource->queue = ALBufferListStart; // Update Current Buffer - ALSource->ulBufferID = ALBufferListStart->buffer; + ALSource->Buffer = ALBufferListStart->buffer; } else { @@ -1708,7 +1715,6 @@ ALAPI ALvoid ALAPIENTRY alSourceUnqueueBuffers( ALuint source, ALsizei n, ALuint ALsource *ALSource; ALsizei i; ALbufferlistitem *ALBufferList; - ALuint BufferID; ALboolean bBuffersProcessed; if (n == 0) @@ -1747,10 +1753,10 @@ ALAPI ALvoid ALAPIENTRY alSourceUnqueueBuffers( ALuint source, ALsizei n, ALuint ALSource->queue = ALBufferList->next; // Record name of buffer - buffers[i] = ALBufferList->buffer; + buffers[i] = ALBufferList->buffer->buffer; // Decrement buffer reference counter if(ALBufferList->buffer) - ((ALbuffer*)(ALTHUNK_LOOKUPENTRY(ALBufferList->buffer)))->refcount--; + ALBufferList->buffer->refcount--; // Release memory for buffer list item free(ALBufferList); @@ -1759,12 +1765,10 @@ ALAPI ALvoid ALAPIENTRY alSourceUnqueueBuffers( ALuint source, ALsizei n, ALuint if(ALSource->state != AL_PLAYING) { - if (ALSource->queue) - BufferID = ALSource->queue->buffer; + if(ALSource->queue) + ALSource->Buffer = ALSource->queue->buffer; else - BufferID = 0; - - ALSource->ulBufferID = BufferID; + ALSource->Buffer = NULL; } if((ALuint)n > ALSource->BuffersPlayed) @@ -1824,7 +1828,7 @@ static ALvoid InitSourceParams(ALCcontext *Context, ALsource *pSource) pSource->state = AL_INITIAL; pSource->lSourceType = AL_UNDETERMINED; - pSource->ulBufferID= 0; + pSource->Buffer = NULL; } @@ -1845,9 +1849,9 @@ static ALboolean GetSourceOffset(ALsource *pSource, ALenum eName, ALfloat *pflOf ALboolean bReturn = AL_TRUE; ALint lTotalBufferDataSize; - if (((pSource->state == AL_PLAYING) || (pSource->state == AL_PAUSED)) && (pSource->ulBufferID)) + if((pSource->state == AL_PLAYING || pSource->state == AL_PAUSED) && pSource->Buffer) { - pBuffer = ALTHUNK_LOOKUPENTRY(pSource->ulBufferID); + pBuffer = pSource->Buffer; // Get Current Buffer Size and frequency (in milliseconds) flBufferFreq = (ALfloat)pBuffer->frequency; eOriginalFormat = pBuffer->eOriginalFormat; @@ -1857,9 +1861,9 @@ static ALboolean GetSourceOffset(ALsource *pSource, ALenum eName, ALfloat *pflOf readPos = pSource->position * lChannels * 2; // NOTE : This is the byte offset into the *current* buffer // Add byte length of any processed buffers in the queue pBufferList = pSource->queue; - while ((pBufferList) && (pBufferList->bufferstate == PROCESSED)) + while(pBufferList && pBufferList->bufferstate == PROCESSED) { - readPos += ((ALbuffer*)ALTHUNK_LOOKUPENTRY(pBufferList->buffer))->size; + readPos += pBufferList->buffer->size; pBufferList = pBufferList->next; } @@ -1873,7 +1877,7 @@ static ALboolean GetSourceOffset(ALsource *pSource, ALenum eName, ALfloat *pflOf while (pBufferList) { if (pBufferList->buffer) - lTotalBufferDataSize += ((ALbuffer*)ALTHUNK_LOOKUPENTRY(pBufferList->buffer))->size; + lTotalBufferDataSize += pBufferList->buffer->size; pBufferList = pBufferList->next; } @@ -1992,7 +1996,7 @@ static void ApplyOffset(ALsource *pSource, ALboolean bUpdateContext) pSource->BuffersPlayed = 0; while (pBufferList) { - pBuffer = ALTHUNK_LOOKUPENTRY(pBufferList->buffer); + pBuffer = pBufferList->buffer; lBufferSize = pBuffer ? pBuffer->size : 0; if ((lTotalBufferSize + lBufferSize) <= lByteOffset) @@ -2009,8 +2013,8 @@ static void ApplyOffset(ALsource *pSource, ALboolean bUpdateContext) // Offset is within this buffer pBufferList->bufferstate = PENDING; - // Set Current Buffer ID - pSource->ulBufferID = pBufferList->buffer; + // Set Current Buffer + pSource->Buffer = pBufferList->buffer; // SW Mixer Positions are in Samples pSource->position = (lByteOffset - lTotalBufferSize) / @@ -2063,7 +2067,7 @@ static ALint GetByteOffset(ALsource *pSource) { if (pBufferList->buffer) { - pBuffer = (ALbuffer*)ALTHUNK_LOOKUPENTRY(pBufferList->buffer); + pBuffer = pBufferList->buffer; break; } pBufferList = pBufferList->next; @@ -2131,7 +2135,7 @@ static ALint GetByteOffset(ALsource *pSource) while (pBufferList) { if (pBufferList->buffer) - lTotalBufferDataSize += ((ALbuffer*)ALTHUNK_LOOKUPENTRY(pBufferList->buffer))->size; + lTotalBufferDataSize += pBufferList->buffer->size; pBufferList = pBufferList->next; } -- 2.11.4.GIT