jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1702 lines
64KB

  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include "JackCoreAudioDriver.h"
  16. #include "JackEngineControl.h"
  17. #include "JackMachThread.h"
  18. #include "JackGraphManager.h"
  19. #include "JackError.h"
  20. #include "JackClientControl.h"
  21. #include "JackDriverLoader.h"
  22. #include "JackGlobals.h"
  23. #include "JackTools.h"
  24. #include "JackCompilerDeps.h"
  25. #include <iostream>
  26. #include <CoreServices/CoreServices.h>
  27. #include <CoreFoundation/CFNumber.h>
  28. namespace Jack
  29. {
  30. static void Print4CharCode(const char* msg, long c)
  31. {
  32. UInt32 __4CC_number = (c);
  33. char __4CC_string[5];
  34. *((SInt32*)__4CC_string) = EndianU32_NtoB(__4CC_number);
  35. __4CC_string[4] = 0;
  36. jack_log("%s'%s'", (msg), __4CC_string);
  37. }
  38. static void printError(OSStatus err)
  39. {
  40. switch (err) {
  41. case kAudioHardwareNoError:
  42. jack_log("error code : kAudioHardwareNoError");
  43. break;
  44. case kAudioConverterErr_FormatNotSupported:
  45. jack_log("error code : kAudioConverterErr_FormatNotSupported");
  46. break;
  47. case kAudioConverterErr_OperationNotSupported:
  48. jack_log("error code : kAudioConverterErr_OperationNotSupported");
  49. break;
  50. case kAudioConverterErr_PropertyNotSupported:
  51. jack_log("error code : kAudioConverterErr_PropertyNotSupported");
  52. break;
  53. case kAudioConverterErr_InvalidInputSize:
  54. jack_log("error code : kAudioConverterErr_InvalidInputSize");
  55. break;
  56. case kAudioConverterErr_InvalidOutputSize:
  57. jack_log("error code : kAudioConverterErr_InvalidOutputSize");
  58. break;
  59. case kAudioConverterErr_UnspecifiedError:
  60. jack_log("error code : kAudioConverterErr_UnspecifiedError");
  61. break;
  62. case kAudioConverterErr_BadPropertySizeError:
  63. jack_log("error code : kAudioConverterErr_BadPropertySizeError");
  64. break;
  65. case kAudioConverterErr_RequiresPacketDescriptionsError:
  66. jack_log("error code : kAudioConverterErr_RequiresPacketDescriptionsError");
  67. break;
  68. case kAudioConverterErr_InputSampleRateOutOfRange:
  69. jack_log("error code : kAudioConverterErr_InputSampleRateOutOfRange");
  70. break;
  71. case kAudioConverterErr_OutputSampleRateOutOfRange:
  72. jack_log("error code : kAudioConverterErr_OutputSampleRateOutOfRange");
  73. break;
  74. case kAudioHardwareNotRunningError:
  75. jack_log("error code : kAudioHardwareNotRunningError");
  76. break;
  77. case kAudioHardwareUnknownPropertyError:
  78. jack_log("error code : kAudioHardwareUnknownPropertyError");
  79. break;
  80. case kAudioHardwareIllegalOperationError:
  81. jack_log("error code : kAudioHardwareIllegalOperationError");
  82. break;
  83. case kAudioHardwareBadDeviceError:
  84. jack_log("error code : kAudioHardwareBadDeviceError");
  85. break;
  86. case kAudioHardwareBadStreamError:
  87. jack_log("error code : kAudioHardwareBadStreamError");
  88. break;
  89. case kAudioDeviceUnsupportedFormatError:
  90. jack_log("error code : kAudioDeviceUnsupportedFormatError");
  91. break;
  92. case kAudioDevicePermissionsError:
  93. jack_log("error code : kAudioDevicePermissionsError");
  94. break;
  95. case kAudioHardwareBadObjectError:
  96. jack_log("error code : kAudioHardwareBadObjectError");
  97. break;
  98. case kAudioHardwareUnsupportedOperationError:
  99. jack_log("error code : kAudioHardwareUnsupportedOperationError");
  100. break;
  101. default:
  102. Print4CharCode("error code : unknown", err);
  103. break;
  104. }
  105. }
  106. static OSStatus DisplayDeviceNames()
  107. {
  108. UInt32 size;
  109. Boolean isWritable;
  110. int i, deviceNum;
  111. OSStatus err;
  112. CFStringRef UIname;
  113. err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
  114. if (err != noErr)
  115. return err;
  116. deviceNum = size / sizeof(AudioDeviceID);
  117. AudioDeviceID devices[deviceNum];
  118. err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
  119. if (err != noErr)
  120. return err;
  121. for (i = 0; i < deviceNum; i++) {
  122. char device_name[256];
  123. char internal_name[256];
  124. size = sizeof(CFStringRef);
  125. UIname = NULL;
  126. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
  127. if (err == noErr) {
  128. CFStringGetCString(UIname, internal_name, 256, CFStringGetSystemEncoding());
  129. } else {
  130. goto error;
  131. }
  132. size = 256;
  133. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &size, device_name);
  134. if (err != noErr)
  135. return err;
  136. jack_info("Device name = \'%s\', internal_name = \'%s\' (to be used as -C, -P, or -d parameter)", device_name, internal_name);
  137. }
  138. return noErr;
  139. error:
  140. if (UIname != NULL)
  141. CFRelease(UIname);
  142. return err;
  143. }
  144. static CFStringRef GetDeviceName(AudioDeviceID id)
  145. {
  146. UInt32 size = sizeof(CFStringRef);
  147. CFStringRef UIname;
  148. OSStatus err = AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
  149. return (err == noErr) ? UIname : NULL;
  150. }
  151. OSStatus JackCoreAudioDriver::Render(void *inRefCon,
  152. AudioUnitRenderActionFlags *ioActionFlags,
  153. const AudioTimeStamp *inTimeStamp,
  154. UInt32 inBusNumber,
  155. UInt32 inNumberFrames,
  156. AudioBufferList *ioData)
  157. {
  158. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inRefCon;
  159. driver->fActionFags = ioActionFlags;
  160. driver->fCurrentTime = (AudioTimeStamp *)inTimeStamp;
  161. driver->fDriverOutputData = ioData;
  162. driver->CycleTakeBeginTime();
  163. return driver->Process();
  164. }
  165. int JackCoreAudioDriver::Read()
  166. {
  167. AudioUnitRender(fAUHAL, fActionFags, fCurrentTime, 1, fEngineControl->fBufferSize, fJackInputData);
  168. return 0;
  169. }
  170. int JackCoreAudioDriver::Write()
  171. {
  172. for (int i = 0; i < fPlaybackChannels; i++) {
  173. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  174. float* buffer = GetOutputBuffer(i);
  175. int size = sizeof(float) * fEngineControl->fBufferSize;
  176. memcpy((float*)fDriverOutputData->mBuffers[i].mData, buffer, size);
  177. // Monitor ports
  178. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
  179. memcpy(GetMonitorBuffer(i), buffer, size);
  180. } else {
  181. memset((float*)fDriverOutputData->mBuffers[i].mData, 0, sizeof(float) * fEngineControl->fBufferSize);
  182. }
  183. }
  184. return 0;
  185. }
  186. // Will run only once
  187. OSStatus JackCoreAudioDriver::MeasureCallback(AudioDeviceID inDevice,
  188. const AudioTimeStamp* inNow,
  189. const AudioBufferList* inInputData,
  190. const AudioTimeStamp* inInputTime,
  191. AudioBufferList* outOutputData,
  192. const AudioTimeStamp* inOutputTime,
  193. void* inClientData)
  194. {
  195. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
  196. AudioDeviceStop(driver->fDeviceID, MeasureCallback);
  197. jack_log("JackCoreAudioDriver::MeasureCallback called");
  198. JackMachThread::GetParams(pthread_self(), &driver->fEngineControl->fPeriod, &driver->fEngineControl->fComputation, &driver->fEngineControl->fConstraint);
  199. if (driver->fComputationGrain > 0) {
  200. jack_log("JackCoreAudioDriver::MeasureCallback : RT thread computation setup to %d percent of period", int(driver->fComputationGrain * 100));
  201. driver->fEngineControl->fComputation = driver->fEngineControl->fPeriod * driver->fComputationGrain;
  202. }
  203. // Setup threadded based log function
  204. set_threaded_log_function();
  205. return noErr;
  206. }
  207. OSStatus JackCoreAudioDriver::SRNotificationCallback(AudioDeviceID inDevice,
  208. UInt32 inChannel,
  209. Boolean isInput,
  210. AudioDevicePropertyID inPropertyID,
  211. void* inClientData)
  212. {
  213. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
  214. switch (inPropertyID) {
  215. case kAudioDevicePropertyNominalSampleRate: {
  216. jack_log("JackCoreAudioDriver::SRNotificationCallback kAudioDevicePropertyNominalSampleRate");
  217. driver->fState = true;
  218. break;
  219. }
  220. }
  221. return noErr;
  222. }
  223. // A better implementation would possibly try to recover in case of hardware device change (see HALLAB HLFilePlayerWindowControllerAudioDevicePropertyListenerProc code)
  224. OSStatus JackCoreAudioDriver::DeviceNotificationCallback(AudioDeviceID inDevice,
  225. UInt32 inChannel,
  226. Boolean isInput,
  227. AudioDevicePropertyID inPropertyID,
  228. void* inClientData)
  229. {
  230. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
  231. switch (inPropertyID) {
  232. case kAudioDeviceProcessorOverload: {
  233. jack_error("JackCoreAudioDriver::DeviceNotificationCallback kAudioDeviceProcessorOverload");
  234. jack_time_t cur_time = GetMicroSeconds();
  235. driver->NotifyXRun(cur_time, float(cur_time - driver->fBeginDateUst)); // Better this value than nothing...
  236. break;
  237. }
  238. case kAudioDevicePropertyStreamConfiguration: {
  239. jack_error("Cannot handle kAudioDevicePropertyStreamConfiguration : server will quit...");
  240. driver->NotifyFailure(JackBackendError, "Another application has changed the device configuration."); // Message length limited to JACK_MESSAGE_SIZE
  241. driver->CloseAUHAL();
  242. kill(JackTools::GetPID(), SIGINT);
  243. return kAudioHardwareUnsupportedOperationError;
  244. }
  245. case kAudioDevicePropertyNominalSampleRate: {
  246. jack_error("Cannot handle kAudioDevicePropertyNominalSampleRate : server will quit...");
  247. driver->NotifyFailure(JackBackendError, "Another application has changed the sample rate."); // Message length limited to JACK_MESSAGE_SIZE
  248. driver->CloseAUHAL();
  249. kill(JackTools::GetPID(), SIGINT);
  250. return kAudioHardwareUnsupportedOperationError;
  251. }
  252. }
  253. return noErr;
  254. }
  255. OSStatus JackCoreAudioDriver::GetDeviceIDFromUID(const char* UID, AudioDeviceID* id)
  256. {
  257. UInt32 size = sizeof(AudioValueTranslation);
  258. CFStringRef inIUD = CFStringCreateWithCString(NULL, UID, CFStringGetSystemEncoding());
  259. AudioValueTranslation value = { &inIUD, sizeof(CFStringRef), id, sizeof(AudioDeviceID) };
  260. if (inIUD == NULL) {
  261. return kAudioHardwareUnspecifiedError;
  262. } else {
  263. OSStatus res = AudioHardwareGetProperty(kAudioHardwarePropertyDeviceForUID, &size, &value);
  264. CFRelease(inIUD);
  265. jack_log("GetDeviceIDFromUID %s %ld", UID, *id);
  266. return (*id == kAudioDeviceUnknown) ? kAudioHardwareBadDeviceError : res;
  267. }
  268. }
  269. OSStatus JackCoreAudioDriver::GetDefaultDevice(AudioDeviceID* id)
  270. {
  271. OSStatus res;
  272. UInt32 theSize = sizeof(UInt32);
  273. AudioDeviceID inDefault;
  274. AudioDeviceID outDefault;
  275. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr)
  276. return res;
  277. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr)
  278. return res;
  279. jack_log("GetDefaultDevice: input = %ld output = %ld", inDefault, outDefault);
  280. // Get the device only if default input and output are the same
  281. if (inDefault == outDefault) {
  282. *id = inDefault;
  283. return noErr;
  284. } else {
  285. jack_error("Default input and output devices are not the same !!");
  286. return kAudioHardwareBadDeviceError;
  287. }
  288. }
  289. OSStatus JackCoreAudioDriver::GetDefaultInputDevice(AudioDeviceID* id)
  290. {
  291. OSStatus res;
  292. UInt32 theSize = sizeof(UInt32);
  293. AudioDeviceID inDefault;
  294. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr)
  295. return res;
  296. jack_log("GetDefaultInputDevice: input = %ld ", inDefault);
  297. *id = inDefault;
  298. return noErr;
  299. }
  300. OSStatus JackCoreAudioDriver::GetDefaultOutputDevice(AudioDeviceID* id)
  301. {
  302. OSStatus res;
  303. UInt32 theSize = sizeof(UInt32);
  304. AudioDeviceID outDefault;
  305. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr)
  306. return res;
  307. jack_log("GetDefaultOutputDevice: output = %ld", outDefault);
  308. *id = outDefault;
  309. return noErr;
  310. }
  311. OSStatus JackCoreAudioDriver::GetDeviceNameFromID(AudioDeviceID id, char* name)
  312. {
  313. UInt32 size = 256;
  314. return AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceName, &size, name);
  315. }
  316. OSStatus JackCoreAudioDriver::GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput)
  317. {
  318. OSStatus err = noErr;
  319. UInt32 outSize;
  320. Boolean outWritable;
  321. AudioBufferList* bufferList = 0;
  322. channelCount = 0;
  323. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
  324. if (err == noErr) {
  325. bufferList = (AudioBufferList*)malloc(outSize);
  326. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
  327. if (err == noErr) {
  328. for (unsigned int i = 0; i < bufferList->mNumberBuffers; i++)
  329. channelCount += bufferList->mBuffers[i].mNumberChannels;
  330. }
  331. if (bufferList)
  332. free(bufferList);
  333. }
  334. return err;
  335. }
  336. JackCoreAudioDriver::JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
  337. : JackAudioDriver(name, alias, engine, table),
  338. fJackInputData(NULL),
  339. fDriverOutputData(NULL),
  340. fPluginID(0),
  341. fState(false),
  342. fHogged(false),
  343. fIOUsage(1.f),
  344. fComputationGrain(-1.f)
  345. {}
  346. JackCoreAudioDriver::~JackCoreAudioDriver()
  347. {}
  348. OSStatus JackCoreAudioDriver::DestroyAggregateDevice()
  349. {
  350. OSStatus osErr = noErr;
  351. AudioObjectPropertyAddress pluginAOPA;
  352. pluginAOPA.mSelector = kAudioPlugInDestroyAggregateDevice;
  353. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  354. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  355. UInt32 outDataSize;
  356. osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
  357. if (osErr != noErr) {
  358. jack_error("JackCoreAudioDriver::DestroyAggregateDevice : AudioObjectGetPropertyDataSize error");
  359. printError(osErr);
  360. return osErr;
  361. }
  362. osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, 0, NULL, &outDataSize, &fDeviceID);
  363. if (osErr != noErr) {
  364. jack_error("JackCoreAudioDriver::DestroyAggregateDevice : AudioObjectGetPropertyData error");
  365. printError(osErr);
  366. return osErr;
  367. }
  368. return noErr;
  369. }
  370. OSStatus JackCoreAudioDriver::CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, AudioDeviceID* outAggregateDevice)
  371. {
  372. OSStatus osErr = noErr;
  373. UInt32 outSize;
  374. Boolean outWritable;
  375. //---------------------------------------------------------------------------
  376. // Start to create a new aggregate by getting the base audio hardware plugin
  377. //---------------------------------------------------------------------------
  378. char capture_name[256];
  379. char playback_name[256];
  380. GetDeviceNameFromID(captureDeviceID, capture_name);
  381. GetDeviceNameFromID(playbackDeviceID, playback_name);
  382. jack_info("Separated input = '%s' and output = '%s' devices, create a private aggregate device to handle them...", capture_name, playback_name);
  383. osErr = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyPlugInForBundleID, &outSize, &outWritable);
  384. if (osErr != noErr) {
  385. jack_error("JackCoreAudioDriver::CreateAggregateDevice : AudioHardwareGetPropertyInfo kAudioHardwarePropertyPlugInForBundleID error");
  386. printError(osErr);
  387. return osErr;
  388. }
  389. AudioValueTranslation pluginAVT;
  390. CFStringRef inBundleRef = CFSTR("com.apple.audio.CoreAudio");
  391. pluginAVT.mInputData = &inBundleRef;
  392. pluginAVT.mInputDataSize = sizeof(inBundleRef);
  393. pluginAVT.mOutputData = &fPluginID;
  394. pluginAVT.mOutputDataSize = sizeof(fPluginID);
  395. osErr = AudioHardwareGetProperty(kAudioHardwarePropertyPlugInForBundleID, &outSize, &pluginAVT);
  396. if (osErr != noErr) {
  397. jack_error("JackCoreAudioDriver::CreateAggregateDevice : AudioHardwareGetProperty kAudioHardwarePropertyPlugInForBundleID error");
  398. printError(osErr);
  399. return osErr;
  400. }
  401. //-------------------------------------------------
  402. // Create a CFDictionary for our aggregate device
  403. //-------------------------------------------------
  404. CFMutableDictionaryRef aggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  405. CFStringRef AggregateDeviceNameRef = CFSTR("JackDuplex");
  406. CFStringRef AggregateDeviceUIDRef = CFSTR("com.grame.JackDuplex");
  407. // add the name of the device to the dictionary
  408. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceNameKey), AggregateDeviceNameRef);
  409. // add our choice of UID for the aggregate device to the dictionary
  410. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceUIDKey), AggregateDeviceUIDRef);
  411. // add a "private aggregate key" to the dictionary
  412. int value = 1;
  413. CFNumberRef AggregateDeviceNumberRef = CFNumberCreate(NULL, kCFNumberIntType, &value);
  414. SInt32 system;
  415. Gestalt(gestaltSystemVersion, &system);
  416. jack_log("JackCoreAudioDriver::CreateAggregateDevice : system version = %x limit = %x", system, 0x00001054);
  417. // Starting with 10.5.4 systems, the AD can be internal... (better)
  418. if (system < 0x00001054) {
  419. jack_log("JackCoreAudioDriver::CreateAggregateDevice : public aggregate device....");
  420. } else {
  421. jack_log("JackCoreAudioDriver::CreateAggregateDevice : private aggregate device....");
  422. CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceIsPrivateKey), AggregateDeviceNumberRef);
  423. }
  424. //-------------------------------------------------
  425. // Create a CFMutableArray for our sub-device list
  426. //-------------------------------------------------
  427. CFStringRef captureDeviceUID = GetDeviceName(captureDeviceID);
  428. CFStringRef playbackDeviceUID = GetDeviceName(playbackDeviceID);
  429. if (captureDeviceUID == NULL || playbackDeviceUID == NULL)
  430. return -1;
  431. // we need to append the UID for each device to a CFMutableArray, so create one here
  432. CFMutableArrayRef subDevicesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
  433. // two sub-devices in this example, so append the sub-device's UID to the CFArray
  434. CFArrayAppendValue(subDevicesArray, captureDeviceUID);
  435. CFArrayAppendValue(subDevicesArray, playbackDeviceUID);
  436. //-----------------------------------------------------------------------
  437. // Feed the dictionary to the plugin, to create a blank aggregate device
  438. //-----------------------------------------------------------------------
  439. AudioObjectPropertyAddress pluginAOPA;
  440. pluginAOPA.mSelector = kAudioPlugInCreateAggregateDevice;
  441. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  442. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  443. UInt32 outDataSize;
  444. osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize);
  445. if (osErr != noErr) {
  446. jack_error("JackCoreAudioDriver::CreateAggregateDevice : AudioObjectGetPropertyDataSize error");
  447. printError(osErr);
  448. return osErr;
  449. }
  450. osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, sizeof(aggDeviceDict), &aggDeviceDict, &outDataSize, outAggregateDevice);
  451. if (osErr != noErr) {
  452. jack_error("JackCoreAudioDriver::CreateAggregateDevice : AudioObjectGetPropertyData error");
  453. printError(osErr);
  454. return osErr;
  455. }
  456. // pause for a bit to make sure that everything completed correctly
  457. // this is to work around a bug in the HAL where a new aggregate device seems to disappear briefly after it is created
  458. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  459. //-------------------------
  460. // Set the sub-device list
  461. //-------------------------
  462. pluginAOPA.mSelector = kAudioAggregateDevicePropertyFullSubDeviceList;
  463. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  464. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  465. outDataSize = sizeof(CFMutableArrayRef);
  466. osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &subDevicesArray);
  467. if (osErr != noErr) {
  468. jack_error("JackCoreAudioDriver::CreateAggregateDevice : AudioObjectSetPropertyData for sub-device list error");
  469. printError(osErr);
  470. return osErr;
  471. }
  472. // pause again to give the changes time to take effect
  473. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  474. //-----------------------
  475. // Set the master device
  476. //-----------------------
  477. // set the master device manually (this is the device which will act as the master clock for the aggregate device)
  478. // pass in the UID of the device you want to use
  479. pluginAOPA.mSelector = kAudioAggregateDevicePropertyMasterSubDevice;
  480. pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal;
  481. pluginAOPA.mElement = kAudioObjectPropertyElementMaster;
  482. outDataSize = sizeof(CFStringRef);
  483. osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &captureDeviceUID); // capture is master...
  484. if (osErr != noErr) {
  485. jack_error("JackCoreAudioDriver::CreateAggregateDevice : AudioObjectSetPropertyData for master device error");
  486. printError(osErr);
  487. return osErr;
  488. }
  489. // pause again to give the changes time to take effect
  490. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
  491. //----------
  492. // Clean up
  493. //----------
  494. // release the private AD key
  495. CFRelease(AggregateDeviceNumberRef);
  496. // release the CF objects we have created - we don't need them any more
  497. CFRelease(aggDeviceDict);
  498. CFRelease(subDevicesArray);
  499. // release the device UID
  500. CFRelease(captureDeviceUID);
  501. CFRelease(playbackDeviceUID);
  502. jack_log("New aggregate device %ld", *outAggregateDevice);
  503. return noErr;
  504. }
  505. int JackCoreAudioDriver::SetupDevices(const char* capture_driver_uid, const char* playback_driver_uid, char* capture_driver_name, char* playback_driver_name)
  506. {
  507. capture_driver_name[0] = 0;
  508. playback_driver_name[0] = 0;
  509. // Duplex
  510. if (strcmp(capture_driver_uid, "") != 0 && strcmp(playback_driver_uid, "") != 0) {
  511. jack_log("JackCoreAudioDriver::Open duplex");
  512. // Same device for capture and playback...
  513. if (strcmp(capture_driver_uid, playback_driver_uid) == 0) {
  514. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  515. jack_log("Will take default in/out");
  516. if (GetDefaultDevice(&fDeviceID) != noErr) {
  517. jack_error("Cannot open default device");
  518. return -1;
  519. }
  520. }
  521. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  522. jack_error("Cannot get device name from device ID");
  523. return -1;
  524. }
  525. } else {
  526. // Creates aggregate device
  527. AudioDeviceID captureID, playbackID;
  528. if (GetDeviceIDFromUID(capture_driver_uid, &captureID) != noErr)
  529. return -1;
  530. if (GetDeviceIDFromUID(playback_driver_uid, &playbackID) != noErr)
  531. return -1;
  532. if (CreateAggregateDevice(captureID, playbackID, &fDeviceID) != noErr)
  533. return -1;
  534. }
  535. // Capture only
  536. } else if (strcmp(capture_driver_uid, "") != 0) {
  537. jack_log("JackCoreAudioDriver::Open capture only");
  538. if (GetDeviceIDFromUID(capture_driver_uid, &fDeviceID) != noErr) {
  539. jack_log("Will take default input");
  540. if (GetDefaultInputDevice(&fDeviceID) != noErr) {
  541. jack_error("Cannot open default device");
  542. return -1;
  543. }
  544. }
  545. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr) {
  546. jack_error("Cannot get device name from device ID");
  547. return -1;
  548. }
  549. // Playback only
  550. } else if (strcmp(playback_driver_uid, "") != 0) {
  551. jack_log("JackCoreAudioDriver::Open playback only");
  552. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  553. jack_log("Will take default output");
  554. if (GetDefaultOutputDevice(&fDeviceID) != noErr) {
  555. jack_error("Cannot open default device");
  556. return -1;
  557. }
  558. }
  559. if (GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  560. jack_error("Cannot get device name from device ID");
  561. return -1;
  562. }
  563. // Use default driver in duplex mode
  564. } else {
  565. jack_log("JackCoreAudioDriver::Open default driver");
  566. if (GetDefaultDevice(&fDeviceID) != noErr) {
  567. jack_error("Cannot open default device");
  568. return -1;
  569. }
  570. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  571. jack_error("Cannot get device name from device ID");
  572. return -1;
  573. }
  574. }
  575. if (fHogged) {
  576. if (TakeHog()) {
  577. jack_info("Device = %ld has been hogged", fDeviceID);
  578. } else {
  579. jack_error("Cannot hog device = %ld", fDeviceID);
  580. }
  581. }
  582. return 0;
  583. }
  584. /*
  585. Return the max possible input channels in in_nChannels and output channels in out_nChannels.
  586. */
  587. int JackCoreAudioDriver::SetupChannels(bool capturing, bool playing, int& inchannels, int& outchannels, int& in_nChannels, int& out_nChannels, bool strict)
  588. {
  589. OSStatus err = noErr;
  590. if (capturing) {
  591. err = GetTotalChannels(fDeviceID, in_nChannels, true);
  592. if (err != noErr) {
  593. jack_error("Cannot get input channel number");
  594. printError(err);
  595. return -1;
  596. } else {
  597. jack_log("Max input channels : %d", in_nChannels);
  598. }
  599. }
  600. if (playing) {
  601. err = GetTotalChannels(fDeviceID, out_nChannels, false);
  602. if (err != noErr) {
  603. jack_error("Cannot get output channel number");
  604. printError(err);
  605. return -1;
  606. } else {
  607. jack_log("Max output channels : %d", out_nChannels);
  608. }
  609. }
  610. if (inchannels > in_nChannels) {
  611. jack_error("This device hasn't required input channels inchannels = %d in_nChannels = %d", inchannels, in_nChannels);
  612. if (strict)
  613. return -1;
  614. }
  615. if (outchannels > out_nChannels) {
  616. jack_error("This device hasn't required output channels outchannels = %d out_nChannels = %d", outchannels, out_nChannels);
  617. if (strict)
  618. return -1;
  619. }
  620. if (inchannels == 0) {
  621. jack_log("Setup max in channels = %d", in_nChannels);
  622. inchannels = in_nChannels;
  623. }
  624. if (outchannels == 0) {
  625. jack_log("Setup max out channels = %d", out_nChannels);
  626. outchannels = out_nChannels;
  627. }
  628. return 0;
  629. }
  630. int JackCoreAudioDriver::SetupBufferSizeAndSampleRate(jack_nframes_t buffer_size, jack_nframes_t samplerate)
  631. {
  632. OSStatus err = noErr;
  633. UInt32 outSize;
  634. Float64 sampleRate;
  635. // Setting buffer size
  636. outSize = sizeof(UInt32);
  637. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
  638. if (err != noErr) {
  639. jack_error("Cannot set buffer size %ld", buffer_size);
  640. printError(err);
  641. return -1;
  642. }
  643. // Get sample rate
  644. outSize = sizeof(Float64);
  645. err = AudioDeviceGetProperty(fDeviceID, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
  646. if (err != noErr) {
  647. jack_error("Cannot get current sample rate");
  648. printError(err);
  649. return -1;
  650. }
  651. // If needed, set new sample rate
  652. if (samplerate != (jack_nframes_t)sampleRate) {
  653. sampleRate = (Float64)samplerate;
  654. // To get SR change notification
  655. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback, this);
  656. if (err != noErr) {
  657. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  658. printError(err);
  659. return -1;
  660. }
  661. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate);
  662. if (err != noErr) {
  663. jack_error("Cannot set sample rate = %ld", samplerate);
  664. printError(err);
  665. return -1;
  666. }
  667. // Waiting for SR change notification
  668. int count = 0;
  669. while (!fState && count++ < 100) {
  670. usleep(100000);
  671. jack_log("Wait count = %d", count);
  672. }
  673. // Remove SR change notification
  674. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback);
  675. }
  676. return 0;
  677. }
  678. int JackCoreAudioDriver::OpenAUHAL(bool capturing,
  679. bool playing,
  680. int inchannels,
  681. int outchannels,
  682. int in_nChannels,
  683. int out_nChannels,
  684. jack_nframes_t buffer_size,
  685. jack_nframes_t samplerate,
  686. bool strict)
  687. {
  688. ComponentResult err1;
  689. UInt32 enableIO;
  690. AudioStreamBasicDescription srcFormat, dstFormat;
  691. jack_log("OpenAUHAL capturing = %d playing = %d inchannels = %d outchannels = %d in_nChannels = %d out_nChannels = %d", capturing, playing, inchannels, outchannels, in_nChannels, out_nChannels);
  692. if (inchannels == 0 && outchannels == 0) {
  693. jack_error("No input and output channels...");
  694. return -1;
  695. }
  696. // AUHAL
  697. ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  698. Component HALOutput = FindNextComponent(NULL, &cd);
  699. err1 = OpenAComponent(HALOutput, &fAUHAL);
  700. if (err1 != noErr) {
  701. jack_error("Error calling OpenAComponent");
  702. printError(err1);
  703. return -1;
  704. }
  705. err1 = AudioUnitInitialize(fAUHAL);
  706. if (err1 != noErr) {
  707. jack_error("Cannot initialize AUHAL unit");
  708. printError(err1);
  709. return -1;
  710. }
  711. // Start I/O
  712. if (capturing && inchannels > 0) {
  713. enableIO = 1;
  714. jack_log("Setup AUHAL input on");
  715. } else {
  716. enableIO = 0;
  717. jack_log("Setup AUHAL input off");
  718. }
  719. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
  720. if (err1 != noErr) {
  721. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
  722. printError(err1);
  723. if (strict)
  724. return -1;
  725. }
  726. if (playing && outchannels > 0) {
  727. enableIO = 1;
  728. jack_log("Setup AUHAL output on");
  729. } else {
  730. enableIO = 0;
  731. jack_log("Setup AUHAL output off");
  732. }
  733. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
  734. if (err1 != noErr) {
  735. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
  736. printError(err1);
  737. if (strict)
  738. return -1;
  739. }
  740. AudioDeviceID currAudioDeviceID;
  741. UInt32 size = sizeof(AudioDeviceID);
  742. err1 = AudioUnitGetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &currAudioDeviceID, &size);
  743. if (err1 != noErr) {
  744. jack_error("Error calling AudioUnitGetProperty - kAudioOutputUnitProperty_CurrentDevice");
  745. printError(err1);
  746. if (strict)
  747. return -1;
  748. } else {
  749. jack_log("AudioUnitGetPropertyCurrentDevice = %d", currAudioDeviceID);
  750. }
  751. // Setup up choosen device, in both input and output cases
  752. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
  753. if (err1 != noErr) {
  754. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
  755. printError(err1);
  756. if (strict)
  757. return -1;
  758. }
  759. // Set buffer size
  760. if (capturing && inchannels > 0) {
  761. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&buffer_size, sizeof(UInt32));
  762. if (err1 != noErr) {
  763. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  764. printError(err1);
  765. if (strict)
  766. return -1;
  767. }
  768. }
  769. if (playing && outchannels > 0) {
  770. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&buffer_size, sizeof(UInt32));
  771. if (err1 != noErr) {
  772. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  773. printError(err1);
  774. if (strict)
  775. return -1;
  776. }
  777. }
  778. // Setup channel map
  779. if (capturing && inchannels > 0 && inchannels < in_nChannels) {
  780. SInt32 chanArr[in_nChannels];
  781. for (int i = 0; i < in_nChannels; i++) {
  782. chanArr[i] = -1;
  783. }
  784. for (int i = 0; i < inchannels; i++) {
  785. chanArr[i] = i;
  786. }
  787. AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
  788. if (err1 != noErr) {
  789. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
  790. printError(err1);
  791. }
  792. }
  793. if (playing && outchannels > 0 && outchannels < out_nChannels) {
  794. SInt32 chanArr[out_nChannels];
  795. for (int i = 0; i < out_nChannels; i++) {
  796. chanArr[i] = -1;
  797. }
  798. for (int i = 0; i < outchannels; i++) {
  799. chanArr[i] = i;
  800. }
  801. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
  802. if (err1 != noErr) {
  803. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
  804. printError(err1);
  805. }
  806. }
  807. // Setup stream converters
  808. jack_log("Setup AUHAL input stream converter SR = %ld", samplerate);
  809. srcFormat.mSampleRate = samplerate;
  810. srcFormat.mFormatID = kAudioFormatLinearPCM;
  811. srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  812. srcFormat.mBytesPerPacket = sizeof(float);
  813. srcFormat.mFramesPerPacket = 1;
  814. srcFormat.mBytesPerFrame = sizeof(float);
  815. srcFormat.mChannelsPerFrame = outchannels;
  816. srcFormat.mBitsPerChannel = 32;
  817. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, sizeof(AudioStreamBasicDescription));
  818. if (err1 != noErr) {
  819. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  820. printError(err1);
  821. }
  822. jack_log("Setup AUHAL output stream converter SR = %ld", samplerate);
  823. dstFormat.mSampleRate = samplerate;
  824. dstFormat.mFormatID = kAudioFormatLinearPCM;
  825. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  826. dstFormat.mBytesPerPacket = sizeof(float);
  827. dstFormat.mFramesPerPacket = 1;
  828. dstFormat.mBytesPerFrame = sizeof(float);
  829. dstFormat.mChannelsPerFrame = inchannels;
  830. dstFormat.mBitsPerChannel = 32;
  831. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, sizeof(AudioStreamBasicDescription));
  832. if (err1 != noErr) {
  833. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  834. printError(err1);
  835. }
  836. // Setup callbacks
  837. if (inchannels > 0 && outchannels == 0) {
  838. AURenderCallbackStruct output;
  839. output.inputProc = Render;
  840. output.inputProcRefCon = this;
  841. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  842. if (err1 != noErr) {
  843. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  844. printError(err1);
  845. return -1;
  846. }
  847. } else {
  848. AURenderCallbackStruct output;
  849. output.inputProc = Render;
  850. output.inputProcRefCon = this;
  851. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  852. if (err1 != noErr) {
  853. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  854. printError(err1);
  855. return -1;
  856. }
  857. }
  858. return 0;
  859. }
  860. int JackCoreAudioDriver::SetupBuffers(int inchannels)
  861. {
  862. // Prepare buffers
  863. fJackInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
  864. if (fJackInputData == 0) {
  865. jack_error("Cannot allocate memory for input buffers");
  866. return -1;
  867. }
  868. fJackInputData->mNumberBuffers = inchannels;
  869. for (int i = 0; i < fCaptureChannels; i++) {
  870. fJackInputData->mBuffers[i].mNumberChannels = 1;
  871. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
  872. }
  873. return 0;
  874. }
  875. void JackCoreAudioDriver::DisposeBuffers()
  876. {
  877. if (fJackInputData) {
  878. free(fJackInputData);
  879. fJackInputData = 0;
  880. }
  881. }
  882. void JackCoreAudioDriver::CloseAUHAL()
  883. {
  884. AudioUnitUninitialize(fAUHAL);
  885. CloseComponent(fAUHAL);
  886. }
  887. int JackCoreAudioDriver::AddListeners()
  888. {
  889. OSStatus err = noErr;
  890. // Add listeners
  891. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback, this);
  892. if (err != noErr) {
  893. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
  894. printError(err);
  895. return -1;
  896. }
  897. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback, this);
  898. if (err != noErr) {
  899. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioHardwarePropertyDevices");
  900. printError(err);
  901. return -1;
  902. }
  903. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback, this);
  904. if (err != noErr) {
  905. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  906. printError(err);
  907. return -1;
  908. }
  909. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback, this);
  910. if (err != noErr) {
  911. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyDeviceIsRunning");
  912. printError(err);
  913. return -1;
  914. }
  915. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
  916. if (err != noErr) {
  917. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
  918. printError(err);
  919. return -1;
  920. }
  921. err = AudioDeviceAddPropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
  922. if (err != noErr) {
  923. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
  924. printError(err);
  925. return -1;
  926. }
  927. if (!fEngineControl->fSyncMode && fIOUsage != 1.f) {
  928. UInt32 outSize = sizeof(float);
  929. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyIOCycleUsage, outSize, &fIOUsage);
  930. if (err != noErr) {
  931. jack_error("Error calling AudioDeviceSetProperty kAudioDevicePropertyIOCycleUsage");
  932. printError(err);
  933. }
  934. }
  935. return 0;
  936. }
  937. void JackCoreAudioDriver::RemoveListeners()
  938. {
  939. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback);
  940. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback);
  941. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback);
  942. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback);
  943. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
  944. AudioDeviceRemovePropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
  945. }
  946. int JackCoreAudioDriver::Open(jack_nframes_t buffer_size,
  947. jack_nframes_t samplerate,
  948. bool capturing,
  949. bool playing,
  950. int inchannels,
  951. int outchannels,
  952. bool monitor,
  953. const char* capture_driver_uid,
  954. const char* playback_driver_uid,
  955. jack_nframes_t capture_latency,
  956. jack_nframes_t playback_latency,
  957. int async_output_latency,
  958. int computation_grain,
  959. bool hogged)
  960. {
  961. int in_nChannels = 0;
  962. int out_nChannels = 0;
  963. char capture_driver_name[256];
  964. char playback_driver_name[256];
  965. // Keep initial state
  966. fCapturing = capturing;
  967. fPlaying = playing;
  968. fInChannels = inchannels;
  969. fOutChannels = outchannels;
  970. fMonitor = monitor;
  971. strcpy(fCaptureUID, capture_driver_uid);
  972. strcpy(fPlaybackUID, playback_driver_uid);
  973. fCaptureLatency = capture_latency;
  974. fPlaybackLatency = playback_latency;
  975. fIOUsage = float(async_output_latency) / 100.f;
  976. fComputationGrain = float(computation_grain) / 100.f;
  977. fHogged = hogged;
  978. SInt32 major;
  979. SInt32 minor;
  980. Gestalt(gestaltSystemVersionMajor, &major);
  981. Gestalt(gestaltSystemVersionMinor, &minor);
  982. // Starting with 10.6 systems, the HAL notification thread is created internally
  983. if (major == 10 && minor >= 6) {
  984. CFRunLoopRef theRunLoop = NULL;
  985. AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
  986. OSStatus theError = AudioObjectSetPropertyData (kAudioObjectSystemObject, &theAddress, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop);
  987. if (theError != noErr) {
  988. jack_error("JackCoreAudioDriver::Open kAudioHardwarePropertyRunLoop error");
  989. }
  990. }
  991. if (SetupDevices(capture_driver_uid, playback_driver_uid, capture_driver_name, playback_driver_name) < 0)
  992. return -1;
  993. // Generic JackAudioDriver Open
  994. if (JackAudioDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency) != 0)
  995. return -1;
  996. if (SetupChannels(capturing, playing, inchannels, outchannels, in_nChannels, out_nChannels, true) < 0)
  997. return -1;
  998. if (SetupBufferSizeAndSampleRate(buffer_size, samplerate) < 0)
  999. return -1;
  1000. if (OpenAUHAL(capturing, playing, inchannels, outchannels, in_nChannels, out_nChannels, buffer_size, samplerate, true) < 0)
  1001. goto error;
  1002. if (capturing && inchannels > 0)
  1003. if (SetupBuffers(inchannels) < 0)
  1004. goto error;
  1005. if (AddListeners() < 0)
  1006. goto error;
  1007. // Core driver may have changed the in/out values
  1008. fCaptureChannels = inchannels;
  1009. fPlaybackChannels = outchannels;
  1010. return noErr;
  1011. error:
  1012. Close();
  1013. return -1;
  1014. }
  1015. int JackCoreAudioDriver::Close()
  1016. {
  1017. jack_log("JackCoreAudioDriver::Close");
  1018. Stop();
  1019. JackAudioDriver::Close();
  1020. RemoveListeners();
  1021. DisposeBuffers();
  1022. CloseAUHAL();
  1023. if (fPluginID > 0)
  1024. DestroyAggregateDevice();
  1025. return 0;
  1026. }
  1027. int JackCoreAudioDriver::Attach()
  1028. {
  1029. OSStatus err;
  1030. JackPort* port;
  1031. jack_port_id_t port_index;
  1032. UInt32 size;
  1033. Boolean isWritable;
  1034. char channel_name[64];
  1035. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  1036. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  1037. unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  1038. jack_log("JackCoreAudioDriver::Attach fBufferSize %ld fSampleRate %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  1039. for (int i = 0; i < fCaptureChannels; i++) {
  1040. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, &isWritable);
  1041. if (err != noErr)
  1042. jack_log("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error ");
  1043. if (err == noErr && size > 0) {
  1044. err = AudioDeviceGetProperty(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, channel_name);
  1045. if (err != noErr)
  1046. jack_log("AudioDeviceGetProperty kAudioDevicePropertyChannelName error ");
  1047. snprintf(alias, sizeof(alias) - 1, "%s:%s:out_%s%u", fAliasName, fCaptureDriverName, channel_name, i + 1);
  1048. } else {
  1049. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%u", fAliasName, fCaptureDriverName, i + 1);
  1050. }
  1051. snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl.fName, i + 1);
  1052. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  1053. jack_error("Cannot register port for %s", name);
  1054. return -1;
  1055. }
  1056. size = sizeof(UInt32);
  1057. UInt32 value1 = 0;
  1058. UInt32 value2 = 0;
  1059. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertyLatency, &size, &value1);
  1060. if (err != noErr)
  1061. jack_log("AudioDeviceGetProperty kAudioDevicePropertyLatency error ");
  1062. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertySafetyOffset, &size, &value2);
  1063. if (err != noErr)
  1064. jack_log("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error ");
  1065. port = fGraphManager->GetPort(port_index);
  1066. port->SetAlias(alias);
  1067. port->SetLatency(fEngineControl->fBufferSize + value1 + value2 + fCaptureLatency);
  1068. fCapturePortList[i] = port_index;
  1069. }
  1070. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  1071. for (int i = 0; i < fPlaybackChannels; i++) {
  1072. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, &isWritable);
  1073. if (err != noErr)
  1074. jack_log("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error ");
  1075. if (err == noErr && size > 0) {
  1076. err = AudioDeviceGetProperty(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, channel_name);
  1077. if (err != noErr)
  1078. jack_log("AudioDeviceGetProperty kAudioDevicePropertyChannelName error ");
  1079. snprintf(alias, sizeof(alias) - 1, "%s:%s:in_%s%u", fAliasName, fPlaybackDriverName, channel_name, i + 1);
  1080. } else {
  1081. snprintf(alias, sizeof(alias) - 1, "%s:%s:in%u", fAliasName, fPlaybackDriverName, i + 1);
  1082. }
  1083. snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl.fName, i + 1);
  1084. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  1085. jack_error("Cannot register port for %s", name);
  1086. return -1;
  1087. }
  1088. size = sizeof(UInt32);
  1089. UInt32 value1 = 0;
  1090. UInt32 value2 = 0;
  1091. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertyLatency, &size, &value1);
  1092. if (err != noErr)
  1093. jack_log("AudioDeviceGetProperty kAudioDevicePropertyLatency error ");
  1094. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertySafetyOffset, &size, &value2);
  1095. if (err != noErr)
  1096. jack_log("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error ");
  1097. port = fGraphManager->GetPort(port_index);
  1098. port->SetAlias(alias);
  1099. // Add more latency if "async" mode is used...
  1100. port->SetLatency(fEngineControl->fBufferSize + ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize * fIOUsage) + value1 + value2 + fPlaybackLatency);
  1101. fPlaybackPortList[i] = port_index;
  1102. // Monitor ports
  1103. if (fWithMonitorPorts) {
  1104. jack_log("Create monitor port ");
  1105. snprintf(name, sizeof(name) - 1, "%s:monitor_%u", fClientControl.fName, i + 1);
  1106. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize)) == NO_PORT) {
  1107. jack_error("Cannot register monitor port for %s", name);
  1108. return -1;
  1109. } else {
  1110. port = fGraphManager->GetPort(port_index);
  1111. port->SetAlias(alias);
  1112. port->SetLatency(fEngineControl->fBufferSize);
  1113. fMonitorPortList[i] = port_index;
  1114. }
  1115. }
  1116. }
  1117. // Input buffers do no change : prepare them only once
  1118. for (int i = 0; i < fCaptureChannels; i++) {
  1119. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  1120. }
  1121. return 0;
  1122. }
  1123. int JackCoreAudioDriver::Start()
  1124. {
  1125. jack_log("JackCoreAudioDriver::Start");
  1126. JackAudioDriver::Start();
  1127. /*
  1128. #ifdef MAC_OS_X_VERSION_10_5
  1129. OSStatus err = AudioDeviceCreateIOProcID(fDeviceID, MeasureCallback, this, &fMesureCallbackID);
  1130. #else
  1131. OSStatus err = AudioDeviceAddIOProc(fDeviceID, MeasureCallback, this);
  1132. #endif
  1133. */
  1134. OSStatus err = AudioDeviceAddIOProc(fDeviceID, MeasureCallback, this);
  1135. if (err != noErr)
  1136. return -1;
  1137. err = AudioOutputUnitStart(fAUHAL);
  1138. if (err != noErr)
  1139. return -1;
  1140. if ((err = AudioDeviceStart(fDeviceID, MeasureCallback)) != noErr) {
  1141. jack_error("Cannot start MeasureCallback");
  1142. printError(err);
  1143. return -1;
  1144. }
  1145. return 0;
  1146. }
  1147. int JackCoreAudioDriver::Stop()
  1148. {
  1149. jack_log("JackCoreAudioDriver::Stop");
  1150. AudioDeviceStop(fDeviceID, MeasureCallback);
  1151. /*
  1152. #ifdef MAC_OS_X_VERSION_10_5
  1153. AudioDeviceDestroyIOProcID(fDeviceID, fMesureCallbackID);
  1154. #else
  1155. AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
  1156. #endif
  1157. */
  1158. AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
  1159. return (AudioOutputUnitStop(fAUHAL) == noErr) ? 0 : -1;
  1160. }
  1161. int JackCoreAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  1162. {
  1163. OSStatus err;
  1164. UInt32 outSize = sizeof(UInt32);
  1165. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
  1166. if (err != noErr) {
  1167. jack_error("Cannot set buffer size %ld", buffer_size);
  1168. printError(err);
  1169. return -1;
  1170. }
  1171. JackAudioDriver::SetBufferSize(buffer_size); // never fails
  1172. // Input buffers do no change : prepare them only once
  1173. for (int i = 0; i < fCaptureChannels; i++) {
  1174. fJackInputData->mBuffers[i].mNumberChannels = 1;
  1175. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
  1176. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  1177. }
  1178. return 0;
  1179. }
  1180. bool JackCoreAudioDriver::TakeHogAux(AudioDeviceID deviceID, bool isInput)
  1181. {
  1182. pid_t hog_pid;
  1183. OSStatus err;
  1184. UInt32 propSize = sizeof(hog_pid);
  1185. err = AudioDeviceGetProperty(deviceID, 0, isInput, kAudioDevicePropertyHogMode, &propSize, &hog_pid);
  1186. if (err) {
  1187. jack_error("Cannot read hog state...");
  1188. printError(err);
  1189. }
  1190. if (hog_pid != getpid()) {
  1191. hog_pid = getpid();
  1192. err = AudioDeviceSetProperty(deviceID, 0, 0, isInput, kAudioDevicePropertyHogMode, propSize, &hog_pid);
  1193. if (err != noErr) {
  1194. jack_error("Can't hog device = %d because it's being hogged by another program", deviceID);
  1195. return false;
  1196. }
  1197. }
  1198. return true;
  1199. }
  1200. bool JackCoreAudioDriver::TakeHog()
  1201. {
  1202. OSStatus err = noErr;
  1203. AudioObjectID sub_device[32];
  1204. UInt32 outSize = sizeof(sub_device);
  1205. err = AudioDeviceGetProperty(fDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device);
  1206. if (err != noErr) {
  1207. jack_log("Device does not have subdevices");
  1208. return TakeHogAux(fDeviceID, true);
  1209. } else {
  1210. int num_devices = outSize / sizeof(AudioObjectID);
  1211. jack_log("Device does has %d subdevices", num_devices);
  1212. for (int i = 0; i < num_devices; i++) {
  1213. if (!TakeHogAux(sub_device[i], true)) {
  1214. return false;
  1215. }
  1216. }
  1217. return true;
  1218. }
  1219. }
  1220. } // end of namespace
  1221. #ifdef __cplusplus
  1222. extern "C"
  1223. {
  1224. #endif
  1225. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  1226. {
  1227. jack_driver_desc_t *desc;
  1228. unsigned int i;
  1229. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  1230. strcpy(desc->name, "coreaudio"); // size MUST be less then JACK_DRIVER_NAME_MAX + 1
  1231. strcpy(desc->desc, "Apple CoreAudio API based audio backend"); // size MUST be less then JACK_DRIVER_PARAM_DESC + 1
  1232. desc->nparams = 16;
  1233. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  1234. i = 0;
  1235. strcpy(desc->params[i].name, "channels");
  1236. desc->params[i].character = 'c';
  1237. desc->params[i].type = JackDriverParamInt;
  1238. desc->params[i].value.ui = 0;
  1239. strcpy(desc->params[i].short_desc, "Maximum number of channels");
  1240. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1241. i++;
  1242. strcpy(desc->params[i].name, "inchannels");
  1243. desc->params[i].character = 'i';
  1244. desc->params[i].type = JackDriverParamInt;
  1245. desc->params[i].value.ui = 0;
  1246. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  1247. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1248. i++;
  1249. strcpy(desc->params[i].name, "outchannels");
  1250. desc->params[i].character = 'o';
  1251. desc->params[i].type = JackDriverParamInt;
  1252. desc->params[i].value.ui = 0;
  1253. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  1254. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1255. i++;
  1256. strcpy(desc->params[i].name, "capture");
  1257. desc->params[i].character = 'C';
  1258. desc->params[i].type = JackDriverParamString;
  1259. strcpy(desc->params[i].value.str, "will take default CoreAudio input device");
  1260. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set CoreAudio device name");
  1261. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1262. i++;
  1263. strcpy(desc->params[i].name, "playback");
  1264. desc->params[i].character = 'P';
  1265. desc->params[i].type = JackDriverParamString;
  1266. strcpy(desc->params[i].value.str, "will take default CoreAudio output device");
  1267. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set CoreAudio device name");
  1268. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1269. i++;
  1270. strcpy (desc->params[i].name, "monitor");
  1271. desc->params[i].character = 'm';
  1272. desc->params[i].type = JackDriverParamBool;
  1273. desc->params[i].value.i = 0;
  1274. strcpy(desc->params[i].short_desc, "Provide monitor ports for the output");
  1275. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1276. i++;
  1277. strcpy(desc->params[i].name, "duplex");
  1278. desc->params[i].character = 'D';
  1279. desc->params[i].type = JackDriverParamBool;
  1280. desc->params[i].value.i = TRUE;
  1281. strcpy(desc->params[i].short_desc, "Provide both capture and playback ports");
  1282. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1283. i++;
  1284. strcpy(desc->params[i].name, "rate");
  1285. desc->params[i].character = 'r';
  1286. desc->params[i].type = JackDriverParamUInt;
  1287. desc->params[i].value.ui = 44100U;
  1288. strcpy(desc->params[i].short_desc, "Sample rate");
  1289. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1290. i++;
  1291. strcpy(desc->params[i].name, "period");
  1292. desc->params[i].character = 'p';
  1293. desc->params[i].type = JackDriverParamUInt;
  1294. desc->params[i].value.ui = 128U;
  1295. strcpy(desc->params[i].short_desc, "Frames per period");
  1296. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1297. i++;
  1298. strcpy(desc->params[i].name, "device");
  1299. desc->params[i].character = 'd';
  1300. desc->params[i].type = JackDriverParamString;
  1301. strcpy(desc->params[i].value.str, "will take default CoreAudio device name");
  1302. strcpy(desc->params[i].short_desc, "CoreAudio device name");
  1303. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1304. i++;
  1305. strcpy(desc->params[i].name, "input-latency");
  1306. desc->params[i].character = 'I';
  1307. desc->params[i].type = JackDriverParamUInt;
  1308. desc->params[i].value.i = 0;
  1309. strcpy(desc->params[i].short_desc, "Extra input latency (frames)");
  1310. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1311. i++;
  1312. strcpy(desc->params[i].name, "output-latency");
  1313. desc->params[i].character = 'O';
  1314. desc->params[i].type = JackDriverParamUInt;
  1315. desc->params[i].value.i = 0;
  1316. strcpy(desc->params[i].short_desc, "Extra output latency (frames)");
  1317. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1318. i++;
  1319. strcpy(desc->params[i].name, "list-devices");
  1320. desc->params[i].character = 'l';
  1321. desc->params[i].type = JackDriverParamBool;
  1322. desc->params[i].value.i = FALSE;
  1323. strcpy(desc->params[i].short_desc, "Display available CoreAudio devices");
  1324. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1325. i++;
  1326. strcpy(desc->params[i].name, "hog");
  1327. desc->params[i].character = 'H';
  1328. desc->params[i].type = JackDriverParamBool;
  1329. desc->params[i].value.i = FALSE;
  1330. strcpy(desc->params[i].short_desc, "Take exclusive access of the audio device");
  1331. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1332. i++;
  1333. strcpy(desc->params[i].name, "async-latency");
  1334. desc->params[i].character = 'L';
  1335. desc->params[i].type = JackDriverParamUInt;
  1336. desc->params[i].value.i = 100;
  1337. strcpy(desc->params[i].short_desc, "Extra output latency in asynchronous mode (percent)");
  1338. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1339. i++;
  1340. strcpy(desc->params[i].name, "grain");
  1341. desc->params[i].character = 'G';
  1342. desc->params[i].type = JackDriverParamUInt;
  1343. desc->params[i].value.i = 100;
  1344. strcpy(desc->params[i].short_desc, "Computation grain in RT thread (percent)");
  1345. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1346. return desc;
  1347. }
  1348. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  1349. {
  1350. jack_nframes_t srate = 44100;
  1351. jack_nframes_t frames_per_interrupt = 128;
  1352. int capture = FALSE;
  1353. int playback = FALSE;
  1354. int chan_in = 0;
  1355. int chan_out = 0;
  1356. bool monitor = false;
  1357. const char* capture_driver_uid = "";
  1358. const char* playback_driver_uid = "";
  1359. const JSList *node;
  1360. const jack_driver_param_t *param;
  1361. jack_nframes_t systemic_input_latency = 0;
  1362. jack_nframes_t systemic_output_latency = 0;
  1363. int async_output_latency = 100;
  1364. int computation_grain = -1;
  1365. bool hogged = false;
  1366. for (node = params; node; node = jack_slist_next(node)) {
  1367. param = (const jack_driver_param_t *) node->data;
  1368. switch (param->character) {
  1369. case 'd':
  1370. capture_driver_uid = strdup(param->value.str);
  1371. playback_driver_uid = strdup(param->value.str);
  1372. break;
  1373. case 'D':
  1374. capture = TRUE;
  1375. playback = TRUE;
  1376. break;
  1377. case 'c':
  1378. chan_in = chan_out = (int) param->value.ui;
  1379. break;
  1380. case 'i':
  1381. chan_in = (int) param->value.ui;
  1382. break;
  1383. case 'o':
  1384. chan_out = (int) param->value.ui;
  1385. break;
  1386. case 'C':
  1387. capture = TRUE;
  1388. if (strcmp(param->value.str, "none") != 0) {
  1389. capture_driver_uid = strdup(param->value.str);
  1390. }
  1391. break;
  1392. case 'P':
  1393. playback = TRUE;
  1394. if (strcmp(param->value.str, "none") != 0) {
  1395. playback_driver_uid = strdup(param->value.str);
  1396. }
  1397. break;
  1398. case 'm':
  1399. monitor = param->value.i;
  1400. break;
  1401. case 'r':
  1402. srate = param->value.ui;
  1403. break;
  1404. case 'p':
  1405. frames_per_interrupt = (unsigned int) param->value.ui;
  1406. break;
  1407. case 'I':
  1408. systemic_input_latency = param->value.ui;
  1409. break;
  1410. case 'O':
  1411. systemic_output_latency = param->value.ui;
  1412. break;
  1413. case 'l':
  1414. Jack::DisplayDeviceNames();
  1415. break;
  1416. case 'H':
  1417. hogged = true;
  1418. break;
  1419. case 'L':
  1420. async_output_latency = param->value.ui;
  1421. break;
  1422. case 'G':
  1423. computation_grain = param->value.ui;
  1424. break;
  1425. }
  1426. }
  1427. /* duplex is the default */
  1428. if (!capture && !playback) {
  1429. capture = TRUE;
  1430. playback = TRUE;
  1431. }
  1432. Jack::JackCoreAudioDriver* driver = new Jack::JackCoreAudioDriver("system", "coreaudio", engine, table);
  1433. if (driver->Open(frames_per_interrupt, srate, capture, playback, chan_in, chan_out, monitor, capture_driver_uid,
  1434. playback_driver_uid, systemic_input_latency, systemic_output_latency, async_output_latency, computation_grain, hogged) == 0) {
  1435. return driver;
  1436. } else {
  1437. delete driver;
  1438. return NULL;
  1439. }
  1440. }
  1441. #ifdef __cplusplus
  1442. }
  1443. #endif