JackCoreAudioDriver.cpp

00001 /*
00002 Copyright (C) 2004-2006 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 #include "JackCoreAudioDriver.h"
00021 #include "JackEngineControl.h"
00022 #include "JackMachThread.h"
00023 #include "JackGraphManager.h"
00024 #include "JackError.h"
00025 #include "JackClientControl.h"
00026 #include "JackDriverLoader.h"
00027 #include "JackGlobals.h"
00028 #include "driver_interface.h"
00029 #include <iostream>
00030 
00031 namespace Jack
00032 {
00033 
00034 static void PrintStreamDesc(AudioStreamBasicDescription *inDesc)
00035 {
00036     JackLog("- - - - - - - - - - - - - - - - - - - -\n");
00037     JackLog("  Sample Rate:%f\n", inDesc->mSampleRate);
00038     JackLog("  Format ID:%.*s\n", (int) sizeof(inDesc->mFormatID), (char*)&inDesc->mFormatID);
00039     JackLog("  Format Flags:%lX\n", inDesc->mFormatFlags);
00040     JackLog("  Bytes per Packet:%ld\n", inDesc->mBytesPerPacket);
00041     JackLog("  Frames per Packet:%ld\n", inDesc->mFramesPerPacket);
00042     JackLog("  Bytes per Frame:%ld\n", inDesc->mBytesPerFrame);
00043     JackLog("  Channels per Frame:%ld\n", inDesc->mChannelsPerFrame);
00044     JackLog("  Bits per Channel:%ld\n", inDesc->mBitsPerChannel);
00045     JackLog("- - - - - - - - - - - - - - - - - - - -\n");
00046 }
00047 
00048 static void printError(OSStatus err)
00049 {
00050     switch (err) {
00051         case kAudioHardwareNoError:
00052             JackLog("error code : kAudioHardwareNoError\n");
00053             break;
00054         case kAudioConverterErr_FormatNotSupported:
00055             JackLog("error code : kAudioConverterErr_FormatNotSupported\n");
00056             break;
00057         case kAudioConverterErr_OperationNotSupported:
00058             JackLog("error code : kAudioConverterErr_OperationNotSupported\n");
00059             break;
00060         case kAudioConverterErr_PropertyNotSupported:
00061             JackLog("error code : kAudioConverterErr_PropertyNotSupported\n");
00062             break;
00063         case kAudioConverterErr_InvalidInputSize:
00064             JackLog("error code : kAudioConverterErr_InvalidInputSize\n");
00065             break;
00066         case kAudioConverterErr_InvalidOutputSize:
00067             JackLog("error code : kAudioConverterErr_InvalidOutputSize\n");
00068             break;
00069         case kAudioConverterErr_UnspecifiedError:
00070             JackLog("error code : kAudioConverterErr_UnspecifiedError\n");
00071             break;
00072         case kAudioConverterErr_BadPropertySizeError:
00073             JackLog("error code : kAudioConverterErr_BadPropertySizeError\n");
00074             break;
00075         case kAudioConverterErr_RequiresPacketDescriptionsError:
00076             JackLog("error code : kAudioConverterErr_RequiresPacketDescriptionsError\n");
00077             break;
00078         case kAudioConverterErr_InputSampleRateOutOfRange:
00079             JackLog("error code : kAudioConverterErr_InputSampleRateOutOfRange\n");
00080             break;
00081         case kAudioConverterErr_OutputSampleRateOutOfRange:
00082             JackLog("error code : kAudioConverterErr_OutputSampleRateOutOfRange\n");
00083             break;
00084         case kAudioHardwareNotRunningError:
00085             JackLog("error code : kAudioHardwareNotRunningError\n");
00086             break;
00087         case kAudioHardwareUnknownPropertyError:
00088             JackLog("error code : kAudioHardwareUnknownPropertyError\n");
00089             break;
00090         case kAudioHardwareIllegalOperationError:
00091             JackLog("error code : kAudioHardwareIllegalOperationError\n");
00092             break;
00093         case kAudioHardwareBadDeviceError:
00094             JackLog("error code : kAudioHardwareBadDeviceError\n");
00095             break;
00096         case kAudioHardwareBadStreamError:
00097             JackLog("error code : kAudioHardwareBadStreamError\n");
00098             break;
00099         case kAudioDeviceUnsupportedFormatError:
00100             JackLog("error code : kAudioDeviceUnsupportedFormatError\n");
00101             break;
00102         case kAudioDevicePermissionsError:
00103             JackLog("error code : kAudioDevicePermissionsError\n");
00104             break;
00105         default:
00106             JackLog("error code : unknown\n");
00107             break;
00108     }
00109 }
00110 
00111 static OSStatus DisplayDeviceNames()
00112 {
00113     UInt32 size;
00114     Boolean isWritable;
00115     int i, deviceNum;
00116     OSStatus err;
00117     CFStringRef UIname;
00118 
00119     err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
00120     if (err != noErr)
00121         return err;
00122 
00123     deviceNum = size / sizeof(AudioDeviceID);
00124     AudioDeviceID devices[deviceNum];
00125 
00126     err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
00127     if (err != noErr)
00128         return err;
00129 
00130     for (i = 0; i < deviceNum; i++) {
00131         char device_name[256];
00132         char internal_name[256];
00133 
00134         size = sizeof(CFStringRef);
00135         UIname = NULL;
00136         err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
00137         if (err == noErr) {
00138             CFStringGetCString(UIname, internal_name, 256, CFStringGetSystemEncoding());
00139         } else {
00140             goto error;
00141         }
00142 
00143         size = 256;
00144         err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &size, device_name);
00145         if (err != noErr)
00146             return err;
00147 
00148         printf("Device name = \'%s\', internal_name = \'%s\' (to be used as -C, -P, or -d parameter)\n", device_name, internal_name);
00149     }
00150 
00151     return noErr;
00152 
00153 error:
00154     if (UIname != NULL)
00155         CFRelease(UIname);
00156     return err;
00157 }
00158 
00159 #if IO_CPU
00160 
00161 OSStatus JackCoreAudioDriver::Render(void *inRefCon,
00162                                      AudioUnitRenderActionFlags *ioActionFlags,
00163                                      const AudioTimeStamp *inTimeStamp,
00164                                      UInt32 inBusNumber,
00165                                      UInt32 inNumberFrames,
00166                                      AudioBufferList *ioData)
00167 {
00168     JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inRefCon;
00169     driver->fLastWaitUst = GetMicroSeconds(); // Take callback date here
00170     memcpy(&driver->fActionFags, ioActionFlags, sizeof(AudioUnitRenderActionFlags));
00171     memcpy(&driver->fCurrentTime, inTimeStamp, sizeof(AudioTimeStamp));
00172     driver->fDriverOutputData = ioData;
00173     float delta;
00174     OSStatus res = driver->Process();
00175     if ((delta = float(GetMicroSeconds() - driver->fLastWaitUst) / float(driver->fEngineControl->fPeriodUsecs)) > 0.1) {
00176         JackLog("IO CPU %f\n", delta);
00177     }
00178     return res;
00179 }
00180 
00181 #else
00182 
00183 OSStatus JackCoreAudioDriver::Render(void *inRefCon,
00184                                      AudioUnitRenderActionFlags *ioActionFlags,
00185                                      const AudioTimeStamp *inTimeStamp,
00186                                      UInt32 inBusNumber,
00187                                      UInt32 inNumberFrames,
00188                                      AudioBufferList *ioData)
00189 {
00190     JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inRefCon;
00191     driver->fLastWaitUst = GetMicroSeconds(); // Take callback date here
00192     memcpy(&driver->fActionFags, ioActionFlags, sizeof(AudioUnitRenderActionFlags));
00193     memcpy(&driver->fCurrentTime, inTimeStamp, sizeof(AudioTimeStamp));
00194     driver->fDriverOutputData = ioData;
00195     return driver->Process();
00196 }
00197 
00198 #endif
00199 
00200 int JackCoreAudioDriver::Read()
00201 {
00202     AudioUnitRender(fAUHAL, &fActionFags, &fCurrentTime, 1, fEngineControl->fBufferSize, fJackInputData);
00203     return 0;
00204 }
00205 
00206 int JackCoreAudioDriver::Write()
00207 {
00208     for (int i = 0; i < fPlaybackChannels; i++) {
00209         if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
00210             float* buffer = GetOutputBuffer(i);
00211             int size = sizeof(float) * fEngineControl->fBufferSize;
00212             memcpy((float*)fDriverOutputData->mBuffers[i].mData, buffer, size);
00213             // Monitor ports
00214             if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
00215                 memcpy(GetMonitorBuffer(i), buffer, size);
00216         } else {
00217             memset((float*)fDriverOutputData->mBuffers[i].mData, 0, sizeof(float) * fEngineControl->fBufferSize);
00218         }
00219     }
00220     return 0;
00221 }
00222 
00223 // Will run only once
00224 OSStatus JackCoreAudioDriver::MeasureCallback(AudioDeviceID inDevice,
00225         const AudioTimeStamp* inNow,
00226         const AudioBufferList* inInputData,
00227         const AudioTimeStamp* inInputTime,
00228         AudioBufferList* outOutputData,
00229         const AudioTimeStamp* inOutputTime,
00230         void* inClientData)
00231 {
00232     JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
00233     AudioDeviceStop(driver->fDeviceID, MeasureCallback);
00234     AudioDeviceRemoveIOProc(driver->fDeviceID, MeasureCallback);
00235     JackLog("JackCoreAudioDriver::MeasureCallback called\n");
00236     JackMachThread::GetParams(&driver->fEngineControl->fPeriod, &driver->fEngineControl->fComputation, &driver->fEngineControl->fConstraint);
00237     return noErr;
00238 }
00239 
00240 OSStatus JackCoreAudioDriver::DeviceNotificationCallback(AudioDeviceID inDevice,
00241         UInt32 inChannel,
00242         Boolean isInput,
00243         AudioDevicePropertyID inPropertyID,
00244         void* inClientData)
00245 {
00246     JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
00247     if (inPropertyID == kAudioDeviceProcessorOverload) {
00248         JackLog("JackCoreAudioDriver::NotificationCallback kAudioDeviceProcessorOverload\n");
00249 #ifdef DEBUG
00250         //driver->fLogFile->Capture(AudioGetCurrentHostTime() - AudioConvertNanosToHostTime(LOG_SAMPLE_DURATION * 1000000), AudioGetCurrentHostTime(), true, "Captured Latency Log for I/O Cycle Overload\n");
00251 #endif
00252         driver->NotifyXRun(GetMicroSeconds());
00253     }
00254     return noErr;
00255 }
00256 
00257 OSStatus JackCoreAudioDriver::GetDeviceIDFromUID(const char* UID, AudioDeviceID* id)
00258 {
00259     UInt32 size = sizeof(AudioValueTranslation);
00260     CFStringRef inIUD = CFStringCreateWithCString(NULL, UID, CFStringGetSystemEncoding());
00261     AudioValueTranslation value = { &inIUD, sizeof(CFStringRef), id, sizeof(AudioDeviceID) };
00262 
00263     if (inIUD == NULL) {
00264         return kAudioHardwareUnspecifiedError;
00265     } else {
00266         OSStatus res = AudioHardwareGetProperty(kAudioHardwarePropertyDeviceForUID, &size, &value);
00267         CFRelease(inIUD);
00268         JackLog("get_device_id_from_uid %s %ld \n", UID, *id);
00269         return (*id == kAudioDeviceUnknown) ? kAudioHardwareBadDeviceError : res;
00270     }
00271 }
00272 
00273 OSStatus JackCoreAudioDriver::GetDefaultDevice(AudioDeviceID* id)
00274 {
00275     OSStatus res;
00276     UInt32 theSize = sizeof(UInt32);
00277     AudioDeviceID inDefault;
00278     AudioDeviceID outDefault;
00279 
00280     if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr)
00281         return res;
00282 
00283     if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr)
00284         return res;
00285 
00286     JackLog("GetDefaultDevice: input = %ld output = %ld\n", inDefault, outDefault);
00287 
00288     // Get the device only if default input and ouput are the same
00289     if (inDefault == outDefault) {
00290         *id = inDefault;
00291         return noErr;
00292     } else {
00293         jack_error("Default input and output devices are not the same !!");
00294         return kAudioHardwareBadDeviceError;
00295     }
00296 }
00297 
00298 OSStatus JackCoreAudioDriver::GetDefaultInputDevice(AudioDeviceID* id)
00299 {
00300     OSStatus res;
00301     UInt32 theSize = sizeof(UInt32);
00302     AudioDeviceID inDefault;
00303 
00304     if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr)
00305         return res;
00306 
00307     JackLog("GetDefaultInputDevice: input = %ld \n", inDefault);
00308     *id = inDefault;
00309     return noErr;
00310 }
00311 
00312 OSStatus JackCoreAudioDriver::GetDefaultOutputDevice(AudioDeviceID* id)
00313 {
00314     OSStatus res;
00315     UInt32 theSize = sizeof(UInt32);
00316     AudioDeviceID outDefault;
00317 
00318     if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr)
00319         return res;
00320 
00321     JackLog("GetDefaultOutputDevice: output = %ld\n", outDefault);
00322     *id = outDefault;
00323     return noErr;
00324 }
00325 
00326 OSStatus JackCoreAudioDriver::GetDeviceNameFromID(AudioDeviceID id, char* name)
00327 {
00328     UInt32 size = 256;
00329     return AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceName, &size, name);
00330 }
00331 
00332 OSStatus JackCoreAudioDriver::GetTotalChannels(AudioDeviceID device, long* channelCount, bool isInput)
00333 {
00334     OSStatus    err = noErr;
00335     UInt32      outSize;
00336     Boolean     outWritable;
00337     AudioBufferList*    bufferList = 0;
00338     AudioStreamID*      streamList = 0;
00339     int i, numStream;
00340 
00341     err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreams, &outSize, &outWritable);
00342     if (err == noErr) {
00343         streamList = (AudioStreamID*)malloc(outSize);
00344         numStream = outSize / sizeof(AudioStreamID);
00345         JackLog("GetTotalChannels device stream number = %ld numStream = %ld\n", device, numStream);
00346         err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreams, &outSize, streamList);
00347         if (err == noErr) {
00348             AudioStreamBasicDescription streamDesc;
00349             outSize = sizeof(AudioStreamBasicDescription);
00350             for (i = 0; i < numStream; i++) {
00351                 err = AudioStreamGetProperty(streamList[i], 0, kAudioDevicePropertyStreamFormat, &outSize, &streamDesc);
00352                 JackLog("GetTotalChannels streamDesc mFormatFlags = %ld mChannelsPerFrame = %ld\n", streamDesc.mFormatFlags, streamDesc.mChannelsPerFrame);
00353             }
00354         }
00355     }
00356 
00357     *channelCount = 0;
00358     err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
00359     if (err == noErr) {
00360         bufferList = (AudioBufferList*)malloc(outSize);
00361         err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
00362         if (err == noErr) {
00363             for (i = 0; i < bufferList->mNumberBuffers; i++)
00364                 *channelCount += bufferList->mBuffers[i].mNumberChannels;
00365         }
00366     }
00367 
00368     if (streamList)
00369         free(streamList);
00370     if (bufferList)
00371         free(bufferList);
00372 
00373     return err;
00374 }
00375 
00376 JackCoreAudioDriver::JackCoreAudioDriver(const char* name, JackEngine* engine, JackSynchro** table)
00377         : JackAudioDriver(name, engine, table), fJackInputData(NULL), fDriverOutputData(NULL)
00378 {
00379 #ifdef DEBUG
00380     //fLogFile = new CALatencyLog("jackmp_latency", ".txt");
00381 #endif
00382 }
00383 
00384 JackCoreAudioDriver::~JackCoreAudioDriver()
00385 {
00386 #ifdef DEBUG
00387     //delete fLogFile;
00388 #endif
00389 }
00390 
00391 int JackCoreAudioDriver::Open(jack_nframes_t nframes,
00392                               jack_nframes_t samplerate,
00393                               int capturing,
00394                               int playing,
00395                               int inchannels,
00396                               int outchannels,
00397                               bool monitor,
00398                               const char* capture_driver_uid,
00399                               const char* playback_driver_uid,
00400                               jack_nframes_t capture_latency,
00401                               jack_nframes_t playback_latency)
00402 {
00403     OSStatus err = noErr;
00404     ComponentResult err1;
00405     UInt32 outSize;
00406     UInt32 enableIO;
00407     AudioStreamBasicDescription srcFormat, dstFormat, sampleRate;
00408     long in_nChannels = 0;
00409     long out_nChannels = 0;
00410     char capture_driver_name[256];
00411     char playback_driver_name[256];
00412     float iousage;
00413 
00414     capture_driver_name[0] = 0;
00415     playback_driver_name[0] = 0;
00416 
00417     JackLog("JackCoreAudioDriver::Open nframes = %ld in = %ld out = %ld capture name = %s playback name = %s\n",
00418             nframes, inchannels, outchannels, capture_driver_uid, playback_driver_uid);
00419 
00420     // Duplex
00421     if (capture_driver_uid != NULL && playback_driver_uid != NULL) {
00422         JackLog("JackCoreAudioDriver::Open duplex \n");
00423         if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
00424             if (GetDefaultDevice(&fDeviceID) != noErr) {
00425                 jack_error("Cannot open default device");
00426                 return -1;
00427             }
00428         }
00429         if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
00430             jack_error("Cannot get device name from device ID");
00431             return -1;
00432         }
00433 
00434         // Capture only
00435     } else if (capture_driver_uid != NULL) {
00436         JackLog("JackCoreAudioDriver::Open capture only \n");
00437         if (GetDeviceIDFromUID(capture_driver_uid, &fDeviceID) != noErr) {
00438             if (GetDefaultInputDevice(&fDeviceID) != noErr) {
00439                 jack_error("Cannot open default device");
00440                 return -1;
00441             }
00442         }
00443         if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr) {
00444             jack_error("Cannot get device name from device ID");
00445             return -1;
00446         }
00447 
00448         // Playback only
00449     } else if (playback_driver_uid != NULL) {
00450         JackLog("JackCoreAudioDriver::Open playback only \n");
00451         if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
00452             if (GetDefaultOutputDevice(&fDeviceID) != noErr) {
00453                 jack_error("Cannot open default device");
00454                 return -1;
00455             }
00456         }
00457         if (GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
00458             jack_error("Cannot get device name from device ID");
00459             return -1;
00460         }
00461 
00462         // Use default driver in duplex mode
00463     } else {
00464         JackLog("JackCoreAudioDriver::Open default driver \n");
00465         if (GetDefaultDevice(&fDeviceID) != noErr) {
00466             jack_error("Cannot open default device");
00467             return -1;
00468         }
00469         if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
00470             jack_error("Cannot get device name from device ID");
00471             return -1;
00472         }
00473     }
00474 
00475     // Generic JackAudioDriver Open
00476     if (JackAudioDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency) != 0) {
00477         return -1;
00478     }
00479 
00480     if (capturing) {
00481         err = GetTotalChannels(fDeviceID, &in_nChannels, true);
00482         if (err != noErr) {
00483             jack_error("Cannot get input channel number");
00484             printError(err);
00485             return -1;
00486         }
00487     }
00488 
00489     if (playing) {
00490         err = GetTotalChannels(fDeviceID, &out_nChannels, false);
00491         if (err != noErr) {
00492             jack_error("Cannot get output channel number");
00493             printError(err);
00494             return -1;
00495         }
00496     }
00497 
00498     if (inchannels > in_nChannels) {
00499         jack_error("This device hasn't required input channels inchannels = %ld in_nChannels = %ld", inchannels, in_nChannels);
00500         return -1;
00501     }
00502 
00503     if (outchannels > out_nChannels) {
00504         jack_error("This device hasn't required output channels outchannels = %ld out_nChannels = %ld", outchannels, out_nChannels);
00505         return -1;
00506     }
00507 
00508     if (inchannels == 0) {
00509         JackLog("Setup max in channels = %ld\n", in_nChannels);
00510         inchannels = in_nChannels;
00511     }
00512 
00513     if (outchannels == 0) {
00514         JackLog("Setup max out channels = %ld\n", out_nChannels);
00515         outchannels = out_nChannels;
00516     }
00517 
00518     // Setting buffer size
00519     outSize = sizeof(UInt32);
00520     err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &fEngineControl->fBufferSize);
00521     if (err != noErr) {
00522         jack_error("Cannot set buffer size %ld", nframes);
00523         printError(err);
00524         return -1;
00525     }
00526 
00527     // Set sample rate
00528     if (capturing && inchannels > 0) {
00529         outSize = sizeof(AudioStreamBasicDescription);
00530         err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertyStreamFormat, &outSize, &sampleRate);
00531         if (err != noErr) {
00532             jack_error("Cannot get current sample rate");
00533             printError(err);
00534             return -1;
00535         }
00536 
00537         if (samplerate != (unsigned long)sampleRate.mSampleRate) {
00538             sampleRate.mSampleRate = (Float64)samplerate;
00539             err = AudioDeviceSetProperty(fDeviceID, NULL, 0, true, kAudioDevicePropertyStreamFormat, outSize, &sampleRate);
00540             if (err != noErr) {
00541                 jack_error("Cannot set sample rate = %ld", samplerate);
00542                 printError(err);
00543                 return -1;
00544             }
00545         }
00546     }
00547 
00548     if (playing && outchannels > 0) {
00549         outSize = sizeof(AudioStreamBasicDescription);
00550         err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertyStreamFormat, &outSize, &sampleRate);
00551         if (err != noErr) {
00552             jack_error("Cannot get current sample rate");
00553             printError(err);
00554             return -1;
00555         }
00556 
00557         if (samplerate != (unsigned long)sampleRate.mSampleRate) {
00558             sampleRate.mSampleRate = (Float64)samplerate;
00559             err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyStreamFormat, outSize, &sampleRate);
00560             if (err != noErr) {
00561                 jack_error("Cannot set sample rate = %ld", samplerate);
00562                 printError(err);
00563                 return -1;
00564             }
00565         }
00566     }
00567 
00568     // AUHAL
00569     ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
00570     Component HALOutput = FindNextComponent(NULL, &cd);
00571 
00572     err1 = OpenAComponent(HALOutput, &fAUHAL);
00573     if (err1 != noErr) {
00574         jack_error("Error calling OpenAComponent");
00575         printError(err1);
00576         goto error;
00577     }
00578 
00579     err1 = AudioUnitInitialize(fAUHAL);
00580     if (err1 != noErr) {
00581         jack_error("Cannot initialize AUHAL unit");
00582         printError(err1);
00583         goto error;
00584     }
00585 
00586     // Start I/O
00587     enableIO = 1;
00588     if (capturing && inchannels > 0) {
00589         JackLog("Setup AUHAL input\n");
00590         err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
00591         if (err1 != noErr) {
00592             jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
00593             printError(err1);
00594             goto error;
00595         }
00596     }
00597 
00598     if (playing && outchannels > 0) {
00599         JackLog("Setup AUHAL output\n");
00600         err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
00601         if (err1 != noErr) {
00602             jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
00603             printError(err1);
00604             goto error;
00605         }
00606     }
00607 
00608     // Setup up choosen device, in both input and output cases
00609     err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
00610     if (err1 != noErr) {
00611         jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
00612         printError(err1);
00613         goto error;
00614     }
00615 
00616     // Set buffer size
00617     if (capturing && inchannels > 0) {
00618         err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*) & nframes, sizeof(UInt32));
00619         if (err1 != noErr) {
00620             jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
00621             printError(err1);
00622             goto error;
00623         }
00624     }
00625 
00626     if (playing && outchannels > 0) {
00627         err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*) & nframes, sizeof(UInt32));
00628         if (err1 != noErr) {
00629             jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
00630             printError(err1);
00631             goto error;
00632         }
00633     }
00634 
00635     // Setup channel map
00636     if (capturing && inchannels > 0 && inchannels < in_nChannels) {
00637         SInt32 chanArr[in_nChannels];
00638         for (int i = 0; i < in_nChannels; i++) {
00639             chanArr[i] = -1;
00640         }
00641         for (int i = 0; i < inchannels; i++) {
00642             chanArr[i] = i;
00643         }
00644         AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
00645         if (err1 != noErr) {
00646             jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
00647             printError(err1);
00648         }
00649     }
00650 
00651     if (playing && outchannels > 0 && outchannels < out_nChannels) {
00652         SInt32 chanArr[out_nChannels];
00653         for (int i = 0; i < out_nChannels; i++) {
00654             chanArr[i] = -1;
00655         }
00656         for (int i = 0; i < outchannels; i++) {
00657             chanArr[i] = i;
00658         }
00659         err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
00660         if (err1 != noErr) {
00661             jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
00662             printError(err1);
00663         }
00664     }
00665 
00666     // Setup stream converters
00667     if (capturing && inchannels > 0) {
00668         srcFormat.mSampleRate = samplerate;
00669         srcFormat.mFormatID = kAudioFormatLinearPCM;
00670         srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
00671         srcFormat.mBytesPerPacket = sizeof(float);
00672         srcFormat.mFramesPerPacket = 1;
00673         srcFormat.mBytesPerFrame = sizeof(float);
00674         srcFormat.mChannelsPerFrame = outchannels;
00675         srcFormat.mBitsPerChannel = 32;
00676 
00677         err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, sizeof(AudioStreamBasicDescription));
00678         if (err1 != noErr) {
00679             jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
00680             printError(err1);
00681         }
00682     }
00683 
00684     if (playing && outchannels > 0) {
00685         dstFormat.mSampleRate = samplerate;
00686         dstFormat.mFormatID = kAudioFormatLinearPCM;
00687         dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
00688         dstFormat.mBytesPerPacket = sizeof(float);
00689         dstFormat.mFramesPerPacket = 1;
00690         dstFormat.mBytesPerFrame = sizeof(float);
00691         dstFormat.mChannelsPerFrame = inchannels;
00692         dstFormat.mBitsPerChannel = 32;
00693 
00694         err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, sizeof(AudioStreamBasicDescription));
00695         if (err1 != noErr) {
00696             jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
00697             printError(err1);
00698         }
00699     }
00700 
00701     // Setup callbacks
00702     if (inchannels > 0 && outchannels == 0) {
00703         AURenderCallbackStruct output;
00704         output.inputProc = Render;
00705         output.inputProcRefCon = this;
00706         err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
00707         if (err1 != noErr) {
00708             jack_error("Error calling  AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
00709             printError(err1);
00710             goto error;
00711         }
00712     } else {
00713         AURenderCallbackStruct output;
00714         output.inputProc = Render;
00715         output.inputProcRefCon = this;
00716         err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
00717         if (err1 != noErr) {
00718             jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
00719             printError(err1);
00720             goto error;
00721         }
00722     }
00723 
00724     // Prepare buffers
00725     if (capturing && inchannels > 0) {
00726         fJackInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
00727         if (fJackInputData == 0) {
00728             jack_error("Cannot allocate memory for input buffers");
00729             goto error;
00730         }
00731         fJackInputData->mNumberBuffers = inchannels;
00732         for (int i = 0; i < fCaptureChannels; i++) {
00733             fJackInputData->mBuffers[i].mNumberChannels = 1;
00734             fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
00735         }
00736     }
00737 
00738     // Add listeners
00739     err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback, this);
00740     if (err != noErr) {
00741         jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
00742         printError(err1);
00743         goto error;
00744     }
00745 
00746     err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback, this);
00747     if (err != noErr) {
00748         jack_error("Error calling AudioDeviceAddPropertyListener with kAudioHardwarePropertyDevices");
00749         printError(err1);
00750         goto error;
00751     }
00752 
00753     fDriverOutputData = 0;
00754 
00755 #if IO_CPU
00756 
00757     outSize = sizeof(float);
00758     iousage = 0.4f;
00759     err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyIOCycleUsage, outSize, &iousage);
00760     if (err != noErr) {
00761         jack_error("Error calling AudioDeviceSetProperty kAudioDevicePropertyIOCycleUsage");
00762         printError(err);
00763     }
00764 #endif
00765 
00766     // Core driver may have changed the in/out values
00767     fCaptureChannels = inchannels;
00768     fPlaybackChannels = outchannels;
00769     return 0;
00770 
00771 error:
00772     AudioUnitUninitialize(fAUHAL);
00773     CloseComponent(fAUHAL);
00774     return -1;
00775 }
00776 
00777 int JackCoreAudioDriver::Close()
00778 {
00779         JackLog("JackCoreAudioDriver::Close\n");
00780     JackAudioDriver::Close();
00781     // Possibly (if MeasureCallback has not been called)
00782     AudioDeviceStop(fDeviceID, MeasureCallback);
00783     AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
00784     AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback);
00785         AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback);
00786     free(fJackInputData);
00787     AudioUnitUninitialize(fAUHAL);
00788     CloseComponent(fAUHAL);
00789     return 0;
00790 }
00791 
00792 int JackCoreAudioDriver::Attach()
00793 {
00794     OSStatus err;
00795     JackPort* port;
00796     jack_port_id_t port_index;
00797     UInt32 size;
00798     Boolean isWritable;
00799     char channel_name[64];
00800     char buf[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
00801     unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
00802 
00803     JackLog("JackCoreAudioDriver::Attach fBufferSize %ld fSampleRate %ld\n", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
00804 
00805     for (int i = 0; i < fCaptureChannels; i++) {
00806 
00807         err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, &isWritable);
00808         if (err != noErr)
00809             JackLog("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error \n");
00810         if (err == noErr && size > 0) {
00811             err = AudioDeviceGetProperty(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, channel_name);
00812             if (err != noErr)
00813                 JackLog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
00814             snprintf(buf, sizeof(buf) - 1, "%s:%s:out_%s%u", fClientControl->fName, fCaptureDriverName, channel_name, i + 1);
00815         } else {
00816             snprintf(buf, sizeof(buf) - 1, "%s:%s:out%u", fClientControl->fName, fCaptureDriverName, i + 1);
00817         }
00818 
00819         if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, (JackPortFlags)port_flags)) == NO_PORT) {
00820             jack_error("Cannot register port for %s", buf);
00821             return -1;
00822         }
00823                 
00824                 size = sizeof(UInt32);
00825                 UInt32 value1 = 0;
00826                 UInt32 value2 = 0;
00827                 err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertyLatency, &size, &value1);  
00828                 if (err != noErr) 
00829                         JackLog("AudioDeviceGetProperty kAudioDevicePropertyLatency error \n");
00830                 err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertySafetyOffset, &size, &value2);     
00831                 if (err != noErr) 
00832                         JackLog("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error \n");
00833         
00834         port = fGraphManager->GetPort(port_index);
00835                 port->Rename("system:capture_%d", i + 1);
00836         port->SetLatency(fEngineControl->fBufferSize + value1 + value2 + fCaptureLatency);
00837         fCapturePortList[i] = port_index;
00838     }
00839 
00840     port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
00841 
00842     for (int i = 0; i < fPlaybackChannels; i++) {
00843 
00844         err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, &isWritable);
00845         if (err != noErr)
00846             JackLog("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error \n");
00847         if (err == noErr && size > 0) {
00848             err = AudioDeviceGetProperty(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, channel_name);
00849             if (err != noErr)
00850                 JackLog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
00851             snprintf(buf, sizeof(buf) - 1, "%s:%s:in_%s%u", fClientControl->fName, fPlaybackDriverName, channel_name, i + 1);
00852         } else {
00853             snprintf(buf, sizeof(buf) - 1, "%s:%s:in%u", fClientControl->fName, fPlaybackDriverName, i + 1);
00854         }
00855 
00856         if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, (JackPortFlags)port_flags)) == NO_PORT) {
00857             jack_error("Cannot register port for %s", buf);
00858             return -1;
00859         }
00860                 
00861                 size = sizeof(UInt32);
00862                 UInt32 value1 = 0;
00863                 UInt32 value2 = 0;
00864                 err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertyLatency, &size, &value1); 
00865                 if (err != noErr) 
00866                         JackLog("AudioDeviceGetProperty kAudioDevicePropertyLatency error \n");
00867                 err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertySafetyOffset, &size, &value2);    
00868                 if (err != noErr) 
00869                         JackLog("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error \n");
00870 
00871             port = fGraphManager->GetPort(port_index);
00872                 port->Rename("system:playback_%d", i + 1);
00873         port->SetLatency(fEngineControl->fBufferSize + value1 + value2 + fPlaybackLatency);
00874         fPlaybackPortList[i] = port_index;
00875 
00876         // Monitor ports
00877         if (fWithMonitorPorts) {
00878             JackLog("Create monitor port \n");
00879             snprintf(buf, sizeof(buf) - 1, "%s:%s:monitor_%u", fClientControl->fName, fPlaybackDriverName, i + 1);
00880             if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JackPortIsOutput)) == NO_PORT) {
00881                 jack_error("Cannot register monitor port for %s", buf);
00882                 return -1;
00883             } else {
00884                 port = fGraphManager->GetPort(port_index);
00885                 port->SetLatency(fEngineControl->fBufferSize);
00886                 fMonitorPortList[i] = port_index;
00887             }
00888         }
00889     }
00890 
00891     // Input buffers do no change : prepare them only once
00892     for (int i = 0; i < fCaptureChannels; i++) {
00893         fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
00894     }
00895 
00896     return 0;
00897 }
00898 
00899 int JackCoreAudioDriver::Start()
00900 {
00901     JackLog("JackCoreAudioDriver::Start\n");
00902     JackAudioDriver::Start();
00903 
00904     OSStatus err = AudioDeviceAddIOProc(fDeviceID, MeasureCallback, this);
00905     if (err != noErr)
00906         return -1;
00907 
00908     err = AudioOutputUnitStart(fAUHAL);
00909     if (err != noErr)
00910         return -1;
00911 
00912     if ((err = AudioDeviceStart(fDeviceID, MeasureCallback)) != noErr) {
00913         jack_error("Cannot start MeasureCallback");
00914         printError(err);
00915         return -1;
00916     }
00917 
00918     return 0;
00919 }
00920 
00921 int JackCoreAudioDriver::Stop()
00922 {
00923     AudioDeviceStop(fDeviceID, MeasureCallback);
00924     AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
00925     JackLog("JackCoreAudioDriver::Stop\n");
00926     return (AudioOutputUnitStop(fAUHAL) == noErr) ? 0 : -1;
00927 }
00928 
00929 int JackCoreAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
00930 {
00931     OSStatus err;
00932     UInt32 outSize = sizeof(UInt32);
00933 
00934     err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
00935     if (err != noErr) {
00936         jack_error("Cannot set buffer size %ld", buffer_size);
00937         printError(err);
00938         return -1;
00939     }
00940 
00941         JackAudioDriver::SetBufferSize(buffer_size); // never fails
00942 
00943     // Input buffers do no change : prepare them only once
00944     for (int i = 0; i < fCaptureChannels; i++) {
00945         fJackInputData->mBuffers[i].mNumberChannels = 1;
00946         fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
00947         fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
00948     }
00949 
00950     return 0;
00951 }
00952 
00953 } // end of namespace
00954 
00955 
00956 #ifdef __cplusplus
00957 extern "C"
00958 {
00959 #endif
00960 
00961     jack_driver_desc_t* driver_get_descriptor() {
00962         jack_driver_desc_t *desc;
00963         unsigned int i;
00964         desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
00965 
00966         strcpy(desc->name, "coreaudio");
00967         desc->nparams = 13;
00968         desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
00969 
00970         i = 0;
00971         strcpy(desc->params[i].name, "channels");
00972         desc->params[i].character = 'c';
00973         desc->params[i].type = JackDriverParamInt;
00974         desc->params[i].value.ui = 0;
00975         strcpy(desc->params[i].short_desc, "Maximum number of channels");
00976         strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
00977 
00978         i++;
00979         strcpy(desc->params[i].name, "inchannels");
00980         desc->params[i].character = 'i';
00981         desc->params[i].type = JackDriverParamInt;
00982         desc->params[i].value.ui = 0;
00983         strcpy(desc->params[i].short_desc, "Maximum number of input channels");
00984         strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
00985 
00986         i++;
00987         strcpy(desc->params[i].name, "outchannels");
00988         desc->params[i].character = 'o';
00989         desc->params[i].type = JackDriverParamInt;
00990         desc->params[i].value.ui = 0;
00991         strcpy(desc->params[i].short_desc, "Maximum number of output channels");
00992         strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
00993 
00994         i++;
00995         strcpy(desc->params[i].name, "capture");
00996         desc->params[i].character = 'C';
00997         desc->params[i].type = JackDriverParamString;
00998         strcpy(desc->params[i].value.str, "will take default CoreAudio input device");
00999         strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set CoreAudio device name");
01000         strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
01001 
01002         i++;
01003         strcpy(desc->params[i].name, "playback");
01004         desc->params[i].character = 'P';
01005         desc->params[i].type = JackDriverParamString;
01006         strcpy(desc->params[i].value.str, "will take default CoreAudio output device");
01007         strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set CoreAudio device name");
01008         strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
01009 
01010         i++;
01011         strcpy (desc->params[i].name, "monitor");
01012         desc->params[i].character = 'm';
01013         desc->params[i].type = JackDriverParamBool;
01014         desc->params[i].value.i = 0;
01015         strcpy(desc->params[i].short_desc, "Provide monitor ports for the output");
01016         strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
01017 
01018         i++;
01019         strcpy(desc->params[i].name, "duplex");
01020         desc->params[i].character = 'D';
01021         desc->params[i].type = JackDriverParamBool;
01022         desc->params[i].value.i = TRUE;
01023         strcpy(desc->params[i].short_desc, "Provide both capture and playback ports");
01024         strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
01025 
01026         i++;
01027         strcpy(desc->params[i].name, "rate");
01028         desc->params[i].character = 'r';
01029         desc->params[i].type = JackDriverParamUInt;
01030         desc->params[i].value.ui = 44100U;
01031         strcpy(desc->params[i].short_desc, "Sample rate");
01032         strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
01033 
01034         i++;
01035         strcpy(desc->params[i].name, "period");
01036         desc->params[i].character = 'p';
01037         desc->params[i].type = JackDriverParamUInt;
01038         desc->params[i].value.ui = 128U;
01039         strcpy(desc->params[i].short_desc, "Frames per period");
01040         strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
01041 
01042         i++;
01043         strcpy(desc->params[i].name, "device");
01044         desc->params[i].character = 'd';
01045         desc->params[i].type = JackDriverParamString;
01046         desc->params[i].value.ui = 128U;
01047         strcpy(desc->params[i].value.str, "will take default CoreAudio device name");
01048         strcpy(desc->params[i].short_desc, "CoreAudio device name");
01049         strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
01050 
01051         i++;
01052         strcpy(desc->params[i].name, "input-latency");
01053         desc->params[i].character = 'I';
01054         desc->params[i].type = JackDriverParamUInt;
01055         desc->params[i].value.i = 0;
01056         strcpy(desc->params[i].short_desc, "Extra input latency");
01057         strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
01058 
01059         i++;
01060         strcpy(desc->params[i].name, "output-latency");
01061         desc->params[i].character = 'O';
01062         desc->params[i].type = JackDriverParamUInt;
01063         desc->params[i].value.i = 0;
01064         strcpy(desc->params[i].short_desc, "Extra output latency");
01065         strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
01066 
01067         i++;
01068         strcpy(desc->params[i].name, "list-devices");
01069         desc->params[i].character = 'l';
01070         desc->params[i].type = JackDriverParamBool;
01071         desc->params[i].value.i = TRUE;
01072         strcpy(desc->params[i].short_desc, "Display available CoreAudio devices");
01073         strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
01074 
01075         return desc;
01076     }
01077 
01078     Jack::JackDriverClientInterface* driver_initialize(Jack::JackEngine* engine, Jack::JackSynchro** table, const JSList* params) {
01079         jack_nframes_t srate = 44100;
01080         jack_nframes_t frames_per_interrupt = 128;
01081         int capture = FALSE;
01082         int playback = FALSE;
01083         int chan_in = 0;
01084         int chan_out = 0;
01085         bool monitor = false;
01086         char* capture_pcm_name = "";
01087         char* playback_pcm_name = "";
01088         const JSList *node;
01089         const jack_driver_param_t *param;
01090         jack_nframes_t systemic_input_latency = 0;
01091         jack_nframes_t systemic_output_latency = 0;
01092 
01093         for (node = params; node; node = jack_slist_next(node)) {
01094             param = (const jack_driver_param_t *) node->data;
01095 
01096             switch (param->character) {
01097 
01098                 case 'd':
01099                     capture_pcm_name = strdup(param->value.str);
01100                     playback_pcm_name = strdup(param->value.str);
01101                     break;
01102 
01103                 case 'D':
01104                     capture = TRUE;
01105                     playback = TRUE;
01106                     break;
01107 
01108                 case 'c':
01109                     chan_in = chan_out = (int) param->value.ui;
01110                     break;
01111 
01112                 case 'i':
01113                     chan_in = (int) param->value.ui;
01114                     break;
01115 
01116                 case 'o':
01117                     chan_out = (int) param->value.ui;
01118                     break;
01119 
01120                 case 'C':
01121                     capture = TRUE;
01122                     if (strcmp(param->value.str, "none") != 0) {
01123                         capture_pcm_name = strdup(param->value.str);
01124                     }
01125                     break;
01126 
01127                 case 'P':
01128                     playback = TRUE;
01129                     if (strcmp(param->value.str, "none") != 0) {
01130                         playback_pcm_name = strdup(param->value.str);
01131                     }
01132                     break;
01133 
01134                 case 'm':
01135                     monitor = param->value.i;
01136                     break;
01137 
01138                 case 'r':
01139                     srate = param->value.ui;
01140                     break;
01141 
01142                 case 'p':
01143                     frames_per_interrupt = (unsigned int) param->value.ui;
01144                     break;
01145 
01146                 case 'I':
01147                     systemic_input_latency = param->value.ui;
01148                     break;
01149 
01150                 case 'O':
01151                     systemic_output_latency = param->value.ui;
01152                     break;
01153 
01154                 case 'l':
01155                     Jack::DisplayDeviceNames();
01156                     break;
01157             }
01158         }
01159 
01160         /* duplex is the default */
01161         if (!capture && !playback) {
01162             capture = TRUE;
01163             playback = TRUE;
01164         }
01165 
01166         Jack::JackDriverClientInterface* driver = new Jack::JackCoreAudioDriver("coreaudio", engine, table);
01167         if (driver->Open(frames_per_interrupt, srate, capture, playback, chan_in, chan_out, monitor, capture_pcm_name, playback_pcm_name, systemic_input_latency, systemic_output_latency) == 0) {
01168             return driver;
01169         } else {
01170             delete driver;
01171             return NULL;
01172         }
01173     }
01174 
01175 #ifdef __cplusplus
01176 }
01177 #endif
01178 
01179 

Generated on Mon Oct 15 17:11:14 2007 for Jackdmp by  doxygen 1.5.1