BetterAudio plugin (lag-reducing and major improvements)

Description
This script allows you to incorporate some improvements in the audio management made by the game engine (as the needs of my game requires it). Now the script can do the next things:
  • Stop the audio and music when the game is out focus in all platforms.
  • Added a "Global Volume" option to the window options, and the possibility to hide the secondary volume controls. Also, the option can work in a quadratic function, improving the perception of changing volume.
  • Audio cache system that stores in cache recently used audio files, and also adds the possibility of preemptive cache future-audio files in events, map and battle (skills and items).
  • Option to always use (and only-use) OGG files. Then you can delete m4a files after you export the game (reducing size), but Safari will not play sound.
  • Added an option to mark audio files as used (these files will not be deleted when the game is exported with the "remove unused files" option).
  • Force to play a specified Bgm and Bgs on all maps when a switch is ON. The list of bgm, bgs and switches can be specify in the parameters of the plugin.

Important note
It is in beta since I have developed it for my game that uses RpgMakerMV 1.6.2 and CoreScript v1.3b, and I don't know if it works in other versions or if it gives errors. Also, I intend to add improvements to the script as my game needs arise. You can activate the debug parameter and see in the game console (F8) that this plugin is making, according to the options that you activate.

License
The MIT License. Credits appreciated but not required.

Known bugs
None, but tested only in RpgMakerMV 1.6.2 and CoreScript v1.3b.

Note and Changelog
Please put in this thread your experience with the plugin, errors that arise or anything else related.
* v0.9.0 Preload battle BGM and ME changes in events.
* v0.8.5 Refresh battle BGM cache on map changes.
* v0.8.4 Fixed a bug with possible empty play commands.
* v0.8.3 Added the option "Always use M4A files".
* v0.8.2 Fixed an error with encrypted preloading.
* v0.8.1 Avoid the need of map-cache to use the new option.
* v0.8.0 Option to force map bgm-bgs based on switches.
* v0.7.1 Added title bgm early load with DataManager.
* v0.7.0 Added map bgm and bgs early load with DataManager.
* v0.6.1 Fixed a reference bug with tmax variable.
* v0.6.0 Limit the max-preemptive cache to max-cache.
* v0.5.2 Fixed a bug with out-of-focus audio in mobile.
* v0.5.1 Organized plugin parameters.
* v0.5.0 Added the "Global Volume" options.
* v0.4.0 Added the option "Always use OGG files".
* v0.3.1 Solved a out-of-range bug with animationId.
* v0.3.0 First public release of the plugin.
Bump with a big update of the plugin:
* v0.6.0 Limit the max-preemptive cache to max-cache.
* v0.5.2 Fixed a bug with out-of-focus audio in mobile.
* v0.5.1 Organized plugin parameters.
* v0.5.0 Added the "Global Volume" options.
* v0.4.0 Added the option "Always use OGG files".
* v0.3.1 Solved a out-of-range bug with animationId.
Bump with a new big update of the plugin:
* v0.8.1 Avoid the need of map-cache to use the new option.
* v0.8.0 Option to force map bgm-bgs based on switches.
* v0.7.1 Added title bgm early load with DataManager.
* v0.7.0 Added map bgm and bgs early load with DataManager.
* v0.6.1 Fixed a reference bug with tmax variable.
I have no idea how good this plugin is compared with the other couple of audio plugins there.
it might be better, maybe the same, who knows, but it can definetly come in handy for
devs using many sounds effect/music effects that want it dynamic.

I dont see any fir for my game for now, but I keep my eyes on it when I need it or
want to try when Im using sounds/musics/bgm etc.
For now, as long as I know because others plugins didn't meet my game's requirement, I believe that is the only one capable to force the game to play a specified Bgm and Bgs on all maps when a switch is ON. But it's not my goal rank the audio plugins, but contribute to the community with the things that I'm doing for my game.

