File size: 2,679 Bytes
af9a321
 
 
 
 
 
 
362db70
af9a321
 
 
 
 
 
 
 
 
362db70
af9a321
362db70
af9a321
 
 
 
362db70
af9a321
 
 
362db70
af9a321
 
 
362db70
af9a321
 
 
 
 
362db70
 
af9a321
 
 
 
 
 
 
362db70
af9a321
362db70
 
 
 
 
 
af9a321
 
362db70
af9a321
362db70
 
 
 
 
 
 
 
 
 
 
af9a321
 
 
 
 
 
 
 
 
 
 
 
362db70
 
 
 
 
af9a321
 
 
 
 
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
#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();
}

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;

    // Clear extra output channels
    auto totalNumInputChannels = getTotalNumInputChannels();
    auto totalNumOutputChannels = getTotalNumOutputChannels();
    for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
        buffer.clear(i, 0, buffer.getNumSamples());

    // Route audio through hosted plugin
    hostManager.processBlock(buffer, midiMessages);
}

juce::AudioProcessorEditor* PluginBridgeProcessor::createEditor()
{
    return new PluginBridgeEditor(*this);
}

void PluginBridgeProcessor::getStateInformation(juce::MemoryBlock& destData)
{
    // Save the path of the hosted plugin so it reloads on session restore
    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());
}

// JUCE plugin entry point
juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
    return new PluginBridgeProcessor();
}