Jack2  1.9.10
TiPhoneCoreAudioRenderer.h
00001 /*
00002 Copyright (C) 2010 Grame
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018 */
00019 
00020 #ifndef __TiPhoneCoreAudioRenderer__
00021 #define __TiPhoneCoreAudioRenderer__
00022 
00023 #include <AudioToolbox/AudioConverter.h>
00024 #include <AudioToolbox/AudioServices.h>
00025 #include <AudioUnit/AudioUnit.h>
00026 
00027 #define MAX_CHANNELS 256
00028 #define OPEN_ERR -1
00029 #define NO_ERR 0
00030 
00031 typedef void (*AudioCallback) (int frames, float** inputs, float** outputs, void* arg);
00032 
00033 class TiPhoneCoreAudioRenderer
00034 {
00035 
00036     private:
00037 
00038                 AudioUnit fAUHAL;
00039         AudioCallback fAudioCallback;
00040         void* fCallbackArg;
00041         
00042         int     fDevNumInChans;
00043         int     fDevNumOutChans;
00044         
00045         AudioBufferList* fCAInputData;
00046      
00047         float* fInChannel[MAX_CHANNELS];
00048         float* fOutChannel[MAX_CHANNELS];
00049         
00050                 static OSStatus Render(void *inRefCon,
00051                                AudioUnitRenderActionFlags *ioActionFlags,
00052                                const AudioTimeStamp *inTimeStamp,
00053                                UInt32 inBusNumber,
00054                                UInt32 inNumberFrames,
00055                                AudioBufferList *ioData);
00056                                
00057         static void InterruptionListener(void *inClientData, UInt32 inInterruption);
00058 
00059     public:
00060 
00061         TiPhoneCoreAudioRenderer(int input, int output)
00062             :fAudioCallback(NULL), fCallbackArg(NULL), fDevNumInChans(input), fDevNumOutChans(output), fCAInputData(NULL)
00063         {
00064             memset(fInChannel, 0, sizeof(float*) * MAX_CHANNELS);
00065             memset(fOutChannel, 0, sizeof(float*) * MAX_CHANNELS);
00066             
00067             for (int i = 0; i < fDevNumInChans; i++) {
00068                 fInChannel[i] = new float[8192];
00069             }
00070     
00071             for (int i = 0; i < fDevNumOutChans; i++) {
00072                 fOutChannel[i] = new float[8192];
00073             }
00074         }
00075         
00076         virtual ~TiPhoneCoreAudioRenderer()
00077         {
00078             for (int i = 0; i < fDevNumInChans; i++) {
00079                 delete[] fInChannel[i];
00080             }
00081     
00082             for (int i = 0; i < fDevNumOutChans; i++) {
00083                 delete[] fOutChannel[i]; 
00084             }
00085             
00086             if (fCAInputData) {
00087                 for (int i = 0; i < fDevNumInChans; i++) {
00088                     free(fCAInputData->mBuffers[i].mData);
00089                 }
00090                 free(fCAInputData);
00091             }
00092         }
00093 
00094         int Open(int bufferSize, int sampleRate);
00095         int Close();
00096 
00097         int Start();
00098         int Stop();
00099         
00100         void SetAudioCallback(AudioCallback callback, void* arg)
00101         {
00102             fAudioCallback = callback;
00103             fCallbackArg = arg;
00104         }
00105         
00106         void PerformAudioCallback(int frames)
00107         {
00108             if (fAudioCallback) {
00109                 fAudioCallback(frames, fInChannel, fOutChannel, fCallbackArg);
00110             }
00111         }
00112 
00113 };
00114 
00115 typedef TiPhoneCoreAudioRenderer * TiPhoneCoreAudioRendererPtr;
00116 
00117 #endif