AU sample rate


#1

(This is a hosting question rather than a plugin question, so I hope this
is the right forum, please let me know if not).

Some AUs are only capable of changing sample rate when the plugin
is uninitialised (e.g. Kontakt 4).

Given that the factory creates initialised instances, I got around this
by adding the following at the start of prepareToPlay(). The subsequent
call to initialise() initialises the plugin with the correct sample rate.

    if (audioUnit)
    {
        if (initialised)
            AudioUnitUninitialize (audioUnit);
        initialised = false;
        Float64 sr = sampleRate_;
        AudioUnitSetProperty (audioUnit, kAudioUnitProperty_SampleRate, kAudioUnitScope_Input, 0, &sr, sizeof(Float64));
        AudioUnitSetProperty (audioUnit, kAudioUnitProperty_SampleRate, kAudioUnitScope_Output, 0, &sr, sizeof(Float64));
    }

#2

Interesting, thanks!

Might be a bit wasteful to keep resetting the plugin unnecessarily though - how about this variation, which will check that the sample rate actually needs to be changed before doing it:

[code]
if (audioUnit != 0)
{
Float64 sampleRateIn = 0, sampleRateOut = 0;
AudioUnitGetProperty (audioUnit, kAudioUnitProperty_SampleRate, kAudioUnitScope_Input, 0, &sampleRateIn, sizeof (sampleRateIn));
AudioUnitGetProperty (audioUnit, kAudioUnitProperty_SampleRate, kAudioUnitScope_Output, 0, &sampleRateOut, sizeof (sampleRateOut));

    if (sampleRateIn != sampleRate_ || sampleRateOut != sampleRate_)
    {
        if (initialised)
        {
            AudioUnitUninitialize (audioUnit);
            initialised = false;
        }

        Float64 sr = sampleRate_;
        AudioUnitSetProperty (audioUnit, kAudioUnitProperty_SampleRate, kAudioUnitScope_Input, 0, &sr, sizeof (Float64));
        AudioUnitSetProperty (audioUnit, kAudioUnitProperty_SampleRate, kAudioUnitScope_Output, 0, &sr, sizeof (Float64));
    }
}  [/code]

#3

Good call. Note that you have to pass the size by pointer in AudioUnitGetProperty.
I checked Kontakt 4, and it works fine.

Do you think this might make it into the juce trunk?

Thanks,
Joe


#4

Thanks! Yes, I’ll pop this in the trunk next time I do a check-in…