fix track position calculation on streaming audio

pull/1/head
cosmonaut 2021-11-03 12:07:31 -07:00
parent 61f3511fdf
commit 8c8d481dcf
1 changed files with 13 additions and 2 deletions

View File

@ -175,6 +175,7 @@ typedef struct FAudioGMS_StreamingSound
stb_vorbis_info info; stb_vorbis_info info;
float* streamBuffer; float* streamBuffer;
uint32_t streamBufferSize; uint32_t streamBufferSize;
uint32_t mostRecentBufferOffset; /* used for calculating track position */
} FAudioGMS_StreamingSound; } FAudioGMS_StreamingSound;
typedef struct FAudioGMS_SoundInstance typedef struct FAudioGMS_SoundInstance
@ -861,6 +862,8 @@ static void FAudioGMS_INTERNAL_SoundInstance_AddBuffer(FAudioGMS_SoundInstance*
instance->soundData.streamingSound.streamBufferSize = requiredStagingBufferSize; 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 */ /* NOTE: this function returns samples per channel, not total samples */
uint32_t sampleCount = stb_vorbis_get_samples_float_interleaved( uint32_t sampleCount = stb_vorbis_get_samples_float_interleaved(
instance->soundData.streamingSound.fileHandle, instance->soundData.streamingSound.fileHandle,
@ -927,6 +930,7 @@ double FAudioGMS_StreamingSound_LoadOGG(char* filePath)
instance->soundData.streamingSound.info = info; instance->soundData.streamingSound.info = info;
instance->soundData.streamingSound.streamBuffer = NULL; instance->soundData.streamingSound.streamBuffer = NULL;
instance->soundData.streamingSound.streamBufferSize = 0; instance->soundData.streamingSound.streamBufferSize = 0;
instance->soundData.streamingSound.mostRecentBufferOffset = 0;
FAudioGMS_INTERNAL_SoundInstance_AddBuffer(instance); FAudioGMS_INTERNAL_SoundInstance_AddBuffer(instance);
@ -1278,11 +1282,18 @@ double FAudioGMS_SoundInstance_GetTrackPositionInSeconds(double soundInstanceID)
FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID);
if (instance != NULL) if (instance != NULL)
{
if (instance->isStatic)
{ {
uint32_t sampleFrame = instance->voice.handle->src.curBufferOffset / sizeof(float); uint32_t sampleFrame = instance->voice.handle->src.curBufferOffset / sizeof(float);
return sampleFrame / instance->format.nSamplesPerSec; return sampleFrame / instance->format.nSamplesPerSec;
} }
else else
{
return ((double)instance->soundData.streamingSound.mostRecentBufferOffset + instance->voice.handle->src.curBufferOffset) / instance->format.nSamplesPerSec;
}
}
else
{ {
Log("Invalid sound instance!"); Log("Invalid sound instance!");
return -1.0; return -1.0;