Title BGM Lag (Fixed)

Hey, I've got a little problem with my project, in the opening of the game, the BGM that I chose to play on the title screen plays tiny bit later than the animations. What happens is > the engine draws the background picture > 0.5 pause > and then draws command window and starts the BGM simultaneously.

I'm using MoG hunter plugins for title animations but I tried turning them off and it still has that 0.5 sec delay, I tested it even on a default project, you can try it too, there is tiny lag between the background showing and the BGM playing. And with the extra animations I've put at the start it's noticeable.

It's not bugging me THAT much but if there is a solution I'd like to know it.
It wouldn't surprise me that much if the lag is simply the time it takes for the game to load in the background track, but I'm not sure. If that is it, probably the best you could do would be a mod that preloads the track (so you just see a "Loading" screen instead of the menu while it's loading).
Can you also share the title BGM? It's also possible that there is a delayed start in the song itself, especially if it's a MP3 file converted to OGG
Can you also share the title BGM? It's also possible that there is a delayed start in the song itself, especially if it's a MP3 file converted to OGG
I checked that already, I even cut a part of the BGM with Audible, the OGG file itself, but It did not work.
As I said, this is visible even on the default new project if you are observant. The background is drawn then few frames later the command window is drawn and the BGM starts. It's a tiny lag between them, it's not that big of a deal if they are misaligned, I'm not a AAA developer. But It annoys me a little.
I also tried to pre-load the BGM with the game starting as Solar_Flare suggested, but I am not that good at coding so I failed at achieving what I wanted.
You should be wary of editing BGMs with a program like Audible. The RTP tracks in particular contain special tags specifying how the game should loop them. I don't know if Audible would preserve those tags, but even if it does, merely preserving them isn't enough if you're cutting off a portion of the track – they would need to be updated to reflect the altered track length. I would expect many non-RTP tracks to contain similar tags. I am not sure whether these tags are a general standard or if they are specific to RPGMaker.
Try this as a plugin - it will run the AudioManager first when $dataSystem is loaded and will wait until the ._sourceNode (the audio itself) is loaded before fading into the title during the Scene Boot.

The const alias_load is for if you do not want the game to show the "loading" picture since we are at the beginning of the game. You can remove this method if you would like - I just find it distracting to have it come on for a second just to have it go off.

(not fully tested)
Spoiler
JavaScript:
void (($) => { // Scene_Boot
    // Overwrite method to check for data system and audio source node
    $.isReady = function() {
        if (Scene_Base.prototype.isReady.call(this)) {
            return DataManager.isDatabaseLoaded() && this.isGameFontLoaded()
                && !!AudioManager._bgmBuffer._sourceNode;
        } else {
            if ($dataSystem && !AudioManager._bgmBuffer) {
                AudioManager.playBgm($dataSystem.titleBgm);
            }
            return false;
        }
    }

    // optional - removes loading picture from scene boot and scene title
    const alias_load = Graphics._paintUpperCanvas;
    Graphics._paintUpperCanvas = function() {
        const name = SceneManager._scene.constructor.name;
        if (['Scene_Boot', 'Scene_Title'].includes(name)) return;
        alias_load.call(this);
    }
})(Scene_Boot.prototype);

This is also assuming you are not using a preloading plugin as well. That might be a better option overall if it works for you.
The RTP tracks in particular contain special tags
Yes, these tags only matter if the BGM is not looped by itself. But the BGM I use is 90 sec, I doubt anyone will stay at the title screen that long.
JavaScript:
void (($) => { // Scene_Boot
// Overwrite method to check for data system and audio source node
$.isReady = function() {
if (Scene_Base.prototype.isReady.call(this)) {
return DataManager.isDatabaseLoaded() && this.isGameFontLoaded()
&& !!AudioManager._bgmBuffer._sourceNode;
} else {
if ($dataSystem && !AudioManager._bgmBuffer) {
AudioManager.playBgm($dataSystem.titleBgm);
}
return false;
}
}

// optional - removes loading picture from scene boot and scene title
const alias_load = Graphics._paintUpperCanvas;
Graphics._paintUpperCanvas = function() {
const name = SceneManager._scene.constructor.name;
if (['Scene_Boot', 'Scene_Title'].includes(name)) return;
alias_load.call(this);
}
})(Scene_Boot.prototype);
This worked like a charm. The only problem is that I use MOG_Hunter plugins for splash screen images at the start, and the BGM was starting the moment the splash screen appeared, not the title.
I did modify the code a bit.

So for anyone in the future stumbling upon this threat > the code above works fine.
This is what I used in my specific case:
Spoiler
JavaScript:
void (($) => {

    const _isReady = $.isReady || Scene_Base.prototype.isReady;

    $.isReady = function () {
        if (_isReady.call(this)) {

            if ($dataSystem && !this._titleBgmPreloaded) {
                AudioManager.playBgm($dataSystem.titleBgm);
                this._titleBgmPreloaded = true;
            }

            return !!AudioManager._bgmBuffer && !!AudioManager._bgmBuffer._sourceNode;
        } else {
            return false;
        }
    };

})(Scene_Title.prototype);


(function () {
    const alias_load = Graphics._paintUpperCanvas;
    Graphics._paintUpperCanvas = function () {
        const scene = SceneManager._scene;
        const name = scene && scene.constructor ? scene.constructor.name : '';
        if (name === 'Scene_Boot' || name === 'Scene_Title') return;
        alias_load.call(this);
    };
})();
But the BGM I use is 90 sec
…that's not long at all. I would say it's basically guaranteed that someone will stay at the title screen for that long. Anyone who starts out with Options, for example.
Yes, these tags only matter if the BGM is not looped by itself. But the BGM I use is 90 sec, I doubt anyone will stay at the title screen that long.

This worked like a charm. The only problem is that I use MOG_Hunter plugins for splash screen images at the start, and the BGM was starting the moment the splash screen appeared, not the title.
I did modify the code a bit.

So for anyone in the future stumbling upon this threat > the code above works fine.
This is what I used in my specific case:
Spoiler
JavaScript:
void (($) => {

    const _isReady = $.isReady || Scene_Base.prototype.isReady;

    $.isReady = function () {
        if (_isReady.call(this)) {

            if ($dataSystem && !this._titleBgmPreloaded) {
                AudioManager.playBgm($dataSystem.titleBgm);
                this._titleBgmPreloaded = true;
            }

            return !!AudioManager._bgmBuffer && !!AudioManager._bgmBuffer._sourceNode;
        } else {
            return false;
        }
    };

})(Scene_Title.prototype);


(function () {
    const alias_load = Graphics._paintUpperCanvas;
    Graphics._paintUpperCanvas = function () {
        const scene = SceneManager._scene;
        const name = scene && scene.constructor ? scene.constructor.name : '';
        if (name === 'Scene_Boot' || name === 'Scene_Title') return;
        alias_load.call(this);
    };
})();
I would simplify even more to this and just create it for the Scene Title:
Spoiler
JavaScript:
void (($, $$) => { // Scene_Title, Graphics

    $.isReady = function() {
        if (Scene_Base.prototype.isReady.call(this)) {
            return !!AudioManager._bgmBuffer._sourceNode;
        }
    
        return false;
    }

    const alias_load = $$._paintUpperCanvas;
    $$._paintUpperCanvas = function() {
        const name = SceneManager._scene.constructor.name;
        if (['Scene_Boot', 'Scene_Title'].includes(name)) return;

        alias_load.call(this);
    }

})(Scene_Title.prototype, Graphics);
That way you are only waiting for the source node since data system is already loaded and other factors should already be present before the title even starts.

but I am not that good at coding so I failed at achieving what I wanted.

Everyone starts somewhere, though, it looks like you are pretty competent or at least have a good handle on the code to where you could reference my code and make it into something that worked for you. Congrats and cheers!

Original thread: https://forums.rpgmakerweb.com/threads/title-bgm-lag-fixed.181103/