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.

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