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.

1192 lines
45KB

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