Imagination PowerVR SDK Blog

Using OpenAL on iOS in a PVRShell application


#1

Hi,

I know that there are good reasons not to include audio support in the PowerVR SDK. However, I spent a few hours this week to include basic audio support to a simple demo game. Here are a few tips (which would have saved me some hours :wink: but you still have to read about the OpenAL API to know how to actually use OpenAL:

- A good, free documentation of the OpenAL API is available one www.openal.org, in particular here: http://connect.creativelabs.com/openal/Documentation/OpenAL_Programmers_Guide.pdf

- A more detailed description is given in “Beginning iPhone Games Development” http://apress.com/book/view/9781430225997

- Good OpenAL/iOS examples are “oalTouch” and “MusicCube”; in particular “oalTouch” comes with two files “MyOpenALSupport.c” and “MyOpenALSupport.h”, which are very useful to load sound files. I had to use
   extern “C” {
   #include “MyOpenALSupport.h”
   }
to include it in the cpp file. (I’m mentioning this because I never know when it is necessary; thus, I tend to forget it.)

- It is straightforward to open an OpenAL device and context in a PVRShell application (see the OpenAL documentation); however, it is more difficult to react well to interrupts of the application, e.g. by phone calls or alarms (see the discussion in the “Beginning iPhone Games Development”). Since I’m only programming demos, I tend to ignore these problems

- To load sound data I came up with this code (probably there are better alternatives):

   #include <CoreFoundation/CFString.h>
   …
   {
        ALenum  error = AL_NO_ERROR;
        ALenum  format;
        ALsizei size;
        ALsizei freq;
       
        CPVRTString filename = CPVRTResourceFile::GetReadPath() + “Pendulum.wav”;
        CFStringRef cffilename = CFStringCreateWithCString(kCFAllocatorDefault,
             filename.c_str(), kCFStringEncodingMacRoman);
        CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cffilename,
             kCFURLPOSIXPathStyle, false);
        CFRelease(cffilename);
        if (fileURL)
        {   
            m_BackgroundAudioData = MyGetOpenALAudioData(fileURL, &size, &format, &freq);
            CFRelease(fileURL);
           
            if((error = alGetError()) != AL_NO_ERROR) {
                PVRShellSet(prefExitMessage, “ERROR: Cannot load sound.”);
                return false;
            }
           
            // use the static buffer data API
            alBufferDataStaticProc(m_uiBackgroundAudioBuffer, format,
               m_BackgroundAudioData, size, freq);           
           
            if((error = alGetError()) != AL_NO_ERROR) {
                PVRShellSet(prefExitMessage, “ERROR: Cannot attach audio data.”);
                return false;
            }       
        }
        else {
            PVRShellSet(prefExitMessage, “ERROR: Cannot find file.”);
            return false;
        }
    }

m_uiBackgroundAudioBuffer has been generated with OpenAL before this code and the buffer is released when quitting the application; m_BackgroundAudioData is of type “void *” and is also released when quitting the application (with “free(m_BackgroundAudioData)”)

- one good source of sound clips is GarageBand; and I’m using Audacity to convert to mono (which is necessary for the 3d effects of OpenAL).

Hope this helps,

Martin