Skip to content

Commit 16b080b

Browse files
committed
Fix crashes on unseekable or pure empty vorbis files
1 parent 549c1ad commit 16b080b

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

source/lime/_internal/backend/native/NativeAudioSource.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ class NativeAudioSource {
372372

373373
if (playing) {
374374
var timeRemaining = (getLength() - value) / getPitch();
375-
if (timeRemaining < 8) complete();
375+
if (timeRemaining < 8 && value > 8) complete();
376376
else {
377377
completed = streamEnded = false;
378378
if (streamed) {

source/lime/media/AudioBuffer.hx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,29 @@ class AudioBuffer
312312
if (vorbisFile == null) return null;
313313

314314
var info = vorbisFile.info();
315+
if (info == null) return null;
315316

316317
var audioBuffer = new AudioBuffer();
317318
audioBuffer.channels = info.channels;
318319
audioBuffer.sampleRate = info.rate;
319320
audioBuffer.bitsPerSample = 16;
320-
audioBuffer.__srcVorbisFile = vorbisFile;
321+
322+
if (!vorbisFile.seekable() ||
323+
vorbisFile.pcmTotal() < #if lime_cffi @:privateAccess lime._internal.backend.native.NativeAudioSource.STREAM_BUFFER_SAMPLES #else 0x4000 #end)
324+
{
325+
// convert it to static if its too short or unseekable.
326+
vorbisFile.rawSeek(0);
327+
328+
var isBigEndian = lime.system.System.endianness == lime.system.Endian.BIG_ENDIAN;
329+
var bytes:Bytes = Bytes.alloc(Std.int(haxe.Int64.toInt(vorbisFile.pcmTotal()) * info.channels * 2));
330+
var total = 0, result = 0;
331+
do {
332+
total += (result = vorbisFile.read(bytes, total, 0x1000, isBigEndian, 2, true));
333+
} while (result > 0);
334+
335+
audioBuffer.data = new UInt8Array(bytes);
336+
}
337+
else audioBuffer.__srcVorbisFile = vorbisFile;
321338

322339
return audioBuffer;
323340
}

0 commit comments

Comments
 (0)