Commit fb83936f authored by Max Kellermann's avatar Max Kellermann

apple/AudioUnit: add AudioUnitSetPropertyT()

parent db8bf52f
...@@ -52,4 +52,60 @@ AudioUnitGetPropertyT(AudioUnit inUnit, AudioUnitPropertyID inID, ...@@ -52,4 +52,60 @@ AudioUnitGetPropertyT(AudioUnit inUnit, AudioUnitPropertyID inID,
return value; return value;
} }
template<typename T>
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<UInt32>(inUnit,
kAudioDevicePropertyBufferFrameSize,
kAudioUnitScope_Global, 0);
}
inline void
AudioUnitSetBufferFrameSize(AudioUnit inUnit, const UInt32 &value)
{
AudioUnitSetPropertyT(inUnit, kAudioDevicePropertyBufferFrameSize,
kAudioUnitScope_Global, 0,
value);
}
#endif #endif
...@@ -450,24 +450,14 @@ osx_output_set_buffer_size(AudioUnit au, AudioStreamBasicDescription desc) ...@@ -450,24 +450,14 @@ osx_output_set_buffer_size(AudioUnit au, AudioStreamBasicDescription desc)
kAudioUnitScope_Global, kAudioUnitScope_Global,
0); 0);
UInt32 buffer_frame_size = value_range.mMaximum; try {
OSStatus err; AudioUnitSetBufferFrameSize(au, value_range.mMaximum);
err = AudioUnitSetProperty(au, } catch (...) {
kAudioDevicePropertyBufferFrameSize, LogError(std::current_exception(),
kAudioUnitScope_Global, "Failed to set maximum buffer size");
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<UInt32>(au,
kAudioDevicePropertyBufferFrameSize,
kAudioUnitScope_Global,
0);
auto buffer_frame_size = AudioUnitGetBufferFrameSize(au);
buffer_frame_size *= desc.mBytesPerFrame; buffer_frame_size *= desc.mBytesPerFrame;
// We set the frame size to a power of two integer that // We set the frame size to a power of two integer that
...@@ -583,15 +573,7 @@ osx_output_set_device(OSXOutput *oo) ...@@ -583,15 +573,7 @@ osx_output_set_device(OSXOutput *oo)
"found matching device: ID=%u, name=%s", "found matching device: ID=%u, name=%s",
(unsigned)id, oo->device_name); (unsigned)id, oo->device_name);
OSStatus status; AudioUnitSetCurrentDevice(oo->au, id);
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");
oo->dev_id = id; oo->dev_id = id;
FormatDebug(osx_output_domain, FormatDebug(osx_output_domain,
...@@ -742,29 +724,15 @@ OSXOutput::Open(AudioFormat &audio_format) ...@@ -742,29 +724,15 @@ OSXOutput::Open(AudioFormat &audio_format)
dop_enabled = params.dop; dop_enabled = params.dop;
#endif #endif
OSStatus status = AudioUnitSetInputStreamFormat(au, asbd);
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");
AURenderCallbackStruct callback; AURenderCallbackStruct callback;
callback.inputProc = osx_render; callback.inputProc = osx_render;
callback.inputProcRefCon = this; callback.inputProcRefCon = this;
status = AudioUnitSetInputRenderCallback(au, callback);
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");
}
status = AudioUnitInitialize(au); OSStatus status = AudioUnitInitialize(au);
if (status != noErr) if (status != noErr)
Apple::ThrowOSStatus(status, "Unable to initialize OS X audio unit"); Apple::ThrowOSStatus(status, "Unable to initialize OS X audio unit");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment