From e5ebe345ad0e736f24635e96bd9ed3566126dd88 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 20 Aug 2012 15:57:27 -0700 Subject: [PATCH] Add the option to retrieve the source offset and latency in seconds --- OpenAL32/Include/alMain.h | 1 + OpenAL32/alSource.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/OpenAL32/Include/alMain.h b/OpenAL32/Include/alMain.h index 597dbbac..69b4a29f 100644 --- a/OpenAL32/Include/alMain.h +++ b/OpenAL32/Include/alMain.h @@ -45,6 +45,7 @@ AL_API ALvoid AL_APIENTRY alProcessUpdatesSOFT(void); #ifndef AL_SOFT_source_latency #define AL_SOFT_source_latency 1 #define AL_SAMPLE_OFFSET_LATENCY_SOFT 0x1200 +#define AL_SEC_OFFSET_LATENCY_SOFT 0x1201 typedef int64_t ALint64SOFT; typedef uint64_t ALuint64SOFT; typedef void (AL_APIENTRY*LPALGETSOURCEDSOFT)(ALuint,ALenum,ALdouble*); diff --git a/OpenAL32/alSource.c b/OpenAL32/alSource.c index 52688827..dc9cb283 100644 --- a/OpenAL32/alSource.c +++ b/OpenAL32/alSource.c @@ -49,6 +49,7 @@ const ALsizei ResamplerPrePadding[ResamplerMax] = { static ALvoid InitSourceParams(ALsource *Source); static ALint64 GetSourceOffset(ALsource *Source); +static ALdouble GetSourceSecOffset(ALsource *Source); static ALvoid GetSourceOffsets(ALsource *Source, ALenum name, ALdouble *offsets, ALdouble updateLen); static ALint GetSampleOffset(ALsource *Source); @@ -109,6 +110,14 @@ static ALenum GetSourcedv(ALsource *Source, ALCcontext *Context, ALenum name, AL UnlockContext(Context); break; + case AL_SEC_OFFSET_LATENCY_SOFT: + LockContext(Context); + values[0] = GetSourceSecOffset(Source); + values[1] = (ALdouble)ALCdevice_GetLatency(Context->Device) / + 1000000000.0; + UnlockContext(Context); + break; + case AL_POSITION: LockContext(Context); values[0] = Source->Position[0]; @@ -1329,6 +1338,7 @@ AL_API void AL_APIENTRY alGetSourcedvSOFT(ALuint source, ALenum param, ALdouble case AL_SAMPLE_RW_OFFSETS_SOFT: case AL_BYTE_RW_OFFSETS_SOFT: + case AL_SEC_OFFSET_LATENCY_SOFT: case AL_POSITION: case AL_VELOCITY: @@ -2183,6 +2193,47 @@ static ALint64 GetSourceOffset(ALsource *Source) return (ALint64)minu64(readPos, MAKEU64(0x7fffffff,0xffffffff)); } +/* GetSourceSecOffset + * + * Gets the current read offset for the given Source, in seconds. The offset is + * relative to the start of the queue (not the start of the current buffer). + */ +static ALdouble GetSourceSecOffset(ALsource *Source) +{ + const ALbufferlistitem *BufferList; + const ALbuffer *Buffer = NULL; + ALuint64 readPos; + ALuint i; + + BufferList = Source->queue; + while(BufferList) + { + if(BufferList->buffer) + { + Buffer = BufferList->buffer; + break; + } + BufferList = BufferList->next; + } + + if((Source->state != AL_PLAYING && Source->state != AL_PAUSED) || !Buffer) + return 0.0; + + /* NOTE: This is the offset into the *current* buffer, so add the length of + * any played buffers */ + readPos = (ALuint64)Source->position << FRACTIONBITS; + readPos |= (ALuint64)Source->position_fraction; + BufferList = Source->queue; + for(i = 0;i < Source->BuffersPlayed && BufferList;i++) + { + if(BufferList->buffer) + readPos += (ALuint64)BufferList->buffer->SampleLen << FRACTIONBITS; + BufferList = BufferList->next; + } + + return (ALdouble)readPos / (ALdouble)FRACTIONONE / (ALdouble)Buffer->Frequency; +} + /* GetSourceOffsets * * Gets the current read and write offsets for the given Source, in the -- 2.11.4.GIT