File size: 6,180 Bytes
4f0132c
 
4fe041d
 
caf4644
 
 
 
 
 
4fe041d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48e03fd
 
4fe041d
48e03fd
 
 
 
 
 
 
4fe041d
48e03fd
4fe041d
48e03fd
4fe041d
 
 
48e03fd
4fe041d
48e03fd
4fe041d
 
 
48e03fd
4fe041d
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
#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<juce::PluginDescription> 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<juce::AudioPluginInstance> 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<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;
}