#include "HostedPluginManager.h" HostedPluginManager::HostedPluginManager() { // Register VST3 and AU formats formatManager.addDefaultFormats(); } HostedPluginManager::~HostedPluginManager() { unloadPlugin(); } void HostedPluginManager::loadPlugin(const juce::File& pluginFile, double sampleRate, int blockSize) { // Unload existing plugin first unloadPlugin(); currentSampleRate = sampleRate; currentBlockSize = blockSize; juce::String errorMessage; juce::OwnedArray typesFound; // Scan the file to find plugin descriptions for (int i = 0; i < formatManager.getNumFormats(); ++i) { auto* format = formatManager.getFormat(i); format->findAllTypesForFile(typesFound, pluginFile.getFullPathName()); if (typesFound.size() > 0) break; } if (typesFound.isEmpty()) { DBG("PluginBridge: No plugin found in file: " + pluginFile.getFullPathName()); return; } // Use the first plugin description found auto& desc = *typesFound[0]; // Create plugin instance (synchronous — safe here since we're on message thread) auto instance = formatManager.createPluginInstance( desc, sampleRate, blockSize, errorMessage); if (instance == nullptr) { DBG("PluginBridge: Failed to create plugin: " + errorMessage); return; } // Enable all buses before use 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(sampleRate, blockSize); // 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(); // Try to get stable ID from HostedParameter if (auto* hosted = dynamic_cast(param)) info.id = hosted->getParameterID(); else info.id = juce::String(i); results.push_back(info); } // Cap results to avoid token bloat 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; }