2011-03-15 04:58:56 -07:00
|
|
|
/**
|
|
|
|
* OpenAL cross platform audio library
|
|
|
|
* Copyright (C) 1999-2007 by authors.
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2014-08-18 14:11:03 +02:00
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2011-03-15 04:58:56 -07:00
|
|
|
* Or go to http://www.gnu.org/copyleft/lgpl.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2018-11-15 21:24:09 -08:00
|
|
|
#include "backends/coreaudio.h"
|
|
|
|
|
2011-03-15 04:58:56 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "alMain.h"
|
2012-09-14 02:14:29 -07:00
|
|
|
#include "alu.h"
|
2018-01-11 09:16:28 -08:00
|
|
|
#include "ringbuffer.h"
|
2018-12-27 12:04:18 -08:00
|
|
|
#include "converter.h"
|
2011-03-15 04:58:56 -07:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <AudioUnit/AudioUnit.h>
|
2011-06-27 23:49:17 -07:00
|
|
|
#include <AudioToolbox/AudioToolbox.h>
|
2011-03-15 04:58:56 -07:00
|
|
|
|
|
|
|
|
|
|
|
static const ALCchar ca_device[] = "CoreAudio Default";
|
|
|
|
|
2011-06-27 23:49:17 -07:00
|
|
|
|
2018-11-13 02:09:21 -08:00
|
|
|
struct ALCcoreAudioPlayback final : public ALCbackend {
|
2018-12-27 12:04:18 -08:00
|
|
|
AudioUnit mAudioUnit;
|
2011-06-27 23:49:17 -07:00
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
ALuint mFrameSize{0u};
|
|
|
|
AudioStreamBasicDescription mFormat{}; // This is the OpenAL format as a CoreAudio ASBD
|
2018-11-13 02:09:21 -08:00
|
|
|
};
|
2017-04-09 11:21:02 -07:00
|
|
|
|
|
|
|
static void ALCcoreAudioPlayback_Construct(ALCcoreAudioPlayback *self, ALCdevice *device);
|
|
|
|
static void ALCcoreAudioPlayback_Destruct(ALCcoreAudioPlayback *self);
|
|
|
|
static ALCenum ALCcoreAudioPlayback_open(ALCcoreAudioPlayback *self, const ALCchar *name);
|
|
|
|
static ALCboolean ALCcoreAudioPlayback_reset(ALCcoreAudioPlayback *self);
|
|
|
|
static ALCboolean ALCcoreAudioPlayback_start(ALCcoreAudioPlayback *self);
|
|
|
|
static void ALCcoreAudioPlayback_stop(ALCcoreAudioPlayback *self);
|
|
|
|
static DECLARE_FORWARD2(ALCcoreAudioPlayback, ALCbackend, ALCenum, captureSamples, void*, ALCuint)
|
|
|
|
static DECLARE_FORWARD(ALCcoreAudioPlayback, ALCbackend, ALCuint, availableSamples)
|
|
|
|
static DECLARE_FORWARD(ALCcoreAudioPlayback, ALCbackend, ClockLatency, getClockLatency)
|
|
|
|
static DECLARE_FORWARD(ALCcoreAudioPlayback, ALCbackend, void, lock)
|
|
|
|
static DECLARE_FORWARD(ALCcoreAudioPlayback, ALCbackend, void, unlock)
|
|
|
|
DECLARE_DEFAULT_ALLOCATORS(ALCcoreAudioPlayback)
|
|
|
|
|
|
|
|
DEFINE_ALCBACKEND_VTABLE(ALCcoreAudioPlayback);
|
|
|
|
|
|
|
|
|
|
|
|
static void ALCcoreAudioPlayback_Construct(ALCcoreAudioPlayback *self, ALCdevice *device)
|
|
|
|
{
|
2018-11-13 02:09:21 -08:00
|
|
|
new (self) ALCcoreAudioPlayback{};
|
2017-04-09 11:21:02 -07:00
|
|
|
ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
|
|
|
|
SET_VTABLE2(ALCcoreAudioPlayback, ALCbackend, self);
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
static void ALCcoreAudioPlayback_Destruct(ALCcoreAudioPlayback *self)
|
2011-06-27 23:49:17 -07:00
|
|
|
{
|
2018-12-27 12:04:18 -08:00
|
|
|
AudioUnitUninitialize(self->mAudioUnit);
|
|
|
|
AudioComponentInstanceDispose(self->mAudioUnit);
|
2018-01-29 01:00:53 -08:00
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
|
2018-11-13 02:09:21 -08:00
|
|
|
self->~ALCcoreAudioPlayback();
|
2017-04-09 11:21:02 -07:00
|
|
|
}
|
2011-06-27 23:49:17 -07:00
|
|
|
|
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
static OSStatus ALCcoreAudioPlayback_MixerProc(void *inRefCon,
|
|
|
|
AudioUnitRenderActionFlags* UNUSED(ioActionFlags), const AudioTimeStamp* UNUSED(inTimeStamp),
|
|
|
|
UInt32 UNUSED(inBusNumber), UInt32 UNUSED(inNumberFrames), AudioBufferList *ioData)
|
|
|
|
{
|
2018-11-13 02:09:21 -08:00
|
|
|
ALCcoreAudioPlayback *self = static_cast<ALCcoreAudioPlayback*>(inRefCon);
|
2017-04-09 11:21:02 -07:00
|
|
|
ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
|
|
|
|
|
2018-03-23 11:58:11 -07:00
|
|
|
ALCcoreAudioPlayback_lock(self);
|
2017-04-09 11:21:02 -07:00
|
|
|
aluMixData(device, ioData->mBuffers[0].mData,
|
2018-12-27 12:04:18 -08:00
|
|
|
ioData->mBuffers[0].mDataByteSize / self->mFrameSize);
|
2018-03-23 11:58:11 -07:00
|
|
|
ALCcoreAudioPlayback_unlock(self);
|
2011-03-15 04:58:56 -07:00
|
|
|
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
|
|
|
|
static ALCenum ALCcoreAudioPlayback_open(ALCcoreAudioPlayback *self, const ALCchar *name)
|
2011-03-15 04:58:56 -07:00
|
|
|
{
|
2017-04-09 11:21:02 -07:00
|
|
|
ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
|
2015-11-13 20:11:12 -04:00
|
|
|
AudioComponentDescription desc;
|
|
|
|
AudioComponent comp;
|
2011-03-15 04:58:56 -07:00
|
|
|
OSStatus err;
|
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
if(!name)
|
|
|
|
name = ca_device;
|
|
|
|
else if(strcmp(name, ca_device) != 0)
|
2011-08-24 14:24:48 -07:00
|
|
|
return ALC_INVALID_VALUE;
|
2011-03-15 04:58:56 -07:00
|
|
|
|
|
|
|
/* open the default output unit */
|
|
|
|
desc.componentType = kAudioUnitType_Output;
|
2018-09-07 23:01:17 -07:00
|
|
|
#if TARGET_OS_IOS
|
|
|
|
desc.componentSubType = kAudioUnitSubType_RemoteIO;
|
|
|
|
#else
|
2011-03-15 04:58:56 -07:00
|
|
|
desc.componentSubType = kAudioUnitSubType_DefaultOutput;
|
2018-09-07 23:01:17 -07:00
|
|
|
#endif
|
2011-03-15 04:58:56 -07:00
|
|
|
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
|
|
|
|
desc.componentFlags = 0;
|
|
|
|
desc.componentFlagsMask = 0;
|
|
|
|
|
2015-11-13 20:11:12 -04:00
|
|
|
comp = AudioComponentFindNext(NULL, &desc);
|
2011-03-15 04:58:56 -07:00
|
|
|
if(comp == NULL)
|
|
|
|
{
|
2015-11-13 20:11:12 -04:00
|
|
|
ERR("AudioComponentFindNext failed\n");
|
2011-08-24 14:24:48 -07:00
|
|
|
return ALC_INVALID_VALUE;
|
2011-03-15 04:58:56 -07:00
|
|
|
}
|
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioComponentInstanceNew(comp, &self->mAudioUnit);
|
2011-03-15 11:52:18 -07:00
|
|
|
if(err != noErr)
|
2011-03-15 04:58:56 -07:00
|
|
|
{
|
2015-11-13 20:11:12 -04:00
|
|
|
ERR("AudioComponentInstanceNew failed\n");
|
2011-08-24 14:24:48 -07:00
|
|
|
return ALC_INVALID_VALUE;
|
2011-03-15 04:58:56 -07:00
|
|
|
}
|
|
|
|
|
2012-03-12 21:58:40 -07:00
|
|
|
/* init and start the default audio unit... */
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioUnitInitialize(self->mAudioUnit);
|
2012-03-12 21:58:40 -07:00
|
|
|
if(err != noErr)
|
|
|
|
{
|
|
|
|
ERR("AudioUnitInitialize failed\n");
|
2018-12-27 12:04:18 -08:00
|
|
|
AudioComponentInstanceDispose(self->mAudioUnit);
|
2012-03-13 13:48:42 -07:00
|
|
|
return ALC_INVALID_VALUE;
|
2012-03-12 21:58:40 -07:00
|
|
|
}
|
|
|
|
|
2018-11-18 18:45:45 -08:00
|
|
|
device->DeviceName = name;
|
2011-08-24 14:24:48 -07:00
|
|
|
return ALC_NO_ERROR;
|
2011-03-15 04:58:56 -07:00
|
|
|
}
|
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
static ALCboolean ALCcoreAudioPlayback_reset(ALCcoreAudioPlayback *self)
|
2011-03-15 04:58:56 -07:00
|
|
|
{
|
2017-04-09 11:21:02 -07:00
|
|
|
ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
|
2011-03-15 04:58:56 -07:00
|
|
|
AudioStreamBasicDescription streamFormat;
|
|
|
|
AURenderCallbackStruct input;
|
|
|
|
OSStatus err;
|
|
|
|
UInt32 size;
|
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioUnitUninitialize(self->mAudioUnit);
|
2011-03-15 11:52:18 -07:00
|
|
|
if(err != noErr)
|
2012-03-12 21:58:40 -07:00
|
|
|
ERR("-- AudioUnitUninitialize failed.\n");
|
2011-03-15 04:58:56 -07:00
|
|
|
|
|
|
|
/* retrieve default output unit's properties (output side) */
|
|
|
|
size = sizeof(AudioStreamBasicDescription);
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioUnitGetProperty(self->mAudioUnit, kAudioUnitProperty_StreamFormat,
|
|
|
|
kAudioUnitScope_Output, 0, &streamFormat, &size);
|
2011-03-15 11:52:18 -07:00
|
|
|
if(err != noErr || size != sizeof(AudioStreamBasicDescription))
|
2011-03-15 04:58:56 -07:00
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("AudioUnitGetProperty failed\n");
|
2011-03-15 04:58:56 -07:00
|
|
|
return ALC_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2011-07-10 22:26:02 -07:00
|
|
|
TRACE("Output streamFormat of default output unit -\n");
|
|
|
|
TRACE(" streamFormat.mFramesPerPacket = %d\n", streamFormat.mFramesPerPacket);
|
|
|
|
TRACE(" streamFormat.mChannelsPerFrame = %d\n", streamFormat.mChannelsPerFrame);
|
|
|
|
TRACE(" streamFormat.mBitsPerChannel = %d\n", streamFormat.mBitsPerChannel);
|
|
|
|
TRACE(" streamFormat.mBytesPerPacket = %d\n", streamFormat.mBytesPerPacket);
|
|
|
|
TRACE(" streamFormat.mBytesPerFrame = %d\n", streamFormat.mBytesPerFrame);
|
|
|
|
TRACE(" streamFormat.mSampleRate = %5.0f\n", streamFormat.mSampleRate);
|
2011-03-15 04:58:56 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* set default output unit's input side to match output side */
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioUnitSetProperty(self->mAudioUnit, kAudioUnitProperty_StreamFormat,
|
|
|
|
kAudioUnitScope_Input, 0, &streamFormat, size);
|
2011-03-15 11:52:18 -07:00
|
|
|
if(err != noErr)
|
2011-03-15 04:58:56 -07:00
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("AudioUnitSetProperty failed\n");
|
2011-03-15 04:58:56 -07:00
|
|
|
return ALC_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-05-03 02:29:26 -07:00
|
|
|
if(device->Frequency != streamFormat.mSampleRate)
|
|
|
|
{
|
2016-08-04 21:06:00 -07:00
|
|
|
device->NumUpdates = (ALuint)((ALuint64)device->NumUpdates *
|
2011-05-03 02:29:26 -07:00
|
|
|
streamFormat.mSampleRate /
|
|
|
|
device->Frequency);
|
|
|
|
device->Frequency = streamFormat.mSampleRate;
|
|
|
|
}
|
2011-03-15 04:58:56 -07:00
|
|
|
|
|
|
|
/* FIXME: How to tell what channels are what in the output device, and how
|
|
|
|
* to specify what we're giving? eg, 6.0 vs 5.1 */
|
|
|
|
switch(streamFormat.mChannelsPerFrame)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
device->FmtChans = DevFmtMono;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
device->FmtChans = DevFmtStereo;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
device->FmtChans = DevFmtQuad;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
device->FmtChans = DevFmtX51;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
device->FmtChans = DevFmtX61;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
device->FmtChans = DevFmtX71;
|
|
|
|
break;
|
|
|
|
default:
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("Unhandled channel count (%d), using Stereo\n", streamFormat.mChannelsPerFrame);
|
2011-03-15 04:58:56 -07:00
|
|
|
device->FmtChans = DevFmtStereo;
|
|
|
|
streamFormat.mChannelsPerFrame = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SetDefaultWFXChannelOrder(device);
|
|
|
|
|
|
|
|
/* use channel count and sample rate from the default output unit's current
|
|
|
|
* parameters, but reset everything else */
|
|
|
|
streamFormat.mFramesPerPacket = 1;
|
2012-12-03 11:50:02 -08:00
|
|
|
streamFormat.mFormatFlags = 0;
|
2011-03-15 04:58:56 -07:00
|
|
|
switch(device->FmtType)
|
|
|
|
{
|
|
|
|
case DevFmtUByte:
|
|
|
|
device->FmtType = DevFmtByte;
|
|
|
|
/* fall-through */
|
|
|
|
case DevFmtByte:
|
2012-12-03 11:50:02 -08:00
|
|
|
streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
|
2011-03-15 04:58:56 -07:00
|
|
|
streamFormat.mBitsPerChannel = 8;
|
|
|
|
break;
|
|
|
|
case DevFmtUShort:
|
|
|
|
device->FmtType = DevFmtShort;
|
|
|
|
/* fall-through */
|
|
|
|
case DevFmtShort:
|
2012-12-03 11:50:02 -08:00
|
|
|
streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
|
2011-03-15 04:58:56 -07:00
|
|
|
streamFormat.mBitsPerChannel = 16;
|
|
|
|
break;
|
2012-02-14 11:44:57 -08:00
|
|
|
case DevFmtUInt:
|
|
|
|
device->FmtType = DevFmtInt;
|
|
|
|
/* fall-through */
|
|
|
|
case DevFmtInt:
|
2012-12-03 11:50:02 -08:00
|
|
|
streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
|
|
|
|
streamFormat.mBitsPerChannel = 32;
|
|
|
|
break;
|
|
|
|
case DevFmtFloat:
|
|
|
|
streamFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat;
|
2012-02-14 11:44:57 -08:00
|
|
|
streamFormat.mBitsPerChannel = 32;
|
|
|
|
break;
|
2011-03-15 04:58:56 -07:00
|
|
|
}
|
2012-12-03 11:50:02 -08:00
|
|
|
streamFormat.mBytesPerFrame = streamFormat.mChannelsPerFrame *
|
|
|
|
streamFormat.mBitsPerChannel / 8;
|
|
|
|
streamFormat.mBytesPerPacket = streamFormat.mBytesPerFrame;
|
2011-03-15 04:58:56 -07:00
|
|
|
streamFormat.mFormatID = kAudioFormatLinearPCM;
|
2012-12-03 11:50:02 -08:00
|
|
|
streamFormat.mFormatFlags |= kAudioFormatFlagsNativeEndian |
|
|
|
|
kLinearPCMFormatFlagIsPacked;
|
2011-03-15 04:58:56 -07:00
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioUnitSetProperty(self->mAudioUnit, kAudioUnitProperty_StreamFormat,
|
|
|
|
kAudioUnitScope_Input, 0, &streamFormat, sizeof(AudioStreamBasicDescription));
|
2011-03-15 11:52:18 -07:00
|
|
|
if(err != noErr)
|
2011-03-15 04:58:56 -07:00
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("AudioUnitSetProperty failed\n");
|
2011-03-15 04:58:56 -07:00
|
|
|
return ALC_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup callback */
|
2018-12-27 12:04:18 -08:00
|
|
|
self->mFrameSize = device->frameSizeFromFmt();
|
2017-04-09 11:21:02 -07:00
|
|
|
input.inputProc = ALCcoreAudioPlayback_MixerProc;
|
|
|
|
input.inputProcRefCon = self;
|
2011-03-15 04:58:56 -07:00
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioUnitSetProperty(self->mAudioUnit, kAudioUnitProperty_SetRenderCallback,
|
|
|
|
kAudioUnitScope_Input, 0, &input, sizeof(AURenderCallbackStruct));
|
2011-03-15 11:52:18 -07:00
|
|
|
if(err != noErr)
|
2011-03-15 04:58:56 -07:00
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("AudioUnitSetProperty failed\n");
|
2011-03-15 04:58:56 -07:00
|
|
|
return ALC_FALSE;
|
|
|
|
}
|
|
|
|
|
2012-03-12 21:58:40 -07:00
|
|
|
/* init the default audio unit... */
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioUnitInitialize(self->mAudioUnit);
|
2012-03-12 21:58:40 -07:00
|
|
|
if(err != noErr)
|
|
|
|
{
|
|
|
|
ERR("AudioUnitInitialize failed\n");
|
|
|
|
return ALC_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ALC_TRUE;
|
|
|
|
}
|
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
static ALCboolean ALCcoreAudioPlayback_start(ALCcoreAudioPlayback *self)
|
2012-03-12 21:58:40 -07:00
|
|
|
{
|
2018-12-27 12:04:18 -08:00
|
|
|
OSStatus err = AudioOutputUnitStart(self->mAudioUnit);
|
2012-03-12 21:58:40 -07:00
|
|
|
if(err != noErr)
|
|
|
|
{
|
|
|
|
ERR("AudioOutputUnitStart failed\n");
|
|
|
|
return ALC_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-03-15 04:58:56 -07:00
|
|
|
return ALC_TRUE;
|
|
|
|
}
|
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
static void ALCcoreAudioPlayback_stop(ALCcoreAudioPlayback *self)
|
2011-03-15 04:58:56 -07:00
|
|
|
{
|
2018-12-27 12:04:18 -08:00
|
|
|
OSStatus err = AudioOutputUnitStop(self->mAudioUnit);
|
2017-04-09 11:21:02 -07:00
|
|
|
if(err != noErr)
|
|
|
|
ERR("AudioOutputUnitStop failed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-13 02:09:21 -08:00
|
|
|
struct ALCcoreAudioCapture final : public ALCbackend {
|
2018-12-27 12:04:18 -08:00
|
|
|
AudioUnit mAudioUnit{0};
|
2017-04-09 11:21:02 -07:00
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
ALuint mFrameSize{0u};
|
|
|
|
AudioStreamBasicDescription mFormat{}; // This is the OpenAL format as a CoreAudio ASBD
|
2017-04-09 11:21:02 -07:00
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
std::unique_ptr<SampleConverter> mConverter;
|
2017-04-09 11:21:02 -07:00
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
RingBufferPtr mRing{nullptr};
|
2018-11-13 02:09:21 -08:00
|
|
|
};
|
2017-04-09 11:21:02 -07:00
|
|
|
|
|
|
|
static void ALCcoreAudioCapture_Construct(ALCcoreAudioCapture *self, ALCdevice *device);
|
|
|
|
static void ALCcoreAudioCapture_Destruct(ALCcoreAudioCapture *self);
|
|
|
|
static ALCenum ALCcoreAudioCapture_open(ALCcoreAudioCapture *self, const ALCchar *name);
|
|
|
|
static DECLARE_FORWARD(ALCcoreAudioCapture, ALCbackend, ALCboolean, reset)
|
|
|
|
static ALCboolean ALCcoreAudioCapture_start(ALCcoreAudioCapture *self);
|
|
|
|
static void ALCcoreAudioCapture_stop(ALCcoreAudioCapture *self);
|
|
|
|
static ALCenum ALCcoreAudioCapture_captureSamples(ALCcoreAudioCapture *self, ALCvoid *buffer, ALCuint samples);
|
|
|
|
static ALCuint ALCcoreAudioCapture_availableSamples(ALCcoreAudioCapture *self);
|
|
|
|
static DECLARE_FORWARD(ALCcoreAudioCapture, ALCbackend, ClockLatency, getClockLatency)
|
|
|
|
static DECLARE_FORWARD(ALCcoreAudioCapture, ALCbackend, void, lock)
|
|
|
|
static DECLARE_FORWARD(ALCcoreAudioCapture, ALCbackend, void, unlock)
|
|
|
|
DECLARE_DEFAULT_ALLOCATORS(ALCcoreAudioCapture)
|
|
|
|
|
|
|
|
DEFINE_ALCBACKEND_VTABLE(ALCcoreAudioCapture);
|
|
|
|
|
|
|
|
|
|
|
|
static void ALCcoreAudioCapture_Construct(ALCcoreAudioCapture *self, ALCdevice *device)
|
|
|
|
{
|
2018-11-13 02:09:21 -08:00
|
|
|
new (self) ALCcoreAudioCapture{};
|
2017-04-09 11:21:02 -07:00
|
|
|
ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
|
|
|
|
SET_VTABLE2(ALCcoreAudioCapture, ALCbackend, self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ALCcoreAudioCapture_Destruct(ALCcoreAudioCapture *self)
|
|
|
|
{
|
2018-12-27 12:04:18 -08:00
|
|
|
if(self->mAudioUnit)
|
|
|
|
AudioComponentInstanceDispose(self->mAudioUnit);
|
|
|
|
self->mAudioUnit = 0;
|
2018-03-24 08:58:18 -07:00
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
|
2018-11-13 02:09:21 -08:00
|
|
|
self->~ALCcoreAudioCapture();
|
2017-04-09 11:21:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static OSStatus ALCcoreAudioCapture_RecordProc(void *inRefCon,
|
|
|
|
AudioUnitRenderActionFlags* UNUSED(ioActionFlags),
|
|
|
|
const AudioTimeStamp *inTimeStamp, UInt32 UNUSED(inBusNumber),
|
|
|
|
UInt32 inNumberFrames, AudioBufferList* UNUSED(ioData))
|
|
|
|
{
|
2018-12-26 21:22:17 -08:00
|
|
|
auto self = static_cast<ALCcoreAudioCapture*>(inRefCon);
|
2018-12-27 12:04:18 -08:00
|
|
|
RingBuffer *ring{self->mRing.get()};
|
2017-04-09 11:21:02 -07:00
|
|
|
AudioUnitRenderActionFlags flags = 0;
|
2018-12-27 12:04:18 -08:00
|
|
|
union {
|
|
|
|
ALbyte _[sizeof(AudioBufferList) + sizeof(AudioBuffer)];
|
|
|
|
AudioBufferList list;
|
|
|
|
} audiobuf = { { 0 } };
|
2011-03-15 04:58:56 -07:00
|
|
|
OSStatus err;
|
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
auto rec_vec = ring->getWriteVector();
|
|
|
|
|
|
|
|
// Fill the ringbuffer's first segment with data from the input device
|
|
|
|
size_t total_read{minz(rec_vec.first.len, inNumberFrames)};
|
|
|
|
audiobuf.list.mNumberBuffers = 1;
|
|
|
|
audiobuf.list.mBuffers[0].mNumberChannels = self->mFormat.mChannelsPerFrame;
|
|
|
|
audiobuf.list.mBuffers[0].mData = rec_vec.first.buf;
|
|
|
|
audiobuf.list.mBuffers[0].mDataByteSize = total_read * self->mFormat.mBytesPerFrame;
|
|
|
|
err = AudioUnitRender(self->mAudioUnit, &flags, inTimeStamp, 1, inNumberFrames,
|
|
|
|
&audiobuf.list);
|
|
|
|
if(err == noErr && inNumberFrames > rec_vec.first.len && rec_vec.second.len > 0)
|
|
|
|
{
|
|
|
|
/* If there's still more to get and there's space in the ringbuffer's
|
|
|
|
* second segment, fill that with data too.
|
|
|
|
*/
|
|
|
|
const size_t remlen{inNumberFrames - rec_vec.first.len};
|
|
|
|
const size_t toread{minz(rec_vec.second.len, remlen)};
|
|
|
|
total_read += toread;
|
|
|
|
|
|
|
|
audiobuf.list.mNumberBuffers = 1;
|
|
|
|
audiobuf.list.mBuffers[0].mNumberChannels = self->mFormat.mChannelsPerFrame;
|
|
|
|
audiobuf.list.mBuffers[0].mData = rec_vec.second.buf;
|
|
|
|
audiobuf.list.mBuffers[0].mDataByteSize = toread * self->mFormat.mBytesPerFrame;
|
|
|
|
err = AudioUnitRender(self->mAudioUnit, &flags, inTimeStamp, 1, inNumberFrames,
|
|
|
|
&audiobuf.list);
|
|
|
|
}
|
2011-03-15 11:52:18 -07:00
|
|
|
if(err != noErr)
|
2017-04-09 11:21:02 -07:00
|
|
|
{
|
|
|
|
ERR("AudioUnitRender error: %d\n", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
ring->writeAdvance(total_read);
|
2017-04-09 11:21:02 -07:00
|
|
|
return noErr;
|
2011-03-15 04:58:56 -07:00
|
|
|
}
|
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
|
|
|
|
static ALCenum ALCcoreAudioCapture_open(ALCcoreAudioCapture *self, const ALCchar *name)
|
2011-03-15 04:58:56 -07:00
|
|
|
{
|
2017-04-09 11:21:02 -07:00
|
|
|
ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
|
2011-06-27 23:49:17 -07:00
|
|
|
AudioStreamBasicDescription requestedFormat; // The application requested format
|
|
|
|
AudioStreamBasicDescription hardwareFormat; // The hardware format
|
|
|
|
AudioStreamBasicDescription outputFormat; // The AudioUnit output format
|
|
|
|
AURenderCallbackStruct input;
|
2015-11-13 20:11:12 -04:00
|
|
|
AudioComponentDescription desc;
|
2011-06-27 23:49:17 -07:00
|
|
|
UInt32 outputFrameCount;
|
|
|
|
UInt32 propertySize;
|
2015-11-13 20:11:12 -04:00
|
|
|
AudioObjectPropertyAddress propertyAddress;
|
2011-06-27 23:49:17 -07:00
|
|
|
UInt32 enableIO;
|
2015-11-13 20:11:12 -04:00
|
|
|
AudioComponent comp;
|
2011-06-27 23:49:17 -07:00
|
|
|
OSStatus err;
|
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
if(!name)
|
|
|
|
name = ca_device;
|
|
|
|
else if(strcmp(name, ca_device) != 0)
|
2014-08-26 14:50:14 -07:00
|
|
|
return ALC_INVALID_VALUE;
|
|
|
|
|
2011-06-27 23:49:17 -07:00
|
|
|
desc.componentType = kAudioUnitType_Output;
|
2018-09-07 23:01:17 -07:00
|
|
|
#if TARGET_OS_IOS
|
|
|
|
desc.componentSubType = kAudioUnitSubType_RemoteIO;
|
|
|
|
#else
|
2011-06-27 23:49:17 -07:00
|
|
|
desc.componentSubType = kAudioUnitSubType_HALOutput;
|
2018-09-07 23:01:17 -07:00
|
|
|
#endif
|
2011-06-27 23:49:17 -07:00
|
|
|
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
|
|
|
|
desc.componentFlags = 0;
|
|
|
|
desc.componentFlagsMask = 0;
|
|
|
|
|
|
|
|
// Search for component with given description
|
2015-11-13 20:11:12 -04:00
|
|
|
comp = AudioComponentFindNext(NULL, &desc);
|
2011-06-27 23:49:17 -07:00
|
|
|
if(comp == NULL)
|
|
|
|
{
|
2015-11-13 20:11:12 -04:00
|
|
|
ERR("AudioComponentFindNext failed\n");
|
2011-08-24 14:44:15 -07:00
|
|
|
return ALC_INVALID_VALUE;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open the component
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioComponentInstanceNew(comp, &self->mAudioUnit);
|
2011-06-27 23:49:17 -07:00
|
|
|
if(err != noErr)
|
|
|
|
{
|
2015-11-13 20:11:12 -04:00
|
|
|
ERR("AudioComponentInstanceNew failed\n");
|
2018-12-27 12:04:18 -08:00
|
|
|
return ALC_INVALID_VALUE;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Turn off AudioUnit output
|
|
|
|
enableIO = 0;
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioUnitSetProperty(self->mAudioUnit, kAudioOutputUnitProperty_EnableIO,
|
|
|
|
kAudioUnitScope_Output, 0, &enableIO, sizeof(ALuint));
|
2011-06-27 23:49:17 -07:00
|
|
|
if(err != noErr)
|
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("AudioUnitSetProperty failed\n");
|
2018-12-27 12:04:18 -08:00
|
|
|
return ALC_INVALID_VALUE;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Turn on AudioUnit input
|
|
|
|
enableIO = 1;
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioUnitSetProperty(self->mAudioUnit, kAudioOutputUnitProperty_EnableIO,
|
|
|
|
kAudioUnitScope_Input, 1, &enableIO, sizeof(ALuint));
|
2011-06-27 23:49:17 -07:00
|
|
|
if(err != noErr)
|
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("AudioUnitSetProperty failed\n");
|
2018-12-27 12:04:18 -08:00
|
|
|
return ALC_INVALID_VALUE;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
2018-09-07 23:01:17 -07:00
|
|
|
#if !TARGET_OS_IOS
|
2011-06-27 23:49:17 -07:00
|
|
|
{
|
2018-11-13 02:09:21 -08:00
|
|
|
// Get the default input device
|
|
|
|
AudioDeviceID inputDevice = kAudioDeviceUnknown;
|
|
|
|
|
|
|
|
propertySize = sizeof(AudioDeviceID);
|
|
|
|
propertyAddress.mSelector = kAudioHardwarePropertyDefaultInputDevice;
|
|
|
|
propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
|
|
|
|
propertyAddress.mElement = kAudioObjectPropertyElementMaster;
|
|
|
|
|
|
|
|
err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize, &inputDevice);
|
|
|
|
if(err != noErr)
|
|
|
|
{
|
|
|
|
ERR("AudioObjectGetPropertyData failed\n");
|
2018-12-27 12:04:18 -08:00
|
|
|
return ALC_INVALID_VALUE;
|
2018-11-13 02:09:21 -08:00
|
|
|
}
|
|
|
|
if(inputDevice == kAudioDeviceUnknown)
|
|
|
|
{
|
|
|
|
ERR("No input device found\n");
|
2018-12-27 12:04:18 -08:00
|
|
|
return ALC_INVALID_VALUE;
|
2018-11-13 02:09:21 -08:00
|
|
|
}
|
2011-06-27 23:49:17 -07:00
|
|
|
|
2018-11-13 02:09:21 -08:00
|
|
|
// Track the input device
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioUnitSetProperty(self->mAudioUnit, kAudioOutputUnitProperty_CurrentDevice,
|
|
|
|
kAudioUnitScope_Global, 0, &inputDevice, sizeof(AudioDeviceID));
|
2018-11-13 02:09:21 -08:00
|
|
|
if(err != noErr)
|
|
|
|
{
|
|
|
|
ERR("AudioUnitSetProperty failed\n");
|
2018-12-27 12:04:18 -08:00
|
|
|
return ALC_INVALID_VALUE;
|
2018-11-13 02:09:21 -08:00
|
|
|
}
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
2018-09-07 23:01:17 -07:00
|
|
|
#endif
|
2011-06-27 23:49:17 -07:00
|
|
|
|
|
|
|
// set capture callback
|
2017-04-09 11:21:02 -07:00
|
|
|
input.inputProc = ALCcoreAudioCapture_RecordProc;
|
|
|
|
input.inputProcRefCon = self;
|
2011-06-27 23:49:17 -07:00
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioUnitSetProperty(self->mAudioUnit, kAudioOutputUnitProperty_SetInputCallback,
|
|
|
|
kAudioUnitScope_Global, 0, &input, sizeof(AURenderCallbackStruct));
|
2011-06-27 23:49:17 -07:00
|
|
|
if(err != noErr)
|
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("AudioUnitSetProperty failed\n");
|
2018-12-27 12:04:18 -08:00
|
|
|
return ALC_INVALID_VALUE;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the device
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioUnitInitialize(self->mAudioUnit);
|
2011-06-27 23:49:17 -07:00
|
|
|
if(err != noErr)
|
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("AudioUnitInitialize failed\n");
|
2018-12-27 12:04:18 -08:00
|
|
|
return ALC_INVALID_VALUE;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the hardware format
|
|
|
|
propertySize = sizeof(AudioStreamBasicDescription);
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioUnitGetProperty(self->mAudioUnit, kAudioUnitProperty_StreamFormat,
|
|
|
|
kAudioUnitScope_Input, 1, &hardwareFormat, &propertySize);
|
2011-06-27 23:49:17 -07:00
|
|
|
if(err != noErr || propertySize != sizeof(AudioStreamBasicDescription))
|
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("AudioUnitGetProperty failed\n");
|
2018-12-27 12:04:18 -08:00
|
|
|
return ALC_INVALID_VALUE;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the requested format description
|
|
|
|
switch(device->FmtType)
|
|
|
|
{
|
|
|
|
case DevFmtUByte:
|
|
|
|
requestedFormat.mBitsPerChannel = 8;
|
|
|
|
requestedFormat.mFormatFlags = kAudioFormatFlagIsPacked;
|
|
|
|
break;
|
|
|
|
case DevFmtShort:
|
|
|
|
requestedFormat.mBitsPerChannel = 16;
|
|
|
|
requestedFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;
|
|
|
|
break;
|
2012-02-14 11:44:57 -08:00
|
|
|
case DevFmtInt:
|
|
|
|
requestedFormat.mBitsPerChannel = 32;
|
|
|
|
requestedFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;
|
|
|
|
break;
|
2011-06-27 23:49:17 -07:00
|
|
|
case DevFmtFloat:
|
|
|
|
requestedFormat.mBitsPerChannel = 32;
|
|
|
|
requestedFormat.mFormatFlags = kAudioFormatFlagIsPacked;
|
|
|
|
break;
|
|
|
|
case DevFmtByte:
|
|
|
|
case DevFmtUShort:
|
2012-02-14 11:44:57 -08:00
|
|
|
case DevFmtUInt:
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("%s samples not supported\n", DevFmtTypeString(device->FmtType));
|
2018-12-27 12:04:18 -08:00
|
|
|
return ALC_INVALID_VALUE;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(device->FmtChans)
|
|
|
|
{
|
|
|
|
case DevFmtMono:
|
|
|
|
requestedFormat.mChannelsPerFrame = 1;
|
|
|
|
break;
|
|
|
|
case DevFmtStereo:
|
|
|
|
requestedFormat.mChannelsPerFrame = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DevFmtQuad:
|
|
|
|
case DevFmtX51:
|
2014-11-07 00:54:16 -08:00
|
|
|
case DevFmtX51Rear:
|
2011-06-27 23:49:17 -07:00
|
|
|
case DevFmtX61:
|
|
|
|
case DevFmtX71:
|
2017-04-12 18:26:07 -07:00
|
|
|
case DevFmtAmbi3D:
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("%s not supported\n", DevFmtChannelsString(device->FmtChans));
|
2018-12-27 12:04:18 -08:00
|
|
|
return ALC_INVALID_VALUE;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
requestedFormat.mBytesPerFrame = requestedFormat.mChannelsPerFrame * requestedFormat.mBitsPerChannel / 8;
|
|
|
|
requestedFormat.mBytesPerPacket = requestedFormat.mBytesPerFrame;
|
|
|
|
requestedFormat.mSampleRate = device->Frequency;
|
|
|
|
requestedFormat.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
requestedFormat.mReserved = 0;
|
|
|
|
requestedFormat.mFramesPerPacket = 1;
|
|
|
|
|
|
|
|
// save requested format description for later use
|
2018-12-27 12:04:18 -08:00
|
|
|
self->mFormat = requestedFormat;
|
|
|
|
self->mFrameSize = device->frameSizeFromFmt();
|
2011-06-27 23:49:17 -07:00
|
|
|
|
|
|
|
// Use intermediate format for sample rate conversion (outputFormat)
|
|
|
|
// Set sample rate to the same as hardware for resampling later
|
|
|
|
outputFormat = requestedFormat;
|
|
|
|
outputFormat.mSampleRate = hardwareFormat.mSampleRate;
|
|
|
|
|
|
|
|
// The output format should be the requested format, but using the hardware sample rate
|
|
|
|
// This is because the AudioUnit will automatically scale other properties, except for sample rate
|
2018-12-27 12:04:18 -08:00
|
|
|
err = AudioUnitSetProperty(self->mAudioUnit, kAudioUnitProperty_StreamFormat,
|
|
|
|
kAudioUnitScope_Output, 1, (void *)&outputFormat, sizeof(outputFormat));
|
2011-06-27 23:49:17 -07:00
|
|
|
if(err != noErr)
|
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("AudioUnitSetProperty failed\n");
|
2018-12-27 12:04:18 -08:00
|
|
|
return ALC_INVALID_VALUE;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the AudioUnit output format frame count
|
2018-12-27 12:04:18 -08:00
|
|
|
ALuint64 FrameCount64{device->UpdateSize};
|
|
|
|
FrameCount64 = (FrameCount64*outputFormat.mSampleRate + device->Frequency-1) /
|
|
|
|
device->Frequency;
|
|
|
|
FrameCount64 += MAX_RESAMPLE_PADDING*2;
|
|
|
|
if(FrameCount64 > std::numeric_limits<uint32_t>::max()/2)
|
2011-06-27 23:49:17 -07:00
|
|
|
{
|
2018-12-27 12:04:18 -08:00
|
|
|
ERR("FrameCount too large\n");
|
|
|
|
return ALC_INVALID_VALUE;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
outputFrameCount = static_cast<uint32_t>(FrameCount64);
|
|
|
|
err = AudioUnitSetProperty(self->mAudioUnit, kAudioUnitProperty_MaximumFramesPerSlice,
|
|
|
|
kAudioUnitScope_Output, 0, &outputFrameCount, sizeof(outputFrameCount));
|
2011-06-27 23:49:17 -07:00
|
|
|
if(err != noErr)
|
|
|
|
{
|
2018-12-27 12:04:18 -08:00
|
|
|
ERR("AudioUnitSetProperty failed: %d\n", err);
|
|
|
|
return ALC_INVALID_VALUE;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
// Set up sample converter if needed
|
|
|
|
if(outputFormat.mSampleRate != device->Frequency)
|
|
|
|
self->mConverter.reset(CreateSampleConverter(device->FmtType, device->FmtType,
|
|
|
|
self->mFormat.mChannelsPerFrame, hardwareFormat.mSampleRate, device->Frequency,
|
|
|
|
BSinc24Resampler));
|
2011-06-27 23:49:17 -07:00
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
self->mRing.reset(ll_ringbuffer_create(outputFrameCount, self->mFrameSize, false));
|
|
|
|
if(!self->mRing) return ALC_INVALID_VALUE;
|
2011-06-27 23:49:17 -07:00
|
|
|
|
2018-11-18 18:45:45 -08:00
|
|
|
device->DeviceName = name;
|
2011-08-24 14:44:15 -07:00
|
|
|
return ALC_NO_ERROR;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
|
|
|
|
static ALCboolean ALCcoreAudioCapture_start(ALCcoreAudioCapture *self)
|
2011-06-27 23:49:17 -07:00
|
|
|
{
|
2018-12-27 12:04:18 -08:00
|
|
|
OSStatus err = AudioOutputUnitStart(self->mAudioUnit);
|
2011-06-27 23:49:17 -07:00
|
|
|
if(err != noErr)
|
2017-04-09 11:21:02 -07:00
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("AudioOutputUnitStart failed\n");
|
2017-04-09 11:21:02 -07:00
|
|
|
return ALC_FALSE;
|
|
|
|
}
|
|
|
|
return ALC_TRUE;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
static void ALCcoreAudioCapture_stop(ALCcoreAudioCapture *self)
|
2011-06-27 23:49:17 -07:00
|
|
|
{
|
2018-12-27 12:04:18 -08:00
|
|
|
OSStatus err = AudioOutputUnitStop(self->mAudioUnit);
|
2011-06-27 23:49:17 -07:00
|
|
|
if(err != noErr)
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("AudioOutputUnitStop failed\n");
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
static ALCenum ALCcoreAudioCapture_captureSamples(ALCcoreAudioCapture *self, ALCvoid *buffer, ALCuint samples)
|
2011-06-27 23:49:17 -07:00
|
|
|
{
|
2018-12-27 12:04:18 -08:00
|
|
|
RingBuffer *ring{self->mRing.get()};
|
2011-06-27 23:49:17 -07:00
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
if(!self->mConverter)
|
|
|
|
{
|
|
|
|
ring->read(buffer, samples);
|
|
|
|
return ALC_NO_ERROR;
|
|
|
|
}
|
2011-06-27 23:49:17 -07:00
|
|
|
|
2018-12-27 12:04:18 -08:00
|
|
|
auto rec_vec = ring->getReadVector();
|
|
|
|
const void *src0{rec_vec.first.buf};
|
|
|
|
auto src0len = static_cast<ALsizei>(rec_vec.first.len);
|
|
|
|
auto got = static_cast<ALuint>(SampleConverterInput(self->mConverter.get(), &src0, &src0len,
|
|
|
|
buffer, samples));
|
|
|
|
size_t total_read{rec_vec.first.len - src0len};
|
|
|
|
if(got < samples && !src0len && rec_vec.second.len > 0)
|
2011-09-14 02:01:35 -07:00
|
|
|
{
|
2018-12-27 12:04:18 -08:00
|
|
|
const void *src1{rec_vec.second.buf};
|
|
|
|
auto src1len = static_cast<ALsizei>(rec_vec.second.len);
|
|
|
|
got += static_cast<ALuint>(SampleConverterInput(self->mConverter.get(), &src1, &src1len,
|
|
|
|
static_cast<char*>(buffer)+got, samples-got));
|
|
|
|
total_read += rec_vec.second.len - src1len;
|
2011-06-27 23:49:17 -07:00
|
|
|
}
|
2018-12-27 12:04:18 -08:00
|
|
|
|
|
|
|
ring->readAdvance(total_read);
|
2011-09-14 02:01:35 -07:00
|
|
|
return ALC_NO_ERROR;
|
2011-03-15 04:58:56 -07:00
|
|
|
}
|
|
|
|
|
2017-04-09 11:21:02 -07:00
|
|
|
static ALCuint ALCcoreAudioCapture_availableSamples(ALCcoreAudioCapture *self)
|
2011-09-18 20:27:34 -07:00
|
|
|
{
|
2018-12-27 12:04:18 -08:00
|
|
|
RingBuffer *ring{self->mRing.get()};
|
|
|
|
|
|
|
|
if(!self->mConverter) return ring->readSpace();
|
|
|
|
return SampleConverterAvailableOut(self->mConverter.get(), ring->readSpace());
|
2011-09-18 20:27:34 -07:00
|
|
|
}
|
|
|
|
|
2011-09-14 02:01:35 -07:00
|
|
|
|
2018-11-15 21:24:09 -08:00
|
|
|
BackendFactory &CoreAudioBackendFactory::getFactory()
|
2017-04-09 11:21:02 -07:00
|
|
|
{
|
2018-11-15 21:24:09 -08:00
|
|
|
static CoreAudioBackendFactory factory{};
|
|
|
|
return factory;
|
2017-04-09 11:21:02 -07:00
|
|
|
}
|
|
|
|
|
2018-11-15 21:24:09 -08:00
|
|
|
bool CoreAudioBackendFactory::init() { return true; }
|
2017-04-09 11:21:02 -07:00
|
|
|
|
2018-11-15 21:24:09 -08:00
|
|
|
bool CoreAudioBackendFactory::querySupport(ALCbackend_Type type)
|
|
|
|
{ return (type == ALCbackend_Playback || ALCbackend_Capture); }
|
2011-03-15 04:58:56 -07:00
|
|
|
|
2018-12-24 19:29:01 -08:00
|
|
|
void CoreAudioBackendFactory::probe(DevProbe type, std::string *outnames)
|
2011-03-15 04:58:56 -07:00
|
|
|
{
|
2011-06-14 04:02:58 -07:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case ALL_DEVICE_PROBE:
|
|
|
|
case CAPTURE_DEVICE_PROBE:
|
2018-11-15 04:24:33 -08:00
|
|
|
/* Includes null char. */
|
|
|
|
outnames->append(ca_device, sizeof(ca_device));
|
2011-06-14 04:02:58 -07:00
|
|
|
break;
|
|
|
|
}
|
2011-03-15 04:58:56 -07:00
|
|
|
}
|
2017-04-09 11:21:02 -07:00
|
|
|
|
2018-11-15 21:24:09 -08:00
|
|
|
ALCbackend *CoreAudioBackendFactory::createBackend(ALCdevice *device, ALCbackend_Type type)
|
2017-04-09 11:21:02 -07:00
|
|
|
{
|
|
|
|
if(type == ALCbackend_Playback)
|
|
|
|
{
|
|
|
|
ALCcoreAudioPlayback *backend;
|
|
|
|
NEW_OBJ(backend, ALCcoreAudioPlayback)(device);
|
2018-11-15 21:24:09 -08:00
|
|
|
if(!backend) return nullptr;
|
2017-04-09 11:21:02 -07:00
|
|
|
return STATIC_CAST(ALCbackend, backend);
|
|
|
|
}
|
|
|
|
if(type == ALCbackend_Capture)
|
|
|
|
{
|
|
|
|
ALCcoreAudioCapture *backend;
|
|
|
|
NEW_OBJ(backend, ALCcoreAudioCapture)(device);
|
2018-11-15 21:24:09 -08:00
|
|
|
if(!backend) return nullptr;
|
2017-04-09 11:21:02 -07:00
|
|
|
return STATIC_CAST(ALCbackend, backend);
|
|
|
|
}
|
|
|
|
|
2018-11-15 21:24:09 -08:00
|
|
|
return nullptr;
|
2017-04-09 11:21:02 -07:00
|
|
|
}
|