diff --git a/modules/juce_audio_processors/format/juce_AudioPluginFormat.cpp b/modules/juce_audio_processors/format/juce_AudioPluginFormat.cpp index d88ed0029e..c42d3fb7e6 100644 --- a/modules/juce_audio_processors/format/juce_AudioPluginFormat.cpp +++ b/modules/juce_audio_processors/format/juce_AudioPluginFormat.cpp @@ -27,7 +27,7 @@ namespace juce { -AudioPluginFormat::AudioPluginFormat() noexcept {} +AudioPluginFormat::AudioPluginFormat() {} AudioPluginFormat::~AudioPluginFormat() {} std::unique_ptr AudioPluginFormat::createInstanceFromDescription (const PluginDescription& desc, @@ -69,41 +69,31 @@ std::unique_ptr AudioPluginFormat::createInstanceFromDescri return instance; } +struct AudioPluginFormat::AsyncCreateMessage : public Message +{ + AsyncCreateMessage (const PluginDescription& d, double sr, int size, PluginCreationCallback call) + : desc (d), sampleRate (sr), bufferSize (size), callbackToUse (std::move (call)) + { + } + + PluginDescription desc; + double sampleRate; + int bufferSize; + PluginCreationCallback callbackToUse; +}; + void AudioPluginFormat::createPluginInstanceAsync (const PluginDescription& description, double initialSampleRate, int initialBufferSize, PluginCreationCallback callback) { jassert (callback != nullptr); + postMessage (new AsyncCreateMessage (description, initialSampleRate, initialBufferSize, std::move (callback))); +} - if (MessageManager::getInstance()->isThisTheMessageThread()) - { - createPluginInstance (description, initialSampleRate, initialBufferSize, std::move (callback)); - return; - } - - struct InvokeOnMessageThread : public CallbackMessage - { - InvokeOnMessageThread (AudioPluginFormat& f, const PluginDescription& d, - double sr, int size, PluginCreationCallback call) - : format (f), desc (d), sampleRate (sr), bufferSize (size), - callbackToUse (std::move (call)) - { - post(); - } - - void messageCallback() override - { - format.createPluginInstance (desc, sampleRate, bufferSize, std::move (callbackToUse)); - } - - AudioPluginFormat& format; - PluginDescription desc; - double sampleRate; - int bufferSize; - PluginCreationCallback callbackToUse; - }; - - new InvokeOnMessageThread (*this, description, initialSampleRate, initialBufferSize, std::move (callback)); +void AudioPluginFormat::handleMessage (const Message& message) +{ + if (auto m = dynamic_cast (&message)) + createPluginInstance (m->desc, m->sampleRate, m->bufferSize, std::move (m->callbackToUse)); } } // namespace juce diff --git a/modules/juce_audio_processors/format/juce_AudioPluginFormat.h b/modules/juce_audio_processors/format/juce_AudioPluginFormat.h index 4caefc1d6c..88f263b512 100644 --- a/modules/juce_audio_processors/format/juce_AudioPluginFormat.h +++ b/modules/juce_audio_processors/format/juce_AudioPluginFormat.h @@ -35,11 +35,11 @@ namespace juce @tags{Audio} */ -class JUCE_API AudioPluginFormat +class JUCE_API AudioPluginFormat : private MessageListener { public: /** Destructor. */ - virtual ~AudioPluginFormat(); + ~AudioPluginFormat() override; //============================================================================== /** Returns the format name. @@ -136,7 +136,7 @@ protected: //============================================================================== friend class AudioPluginFormatManager; - AudioPluginFormat() noexcept; + AudioPluginFormat(); /** Implementors must override this function. This is guaranteed to be called on the message thread. You may call the callback on any thread. @@ -147,6 +147,9 @@ protected: virtual bool requiresUnblockedMessageThreadDuringCreation (const PluginDescription&) const noexcept = 0; private: + struct AsyncCreateMessage; + void handleMessage (const Message&) override; + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioPluginFormat) };