From 8660a189ba7b6e452ad46f1b035a2134c46b2553 Mon Sep 17 00:00:00 2001 From: Justin Espedal Date: Fri, 10 Jun 2022 13:23:53 +0900 Subject: [PATCH] Fix getCurrentTime for streaming native sounds vorbisFile.timeTell() returns the current decoding offset in seconds, and it's updated each time vorisFile.read() is called. Since data is buffered by calling vorbisFile.read() multiple times, the associated time values also need to be preserved in order to get the correct time offset. Without this change, getCurrentTime() will return some time in the future relative to the amount of buffered data. --- .../_internal/backend/native/NativeAudioSource.hx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lime/_internal/backend/native/NativeAudioSource.hx b/src/lime/_internal/backend/native/NativeAudioSource.hx index e15e41360..ee3cc8031 100644 --- a/src/lime/_internal/backend/native/NativeAudioSource.hx +++ b/src/lime/_internal/backend/native/NativeAudioSource.hx @@ -27,6 +27,7 @@ class NativeAudioSource private static var STREAM_TIMER_FREQUENCY = 100; private var buffers:Array; + private var bufferTimeBlocks:Array; private var completed:Bool; private var dataLength:Int; private var format:Int; @@ -103,10 +104,12 @@ class NativeAudioSource dataLength = Std.int(Int64.toInt(vorbisFile.pcmTotal()) * parent.buffer.channels * (parent.buffer.bitsPerSample / 8)); buffers = new Array(); + bufferTimeBlocks = new Array(); for (i in 0...STREAM_NUM_BUFFERS) { buffers.push(AL.createBuffer()); + bufferTimeBlocks.push(0); } handle = AL.createSource(); @@ -214,6 +217,12 @@ class NativeAudioSource var buffer = new UInt8Array(length); var read = 0, total = 0, readMax; + for (i in 0...STREAM_NUM_BUFFERS-1) + { + bufferTimeBlocks[i] = bufferTimeBlocks[i + 1]; + } + bufferTimeBlocks[STREAM_NUM_BUFFERS-1] = vorbisFile.timeTell(); + while (total < length) { readMax = 4096; @@ -364,7 +373,7 @@ class NativeAudioSource { if (stream) { - var time = (Std.int(parent.buffer.__srcVorbisFile.timeTell() * 1000) + Std.int(AL.getSourcef(handle, AL.SEC_OFFSET) * 1000)) - parent.offset; + var time = (Std.int(bufferTimeBlocks[0] * 1000) + Std.int(AL.getSourcef(handle, AL.SEC_OFFSET) * 1000)) - parent.offset; if (time < 0) return 0; return time; }