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.

606 lines
22KB

  1. /*
  2. Copyright (C) 2008 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 "JackCoreAudioIOAdapter.h"
  16. #include "JackError.h"
  17. #include <unistd.h>
  18. namespace Jack
  19. {
  20. static void printError(OSStatus err)
  21. {
  22. switch (err) {
  23. case kAudioHardwareNoError:
  24. jack_log("error code : kAudioHardwareNoError");
  25. break;
  26. case kAudioConverterErr_FormatNotSupported:
  27. jack_log("error code : kAudioConverterErr_FormatNotSupported");
  28. break;
  29. case kAudioConverterErr_OperationNotSupported:
  30. jack_log("error code : kAudioConverterErr_OperationNotSupported");
  31. break;
  32. case kAudioConverterErr_PropertyNotSupported:
  33. jack_log("error code : kAudioConverterErr_PropertyNotSupported");
  34. break;
  35. case kAudioConverterErr_InvalidInputSize:
  36. jack_log("error code : kAudioConverterErr_InvalidInputSize");
  37. break;
  38. case kAudioConverterErr_InvalidOutputSize:
  39. jack_log("error code : kAudioConverterErr_InvalidOutputSize");
  40. break;
  41. case kAudioConverterErr_UnspecifiedError:
  42. jack_log("error code : kAudioConverterErr_UnspecifiedError");
  43. break;
  44. case kAudioConverterErr_BadPropertySizeError:
  45. jack_log("error code : kAudioConverterErr_BadPropertySizeError");
  46. break;
  47. case kAudioConverterErr_RequiresPacketDescriptionsError:
  48. jack_log("error code : kAudioConverterErr_RequiresPacketDescriptionsError");
  49. break;
  50. case kAudioConverterErr_InputSampleRateOutOfRange:
  51. jack_log("error code : kAudioConverterErr_InputSampleRateOutOfRange");
  52. break;
  53. case kAudioConverterErr_OutputSampleRateOutOfRange:
  54. jack_log("error code : kAudioConverterErr_OutputSampleRateOutOfRange");
  55. break;
  56. case kAudioHardwareNotRunningError:
  57. jack_log("error code : kAudioHardwareNotRunningError");
  58. break;
  59. case kAudioHardwareUnknownPropertyError:
  60. jack_log("error code : kAudioHardwareUnknownPropertyError");
  61. break;
  62. case kAudioHardwareIllegalOperationError:
  63. jack_log("error code : kAudioHardwareIllegalOperationError");
  64. break;
  65. case kAudioHardwareBadDeviceError:
  66. jack_log("error code : kAudioHardwareBadDeviceError");
  67. break;
  68. case kAudioHardwareBadStreamError:
  69. jack_log("error code : kAudioHardwareBadStreamError");
  70. break;
  71. case kAudioDeviceUnsupportedFormatError:
  72. jack_log("error code : kAudioDeviceUnsupportedFormatError");
  73. break;
  74. case kAudioDevicePermissionsError:
  75. jack_log("error code : kAudioDevicePermissionsError");
  76. break;
  77. case kAudioHardwareBadObjectError:
  78. jack_log("error code : kAudioHardwareBadObjectError");
  79. break;
  80. case kAudioHardwareUnsupportedOperationError:
  81. jack_log("error code : kAudioHardwareUnsupportedOperationError");
  82. break;
  83. default:
  84. jack_log("error code : unknown");
  85. break;
  86. }
  87. }
  88. OSStatus JackCoreAudioIOAdapter::SRNotificationCallback(AudioDeviceID inDevice,
  89. UInt32 inChannel,
  90. Boolean isInput,
  91. AudioDevicePropertyID inPropertyID,
  92. void* inClientData)
  93. {
  94. JackCoreAudioIOAdapter* driver = static_cast<JackCoreAudioIOAdapter*>(inClientData);
  95. switch (inPropertyID) {
  96. case kAudioDevicePropertyNominalSampleRate: {
  97. jack_log("JackCoreAudioDriver::SRNotificationCallback kAudioDevicePropertyNominalSampleRate");
  98. driver->fState = true;
  99. break;
  100. }
  101. }
  102. return noErr;
  103. }
  104. OSStatus JackCoreAudioIOAdapter::Render(void *inRefCon,
  105. AudioUnitRenderActionFlags *ioActionFlags,
  106. const AudioTimeStamp *inTimeStamp,
  107. UInt32 inBusNumber,
  108. UInt32 inNumberFrames,
  109. AudioBufferList *ioData)
  110. {
  111. JackCoreAudioIOAdapter* adapter = static_cast<JackCoreAudioIOAdapter*>(inRefCon);
  112. jack_log("JackCoreAudioIOAdapter::Render inNumberFrames %ld", inNumberFrames);
  113. AudioUnitRender(adapter->fAUHAL, ioActionFlags, inTimeStamp, 1, inNumberFrames, adapter->fInputData);
  114. /*
  115. adapter->fLastCallbackTime = adapter->fCurCallbackTime;
  116. adapter->fCurCallbackTime = jack_get_time();
  117. adapter->fConsumerFilter.AddValue(adapter->fCurCallbackTime - adapter->fLastCallbackTime);
  118. adapter->fProducerFilter.AddValue(adapter->fDeltaTime);
  119. */
  120. jack_log("JackCoreAudioIOAdapter::Render delta %ld", adapter->fCurCallbackTime - adapter->fLastCallbackTime);
  121. //printf("JackCoreAudioIOAdapter::Render delta %ld\n", adapter->fCurCallbackTime - adapter->fLastCallbackTime);
  122. if (!adapter->fRunning) {
  123. adapter->fRunning = true;
  124. jack_time_t time = jack_get_time();
  125. adapter->fProducerDLL.Init(time);
  126. adapter->fConsumerDLL.Init(time);
  127. adapter->fCurFrames = 0;
  128. }
  129. // DLL based
  130. //adapter->fConsumerDLL.IncFrame(adapter->fConsumerTime);
  131. jack_time_t time = jack_get_time();
  132. adapter->fProducerDLL.IncFrame(time);
  133. /*
  134. jack_time_t time1 = adapter->fConsumerDLL.CurFrame2Time();
  135. jack_time_t time2 = adapter->fProducerDLL.CurFrame2Time();
  136. */
  137. jack_nframes_t time1 = adapter->fConsumerDLL.Time2Frames(time);
  138. jack_nframes_t time2 = adapter->fProducerDLL.Time2Frames(time);
  139. printf("time1 %ld time2 %ld\n",time1, time2);
  140. double src_ratio_output = double(time2) / double(time1);
  141. double src_ratio_input = double(time1) / double(time2);
  142. /*
  143. jack_time_t val2 = adapter->fConsumerFilter.GetVal();
  144. jack_time_t val1 = adapter->fProducerFilter.GetVal();
  145. double src_ratio_output = double(val1) / double(val2);
  146. double src_ratio_input = double(val2) / double(val1);
  147. */
  148. if (src_ratio_input < 0.8f || src_ratio_input > 1.2f) {
  149. jack_error("src_ratio_input = %f", src_ratio_input);
  150. src_ratio_input = 1;
  151. }
  152. if (src_ratio_output < 0.8f || src_ratio_output > 1.2f) {
  153. jack_error("src_ratio_output = %f", src_ratio_output);
  154. src_ratio_output = 1;
  155. }
  156. src_ratio_input = Range(0.8f, 1.2f, src_ratio_input);
  157. src_ratio_output = Range(0.8f, 1.2f, src_ratio_output);
  158. //jack_log("Callback resampler src_ratio_input = %f src_ratio_output = %f", src_ratio_input, src_ratio_output);
  159. //jack_info("Callback resampler src_ratio_input = %f src_ratio_output = %f", src_ratio_input, src_ratio_output);
  160. printf("Callback resampler src_ratio_input = %f src_ratio_output = %f\n", src_ratio_input, src_ratio_output);
  161. for (int i = 0; i < adapter->fCaptureChannels; i++) {
  162. adapter->fCaptureRingBuffer[i].SetRatio(time1, time2);
  163. adapter->fCaptureRingBuffer[i].WriteResample((float*)adapter->fInputData->mBuffers[i].mData, inNumberFrames);
  164. // adapter->fCaptureRingBuffer[i].Write((float*)adapter->fInputData->mBuffers[i].mData, inNumberFrames);
  165. }
  166. for (int i = 0; i < adapter->fPlaybackChannels; i++) {
  167. adapter->fPlaybackRingBuffer[i].SetRatio(time2, time1);
  168. adapter->fPlaybackRingBuffer[i].ReadResample((float*)ioData->mBuffers[i].mData, inNumberFrames);
  169. // adapter->fPlaybackRingBuffer[i].Read((float*)ioData->mBuffers[i].mData, inNumberFrames);
  170. }
  171. return noErr;
  172. }
  173. OSStatus JackCoreAudioIOAdapter::GetDefaultDevice(AudioDeviceID* id)
  174. {
  175. OSStatus res;
  176. UInt32 theSize = sizeof(UInt32);
  177. AudioDeviceID inDefault;
  178. AudioDeviceID outDefault;
  179. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr)
  180. return res;
  181. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr)
  182. return res;
  183. jack_log("GetDefaultDevice: input = %ld output = %ld", inDefault, outDefault);
  184. // Get the device only if default input and ouput are the same
  185. if (inDefault == outDefault) {
  186. *id = inDefault;
  187. return noErr;
  188. } else {
  189. jack_error("Default input and output devices are not the same !!");
  190. return kAudioHardwareBadDeviceError;
  191. }
  192. }
  193. OSStatus JackCoreAudioIOAdapter::GetTotalChannels(AudioDeviceID device, int* channelCount, bool isInput)
  194. {
  195. OSStatus err = noErr;
  196. UInt32 outSize;
  197. Boolean outWritable;
  198. AudioBufferList* bufferList = 0;
  199. *channelCount = 0;
  200. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
  201. if (err == noErr) {
  202. bufferList = (AudioBufferList*)malloc(outSize);
  203. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
  204. if (err == noErr) {
  205. for (unsigned int i = 0; i < bufferList->mNumberBuffers; i++)
  206. *channelCount += bufferList->mBuffers[i].mNumberChannels;
  207. }
  208. if (bufferList)
  209. free(bufferList);
  210. }
  211. return err;
  212. }
  213. // Setup
  214. int JackCoreAudioIOAdapter::SetupDevices(const char* capture_driver_uid,
  215. const char* playback_driver_uid,
  216. char* capture_driver_name,
  217. char* playback_driver_name)
  218. {
  219. if (GetDefaultDevice(&fDeviceID) != noErr) {
  220. jack_error("Cannot open default device");
  221. return -1;
  222. }
  223. return 0;
  224. }
  225. int JackCoreAudioIOAdapter::SetupChannels(bool capturing,
  226. bool playing,
  227. int& inchannels,
  228. int& outchannels,
  229. int& in_nChannels,
  230. int& out_nChannels,
  231. bool strict)
  232. {
  233. OSStatus err = noErr;
  234. err = GetTotalChannels(fDeviceID, &in_nChannels, true);
  235. if (err != noErr) {
  236. jack_error("Cannot get input channel number");
  237. printError(err);
  238. return -1;
  239. }
  240. err = GetTotalChannels(fDeviceID, &out_nChannels, false);
  241. if (err != noErr) {
  242. jack_error("Cannot get output channel number");
  243. printError(err);
  244. return -1;
  245. }
  246. return 0;
  247. }
  248. int JackCoreAudioIOAdapter::SetupBufferSizeAndSampleRate(jack_nframes_t nframes, jack_nframes_t samplerate)
  249. {
  250. OSStatus err = noErr;
  251. UInt32 outSize;
  252. Float64 sampleRate;
  253. // Setting buffer size
  254. outSize = sizeof(UInt32);
  255. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &nframes);
  256. if (err != noErr) {
  257. jack_error("Cannot set buffer size %ld", nframes);
  258. printError(err);
  259. return -1;
  260. }
  261. // Get sample rate
  262. outSize = sizeof(Float64);
  263. err = AudioDeviceGetProperty(fDeviceID, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
  264. if (err != noErr) {
  265. jack_error("Cannot get current sample rate");
  266. printError(err);
  267. return -1;
  268. }
  269. // If needed, set new sample rate
  270. if (samplerate != (jack_nframes_t)sampleRate) {
  271. sampleRate = (Float64)samplerate;
  272. // To get SR change notification
  273. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback, this);
  274. if (err != noErr) {
  275. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  276. printError(err);
  277. return -1;
  278. }
  279. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate);
  280. if (err != noErr) {
  281. jack_error("Cannot set sample rate = %ld", samplerate);
  282. printError(err);
  283. return -1;
  284. }
  285. // Waiting for SR change notification
  286. int count = 0;
  287. while (!fState && count++ < 100) {
  288. usleep(100000);
  289. jack_log("Wait count = %ld", count);
  290. }
  291. // Remove SR change notification
  292. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback);
  293. }
  294. return 0;
  295. }
  296. int JackCoreAudioIOAdapter::SetupBuffers(int inchannels, int outchannels)
  297. {
  298. jack_log("JackCoreAudioIOAdapter::SetupBuffers: input = %ld output = %ld", inchannels, outchannels);
  299. // Prepare buffers
  300. fInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
  301. if (fInputData == 0) {
  302. jack_error("Cannot allocate memory for input buffers");
  303. return -1;
  304. }
  305. fInputData->mNumberBuffers = inchannels;
  306. for (int i = 0; i < fCaptureChannels; i++) {
  307. fInputData->mBuffers[i].mNumberChannels = 1;
  308. fInputData->mBuffers[i].mDataByteSize = fBufferSize * sizeof(float);
  309. fInputData->mBuffers[i].mData = malloc(fBufferSize * sizeof(float));
  310. }
  311. return 0;
  312. }
  313. void JackCoreAudioIOAdapter::DisposeBuffers()
  314. {
  315. if (fInputData) {
  316. for (int i = 0; i < fCaptureChannels; i++)
  317. free(fInputData->mBuffers[i].mData);
  318. free(fInputData);
  319. fInputData = 0;
  320. }
  321. }
  322. int JackCoreAudioIOAdapter::OpenAUHAL(bool capturing,
  323. bool playing,
  324. int inchannels,
  325. int outchannels,
  326. int in_nChannels,
  327. int out_nChannels,
  328. jack_nframes_t nframes,
  329. jack_nframes_t samplerate,
  330. bool strict)
  331. {
  332. ComponentResult err1;
  333. UInt32 enableIO;
  334. AudioStreamBasicDescription srcFormat, dstFormat;
  335. jack_log("OpenAUHAL capturing = %ld playing = %ld playing = %ld outchannels = %ld in_nChannels = %ld out_nChannels = %ld ", capturing, playing, inchannels, inchannels, in_nChannels, out_nChannels);
  336. // AUHAL
  337. ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  338. Component HALOutput = FindNextComponent(NULL, &cd);
  339. err1 = OpenAComponent(HALOutput, &fAUHAL);
  340. if (err1 != noErr) {
  341. jack_error("Error calling OpenAComponent");
  342. printError(err1);
  343. return -1;
  344. }
  345. err1 = AudioUnitInitialize(fAUHAL);
  346. if (err1 != noErr) {
  347. jack_error("Cannot initialize AUHAL unit");
  348. printError(err1);
  349. return -1;
  350. }
  351. // Start I/O
  352. enableIO = 1;
  353. if (capturing && inchannels > 0) {
  354. jack_log("Setup AUHAL input");
  355. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
  356. if (err1 != noErr) {
  357. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
  358. printError(err1);
  359. if (strict)
  360. return -1;
  361. }
  362. }
  363. if (playing && outchannels > 0) {
  364. jack_log("Setup AUHAL output");
  365. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
  366. if (err1 != noErr) {
  367. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
  368. printError(err1);
  369. if (strict)
  370. return -1;
  371. }
  372. }
  373. // Setup up choosen device, in both input and output cases
  374. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
  375. if (err1 != noErr) {
  376. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
  377. printError(err1);
  378. if (strict)
  379. return -1;
  380. }
  381. // Set buffer size
  382. if (capturing && inchannels > 0) {
  383. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*) & nframes, sizeof(UInt32));
  384. if (err1 != noErr) {
  385. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  386. printError(err1);
  387. if (strict)
  388. return -1;
  389. }
  390. }
  391. if (playing && outchannels > 0) {
  392. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*) & nframes, sizeof(UInt32));
  393. if (err1 != noErr) {
  394. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  395. printError(err1);
  396. if (strict)
  397. return -1;
  398. }
  399. }
  400. // Setup channel map
  401. if (capturing && inchannels > 0 && inchannels < in_nChannels) {
  402. SInt32 chanArr[in_nChannels];
  403. for (int i = 0; i < in_nChannels; i++) {
  404. chanArr[i] = -1;
  405. }
  406. for (int i = 0; i < inchannels; i++) {
  407. chanArr[i] = i;
  408. }
  409. AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
  410. if (err1 != noErr) {
  411. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
  412. printError(err1);
  413. }
  414. }
  415. if (playing && outchannels > 0 && outchannels < out_nChannels) {
  416. SInt32 chanArr[out_nChannels];
  417. for (int i = 0; i < out_nChannels; i++) {
  418. chanArr[i] = -1;
  419. }
  420. for (int i = 0; i < outchannels; i++) {
  421. chanArr[i] = i;
  422. }
  423. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
  424. if (err1 != noErr) {
  425. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
  426. printError(err1);
  427. }
  428. }
  429. // Setup stream converters
  430. jack_log("Setup AUHAL input stream converter SR = %ld", samplerate);
  431. srcFormat.mSampleRate = samplerate;
  432. srcFormat.mFormatID = kAudioFormatLinearPCM;
  433. srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  434. srcFormat.mBytesPerPacket = sizeof(float);
  435. srcFormat.mFramesPerPacket = 1;
  436. srcFormat.mBytesPerFrame = sizeof(float);
  437. srcFormat.mChannelsPerFrame = outchannels;
  438. srcFormat.mBitsPerChannel = 32;
  439. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, sizeof(AudioStreamBasicDescription));
  440. if (err1 != noErr) {
  441. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  442. printError(err1);
  443. }
  444. jack_log("Setup AUHAL output stream converter SR = %ld", samplerate);
  445. dstFormat.mSampleRate = samplerate;
  446. dstFormat.mFormatID = kAudioFormatLinearPCM;
  447. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  448. dstFormat.mBytesPerPacket = sizeof(float);
  449. dstFormat.mFramesPerPacket = 1;
  450. dstFormat.mBytesPerFrame = sizeof(float);
  451. dstFormat.mChannelsPerFrame = inchannels;
  452. dstFormat.mBitsPerChannel = 32;
  453. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, sizeof(AudioStreamBasicDescription));
  454. if (err1 != noErr) {
  455. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  456. printError(err1);
  457. }
  458. // Setup callbacks
  459. if (inchannels > 0 && outchannels == 0) {
  460. AURenderCallbackStruct output;
  461. output.inputProc = Render;
  462. output.inputProcRefCon = this;
  463. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  464. if (err1 != noErr) {
  465. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  466. printError(err1);
  467. return -1;
  468. }
  469. } else {
  470. AURenderCallbackStruct output;
  471. output.inputProc = Render;
  472. output.inputProcRefCon = this;
  473. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  474. if (err1 != noErr) {
  475. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  476. printError(err1);
  477. return -1;
  478. }
  479. }
  480. return 0;
  481. }
  482. void JackCoreAudioIOAdapter::CloseAUHAL()
  483. {
  484. AudioUnitUninitialize(fAUHAL);
  485. CloseComponent(fAUHAL);
  486. }
  487. int JackCoreAudioIOAdapter::Open()
  488. {
  489. OSStatus err;
  490. int in_nChannels = 0;
  491. int out_nChannels = 0;
  492. if (SetupDevices("", "", "", "") < 0)
  493. return -1;
  494. if (SetupChannels(true, true, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, true) < 0)
  495. return -1;
  496. if (SetupBufferSizeAndSampleRate(fBufferSize, fSampleRate) < 0)
  497. return -1;
  498. if (OpenAUHAL(true, true, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, fBufferSize, fSampleRate, true) < 0)
  499. goto error;
  500. if (SetupBuffers(fCaptureChannels, fPlaybackChannels) < 0)
  501. goto error;
  502. err = AudioOutputUnitStart(fAUHAL);
  503. if (err != noErr)
  504. goto error;
  505. return 0;
  506. error:
  507. Close();
  508. return -1;
  509. }
  510. int JackCoreAudioIOAdapter::Close()
  511. {
  512. AudioOutputUnitStop(fAUHAL);
  513. DisposeBuffers();
  514. CloseAUHAL();
  515. return 0;
  516. }
  517. }