Search code examples
iphonememorycore-audiomixer

How to limit memory consumption when using audio unit


For my app, I need to play music on background when user navigate inside it. So, starting from MixerHost, I developed an audio mixer which is able to play 8 tracks simultaneously. Nevertheless, It consumes to much memory because the 8 tracks files are entirely loaded in 8 buffers.

To limit the memory consumption, I load only a small chunk of data at the beginning, and I feed with new data in the callback like that

result = ExtAudioFileRead ( audioFileObject, &numberOfPacketsToRead, bufferList );

It works quite well, but sometimes the playback is shortly paused. I know the origin of the problem: making FS access in the callback.

But is there another solution to limit memory consumption ?


Solution

  • The way this is typically handled is with a shared ring buffer. The ring buffer acts like a shock absorber between the real-time render thread and the slow disk accesses. Create a new thread that does nothing but read audio from the file and stores it in the ring buffer. Then, in your render callback just read from the ring buffer.

    Apple has provided an implementation of a ring buffer suitable for use with Audio Units, called CARingBuffer. It's available in /Developer/Extras/CoreAudio/PublicUtility/CARingBuffer.