From 8c8d481dcf1b768dd7f9bf1acea781b6e7235d8c Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 3 Nov 2021 12:07:31 -0700 Subject: [PATCH] fix track position calculation on streaming audio --- src/FAudioGMS.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/FAudioGMS.c b/src/FAudioGMS.c index 2e23a34..83ae79c 100644 --- a/src/FAudioGMS.c +++ b/src/FAudioGMS.c @@ -175,6 +175,7 @@ typedef struct FAudioGMS_StreamingSound stb_vorbis_info info; float* streamBuffer; uint32_t streamBufferSize; + uint32_t mostRecentBufferOffset; /* used for calculating track position */ } FAudioGMS_StreamingSound; typedef struct FAudioGMS_SoundInstance @@ -861,6 +862,8 @@ static void FAudioGMS_INTERNAL_SoundInstance_AddBuffer(FAudioGMS_SoundInstance* instance->soundData.streamingSound.streamBufferSize = requiredStagingBufferSize; } + instance->soundData.streamingSound.mostRecentBufferOffset = stb_vorbis_get_sample_offset(instance->soundData.streamingSound.fileHandle); + /* NOTE: this function returns samples per channel, not total samples */ uint32_t sampleCount = stb_vorbis_get_samples_float_interleaved( instance->soundData.streamingSound.fileHandle, @@ -927,6 +930,7 @@ double FAudioGMS_StreamingSound_LoadOGG(char* filePath) instance->soundData.streamingSound.info = info; instance->soundData.streamingSound.streamBuffer = NULL; instance->soundData.streamingSound.streamBufferSize = 0; + instance->soundData.streamingSound.mostRecentBufferOffset = 0; FAudioGMS_INTERNAL_SoundInstance_AddBuffer(instance); @@ -1279,8 +1283,15 @@ double FAudioGMS_SoundInstance_GetTrackPositionInSeconds(double soundInstanceID) if (instance != NULL) { - uint32_t sampleFrame = instance->voice.handle->src.curBufferOffset / sizeof(float); - return sampleFrame / instance->format.nSamplesPerSec; + if (instance->isStatic) + { + uint32_t sampleFrame = instance->voice.handle->src.curBufferOffset / sizeof(float); + return sampleFrame / instance->format.nSamplesPerSec; + } + else + { + return ((double)instance->soundData.streamingSound.mostRecentBufferOffset + instance->voice.handle->src.curBufferOffset) / instance->format.nSamplesPerSec; + } } else {