I have almost two dozen sound files. Majority of them are 8KB in size. Only three of them are 12KB and 16KB. Under the audio.loadSound() section of the audio API, it says You should use this to load all your short sounds, especially ones you may play frequently. For best results, load all the sounds at the launch of your app or the start of a new level.
So I do the following, but I'm not entirely sure if this is the right way to go:
-- load all sound effects when game app launches-- most of these sounds are played often in each game modules_G.mySound1 = audio.loadSound("mySound1.aac")_G.mySound2 = audio.loadSound("mySound2.aac")_G.mySound3 = audio.loadSound("mySound3.aac")_G.mySound4 = audio.loadSound("mySound4.aac")-- I continue to add more, but you get the idea
In my game module where sounds need to play:
-- localize globally loaded sound-- I include ones that are played in this module-- in other words, I don't localize all globally loaded sound in each modulelocal mySound2 = _G.mySound2local mySound3 = _G.mySound3local mySound4 = _G.mySound4--when I need to play the soundaudio.play(mySound2)audio.play(mySound3)audio.play(mySound4)
I localized the globally loaded sounds because I hear localizing is important for performance (and tip #1 says I should too). In addition, I remember reading something about table-look-up impacting performance, and _G is a table, isn't it?
So, instead of doing audio.play(_G.mySound1), I do audio.play(mySound1) by localizing the globally loaded sound. By doing this, I don't need to load & dispose and then load the same sound again when I play one level after another (or when I reload a same level to play again.)
The thing is, currently, I don't dispose the local variables such as mySound1 when I unload the module even though the audio.loadSound() section of the audio API says: Please note that you are responsible for unloading (cleaning up) any audio files you load with this API. Use the audio.dispose() API to clean up audio handles you are completely done with them and want to unload from memory to get back more RAM.
When I try disposing localized version of the globally loaded sound like audio.dispose(mySound1), it gave me an error, so I decided not to dispose these local variables at all. And, I've been monitoring if there's any memory leak associated with this practice -- but so far, I haven't seen any issue.
However, I'm not entirely sure how I set up my sound load/play is a good practice. Is it better if I load the sound at the start of each level (and not use _G table at all)?
But then, if I don't use the _G table and load/dispose sound at start/end of each game level, I will essentially be unloading a dozen sound files and then reloading the same dozen sound files immediately after disposing them. So this feels wrong too.
Any thoughts? I'd like to hear how others are handling a case like this, and if there's a better approach I should use.