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.

1255 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. goto error;
  661. }
  662. JackLog("Setup AUHAL output stream converter SR = %ld\n", samplerate);
  663. dstFormat.mSampleRate = samplerate;
  664. dstFormat.mFormatID = kAudioFormatLinearPCM;
  665. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  666. dstFormat.mBytesPerPacket = sizeof(float);
  667. dstFormat.mFramesPerPacket = 1;
  668. dstFormat.mBytesPerFrame = sizeof(float);
  669. dstFormat.mChannelsPerFrame = inchannels;
  670. dstFormat.mBitsPerChannel = 32;
  671. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, sizeof(AudioStreamBasicDescription));
  672. if (err1 != noErr) {
  673. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  674. printError(err1);
  675. goto error;
  676. }
  677. // Setup callbacks
  678. if (inchannels > 0 && outchannels == 0) {
  679. AURenderCallbackStruct output;
  680. output.inputProc = Render;
  681. output.inputProcRefCon = this;
  682. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  683. if (err1 != noErr) {
  684. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  685. printError(err1);
  686. goto error;
  687. }
  688. } else {
  689. AURenderCallbackStruct output;
  690. output.inputProc = Render;
  691. output.inputProcRefCon = this;
  692. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  693. if (err1 != noErr) {
  694. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  695. printError(err1);
  696. goto error;
  697. }
  698. }
  699. // Prepare buffers
  700. if (capturing && inchannels > 0) {
  701. fJackInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
  702. if (fJackInputData == 0) {
  703. jack_error("Cannot allocate memory for input buffers");
  704. goto error;
  705. }
  706. fJackInputData->mNumberBuffers = inchannels;
  707. for (int i = 0; i < fCaptureChannels; i++) {
  708. fJackInputData->mBuffers[i].mNumberChannels = 1;
  709. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
  710. }
  711. }
  712. // Add listeners
  713. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback, this);
  714. if (err != noErr) {
  715. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
  716. printError(err1);
  717. goto error;
  718. }
  719. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback, this);
  720. if (err != noErr) {
  721. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioHardwarePropertyDevices");
  722. printError(err1);
  723. goto error;
  724. }
  725. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback, this);
  726. if (err != noErr) {
  727. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  728. printError(err1);
  729. goto error;
  730. }
  731. fDriverOutputData = 0;
  732. #if IO_CPU
  733. outSize = sizeof(float);
  734. iousage = 0.4f;
  735. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyIOCycleUsage, outSize, &iousage);
  736. if (err != noErr) {
  737. jack_error("Error calling AudioDeviceSetProperty kAudioDevicePropertyIOCycleUsage");
  738. printError(err);
  739. }
  740. #endif
  741. // Core driver may have changed the in/out values
  742. fCaptureChannels = inchannels;
  743. fPlaybackChannels = outchannels;
  744. return noErr;
  745. error:
  746. AudioUnitUninitialize(fAUHAL);
  747. CloseComponent(fAUHAL);
  748. return -1;
  749. }
  750. int JackCoreAudioDriver::Close()
  751. {
  752. JackLog("JackCoreAudioDriver::Close\n");
  753. JackAudioDriver::Close();
  754. // Possibly (if MeasureCallback has not been called)
  755. AudioDeviceStop(fDeviceID, MeasureCallback);
  756. AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
  757. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback);
  758. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback);
  759. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback);
  760. free(fJackInputData);
  761. AudioUnitUninitialize(fAUHAL);
  762. CloseComponent(fAUHAL);
  763. return 0;
  764. }
  765. int JackCoreAudioDriver::Attach()
  766. {
  767. OSStatus err;
  768. JackPort* port;
  769. jack_port_id_t port_index;
  770. UInt32 size;
  771. Boolean isWritable;
  772. char channel_name[64];
  773. char buf[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  774. unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  775. JackLog("JackCoreAudioDriver::Attach fBufferSize %ld fSampleRate %ld\n", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  776. for (int i = 0; i < fCaptureChannels; i++) {
  777. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, &isWritable);
  778. if (err != noErr)
  779. JackLog("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error \n");
  780. if (err == noErr && size > 0) {
  781. err = AudioDeviceGetProperty(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, channel_name);
  782. if (err != noErr)
  783. JackLog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
  784. snprintf(buf, sizeof(buf) - 1, "%s:%s:out_%s%u", fClientControl->fName, fCaptureDriverName, channel_name, i + 1);
  785. } else {
  786. snprintf(buf, sizeof(buf) - 1, "%s:%s:out%u", fClientControl->fName, fCaptureDriverName, i + 1);
  787. }
  788. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags)) == NO_PORT) {
  789. jack_error("Cannot register port for %s", buf);
  790. return -1;
  791. }
  792. size = sizeof(UInt32);
  793. UInt32 value1 = 0;
  794. UInt32 value2 = 0;
  795. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertyLatency, &size, &value1);
  796. if (err != noErr)
  797. JackLog("AudioDeviceGetProperty kAudioDevicePropertyLatency error \n");
  798. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertySafetyOffset, &size, &value2);
  799. if (err != noErr)
  800. JackLog("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error \n");
  801. port = fGraphManager->GetPort(port_index);
  802. port->Rename("system:capture_%d", i + 1);
  803. port->SetLatency(fEngineControl->fBufferSize + value1 + value2 + fCaptureLatency);
  804. fCapturePortList[i] = port_index;
  805. }
  806. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  807. for (int i = 0; i < fPlaybackChannels; i++) {
  808. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, &isWritable);
  809. if (err != noErr)
  810. JackLog("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error \n");
  811. if (err == noErr && size > 0) {
  812. err = AudioDeviceGetProperty(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, channel_name);
  813. if (err != noErr)
  814. JackLog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
  815. snprintf(buf, sizeof(buf) - 1, "%s:%s:in_%s%u", fClientControl->fName, fPlaybackDriverName, channel_name, i + 1);
  816. } else {
  817. snprintf(buf, sizeof(buf) - 1, "%s:%s:in%u", fClientControl->fName, fPlaybackDriverName, i + 1);
  818. }
  819. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags)) == NO_PORT) {
  820. jack_error("Cannot register port for %s", buf);
  821. return -1;
  822. }
  823. size = sizeof(UInt32);
  824. UInt32 value1 = 0;
  825. UInt32 value2 = 0;
  826. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertyLatency, &size, &value1);
  827. if (err != noErr)
  828. JackLog("AudioDeviceGetProperty kAudioDevicePropertyLatency error \n");
  829. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertySafetyOffset, &size, &value2);
  830. if (err != noErr)
  831. JackLog("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error \n");
  832. port = fGraphManager->GetPort(port_index);
  833. port->Rename("system:playback_%d", i + 1);
  834. port->SetLatency(fEngineControl->fBufferSize + value1 + value2 + fPlaybackLatency);
  835. fPlaybackPortList[i] = port_index;
  836. // Monitor ports
  837. if (fWithMonitorPorts) {
  838. JackLog("Create monitor port \n");
  839. snprintf(buf, sizeof(buf) - 1, "%s:%s:monitor_%u", fClientControl->fName, fPlaybackDriverName, i + 1);
  840. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput)) == NO_PORT) {
  841. jack_error("Cannot register monitor port for %s", buf);
  842. return -1;
  843. } else {
  844. port = fGraphManager->GetPort(port_index);
  845. port->SetLatency(fEngineControl->fBufferSize);
  846. fMonitorPortList[i] = port_index;
  847. }
  848. }
  849. }
  850. // Input buffers do no change : prepare them only once
  851. for (int i = 0; i < fCaptureChannels; i++) {
  852. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  853. }
  854. return 0;
  855. }
  856. int JackCoreAudioDriver::Start()
  857. {
  858. JackLog("JackCoreAudioDriver::Start\n");
  859. JackAudioDriver::Start();
  860. OSStatus err = AudioDeviceAddIOProc(fDeviceID, MeasureCallback, this);
  861. if (err != noErr)
  862. return -1;
  863. err = AudioOutputUnitStart(fAUHAL);
  864. if (err != noErr)
  865. return -1;
  866. if ((err = AudioDeviceStart(fDeviceID, MeasureCallback)) != noErr) {
  867. jack_error("Cannot start MeasureCallback");
  868. printError(err);
  869. return -1;
  870. }
  871. return 0;
  872. }
  873. int JackCoreAudioDriver::Stop()
  874. {
  875. AudioDeviceStop(fDeviceID, MeasureCallback);
  876. AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
  877. JackLog("JackCoreAudioDriver::Stop\n");
  878. return (AudioOutputUnitStop(fAUHAL) == noErr) ? 0 : -1;
  879. }
  880. int JackCoreAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  881. {
  882. OSStatus err;
  883. UInt32 outSize = sizeof(UInt32);
  884. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
  885. if (err != noErr) {
  886. jack_error("Cannot set buffer size %ld", buffer_size);
  887. printError(err);
  888. return -1;
  889. }
  890. JackAudioDriver::SetBufferSize(buffer_size); // never fails
  891. // Input buffers do no change : prepare them only once
  892. for (int i = 0; i < fCaptureChannels; i++) {
  893. fJackInputData->mBuffers[i].mNumberChannels = 1;
  894. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
  895. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  896. }
  897. return 0;
  898. }
  899. } // end of namespace
  900. #ifdef __cplusplus
  901. extern "C"
  902. {
  903. #endif
  904. jack_driver_desc_t* driver_get_descriptor() {
  905. jack_driver_desc_t *desc;
  906. unsigned int i;
  907. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  908. strcpy(desc->name, "coreaudio");
  909. desc->nparams = 13;
  910. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  911. i = 0;
  912. strcpy(desc->params[i].name, "channels");
  913. desc->params[i].character = 'c';
  914. desc->params[i].type = JackDriverParamInt;
  915. desc->params[i].value.ui = 0;
  916. strcpy(desc->params[i].short_desc, "Maximum number of channels");
  917. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  918. i++;
  919. strcpy(desc->params[i].name, "inchannels");
  920. desc->params[i].character = 'i';
  921. desc->params[i].type = JackDriverParamInt;
  922. desc->params[i].value.ui = 0;
  923. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  924. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  925. i++;
  926. strcpy(desc->params[i].name, "outchannels");
  927. desc->params[i].character = 'o';
  928. desc->params[i].type = JackDriverParamInt;
  929. desc->params[i].value.ui = 0;
  930. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  931. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  932. i++;
  933. strcpy(desc->params[i].name, "capture");
  934. desc->params[i].character = 'C';
  935. desc->params[i].type = JackDriverParamString;
  936. strcpy(desc->params[i].value.str, "will take default CoreAudio input device");
  937. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set CoreAudio device name");
  938. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  939. i++;
  940. strcpy(desc->params[i].name, "playback");
  941. desc->params[i].character = 'P';
  942. desc->params[i].type = JackDriverParamString;
  943. strcpy(desc->params[i].value.str, "will take default CoreAudio output device");
  944. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set CoreAudio device name");
  945. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  946. i++;
  947. strcpy (desc->params[i].name, "monitor");
  948. desc->params[i].character = 'm';
  949. desc->params[i].type = JackDriverParamBool;
  950. desc->params[i].value.i = 0;
  951. strcpy(desc->params[i].short_desc, "Provide monitor ports for the output");
  952. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  953. i++;
  954. strcpy(desc->params[i].name, "duplex");
  955. desc->params[i].character = 'D';
  956. desc->params[i].type = JackDriverParamBool;
  957. desc->params[i].value.i = TRUE;
  958. strcpy(desc->params[i].short_desc, "Provide both capture and playback ports");
  959. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  960. i++;
  961. strcpy(desc->params[i].name, "rate");
  962. desc->params[i].character = 'r';
  963. desc->params[i].type = JackDriverParamUInt;
  964. desc->params[i].value.ui = 44100U;
  965. strcpy(desc->params[i].short_desc, "Sample rate");
  966. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  967. i++;
  968. strcpy(desc->params[i].name, "period");
  969. desc->params[i].character = 'p';
  970. desc->params[i].type = JackDriverParamUInt;
  971. desc->params[i].value.ui = 128U;
  972. strcpy(desc->params[i].short_desc, "Frames per period");
  973. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  974. i++;
  975. strcpy(desc->params[i].name, "device");
  976. desc->params[i].character = 'd';
  977. desc->params[i].type = JackDriverParamString;
  978. desc->params[i].value.ui = 128U;
  979. strcpy(desc->params[i].value.str, "will take default CoreAudio device name");
  980. strcpy(desc->params[i].short_desc, "CoreAudio device name");
  981. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  982. i++;
  983. strcpy(desc->params[i].name, "input-latency");
  984. desc->params[i].character = 'I';
  985. desc->params[i].type = JackDriverParamUInt;
  986. desc->params[i].value.i = 0;
  987. strcpy(desc->params[i].short_desc, "Extra input latency");
  988. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  989. i++;
  990. strcpy(desc->params[i].name, "output-latency");
  991. desc->params[i].character = 'O';
  992. desc->params[i].type = JackDriverParamUInt;
  993. desc->params[i].value.i = 0;
  994. strcpy(desc->params[i].short_desc, "Extra output latency");
  995. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  996. i++;
  997. strcpy(desc->params[i].name, "list-devices");
  998. desc->params[i].character = 'l';
  999. desc->params[i].type = JackDriverParamBool;
  1000. desc->params[i].value.i = TRUE;
  1001. strcpy(desc->params[i].short_desc, "Display available CoreAudio devices");
  1002. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1003. return desc;
  1004. }
  1005. Jack::JackDriverClientInterface* driver_initialize(Jack::JackEngine* engine, Jack::JackSynchro** table, const JSList* params) {
  1006. jack_nframes_t srate = 44100;
  1007. jack_nframes_t frames_per_interrupt = 128;
  1008. int capture = FALSE;
  1009. int playback = FALSE;
  1010. int chan_in = 0;
  1011. int chan_out = 0;
  1012. bool monitor = false;
  1013. char* capture_pcm_name = "";
  1014. char* playback_pcm_name = "";
  1015. const JSList *node;
  1016. const jack_driver_param_t *param;
  1017. jack_nframes_t systemic_input_latency = 0;
  1018. jack_nframes_t systemic_output_latency = 0;
  1019. for (node = params; node; node = jack_slist_next(node)) {
  1020. param = (const jack_driver_param_t *) node->data;
  1021. switch (param->character) {
  1022. case 'd':
  1023. capture_pcm_name = strdup(param->value.str);
  1024. playback_pcm_name = strdup(param->value.str);
  1025. break;
  1026. case 'D':
  1027. capture = TRUE;
  1028. playback = TRUE;
  1029. break;
  1030. case 'c':
  1031. chan_in = chan_out = (int) param->value.ui;
  1032. break;
  1033. case 'i':
  1034. chan_in = (int) param->value.ui;
  1035. break;
  1036. case 'o':
  1037. chan_out = (int) param->value.ui;
  1038. break;
  1039. case 'C':
  1040. capture = TRUE;
  1041. if (strcmp(param->value.str, "none") != 0) {
  1042. capture_pcm_name = strdup(param->value.str);
  1043. }
  1044. break;
  1045. case 'P':
  1046. playback = TRUE;
  1047. if (strcmp(param->value.str, "none") != 0) {
  1048. playback_pcm_name = strdup(param->value.str);
  1049. }
  1050. break;
  1051. case 'm':
  1052. monitor = param->value.i;
  1053. break;
  1054. case 'r':
  1055. srate = param->value.ui;
  1056. break;
  1057. case 'p':
  1058. frames_per_interrupt = (unsigned int) param->value.ui;
  1059. break;
  1060. case 'I':
  1061. systemic_input_latency = param->value.ui;
  1062. break;
  1063. case 'O':
  1064. systemic_output_latency = param->value.ui;
  1065. break;
  1066. case 'l':
  1067. Jack::DisplayDeviceNames();
  1068. break;
  1069. }
  1070. }
  1071. /* duplex is the default */
  1072. if (!capture && !playback) {
  1073. capture = TRUE;
  1074. playback = TRUE;
  1075. }
  1076. Jack::JackDriverClientInterface* driver = new Jack::JackCoreAudioDriver("coreaudio", engine, table);
  1077. 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) {
  1078. return driver;
  1079. } else {
  1080. delete driver;
  1081. return NULL;
  1082. }
  1083. }
  1084. #ifdef __cplusplus
  1085. }
  1086. #endif