#include "HostedPluginManager.h" HostedPluginManager::HostedPluginManager() { #if JUCE_PLUGINHOST_VST3 formatManager.addFormat(new juce::VST3PluginFormat()); #endif #if JUCE_PLUGINHOST_AU formatManager.addFormat(new juce::AudioUnitPluginFormat()); #endif } HostedPluginManager::~HostedPluginManager() { unloadPlugin(); } void HostedPluginManager::loadPlugin(const juce::File& pluginFile, double sampleRate, int blockSize) { unloadPlugin(); currentSampleRate = sampleRate; currentBlockSize = blockSize; juce::String errorMessage; juce::OwnedArray typesFound; // Wrap in try/catch — some plugins throw or show dialogs that cause exceptions try { for (int i = 0; i < formatManager.getNumFormats(); ++i) { auto* format = formatManager.getFormat(i); format->findAllTypesForFile(typesFound, pluginFile.getFullPathName()); if (typesFound.size() > 0) break; } } catch (...) { DBG("PluginBridge: Exception during plugin scan for: " + pluginFile.getFullPathName()); return; } if (typesFound.isEmpty()) { DBG("PluginBridge: No plugin found in file: " + pluginFile.getFullPathName()); return; } auto desc = *typesFound[0]; // Use async creation — safer for AU plugins and plugins that need message thread formatManager.createPluginInstanceAsync(desc, sampleRate, blockSize, [this, pluginFile](std::unique_ptr instance, const juce::String& error) { if (instance == nullptr) { DBG("PluginBridge: Failed to create plugin: " + error); return; } // Enable all buses instance->enableAllBuses(); // Match bus layout: stereo in/out auto layout = juce::AudioProcessor::BusesLayout(); layout.inputBuses.add(juce::AudioChannelSet::stereo()); layout.outputBuses.add(juce::AudioChannelSet::stereo()); if (instance->checkBusesLayoutSupported(layout)) instance->setBusesLayout(layout); // Prepare for playback instance->prepareToPlay(currentSampleRate, currentBlockSize); // Store { const juce::ScopedLock sl(pluginLock); hostedPlugin = std::move(instance); loadedPluginPath = pluginFile.getFullPathName(); } DBG("PluginBridge: Loaded plugin: " + getPluginName() + " (" + juce::String(getParameterCount()) + " params)"); }); } void HostedPluginManager::unloadPlugin() { const juce::ScopedLock sl(pluginLock); if (hostedPlugin) { hostedPlugin->releaseResources(); hostedPlugin.reset(); loadedPluginPath.clear(); } } bool HostedPluginManager::isLoaded() const { return hostedPlugin != nullptr; } juce::String HostedPluginManager::getPluginName() const { if (hostedPlugin) return hostedPlugin->getName(); return {}; } void HostedPluginManager::prepareToPlay(double sampleRate, int blockSize) { currentSampleRate = sampleRate; currentBlockSize = blockSize; const juce::ScopedLock sl(pluginLock); if (hostedPlugin) hostedPlugin->prepareToPlay(sampleRate, blockSize); } void HostedPluginManager::processBlock(juce::AudioBuffer& buffer, juce::MidiBuffer& midi) { const juce::ScopedLock sl(pluginLock); if (hostedPlugin) hostedPlugin->processBlock(buffer, midi); } void HostedPluginManager::releaseResources() { const juce::ScopedLock sl(pluginLock); if (hostedPlugin) hostedPlugin->releaseResources(); } // --- Parameter Surface --- int HostedPluginManager::getParameterCount() const { if (hostedPlugin) return hostedPlugin->getParameters().size(); return 0; } std::vector HostedPluginManager::searchParams( const juce::String& keyword) const { std::vector results; if (!hostedPlugin) return results; auto& params = hostedPlugin->getParameters(); auto keywordLower = keyword.toLowerCase(); for (int i = 0; i < params.size(); ++i) { auto* param = params[i]; auto name = param->getName(128); if (name.toLowerCase().contains(keywordLower)) { ParamInfo info; info.index = i; info.name = name; info.value = param->getValue(); info.displayText = param->getText(param->getValue(), 64); info.automatable = param->isAutomatable(); if (auto* hosted = dynamic_cast(param)) info.id = hosted->getParameterID(); else info.id = juce::String(i); results.push_back(info); } if (results.size() >= 20) break; } return results; } std::vector> HostedPluginManager::getParams( const std::vector& indices) const { std::vector> results; if (!hostedPlugin) return results; auto& params = hostedPlugin->getParameters(); for (int idx : indices) { if (idx >= 0 && idx < params.size()) results.push_back({idx, params[idx]->getValue()}); } return results; } bool HostedPluginManager::setParams(const std::map& values) { if (!hostedPlugin) return false; auto& params = hostedPlugin->getParameters(); for (auto& [idx, val] : values) { if (idx >= 0 && idx < params.size()) { auto* param = params[idx]; float clampedVal = juce::jlimit(0.0f, 1.0f, val); param->beginChangeGesture(); param->setValueNotifyingHost(clampedVal); param->endChangeGesture(); } } return true; } juce::String HostedPluginManager::getLoadedPluginPath() const { return loadedPluginPath; }