diff --git a/src/apple/AudioUnit.hxx b/src/apple/AudioUnit.hxx index bc6f72b53..c4a5b2ef7 100644 --- a/src/apple/AudioUnit.hxx +++ b/src/apple/AudioUnit.hxx @@ -52,4 +52,60 @@ AudioUnitGetPropertyT(AudioUnit inUnit, AudioUnitPropertyID inID, return value; } +template +void +AudioUnitSetPropertyT(AudioUnit inUnit, AudioUnitPropertyID inID, + AudioUnitScope inScope, + AudioUnitElement inElement, + const T &value) +{ + OSStatus status = AudioUnitSetProperty(inUnit, inID, inScope, + inElement, + &value, sizeof(value)); + if (status != noErr) + Apple::ThrowOSStatus(status); +} + +inline void +AudioUnitSetCurrentDevice(AudioUnit inUnit, const AudioDeviceID &value) +{ + AudioUnitSetPropertyT(inUnit, kAudioOutputUnitProperty_CurrentDevice, + kAudioUnitScope_Global, 0, + value); +} + +inline void +AudioUnitSetInputStreamFormat(AudioUnit inUnit, + const AudioStreamBasicDescription &value) +{ + AudioUnitSetPropertyT(inUnit, kAudioUnitProperty_StreamFormat, + kAudioUnitScope_Input, 0, + value); +} + +inline void +AudioUnitSetInputRenderCallback(AudioUnit inUnit, + const AURenderCallbackStruct &value) +{ + AudioUnitSetPropertyT(inUnit, kAudioUnitProperty_SetRenderCallback, + kAudioUnitScope_Input, 0, + value); +} + +inline UInt32 +AudioUnitGetBufferFrameSize(AudioUnit inUnit) +{ + return AudioUnitGetPropertyT(inUnit, + kAudioDevicePropertyBufferFrameSize, + kAudioUnitScope_Global, 0); +} + +inline void +AudioUnitSetBufferFrameSize(AudioUnit inUnit, const UInt32 &value) +{ + AudioUnitSetPropertyT(inUnit, kAudioDevicePropertyBufferFrameSize, + kAudioUnitScope_Global, 0, + value); +} + #endif diff --git a/src/output/plugins/OSXOutputPlugin.cxx b/src/output/plugins/OSXOutputPlugin.cxx index b3a58f972..fa65fa437 100644 --- a/src/output/plugins/OSXOutputPlugin.cxx +++ b/src/output/plugins/OSXOutputPlugin.cxx @@ -450,24 +450,14 @@ osx_output_set_buffer_size(AudioUnit au, AudioStreamBasicDescription desc) kAudioUnitScope_Global, 0); - UInt32 buffer_frame_size = value_range.mMaximum; - OSStatus err; - err = AudioUnitSetProperty(au, - kAudioDevicePropertyBufferFrameSize, - kAudioUnitScope_Global, - 0, - &buffer_frame_size, - sizeof(buffer_frame_size)); - if (err != noErr) - FormatWarning(osx_output_domain, - "Failed to set maximum buffer size: %d", - err); - - buffer_frame_size = AudioUnitGetPropertyT(au, - kAudioDevicePropertyBufferFrameSize, - kAudioUnitScope_Global, - 0); + try { + AudioUnitSetBufferFrameSize(au, value_range.mMaximum); + } catch (...) { + LogError(std::current_exception(), + "Failed to set maximum buffer size"); + } + auto buffer_frame_size = AudioUnitGetBufferFrameSize(au); buffer_frame_size *= desc.mBytesPerFrame; // We set the frame size to a power of two integer that @@ -583,15 +573,7 @@ osx_output_set_device(OSXOutput *oo) "found matching device: ID=%u, name=%s", (unsigned)id, oo->device_name); - OSStatus status; - status = AudioUnitSetProperty(oo->au, - kAudioOutputUnitProperty_CurrentDevice, - kAudioUnitScope_Global, - 0, - &id, sizeof(id)); - if (status != noErr) - Apple::ThrowOSStatus(status, - "Unable to set OS X audio output device"); + AudioUnitSetCurrentDevice(oo->au, id); oo->dev_id = id; FormatDebug(osx_output_domain, @@ -742,29 +724,15 @@ OSXOutput::Open(AudioFormat &audio_format) dop_enabled = params.dop; #endif - OSStatus status = - AudioUnitSetProperty(au, kAudioUnitProperty_StreamFormat, - kAudioUnitScope_Input, 0, - &asbd, - sizeof(asbd)); - if (status != noErr) - throw std::runtime_error("Unable to set format on OS X device"); + AudioUnitSetInputStreamFormat(au, asbd); AURenderCallbackStruct callback; callback.inputProc = osx_render; callback.inputProcRefCon = this; - status = - AudioUnitSetProperty(au, - kAudioUnitProperty_SetRenderCallback, - kAudioUnitScope_Input, 0, - &callback, sizeof(callback)); - if (status != noErr) { - AudioComponentInstanceDispose(au); - throw std::runtime_error("Unable to set callback for OS X audio unit"); - } + AudioUnitSetInputRenderCallback(au, callback); - status = AudioUnitInitialize(au); + OSStatus status = AudioUnitInitialize(au); if (status != noErr) Apple::ThrowOSStatus(status, "Unable to initialize OS X audio unit");