SE Audio Lag/Caching Problem

Hello everybody, this is my first post on this forum, so please forgive me if this is the wrong category.

In my MZ project the protagonist is sitting in a wheelchair and I want the wheelchair sound to start playing as soon as the character moves. The SE I use for this is a 7.08kb small .ogg but it nonetheless only starts playing after about 1.5 seconds in and also only stops after about this time after the character has already stopped.
I wrote my own plugin to cache the SE but this didn't remove the sound lag.
(function() {
var wheelchairAudio = 'wheelchair.ogg';

DataManager.preloadWheelchairAudio = function() {
AudioManager.createBuffer('se', wheelchairAudio);
};

var _DataManager_loadDatabase = DataManager.loadDatabase;
DataManager.loadDatabase = function() {
_DataManager_loadDatabase.call(this);
this.preloadWheelchairAudio();
};
})();
I also tried some of the preloaders provided at this forum, but not a single one is working for me.
The method I use to play the SE is by calling a common event with a parallel event script call on the map by using

$gameTemp.reserveCommonEvent(1)

and the common event (1) contains

If: Script: $gamePlayer.isMoving()
Play SE : Wheelchair (90, 100, 0)
End
If: Script: !gamePlayer.isMoving()
Stop SE
End

Has anyone found a solution to this already?
Load/decode/decompression delay seems unlikely to be as long as 1.5 seconds in this case. I think this is more likely to be a problem with event timing/logic.

Have you tried using a "footstep" or "walking sound" plugin? E.g.

$gameTemp.reserveCommonEvent(1)
This tells the engine to queue common event 1 to run at the next opportunity. If some other non-Parallel event is already running, then this will create a delay. It is also a Script command, so it wastes some processing power by recompiling the code every time you call it.

By contrast, the Common Event command creates a child interpreter that runs the specified event as a subroutine of the calling event. This means your common event will be processed as part of the Parallel event, so no delays or anything.

If: Script: $gamePlayer.isMoving()
Play SE : Wheelchair (90, 100, 0)
End
If: Script: !gamePlayer.isMoving()
Stop SE
End
Two points here:
  1. character.isMoving() checks whether the character sprite is at fractional tile coordinates. Each frame, events are processed before the player. So events will report $gamePlayer.isMoving() based on whether the player sprite was between tiles in the previous frame.

    You can work around this by also checking for player input. E.g. here is the Is Player Moving? example from the MV / MZ Script Call List:

    var p = $gamePlayer, d = p.getInputDirection(); p.isMoving() || (d > 0 && p.canPass(p.x, p.y, d)) || $gameTemp.isDestinationValid()

  2. You have a typo in the second branch condition. Instead, consider using a single If...Else.

Spoiler: re your smol plugin
AudioManager.createBuffer('se', wheelchairAudio);
Note that this just creates a new audio buffer...it doesn't preload anything. The engine doesn't implement a generic audio cache like it does for images: it loads most audio from disk every time. (I'm not certain exactly what sort of auto-caching, if any, is used by the browser runtime, e.g. Chromium for standalone deployment.)

To reliably cache audio, you can use a static buffer approach, like the engine does with system sounds: load the audio once, then redirect to that buffer whenever that audio is requested.
JavaScript:
AudioManager.loadStaticSe({ name: "wheelchair", volume: 10, pitch: 100, pan: 0 });
JavaScript:
AudioManager.playStaticSe({ name: "wheelchair", volume: 10, pitch: 100, pan: 0 });
Note that statically-buffered audio cannot play more than 1 instance at a time, because it's all using the same buffer. In theory you could get around this by cloning the buffer each time, but I haven't tried that personally.
The method I use to play the SE is by calling a common event with a parallel event script call on the map by using
$gameTemp.reserveCommonEvent(1)
This seems odd. You shouldn't ever call a parallel event, you just turn it on and let it run in parallel.
I did not look at the several different Footstep plugins so far, no. Only the sound plugins so far. Thank you for the tip.
There are no other events at the map at all, since I am always testing plugins isolated from the rest to see if it would work on it's own. I can sort out conflicts later.

var p = $gamePlayer, d = p.getInputDirection(); p.isMoving() || (d > 0 && p.canPass(p.x, p.y, d)) || $gameTemp.isDestinationValid()

,looks pretty good, thank you for pointing me towards the Script Call List.

The typo is caused by copying those lines by hand since it won't copy&paste the event page from the IDE, my bad.

That the script doesn't preload anything is good to know. I am not a sound guy, I have ever only made load/save code in js, I am a C# user normaly and pretty new to RPG Maker. This also explains why I heavily rely on scripts instead of the in-built event handling I guess. I try to get more into the IDE internal handling of stuff in the future.

I guess I make it easy on me for now and try out Gabe's plugin. Thank you for your detailed help, I appreciate that.
This also explains why I heavily rely on scripts instead of the in-built event handling I guess.
Just as a piece of advice - it's worth your time to learn how the event system works. There are plenty of things that are going to be both easier and more efficient to do through event commands than through script.

Even coders proficient with JavaScript/MZ's engine don't use it for everything.

Good luck with your project!
This code now works when I call the common event without the use of a script. Thank you all.

(function() {
// Preload the wheelchair SE
var wheelchairSe = { name: 'wheelchair', pan: 90, pitch: 100, volume: 100 };
AudioManager.loadStaticSe('Wheelchair', wheelchairSe);

// Variable to track whether wheelchair sound is playing
var wheelchairSoundPlaying = false;

// Function to check if the player is moving
function isPlayerMoving() {
var player = $gamePlayer;
var direction = player.getInputDirection();
return player.isMoving() || (direction > 0 && player.canPass(player.x, player.y, direction));
}

// Function to play the wheelchair SE
function playWheelchairSound() {
if (!wheelchairSoundPlaying) {
AudioManager.playStaticSe('Wheelchair');
wheelchairSoundPlaying = true;
}
}

// Function to stop the wheelchair SE
function stopWheelchairSound() {
if (wheelchairSoundPlaying) {
AudioManager.stopAll();
wheelchairSoundPlaying = false;
}
}

// Update the wheelchair sound on player movement
var _Game_Player_update = Game_Player.prototype.update;
Game_Player.prototype.update = function(sceneActive) {
_Game_Player_update.call(this, sceneActive);
if (sceneActive) {
if (isPlayerMoving()) {
playWheelchairSound();
} else {
stopWheelchairSound();
}
}
};
})();
This thread is being closed due to being solved. If you need it reopened, report it to let us know why. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/se-audio-lag-caching-problem.166690/