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.

1334 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(bool capturing, bool 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(bool capturing,
  522. bool 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. JackLog("OpenAUHAL capturing = %ld playing = %ld playing = %ld outchannels = %ld in_nChannels = %ld out_nChannels = %ld \n", capturing, playing, inchannels, inchannels, in_nChannels, out_nChannels);
  535. // AUHAL
  536. ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  537. Component HALOutput = FindNextComponent(NULL, &cd);
  538. err1 = OpenAComponent(HALOutput, &fAUHAL);
  539. if (err1 != noErr) {
  540. jack_error("Error calling OpenAComponent");
  541. printError(err1);
  542. return -1;
  543. }
  544. err1 = AudioUnitInitialize(fAUHAL);
  545. if (err1 != noErr) {
  546. jack_error("Cannot initialize AUHAL unit");
  547. printError(err1);
  548. return -1;
  549. }
  550. // Start I/O
  551. enableIO = 1;
  552. if (capturing && inchannels > 0) {
  553. JackLog("Setup AUHAL input\n");
  554. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
  555. if (err1 != noErr) {
  556. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
  557. printError(err1);
  558. if (strict)
  559. return -1;
  560. }
  561. }
  562. if (playing && outchannels > 0) {
  563. JackLog("Setup AUHAL output\n");
  564. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
  565. if (err1 != noErr) {
  566. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
  567. printError(err1);
  568. if (strict)
  569. return -1;
  570. }
  571. }
  572. // Setup up choosen device, in both input and output cases
  573. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
  574. if (err1 != noErr) {
  575. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
  576. printError(err1);
  577. if (strict)
  578. return -1;
  579. }
  580. // Set buffer size
  581. if (capturing && inchannels > 0) {
  582. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&nframes, sizeof(UInt32));
  583. if (err1 != noErr) {
  584. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  585. printError(err1);
  586. if (strict)
  587. return -1;
  588. }
  589. }
  590. if (playing && outchannels > 0) {
  591. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&nframes, sizeof(UInt32));
  592. if (err1 != noErr) {
  593. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  594. printError(err1);
  595. if (strict)
  596. return -1;
  597. }
  598. }
  599. // Setup channel map
  600. if (capturing && inchannels > 0 && inchannels < in_nChannels) {
  601. SInt32 chanArr[in_nChannels];
  602. for (int i = 0; i < in_nChannels; i++) {
  603. chanArr[i] = -1;
  604. }
  605. for (int i = 0; i < inchannels; i++) {
  606. chanArr[i] = i;
  607. }
  608. AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
  609. if (err1 != noErr) {
  610. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
  611. printError(err1);
  612. }
  613. }
  614. if (playing && outchannels > 0 && outchannels < out_nChannels) {
  615. SInt32 chanArr[out_nChannels];
  616. for (int i = 0; i < out_nChannels; i++) {
  617. chanArr[i] = -1;
  618. }
  619. for (int i = 0; i < outchannels; i++) {
  620. chanArr[i] = i;
  621. }
  622. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
  623. if (err1 != noErr) {
  624. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
  625. printError(err1);
  626. }
  627. }
  628. // Setup stream converters
  629. JackLog("Setup AUHAL input stream converter SR = %ld\n", samplerate);
  630. srcFormat.mSampleRate = samplerate;
  631. srcFormat.mFormatID = kAudioFormatLinearPCM;
  632. srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  633. srcFormat.mBytesPerPacket = sizeof(float);
  634. srcFormat.mFramesPerPacket = 1;
  635. srcFormat.mBytesPerFrame = sizeof(float);
  636. srcFormat.mChannelsPerFrame = outchannels;
  637. srcFormat.mBitsPerChannel = 32;
  638. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, sizeof(AudioStreamBasicDescription));
  639. if (err1 != noErr) {
  640. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  641. printError(err1);
  642. }
  643. JackLog("Setup AUHAL output stream converter SR = %ld\n", samplerate);
  644. dstFormat.mSampleRate = samplerate;
  645. dstFormat.mFormatID = kAudioFormatLinearPCM;
  646. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  647. dstFormat.mBytesPerPacket = sizeof(float);
  648. dstFormat.mFramesPerPacket = 1;
  649. dstFormat.mBytesPerFrame = sizeof(float);
  650. dstFormat.mChannelsPerFrame = inchannels;
  651. dstFormat.mBitsPerChannel = 32;
  652. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, sizeof(AudioStreamBasicDescription));
  653. if (err1 != noErr) {
  654. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  655. printError(err1);
  656. }
  657. // Setup callbacks
  658. if (inchannels > 0 && outchannels == 0) {
  659. AURenderCallbackStruct output;
  660. output.inputProc = Render;
  661. output.inputProcRefCon = this;
  662. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  663. if (err1 != noErr) {
  664. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  665. printError(err1);
  666. return -1;
  667. }
  668. } else {
  669. AURenderCallbackStruct output;
  670. output.inputProc = Render;
  671. output.inputProcRefCon = this;
  672. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  673. if (err1 != noErr) {
  674. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  675. printError(err1);
  676. return -1;
  677. }
  678. }
  679. return 0;
  680. }
  681. int JackCoreAudioDriver::SetupBuffers(int inchannels, int outchannels)
  682. {
  683. // Prepare buffers
  684. fJackInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
  685. if (fJackInputData == 0) {
  686. jack_error("Cannot allocate memory for input buffers");
  687. return -1;
  688. }
  689. fJackInputData->mNumberBuffers = inchannels;
  690. for (int i = 0; i < fCaptureChannels; i++) {
  691. fJackInputData->mBuffers[i].mNumberChannels = 1;
  692. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
  693. }
  694. return 0;
  695. }
  696. void JackCoreAudioDriver::DisposeBuffers()
  697. {
  698. if (fJackInputData) {
  699. free(fJackInputData);
  700. fJackInputData = 0;
  701. }
  702. }
  703. void JackCoreAudioDriver::CloseAUHAL()
  704. {
  705. AudioUnitUninitialize(fAUHAL);
  706. CloseComponent(fAUHAL);
  707. }
  708. int JackCoreAudioDriver::AddListeners()
  709. {
  710. OSStatus err = noErr;
  711. // Add listeners
  712. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback, this);
  713. if (err != noErr) {
  714. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
  715. printError(err);
  716. return -1;
  717. }
  718. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback, this);
  719. if (err != noErr) {
  720. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioHardwarePropertyDevices");
  721. printError(err);
  722. return -1;
  723. }
  724. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback, this);
  725. if (err != noErr) {
  726. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  727. printError(err);
  728. return -1;
  729. }
  730. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback, this);
  731. if (err != noErr) {
  732. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyDeviceIsRunning");
  733. printError(err);
  734. return -1;
  735. }
  736. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
  737. if (err != noErr) {
  738. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
  739. printError(err);
  740. return -1;
  741. }
  742. err = AudioDeviceAddPropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback, this);
  743. if (err != noErr) {
  744. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
  745. printError(err);
  746. return -1;
  747. }
  748. return 0;
  749. }
  750. void JackCoreAudioDriver::RemoveListeners()
  751. {
  752. AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
  753. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDeviceProcessorOverload, DeviceNotificationCallback);
  754. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioHardwarePropertyDevices, DeviceNotificationCallback);
  755. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, DeviceNotificationCallback);
  756. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyDeviceIsRunning, DeviceNotificationCallback);
  757. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
  758. AudioDeviceRemovePropertyListener(fDeviceID, 0, false, kAudioDevicePropertyStreamConfiguration, DeviceNotificationCallback);
  759. }
  760. int JackCoreAudioDriver::Open(jack_nframes_t nframes,
  761. jack_nframes_t samplerate,
  762. bool capturing,
  763. bool playing,
  764. int inchannels,
  765. int outchannels,
  766. bool monitor,
  767. const char* capture_driver_uid,
  768. const char* playback_driver_uid,
  769. jack_nframes_t capture_latency,
  770. jack_nframes_t playback_latency)
  771. {
  772. int in_nChannels = 0;
  773. int out_nChannels = 0;
  774. char capture_driver_name[256];
  775. char playback_driver_name[256];
  776. // Keep initial state
  777. fCapturing = capturing;
  778. fPlaying = playing;
  779. fInChannels = inchannels;
  780. fOutChannels = outchannels;
  781. fMonitor = monitor;
  782. strcpy(fCaptureUID, capture_driver_uid);
  783. strcpy(fPlaybackUID, playback_driver_uid);
  784. fCaptureLatency = capture_latency;
  785. fPlaybackLatency = playback_latency;
  786. if (SetupDevices(capture_driver_uid, playback_driver_uid, capture_driver_name, playback_driver_name) < 0)
  787. return -1;
  788. // Generic JackAudioDriver Open
  789. if (JackAudioDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency) != 0)
  790. return -1;
  791. if (SetupChannels(capturing, playing, inchannels, outchannels, in_nChannels, out_nChannels, true) < 0)
  792. return -1;
  793. if (SetupBufferSizeAndSampleRate(nframes, samplerate) < 0)
  794. return -1;
  795. if (OpenAUHAL(capturing, playing, inchannels, outchannels, in_nChannels, out_nChannels, nframes, samplerate, true) < 0)
  796. goto error;
  797. if (capturing && inchannels > 0)
  798. if (SetupBuffers(inchannels, outchannels) < 0)
  799. goto error;
  800. if (AddListeners() < 0)
  801. goto error;
  802. // Core driver may have changed the in/out values
  803. fCaptureChannels = inchannels;
  804. fPlaybackChannels = outchannels;
  805. return noErr;
  806. error:
  807. Close();
  808. return -1;
  809. }
  810. int JackCoreAudioDriver::Close()
  811. {
  812. JackLog("JackCoreAudioDriver::Close\n");
  813. Stop();
  814. JackAudioDriver::Close();
  815. RemoveListeners();
  816. DisposeBuffers();
  817. CloseAUHAL();
  818. return 0;
  819. }
  820. int JackCoreAudioDriver::Attach()
  821. {
  822. OSStatus err;
  823. JackPort* port;
  824. jack_port_id_t port_index;
  825. UInt32 size;
  826. Boolean isWritable;
  827. char channel_name[64];
  828. char buf[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  829. unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  830. JackLog("JackCoreAudioDriver::Attach fBufferSize %ld fSampleRate %ld\n", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  831. for (int i = 0; i < fCaptureChannels; i++) {
  832. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, &isWritable);
  833. if (err != noErr)
  834. JackLog("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error \n");
  835. if (err == noErr && size > 0) {
  836. err = AudioDeviceGetProperty(fDeviceID, i + 1, true, kAudioDevicePropertyChannelName, &size, channel_name);
  837. if (err != noErr)
  838. JackLog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
  839. snprintf(buf, sizeof(buf) - 1, "%s:%s:out_%s%u", fClientControl->fName, fCaptureDriverName, channel_name, i + 1);
  840. } else {
  841. snprintf(buf, sizeof(buf) - 1, "%s:%s:out%u", fClientControl->fName, fCaptureDriverName, i + 1);
  842. }
  843. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  844. jack_error("Cannot register port for %s", buf);
  845. return -1;
  846. }
  847. size = sizeof(UInt32);
  848. UInt32 value1 = 0;
  849. UInt32 value2 = 0;
  850. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertyLatency, &size, &value1);
  851. if (err != noErr)
  852. JackLog("AudioDeviceGetProperty kAudioDevicePropertyLatency error \n");
  853. err = AudioDeviceGetProperty(fDeviceID, 0, true, kAudioDevicePropertySafetyOffset, &size, &value2);
  854. if (err != noErr)
  855. JackLog("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error \n");
  856. port = fGraphManager->GetPort(port_index);
  857. port->Rename("system:capture_%d", i + 1);
  858. port->SetLatency(fEngineControl->fBufferSize + value1 + value2 + fCaptureLatency);
  859. fCapturePortList[i] = port_index;
  860. }
  861. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  862. for (int i = 0; i < fPlaybackChannels; i++) {
  863. err = AudioDeviceGetPropertyInfo(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, &isWritable);
  864. if (err != noErr)
  865. JackLog("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error \n");
  866. if (err == noErr && size > 0) {
  867. err = AudioDeviceGetProperty(fDeviceID, i + 1, false, kAudioDevicePropertyChannelName, &size, channel_name);
  868. if (err != noErr)
  869. JackLog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
  870. snprintf(buf, sizeof(buf) - 1, "%s:%s:in_%s%u", fClientControl->fName, fPlaybackDriverName, channel_name, i + 1);
  871. } else {
  872. snprintf(buf, sizeof(buf) - 1, "%s:%s:in%u", fClientControl->fName, fPlaybackDriverName, i + 1);
  873. }
  874. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  875. jack_error("Cannot register port for %s", buf);
  876. return -1;
  877. }
  878. size = sizeof(UInt32);
  879. UInt32 value1 = 0;
  880. UInt32 value2 = 0;
  881. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertyLatency, &size, &value1);
  882. if (err != noErr)
  883. JackLog("AudioDeviceGetProperty kAudioDevicePropertyLatency error \n");
  884. err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertySafetyOffset, &size, &value2);
  885. if (err != noErr)
  886. JackLog("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error \n");
  887. port = fGraphManager->GetPort(port_index);
  888. port->Rename("system:playback_%d", i + 1);
  889. port->SetLatency(fEngineControl->fBufferSize + value1 + value2 + fPlaybackLatency);
  890. fPlaybackPortList[i] = port_index;
  891. // Monitor ports
  892. if (fWithMonitorPorts) {
  893. JackLog("Create monitor port \n");
  894. snprintf(buf, sizeof(buf) - 1, "%s:%s:monitor_%u", fClientControl->fName, fPlaybackDriverName, i + 1);
  895. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize)) == NO_PORT) {
  896. jack_error("Cannot register monitor port for %s", buf);
  897. return -1;
  898. } else {
  899. port = fGraphManager->GetPort(port_index);
  900. port->SetLatency(fEngineControl->fBufferSize);
  901. fMonitorPortList[i] = port_index;
  902. }
  903. }
  904. }
  905. // Input buffers do no change : prepare them only once
  906. for (int i = 0; i < fCaptureChannels; i++) {
  907. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  908. }
  909. return 0;
  910. }
  911. int JackCoreAudioDriver::Start()
  912. {
  913. JackLog("JackCoreAudioDriver::Start\n");
  914. JackAudioDriver::Start();
  915. OSStatus err = AudioDeviceAddIOProc(fDeviceID, MeasureCallback, this);
  916. if (err != noErr)
  917. return -1;
  918. err = AudioOutputUnitStart(fAUHAL);
  919. if (err != noErr)
  920. return -1;
  921. if ((err = AudioDeviceStart(fDeviceID, MeasureCallback)) != noErr) {
  922. jack_error("Cannot start MeasureCallback");
  923. printError(err);
  924. return -1;
  925. }
  926. return 0;
  927. }
  928. int JackCoreAudioDriver::Stop()
  929. {
  930. JackLog("JackCoreAudioDriver::Stop\n");
  931. AudioDeviceStop(fDeviceID, MeasureCallback);
  932. AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
  933. return (AudioOutputUnitStop(fAUHAL) == noErr) ? 0 : -1;
  934. }
  935. int JackCoreAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  936. {
  937. OSStatus err;
  938. UInt32 outSize = sizeof(UInt32);
  939. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &buffer_size);
  940. if (err != noErr) {
  941. jack_error("Cannot set buffer size %ld", buffer_size);
  942. printError(err);
  943. return -1;
  944. }
  945. JackAudioDriver::SetBufferSize(buffer_size); // never fails
  946. // Input buffers do no change : prepare them only once
  947. for (int i = 0; i < fCaptureChannels; i++) {
  948. fJackInputData->mBuffers[i].mNumberChannels = 1;
  949. fJackInputData->mBuffers[i].mDataByteSize = fEngineControl->fBufferSize * sizeof(float);
  950. fJackInputData->mBuffers[i].mData = GetInputBuffer(i);
  951. }
  952. return 0;
  953. }
  954. } // end of namespace
  955. #ifdef __cplusplus
  956. extern "C"
  957. {
  958. #endif
  959. jack_driver_desc_t* driver_get_descriptor() {
  960. jack_driver_desc_t *desc;
  961. unsigned int i;
  962. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  963. strcpy(desc->name, "coreaudio");
  964. desc->nparams = 13;
  965. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  966. i = 0;
  967. strcpy(desc->params[i].name, "channels");
  968. desc->params[i].character = 'c';
  969. desc->params[i].type = JackDriverParamInt;
  970. desc->params[i].value.ui = 0;
  971. strcpy(desc->params[i].short_desc, "Maximum number of channels");
  972. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  973. i++;
  974. strcpy(desc->params[i].name, "inchannels");
  975. desc->params[i].character = 'i';
  976. desc->params[i].type = JackDriverParamInt;
  977. desc->params[i].value.ui = 0;
  978. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  979. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  980. i++;
  981. strcpy(desc->params[i].name, "outchannels");
  982. desc->params[i].character = 'o';
  983. desc->params[i].type = JackDriverParamInt;
  984. desc->params[i].value.ui = 0;
  985. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  986. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  987. i++;
  988. strcpy(desc->params[i].name, "capture");
  989. desc->params[i].character = 'C';
  990. desc->params[i].type = JackDriverParamString;
  991. strcpy(desc->params[i].value.str, "will take default CoreAudio input device");
  992. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set CoreAudio device name");
  993. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  994. i++;
  995. strcpy(desc->params[i].name, "playback");
  996. desc->params[i].character = 'P';
  997. desc->params[i].type = JackDriverParamString;
  998. strcpy(desc->params[i].value.str, "will take default CoreAudio output device");
  999. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set CoreAudio device name");
  1000. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1001. i++;
  1002. strcpy (desc->params[i].name, "monitor");
  1003. desc->params[i].character = 'm';
  1004. desc->params[i].type = JackDriverParamBool;
  1005. desc->params[i].value.i = 0;
  1006. strcpy(desc->params[i].short_desc, "Provide monitor ports for the output");
  1007. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1008. i++;
  1009. strcpy(desc->params[i].name, "duplex");
  1010. desc->params[i].character = 'D';
  1011. desc->params[i].type = JackDriverParamBool;
  1012. desc->params[i].value.i = TRUE;
  1013. strcpy(desc->params[i].short_desc, "Provide both capture and playback ports");
  1014. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1015. i++;
  1016. strcpy(desc->params[i].name, "rate");
  1017. desc->params[i].character = 'r';
  1018. desc->params[i].type = JackDriverParamUInt;
  1019. desc->params[i].value.ui = 44100U;
  1020. strcpy(desc->params[i].short_desc, "Sample rate");
  1021. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1022. i++;
  1023. strcpy(desc->params[i].name, "period");
  1024. desc->params[i].character = 'p';
  1025. desc->params[i].type = JackDriverParamUInt;
  1026. desc->params[i].value.ui = 128U;
  1027. strcpy(desc->params[i].short_desc, "Frames per period");
  1028. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1029. i++;
  1030. strcpy(desc->params[i].name, "device");
  1031. desc->params[i].character = 'd';
  1032. desc->params[i].type = JackDriverParamString;
  1033. desc->params[i].value.ui = 128U;
  1034. strcpy(desc->params[i].value.str, "will take default CoreAudio device name");
  1035. strcpy(desc->params[i].short_desc, "CoreAudio device name");
  1036. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1037. i++;
  1038. strcpy(desc->params[i].name, "input-latency");
  1039. desc->params[i].character = 'I';
  1040. desc->params[i].type = JackDriverParamUInt;
  1041. desc->params[i].value.i = 0;
  1042. strcpy(desc->params[i].short_desc, "Extra input latency");
  1043. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1044. i++;
  1045. strcpy(desc->params[i].name, "output-latency");
  1046. desc->params[i].character = 'O';
  1047. desc->params[i].type = JackDriverParamUInt;
  1048. desc->params[i].value.i = 0;
  1049. strcpy(desc->params[i].short_desc, "Extra output latency");
  1050. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1051. i++;
  1052. strcpy(desc->params[i].name, "list-devices");
  1053. desc->params[i].character = 'l';
  1054. desc->params[i].type = JackDriverParamBool;
  1055. desc->params[i].value.i = TRUE;
  1056. strcpy(desc->params[i].short_desc, "Display available CoreAudio devices");
  1057. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  1058. return desc;
  1059. }
  1060. Jack::JackDriverClientInterface* driver_initialize(Jack::JackEngine* engine, Jack::JackSynchro** table, const JSList* params) {
  1061. jack_nframes_t srate = 44100;
  1062. jack_nframes_t frames_per_interrupt = 128;
  1063. int capture = FALSE;
  1064. int playback = FALSE;
  1065. int chan_in = 0;
  1066. int chan_out = 0;
  1067. bool monitor = false;
  1068. char* capture_pcm_name = "";
  1069. char* playback_pcm_name = "";
  1070. const JSList *node;
  1071. const jack_driver_param_t *param;
  1072. jack_nframes_t systemic_input_latency = 0;
  1073. jack_nframes_t systemic_output_latency = 0;
  1074. for (node = params; node; node = jack_slist_next(node)) {
  1075. param = (const jack_driver_param_t *) node->data;
  1076. switch (param->character) {
  1077. case 'd':
  1078. capture_pcm_name = strdup(param->value.str);
  1079. playback_pcm_name = strdup(param->value.str);
  1080. break;
  1081. case 'D':
  1082. capture = TRUE;
  1083. playback = TRUE;
  1084. break;
  1085. case 'c':
  1086. chan_in = chan_out = (int) param->value.ui;
  1087. break;
  1088. case 'i':
  1089. chan_in = (int) param->value.ui;
  1090. break;
  1091. case 'o':
  1092. chan_out = (int) param->value.ui;
  1093. break;
  1094. case 'C':
  1095. capture = TRUE;
  1096. if (strcmp(param->value.str, "none") != 0) {
  1097. capture_pcm_name = strdup(param->value.str);
  1098. }
  1099. break;
  1100. case 'P':
  1101. playback = TRUE;
  1102. if (strcmp(param->value.str, "none") != 0) {
  1103. playback_pcm_name = strdup(param->value.str);
  1104. }
  1105. break;
  1106. case 'm':
  1107. monitor = param->value.i;
  1108. break;
  1109. case 'r':
  1110. srate = param->value.ui;
  1111. break;
  1112. case 'p':
  1113. frames_per_interrupt = (unsigned int) param->value.ui;
  1114. break;
  1115. case 'I':
  1116. systemic_input_latency = param->value.ui;
  1117. break;
  1118. case 'O':
  1119. systemic_output_latency = param->value.ui;
  1120. break;
  1121. case 'l':
  1122. Jack::DisplayDeviceNames();
  1123. break;
  1124. }
  1125. }
  1126. /* duplex is the default */
  1127. if (!capture && !playback) {
  1128. capture = TRUE;
  1129. playback = TRUE;
  1130. }
  1131. Jack::JackDriverClientInterface* driver = new Jack::JackCoreAudioDriver("coreaudio", engine, table);
  1132. 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) {
  1133. return driver;
  1134. } else {
  1135. delete driver;
  1136. return NULL;
  1137. }
  1138. }
  1139. #ifdef __cplusplus
  1140. }
  1141. #endif