REQ: directives for multiple SoundPool allocation

Monkey Targets Forums/Android/REQ: directives for multiple SoundPool allocation

Nobuyuki(Posted 2013) [#1]
Hello,

This is a feature request specific to Android. Currently, mojo only allocates one SoundPool resource for all the sound effects in a game. This is usually not a problem, and probably a good thing for most uses because it minimizes heap allocation, however, as many of us have discovered, Android only allows 1mb of allocation per SoundPool, causing errors like this:



This happens even on small samples, as you can see, if you have any larger looped samples in memory. By many estimates on the forums, you can have up to 10 seconds of sound data loaded into memory in total with the current system before this starts to happen. I've attempted to get around this inside Monkey by using the various Discard() functions and hoping the system would free the memory before I had to allocate more, but still, in my current project, sounds continue to drop out.

There seem to be many ways to approach this limitation on the native side -- One of the solutions would involve having a custom sound system, but an easier solution would be to hack mojo to allocate multiple SoundPools behind the scenes, hardcoding certain channels to certain SoundPools. This would be transparent to the monkey coder, but would increase the total heap size necessary to bootstrap a monkey game.

Instead what I'm requesting is something similar to the above solution, but to be allocated with a preprocessor directive. The directive would specify either how many SoundPools it would allocate, or how much memory, in multiples of 1mb. The free channels could either be divvied up between SoundPools (this would be the easier of the two suggestions to implement), or -- perhaps a better longterm solution -- a pool manager to track allocations and assign them dynamically based on the sample's size to the most empty SoundPool could be written.

I'll update this post to make the case for this (and final remarks) soon, but right now I'm a bit short on time and gotta go. Throwing this up here, for now!


Edit: Okay, so, I have a bit more time here to explain what's going on in my project. We have both sound and music in the game, and some samples are short looping types which (for speed purposes) need to be in memory whenever the screen which plays them is active. As ambient environmental effects, they're in somewhat of a no-man's land between whether they're best suited for MediaPlayer or SoundPool. However, Monkey only allocates one of each of these, regardless, so I couldn't use the looping samples like "Music" regardless.

There appears to be a delay between when my code tells the sounds to be released (between screens) and when they're actually released, causing the re-allocations of sounds on the next screen to fail with the above types of errors. As I said before, the real limit of SoundPool is 1mb for all allocated sounds, but this wouldn't be so much of a problem if sounds were unloaded when -I- wanted them to be, not when mojo/android/whatever is ready to make that sweep. If it's not within mojo's control to flush immediately, then a workaround would probably have to be provided.

Since we can't force the OS to increase the allocation size of a pool, I figured having multiple ones would be the first place to look... SoundPool may or may not have exclusive access when preparing to play a sample and can't mix multiple allocations, but I'm not sure of this.

Anyone else have similar experiences? Know about SoundPool usage? Etc? Please leave comments below :)