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.

607 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. adapter->fCurFrames += inNumberFrames;
  134. /*
  135. jack_time_t time1 = adapter->fConsumerDLL.CurFrame2Time();
  136. jack_time_t time2 = adapter->fProducerDLL.CurFrame2Time();
  137. */
  138. jack_nframes_t time1 = adapter->fConsumerDLL.Time2Frames(time);
  139. jack_nframes_t time2 = adapter->fProducerDLL.Time2Frames(time);
  140. printf("time1 %ld time2 %ld\n",time1, time2);
  141. double src_ratio_output = double(time2) / double(time1);
  142. double src_ratio_input = double(time1) / double(time2);
  143. /*
  144. jack_time_t val2 = adapter->fConsumerFilter.GetVal();
  145. jack_time_t val1 = adapter->fProducerFilter.GetVal();
  146. double src_ratio_output = double(val1) / double(val2);
  147. double src_ratio_input = double(val2) / double(val1);
  148. */
  149. if (src_ratio_input < 0.8f || src_ratio_input > 1.2f) {
  150. jack_error("src_ratio_input = %f", src_ratio_input);
  151. src_ratio_input = 1;
  152. }
  153. if (src_ratio_output < 0.8f || src_ratio_output > 1.2f) {
  154. jack_error("src_ratio_output = %f", src_ratio_output);
  155. src_ratio_output = 1;
  156. }
  157. src_ratio_input = Range(0.8f, 1.2f, src_ratio_input);
  158. src_ratio_output = Range(0.8f, 1.2f, src_ratio_output);
  159. //jack_log("Callback resampler src_ratio_input = %f src_ratio_output = %f", src_ratio_input, src_ratio_output);
  160. //jack_info("Callback resampler src_ratio_input = %f src_ratio_output = %f", src_ratio_input, src_ratio_output);
  161. printf("Callback resampler src_ratio_input = %f src_ratio_output = %f\n", src_ratio_input, src_ratio_output);
  162. for (int i = 0; i < adapter->fCaptureChannels; i++) {
  163. adapter->fCaptureRingBuffer[i].SetRatio(src_ratio_input);
  164. adapter->fCaptureRingBuffer[i].WriteResample((float*)adapter->fInputData->mBuffers[i].mData, inNumberFrames);
  165. // adapter->fCaptureRingBuffer[i].Write((float*)adapter->fInputData->mBuffers[i].mData, inNumberFrames);
  166. }
  167. for (int i = 0; i < adapter->fPlaybackChannels; i++) {
  168. adapter->fPlaybackRingBuffer[i].SetRatio(src_ratio_output);
  169. adapter->fPlaybackRingBuffer[i].ReadResample((float*)ioData->mBuffers[i].mData, inNumberFrames);
  170. // adapter->fPlaybackRingBuffer[i].Read((float*)ioData->mBuffers[i].mData, inNumberFrames);
  171. }
  172. return noErr;
  173. }
  174. OSStatus JackCoreAudioIOAdapter::GetDefaultDevice(AudioDeviceID* id)
  175. {
  176. OSStatus res;
  177. UInt32 theSize = sizeof(UInt32);
  178. AudioDeviceID inDefault;
  179. AudioDeviceID outDefault;
  180. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &theSize, &inDefault)) != noErr)
  181. return res;
  182. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &theSize, &outDefault)) != noErr)
  183. return res;
  184. jack_log("GetDefaultDevice: input = %ld output = %ld", inDefault, outDefault);
  185. // Get the device only if default input and ouput are the same
  186. if (inDefault == outDefault) {
  187. *id = inDefault;
  188. return noErr;
  189. } else {
  190. jack_error("Default input and output devices are not the same !!");
  191. return kAudioHardwareBadDeviceError;
  192. }
  193. }
  194. OSStatus JackCoreAudioIOAdapter::GetTotalChannels(AudioDeviceID device, int* channelCount, bool isInput)
  195. {
  196. OSStatus err = noErr;
  197. UInt32 outSize;
  198. Boolean outWritable;
  199. AudioBufferList* bufferList = 0;
  200. *channelCount = 0;
  201. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
  202. if (err == noErr) {
  203. bufferList = (AudioBufferList*)malloc(outSize);
  204. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
  205. if (err == noErr) {
  206. for (unsigned int i = 0; i < bufferList->mNumberBuffers; i++)
  207. *channelCount += bufferList->mBuffers[i].mNumberChannels;
  208. }
  209. if (bufferList)
  210. free(bufferList);
  211. }
  212. return err;
  213. }
  214. // Setup
  215. int JackCoreAudioIOAdapter::SetupDevices(const char* capture_driver_uid,
  216. const char* playback_driver_uid,
  217. char* capture_driver_name,
  218. char* playback_driver_name)
  219. {
  220. if (GetDefaultDevice(&fDeviceID) != noErr) {
  221. jack_error("Cannot open default device");
  222. return -1;
  223. }
  224. return 0;
  225. }
  226. int JackCoreAudioIOAdapter::SetupChannels(bool capturing,
  227. bool playing,
  228. int& inchannels,
  229. int& outchannels,
  230. int& in_nChannels,
  231. int& out_nChannels,
  232. bool strict)
  233. {
  234. OSStatus err = noErr;
  235. err = GetTotalChannels(fDeviceID, &in_nChannels, true);
  236. if (err != noErr) {
  237. jack_error("Cannot get input channel number");
  238. printError(err);
  239. return -1;
  240. }
  241. err = GetTotalChannels(fDeviceID, &out_nChannels, false);
  242. if (err != noErr) {
  243. jack_error("Cannot get output channel number");
  244. printError(err);
  245. return -1;
  246. }
  247. return 0;
  248. }
  249. int JackCoreAudioIOAdapter::SetupBufferSizeAndSampleRate(jack_nframes_t nframes, jack_nframes_t samplerate)
  250. {
  251. OSStatus err = noErr;
  252. UInt32 outSize;
  253. Float64 sampleRate;
  254. // Setting buffer size
  255. outSize = sizeof(UInt32);
  256. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &nframes);
  257. if (err != noErr) {
  258. jack_error("Cannot set buffer size %ld", nframes);
  259. printError(err);
  260. return -1;
  261. }
  262. // Get sample rate
  263. outSize = sizeof(Float64);
  264. err = AudioDeviceGetProperty(fDeviceID, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
  265. if (err != noErr) {
  266. jack_error("Cannot get current sample rate");
  267. printError(err);
  268. return -1;
  269. }
  270. // If needed, set new sample rate
  271. if (samplerate != (jack_nframes_t)sampleRate) {
  272. sampleRate = (Float64)samplerate;
  273. // To get SR change notification
  274. err = AudioDeviceAddPropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback, this);
  275. if (err != noErr) {
  276. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  277. printError(err);
  278. return -1;
  279. }
  280. err = AudioDeviceSetProperty(fDeviceID, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate);
  281. if (err != noErr) {
  282. jack_error("Cannot set sample rate = %ld", samplerate);
  283. printError(err);
  284. return -1;
  285. }
  286. // Waiting for SR change notification
  287. int count = 0;
  288. while (!fState && count++ < 100) {
  289. usleep(100000);
  290. jack_log("Wait count = %ld", count);
  291. }
  292. // Remove SR change notification
  293. AudioDeviceRemovePropertyListener(fDeviceID, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback);
  294. }
  295. return 0;
  296. }
  297. int JackCoreAudioIOAdapter::SetupBuffers(int inchannels, int outchannels)
  298. {
  299. jack_log("JackCoreAudioIOAdapter::SetupBuffers: input = %ld output = %ld", inchannels, outchannels);
  300. // Prepare buffers
  301. fInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
  302. if (fInputData == 0) {
  303. jack_error("Cannot allocate memory for input buffers");
  304. return -1;
  305. }
  306. fInputData->mNumberBuffers = inchannels;
  307. for (int i = 0; i < fCaptureChannels; i++) {
  308. fInputData->mBuffers[i].mNumberChannels = 1;
  309. fInputData->mBuffers[i].mDataByteSize = fBufferSize * sizeof(float);
  310. fInputData->mBuffers[i].mData = malloc(fBufferSize * sizeof(float));
  311. }
  312. return 0;
  313. }
  314. void JackCoreAudioIOAdapter::DisposeBuffers()
  315. {
  316. if (fInputData) {
  317. for (int i = 0; i < fCaptureChannels; i++)
  318. free(fInputData->mBuffers[i].mData);
  319. free(fInputData);
  320. fInputData = 0;
  321. }
  322. }
  323. int JackCoreAudioIOAdapter::OpenAUHAL(bool capturing,
  324. bool playing,
  325. int inchannels,
  326. int outchannels,
  327. int in_nChannels,
  328. int out_nChannels,
  329. jack_nframes_t nframes,
  330. jack_nframes_t samplerate,
  331. bool strict)
  332. {
  333. ComponentResult err1;
  334. UInt32 enableIO;
  335. AudioStreamBasicDescription srcFormat, dstFormat;
  336. 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);
  337. // AUHAL
  338. ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  339. Component HALOutput = FindNextComponent(NULL, &cd);
  340. err1 = OpenAComponent(HALOutput, &fAUHAL);
  341. if (err1 != noErr) {
  342. jack_error("Error calling OpenAComponent");
  343. printError(err1);
  344. return -1;
  345. }
  346. err1 = AudioUnitInitialize(fAUHAL);
  347. if (err1 != noErr) {
  348. jack_error("Cannot initialize AUHAL unit");
  349. printError(err1);
  350. return -1;
  351. }
  352. // Start I/O
  353. enableIO = 1;
  354. if (capturing && inchannels > 0) {
  355. jack_log("Setup AUHAL input");
  356. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
  357. if (err1 != noErr) {
  358. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
  359. printError(err1);
  360. if (strict)
  361. return -1;
  362. }
  363. }
  364. if (playing && outchannels > 0) {
  365. jack_log("Setup AUHAL output");
  366. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
  367. if (err1 != noErr) {
  368. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
  369. printError(err1);
  370. if (strict)
  371. return -1;
  372. }
  373. }
  374. // Setup up choosen device, in both input and output cases
  375. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID));
  376. if (err1 != noErr) {
  377. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
  378. printError(err1);
  379. if (strict)
  380. return -1;
  381. }
  382. // Set buffer size
  383. if (capturing && inchannels > 0) {
  384. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*) & nframes, sizeof(UInt32));
  385. if (err1 != noErr) {
  386. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  387. printError(err1);
  388. if (strict)
  389. return -1;
  390. }
  391. }
  392. if (playing && outchannels > 0) {
  393. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*) & nframes, sizeof(UInt32));
  394. if (err1 != noErr) {
  395. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  396. printError(err1);
  397. if (strict)
  398. return -1;
  399. }
  400. }
  401. // Setup channel map
  402. if (capturing && inchannels > 0 && inchannels < in_nChannels) {
  403. SInt32 chanArr[in_nChannels];
  404. for (int i = 0; i < in_nChannels; i++) {
  405. chanArr[i] = -1;
  406. }
  407. for (int i = 0; i < inchannels; i++) {
  408. chanArr[i] = i;
  409. }
  410. AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
  411. if (err1 != noErr) {
  412. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
  413. printError(err1);
  414. }
  415. }
  416. if (playing && outchannels > 0 && outchannels < out_nChannels) {
  417. SInt32 chanArr[out_nChannels];
  418. for (int i = 0; i < out_nChannels; i++) {
  419. chanArr[i] = -1;
  420. }
  421. for (int i = 0; i < outchannels; i++) {
  422. chanArr[i] = i;
  423. }
  424. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
  425. if (err1 != noErr) {
  426. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
  427. printError(err1);
  428. }
  429. }
  430. // Setup stream converters
  431. jack_log("Setup AUHAL input stream converter SR = %ld", samplerate);
  432. srcFormat.mSampleRate = samplerate;
  433. srcFormat.mFormatID = kAudioFormatLinearPCM;
  434. srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  435. srcFormat.mBytesPerPacket = sizeof(float);
  436. srcFormat.mFramesPerPacket = 1;
  437. srcFormat.mBytesPerFrame = sizeof(float);
  438. srcFormat.mChannelsPerFrame = outchannels;
  439. srcFormat.mBitsPerChannel = 32;
  440. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, sizeof(AudioStreamBasicDescription));
  441. if (err1 != noErr) {
  442. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  443. printError(err1);
  444. }
  445. jack_log("Setup AUHAL output stream converter SR = %ld", samplerate);
  446. dstFormat.mSampleRate = samplerate;
  447. dstFormat.mFormatID = kAudioFormatLinearPCM;
  448. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  449. dstFormat.mBytesPerPacket = sizeof(float);
  450. dstFormat.mFramesPerPacket = 1;
  451. dstFormat.mBytesPerFrame = sizeof(float);
  452. dstFormat.mChannelsPerFrame = inchannels;
  453. dstFormat.mBitsPerChannel = 32;
  454. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, sizeof(AudioStreamBasicDescription));
  455. if (err1 != noErr) {
  456. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  457. printError(err1);
  458. }
  459. // Setup callbacks
  460. if (inchannels > 0 && outchannels == 0) {
  461. AURenderCallbackStruct output;
  462. output.inputProc = Render;
  463. output.inputProcRefCon = this;
  464. err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  465. if (err1 != noErr) {
  466. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  467. printError(err1);
  468. return -1;
  469. }
  470. } else {
  471. AURenderCallbackStruct output;
  472. output.inputProc = Render;
  473. output.inputProcRefCon = this;
  474. err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  475. if (err1 != noErr) {
  476. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  477. printError(err1);
  478. return -1;
  479. }
  480. }
  481. return 0;
  482. }
  483. void JackCoreAudioIOAdapter::CloseAUHAL()
  484. {
  485. AudioUnitUninitialize(fAUHAL);
  486. CloseComponent(fAUHAL);
  487. }
  488. int JackCoreAudioIOAdapter::Open()
  489. {
  490. OSStatus err;
  491. int in_nChannels = 0;
  492. int out_nChannels = 0;
  493. if (SetupDevices("", "", "", "") < 0)
  494. return -1;
  495. if (SetupChannels(true, true, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, true) < 0)
  496. return -1;
  497. if (SetupBufferSizeAndSampleRate(fBufferSize, fSampleRate) < 0)
  498. return -1;
  499. if (OpenAUHAL(true, true, fCaptureChannels, fPlaybackChannels, in_nChannels, out_nChannels, fBufferSize, fSampleRate, true) < 0)
  500. goto error;
  501. if (SetupBuffers(fCaptureChannels, fPlaybackChannels) < 0)
  502. goto error;
  503. err = AudioOutputUnitStart(fAUHAL);
  504. if (err != noErr)
  505. goto error;
  506. return 0;
  507. error:
  508. Close();
  509. return -1;
  510. }
  511. int JackCoreAudioIOAdapter::Close()
  512. {
  513. AudioOutputUnitStop(fAUHAL);
  514. DisposeBuffers();
  515. CloseAUHAL();
  516. return 0;
  517. }
  518. }