Plug-And-Play OpenAL / AVFoundation Sound Engine For iPhone Apps

I recently found a great sound engine for iPhone applications that is pretty much plug-and-play.

openal Plug And Play OpenAL / AVFoundation Sound Engine For iPhone Apps

OpenAL lets you play multiple sounds at once

This sound engine was written by Alex Restrepo under a creative commons attribution-share alike licence, so you are free to use it so long as you attribute the code to the author and if you change it and share it you must do so under the same license. I have corrected one small mistake (a semi-colon where it should not have been) and also edited the formatting of the code to my personal preference.

You can get an Xcode example project that uses the sound engine here. If you just want the sound engine itself you can get it here.

If you look at the example project you will see how easy this sound engine is to use. It provides simple methods for playing and managing background music and for playing sound effects on top. These classes use AVFoundation to play the background music and OpenAL to play other sounds on top. I am not informed enough about either of these to know what advantages or disadvantages this may offer but will look into it and possibly do a pure OpenAL version.

To add the sound engine to your own project just add the class files included in SoundEngine.zip to your project. Also be sure to add the OpenAL.framework, AVFoundation.framework and the AudioToolbox.framework otherwise you will get a host of strange errors.

You have the option of using the CMOpenALSoundManager as a singleton. If you do use it as a singleton (you can do this by setting USE_AS_SINGLETON to 1 at the top of CMOpenALSoundManager.h) you just need to initialize it somewhere in your code with:

[[CMOpenALSoundManager sharedCMOpenALSoundManager] init];

You also need to enumerate the tags for your sounds (ideally somewhere that every class that needs them will have access to them):

enum mySoundIds { BUTTONSOUND1 BUTTONSOUND2 SUCCESSSOUND ERRORSOUND };

You can then add your sound effects by passing an array of sound file names. Assuming you have an array named mySoundFileNamesArray you would do this:

[CMOpenALSoundManager sharedCMOpenALSoundManager].soundFileNames = mySoundFileNamesArray;

To access your sound manager from anywhere in your project make sure you have included the CMOpenSoundManager header file and then reference the sound manager with:

[CMOpenALSoundManager sharedCMOpenALSoundManager]

Here is an example of playing some background music and then playing a sound effect:

[[CMOpenALSoundManager sharedCMOpenALSoundManager] playBackgroundMusic:@"backgroundLoop.m4a"]; [[CMOpenALSoundManager sharedCMOpenALSoundManager] playSoundWithID:BUTTONSOUND1];

One thing that I am not satisfied about with this system is the need for the enumeration. The reason being that you need to know which order the sounds are going to be loaded so that the correct sounds match the correct enum value. A better way in my opinion would be to use NSString keys, so that when you load a sound, you also pass a key. This is the approach taken in the code on the gethacktes.net blog, which is another great resource for OpenAL on the iPhone. Doing it this way allows you to lazy load sounds in any order. If I make that change I will be sure to post it here.

Share and Enjoy:
  • printfriendly Plug And Play OpenAL / AVFoundation Sound Engine For iPhone Apps
  • digg Plug And Play OpenAL / AVFoundation Sound Engine For iPhone Apps
  • stumbleupon Plug And Play OpenAL / AVFoundation Sound Engine For iPhone Apps
  • delicious Plug And Play OpenAL / AVFoundation Sound Engine For iPhone Apps
  • facebook Plug And Play OpenAL / AVFoundation Sound Engine For iPhone Apps
  • yahoobuzz Plug And Play OpenAL / AVFoundation Sound Engine For iPhone Apps
  • twitter Plug And Play OpenAL / AVFoundation Sound Engine For iPhone Apps
  • googlebookmark Plug And Play OpenAL / AVFoundation Sound Engine For iPhone Apps
  • reddit Plug And Play OpenAL / AVFoundation Sound Engine For iPhone Apps

About jjob

I'm a technologist and a music producer. I make sound, write code and build things with electronics and microcontrollers.
This entry was posted in code, iPhone, Objective-C, sound. Bookmark the permalink.

One Response to Plug-And-Play OpenAL / AVFoundation Sound Engine For iPhone Apps

  1. Old McStopher says:

    I’ve tried this engine and it seems really slick. The only issue I’ve noticed with it is that its endInterruption method doesn’t get called when an audio interruption actually occurs. It seems to be called upon initialization of the CMOpenALSoundManager, but not when an alarm goes off or the phone rings.

    One could try putting an interrupt listener somewhere else in their code and then kill/recreate the entire sound manager, but that seems a bit unnecessary.

    Any clues on this?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>