Sound Engine 2.0.3 BETA

The Stormy Productions remix

Looking for a quick and admittedly very dirty solution for playing sound effects in an iPhone app? Then try this free source code. It uses OpenAL for the sound playing and supports mixing of 32 simultaneous sound effects, along with playing one background music track.

PLEASE NOTE: This code was based on Apple's 1.7 SoundEngine code available from the Crash Landing demo supplied with the iPhone SDK 2.0.

Apple has acknowledged significant problems with the original code and has in fact removed the code from all future SDKs. Per the original source code agreement, Apple grants the right to anyone to modify and reuse the software in any manner (see Apple's original licensing agreement in the source files). As such, I have changed the structure of the code to better suit my needs and I am making it available to anyone who might find it useful.

Why use SoundEngine as a base? Isn't oalTouch a better example? Yes, oalTouch is a much cleaner example, but it makes some API calls that require SDK 2.1. SoundEngine was made for SDK 2.0. I have an app that has already been released on iTunes using the 2.0 OS and needed to add some sound support to it. So, I had to use oalTouch's ugly third cousin as a starting point.

THIS IS NOT A DROP-IN REPLACEMENT FOR THE 1.7 SOUND ENGINE!!!

Specifically, I removed the looping features (OpenAL already provides support for this) and I removed the tying of a sound effect buffer to one OpenAL source. Instead, you load a sound effect, which merely creates the static buffer. To play the sound effect, you then "prime" it, which assigns it to an OpenAL source. If you want to play multiple instances of the same sound, you just prime the sound multiple times. Each time you prime a sound effect, you are returned an OpenAL source ID. There are a maximum of 32 sources and the code assumes a source is available for use if you haven't yet primed it in the past or if it is currently in the AL_STOPPED state. (If you don't know what that means, feel free to ignore it - the code will handle it for you.) I tried to hide the OpenAL details for those who don't want them, while at the same time exposing the source ID for those who want direct control of the OpenAL features for a source.

By making the sounds distinct from the sources, this now allows an easy method for playing multiple instances of the same sound effect without using any extra buffers. If you tried to play the same sound with the original 1.7 SoundEngine code, it would simply stop the sound and restart it. Not ideal.

The ideal usage of this library is to first load all your sound effects once. Then, when you want to play a sound effect, prime it, and start it. Once you start a sound, you can pretty much ignore it. OpenAL will handle stopping it for you and as long as you don't exceed the 32 simultaneous sound limit, you should be ok.

This is my first pass at modifying this library. I assume it still has bugs, however I thought it might be useful for others looking for a quick and (very) dirty solution for playing multiple instances of the same sound.

NO WARRANTEES ARE EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.

Download the source code here.

Again, I must stress this is a very rough pass at providing a simple sound interface for the iPhone. I plan on revising it as time permits. Feel free to send along any suggestions or criticisms. You can contact me via the "Contact" link at the top of this page.

Barebones Usage Example

#import <SoundEngine.h>

UInt32					bangSound;

...

// Setup sound engine. Run  it at 44Khz to match the sound files
SoundEngine_Initialize(44100);
				
// Assume the listener is in the center.
SoundEngine_SetListenerPosition(0.0, 0.0, 1.0);

SoundEngine_LoadEffect([[bundle pathForResource:@"bang" ofType:@"caf"] UTF8String], &bangSound);

...
ALuint sourceID1, sourceID2;
SoundEngine_PrimeEffect( bangSound, &sourceID1);
SoundEngine_StartEffect(sourceID1);
...
// Play a second instance of the same bang sound
SoundEngine_PrimeEffect( bangSound, &sourceID2);
SoundEngine_StartEffect(sourceID2);
...

// Call at program exit
SoundEngine_Teardown();		

				
				

Revision History

2.0.3 Memory leak fixes courtesy of HeeHun Kang.

2.0.2 Fixed bug with reused source not assigned new buffer.

2.0.1 Initial version