From 3bc38d1e3be3d830f7b00c50c0d4de9908a5ee8e Mon Sep 17 00:00:00 2001 From: jules Date: Thu, 7 Jan 2016 09:41:36 +0000 Subject: [PATCH] AudioUnit hosting: added code to handle plugin latency changes --- .../juce_AudioUnitPluginFormat.mm | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/modules/juce_audio_processors/format_types/juce_AudioUnitPluginFormat.mm b/modules/juce_audio_processors/format_types/juce_AudioUnitPluginFormat.mm index d42534cb66..24063c6a5a 100644 --- a/modules/juce_audio_processors/format_types/juce_AudioUnitPluginFormat.mm +++ b/modules/juce_audio_processors/format_types/juce_AudioUnitPluginFormat.mm @@ -445,12 +445,7 @@ public: (int) (numOutputBusChannels * numOutputBusses), (double) newSampleRate, estimatedSamplesPerBlock); - Float64 latencySecs = 0.0; - UInt32 latencySize = sizeof (latencySecs); - AudioUnitGetProperty (audioUnit, kAudioUnitProperty_Latency, kAudioUnitScope_Global, - 0, &latencySecs, &latencySize); - - setLatencySamples (roundToInt (latencySecs * newSampleRate)); + updateLatency(); { AudioStreamBasicDescription stream; @@ -889,6 +884,16 @@ public: } } + void updateLatency() + { + Float64 latencySecs = 0.0; + UInt32 latencySize = sizeof (latencySecs); + AudioUnitGetProperty (audioUnit, kAudioUnitProperty_Latency, kAudioUnitScope_Global, + 0, &latencySecs, &latencySize); + + setLatencySamples (roundToInt (latencySecs * getSampleRate())); + } + void handleIncomingMidiMessage (void*, const MidiMessage& message) { const ScopedLock sl (midiInLock); @@ -993,22 +998,24 @@ private: AUEventListenerAddEventType (eventListenerRef, nullptr, &event); } - // Add a listener for program changes - AudioUnitEvent event; - event.mArgument.mProperty.mAudioUnit = audioUnit; - event.mArgument.mProperty.mPropertyID = kAudioUnitProperty_PresentPreset; - event.mArgument.mProperty.mScope = kAudioUnitScope_Global; - event.mArgument.mProperty.mElement = 0; - - event.mEventType = kAudioUnitEvent_PropertyChange; - AUEventListenerAddEventType (eventListenerRef, nullptr, &event); - - // Add a listener for parameter list changes - event.mArgument.mProperty.mPropertyID = kAudioUnitProperty_ParameterList; - AUEventListenerAddEventType (eventListenerRef, nullptr, &event); + addPropertyChangeListener (kAudioUnitProperty_PresentPreset); + addPropertyChangeListener (kAudioUnitProperty_ParameterList); + addPropertyChangeListener (kAudioUnitProperty_Latency); } } + void addPropertyChangeListener (AudioUnitPropertyID type) const + { + AudioUnitEvent event; + event.mEventType = kAudioUnitEvent_PropertyChange; + event.mArgument.mProperty.mPropertyID = type; + event.mArgument.mProperty.mAudioUnit = audioUnit; + event.mArgument.mProperty.mPropertyID = kAudioUnitProperty_PresentPreset; + event.mArgument.mProperty.mScope = kAudioUnitScope_Global; + event.mArgument.mProperty.mElement = 0; + AUEventListenerAddEventType (eventListenerRef, nullptr, &event); + } + void eventCallback (const AudioUnitEvent& event, AudioUnitParameterValue newValue) { switch (event.mEventType) @@ -1040,6 +1047,8 @@ private: updateHostDisplay(); else if (event.mArgument.mProperty.mPropertyID == kAudioUnitProperty_PresentPreset) sendAllParametersChangedEvents(); + else if (event.mArgument.mProperty.mPropertyID == kAudioUnitProperty_Latency) + updateLatency(); break; }