Use AudioComponent instead of Carbon Component

This commit is contained in:
Jacob Vosmaer 2016-07-02 23:35:44 +02:00
parent 1c4c0fe8a1
commit 4728f7c697

View File

@ -28,7 +28,7 @@
#include "system/ByteOrder.hxx" #include "system/ByteOrder.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <CoreAudio/AudioHardware.h> #include <CoreAudio/CoreAudio.h>
#include <AudioUnit/AudioUnit.h> #include <AudioUnit/AudioUnit.h>
#include <CoreServices/CoreServices.h> #include <CoreServices/CoreServices.h>
@ -40,7 +40,7 @@ struct OSXOutput {
/* only applicable with kAudioUnitSubType_HALOutput */ /* only applicable with kAudioUnitSubType_HALOutput */
const char *device_name; const char *device_name;
AudioUnit au; AudioComponentInstance au;
Mutex mutex; Mutex mutex;
Cond condition; Cond condition;
@ -109,6 +109,7 @@ osx_output_set_device(OSXOutput *oo, Error &error)
OSStatus status; OSStatus status;
UInt32 size, numdevices; UInt32 size, numdevices;
AudioDeviceID *deviceids = nullptr; AudioDeviceID *deviceids = nullptr;
AudioObjectPropertyAddress propaddr;
char name[256]; char name[256];
unsigned int i; unsigned int i;
@ -116,9 +117,8 @@ osx_output_set_device(OSXOutput *oo, Error &error)
goto done; goto done;
/* how many audio devices are there? */ /* how many audio devices are there? */
status = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, propaddr = { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
&size, status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propaddr, 0, nullptr, &size);
nullptr);
if (status != noErr) { if (status != noErr) {
error.Format(osx_output_domain, status, error.Format(osx_output_domain, status,
"Unable to determine number of OS X audio devices: %s", "Unable to determine number of OS X audio devices: %s",
@ -130,9 +130,7 @@ osx_output_set_device(OSXOutput *oo, Error &error)
/* what are the available audio device IDs? */ /* what are the available audio device IDs? */
numdevices = size / sizeof(AudioDeviceID); numdevices = size / sizeof(AudioDeviceID);
deviceids = new AudioDeviceID[numdevices]; deviceids = new AudioDeviceID[numdevices];
status = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propaddr, 0, nullptr, &size, deviceids);
&size,
deviceids);
if (status != noErr) { if (status != noErr) {
error.Format(osx_output_domain, status, error.Format(osx_output_domain, status,
"Unable to determine OS X audio device IDs: %s", "Unable to determine OS X audio device IDs: %s",
@ -142,11 +140,10 @@ osx_output_set_device(OSXOutput *oo, Error &error)
} }
/* which audio device matches oo->device_name? */ /* which audio device matches oo->device_name? */
propaddr = { kAudioObjectPropertyName, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
for (i = 0; i < numdevices; i++) { for (i = 0; i < numdevices; i++) {
size = sizeof(name); size = sizeof(name);
status = AudioDeviceGetProperty(deviceids[i], 0, false, status = AudioObjectGetPropertyData(deviceids[i], &propaddr, 0, nullptr, &size, name);
kAudioDevicePropertyDeviceName,
&size, name);
if (status != noErr) { if (status != noErr) {
error.Format(osx_output_domain, status, error.Format(osx_output_domain, status,
"Unable to determine OS X device name " "Unable to determine OS X device name "
@ -238,21 +235,21 @@ osx_output_enable(AudioOutput *ao, Error &error)
{ {
OSXOutput *oo = (OSXOutput *)ao; OSXOutput *oo = (OSXOutput *)ao;
ComponentDescription desc; AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output; desc.componentType = kAudioUnitType_Output;
desc.componentSubType = oo->component_subtype; desc.componentSubType = oo->component_subtype;
desc.componentManufacturer = kAudioUnitManufacturer_Apple; desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0; desc.componentFlags = 0;
desc.componentFlagsMask = 0; desc.componentFlagsMask = 0;
Component comp = FindNextComponent(nullptr, &desc); AudioComponent comp = AudioComponentFindNext(nullptr, &desc);
if (comp == 0) { if (comp == 0) {
error.Set(osx_output_domain, error.Set(osx_output_domain,
"Error finding OS X component"); "Error finding OS X component");
return false; return false;
} }
OSStatus status = OpenAComponent(comp, &oo->au); OSStatus status = AudioComponentInstanceNew(comp, &oo->au);
if (status != noErr) { if (status != noErr) {
error.Format(osx_output_domain, status, error.Format(osx_output_domain, status,
"Unable to open OS X component: %s", "Unable to open OS X component: %s",
@ -261,7 +258,7 @@ osx_output_enable(AudioOutput *ao, Error &error)
} }
if (!osx_output_set_device(oo, error)) { if (!osx_output_set_device(oo, error)) {
CloseComponent(oo->au); AudioComponentInstanceDispose(oo->au);
return false; return false;
} }
@ -269,14 +266,14 @@ osx_output_enable(AudioOutput *ao, Error &error)
callback.inputProc = osx_render; callback.inputProc = osx_render;
callback.inputProcRefCon = oo; callback.inputProcRefCon = oo;
ComponentResult result = status =
AudioUnitSetProperty(oo->au, AudioUnitSetProperty(oo->au,
kAudioUnitProperty_SetRenderCallback, kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input, 0, kAudioUnitScope_Input, 0,
&callback, sizeof(callback)); &callback, sizeof(callback));
if (result != noErr) { if (status != noErr) {
CloseComponent(oo->au); AudioComponentInstanceDispose(oo->au);
error.Set(osx_output_domain, result, error.Set(osx_output_domain, status,
"unable to set callback for OS X audio unit"); "unable to set callback for OS X audio unit");
return false; return false;
} }
@ -289,7 +286,7 @@ osx_output_disable(AudioOutput *ao)
{ {
OSXOutput *oo = (OSXOutput *)ao; OSXOutput *oo = (OSXOutput *)ao;
CloseComponent(oo->au); AudioComponentInstanceDispose(oo->au);
} }
static void static void
@ -350,18 +347,18 @@ osx_output_open(AudioOutput *ao, AudioFormat &audio_format,
stream_description.mBytesPerFrame = stream_description.mBytesPerPacket; stream_description.mBytesPerFrame = stream_description.mBytesPerPacket;
stream_description.mChannelsPerFrame = audio_format.channels; stream_description.mChannelsPerFrame = audio_format.channels;
ComponentResult result = OSStatus status =
AudioUnitSetProperty(od->au, kAudioUnitProperty_StreamFormat, AudioUnitSetProperty(od->au, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, 0, kAudioUnitScope_Input, 0,
&stream_description, &stream_description,
sizeof(stream_description)); sizeof(stream_description));
if (result != noErr) { if (status != noErr) {
error.Set(osx_output_domain, result, error.Set(osx_output_domain, status,
"Unable to set format on OS X device"); "Unable to set format on OS X device");
return false; return false;
} }
OSStatus status = AudioUnitInitialize(od->au); status = AudioUnitInitialize(od->au);
if (status != noErr) { if (status != noErr) {
error.Format(osx_output_domain, status, error.Format(osx_output_domain, status,
"Unable to initialize OS X audio unit: %s", "Unable to initialize OS X audio unit: %s",