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.

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