1 /* 2 Copyright (C) 2010 Grame 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation; either version 2 of the License, or 7 (at your option) any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with this program; if not, write to the Free Software 16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 18 */ 19 20 #ifndef __TiPhoneCoreAudioRenderer__ 21 #define __TiPhoneCoreAudioRenderer__ 22 23 #include <AudioToolbox/AudioConverter.h> 24 #include <AudioToolbox/AudioServices.h> 25 #include <AudioUnit/AudioUnit.h> 26 27 #define MAX_CHANNELS 256 28 #define OPEN_ERR -1 29 #define NO_ERR 0 30 31 typedef void (*AudioCallback) (int frames, float** inputs, float** outputs, void* arg); 32 33 class TiPhoneCoreAudioRenderer 34 { 35 36 private: 37 38 AudioUnit fAUHAL; 39 AudioCallback fAudioCallback; 40 void* fCallbackArg; 41 42 int fDevNumInChans; 43 int fDevNumOutChans; 44 45 AudioBufferList* fCAInputData; 46 47 float* fInChannel[MAX_CHANNELS]; 48 float* fOutChannel[MAX_CHANNELS]; 49 50 static OSStatus Render(void *inRefCon, 51 AudioUnitRenderActionFlags *ioActionFlags, 52 const AudioTimeStamp *inTimeStamp, 53 UInt32 inBusNumber, 54 UInt32 inNumberFrames, 55 AudioBufferList *ioData); 56 57 static void InterruptionListener(void *inClientData, UInt32 inInterruption); 58 59 public: 60 TiPhoneCoreAudioRenderer(int input,int output)61 TiPhoneCoreAudioRenderer(int input, int output) 62 :fAudioCallback(NULL), fCallbackArg(NULL), fDevNumInChans(input), fDevNumOutChans(output), fCAInputData(NULL) 63 { 64 memset(fInChannel, 0, sizeof(float*) * MAX_CHANNELS); 65 memset(fOutChannel, 0, sizeof(float*) * MAX_CHANNELS); 66 67 for (int i = 0; i < fDevNumInChans; i++) { 68 fInChannel[i] = new float[8192]; 69 } 70 71 for (int i = 0; i < fDevNumOutChans; i++) { 72 fOutChannel[i] = new float[8192]; 73 } 74 } 75 ~TiPhoneCoreAudioRenderer()76 virtual ~TiPhoneCoreAudioRenderer() 77 { 78 for (int i = 0; i < fDevNumInChans; i++) { 79 delete[] fInChannel[i]; 80 } 81 82 for (int i = 0; i < fDevNumOutChans; i++) { 83 delete[] fOutChannel[i]; 84 } 85 86 if (fCAInputData) { 87 for (int i = 0; i < fDevNumInChans; i++) { 88 free(fCAInputData->mBuffers[i].mData); 89 } 90 free(fCAInputData); 91 } 92 } 93 94 int Open(int bufferSize, int sampleRate); 95 int Close(); 96 97 int Start(); 98 int Stop(); 99 SetAudioCallback(AudioCallback callback,void * arg)100 void SetAudioCallback(AudioCallback callback, void* arg) 101 { 102 fAudioCallback = callback; 103 fCallbackArg = arg; 104 } 105 PerformAudioCallback(int frames)106 void PerformAudioCallback(int frames) 107 { 108 if (fAudioCallback) { 109 fAudioCallback(frames, fInChannel, fOutChannel, fCallbackArg); 110 } 111 } 112 113 }; 114 115 typedef TiPhoneCoreAudioRenderer * TiPhoneCoreAudioRendererPtr; 116 117 #endif 118