Invoke AudioUnitGetProperty from inside PluginProcessor

I wanted to invoke something along the lines of the following, e.g. in prepareToPlay:

void PluginProcessor::prepareToPlay (double sampleRate, int estimatedMaxSizeOfBuffer)
{
    if (wrapperType == WrapperType::wrapperType_AudioUnit)
    {
        OSStatus status;
        os_workgroup_t os_workgroup { nullptr };
        uint32_t os_workgroup_index_size;

        AudioUnit ioUnit { nullptr };

        if (status = AudioUnitGetProperty (ioUnit,
                                           kAudioOutputUnitProperty_OSWorkgroup,
                                           kAudioUnitScope_Global,
                                           0,
                                           &os_workgroup,
                                           &os_workgroup_index_size);
            status != noErr)
        {
            DBG ("AudioUnitSetProperty kAudioOutputUnitProperty_OSWorkgroup - Failed with OSStatus: " +
                 std::to_string (status));
        }
    }

(...)
}

For the above to work, I need to fetch the “AudioUnit ioUnit” that the AudioProcessor is part of. Is there a way to do that?

While e.g. for VST3 there’s methods like:
virtual VST3ClientExtensions* getVST3ClientExtensions();
I can’t seem to find anything with which I could fetch such AU-specific information from inside the plugin.

Mind you I’m not very experienced as an Apple-dev so please forgive my naivety.
This follows from my earlier question thread - I figured a dedicated topic is best since this is a much more specific question than the above broader topic.

Thanks!

Although it’s possible to get the AudioUnit handle through ExtensionsVisitor/AudioUnitClient, I wouldn’t recommend it, as such code will be more brittle than using JUCE wrappers.

I don’t think this is the property you’d want - the docs make it sound like this is the workgroup associated with an input or output device. In a plugin, you probably don’t know which devices the host is using, so it will be difficult/impossible to get the correct workgroup in this way.

When loaded as an AudioUnit, an AudioProcessor may receive information about the workgroup that the host is using for playback via the audioWorkgroupContextChanged virtual function. This will receive the current AudioWorkgroup, whenever this changes. Then, you can use AudioWorkgroup::join to join any thread to this workgroup (i.e. not just juce::Thread, any thread that calls join may join the workgroup, as long as other preconditions, such as being a realtime thread, are met).

1 Like

Indeed using audioWorkgroupContextChanged would be infinitely better, than side-stepping the Juce API. While I was aware of it I mistakenly assumed it wouldn’t be useful for my non-Juce threads.

I’ll see if I can use that instead, the slight issue is the thread that should join is started in a library which owns it, but I’ll see how to call AudioWorkgroup::join() from its audio threads anyhow.

Thank you!