File size: 8,030 Bytes
4f0132c 4fe041d caf4644 4fe041d ef289db 4fe041d ef289db 4fe041d ef289db 4fe041d ef289db 4fe041d ef289db 4fe041d 48e03fd 4fe041d 48e03fd ef289db 4fe041d 48e03fd 4fe041d ef289db 48e03fd ef289db 48e03fd ef289db 48e03fd 4fe041d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | #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();
}
bool HostedPluginManager::validatePluginFile(const juce::File& pluginFile)
{
// Use a child process to test if the plugin can be loaded without crashing
// The child process runs: pluginval-style check — just tries to dlopen the binary
// If the child crashes, we know the plugin is unsafe to load in-process
juce::ChildProcess validator;
// Use macOS 'file' command on the binary inside the bundle to verify it's a valid Mach-O
// This doesn't execute plugin code, just validates the binary exists and is loadable
juce::File binary;
if (pluginFile.getFileExtension() == ".vst3")
binary = pluginFile.getChildFile("Contents/MacOS")
.findChildFiles(juce::File::findFiles, false).getFirst();
else
binary = pluginFile.getChildFile("Contents/MacOS")
.findChildFiles(juce::File::findFiles, false).getFirst();
if (!binary.exists())
return false;
// Quick validation: check binary is valid Mach-O using 'file' command
if (!validator.start("file " + binary.getFullPathName().quoted()))
return false;
validator.waitForProcessToFinish(2000); // 2 second timeout
auto output = validator.readAllProcessOutput();
// If it contains "Mach-O" it's at least a valid binary
return output.contains("Mach-O");
}
void HostedPluginManager::loadPlugin(const juce::File& pluginFile,
double sampleRate, int blockSize)
{
unloadPlugin();
currentSampleRate = sampleRate;
currentBlockSize = blockSize;
// Quick sanity check — is this even a valid plugin bundle?
if (!validatePluginFile(pluginFile))
{
DBG("PluginBridge: Invalid plugin binary: " + pluginFile.getFullPathName());
lastError = "Invalid plugin binary";
return;
}
juce::OwnedArray<juce::PluginDescription> typesFound;
// Install signal handler to catch SIGABRT from misbehaving plugins
// Note: this won't fully prevent crashes from plugins that call abort()
// but gives us a fighting chance with ObjC exceptions
for (int i = 0; i < formatManager.getNumFormats(); ++i)
{
auto* format = formatManager.getFormat(i);
// Only try the format that matches the file extension
bool isVST3 = pluginFile.getFileExtension() == ".vst3";
bool isAU = pluginFile.getFileExtension() == ".component";
if (isVST3 && format->getName() != "VST3") continue;
if (isAU && format->getName() != "AudioUnit") continue;
format->findAllTypesForFile(typesFound, pluginFile.getFullPathName());
if (typesFound.size() > 0)
break;
}
if (typesFound.isEmpty())
{
DBG("PluginBridge: No plugin found in file: " + pluginFile.getFullPathName());
lastError = "Plugin not recognized";
return;
}
auto desc = *typesFound[0];
// Create instance asynchronously (safer — allows message loop to run)
formatManager.createPluginInstanceAsync(desc, sampleRate, blockSize,
[this, pluginFile](std::unique_ptr<juce::AudioPluginInstance> instance,
const juce::String& error)
{
if (instance == nullptr)
{
DBG("PluginBridge: Failed to create plugin: " + error);
lastError = error;
return;
}
instance->enableAllBuses();
auto layout = juce::AudioProcessor::BusesLayout();
layout.inputBuses.add(juce::AudioChannelSet::stereo());
layout.outputBuses.add(juce::AudioChannelSet::stereo());
if (instance->checkBusesLayoutSupported(layout))
instance->setBusesLayout(layout);
instance->prepareToPlay(currentSampleRate, currentBlockSize);
{
const juce::ScopedLock sl(pluginLock);
hostedPlugin = std::move(instance);
loadedPluginPath = pluginFile.getFullPathName();
lastError.clear();
}
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<float>& 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::ParamInfo> HostedPluginManager::searchParams(
const juce::String& keyword) const
{
std::vector<ParamInfo> 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<juce::AudioPluginInstance::HostedParameter*>(param))
info.id = hosted->getParameterID();
else
info.id = juce::String(i);
results.push_back(info);
}
if (results.size() >= 20)
break;
}
return results;
}
std::vector<std::pair<int, float>> HostedPluginManager::getParams(
const std::vector<int>& indices) const
{
std::vector<std::pair<int, float>> 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<int, float>& 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;
}
|