Jack2 1.9.6
|
00001 /* 00002 Copyright (C) 2004-2008 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 __JackCoreAudioDriver__ 00021 #define __JackCoreAudioDriver__ 00022 00023 #include <AudioToolbox/AudioConverter.h> 00024 #include <CoreAudio/CoreAudio.h> 00025 #include <AudioUnit/AudioUnit.h> 00026 #include "JackAudioDriver.h" 00027 #include "JackTime.h" 00028 00029 #include <vector> 00030 00031 using namespace std; 00032 00033 namespace Jack 00034 { 00035 00036 #define kVersion 102 00037 00038 typedef UInt8 CAAudioHardwareDeviceSectionID; 00039 #define kAudioDeviceSectionInput ((CAAudioHardwareDeviceSectionID)0x01) 00040 #define kAudioDeviceSectionOutput ((CAAudioHardwareDeviceSectionID)0x00) 00041 #define kAudioDeviceSectionGlobal ((CAAudioHardwareDeviceSectionID)0x00) 00042 #define kAudioDeviceSectionWildcard ((CAAudioHardwareDeviceSectionID)0xFF) 00043 00044 #define WAIT_COUNTER 60 00045 00052 class JackCoreAudioDriver : public JackAudioDriver 00053 { 00054 00055 private: 00056 00057 AudioUnit fAUHAL; 00058 00059 AudioBufferList* fJackInputData; 00060 AudioBufferList* fDriverOutputData; 00061 00062 AudioDeviceID fDeviceID; // Used "duplex" device 00063 AudioObjectID fPluginID; // Used for aggregate device 00064 00065 AudioUnitRenderActionFlags* fActionFags; 00066 AudioTimeStamp* fCurrentTime; 00067 00068 bool fState; 00069 bool fHogged; 00070 00071 char fCaptureUID[256]; 00072 char fPlaybackUID[256]; 00073 00074 float fIOUsage; 00075 float fComputationGrain; 00076 bool fClockDriftCompensate; 00077 00078 /* 00079 #ifdef MAC_OS_X_VERSION_10_5 00080 AudioDeviceIOProcID fMesureCallbackID; 00081 #endif 00082 */ 00083 00084 static OSStatus Render(void *inRefCon, 00085 AudioUnitRenderActionFlags *ioActionFlags, 00086 const AudioTimeStamp *inTimeStamp, 00087 UInt32 inBusNumber, 00088 UInt32 inNumberFrames, 00089 AudioBufferList *ioData); 00090 00091 static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice, 00092 UInt32 inChannel, 00093 Boolean isInput, 00094 AudioDevicePropertyID inPropertyID, 00095 void* inClientData); 00096 00097 static OSStatus SRNotificationCallback(AudioDeviceID inDevice, 00098 UInt32 inChannel, 00099 Boolean isInput, 00100 AudioDevicePropertyID inPropertyID, 00101 void* inClientData); 00102 00103 OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id); 00104 OSStatus GetDefaultDevice(AudioDeviceID* id); 00105 OSStatus GetDefaultInputDevice(AudioDeviceID* id); 00106 OSStatus GetDefaultOutputDevice(AudioDeviceID* id); 00107 OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name); 00108 OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput); 00109 00110 // Setup 00111 OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice); 00112 OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice); 00113 OSStatus DestroyAggregateDevice(); 00114 bool IsAggregateDevice(AudioDeviceID device); 00115 00116 int SetupDevices(const char* capture_driver_uid, 00117 const char* playback_driver_uid, 00118 char* capture_driver_name, 00119 char* playback_driver_name, 00120 jack_nframes_t samplerate); 00121 00122 int SetupChannels(bool capturing, 00123 bool playing, 00124 int& inchannels, 00125 int& outchannels, 00126 int& in_nChannels, 00127 int& out_nChannels, 00128 bool strict); 00129 00130 int SetupBuffers(int inchannels); 00131 void DisposeBuffers(); 00132 00133 int SetupBufferSize(jack_nframes_t buffer_size); 00134 int SetupSampleRate(jack_nframes_t samplerate); 00135 int SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate); 00136 00137 int OpenAUHAL(bool capturing, 00138 bool playing, 00139 int inchannels, 00140 int outchannels, 00141 int in_nChannels, 00142 int out_nChannels, 00143 jack_nframes_t nframes, 00144 jack_nframes_t samplerate); 00145 void CloseAUHAL(); 00146 00147 int AddListeners(); 00148 void RemoveListeners(); 00149 00150 bool TakeHogAux(AudioDeviceID deviceID, bool isInput); 00151 bool TakeHog(); 00152 00153 public: 00154 00155 JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table); 00156 virtual ~JackCoreAudioDriver(); 00157 00158 int Open(jack_nframes_t buffer_size, 00159 jack_nframes_t samplerate, 00160 bool capturing, 00161 bool playing, 00162 int chan_in, 00163 int chan_out, 00164 bool monitor, 00165 const char* capture_driver_name, 00166 const char* playback_driver_name, 00167 jack_nframes_t capture_latency, 00168 jack_nframes_t playback_latency, 00169 int async_output_latency, 00170 int computation_grain, 00171 bool hogged, 00172 bool clock_drift); 00173 int Close(); 00174 00175 int Attach(); 00176 00177 int Start(); 00178 int Stop(); 00179 00180 int Read(); 00181 int Write(); 00182 00183 // BufferSize can be changed 00184 bool IsFixedBufferSize() 00185 { 00186 return false; 00187 } 00188 00189 int SetBufferSize(jack_nframes_t buffer_size); 00190 }; 00191 00192 } // end of namespace 00193 00194 #endif