MV Swapping the file of an AudioBuffer

Hi, I'm attempting to create a plugin that will swap a currently playing audio track with another track that is arranged to be very similar (identical in length and tempo) to make some dynamic music changes. I'm currently able to do as I want, by saving the current music track's position before the buffer gets cleared and playing the other music track with that saved position.
However, doing it this way creates a very short, but noticeable "gap" where nothing is playing since the AudioManager needs to stop the first buffer before creating the second one and slightly throws off the rhythm. I would've preferred to just be able to switch the currently playing audio buffer's loaded track so that the audio continues playing without stopping at all. I've been looking through the source code including the AudioManager and WebAudio classes but can't seem to find how exactly RPG Maker actually loads or stores the audio file beyond using the createBuffer function and a URL. Swapping the URL alone doesn't change the buffer's track, and I tried using the private ._load(url) function to no avail. Maybe I'm not understanding something here.

My main question then, is it possible within RPG Maker to change the track of a currently playing audio buffer to have a seamless switch to a different audio file without pausing or stopping, or should I just stick with what I've already implemented even if it has a small hiccup?
By default RMMV/Z load the entire audio file, then decrypt it if necessary, then decode it. Only then can it start playing. That can cause unwanted pauses like you describe with larger files and/or slower file access.

To eliminate such delays, maybe create an extra buffer ahead of time, load your track into that, then swap the buffers as needed.

Technically the Web Audio AudioBuffer interface is only really designed for shorter tracks. If you can find a plugin that implements streaming audio for RMMV then that might be an alternative solution.
Question: is it possible to play two audios at the same time, but set the volume of #2 to 0, when you want to swap them, fadeout #1 and fadin #2?
is it possible to play two audios at the same time
Not using RPG Maker's default audio functionality. The first thing playBgm() does is check whether you're telling it to play the BGM that's already playing; if not, it stops the current audio.
OK, I guess fading/mixing two bgms is not an option for now. 😛
Unless you use BGS as the second BGM.
@utunnels Regardless, that seems like an overly complicated route; it's basically the same thing that caethyril suggested, just more complex to have it actually "playing" and the other processes that go along with that, rather than just having the second buffer loaded.
It's getting a bit off-topic I guess.
I thought maybe a little "transition" time will make it smooth, instea of sudden stop and play.
Some games even cut the music into pieces and make the transition with them, when you need to switch, it let current piece of music finish before playing its next node.
By default RMMV/Z load the entire audio file, then decrypt it if necessary, then decode it. Only then can it start playing. That can cause unwanted pauses like you describe with larger files and/or slower file access.

To eliminate such delays, maybe create an extra buffer ahead of time, load your track into that, then swap the buffers as needed.

Technically the Web Audio AudioBuffer interface is only really designed for shorter tracks. If you can find a plugin that implements streaming audio for RMMV then that might be an alternative solution.
As far as I'm aware, the Web AudioAudioBuffer seems to be used for all tracks regardless of length, at least natively (not including those that need to be decrypted of course.)

I experimented with using two buffers before and got an identical result as the other implementation, but I just implemented the 2nd buffer being created before the swap happens and it creates the seamless transition I was looking for. Looks like the hang up was caused by loading the audio file on demand as you said. Thanks!
As far as I'm aware, the Web AudioAudioBuffer seems to be used for all tracks regardless of length
Mh, like I said, RMMV/Z just use AudioBuffer (via decodeAudioData) for everything, regardless of duration. The Web Audio API offers alternatives better suited to longer tracks, e.g. via createMediaElementSource or createMediaStreamSource, which I think could (with some difficulty) be implemented by a plugin.

I just implemented the 2nd buffer being created before the swap happens and it creates the seamless transition I was looking for. Looks like the hang up was caused by loading the audio file on demand as you said. Thanks!
Great, you're welcome! Happy RPG Making~ 😂
As a bonus, to throw this out there if you don't want to go the custom plugin route, CretaPark made an FMOD integration for MV. I believe FMOD allows for dynamic music. There may be a learning curve to this, though.
Regardless;

Original thread: https://forums.rpgmakerweb.com/threads/swapping-the-file-of-an-audiobuffer.169995/