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.

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