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.

1332 lines
48KB

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