From 91b1d324711c91f22feb38c7fd2e161f1f732bb0 Mon Sep 17 00:00:00 2001 From: David Schornsheim Date: Wed, 21 Feb 2024 16:22:01 +0100 Subject: [PATCH 1/5] Check active state before querying latency. Fixes #229 --- src/wrapasvst3.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wrapasvst3.cpp b/src/wrapasvst3.cpp index b118140b..234c742f 100644 --- a/src/wrapasvst3.cpp +++ b/src/wrapasvst3.cpp @@ -132,7 +132,7 @@ tresult PLUGIN_API ClapAsVst3::getState(IBStream* state) uint32 PLUGIN_API ClapAsVst3::getLatencySamples() { - if (_plugin->_ext._latency) + if (_plugin->_ext._latency && _active) { return _plugin->_ext._latency->get(_plugin->_plugin); } From 45b69bc01f77c848c745f0808df322a6d2a64858 Mon Sep 17 00:00:00 2001 From: David Schornsheim Date: Mon, 26 Feb 2024 12:17:35 +0100 Subject: [PATCH 2/5] Store missed latency call and report after activate() --- src/wrapasvst3.cpp | 19 ++++++++++++++++--- src/wrapasvst3.h | 1 + 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/wrapasvst3.cpp b/src/wrapasvst3.cpp index 234c742f..a699b410 100644 --- a/src/wrapasvst3.cpp +++ b/src/wrapasvst3.cpp @@ -87,6 +87,11 @@ tresult PLUGIN_API ClapAsVst3::setActive(TBool state) _expressionmap & clap_supported_note_expressions::AS_VST3_NOTE_EXPRESSION_TUNING); updateAudioBusses(); + if (_missedLatencyRequest) + { + latency_changed(); + } + _os_attached.on(); } if (!state) @@ -132,11 +137,19 @@ tresult PLUGIN_API ClapAsVst3::getState(IBStream* state) uint32 PLUGIN_API ClapAsVst3::getLatencySamples() { - if (_plugin->_ext._latency && _active) + if (!_plugin->_ext._latency) { - return _plugin->_ext._latency->get(_plugin->_plugin); + return 0; } - return 0; + + if (!_active) + { + _missedLatencyRequest = true; + return 0; + } + + _missedLatencyRequest = false; + return _plugin->_ext._latency->get(_plugin->_plugin); } uint32 PLUGIN_API ClapAsVst3::getTailSamples() diff --git a/src/wrapasvst3.h b/src/wrapasvst3.h index 7ce4f8c2..dca1a788 100644 --- a/src/wrapasvst3.h +++ b/src/wrapasvst3.h @@ -259,6 +259,7 @@ class ClapAsVst3 : public Steinberg::Vst::SingleComponentEffect, std::mutex _processingLock; std::atomic_bool _requestedFlush = false; std::atomic_bool _requestUICallback = false; + bool _missedLatencyRequest = false; // the queue from audiothread to UI thread ClapWrapper::detail::shared::fixedqueue _queueToUI; From 1500382a1996b23c33bd8b8c9e362e4988dca37f Mon Sep 17 00:00:00 2001 From: David Schornsheim Date: Mon, 26 Feb 2024 17:35:37 +0100 Subject: [PATCH 3/5] Prevent infinite loop between CLAP plugin and VST3 wrapper if CLAP plugin reports changed latency during activate() --- src/wrapasvst3.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wrapasvst3.cpp b/src/wrapasvst3.cpp index a699b410..b6790da9 100644 --- a/src/wrapasvst3.cpp +++ b/src/wrapasvst3.cpp @@ -773,7 +773,9 @@ void ClapAsVst3::request_callback() void ClapAsVst3::restartPlugin() { - if (componentHandler) componentHandler->restartComponent(Vst::RestartFlags::kReloadComponent); + if (componentHandler) + componentHandler->restartComponent(Vst::RestartFlags::kIoChanged | + Vst::RestartFlags::kLatencyChanged); } void ClapAsVst3::onBeginEdit(clap_id id) From e283df51710cd2267d3284cf22e932b032b5499d Mon Sep 17 00:00:00 2001 From: David Schornsheim Date: Mon, 26 Feb 2024 17:36:47 +0100 Subject: [PATCH 4/5] Add crash safeguards against incorrect VST3 calling sequences --- src/wrapasvst3.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/wrapasvst3.cpp b/src/wrapasvst3.cpp index b6790da9..153eed40 100644 --- a/src/wrapasvst3.cpp +++ b/src/wrapasvst3.cpp @@ -59,6 +59,10 @@ tresult PLUGIN_API ClapAsVst3::terminate() if (_plugin) { _os_attached.off(); // ensure we are detached + if (_active) + { + _plugin->deactivate(); + } _plugin->terminate(); _plugin.reset(); } @@ -111,6 +115,11 @@ tresult PLUGIN_API ClapAsVst3::setActive(TBool state) tresult PLUGIN_API ClapAsVst3::process(Vst::ProcessData& data) { + if (!_active || !_processing) + { + return kNotInitialized; + } + auto thisFn = _plugin->AlwaysAudioThread(); this->_processAdapter->process(data); return kResultOk; From bbe9f8994df83a0ac4ef77ae51a56e657aeb9eba Mon Sep 17 00:00:00 2001 From: David Schornsheim Date: Mon, 26 Feb 2024 17:37:18 +0100 Subject: [PATCH 5/5] Fix misbehaving host issue --- src/wrapasvst3.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wrapasvst3.cpp b/src/wrapasvst3.cpp index 153eed40..0c3e7e3f 100644 --- a/src/wrapasvst3.cpp +++ b/src/wrapasvst3.cpp @@ -897,12 +897,13 @@ void ClapAsVst3::onIdle() std::lock_guard lock(_processingLock); _requestedFlush = false; - if (!_processing) + if (!_active) { // setup a ProcessAdapter just for flush with no audio Clap::ProcessAdapter pa; pa.setupProcessing(_plugin->_plugin, _plugin->_ext._params, audioInputs, audioOutputs, 0, 0, 0, this->parameters, componentHandler, nullptr, false, false); + auto thisFn = _plugin->AlwaysMainThread(); pa.flush(); } }