pluginbridge / Source /PluginBridgeProcessor.cpp
RAM2118's picture
Fix crash: Scan plugins on message thread via Timer (one per tick), not background thread"
ce21bf1 verified
Raw History Blame
4.55 kB
#include "PluginBridgeProcessor.h"
#include "PluginBridgeEditor.h"
PluginBridgeProcessor::PluginBridgeProcessor()
: AudioProcessor(BusesProperties()
.withInput("Input", juce::AudioChannelSet::stereo(), true)
.withOutput("Output", juce::AudioChannelSet::stereo(), true)),
mcpServer(kDefaultMcpPort, hostManager)
{
mcpServer.start();
}
PluginBridgeProcessor::~PluginBridgeProcessor()
{
mcpServer.stop();
stopTimer();
}
void PluginBridgeProcessor::prepareToPlay(double sampleRate, int samplesPerBlock)
{
hostManager.prepareToPlay(sampleRate, samplesPerBlock);
}
void PluginBridgeProcessor::releaseResources()
{
hostManager.releaseResources();
}
void PluginBridgeProcessor::processBlock(juce::AudioBuffer<float>& buffer,
juce::MidiBuffer& midiMessages)
{
juce::ScopedNoDenormals noDenormals;
auto totalNumInputChannels = getTotalNumInputChannels();
auto totalNumOutputChannels = getTotalNumOutputChannels();
for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
buffer.clear(i, 0, buffer.getNumSamples());
hostManager.processBlock(buffer, midiMessages);
}
juce::AudioProcessorEditor* PluginBridgeProcessor::createEditor()
{
return new PluginBridgeEditor(*this);
}
void PluginBridgeProcessor::getStateInformation(juce::MemoryBlock& destData)
{
juce::ValueTree state("PluginBridgeState");
state.setProperty("hostedPluginPath", hostManager.getLoadedPluginPath(), nullptr);
juce::MemoryOutputStream stream(destData, false);
state.writeToStream(stream);
}
void PluginBridgeProcessor::setStateInformation(const void* data, int sizeInBytes)
{
auto state = juce::ValueTree::readFromData(data, (size_t)sizeInBytes);
if (state.isValid())
{
auto path = state.getProperty("hostedPluginPath").toString();
if (path.isNotEmpty())
{
juce::File pluginFile(path);
if (pluginFile.exists())
loadPluginFromFile(pluginFile);
}
}
}
bool PluginBridgeProcessor::isMcpServerRunning() const
{
return mcpServer.isRunning();
}
int PluginBridgeProcessor::getMcpServerPort() const
{
return mcpServer.getPort();
}
void PluginBridgeProcessor::loadPluginFromFile(const juce::File& file)
{
hostManager.loadPlugin(file, getSampleRate(), getBlockSize());
}
void PluginBridgeProcessor::startPluginScan()
{
if (scanning.load() || scanComplete.load())
return;
scanning.store(true);
// Initialize scanner on message thread
scanFormatManager = std::make_unique<juce::AudioPluginFormatManager>();
#if JUCE_PLUGINHOST_VST3
scanFormatManager->addFormat(new juce::VST3PluginFormat());
#endif
#if JUCE_PLUGINHOST_AU
scanFormatManager->addFormat(new juce::AudioUnitPluginFormat());
#endif
scanList = std::make_unique<juce::KnownPluginList>();
// Build file list to scan
for (int i = 0; i < scanFormatManager->getNumFormats(); ++i)
{
auto* format = scanFormatManager->getFormat(i);
auto paths = format->getDefaultLocationsToSearch();
// Use a temporary scanner just to get the file list
juce::PluginDirectoryScanner tempScanner(
*scanList, *format, paths, true, juce::File());
// We'll use the scanner directly — start it
scanners.add(new ScannerState{
std::make_unique<juce::PluginDirectoryScanner>(
*scanList, *format, paths, true, juce::File()),
false
});
}
currentScannerIndex = 0;
// Use Timer to scan one file per tick on the message thread
startTimerHz(30); // 30 files per second — responsive UI
}
void PluginBridgeProcessor::timerCallback()
{
if (currentScannerIndex >= scanners.size())
{
// All scanners done
stopTimer();
scannedPlugins = scanList->getTypes();
scanning.store(false);
scanComplete.store(true);
scanners.clear();
scanFormatManager.reset();
scanList.reset();
return;
}
auto* state = scanners[currentScannerIndex];
if (state->done)
{
currentScannerIndex++;
return;
}
juce::String pluginName;
if (!state->scanner->scanNextFile(true, pluginName))
{
state->done = true;
currentScannerIndex++;
}
}
// JUCE plugin entry point
juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new PluginBridgeProcessor();
}