/*
File: SoundEngine.cpp
Abstract: These functions play background music tracks, multiple sound effects,
and support stereo panning with a low-latency response.
Version: 1.7
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
("Apple") in consideration of your agreement to the following terms, and your
use, installation, modification or redistribution of this Apple software
constitutes acceptance of these terms. If you do not agree with these terms,
please do not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under
Apple's copyrights in this original Apple software (the "Apple Software"), to
use, reproduce, modify and redistribute the Apple Software, with or without
modifications, in source and/or binary forms; provided that if you redistribute
the Apple Software in its entirety and without modifications, you must retain
this notice and the following text and disclaimers in all such redistributions
of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may be used
to endorse or promote products derived from the Apple Software without specific
prior written permission from Apple. Except as expressly stated in this notice,
no other rights or licenses, express or implied, are granted by Apple herein,
including but not limited to any patent rights that may be infringed by your
derivative works or by other works in which the Apple Software may be
incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2008 Apple Inc. All Rights Reserved.
*/
#if !TARGET_IPHONE_SIMULATOR
/*==================================================================================================
SoundEngine.cpp
==================================================================================================*/
#if !defined(__SoundEngine_cpp__)
#define __SoundEngine_cpp__
//==================================================================================================
// Includes
//==================================================================================================
// System Includes
#include <AudioToolbox/AudioToolbox.h>
#include <CoreFoundation/CFURL.h>
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#include <map>
#include <vector>
#include <pthread.h>
#include <mach/mach.h>
// Local Includes
#include "SoundEngine.h"
#define AssertNoError(inMessage, inHandler) \
if(result != noErr) \
{ \
printf("%s: %d\n", inMessage, (int)result); \
}
#define AssertNoOALError(inMessage, inHandler) \
if((result = alGetError()) != AL_NO_ERROR) \
{ \
printf("%s: %x\n", inMessage, (int)result); \
goto inHandler; \
}
#define kNumberBuffers 3
class OpenALObject;
class BackgroundTrackMgr;
static OpenALObject *sOpenALObject = NULL;
static BackgroundTrackMgr *sBackgroundTrackMgr = NULL;
static Float32 gMasterVolumeGain = 1.0;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
typedef ALvoid AL_APIENTRY (*alBufferDataStaticProcPtr) (const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq);
ALvoid alBufferDataStaticProc(const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq)
{
static alBufferDataStaticProcPtr proc = NULL;
if (proc == NULL) {
proc = (alBufferDataStaticProcPtr) alcGetProcAddress(NULL, (const ALCchar*) "alBufferDataStatic");
}
if (proc)
proc(bid, format, data, size, freq);
return;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
typedef ALvoid AL_APIENTRY (*alcMacOSXMixerOutputRateProcPtr) (const ALdouble value);
ALvoid alcMacOSXMixerOutputRateProc(const ALdouble value)
{
static alcMacOSXMixerOutputRateProcPtr proc = NULL;
if (proc == NULL) {
proc = (alcMacOSXMixerOutputRateProcPtr) alcGetProcAddress(NULL, (const ALCchar*) "alcMacOSXMixerOutputRate");
}
if (proc)
proc(value);
return;
}
#pragma mark ***** OpenALThread *****
//==================================================================================================
// Threading functions
//==================================================================================================
class OpenALThread
{
// returns the thread's priority as it was last set by the API
#define OpenALThread_SET_PRIORITY 0
// returns the thread's priority as it was last scheduled by the Kernel
#define OpenALThread_SCHEDULED_PRIORITY 1
// Types
public:
typedef void* (*ThreadRoutine)(void* inParameter);
// Constants
public:
enum
{
kMinThreadPriority = 1,
kMaxThreadPriority = 63,
kDefaultThreadPriority = 31
};
// Construction/Destruction
public:
OpenALThread(ThreadRoutine inThreadRoutine, void* inParameter)
: mPThread(0),
mSpawningThreadPriority(getScheduledPriority(pthread_self(), OpenALThread_SET_PRIORITY)),
mThreadRoutine(inThreadRoutine),
mThreadParameter(inParameter),
mPriority(kDefaultThreadPriority),
mFixedPriority(false),
mAutoDelete(true) { }
~OpenALThread() { }
// Properties
bool IsRunning() const { return 0 != mPThread; }
void SetAutoDelete(bool b) { mAutoDelete = b; }
void SetPriority(UInt32 inPriority, bool inFixedPriority)
{
OSStatus result = noErr;
mPriority = inPriority;
mFixedPriority = inFixedPriority;
if(mPThread != 0)
{
if (mFixedPriority)
{
thread_extended_policy_data_t theFixedPolicy;
theFixedPolicy.timeshare = false; // set to true for a non-fixed thread
result = thread_policy_set(pthread_mach_thread_np(mPThread), THREAD_EXTENDED_POLICY, (thread_policy_t)&theFixedPolicy, THREAD_EXTENDED_POLICY_COUNT);
if (result) {
printf("OpenALThread::SetPriority: failed to set the fixed-priority policy");
return;
}
}
// We keep a reference to the spawning thread's priority around (initialized in the constructor),
// and set the importance of the child thread relative to the spawning thread's priority.
thread_precedence_policy_data_t thePrecedencePolicy;
thePrecedencePolicy.importance = mPriority - mSpawningThreadPriority;
result =thread_policy_set(pthread_mach_thread_np(mPThread), THREAD_PRECEDENCE_POLICY, (thread_policy_t)&thePrecedencePolicy, THREAD_PRECEDENCE_POLICY_COUNT);
if (result) {
printf("OpenALThread::SetPriority: failed to set the precedence policy");
return;
}
}
}
// Actions
void Start()
{
if(mPThread != 0)
{
printf("OpenALThread::Start: can't start because the thread is already running\n");
return;
}
OSStatus result;
pthread_attr_t theThreadAttributes;
result = pthread_attr_init(&theThreadAttributes);
AssertNoError("Error initializing thread", end);
result = pthread_attr_setdetachstate(&theThreadAttributes, PTHREAD_CREATE_DETACHED);
AssertNoError("Error setting thread detach state", end);
result = pthread_create(&mPThread, &theThreadAttributes, (ThreadRoutine)OpenALThread::Entry