From fdafe925e9ee07cdef4f27a472b4afb908485bb6 Mon Sep 17 00:00:00 2001 From: Oystein Eftevaag Date: Tue, 13 Nov 2007 21:21:17 +0000 Subject: Added sound support for the iPhone svn-id: r29499 --- backends/platform/iphone/osys_iphone.cpp | 55 ++++++++++++++++++++++++++++++-- backends/platform/iphone/osys_iphone.h | 36 ++++++++++++--------- 2 files changed, 74 insertions(+), 17 deletions(-) (limited to 'backends/platform/iphone') diff --git a/backends/platform/iphone/osys_iphone.cpp b/backends/platform/iphone/osys_iphone.cpp index 7cf4ecffb0..34eaaefb14 100644 --- a/backends/platform/iphone/osys_iphone.cpp +++ b/backends/platform/iphone/osys_iphone.cpp @@ -50,6 +50,10 @@ const OSystem::GraphicsMode OSystem_IPHONE::s_supportedGraphicsModes[] = { {0, 0, 0} }; +AQCallbackStruct OSystem_IPHONE::s_AudioQueue; +SoundProc OSystem_IPHONE::s_soundCallback = NULL; +void *OSystem_IPHONE::s_soundParam = NULL; + OSystem_IPHONE::OSystem_IPHONE() : _savefile(NULL), _mixer(NULL), _timer(NULL), _offscreen(NULL), _overlayVisible(false), _overlayBuffer(NULL), _fullscreen(NULL), @@ -572,7 +576,54 @@ void OSystem_IPHONE::deleteMutex(MutexRef mutex) { free(mutex); } +void OSystem_IPHONE::AQBufferCallback(void *in, AudioQueueRef inQ, AudioQueueBufferRef outQB) { + //printf("AQBufferCallback()\n"); + if (s_AudioQueue.frameCount > 0 && s_soundCallback != NULL) { + outQB->mAudioDataByteSize = 4 * s_AudioQueue.frameCount; + s_soundCallback(s_soundParam, (byte *)outQB->mAudioData, outQB->mAudioDataByteSize); + AudioQueueEnqueueBuffer(inQ, outQB, 0, NULL); + } else { + AudioQueueStop(s_AudioQueue.queue, false); + } +} + bool OSystem_IPHONE::setSoundCallback(SoundProc proc, void *param) { + //printf("setSoundCallback()\n"); + s_soundCallback = proc; + s_soundParam = param; + + s_AudioQueue.dataFormat.mSampleRate = AUDIO_SAMPLE_RATE; + s_AudioQueue.dataFormat.mFormatID = kAudioFormatLinearPCM; + s_AudioQueue.dataFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked; + s_AudioQueue.dataFormat.mBytesPerPacket = 4; + s_AudioQueue.dataFormat.mFramesPerPacket = 1; + s_AudioQueue.dataFormat.mBytesPerFrame = 4; + s_AudioQueue.dataFormat.mChannelsPerFrame = 2; + s_AudioQueue.dataFormat.mBitsPerChannel = 16; + s_AudioQueue.frameCount = WAVE_BUFFER_SIZE; + + if (AudioQueueNewOutput(&s_AudioQueue.dataFormat, AQBufferCallback, &s_AudioQueue, 0, kCFRunLoopCommonModes, 0, &s_AudioQueue.queue)) { + printf("Couldn't set the AudioQueue callback!\n"); + return false; + } + + uint32 bufferBytes = s_AudioQueue.frameCount * s_AudioQueue.dataFormat.mBytesPerFrame; + + for (int i = 0; i < AUDIO_BUFFERS; i++) { + if (AudioQueueAllocateBuffer(s_AudioQueue.queue, bufferBytes, &s_AudioQueue.buffers[i])) { + printf("Error allocating AudioQueue buffer!\n"); + return false; + } + + AQBufferCallback(&s_AudioQueue, s_AudioQueue.queue, s_AudioQueue.buffers[i]); + } + + AudioQueueSetParameter(s_AudioQueue.queue, kAudioQueueParam_Volume, 1.0); + if (AudioQueueStart(s_AudioQueue.queue, NULL)) { + printf("Error starting the AudioQueue!\n"); + return false; + } + return true; } @@ -580,11 +631,11 @@ void OSystem_IPHONE::clearSoundCallback() { } int OSystem_IPHONE::getOutputSampleRate() const { - return 22050; + return AUDIO_SAMPLE_RATE; } void OSystem_IPHONE::quit() { - //exit(0); + AudioQueueDispose(s_AudioQueue.queue, true); } void OSystem_IPHONE::setWindowCaption(const char *caption) { diff --git a/backends/platform/iphone/osys_iphone.h b/backends/platform/iphone/osys_iphone.h index 43d7897b6c..7e0a046604 100644 --- a/backends/platform/iphone/osys_iphone.h +++ b/backends/platform/iphone/osys_iphone.h @@ -27,24 +27,28 @@ #include "graphics/surface.h" -// #include -// -// /* Audio Resources */ -// #define AUDIO_BUFFERS 3 -// #define AUDIO_PRECACHE 4 -// #define WAVE_BUFFER_SIZE 735 -// #define WAVE_BUFFER_BANKS 25 -// -// typedef struct AQCallbackStruct { -// AudioQueueRef queue; -// UInt32 frameCount; -// AudioQueueBufferRef mBuffers[AUDIO_BUFFERS]; -// AudioStreamBasicDescription mDataFormat; -// } AQCallbackStruct; +#include + +#define AUDIO_BUFFERS 3 +#define WAVE_BUFFER_SIZE 735 +#define AUDIO_SAMPLE_RATE 44100 + +typedef void (*SoundProc)(void *param, byte *buf, int len); + +typedef struct AQCallbackStruct { + AudioQueueRef queue; + uint32 frameCount; + AudioQueueBufferRef buffers[AUDIO_BUFFERS]; + AudioStreamBasicDescription dataFormat; +} AQCallbackStruct; class OSystem_IPHONE : public OSystem { protected: + static const OSystem::GraphicsMode s_supportedGraphicsModes[]; + static AQCallbackStruct s_AudioQueue; + static SoundProc s_soundCallback; + static void *s_soundParam; Common::SaveFileManager *_savefile; Audio::Mixer *_mixer; @@ -127,7 +131,6 @@ public: virtual void unlockMutex(MutexRef mutex); virtual void deleteMutex(MutexRef mutex); - typedef void (*SoundProc)(void *param, byte *buf, int len); virtual bool setSoundCallback(SoundProc proc, void *param); virtual void clearSoundCallback(); virtual int getOutputSampleRate() const; @@ -139,6 +142,9 @@ public: virtual Common::SaveFileManager *getSavefileManager(); virtual Audio::Mixer *getMixer(); virtual Common::TimerManager *getTimerManager(); + +protected: + static void AQBufferCallback(void *in, AudioQueueRef inQ, AudioQueueBufferRef outQB); }; #endif -- cgit v1.2.3