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.

1188 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,
  250. &theSize, &inDefault)) != noErr)
  251. return res;
  252. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
  253. &theSize, &outDefault)) != noErr)
  254. return res;
  255. JackLog("GetDefaultDevice: input = %ld output = %ld\n", inDefault, outDefault);
  256. // Get the device only if default input and ouput are the same
  257. if (inDefault == outDefault) {
  258. *id = inDefault;
  259. return noErr;
  260. } else {
  261. jack_error("Default input and output devices are not the same !!");
  262. return kAudioHardwareBadDeviceError;
  263. }
  264. }
  265. OSStatus JackCoreAudioDriver::GetDefaultInputDevice(AudioDeviceID* id)
  266. {
  267. OSStatus res;
  268. UInt32 theSize = sizeof(UInt32);
  269. AudioDeviceID inDefault;
  270. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice,
  271. &theSize, &inDefault)) != noErr)
  272. return res;
  273. JackLog("GetDefaultInputDevice: input = %ld \n", inDefault);
  274. *id = inDefault;
  275. return noErr;
  276. }
  277. OSStatus JackCoreAudioDriver::GetDefaultOutputDevice(AudioDeviceID* id)
  278. {
  279. OSStatus res;
  280. UInt32 theSize = sizeof(UInt32);
  281. AudioDeviceID outDefault;
  282. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
  283. &theSize, &outDefault)) != noErr)
  284. return res;
  285. JackLog("GetDefaultOutputDevice: output = %ld\n", outDefault);
  286. *id = outDefault;
  287. return noErr;
  288. }
  289. OSStatus JackCoreAudioDriver::GetDeviceNameFromID(AudioDeviceID id, char* name)
  290. {
  291. UInt32 size = 256;
  292. return AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceName, &size, name);
  293. }
  294. OSStatus JackCoreAudioDriver::GetTotalChannels(AudioDeviceID device, long* channelCount, bool isInput)
  295. {
  296. OSStatus err = noErr;
  297. UInt32 outSize;
  298. Boolean outWritable;
  299. AudioBufferList* bufferList = 0;
  300. AudioStreamID* streamList = 0;
  301. int i, numStream;
  302. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreams, &outSize, &outWritable);
  303. if (err == noErr) {
  304. streamList = (AudioStreamID*)malloc(outSize);
  305. numStream = outSize / sizeof(AudioStreamID);
  306. JackLog("GetTotalChannels device stream number = %ld numStream = %ld\n", device, numStream);
  307. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreams, &outSize, streamList);
  308. if (err == noErr) {
  309. AudioStreamBasicDescription streamDesc;
  310. outSize = sizeof(AudioStreamBasicDescription);
  311. for (i = 0; i < numStream; i++) {
  312. err = AudioStreamGetProperty(streamList[i], 0, kAudioDevicePropertyStreamFormat, &outSize, &streamDesc);
  313. JackLog("GetTotalChannels streamDesc mFormatFlags = %ld mChannelsPerFrame = %ld\n", streamDesc.mFormatFlags, streamDesc.mChannelsPerFrame);
  314. }
  315. }
  316. }
  317. *channelCount = 0;
  318. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
  319. if (err == noErr) {
  320. bufferList = (AudioBufferList*)malloc(outSize);
  321. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
  322. if (err == noErr) {
  323. for (i = 0; i < bufferList->mNumberBuffers; i++)
  324. *channelCount += bufferList->mBuffers[i].mNumberChannels;
  325. }
  326. }
  327. if (streamList)
  328. free(streamList);
  329. if (bufferList)
  330. free(bufferList);
  331. return err;
  332. }
  333. JackCoreAudioDriver::JackCoreAudioDriver(const char* name, JackEngine* engine, JackSynchro** table)
  334. : JackAudioDriver(name, engine, table), fJackInputData(NULL), fDriverOutputData(NULL)
  335. {
  336. #ifdef DEBUG
  337. //fLogFile = new CALatencyLog("jackmp_latency", ".txt");
  338. #endif
  339. }
  340. JackCoreAudioDriver::~JackCoreAudioDriver()
  341. {
  342. #ifdef DEBUG
  343. //delete fLogFile;
  344. #endif
  345. }
  346. int JackCoreAudioDriver::Open(jack_nframes_t nframes,
  347. jack_nframes_t samplerate,
  348. int capturing,
  349. int playing,
  350. int inchannels,
  351. int outchannels,
  352. bool monitor,
  353. const char* capture_driver_uid,
  354. const char* playback_driver_uid,
  355. jack_nframes_t capture_latency,
  356. jack_nframes_t playback_latency)
  357. {
  358. OSStatus err = noErr;
  359. ComponentResult err1;
  360. UInt32 outSize;
  361. UInt32 enableIO;
  362. AudioStreamBasicDescription srcFormat, dstFormat, sampleRate;
  363. long in_nChannels = 0;
  364. long out_nChannels = 0;
  365. char capture_driver_name[256];
  366. char playback_driver_name[256];
  367. float iousage;
  368. capture_driver_name[0] = 0;
  369. playback_driver_name[0] = 0;
  370. JackLog("JackCoreAudioDriver::Open nframes = %ld in = %ld out = %ld capture name = %s playback name = %s\n",
  371. nframes, inchannels, outchannels, capture_driver_uid, playback_driver_uid);
  372. // Duplex
  373. if (capture_driver_uid != NULL && playback_driver_uid != NULL) {
  374. JackLog("JackCoreAudioDriver::Open duplex \n");
  375. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  376. if (GetDefaultDevice(&fDeviceID) != noErr) {
  377. jack_error("Cannot open default device");
  378. return -1;
  379. }
  380. }
  381. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  382. jack_error("Cannot get device name from device ID");
  383. return -1;
  384. }
  385. // Capture only
  386. } else if (capture_driver_uid != NULL) {
  387. JackLog("JackCoreAudioDriver::Open capture only \n");
  388. if (GetDeviceIDFromUID(capture_driver_uid, &fDeviceID) != noErr) {
  389. if (GetDefaultInputDevice(&fDeviceID) != noErr) {
  390. jack_error("Cannot open default device");
  391. return -1;
  392. }
  393. }
  394. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr) {
  395. jack_error("Cannot get device name from device ID");
  396. return -1;
  397. }
  398. // Playback only
  399. } else if (playback_driver_uid != NULL) {
  400. JackLog("JackCoreAudioDriver::Open playback only \n");
  401. if (GetDeviceIDFromUID(playback_driver_uid, &fDeviceID) != noErr) {
  402. if (GetDefaultOutputDevice(&fDeviceID) != noErr) {
  403. jack_error("Cannot open default device");
  404. return -1;
  405. }
  406. }
  407. if (GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  408. jack_error("Cannot get device name from device ID");
  409. return -1;
  410. }
  411. // Use default driver in duplex mode
  412. } else {
  413. JackLog("JackCoreAudioDriver::Open default driver \n");
  414. if (GetDefaultDevice(&fDeviceID) != noErr) {
  415. jack_error("Cannot open default device");
  416. return -1;
  417. }
  418. if (GetDeviceNameFromID(fDeviceID, capture_driver_name) != noErr || GetDeviceNameFromID(fDeviceID, playback_driver_name) != noErr) {
  419. jack_error("Cannot get device name from device ID");
  420. return -1;
  421. }
  422. }
  423. // Generic JackAudioDriver Open
  424. if (JackAudioDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency) != 0) {
  425. return -1;
  426. }
  427. if (capturing) {
  428. err = GetTotalChannels(fDeviceID, &in_nChannels, true);
  429. if (err != noErr) {
  430. jack_error("Cannot get input channel number");
  431. printError(err);
  432. return -1;
  433. }
  434. }
  435. if (playing) {
  436. err = GetTotalChannels(fDeviceID, &out_nChannels, false);
  437. if (err != noErr) {
  438. jack_error("Cannot get output channel number");
  439. printError(err);
  440. return -1;
  441. }
  442. }
  443. if (inchannels > in_nChannels) {
  444. jack_error("This device hasn't required input channels inchannels = %ld in_nChannels = %ld", inchannels, in_nChannels);
  445. return -1;
  446. }
  447. if (outchannels > out_nChannels) {
  448. jack_error("This device hasn't required output channels outchannels = %ld out_nChannels = %ld", outchannels, out_nChannels);
  449. return -1;
  450. }
  451. if (inchannels == 0) {
  452. JackLog("Setup max in channels = %ld\n", in_nChannels);
  453. inchannels = in_nChannels;
  454. }
  455. if (outchannels == 0) {
  456. JackLog("Setup max out channels = %ld\n", out_nChannels);
  457. outchannels = out_nChannels;
  458. }
  459. // Setting buffer size
  460. outSize = sizeof(UInt32);
  461. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &fEngineControl->fBufferSize);
  462. if (err != noErr) {
  463. jack_error("Cannot set buffer size %ld", nframes);
  464. printError(err);
  465. return -1;
  466. }
  467. // Set sample rate
  468. if (capturing && inchannels > 0) {
  469. outSize = sizeof(AudioStreamBasicDescription);
  470. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertyStreamFormat, &outSize, &sampleRate);
  471. if (err != noErr) {
  472. jack_error("Cannot get current sample rate");
  473. printError(err);
  474. return -1;
  475. }
  476. if (samplerate != (unsigned long)sampleRate.mSampleRate) {
  477. sampleRate.mSampleRate = (Float64)samplerate;
  478. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, true, kAudioDevicePropertyStreamFormat, outSize, &sampleRate);
  479. if (err != noErr) {
  480. jack_error("Cannot set sample rate = %ld", samplerate);
  481. printError(err);
  482. return -1;
  483. }
  484. }
  485. }
  486. if (playing && outchannels > 0) {
  487. outSize = sizeof(AudioStreamBasicDescription);
  488. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertyStreamFormat, &outSize, &sampleRate);
  489. if (err != noErr) {
  490. jack_error("Cannot get current sample rate");
  491. printError(err);
  492. return -1;
  493. }
  494. if (samplerate != (unsigned long)sampleRate.mSampleRate) {
  495. sampleRate.mSampleRate = (Float64)samplerate;
  496. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyStreamFormat, outSize, &sampleRate);
  497. if (err != noErr) {
  498. jack_error("Cannot set sample rate = %ld", samplerate);
  499. printError(err);
  500. return -1;
  501. }
  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. fDriverOutputData = 0;
  672. #if IO_CPU
  673. outSize = sizeof(float);
  674. iousage = 0.4f;
  675. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyIOCycleUsage, outSize, &iousage);
  676. if (err != noErr) {
  677. jack_error("Error calling AudioDeviceSetProperty kAudioDevicePropertyIOCycleUsage");
  678. printError(err);
  679. }
  680. #endif
  681. // Core driver may have changed the in/out values
  682. fCaptureChannels = inchannels;
  683. fPlaybackChannels = outchannels;
  684. return 0;
  685. error:
  686. AudioUnitUninitialize(fAUHAL);
  687. CloseComponent(fAUHAL);
  688. return -1;
  689. }
  690. int JackCoreAudioDriver::Close()
  691. {
  692. JackAudioDriver::Close();
  693. // Possibly (if MeasureCallback has not been called)
  694. AudioDeviceStop(fDeviceID, MeasureCallback);
  695. AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
  696. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback);
  697. free(fJackInputData);
  698. AudioUnitUninitialize(fAUHAL);
  699. CloseComponent(fAUHAL);
  700. return 0;
  701. }
  702. int JackCoreAudioDriver::Attach()
  703. {
  704. OSStatus err;
  705. JackPort* port;
  706. jack_port_id_t port_index;
  707. UInt32 size;
  708. Boolean isWritable;
  709. char channel_name[64];
  710. char buf[JACK_PORT_NAME_SIZE];
  711. unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  712. JackLog("JackCoreAudioDriver::Attach fBufferSize %ld fSampleRate %ld\n", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  713. for (int i = 0; i < fCaptureChannels; i++) {
  714. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, &isWritable);
  715. if (err != noErr)
  716. JackLog("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error \n");
  717. if (err == noErr && size > 0) {
  718. err = AudioDeviceGetProperty(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, channel_name);
  719. if (err != noErr)
  720. JackLog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
  721. snprintf(buf, sizeof(buf) - 1, "%s:%s:out_%s%u", fClientControl->fName, fCaptureDriverName, channel_name, i + 1);
  722. } else {
  723. snprintf(buf, sizeof(buf) - 1, "%s:%s:out%u", fClientControl->fName, fCaptureDriverName, i + 1);
  724. }
  725. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, (JackPortFlags)port_flags)) == NO_PORT) {
  726. jack_error("Cannot register port for %s", buf);
  727. return -1;
  728. }
  729. port = fGraphManager->GetPort(port_index);
  730. port->SetLatency(fEngineControl->fBufferSize + fCaptureLatency);
  731. fCapturePortList[i] = port_index;
  732. }
  733. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  734. for (int i = 0; i < fPlaybackChannels; i++) {
  735. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, &isWritable);
  736. if (err != noErr)
  737. JackLog("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error \n");
  738. if (err == noErr && size > 0) {
  739. err = AudioDeviceGetProperty(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, channel_name);
  740. if (err != noErr)
  741. JackLog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
  742. snprintf(buf, sizeof(buf) - 1, "%s:%s:in_%s%u", fClientControl->fName, fPlaybackDriverName, channel_name, i + 1);
  743. } else {
  744. snprintf(buf, sizeof(buf) - 1, "%s:%s:in%u", fClientControl->fName, fPlaybackDriverName, i + 1);
  745. }
  746. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, (JackPortFlags)port_flags)) == NO_PORT) {
  747. jack_error("Cannot register port for %s", buf);
  748. return -1;
  749. }
  750. port = fGraphManager->GetPort(port_index);
  751. port->SetLatency(fEngineControl->fBufferSize + fPlaybackLatency);
  752. fPlaybackPortList[i] = port_index;
  753. // Monitor ports
  754. if (fWithMonitorPorts) {
  755. JackLog("Create monitor port \n");
  756. snprintf(buf, sizeof(buf) - 1, "%s:%s:monitor_%u", fClientControl->fName, fPlaybackDriverName, i + 1);
  757. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JackPortIsOutput)) == NO_PORT) {
  758. jack_error("Cannot register monitor port for %s", buf);
  759. return -1;
  760. } else {
  761. port = fGraphManager->GetPort(port_index);
  762. port->SetLatency(fEngineControl->fBufferSize);
  763. fMonitorPortList[i] = port_index;
  764. }
  765. }
  766. }
  767. // Input buffers do no change : prepare them only once
  768. for (int i = 0; i < fCaptureChannels; i++) {
  769. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  770. }
  771. return 0;
  772. }
  773. int JackCoreAudioDriver::Start()
  774. {
  775. OSStatus err;
  776. JackLog("JackCoreAudioDriver::Start\n");
  777. JackAudioDriver::Start();
  778. err = AudioDeviceAddIOProc(fDeviceID, MeasureCallback, this);
  779. if (err != noErr)
  780. return -1;
  781. err = AudioOutputUnitStart(fAUHAL);
  782. if (err != noErr)
  783. return -1;
  784. if ((err = AudioDeviceStart(fDeviceID, MeasureCallback)) != noErr) {
  785. jack_error("Cannot start MeasureCallback");
  786. printError(err);
  787. return -1;
  788. }
  789. return 0;
  790. }
  791. int JackCoreAudioDriver::Stop()
  792. {
  793. AudioDeviceStop(fDeviceID, MeasureCallback);
  794. AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
  795. JackLog("JackCoreAudioDriver::Stop\n");
  796. return (AudioOutputUnitStop(fAUHAL) == noErr) ? 0 : -1;
  797. }
  798. int JackCoreAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  799. {
  800. OSStatus err;
  801. UInt32 outSize = sizeof(UInt32);
  802. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
  803. if (err != noErr) {
  804. jack_error("Cannot set buffer size %ld", buffer_size);
  805. printError(err);
  806. return -1;
  807. }
  808. fEngineControl->fBufferSize = buffer_size;
  809. fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize); // In microsec
  810. // Input buffers do no change : prepare them only once
  811. for (int i = 0; i < fCaptureChannels; i++) {
  812. fJackInputData->mBuffers[i].mNumberChannels = 1;
  813. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
  814. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  815. }
  816. return 0;
  817. }
  818. void JackCoreAudioDriver::PrintState()
  819. {
  820. std::cout << "JackCoreAudioDriver state" << std::endl;
  821. jack_port_id_t port_index;
  822. std::cout << "Input ports" << std::endl;
  823. for (int i = 0; i < fPlaybackChannels; i++) {
  824. port_index = fCapturePortList[i];
  825. JackPort* port = fGraphManager->GetPort(port_index);
  826. std::cout << port->GetName() << std::endl;
  827. if (fGraphManager->GetConnectionsNum(port_index)) {}
  828. }
  829. std::cout << "Output ports" << std::endl;
  830. for (int i = 0; i < fCaptureChannels; i++) {
  831. port_index = fPlaybackPortList[i];
  832. JackPort* port = fGraphManager->GetPort(port_index);
  833. std::cout << port->GetName() << std::endl;
  834. if (fGraphManager->GetConnectionsNum(port_index)) {}
  835. }
  836. }
  837. } // end of namespace
  838. #ifdef __cplusplus
  839. extern "C"
  840. {
  841. #endif
  842. jack_driver_desc_t* driver_get_descriptor() {
  843. jack_driver_desc_t *desc;
  844. unsigned int i;
  845. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  846. strcpy(desc->name, "coreaudio");
  847. desc->nparams = 13;
  848. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  849. i = 0;
  850. strcpy(desc->params[i].name, "channels");
  851. desc->params[i].character = 'c';
  852. desc->params[i].type = JackDriverParamInt;
  853. desc->params[i].value.ui = 0;
  854. strcpy(desc->params[i].short_desc, "Maximum number of channels");
  855. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  856. i++;
  857. strcpy(desc->params[i].name, "inchannels");
  858. desc->params[i].character = 'i';
  859. desc->params[i].type = JackDriverParamInt;
  860. desc->params[i].value.ui = 0;
  861. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  862. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  863. i++;
  864. strcpy(desc->params[i].name, "outchannels");
  865. desc->params[i].character = 'o';
  866. desc->params[i].type = JackDriverParamInt;
  867. desc->params[i].value.ui = 0;
  868. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  869. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  870. i++;
  871. strcpy(desc->params[i].name, "capture");
  872. desc->params[i].character = 'C';
  873. desc->params[i].type = JackDriverParamString;
  874. strcpy(desc->params[i].value.str, "will take default CoreAudio input device");
  875. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set CoreAudio device name");
  876. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  877. i++;
  878. strcpy(desc->params[i].name, "playback");
  879. desc->params[i].character = 'P';
  880. desc->params[i].type = JackDriverParamString;
  881. strcpy(desc->params[i].value.str, "will take default CoreAudio output device");
  882. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set CoreAudio device name");
  883. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  884. i++;
  885. strcpy (desc->params[i].name, "monitor");
  886. desc->params[i].character = 'm';
  887. desc->params[i].type = JackDriverParamBool;
  888. desc->params[i].value.i = 0;
  889. strcpy(desc->params[i].short_desc, "Provide monitor ports for the output");
  890. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  891. i++;
  892. strcpy(desc->params[i].name, "duplex");
  893. desc->params[i].character = 'D';
  894. desc->params[i].type = JackDriverParamBool;
  895. desc->params[i].value.i = TRUE;
  896. strcpy(desc->params[i].short_desc, "Provide both capture and playback ports");
  897. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  898. i++;
  899. strcpy(desc->params[i].name, "rate");
  900. desc->params[i].character = 'r';
  901. desc->params[i].type = JackDriverParamUInt;
  902. desc->params[i].value.ui = 44100U;
  903. strcpy(desc->params[i].short_desc, "Sample rate");
  904. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  905. i++;
  906. strcpy(desc->params[i].name, "period");
  907. desc->params[i].character = 'p';
  908. desc->params[i].type = JackDriverParamUInt;
  909. desc->params[i].value.ui = 128U;
  910. strcpy(desc->params[i].short_desc, "Frames per period");
  911. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  912. i++;
  913. strcpy(desc->params[i].name, "device");
  914. desc->params[i].character = 'd';
  915. desc->params[i].type = JackDriverParamString;
  916. desc->params[i].value.ui = 128U;
  917. strcpy(desc->params[i].value.str, "will take default CoreAudio device name");
  918. strcpy(desc->params[i].short_desc, "CoreAudio device name");
  919. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  920. i++;
  921. strcpy(desc->params[i].name, "input-latency");
  922. desc->params[i].character = 'I';
  923. desc->params[i].type = JackDriverParamUInt;
  924. desc->params[i].value.i = 0;
  925. strcpy(desc->params[i].short_desc, "Extra input latency");
  926. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  927. i++;
  928. strcpy(desc->params[i].name, "output-latency");
  929. desc->params[i].character = 'O';
  930. desc->params[i].type = JackDriverParamUInt;
  931. desc->params[i].value.i = 0;
  932. strcpy(desc->params[i].short_desc, "Extra output latency");
  933. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  934. i++;
  935. strcpy(desc->params[i].name, "list-devices");
  936. desc->params[i].character = 'l';
  937. desc->params[i].type = JackDriverParamBool;
  938. desc->params[i].value.i = TRUE;
  939. strcpy(desc->params[i].short_desc, "Display available CoreAudio devices");
  940. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  941. return desc;
  942. }
  943. Jack::JackDriverClientInterface* driver_initialize(Jack::JackEngine* engine, Jack::JackSynchro** table, const JSList* params) {
  944. jack_nframes_t srate = 44100;
  945. jack_nframes_t frames_per_interrupt = 128;
  946. int capture = FALSE;
  947. int playback = FALSE;
  948. int chan_in = 0;
  949. int chan_out = 0;
  950. bool monitor = false;
  951. char* capture_pcm_name = "";
  952. char* playback_pcm_name = "";
  953. const JSList *node;
  954. const jack_driver_param_t *param;
  955. jack_nframes_t systemic_input_latency = 0;
  956. jack_nframes_t systemic_output_latency = 0;
  957. for (node = params; node; node = jack_slist_next(node)) {
  958. param = (const jack_driver_param_t *) node->data;
  959. switch (param->character) {
  960. case 'd':
  961. capture_pcm_name = strdup(param->value.str);
  962. playback_pcm_name = strdup(param->value.str);
  963. break;
  964. case 'D':
  965. capture = TRUE;
  966. playback = TRUE;
  967. break;
  968. case 'c':
  969. chan_in = chan_out = (int) param->value.ui;
  970. break;
  971. case 'i':
  972. chan_in = (int) param->value.ui;
  973. break;
  974. case 'o':
  975. chan_out = (int) param->value.ui;
  976. break;
  977. case 'C':
  978. capture = TRUE;
  979. if (strcmp(param->value.str, "none") != 0) {
  980. capture_pcm_name = strdup(param->value.str);
  981. }
  982. break;
  983. case 'P':
  984. playback = TRUE;
  985. if (strcmp(param->value.str, "none") != 0) {
  986. playback_pcm_name = strdup(param->value.str);
  987. }
  988. break;
  989. case 'm':
  990. monitor = param->value.i;
  991. break;
  992. case 'r':
  993. srate = param->value.ui;
  994. break;
  995. case 'p':
  996. frames_per_interrupt = (unsigned int) param->value.ui;
  997. break;
  998. case 'I':
  999. systemic_input_latency = param->value.ui;
  1000. break;
  1001. case 'O':
  1002. systemic_output_latency = param->value.ui;
  1003. break;
  1004. case 'l':
  1005. Jack::DisplayDeviceNames();
  1006. break;
  1007. }
  1008. }
  1009. /* duplex is the default */
  1010. if (!capture && !playback) {
  1011. capture = TRUE;
  1012. playback = TRUE;
  1013. }
  1014. Jack::JackDriverClientInterface* driver = new Jack::JackCoreAudioDriver("coreaudio", engine, table);
  1015. 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) {
  1016. return driver;
  1017. } else {
  1018. delete driver;
  1019. return NULL;
  1020. }
  1021. }
  1022. #ifdef __cplusplus
  1023. }
  1024. #endif