Skip to content

Instantly share code, notes, and snippets.

@mathieugarcia
Created February 9, 2023 18:41
Show Gist options
  • Save mathieugarcia/b3d4cd3aa71db340b052728281c143e8 to your computer and use it in GitHub Desktop.
Save mathieugarcia/b3d4cd3aa71db340b052728281c143e8 to your computer and use it in GitHub Desktop.
Patch for te::ExternalPlugin for async instantiation of AUv3 plugins on iOS
diff --git a/modules/tracktion_engine/plugins/external/tracktion_ExternalPlugin.cpp b/modules/tracktion_engine/plugins/external/tracktion_ExternalPlugin.cpp
index 46d380c2b3..b490fabdb1 100644
--- a/modules/tracktion_engine/plugins/external/tracktion_ExternalPlugin.cpp
+++ b/modules/tracktion_engine/plugins/external/tracktion_ExternalPlugin.cpp
@@ -823,19 +823,40 @@ void ExternalPlugin::doFullInitialisation()
identiferString = createIdentifierString (desc);
updateDebugName();
- if (processing && pluginInstance == nullptr && engine.getEngineBehaviour().shouldLoadPlugin (*this))
+ if ((processing && pluginInstance == nullptr && engine.getEngineBehaviour().shouldLoadPlugin (*this)) ||
+ waitingForInstance)
{
if (isDisabled())
return;
CRASH_TRACER_PLUGIN (getDebugName());
loadError = {};
-
+
+ #if JUCE_IOS
+ // AUv3 requires async instantiation on iOS
+ if (isAU())
+ {
+ if (!waitingForInstance)
+ {
+ waitingForInstance = true;
+ createPluginInstanceAsync (*foundDesc);
+ return;
+ }
+ if (!asyncError.isEmpty())
+ {
+ loadError = asyncError;
+ asyncError = {};
+ }
+ jassert (pluginInstance);
+ waitingForInstance = false;
+ }
+ #else
callBlocking ([this, &foundDesc]
{
CRASH_TRACER_PLUGIN (getDebugName());
loadError = createPluginInstance (*foundDesc);
});
+ #endif
if (pluginInstance != nullptr)
{
@@ -1301,7 +1322,7 @@ void ExternalPlugin::applyToBuffer (const PluginRenderContext& fc)
{
const bool processedBypass = fc.allowBypassedProcessing && ! isEnabled();
- if (pluginInstance != nullptr && (processedBypass || isEnabled()))
+ if (pluginInstance != nullptr && (processedBypass || isEnabled()) && !waitingForInstance)
{
CRASH_TRACER_PLUGIN (getDebugName());
const juce::ScopedLock sl (lock);
@@ -1715,6 +1736,31 @@ juce::String ExternalPlugin::createPluginInstance (const juce::PluginDescription
return error;
}
+void ExternalPlugin::createPluginInstanceAsync (const juce::PluginDescription& description)
+{
+ jassert (! pluginInstance); // This should have already been deleted!
+
+ auto& dm = engine.getDeviceManager();
+
+ fullyInitialised = false;
+
+ juce::String error;
+ engine.getPluginManager().pluginFormatManager
+ .createPluginInstanceAsync (description, dm.getSampleRate(), dm.getBlockSize(),
+ [&](auto _instance, const auto& err) -> void {
+ pluginInstance = std::move(_instance);
+ asyncError = err;
+
+ if (pluginInstance != nullptr)
+ {
+ pluginInstance->enableAllBuses();
+ processorChangedManager = std::make_unique<ProcessorChangedManager> (*this);
+ initialiseFully();
+ initialise({ 0_tp, sampleRate, blockSizeSamples });
+ }
+ });
+}
+
void ExternalPlugin::deletePluginInstance()
{
processorChangedManager.reset();
diff --git a/modules/tracktion_engine/plugins/external/tracktion_ExternalPlugin.h b/modules/tracktion_engine/plugins/external/tracktion_ExternalPlugin.h
index 450473f5d0..5b050a16d8 100644
--- a/modules/tracktion_engine/plugins/external/tracktion_ExternalPlugin.h
+++ b/modules/tracktion_engine/plugins/external/tracktion_ExternalPlugin.h
@@ -150,6 +150,8 @@ private:
std::unique_ptr<PluginPlayHead> playhead;
bool fullyInitialised = false, supportsMPE = false, isFlushingLayoutToState = false;
+ bool waitingForInstance = false;
+ juce::String asyncError;
struct MPEChannelRemapper;
std::unique_ptr<MPEChannelRemapper> mpeRemapper;
@@ -160,6 +162,7 @@ private:
//==============================================================================
juce::String createPluginInstance (const juce::PluginDescription&);
+ void createPluginInstanceAsync (const juce::PluginDescription&);
void deletePluginInstance();
//==============================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment