From 1544d1464b60865f2d572383d0ef130530193cc3 Mon Sep 17 00:00:00 2001 From: Ralty <78720179+Raltyro@users.noreply.github.com> Date: Tue, 15 Jul 2025 18:27:01 +0700 Subject: [PATCH] Fix AudioSwitchFix again --- source/flixel/sound/FlxSound.hx | 2 +- .../backend/system/modules/AudioSwitchFix.hx | 35 ++++++++++++------- .../backend/native/NativeAudioSource.hx | 2 +- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/source/flixel/sound/FlxSound.hx b/source/flixel/sound/FlxSound.hx index 0716dc06..476b21f9 100644 --- a/source/flixel/sound/FlxSound.hx +++ b/source/flixel/sound/FlxSound.hx @@ -674,7 +674,7 @@ class FlxSound extends FlxBasic { _paused = false; _time = startTime; _lastTime = FlxG.game.getTicks(); - if (_channel == null || !_channel.__isValid || _source.__backend == null #if lime_cffi || _source.__backend.disposed || _source.__backend.handle == null #end) + if (_channel == null || !_channel.__isValid || _source == null #if lime_cffi || _source.__backend.disposed || _source.__backend.handle == null #end) makeChannel(); if (_channel != null) { diff --git a/source/funkin/backend/system/modules/AudioSwitchFix.hx b/source/funkin/backend/system/modules/AudioSwitchFix.hx index 25d904db..3e89e373 100644 --- a/source/funkin/backend/system/modules/AudioSwitchFix.hx +++ b/source/funkin/backend/system/modules/AudioSwitchFix.hx @@ -15,19 +15,24 @@ import haxe.Timer; @:dox(hide) class AudioSwitchFix { public static function onAudioDisconnected() @:privateAccess { - var soundList:Array = [FlxG.sound.music]; - for (sound in FlxG.sound.list) if (sound.playing) { - sound.pause(); - soundList.push(sound); + var sources:Array<{source:AudioSource, playing:Bool, time:Float, gain:Float, pitch:Float, position:lime.math.Vector4}> = []; + for (source in AudioSource.activeSources) { + var wasPlaying = source.playing; + sources.push({ + source: source, + playing: wasPlaying, + time: source.currentTime, + gain: source.gain, + pitch: source.pitch, + position: source.position + }); + + source.__backend.dispose(); + if (wasPlaying) source.__backend.playing = true; } - var i = AudioSource.activeSources.length; - while (i-- > 0) AudioSource.activeSources[i].dispose(); - AudioManager.shutdown(); - AudioManager.init(); - Main.changeID++; // #if !lime_doc_gen // if (AudioManager.context.type == OPENAL) // { @@ -39,12 +44,18 @@ class AudioSwitchFix { // alc.processContext(ctx); // } // #end + + for (d in sources) { + d.source.__backend.init(); + d.source.currentTime = d.time; + d.source.gain = d.gain; + d.source.pitch = d.pitch; + d.source.position = d.position; - for (sound in soundList) { - sound.makeChannel(); - sound.resume(); + if (d.playing) d.source.play(); } + Main.changeID++; Main.audioDisconnected = false; } diff --git a/source/lime/_internal/backend/native/NativeAudioSource.hx b/source/lime/_internal/backend/native/NativeAudioSource.hx index 75832ee4..478ed150 100644 --- a/source/lime/_internal/backend/native/NativeAudioSource.hx +++ b/source/lime/_internal/backend/native/NativeAudioSource.hx @@ -106,7 +106,7 @@ class NativeAudioSource { streamed = false; samples = (dataLength = (buffer.data.length >> 0)) / buffer.channels / (buffer.bitsPerSample >> 3); - if (buffer.__srcBuffer == null && (buffer.__srcBuffer = AL.createBuffer()) != null) + if ((buffer.__srcBuffer == null || (AL.getBufferi(buffer.__srcBuffer, AL.SIZE) >> 0) != dataLength) && (buffer.__srcBuffer = AL.createBuffer()) != null) AL.bufferData(buffer.__srcBuffer, format, buffer.data, buffer.data.length, buffer.sampleRate); AL.sourcei(handle, AL.BUFFER, buffer.__srcBuffer);