February 26, 2014

Tips & Tricks for Unity Audio Memory Usage and Optimization

Have too much good audio? 

Here are some tips and facts about memory usage and optimization in Unity with Master Audio.

Unless you are using Resource files (see below for more info), then the amount of memory used by Master Audio will remain constant per Scene. It's not dynamic. 

Typically, to optimize memory usage, we do the following:

1) If the target platform is mobile, we resample all audio clips to 22050Hz so they're smaller. We can't tell the difference in sound. We use .wav files for all sound effects, although some users report success with .ogg files as well. We don’t like to use compressed audio for sound effects because there are memory and performance implications. Keep it raw!

2) Set all music clips to streaming which basically take up zero memory (although the profiler doesn't show this correctly). Note: don't stream more than one audio clip at a time, that's terrible on performance.

3) Set up infrequently used sounds in a Resources folder and configure the Audio Origin as "Resource File" so they will be loaded only when played and promptly unloaded after done playing.

4) If you only need certain sounds in certain Scenes, set up a separate Master Audio per Scene only with needed sounds, or use the Dynamic Sound Group Creator prefab to populate those sounds in each Scene if you have a "persist across Scenes" Master Audio.

All Sound Group Variation audio clips (that aren't Resource files) are loaded into memory when the Scene starts. Extra clones of Variations take up zero memory because they're the same audio clip. Playing a sound does not make the audio memory change (unless it's a resource file).

Have fun and rock on!

Cool image from: http://www.androidtapp.com/audio-glow-music-visualizer/