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.

1180 lines
44KB

  1. /*
  2. Copyright (C) 2004-2006 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 "driver_interface.h"
  24. #include <iostream>
  25. namespace Jack
  26. {
  27. static void PrintStreamDesc(AudioStreamBasicDescription *inDesc)
  28. {
  29. JackLog("- - - - - - - - - - - - - - - - - - - -\n");
  30. JackLog(" Sample Rate:%f\n", inDesc->mSampleRate);
  31. JackLog(" Format ID:%.*s\n", (int) sizeof(inDesc->mFormatID), (char*)&inDesc->mFormatID);
  32. JackLog(" Format Flags:%lX\n", inDesc->mFormatFlags);
  33. JackLog(" Bytes per Packet:%ld\n", inDesc->mBytesPerPacket);
  34. JackLog(" Frames per Packet:%ld\n", inDesc->mFramesPerPacket);
  35. JackLog(" Bytes per Frame:%ld\n", inDesc->mBytesPerFrame);
  36. JackLog(" Channels per Frame:%ld\n", inDesc->mChannelsPerFrame);
  37. JackLog(" Bits per Channel:%ld\n", inDesc->mBitsPerChannel);
  38. JackLog("- - - - - - - - - - - - - - - - - - - -\n");
  39. }
  40. static void printError(OSStatus err)
  41. {
  42. switch (err) {
  43. case kAudioHardwareNoError:
  44. JackLog("error code : kAudioHardwareNoError\n");
  45. break;
  46. case kAudioConverterErr_FormatNotSupported:
  47. JackLog("error code : kAudioConverterErr_FormatNotSupported\n");
  48. break;
  49. case kAudioConverterErr_OperationNotSupported:
  50. JackLog("error code : kAudioConverterErr_OperationNotSupported\n");
  51. break;
  52. case kAudioConverterErr_PropertyNotSupported:
  53. JackLog("error code : kAudioConverterErr_PropertyNotSupported\n");
  54. break;
  55. case kAudioConverterErr_InvalidInputSize:
  56. JackLog("error code : kAudioConverterErr_InvalidInputSize\n");
  57. break;
  58. case kAudioConverterErr_InvalidOutputSize:
  59. JackLog("error code : kAudioConverterErr_InvalidOutputSize\n");
  60. break;
  61. case kAudioConverterErr_UnspecifiedError:
  62. JackLog("error code : kAudioConverterErr_UnspecifiedError\n");
  63. break;
  64. case kAudioConverterErr_BadPropertySizeError:
  65. JackLog("error code : kAudioConverterErr_BadPropertySizeError\n");
  66. break;
  67. case kAudioConverterErr_RequiresPacketDescriptionsError:
  68. JackLog("error code : kAudioConverterErr_RequiresPacketDescriptionsError\n");
  69. break;
  70. case kAudioConverterErr_InputSampleRateOutOfRange:
  71. JackLog("error code : kAudioConverterErr_InputSampleRateOutOfRange\n");
  72. break;
  73. case kAudioConverterErr_OutputSampleRateOutOfRange:
  74. JackLog("error code : kAudioConverterErr_OutputSampleRateOutOfRange\n");
  75. break;
  76. case kAudioHardwareNotRunningError:
  77. JackLog("error code : kAudioHardwareNotRunningError\n");
  78. break;
  79. case kAudioHardwareUnknownPropertyError:
  80. JackLog("error code : kAudioHardwareUnknownPropertyError\n");
  81. break;
  82. case kAudioHardwareIllegalOperationError:
  83. JackLog("error code : kAudioHardwareIllegalOperationError\n");
  84. break;
  85. case kAudioHardwareBadDeviceError:
  86. JackLog("error code : kAudioHardwareBadDeviceError\n");
  87. break;
  88. case kAudioHardwareBadStreamError:
  89. JackLog("error code : kAudioHardwareBadStreamError\n");
  90. break;
  91. case kAudioDeviceUnsupportedFormatError:
  92. JackLog("error code : kAudioDeviceUnsupportedFormatError\n");
  93. break;
  94. case kAudioDevicePermissionsError:
  95. JackLog("error code : kAudioDevicePermissionsError\n");
  96. break;
  97. default:
  98. JackLog("error code : unknown\n");
  99. break;
  100. }
  101. }
  102. static OSStatus DisplayDeviceNames()
  103. {
  104. UInt32 size;
  105. Boolean isWritable;
  106. int i, deviceNum;
  107. OSStatus err;
  108. CFStringRef UIname;
  109. err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
  110. if (err != noErr)
  111. return err;
  112. deviceNum = size / sizeof(AudioDeviceID);
  113. AudioDeviceID devices[deviceNum];
  114. err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
  115. if (err != noErr)
  116. return err;
  117. for (i = 0; i < deviceNum; i++) {
  118. char device_name[256];
  119. char internal_name[256];
  120. size = sizeof(CFStringRef);
  121. UIname = NULL;
  122. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
  123. if (err == noErr) {
  124. CFStringGetCString(UIname, internal_name, 256, CFStringGetSystemEncoding());
  125. } else {
  126. goto error;
  127. }
  128. size = 256;
  129. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &size, device_name);
  130. if (err != noErr)
  131. return err;
  132. printf("Device name = \'%s\', internal_name = \'%s\' (to be used as -C, -P, or -d parameter)\n", device_name, internal_name);
  133. }
  134. return noErr;
  135. error:
  136. if (UIname != NULL)
  137. CFRelease(UIname);
  138. return err;
  139. }
  140. #if IO_CPU
  141. OSStatus JackCoreAudioDriver::Render(void *inRefCon,
  142. AudioUnitRenderActionFlags *ioActionFlags,
  143. const AudioTimeStamp *inTimeStamp,
  144. UInt32 inBusNumber,
  145. UInt32 inNumberFrames,
  146. AudioBufferList *ioData)
  147. {
  148. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inRefCon;
  149. driver->fLastWaitUst = GetMicroSeconds(); // Take callback date here
  150. memcpy(&driver->fActionFags, ioActionFlags, sizeof(AudioUnitRenderActionFlags));
  151. memcpy(&driver->fCurrentTime, inTimeStamp, sizeof(AudioTimeStamp));
  152. driver->fDriverOutputData = ioData;
  153. float delta;
  154. OSStatus res = driver->Process();
  155. if ((delta = float(GetMicroSeconds() - driver->fLastWaitUst) / float(driver->fEngineControl->fPeriodUsecs)) > 0.1) {
  156. JackLog("IO CPU %f\n", delta);
  157. }
  158. return res;
  159. }
  160. #else
  161. OSStatus JackCoreAudioDriver::Render(void *inRefCon,
  162. AudioUnitRenderActionFlags *ioActionFlags,
  163. const AudioTimeStamp *inTimeStamp,
  164. UInt32 inBusNumber,
  165. UInt32 inNumberFrames,
  166. AudioBufferList *ioData)
  167. {
  168. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inRefCon;
  169. driver->fLastWaitUst = GetMicroSeconds(); // Take callback date here
  170. memcpy(&driver->fActionFags, ioActionFlags, sizeof(AudioUnitRenderActionFlags));
  171. memcpy(&driver->fCurrentTime, inTimeStamp, sizeof(AudioTimeStamp));
  172. driver->fDriverOutputData = ioData;
  173. return driver->Process();
  174. }
  175. #endif
  176. int JackCoreAudioDriver::Read()
  177. {
  178. AudioUnitRender(fAUHAL, &fActionFags, &fCurrentTime, 1, fEngineControl->fBufferSize, fJackInputData);
  179. return 0;
  180. }
  181. int JackCoreAudioDriver::Write()
  182. {
  183. for (int i = 0; i < fPlaybackChannels; i++) {
  184. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  185. float* buffer = GetOutputBuffer(i);
  186. int size = sizeof(float) * fEngineControl->fBufferSize;
  187. memcpy((float*)fDriverOutputData->mBuffers[i].mData, buffer, size);
  188. // Monitor ports
  189. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
  190. memcpy(GetMonitorBuffer(i), buffer, size);
  191. } else {
  192. memset((float*)fDriverOutputData->mBuffers[i].mData, 0, sizeof(float) * fEngineControl->fBufferSize);
  193. }
  194. }
  195. return 0;
  196. }
  197. // Will run only once
  198. OSStatus JackCoreAudioDriver::MeasureCallback(AudioDeviceID inDevice,
  199. const AudioTimeStamp* inNow,
  200. const AudioBufferList* inInputData,
  201. const AudioTimeStamp* inInputTime,
  202. AudioBufferList* outOutputData,
  203. const AudioTimeStamp* inOutputTime,
  204. void* inClientData)
  205. {
  206. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
  207. AudioDeviceStop(driver->fDeviceID, MeasureCallback);
  208. AudioDeviceRemoveIOProc(driver->fDeviceID, MeasureCallback);
  209. JackLog("JackCoreAudioDriver::MeasureCallback called\n");
  210. JackMachThread::GetParams(&driver->fEngineControl->fPeriod, &driver->fEngineControl->fComputation, &driver->fEngineControl->fConstraint);
  211. return noErr;
  212. }
  213. OSStatus JackCoreAudioDriver::DeviceNotificationCallback(AudioDeviceID inDevice,
  214. UInt32 inChannel,
  215. Boolean isInput,
  216. AudioDevicePropertyID inPropertyID,
  217. void* inClientData)
  218. {
  219. JackCoreAudioDriver* driver = (JackCoreAudioDriver*)inClientData;
  220. if (inPropertyID == kAudioDeviceProcessorOverload) {
  221. JackLog("JackCoreAudioDriver::NotificationCallback kAudioDeviceProcessorOverload\n");
  222. #ifdef DEBUG
  223. //driver->fLogFile->Capture(AudioGetCurrentHostTime() - AudioConvertNanosToHostTime(LOG_SAMPLE_DURATION * 1000000), AudioGetCurrentHostTime(), true, "Captured Latency Log for I/O Cycle Overload\n");
  224. #endif
  225. driver->NotifyXRun(GetMicroSeconds());
  226. }
  227. return noErr;
  228. }
  229. OSStatus JackCoreAudioDriver::GetDeviceIDFromUID(const char* UID, AudioDeviceID* id)
  230. {
  231. UInt32 size = sizeof(AudioValueTranslation);
  232. CFStringRef inIUD = CFStringCreateWithCString(NULL, UID, CFStringGetSystemEncoding());
  233. AudioValueTranslation value = { &inIUD, sizeof(CFStringRef), id, sizeof(AudioDeviceID) };
  234. if (inIUD == NULL) {
  235. return kAudioHardwareUnspecifiedError;
  236. } else {
  237. OSStatus res = AudioHardwareGetProperty(kAudioHardwarePropertyDeviceForUID, &size, &value);
  238. CFRelease(inIUD);
  239. JackLog("get_device_id_from_uid %s %ld \n", UID, *id);
  240. return (*id == kAudioDeviceUnknown) ? kAudioHardwareBadDeviceError : res;
  241. }
  242. }
  243. OSStatus JackCoreAudioDriver::GetDefaultDevice(AudioDeviceID* id)
  244. {
  245. OSStatus res;
  246. UInt32 theSize = sizeof(UInt32);
  247. AudioDeviceID inDefault;
  248. AudioDeviceID outDefault;
  249. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr)
  250. return res;
  251. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr)
  252. return res;
  253. JackLog("GetDefaultDevice: input = %ld output = %ld\n", inDefault, outDefault);
  254. // Get the device only if default input and ouput are the same
  255. if (inDefault == outDefault) {
  256. *id = inDefault;
  257. return noErr;
  258. } else {
  259. jack_error("Default input and output devices are not the same !!");
  260. return kAudioHardwareBadDeviceError;
  261. }
  262. }
  263. OSStatus JackCoreAudioDriver::GetDefaultInputDevice(AudioDeviceID* id)
  264. {
  265. OSStatus res;
  266. UInt32 theSize = sizeof(UInt32);
  267. AudioDeviceID inDefault;
  268. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr)
  269. return res;
  270. JackLog("GetDefaultInputDevice: input = %ld \n", inDefault);
  271. *id = inDefault;
  272. return noErr;
  273. }
  274. OSStatus JackCoreAudioDriver::GetDefaultOutputDevice(AudioDeviceID* id)
  275. {
  276. OSStatus res;
  277. UInt32 theSize = sizeof(UInt32);
  278. AudioDeviceID outDefault;
  279. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr)
  280. return res;
  281. JackLog("GetDefaultOutputDevice: output = %ld\n", outDefault);
  282. *id = outDefault;
  283. return noErr;
  284. }
  285. OSStatus JackCoreAudioDriver::GetDeviceNameFromID(AudioDeviceID id, char* name)
  286. {
  287. UInt32 size = 256;
  288. return AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceName, &size, name);
  289. }
  290. OSStatus JackCoreAudioDriver::GetTotalChannels(AudioDeviceID device, long* channelCount, bool isInput)
  291. {
  292. OSStatus err = noErr;
  293. UInt32 outSize;
  294. Boolean outWritable;
  295. AudioBufferList* bufferList = 0;
  296. AudioStreamID* streamList = 0;
  297. int i, numStream;
  298. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreams, &outSize, &outWritable);
  299. if (err == noErr) {
  300. streamList = (AudioStreamID*)malloc(outSize);
  301. numStream = outSize / sizeof(AudioStreamID);
  302. JackLog("GetTotalChannels device stream number = %ld numStream = %ld\n", device, numStream);
  303. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreams, &outSize, streamList);
  304. if (err == noErr) {
  305. AudioStreamBasicDescription streamDesc;
  306. outSize = sizeof(AudioStreamBasicDescription);
  307. for (i = 0; i < numStream; i++) {
  308. err = AudioStreamGetProperty(streamList[i], 0, kAudioDevicePropertyStreamFormat, &outSize, &streamDesc);
  309. JackLog("GetTotalChannels streamDesc mFormatFlags = %ld mChannelsPerFrame = %ld\n", streamDesc.mFormatFlags, streamDesc.mChannelsPerFrame);
  310. }
  311. }
  312. }
  313. *channelCount = 0;
  314. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
  315. if (err == noErr) {
  316. bufferList = (AudioBufferList*)malloc(outSize);
  317. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
  318. if (err == noErr) {
  319. for (i = 0; i < bufferList->mNumberBuffers; i++)
  320. *channelCount += bufferList->mBuffers[i].mNumberChannels;
  321. }
  322. }
  323. if (streamList)
  324. free(streamList);
  325. if (bufferList)
  326. free(bufferList);
  327. return err;
  328. }
  329. JackCoreAudioDriver::JackCoreAudioDriver(const char* name, JackEngine* engine, JackSynchro** table)
  330. : JackAudioDriver(name, engine, table), fJackInputData(NULL), fDriverOutputData(NULL)
  331. {
  332. #ifdef DEBUG
  333. //fLogFile = new CALatencyLog("jackmp_latency", ".txt");
  334. #endif
  335. }
  336. JackCoreAudioDriver::~JackCoreAudioDriver()
  337. {
  338. #ifdef DEBUG
  339. //delete fLogFile;
  340. #endif
  341. }
  342. int JackCoreAudioDriver::Open(jack_nframes_t nframes,
  343. jack_nframes_t samplerate,
  344. int capturing,
  345. int playing,
  346. int inchannels,
  347. int outchannels,
  348. bool monitor,
  349. const char* capture_driver_uid,
  350. const char* playback_driver_uid,
  351. jack_nframes_t capture_latency,
  352. jack_nframes_t playback_latency)
  353. {
  354. OSStatus err = noErr;
  355. ComponentResult err1;
  356. UInt32 outSize;
  357. UInt32 enableIO;
  358. AudioStreamBasicDescription srcFormat, dstFormat, sampleRate;
  359. long in_nChannels = 0;
  360. long out_nChannels = 0;
  361. char capture_driver_name[256];
  362. char playback_driver_name[256];
  363. float iousage;
  364. capture_driver_name[0] = 0;
  365. playback_driver_name[0] = 0;
  366. JackLog("JackCoreAudioDriver::Open nframes = %ld in = %ld out = %ld capture name = %s playback name = %s\n",
  367. nframes, inchannels, outchannels, capture_driver_uid, playback_driver_uid);
  368. // Duplex
  369. if (capture_driver_uid != NULL && playback_driver_uid != NULL) {
  370. JackLog("JackCoreAudioDriver::Open duplex \n");
  371. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  372. if (GetDefaultDevice(&fDeviceID) != noErr) {
  373. jack_error("Cannot open default device");
  374. return -1;
  375. }
  376. }
  377. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  378. jack_error("Cannot get device name from device ID");
  379. return -1;
  380. }
  381. // Capture only
  382. } else if (capture_driver_uid != NULL) {
  383. JackLog("JackCoreAudioDriver::Open capture only \n");
  384. if (GetDeviceIDFromUID(capture_driver_uid, &fDeviceID) != noErr) {
  385. if (GetDefaultInputDevice(&fDeviceID) != noErr) {
  386. jack_error("Cannot open default device");
  387. return -1;
  388. }
  389. }
  390. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr) {
  391. jack_error("Cannot get device name from device ID");
  392. return -1;
  393. }
  394. // Playback only
  395. } else if (playback_driver_uid != NULL) {
  396. JackLog("JackCoreAudioDriver::Open playback only \n");
  397. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  398. if (GetDefaultOutputDevice(&fDeviceID) != noErr) {
  399. jack_error("Cannot open default device");
  400. return -1;
  401. }
  402. }
  403. if (GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  404. jack_error("Cannot get device name from device ID");
  405. return -1;
  406. }
  407. // Use default driver in duplex mode
  408. } else {
  409. JackLog("JackCoreAudioDriver::Open default driver \n");
  410. if (GetDefaultDevice(&fDeviceID) != noErr) {
  411. jack_error("Cannot open default device");
  412. return -1;
  413. }
  414. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  415. jack_error("Cannot get device name from device ID");
  416. return -1;
  417. }
  418. }
  419. // Generic JackAudioDriver Open
  420. if (JackAudioDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency) != 0) {
  421. return -1;
  422. }
  423. if (capturing) {
  424. err = GetTotalChannels(fDeviceID, &in_nChannels, true);
  425. if (err != noErr) {
  426. jack_error("Cannot get input channel number");
  427. printError(err);
  428. return -1;
  429. }
  430. }
  431. if (playing) {
  432. err = GetTotalChannels(fDeviceID, &out_nChannels, false);
  433. if (err != noErr) {
  434. jack_error("Cannot get output channel number");
  435. printError(err);
  436. return -1;
  437. }
  438. }
  439. if (inchannels > in_nChannels) {
  440. jack_error("This device hasn't required input channels inchannels = %ld in_nChannels = %ld", inchannels, in_nChannels);
  441. return -1;
  442. }
  443. if (outchannels > out_nChannels) {
  444. jack_error("This device hasn't required output channels outchannels = %ld out_nChannels = %ld", outchannels, out_nChannels);
  445. return -1;
  446. }
  447. if (inchannels == 0) {
  448. JackLog("Setup max in channels = %ld\n", in_nChannels);
  449. inchannels = in_nChannels;
  450. }
  451. if (outchannels == 0) {
  452. JackLog("Setup max out channels = %ld\n", out_nChannels);
  453. outchannels = out_nChannels;
  454. }
  455. // Setting buffer size
  456. outSize = sizeof(UInt32);
  457. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &fEngineControl->fBufferSize);
  458. if (err != noErr) {
  459. jack_error("Cannot set buffer size %ld", nframes);
  460. printError(err);
  461. return -1;
  462. }
  463. // Set sample rate
  464. if (capturing && inchannels > 0) {
  465. outSize = sizeof(AudioStreamBasicDescription);
  466. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertyStreamFormat, &outSize, &sampleRate);
  467. if (err != noErr) {
  468. jack_error("Cannot get current sample rate");
  469. printError(err);
  470. return -1;
  471. }
  472. if (samplerate != (unsigned long)sampleRate.mSampleRate) {
  473. sampleRate.mSampleRate = (Float64)samplerate;
  474. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, true, kAudioDevicePropertyStreamFormat, outSize, &sampleRate);
  475. if (err != noErr) {
  476. jack_error("Cannot set sample rate = %ld", samplerate);
  477. printError(err);
  478. return -1;
  479. }
  480. }
  481. }
  482. if (playing && outchannels > 0) {
  483. outSize = sizeof(AudioStreamBasicDescription);
  484. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertyStreamFormat, &outSize, &sampleRate);
  485. if (err != noErr) {
  486. jack_error("Cannot get current sample rate");
  487. printError(err);
  488. return -1;
  489. }
  490. if (samplerate != (unsigned long)sampleRate.mSampleRate) {
  491. sampleRate.mSampleRate = (Float64)samplerate;
  492. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyStreamFormat, outSize, &sampleRate);
  493. if (err != noErr) {
  494. jack_error("Cannot set sample rate = %ld", samplerate);
  495. printError(err);
  496. return -1;
  497. }
  498. }
  499. }
  500. // AUHAL
  501. ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  502. Component HALOutput = FindNextComponent(NULL, &cd);
  503. err1 = OpenAComponent(HALOutput, &fAUHAL);
  504. if (err1 != noErr) {
  505. jack_error("Error calling OpenAComponent");
  506. printError(err1);
  507. goto error;
  508. }
  509. err1 = AudioUnitInitialize(fAUHAL);
  510. if (err1 != noErr) {
  511. jack_error("Cannot initialize AUHAL unit");
  512. printError(err1);
  513. goto error;
  514. }
  515. // Start I/O
  516. enableIO = 1;
  517. if (capturing && inchannels > 0) {
  518. JackLog("Setup AUHAL input\n");
  519. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
  520. if (err1 != noErr) {
  521. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
  522. printError(err1);
  523. goto error;
  524. }
  525. }
  526. if (playing && outchannels > 0) {
  527. JackLog("Setup AUHAL output\n");
  528. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
  529. if (err1 != noErr) {
  530. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
  531. printError(err1);
  532. goto error;
  533. }
  534. }
  535. // Setup up choosen device, in both input and output cases
  536. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
  537. if (err1 != noErr) {
  538. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
  539. printError(err1);
  540. goto error;
  541. }
  542. // Set buffer size
  543. if (capturing && inchannels > 0) {
  544. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*) & nframes, sizeof(UInt32));
  545. if (err1 != noErr) {
  546. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  547. printError(err1);
  548. goto error;
  549. }
  550. }
  551. if (playing && outchannels > 0) {
  552. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*) & nframes, sizeof(UInt32));
  553. if (err1 != noErr) {
  554. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  555. printError(err1);
  556. goto error;
  557. }
  558. }
  559. // Setup channel map
  560. if (capturing && inchannels > 0 && inchannels < in_nChannels) {
  561. SInt32 chanArr[in_nChannels];
  562. for (int i = 0; i < in_nChannels; i++) {
  563. chanArr[i] = -1;
  564. }
  565. for (int i = 0; i < inchannels; i++) {
  566. chanArr[i] = i;
  567. }
  568. AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
  569. if (err1 != noErr) {
  570. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
  571. printError(err1);
  572. }
  573. }
  574. if (playing && outchannels > 0 && outchannels < out_nChannels) {
  575. SInt32 chanArr[out_nChannels];
  576. for (int i = 0; i < out_nChannels; i++) {
  577. chanArr[i] = -1;
  578. }
  579. for (int i = 0; i < outchannels; i++) {
  580. chanArr[i] = i;
  581. }
  582. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
  583. if (err1 != noErr) {
  584. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
  585. printError(err1);
  586. }
  587. }
  588. // Setup stream converters
  589. if (capturing && inchannels > 0) {
  590. srcFormat.mSampleRate = samplerate;
  591. srcFormat.mFormatID = kAudioFormatLinearPCM;
  592. srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  593. srcFormat.mBytesPerPacket = sizeof(float);
  594. srcFormat.mFramesPerPacket = 1;
  595. srcFormat.mBytesPerFrame = sizeof(float);
  596. srcFormat.mChannelsPerFrame = outchannels;
  597. srcFormat.mBitsPerChannel = 32;
  598. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, sizeof(AudioStreamBasicDescription));
  599. if (err1 != noErr) {
  600. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  601. printError(err1);
  602. }
  603. }
  604. if (playing && outchannels > 0) {
  605. dstFormat.mSampleRate = samplerate;
  606. dstFormat.mFormatID = kAudioFormatLinearPCM;
  607. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  608. dstFormat.mBytesPerPacket = sizeof(float);
  609. dstFormat.mFramesPerPacket = 1;
  610. dstFormat.mBytesPerFrame = sizeof(float);
  611. dstFormat.mChannelsPerFrame = inchannels;
  612. dstFormat.mBitsPerChannel = 32;
  613. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, sizeof(AudioStreamBasicDescription));
  614. if (err1 != noErr) {
  615. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  616. printError(err1);
  617. }
  618. }
  619. // Setup callbacks
  620. if (inchannels > 0 && outchannels == 0) {
  621. AURenderCallbackStruct output;
  622. output.inputProc = Render;
  623. output.inputProcRefCon = this;
  624. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  625. if (err1 != noErr) {
  626. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  627. printError(err1);
  628. goto error;
  629. }
  630. } else {
  631. AURenderCallbackStruct output;
  632. output.inputProc = Render;
  633. output.inputProcRefCon = this;
  634. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  635. if (err1 != noErr) {
  636. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  637. printError(err1);
  638. goto error;
  639. }
  640. }
  641. // Prepare buffers
  642. if (capturing && inchannels > 0) {
  643. fJackInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
  644. if (fJackInputData == 0) {
  645. jack_error("Cannot allocate memory for input buffers");
  646. goto error;
  647. }
  648. fJackInputData->mNumberBuffers = inchannels;
  649. for (int i = 0; i < fCaptureChannels; i++) {
  650. fJackInputData->mBuffers[i].mNumberChannels = 1;
  651. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
  652. }
  653. }
  654. // Add listeners
  655. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback, this);
  656. if (err != noErr) {
  657. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
  658. printError(err1);
  659. goto error;
  660. }
  661. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback, this);
  662. if (err != noErr) {
  663. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioHardwarePropertyDevices");
  664. printError(err1);
  665. goto error;
  666. }
  667. fDriverOutputData = 0;
  668. #if IO_CPU
  669. outSize = sizeof(float);
  670. iousage = 0.4f;
  671. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyIOCycleUsage, outSize, &iousage);
  672. if (err != noErr) {
  673. jack_error("Error calling AudioDeviceSetProperty kAudioDevicePropertyIOCycleUsage");
  674. printError(err);
  675. }
  676. #endif
  677. // Core driver may have changed the in/out values
  678. fCaptureChannels = inchannels;
  679. fPlaybackChannels = outchannels;
  680. return 0;
  681. error:
  682. AudioUnitUninitialize(fAUHAL);
  683. CloseComponent(fAUHAL);
  684. return -1;
  685. }
  686. int JackCoreAudioDriver::Close()
  687. {
  688. JackLog("JackCoreAudioDriver::Close\n");
  689. JackAudioDriver::Close();
  690. // Possibly (if MeasureCallback has not been called)
  691. AudioDeviceStop(fDeviceID, MeasureCallback);
  692. AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
  693. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback);
  694. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback);
  695. free(fJackInputData);
  696. AudioUnitUninitialize(fAUHAL);
  697. CloseComponent(fAUHAL);
  698. return 0;
  699. }
  700. int JackCoreAudioDriver::Attach()
  701. {
  702. OSStatus err;
  703. JackPort* port;
  704. jack_port_id_t port_index;
  705. UInt32 size;
  706. Boolean isWritable;
  707. char channel_name[64];
  708. char buf[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  709. unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  710. JackLog("JackCoreAudioDriver::Attach fBufferSize %ld fSampleRate %ld\n", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  711. for (int i = 0; i < fCaptureChannels; i++) {
  712. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, &isWritable);
  713. if (err != noErr)
  714. JackLog("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error \n");
  715. if (err == noErr && size > 0) {
  716. err = AudioDeviceGetProperty(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, channel_name);
  717. if (err != noErr)
  718. JackLog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
  719. snprintf(buf, sizeof(buf) - 1, "%s:%s:out_%s%u", fClientControl->fName, fCaptureDriverName, channel_name, i + 1);
  720. } else {
  721. snprintf(buf, sizeof(buf) - 1, "%s:%s:out%u", fClientControl->fName, fCaptureDriverName, i + 1);
  722. }
  723. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags)) == NO_PORT) {
  724. jack_error("Cannot register port for %s", buf);
  725. return -1;
  726. }
  727. size = sizeof(UInt32);
  728. UInt32 value1 = 0;
  729. UInt32 value2 = 0;
  730. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertyLatency, &size, &value1);
  731. if (err != noErr)
  732. JackLog("AudioDeviceGetProperty kAudioDevicePropertyLatency error \n");
  733. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertySafetyOffset, &size, &value2);
  734. if (err != noErr)
  735. JackLog("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error \n");
  736. port = fGraphManager->GetPort(port_index);
  737. port->Rename("system:capture_%d", i + 1);
  738. port->SetLatency(fEngineControl->fBufferSize + value1 + value2 + fCaptureLatency);
  739. fCapturePortList[i] = port_index;
  740. }
  741. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  742. for (int i = 0; i < fPlaybackChannels; i++) {
  743. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, &isWritable);
  744. if (err != noErr)
  745. JackLog("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error \n");
  746. if (err == noErr && size > 0) {
  747. err = AudioDeviceGetProperty(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, channel_name);
  748. if (err != noErr)
  749. JackLog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
  750. snprintf(buf, sizeof(buf) - 1, "%s:%s:in_%s%u", fClientControl->fName, fPlaybackDriverName, channel_name, i + 1);
  751. } else {
  752. snprintf(buf, sizeof(buf) - 1, "%s:%s:in%u", fClientControl->fName, fPlaybackDriverName, i + 1);
  753. }
  754. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags)) == NO_PORT) {
  755. jack_error("Cannot register port for %s", buf);
  756. return -1;
  757. }
  758. size = sizeof(UInt32);
  759. UInt32 value1 = 0;
  760. UInt32 value2 = 0;
  761. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertyLatency, &size, &value1);
  762. if (err != noErr)
  763. JackLog("AudioDeviceGetProperty kAudioDevicePropertyLatency error \n");
  764. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertySafetyOffset, &size, &value2);
  765. if (err != noErr)
  766. JackLog("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error \n");
  767. port = fGraphManager->GetPort(port_index);
  768. port->Rename("system:playback_%d", i + 1);
  769. port->SetLatency(fEngineControl->fBufferSize + value1 + value2 + fPlaybackLatency);
  770. fPlaybackPortList[i] = port_index;
  771. // Monitor ports
  772. if (fWithMonitorPorts) {
  773. JackLog("Create monitor port \n");
  774. snprintf(buf, sizeof(buf) - 1, "%s:%s:monitor_%u", fClientControl->fName, fPlaybackDriverName, i + 1);
  775. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput)) == NO_PORT) {
  776. jack_error("Cannot register monitor port for %s", buf);
  777. return -1;
  778. } else {
  779. port = fGraphManager->GetPort(port_index);
  780. port->SetLatency(fEngineControl->fBufferSize);
  781. fMonitorPortList[i] = port_index;
  782. }
  783. }
  784. }
  785. // Input buffers do no change : prepare them only once
  786. for (int i = 0; i < fCaptureChannels; i++) {
  787. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  788. }
  789. return 0;
  790. }
  791. int JackCoreAudioDriver::Start()
  792. {
  793. JackLog("JackCoreAudioDriver::Start\n");
  794. JackAudioDriver::Start();
  795. OSStatus err = AudioDeviceAddIOProc(fDeviceID, MeasureCallback, this);
  796. if (err != noErr)
  797. return -1;
  798. err = AudioOutputUnitStart(fAUHAL);
  799. if (err != noErr)
  800. return -1;
  801. if ((err = AudioDeviceStart(fDeviceID, MeasureCallback)) != noErr) {
  802. jack_error("Cannot start MeasureCallback");
  803. printError(err);
  804. return -1;
  805. }
  806. return 0;
  807. }
  808. int JackCoreAudioDriver::Stop()
  809. {
  810. AudioDeviceStop(fDeviceID, MeasureCallback);
  811. AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
  812. JackLog("JackCoreAudioDriver::Stop\n");
  813. return (AudioOutputUnitStop(fAUHAL) == noErr) ? 0 : -1;
  814. }
  815. int JackCoreAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  816. {
  817. OSStatus err;
  818. UInt32 outSize = sizeof(UInt32);
  819. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
  820. if (err != noErr) {
  821. jack_error("Cannot set buffer size %ld", buffer_size);
  822. printError(err);
  823. return -1;
  824. }
  825. JackAudioDriver::SetBufferSize(buffer_size); // never fails
  826. // Input buffers do no change : prepare them only once
  827. for (int i = 0; i < fCaptureChannels; i++) {
  828. fJackInputData->mBuffers[i].mNumberChannels = 1;
  829. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
  830. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  831. }
  832. return 0;
  833. }
  834. } // end of namespace
  835. #ifdef __cplusplus
  836. extern "C"
  837. {
  838. #endif
  839. jack_driver_desc_t* driver_get_descriptor() {
  840. jack_driver_desc_t *desc;
  841. unsigned int i;
  842. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  843. strcpy(desc->name, "coreaudio");
  844. desc->nparams = 13;
  845. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  846. i = 0;
  847. strcpy(desc->params[i].name, "channels");
  848. desc->params[i].character = 'c';
  849. desc->params[i].type = JackDriverParamInt;
  850. desc->params[i].value.ui = 0;
  851. strcpy(desc->params[i].short_desc, "Maximum number of channels");
  852. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  853. i++;
  854. strcpy(desc->params[i].name, "inchannels");
  855. desc->params[i].character = 'i';
  856. desc->params[i].type = JackDriverParamInt;
  857. desc->params[i].value.ui = 0;
  858. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  859. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  860. i++;
  861. strcpy(desc->params[i].name, "outchannels");
  862. desc->params[i].character = 'o';
  863. desc->params[i].type = JackDriverParamInt;
  864. desc->params[i].value.ui = 0;
  865. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  866. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  867. i++;
  868. strcpy(desc->params[i].name, "capture");
  869. desc->params[i].character = 'C';
  870. desc->params[i].type = JackDriverParamString;
  871. strcpy(desc->params[i].value.str, "will take default CoreAudio input device");
  872. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set CoreAudio device name");
  873. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  874. i++;
  875. strcpy(desc->params[i].name, "playback");
  876. desc->params[i].character = 'P';
  877. desc->params[i].type = JackDriverParamString;
  878. strcpy(desc->params[i].value.str, "will take default CoreAudio output device");
  879. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set CoreAudio device name");
  880. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  881. i++;
  882. strcpy (desc->params[i].name, "monitor");
  883. desc->params[i].character = 'm';
  884. desc->params[i].type = JackDriverParamBool;
  885. desc->params[i].value.i = 0;
  886. strcpy(desc->params[i].short_desc, "Provide monitor ports for the output");
  887. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  888. i++;
  889. strcpy(desc->params[i].name, "duplex");
  890. desc->params[i].character = 'D';
  891. desc->params[i].type = JackDriverParamBool;
  892. desc->params[i].value.i = TRUE;
  893. strcpy(desc->params[i].short_desc, "Provide both capture and playback ports");
  894. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  895. i++;
  896. strcpy(desc->params[i].name, "rate");
  897. desc->params[i].character = 'r';
  898. desc->params[i].type = JackDriverParamUInt;
  899. desc->params[i].value.ui = 44100U;
  900. strcpy(desc->params[i].short_desc, "Sample rate");
  901. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  902. i++;
  903. strcpy(desc->params[i].name, "period");
  904. desc->params[i].character = 'p';
  905. desc->params[i].type = JackDriverParamUInt;
  906. desc->params[i].value.ui = 128U;
  907. strcpy(desc->params[i].short_desc, "Frames per period");
  908. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  909. i++;
  910. strcpy(desc->params[i].name, "device");
  911. desc->params[i].character = 'd';
  912. desc->params[i].type = JackDriverParamString;
  913. desc->params[i].value.ui = 128U;
  914. strcpy(desc->params[i].value.str, "will take default CoreAudio device name");
  915. strcpy(desc->params[i].short_desc, "CoreAudio device name");
  916. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  917. i++;
  918. strcpy(desc->params[i].name, "input-latency");
  919. desc->params[i].character = 'I';
  920. desc->params[i].type = JackDriverParamUInt;
  921. desc->params[i].value.i = 0;
  922. strcpy(desc->params[i].short_desc, "Extra input latency");
  923. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  924. i++;
  925. strcpy(desc->params[i].name, "output-latency");
  926. desc->params[i].character = 'O';
  927. desc->params[i].type = JackDriverParamUInt;
  928. desc->params[i].value.i = 0;
  929. strcpy(desc->params[i].short_desc, "Extra output latency");
  930. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  931. i++;
  932. strcpy(desc->params[i].name, "list-devices");
  933. desc->params[i].character = 'l';
  934. desc->params[i].type = JackDriverParamBool;
  935. desc->params[i].value.i = TRUE;
  936. strcpy(desc->params[i].short_desc, "Display available CoreAudio devices");
  937. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  938. return desc;
  939. }
  940. Jack::JackDriverClientInterface* driver_initialize(Jack::JackEngine* engine, Jack::JackSynchro** table, const JSList* params) {
  941. jack_nframes_t srate = 44100;
  942. jack_nframes_t frames_per_interrupt = 128;
  943. int capture = FALSE;
  944. int playback = FALSE;
  945. int chan_in = 0;
  946. int chan_out = 0;
  947. bool monitor = false;
  948. char* capture_pcm_name = "";
  949. char* playback_pcm_name = "";
  950. const JSList *node;
  951. const jack_driver_param_t *param;
  952. jack_nframes_t systemic_input_latency = 0;
  953. jack_nframes_t systemic_output_latency = 0;
  954. for (node = params; node; node = jack_slist_next(node)) {
  955. param = (const jack_driver_param_t *) node->data;
  956. switch (param->character) {
  957. case 'd':
  958. capture_pcm_name = strdup(param->value.str);
  959. playback_pcm_name = strdup(param->value.str);
  960. break;
  961. case 'D':
  962. capture = TRUE;
  963. playback = TRUE;
  964. break;
  965. case 'c':
  966. chan_in = chan_out = (int) param->value.ui;
  967. break;
  968. case 'i':
  969. chan_in = (int) param->value.ui;
  970. break;
  971. case 'o':
  972. chan_out = (int) param->value.ui;
  973. break;
  974. case 'C':
  975. capture = TRUE;
  976. if (strcmp(param->value.str, "none") != 0) {
  977. capture_pcm_name = strdup(param->value.str);
  978. }
  979. break;
  980. case 'P':
  981. playback = TRUE;
  982. if (strcmp(param->value.str, "none") != 0) {
  983. playback_pcm_name = strdup(param->value.str);
  984. }
  985. break;
  986. case 'm':
  987. monitor = param->value.i;
  988. break;
  989. case 'r':
  990. srate = param->value.ui;
  991. break;
  992. case 'p':
  993. frames_per_interrupt = (unsigned int) param->value.ui;
  994. break;
  995. case 'I':
  996. systemic_input_latency = param->value.ui;
  997. break;
  998. case 'O':
  999. systemic_output_latency = param->value.ui;
  1000. break;
  1001. case 'l':
  1002. Jack::DisplayDeviceNames();
  1003. break;
  1004. }
  1005. }
  1006. /* duplex is the default */
  1007. if (!capture && !playback) {
  1008. capture = TRUE;
  1009. playback = TRUE;
  1010. }
  1011. Jack::JackDriverClientInterface* driver = new Jack::JackCoreAudioDriver("coreaudio", engine, table);
  1012. 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) {
  1013. return driver;
  1014. } else {
  1015. delete driver;
  1016. return NULL;
  1017. }
  1018. }
  1019. #ifdef __cplusplus
  1020. }
  1021. #endif