/* ============================================================================== PluginWrapper.cpp ============================================================================== */ #include "PluginWrapper.h" #if JUCE_WINDOWS #include #endif namespace minixer { namespace { //============================================================================== #if JUCE_WINDOWS bool safeFindAllTypesForFile (juce::AudioPluginFormat* format, juce::OwnedArray* result, const juce::String* fileOrIdentifier) { __try { format->findAllTypesForFile (*result, *fileOrIdentifier); return true; } __except (EXCEPTION_EXECUTE_HANDLER) { return false; } } #endif //============================================================================== juce::AudioChannelSet channelSetFromCount (uint32_t numChannels) { switch (numChannels) { case 0: return juce::AudioChannelSet::disabled(); case 1: return juce::AudioChannelSet::mono(); case 2: return juce::AudioChannelSet::stereo(); default: return juce::AudioChannelSet::discreteChannels (static_cast (numChannels)); } } } // anonymous namespace //============================================================================== PluginWrapper::PluginWrapper() = default; PluginWrapper::~PluginWrapper() { closeEditor(); releaseResources(); plugin.reset(); } //============================================================================== bool PluginWrapper::loadFromFile (const juce::File& file, double sampleRate, int bufferSize, juce::String& error) { juce::AudioPluginFormatManager formatManager; formatManager.addDefaultFormats(); juce::AudioPluginFormat* vst3Format = nullptr; for (int i = 0; i < formatManager.getNumFormats(); ++i) { auto* format = formatManager.getFormat (i); if (format != nullptr && format->getName() == juce::VST3PluginFormat::getFormatName()) { vst3Format = format; break; } } if (vst3Format == nullptr) { error = "VST3 format not available"; return false; } juce::OwnedArray descriptions; const juce::String filePath = file.getFullPathName(); #if JUCE_WINDOWS if (! safeFindAllTypesForFile (vst3Format, &descriptions, &filePath)) { error = "Plugin scan raised a structured exception"; return false; } #else vst3Format->findAllTypesForFile (descriptions, filePath); #endif if (descriptions.isEmpty()) { error = "No plugin descriptions found"; return false; } juce::String instanceError; plugin = vst3Format->createInstanceFromDescription (*descriptions[0], sampleRate, bufferSize, instanceError); if (plugin == nullptr) { error = "Failed to create plugin instance: " + instanceError; return false; } plugin->enableAllBuses(); juce::String layoutError; if (! setChannelLayout (currentInputChannels, currentOutputChannels, layoutError)) { // 保持默认总线布局,仍允许插件运行 juce::Logger::writeToLog ("PluginWrapper: setChannelLayout failed, falling back to default layout: " + layoutError); } prepareToPlay (sampleRate, bufferSize); return true; } //============================================================================== juce::String PluginWrapper::getName() const { return plugin != nullptr ? plugin->getName() : juce::String(); } //============================================================================== bool PluginWrapper::setChannelLayout (uint32_t numInputs, uint32_t numOutputs, juce::String& error) { if (plugin == nullptr) { error = "Plugin not loaded"; return false; } juce::AudioProcessor::BusesLayout layout; const auto mainInputSet = channelSetFromCount (numInputs); const auto mainOutputSet = channelSetFromCount (numOutputs); const int numInputBuses = plugin->getBusCount (true); const int numOutputBuses = plugin->getBusCount (false); for (int i = 0; i < numInputBuses; ++i) layout.inputBuses.add (i == 0 ? mainInputSet : juce::AudioChannelSet::disabled()); for (int i = 0; i < numOutputBuses; ++i) layout.outputBuses.add (i == 0 ? mainOutputSet : juce::AudioChannelSet::disabled()); if (! plugin->setBusesLayout (layout)) { error = "Plugin does not support the requested channel layout"; return false; } currentInputChannels = numInputs; currentOutputChannels = numOutputs; return true; } //============================================================================== void PluginWrapper::prepareToPlay (double sampleRate, int bufferSize) { if (plugin != nullptr) plugin->prepareToPlay (sampleRate, bufferSize); } //============================================================================== void PluginWrapper::releaseResources() { if (plugin != nullptr) plugin->releaseResources(); } //============================================================================== void PluginWrapper::processBlock (const float* const* inputChannels, uint32_t numInputChannels, float* const* outputChannels, uint32_t numOutputChannels, uint32_t numSamples) { if (plugin == nullptr || numSamples == 0) return; juce::AudioBuffer inputBuffer (const_cast (inputChannels), static_cast (numInputChannels), static_cast (numSamples)); juce::AudioBuffer outputBuffer (outputChannels, static_cast (numOutputChannels), static_cast (numSamples)); outputBuffer.clear(); juce::MidiBuffer midi; plugin->processBlock (inputBuffer, midi); } //============================================================================== void PluginWrapper::getStateInformation (juce::MemoryBlock& destData) { if (plugin != nullptr) plugin->getStateInformation (destData); } //============================================================================== void PluginWrapper::setStateInformation (const juce::MemoryBlock& stateData) { if (plugin != nullptr && stateData.getSize() > 0) plugin->setStateInformation (stateData.getData(), static_cast (stateData.getSize())); } //============================================================================== void PluginWrapper::setParameter (int index, float value) { if (plugin == nullptr) return; auto& params = plugin->getParameters(); if (juce::isPositiveAndBelow (index, params.size())) { if (auto* param = params[index]) param->setValue (value); } } //============================================================================== int PluginWrapper::getLatencySamples() const { return plugin != nullptr ? plugin->getLatencySamples() : 0; } //============================================================================== bool PluginWrapper::hasEditor() const { return plugin != nullptr && plugin->hasEditor(); } //============================================================================== void PluginWrapper::showEditor (const juce::String& windowTitle, void* /*parentWindowHandle*/) { if (! hasEditor() || editorWindow != nullptr) return; editor.reset (plugin->createEditorIfNeeded()); if (editor == nullptr) return; editorWindow = std::make_unique (windowTitle, juce::LookAndFeel::getDefaultLookAndFeel() .findColour (juce::ResizableWindow::backgroundColourId), juce::DocumentWindow::closeButton); editorWindow->setContentNonOwned (editor.get(), true); editorWindow->centreWithSize (editor->getWidth(), editor->getHeight()); editorWindow->setVisible (true); } //============================================================================== void PluginWrapper::hideEditor() { closeEditor(); } //============================================================================== void PluginWrapper::closeEditor() { if (editorWindow != nullptr) editorWindow.reset(); editor.reset(); } } // namespace minixer