Thanks for share your interest.
I had some severe audio lag issues in my game, and this plug in has fixed that (not sure how or why, that's more your expertise I believe). I'm going to leave it in my project and will report back should I find any issues, but so far it seems to work flawlessly without impacting any other plugins or performance.

Gracias for the plugin, it might have solved my incredibly annoying audio lag issue! ^-^
I'm glad it works for you. I have updated the plugin correcting an error that occurred when exporting the game with encrypted resources.

Updated:
* v0.8.3 Added the option "Always use M4A files".
* v0.8.2 Fixed an error with encrypted preloading.
with always use m4a files, does that mean, you can drop ogg for pc use?
or pc still need ogg?, I think this one perform better than the ones I know off.

I Download this version, but I keep an eye on it for updates, if there are fixes,
improvements. (in case I lost this page, it will be better safe and sorry later 😀)
@ShadowDragon sadly no. The option to use only OGG or M4A is there in order to allow us to export into different platforms and then reduce safely the size of the bundle by deleting the M4A or OGG files respectively. In fact:
  • Always use M4A files: You can delete ogg files after you export the game (reducing size) but only works when we export the game as a website (works in any browser).
  • Always use OGG files: You can delete m4a files after you export the game (reducing size), but Safari browser will not play sound. So it's recommended for local exports (Windows, MacOsX, Android, etc).
The plugin tries to reduce audio lang (among a lot of other things) by preloading the audio files as soon as the program knows that they will be used (in events, in battle actions or in map loading).
Ic, in that way, that makes it clearer.
thanks for the explaination.
I might have found an incompatibility. So, I'm using Yanfly's battle engine core [ http://www.yanfly.moe/wiki/Battle_Engine_Core_(YEP) ] and whenever battle starts, the game halts after a few seconds with these errors:

Failed to load resource: net::ERR_FILE_NOT_FOUND
audio/me/.ogg Failed to load resource: net::ERR_FILE_NOT_FOUND
audio/bgm/.ogg Failed to load resource: net::ERR_FILE_NOT_FOUND
audio/me/.ogg Failed to load resource: net::ERR_FILE_NOT_FOUND
audio/bgm/.ogg Failed to load resource: net::ERR_FILE_NOT_FOUND
audio/me/.ogg Failed to load resource: net::ERR_FILE_NOT_FOUND
audio/bgm/.ogg Failed to load resource: net::ERR_FILE_NOT_FOUND
audio/me/.ogg Failed to load resource: net::ERR_FILE_NOT_FOUND

this only happens when pre-caching in your plug-in is turned on, without it, the errors don't occur. Turning off battle pre-caching doesn't work either, unfortunately.

I'm using quite a host of plugins, but I assume it's the battle engine one that is incompatible since the issue occurs quite directly after initiating battle?

I'm not asking you to make sure your plugin is compatible, of course, but maybe there is some easy work around to b e found?

Thanks for your time!
Thank you a ton for this script!
I think audio is one of the weakest parts of the MV engine.
Do you think instead of audio caching, we could implement a library that allows the game to stream the OGG audio in real time?
I might have found an incompatibility. So, I'm using Yanfly's battle engine core [ http://www.yanfly.moe/wiki/Battle_Engine_Core_(YEP) ] and whenever battle starts, the game halts after a few seconds with these errors:

Failed to load resource: net::ERR_FILE_NOT_FOUND
audio/me/.ogg Failed to load resource: net::ERR_FILE_NOT_FOUND
audio/bgm/.ogg Failed to load resource: net::ERR_FILE_NOT_FOUND
audio/me/.ogg Failed to load resource: net::ERR_FILE_NOT_FOUND
audio/bgm/.ogg Failed to load resource: net::ERR_FILE_NOT_FOUND
audio/me/.ogg Failed to load resource: net::ERR_FILE_NOT_FOUND
audio/bgm/.ogg Failed to load resource: net::ERR_FILE_NOT_FOUND
audio/me/.ogg Failed to load resource: net::ERR_FILE_NOT_FOUND

this only happens when pre-caching in your plug-in is turned on, without it, the errors don't occur. Turning off battle pre-caching doesn't work either, unfortunately.

I'm using quite a host of plugins, but I assume it's the battle engine one that is incompatible since the issue occurs quite directly after initiating battle?

I'm not asking you to make sure your plugin is compatible, of course, but maybe there is some easy work around to b e found?

Thanks for your time!

@Yadoukai I'm sure that is not Battle_Engine_Core_(YEP) as long as I'm using it without problem. Can you please PM me the stacktrace error log with the debug option of my plugin on? Also, in this battle do you have no-background sound?

@Ozuma I was using this https://forums.rpgmakerweb.com/inde...mprove-rpg-maker-mv-audio-performance.110063/ posted here but it doesn't work well for me. In this case the lag occurs sometimes in middle bgm, and looping marks didn't always work (at least for me).
@Yadoukai I'm sure that is not Battle_Engine_Core_(YEP) as long as I'm using it without problem. Can you please PM me the stacktrace error log with the debug option of my plugin on? Also, in this battle do you have no-background sound?

You had the right idea immediately, setting no battle bgm or victory me will produce the error. I did that because I didn't need a victory me and the battle bgm is supposed to change according to the battle (so I set it to none in the database). I've sent you the error log, even though you probably already know what caused the issue anyway. Hope it's okay.

Is there a sort of easy way around this? If not, just as good, I don't want to add to your workload. ^-^

Thanks for looking into this!
Thanks to @Yadoukai for reporting. Now, the bug related to play empty commands is fixed.
* v0.8.4 Fixed a bug with possible empty play commands.
Nice!
I originally made my plugin so others would start making better plugins for Audio.
on Slow PCs and pretty much all mobiles that Lag was horrendous and MV deletes and reloads Audio everytime you Stop and start it!.
then i found out HTML5 Audio is tied to the frame rate and MV drops frames every time you change scenes causing a stutter.

Thank you a ton for this script!
I think audio is one of the weakest parts of the MV engine.
Do you think instead of audio caching, we could implement a library that allows the game to stream the OGG audio in real time?

HTML5 Audio does that,
but becuase MV will drop frames every time you change scenes you will get a stutter effect.
it also doesnt support pitch shifting and has terrible looping capabilities.
what different about this plugin with AudioStreaming.js ?
what different about this plugin with AudioStreaming.js ?
mainly this plugin doesn't stream audio in any capacity.

this plugin scans your data files whenever a map is loaded, mostly maps and events for any instance of Audio being played and preloads those files so that they will be played immediately. it still decodes the entire file into memory before playing the sound. the decoding part is what slows MV down when trying to play sounds (huge delay in playback), hence why we preload it.
I'll be following this plugin. 🙂
It could be a very tempting alternative in case Kuramubon's AudioStreaming.js gives problems...
Sorry about the double post, but since I wrote one yesterday, I thought and edit might not be reported by the plugin creator.

I've found a little issue when trying your plugin in battle.

If in the middle of a battle, if your character or an event is playing a SE, and the enemy is doing some kind of action that precedes an animation with another SE, then the character/event SE will be silenced.

It's a strange issue, but it can be noticiable if it happens.

EDIT: Also, there's another issue when you are playing on a map and there are encounters

In the first encounter, the battle BGM will delay until it starts playing. This only happens during the first battle, since the audio file is not pre-loaded, I suppose.

EDIT 2: When you setup a parallel process event with a BGM playing, the console will throw a lot of debug codes saying that the audio is being preloaded and stored in cache. Eventually, if you take a look at the Windows task manager, will produce an amount of memory consumption, not only from the RAM, but from the CPU too.

Original thread: https://forums.rpgmakerweb.com/threads/betteraudio-plugin-lag-reducing-and-major-improvements.120169/