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.
This commit is contained in:
@@ -27,6 +27,7 @@ class NativeAudioSource
|
||||
private static var STREAM_TIMER_FREQUENCY = 100;
|
||||
|
||||
private var buffers:Array<ALBuffer>;
|
||||
private var bufferTimeBlocks:Array<Float>;
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user