coreaudio.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <alloca.h>
  25. #include "alMain.h"
  26. #include "alu.h"
  27. #include <CoreServices/CoreServices.h>
  28. #include <unistd.h>
  29. #include <AudioUnit/AudioUnit.h>
  30. #include <AudioToolbox/AudioToolbox.h>
  31. #include "backends/base.h"
  32. typedef struct {
  33. AudioUnit audioUnit;
  34. ALuint frameSize;
  35. ALdouble sampleRateRatio; // Ratio of hardware sample rate / requested sample rate
  36. AudioStreamBasicDescription format; // This is the OpenAL format as a CoreAudio ASBD
  37. AudioConverterRef audioConverter; // Sample rate converter if needed
  38. AudioBufferList *bufferList; // Buffer for data coming from the input device
  39. ALCvoid *resampleBuffer; // Buffer for returned RingBuffer data when resampling
  40. ll_ringbuffer_t *ring;
  41. } ca_data;
  42. static const ALCchar ca_device[] = "CoreAudio Default";
  43. static AudioBufferList* allocate_buffer_list(UInt32 channelCount, UInt32 byteSize)
  44. {
  45. AudioBufferList *list;
  46. list = calloc(1, sizeof(AudioBufferList) + sizeof(AudioBuffer));
  47. if(list)
  48. {
  49. list->mNumberBuffers = 1;
  50. list->mBuffers[0].mNumberChannels = channelCount;
  51. list->mBuffers[0].mDataByteSize = byteSize;
  52. list->mBuffers[0].mData = malloc(byteSize);
  53. if(list->mBuffers[0].mData == NULL)
  54. {
  55. free(list);
  56. list = NULL;
  57. }
  58. }
  59. return list;
  60. }
  61. static void destroy_buffer_list(AudioBufferList* list)
  62. {
  63. if(list)
  64. {
  65. UInt32 i;
  66. for(i = 0;i < list->mNumberBuffers;i++)
  67. free(list->mBuffers[i].mData);
  68. free(list);
  69. }
  70. }
  71. typedef struct ALCcoreAudioPlayback {
  72. DERIVE_FROM_TYPE(ALCbackend);
  73. AudioUnit audioUnit;
  74. ALuint frameSize;
  75. AudioStreamBasicDescription format; // This is the OpenAL format as a CoreAudio ASBD
  76. } ALCcoreAudioPlayback;
  77. static void ALCcoreAudioPlayback_Construct(ALCcoreAudioPlayback *self, ALCdevice *device);
  78. static void ALCcoreAudioPlayback_Destruct(ALCcoreAudioPlayback *self);
  79. static ALCenum ALCcoreAudioPlayback_open(ALCcoreAudioPlayback *self, const ALCchar *name);
  80. static void ALCcoreAudioPlayback_close(ALCcoreAudioPlayback *self);
  81. static ALCboolean ALCcoreAudioPlayback_reset(ALCcoreAudioPlayback *self);
  82. static ALCboolean ALCcoreAudioPlayback_start(ALCcoreAudioPlayback *self);
  83. static void ALCcoreAudioPlayback_stop(ALCcoreAudioPlayback *self);
  84. static DECLARE_FORWARD2(ALCcoreAudioPlayback, ALCbackend, ALCenum, captureSamples, void*, ALCuint)
  85. static DECLARE_FORWARD(ALCcoreAudioPlayback, ALCbackend, ALCuint, availableSamples)
  86. static DECLARE_FORWARD(ALCcoreAudioPlayback, ALCbackend, ClockLatency, getClockLatency)
  87. static DECLARE_FORWARD(ALCcoreAudioPlayback, ALCbackend, void, lock)
  88. static DECLARE_FORWARD(ALCcoreAudioPlayback, ALCbackend, void, unlock)
  89. DECLARE_DEFAULT_ALLOCATORS(ALCcoreAudioPlayback)
  90. DEFINE_ALCBACKEND_VTABLE(ALCcoreAudioPlayback);
  91. static void ALCcoreAudioPlayback_Construct(ALCcoreAudioPlayback *self, ALCdevice *device)
  92. {
  93. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  94. SET_VTABLE2(ALCcoreAudioPlayback, ALCbackend, self);
  95. self->frameSize = 0;
  96. memset(&self->format, 0, sizeof(self->format));
  97. }
  98. static void ALCcoreAudioPlayback_Destruct(ALCcoreAudioPlayback *self)
  99. {
  100. ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
  101. }
  102. static OSStatus ALCcoreAudioPlayback_MixerProc(void *inRefCon,
  103. AudioUnitRenderActionFlags* UNUSED(ioActionFlags), const AudioTimeStamp* UNUSED(inTimeStamp),
  104. UInt32 UNUSED(inBusNumber), UInt32 UNUSED(inNumberFrames), AudioBufferList *ioData)
  105. {
  106. ALCcoreAudioPlayback *self = inRefCon;
  107. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  108. ALCdevice_Lock(device);
  109. aluMixData(device, ioData->mBuffers[0].mData,
  110. ioData->mBuffers[0].mDataByteSize / self->frameSize);
  111. ALCdevice_Unlock(device);
  112. return noErr;
  113. }
  114. static ALCenum ALCcoreAudioPlayback_open(ALCcoreAudioPlayback *self, const ALCchar *name)
  115. {
  116. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  117. AudioComponentDescription desc;
  118. AudioComponent comp;
  119. OSStatus err;
  120. if(!name)
  121. name = ca_device;
  122. else if(strcmp(name, ca_device) != 0)
  123. return ALC_INVALID_VALUE;
  124. /* open the default output unit */
  125. desc.componentType = kAudioUnitType_Output;
  126. desc.componentSubType = kAudioUnitSubType_DefaultOutput;
  127. desc.componentManufacturer = kAudioUnitManufacturer_Apple;
  128. desc.componentFlags = 0;
  129. desc.componentFlagsMask = 0;
  130. comp = AudioComponentFindNext(NULL, &desc);
  131. if(comp == NULL)
  132. {
  133. ERR("AudioComponentFindNext failed\n");
  134. return ALC_INVALID_VALUE;
  135. }
  136. err = AudioComponentInstanceNew(comp, &self->audioUnit);
  137. if(err != noErr)
  138. {
  139. ERR("AudioComponentInstanceNew failed\n");
  140. return ALC_INVALID_VALUE;
  141. }
  142. /* init and start the default audio unit... */
  143. err = AudioUnitInitialize(self->audioUnit);
  144. if(err != noErr)
  145. {
  146. ERR("AudioUnitInitialize failed\n");
  147. AudioComponentInstanceDispose(self->audioUnit);
  148. return ALC_INVALID_VALUE;
  149. }
  150. alstr_copy_cstr(&device->DeviceName, name);
  151. return ALC_NO_ERROR;
  152. }
  153. static void ALCcoreAudioPlayback_close(ALCcoreAudioPlayback *self)
  154. {
  155. AudioUnitUninitialize(self->audioUnit);
  156. AudioComponentInstanceDispose(self->audioUnit);
  157. }
  158. static ALCboolean ALCcoreAudioPlayback_reset(ALCcoreAudioPlayback *self)
  159. {
  160. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  161. AudioStreamBasicDescription streamFormat;
  162. AURenderCallbackStruct input;
  163. OSStatus err;
  164. UInt32 size;
  165. err = AudioUnitUninitialize(self->audioUnit);
  166. if(err != noErr)
  167. ERR("-- AudioUnitUninitialize failed.\n");
  168. /* retrieve default output unit's properties (output side) */
  169. size = sizeof(AudioStreamBasicDescription);
  170. err = AudioUnitGetProperty(self->audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &streamFormat, &size);
  171. if(err != noErr || size != sizeof(AudioStreamBasicDescription))
  172. {
  173. ERR("AudioUnitGetProperty failed\n");
  174. return ALC_FALSE;
  175. }
  176. #if 0
  177. TRACE("Output streamFormat of default output unit -\n");
  178. TRACE(" streamFormat.mFramesPerPacket = %d\n", streamFormat.mFramesPerPacket);
  179. TRACE(" streamFormat.mChannelsPerFrame = %d\n", streamFormat.mChannelsPerFrame);
  180. TRACE(" streamFormat.mBitsPerChannel = %d\n", streamFormat.mBitsPerChannel);
  181. TRACE(" streamFormat.mBytesPerPacket = %d\n", streamFormat.mBytesPerPacket);
  182. TRACE(" streamFormat.mBytesPerFrame = %d\n", streamFormat.mBytesPerFrame);
  183. TRACE(" streamFormat.mSampleRate = %5.0f\n", streamFormat.mSampleRate);
  184. #endif
  185. /* set default output unit's input side to match output side */
  186. err = AudioUnitSetProperty(self->audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &streamFormat, size);
  187. if(err != noErr)
  188. {
  189. ERR("AudioUnitSetProperty failed\n");
  190. return ALC_FALSE;
  191. }
  192. if(device->Frequency != streamFormat.mSampleRate)
  193. {
  194. device->NumUpdates = (ALuint)((ALuint64)device->NumUpdates *
  195. streamFormat.mSampleRate /
  196. device->Frequency);
  197. device->Frequency = streamFormat.mSampleRate;
  198. }
  199. /* FIXME: How to tell what channels are what in the output device, and how
  200. * to specify what we're giving? eg, 6.0 vs 5.1 */
  201. switch(streamFormat.mChannelsPerFrame)
  202. {
  203. case 1:
  204. device->FmtChans = DevFmtMono;
  205. break;
  206. case 2:
  207. device->FmtChans = DevFmtStereo;
  208. break;
  209. case 4:
  210. device->FmtChans = DevFmtQuad;
  211. break;
  212. case 6:
  213. device->FmtChans = DevFmtX51;
  214. break;
  215. case 7:
  216. device->FmtChans = DevFmtX61;
  217. break;
  218. case 8:
  219. device->FmtChans = DevFmtX71;
  220. break;
  221. default:
  222. ERR("Unhandled channel count (%d), using Stereo\n", streamFormat.mChannelsPerFrame);
  223. device->FmtChans = DevFmtStereo;
  224. streamFormat.mChannelsPerFrame = 2;
  225. break;
  226. }
  227. SetDefaultWFXChannelOrder(device);
  228. /* use channel count and sample rate from the default output unit's current
  229. * parameters, but reset everything else */
  230. streamFormat.mFramesPerPacket = 1;
  231. streamFormat.mFormatFlags = 0;
  232. switch(device->FmtType)
  233. {
  234. case DevFmtUByte:
  235. device->FmtType = DevFmtByte;
  236. /* fall-through */
  237. case DevFmtByte:
  238. streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
  239. streamFormat.mBitsPerChannel = 8;
  240. break;
  241. case DevFmtUShort:
  242. device->FmtType = DevFmtShort;
  243. /* fall-through */
  244. case DevFmtShort:
  245. streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
  246. streamFormat.mBitsPerChannel = 16;
  247. break;
  248. case DevFmtUInt:
  249. device->FmtType = DevFmtInt;
  250. /* fall-through */
  251. case DevFmtInt:
  252. streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
  253. streamFormat.mBitsPerChannel = 32;
  254. break;
  255. case DevFmtFloat:
  256. streamFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat;
  257. streamFormat.mBitsPerChannel = 32;
  258. break;
  259. }
  260. streamFormat.mBytesPerFrame = streamFormat.mChannelsPerFrame *
  261. streamFormat.mBitsPerChannel / 8;
  262. streamFormat.mBytesPerPacket = streamFormat.mBytesPerFrame;
  263. streamFormat.mFormatID = kAudioFormatLinearPCM;
  264. streamFormat.mFormatFlags |= kAudioFormatFlagsNativeEndian |
  265. kLinearPCMFormatFlagIsPacked;
  266. err = AudioUnitSetProperty(self->audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &streamFormat, sizeof(AudioStreamBasicDescription));
  267. if(err != noErr)
  268. {
  269. ERR("AudioUnitSetProperty failed\n");
  270. return ALC_FALSE;
  271. }
  272. /* setup callback */
  273. self->frameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType, device->AmbiOrder);
  274. input.inputProc = ALCcoreAudioPlayback_MixerProc;
  275. input.inputProcRefCon = self;
  276. err = AudioUnitSetProperty(self->audioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &input, sizeof(AURenderCallbackStruct));
  277. if(err != noErr)
  278. {
  279. ERR("AudioUnitSetProperty failed\n");
  280. return ALC_FALSE;
  281. }
  282. /* init the default audio unit... */
  283. err = AudioUnitInitialize(self->audioUnit);
  284. if(err != noErr)
  285. {
  286. ERR("AudioUnitInitialize failed\n");
  287. return ALC_FALSE;
  288. }
  289. return ALC_TRUE;
  290. }
  291. static ALCboolean ALCcoreAudioPlayback_start(ALCcoreAudioPlayback *self)
  292. {
  293. OSStatus err = AudioOutputUnitStart(self->audioUnit);
  294. if(err != noErr)
  295. {
  296. ERR("AudioOutputUnitStart failed\n");
  297. return ALC_FALSE;
  298. }
  299. return ALC_TRUE;
  300. }
  301. static void ALCcoreAudioPlayback_stop(ALCcoreAudioPlayback *self)
  302. {
  303. OSStatus err = AudioOutputUnitStop(self->audioUnit);
  304. if(err != noErr)
  305. ERR("AudioOutputUnitStop failed\n");
  306. }
  307. typedef struct ALCcoreAudioCapture {
  308. DERIVE_FROM_TYPE(ALCbackend);
  309. AudioUnit audioUnit;
  310. ALuint frameSize;
  311. ALdouble sampleRateRatio; // Ratio of hardware sample rate / requested sample rate
  312. AudioStreamBasicDescription format; // This is the OpenAL format as a CoreAudio ASBD
  313. AudioConverterRef audioConverter; // Sample rate converter if needed
  314. AudioBufferList *bufferList; // Buffer for data coming from the input device
  315. ALCvoid *resampleBuffer; // Buffer for returned RingBuffer data when resampling
  316. ll_ringbuffer_t *ring;
  317. } ALCcoreAudioCapture;
  318. static void ALCcoreAudioCapture_Construct(ALCcoreAudioCapture *self, ALCdevice *device);
  319. static void ALCcoreAudioCapture_Destruct(ALCcoreAudioCapture *self);
  320. static ALCenum ALCcoreAudioCapture_open(ALCcoreAudioCapture *self, const ALCchar *name);
  321. static void ALCcoreAudioCapture_close(ALCcoreAudioCapture *self);
  322. static DECLARE_FORWARD(ALCcoreAudioCapture, ALCbackend, ALCboolean, reset)
  323. static ALCboolean ALCcoreAudioCapture_start(ALCcoreAudioCapture *self);
  324. static void ALCcoreAudioCapture_stop(ALCcoreAudioCapture *self);
  325. static ALCenum ALCcoreAudioCapture_captureSamples(ALCcoreAudioCapture *self, ALCvoid *buffer, ALCuint samples);
  326. static ALCuint ALCcoreAudioCapture_availableSamples(ALCcoreAudioCapture *self);
  327. static DECLARE_FORWARD(ALCcoreAudioCapture, ALCbackend, ClockLatency, getClockLatency)
  328. static DECLARE_FORWARD(ALCcoreAudioCapture, ALCbackend, void, lock)
  329. static DECLARE_FORWARD(ALCcoreAudioCapture, ALCbackend, void, unlock)
  330. DECLARE_DEFAULT_ALLOCATORS(ALCcoreAudioCapture)
  331. DEFINE_ALCBACKEND_VTABLE(ALCcoreAudioCapture);
  332. static void ALCcoreAudioCapture_Construct(ALCcoreAudioCapture *self, ALCdevice *device)
  333. {
  334. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  335. SET_VTABLE2(ALCcoreAudioCapture, ALCbackend, self);
  336. }
  337. static void ALCcoreAudioCapture_Destruct(ALCcoreAudioCapture *self)
  338. {
  339. ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
  340. }
  341. static OSStatus ALCcoreAudioCapture_RecordProc(void *inRefCon,
  342. AudioUnitRenderActionFlags* UNUSED(ioActionFlags),
  343. const AudioTimeStamp *inTimeStamp, UInt32 UNUSED(inBusNumber),
  344. UInt32 inNumberFrames, AudioBufferList* UNUSED(ioData))
  345. {
  346. ALCcoreAudioCapture *self = inRefCon;
  347. AudioUnitRenderActionFlags flags = 0;
  348. OSStatus err;
  349. // fill the bufferList with data from the input device
  350. err = AudioUnitRender(self->audioUnit, &flags, inTimeStamp, 1, inNumberFrames, self->bufferList);
  351. if(err != noErr)
  352. {
  353. ERR("AudioUnitRender error: %d\n", err);
  354. return err;
  355. }
  356. ll_ringbuffer_write(self->ring, self->bufferList->mBuffers[0].mData, inNumberFrames);
  357. return noErr;
  358. }
  359. static OSStatus ALCcoreAudioCapture_ConvertCallback(AudioConverterRef UNUSED(inAudioConverter),
  360. UInt32 *ioNumberDataPackets, AudioBufferList *ioData,
  361. AudioStreamPacketDescription** UNUSED(outDataPacketDescription),
  362. void *inUserData)
  363. {
  364. ALCcoreAudioCapture *self = inUserData;
  365. // Read from the ring buffer and store temporarily in a large buffer
  366. ll_ringbuffer_read(self->ring, self->resampleBuffer, *ioNumberDataPackets);
  367. // Set the input data
  368. ioData->mNumberBuffers = 1;
  369. ioData->mBuffers[0].mNumberChannels = self->format.mChannelsPerFrame;
  370. ioData->mBuffers[0].mData = self->resampleBuffer;
  371. ioData->mBuffers[0].mDataByteSize = (*ioNumberDataPackets) * self->format.mBytesPerFrame;
  372. return noErr;
  373. }
  374. static ALCenum ALCcoreAudioCapture_open(ALCcoreAudioCapture *self, const ALCchar *name)
  375. {
  376. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  377. AudioStreamBasicDescription requestedFormat; // The application requested format
  378. AudioStreamBasicDescription hardwareFormat; // The hardware format
  379. AudioStreamBasicDescription outputFormat; // The AudioUnit output format
  380. AURenderCallbackStruct input;
  381. AudioComponentDescription desc;
  382. AudioDeviceID inputDevice;
  383. UInt32 outputFrameCount;
  384. UInt32 propertySize;
  385. AudioObjectPropertyAddress propertyAddress;
  386. UInt32 enableIO;
  387. AudioComponent comp;
  388. OSStatus err;
  389. if(!name)
  390. name = ca_device;
  391. else if(strcmp(name, ca_device) != 0)
  392. return ALC_INVALID_VALUE;
  393. desc.componentType = kAudioUnitType_Output;
  394. desc.componentSubType = kAudioUnitSubType_HALOutput;
  395. desc.componentManufacturer = kAudioUnitManufacturer_Apple;
  396. desc.componentFlags = 0;
  397. desc.componentFlagsMask = 0;
  398. // Search for component with given description
  399. comp = AudioComponentFindNext(NULL, &desc);
  400. if(comp == NULL)
  401. {
  402. ERR("AudioComponentFindNext failed\n");
  403. return ALC_INVALID_VALUE;
  404. }
  405. // Open the component
  406. err = AudioComponentInstanceNew(comp, &self->audioUnit);
  407. if(err != noErr)
  408. {
  409. ERR("AudioComponentInstanceNew failed\n");
  410. goto error;
  411. }
  412. // Turn off AudioUnit output
  413. enableIO = 0;
  414. err = AudioUnitSetProperty(self->audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(ALuint));
  415. if(err != noErr)
  416. {
  417. ERR("AudioUnitSetProperty failed\n");
  418. goto error;
  419. }
  420. // Turn on AudioUnit input
  421. enableIO = 1;
  422. err = AudioUnitSetProperty(self->audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(ALuint));
  423. if(err != noErr)
  424. {
  425. ERR("AudioUnitSetProperty failed\n");
  426. goto error;
  427. }
  428. // Get the default input device
  429. propertySize = sizeof(AudioDeviceID);
  430. propertyAddress.mSelector = kAudioHardwarePropertyDefaultInputDevice;
  431. propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
  432. propertyAddress.mElement = kAudioObjectPropertyElementMaster;
  433. err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize, &inputDevice);
  434. if(err != noErr)
  435. {
  436. ERR("AudioObjectGetPropertyData failed\n");
  437. goto error;
  438. }
  439. if(inputDevice == kAudioDeviceUnknown)
  440. {
  441. ERR("No input device found\n");
  442. goto error;
  443. }
  444. // Track the input device
  445. err = AudioUnitSetProperty(self->audioUnit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &inputDevice, sizeof(AudioDeviceID));
  446. if(err != noErr)
  447. {
  448. ERR("AudioUnitSetProperty failed\n");
  449. goto error;
  450. }
  451. // set capture callback
  452. input.inputProc = ALCcoreAudioCapture_RecordProc;
  453. input.inputProcRefCon = self;
  454. err = AudioUnitSetProperty(self->audioUnit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &input, sizeof(AURenderCallbackStruct));
  455. if(err != noErr)
  456. {
  457. ERR("AudioUnitSetProperty failed\n");
  458. goto error;
  459. }
  460. // Initialize the device
  461. err = AudioUnitInitialize(self->audioUnit);
  462. if(err != noErr)
  463. {
  464. ERR("AudioUnitInitialize failed\n");
  465. goto error;
  466. }
  467. // Get the hardware format
  468. propertySize = sizeof(AudioStreamBasicDescription);
  469. err = AudioUnitGetProperty(self->audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 1, &hardwareFormat, &propertySize);
  470. if(err != noErr || propertySize != sizeof(AudioStreamBasicDescription))
  471. {
  472. ERR("AudioUnitGetProperty failed\n");
  473. goto error;
  474. }
  475. // Set up the requested format description
  476. switch(device->FmtType)
  477. {
  478. case DevFmtUByte:
  479. requestedFormat.mBitsPerChannel = 8;
  480. requestedFormat.mFormatFlags = kAudioFormatFlagIsPacked;
  481. break;
  482. case DevFmtShort:
  483. requestedFormat.mBitsPerChannel = 16;
  484. requestedFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;
  485. break;
  486. case DevFmtInt:
  487. requestedFormat.mBitsPerChannel = 32;
  488. requestedFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;
  489. break;
  490. case DevFmtFloat:
  491. requestedFormat.mBitsPerChannel = 32;
  492. requestedFormat.mFormatFlags = kAudioFormatFlagIsPacked;
  493. break;
  494. case DevFmtByte:
  495. case DevFmtUShort:
  496. case DevFmtUInt:
  497. ERR("%s samples not supported\n", DevFmtTypeString(device->FmtType));
  498. goto error;
  499. }
  500. switch(device->FmtChans)
  501. {
  502. case DevFmtMono:
  503. requestedFormat.mChannelsPerFrame = 1;
  504. break;
  505. case DevFmtStereo:
  506. requestedFormat.mChannelsPerFrame = 2;
  507. break;
  508. case DevFmtQuad:
  509. case DevFmtX51:
  510. case DevFmtX51Rear:
  511. case DevFmtX61:
  512. case DevFmtX71:
  513. case DevFmtAmbi3D:
  514. ERR("%s not supported\n", DevFmtChannelsString(device->FmtChans));
  515. goto error;
  516. }
  517. requestedFormat.mBytesPerFrame = requestedFormat.mChannelsPerFrame * requestedFormat.mBitsPerChannel / 8;
  518. requestedFormat.mBytesPerPacket = requestedFormat.mBytesPerFrame;
  519. requestedFormat.mSampleRate = device->Frequency;
  520. requestedFormat.mFormatID = kAudioFormatLinearPCM;
  521. requestedFormat.mReserved = 0;
  522. requestedFormat.mFramesPerPacket = 1;
  523. // save requested format description for later use
  524. self->format = requestedFormat;
  525. self->frameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType, device->AmbiOrder);
  526. // Use intermediate format for sample rate conversion (outputFormat)
  527. // Set sample rate to the same as hardware for resampling later
  528. outputFormat = requestedFormat;
  529. outputFormat.mSampleRate = hardwareFormat.mSampleRate;
  530. // Determine sample rate ratio for resampling
  531. self->sampleRateRatio = outputFormat.mSampleRate / device->Frequency;
  532. // The output format should be the requested format, but using the hardware sample rate
  533. // This is because the AudioUnit will automatically scale other properties, except for sample rate
  534. err = AudioUnitSetProperty(self->audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, (void *)&outputFormat, sizeof(outputFormat));
  535. if(err != noErr)
  536. {
  537. ERR("AudioUnitSetProperty failed\n");
  538. goto error;
  539. }
  540. // Set the AudioUnit output format frame count
  541. outputFrameCount = device->UpdateSize * self->sampleRateRatio;
  542. err = AudioUnitSetProperty(self->audioUnit, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Output, 0, &outputFrameCount, sizeof(outputFrameCount));
  543. if(err != noErr)
  544. {
  545. ERR("AudioUnitSetProperty failed: %d\n", err);
  546. goto error;
  547. }
  548. // Set up sample converter
  549. err = AudioConverterNew(&outputFormat, &requestedFormat, &self->audioConverter);
  550. if(err != noErr)
  551. {
  552. ERR("AudioConverterNew failed: %d\n", err);
  553. goto error;
  554. }
  555. // Create a buffer for use in the resample callback
  556. self->resampleBuffer = malloc(device->UpdateSize * self->frameSize * self->sampleRateRatio);
  557. // Allocate buffer for the AudioUnit output
  558. self->bufferList = allocate_buffer_list(outputFormat.mChannelsPerFrame, device->UpdateSize * self->frameSize * self->sampleRateRatio);
  559. if(self->bufferList == NULL)
  560. goto error;
  561. self->ring = ll_ringbuffer_create(
  562. device->UpdateSize*self->sampleRateRatio*device->NumUpdates + 1,
  563. self->frameSize
  564. );
  565. if(!self->ring) goto error;
  566. alstr_copy_cstr(&device->DeviceName, name);
  567. return ALC_NO_ERROR;
  568. error:
  569. ll_ringbuffer_free(self->ring);
  570. self->ring = NULL;
  571. free(self->resampleBuffer);
  572. destroy_buffer_list(self->bufferList);
  573. if(self->audioConverter)
  574. AudioConverterDispose(self->audioConverter);
  575. if(self->audioUnit)
  576. AudioComponentInstanceDispose(self->audioUnit);
  577. return ALC_INVALID_VALUE;
  578. }
  579. static void ALCcoreAudioCapture_close(ALCcoreAudioCapture *self)
  580. {
  581. ll_ringbuffer_free(self->ring);
  582. self->ring = NULL;
  583. free(self->resampleBuffer);
  584. destroy_buffer_list(self->bufferList);
  585. AudioConverterDispose(self->audioConverter);
  586. AudioComponentInstanceDispose(self->audioUnit);
  587. }
  588. static ALCboolean ALCcoreAudioCapture_start(ALCcoreAudioCapture *self)
  589. {
  590. OSStatus err = AudioOutputUnitStart(self->audioUnit);
  591. if(err != noErr)
  592. {
  593. ERR("AudioOutputUnitStart failed\n");
  594. return ALC_FALSE;
  595. }
  596. return ALC_TRUE;
  597. }
  598. static void ALCcoreAudioCapture_stop(ALCcoreAudioCapture *self)
  599. {
  600. OSStatus err = AudioOutputUnitStop(self->audioUnit);
  601. if(err != noErr)
  602. ERR("AudioOutputUnitStop failed\n");
  603. }
  604. static ALCenum ALCcoreAudioCapture_captureSamples(ALCcoreAudioCapture *self, ALCvoid *buffer, ALCuint samples)
  605. {
  606. AudioBufferList *list;
  607. UInt32 frameCount;
  608. OSStatus err;
  609. // If no samples are requested, just return
  610. if(samples == 0)
  611. return ALC_NO_ERROR;
  612. // Allocate a temporary AudioBufferList to use as the return resamples data
  613. list = alloca(sizeof(AudioBufferList) + sizeof(AudioBuffer));
  614. // Point the resampling buffer to the capture buffer
  615. list->mNumberBuffers = 1;
  616. list->mBuffers[0].mNumberChannels = self->format.mChannelsPerFrame;
  617. list->mBuffers[0].mDataByteSize = samples * self->frameSize;
  618. list->mBuffers[0].mData = buffer;
  619. // Resample into another AudioBufferList
  620. frameCount = samples;
  621. err = AudioConverterFillComplexBuffer(self->audioConverter,
  622. ALCcoreAudioCapture_ConvertCallback, self, &frameCount, list, NULL
  623. );
  624. if(err != noErr)
  625. {
  626. ERR("AudioConverterFillComplexBuffer error: %d\n", err);
  627. return ALC_INVALID_VALUE;
  628. }
  629. return ALC_NO_ERROR;
  630. }
  631. static ALCuint ALCcoreAudioCapture_availableSamples(ALCcoreAudioCapture *self)
  632. {
  633. return ll_ringbuffer_read_space(self->ring) / self->sampleRateRatio;
  634. }
  635. typedef struct ALCcoreAudioBackendFactory {
  636. DERIVE_FROM_TYPE(ALCbackendFactory);
  637. } ALCcoreAudioBackendFactory;
  638. #define ALCCOREAUDIOBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCcoreAudioBackendFactory, ALCbackendFactory) } }
  639. ALCbackendFactory *ALCcoreAudioBackendFactory_getFactory(void);
  640. static ALCboolean ALCcoreAudioBackendFactory_init(ALCcoreAudioBackendFactory *self);
  641. static DECLARE_FORWARD(ALCcoreAudioBackendFactory, ALCbackendFactory, void, deinit)
  642. static ALCboolean ALCcoreAudioBackendFactory_querySupport(ALCcoreAudioBackendFactory *self, ALCbackend_Type type);
  643. static void ALCcoreAudioBackendFactory_probe(ALCcoreAudioBackendFactory *self, enum DevProbe type);
  644. static ALCbackend* ALCcoreAudioBackendFactory_createBackend(ALCcoreAudioBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
  645. DEFINE_ALCBACKENDFACTORY_VTABLE(ALCcoreAudioBackendFactory);
  646. ALCbackendFactory *ALCcoreAudioBackendFactory_getFactory(void)
  647. {
  648. static ALCcoreAudioBackendFactory factory = ALCCOREAUDIOBACKENDFACTORY_INITIALIZER;
  649. return STATIC_CAST(ALCbackendFactory, &factory);
  650. }
  651. static ALCboolean ALCcoreAudioBackendFactory_init(ALCcoreAudioBackendFactory* UNUSED(self))
  652. {
  653. return ALC_TRUE;
  654. }
  655. static ALCboolean ALCcoreAudioBackendFactory_querySupport(ALCcoreAudioBackendFactory* UNUSED(self), ALCbackend_Type type)
  656. {
  657. if(type == ALCbackend_Playback || ALCbackend_Capture)
  658. return ALC_TRUE;
  659. return ALC_FALSE;
  660. }
  661. static void ALCcoreAudioBackendFactory_probe(ALCcoreAudioBackendFactory* UNUSED(self), enum DevProbe type)
  662. {
  663. switch(type)
  664. {
  665. case ALL_DEVICE_PROBE:
  666. AppendAllDevicesList(ca_device);
  667. break;
  668. case CAPTURE_DEVICE_PROBE:
  669. AppendCaptureDeviceList(ca_device);
  670. break;
  671. }
  672. }
  673. static ALCbackend* ALCcoreAudioBackendFactory_createBackend(ALCcoreAudioBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
  674. {
  675. if(type == ALCbackend_Playback)
  676. {
  677. ALCcoreAudioPlayback *backend;
  678. NEW_OBJ(backend, ALCcoreAudioPlayback)(device);
  679. if(!backend) return NULL;
  680. return STATIC_CAST(ALCbackend, backend);
  681. }
  682. if(type == ALCbackend_Capture)
  683. {
  684. ALCcoreAudioCapture *backend;
  685. NEW_OBJ(backend, ALCcoreAudioCapture)(device);
  686. if(!backend) return NULL;
  687. return STATIC_CAST(ALCbackend, backend);
  688. }
  689. return NULL;
  690. }