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.

1265 lines
47KB

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