File size: 2,813 Bytes
3853310
 
 
 
 
5f757c7
3853310
c465bcf
 
3853310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6caa77
3853310
 
5f757c7
 
f6caa77
5f757c7
3853310
c465bcf
f6caa77
c465bcf
 
f6caa77
 
3853310
c465bcf
 
5f757c7
3853310
 
 
c465bcf
f6caa77
 
 
 
c465bcf
 
 
 
 
 
 
 
 
 
3853310
 
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
#pragma once

#include <juce_audio_processors/juce_audio_processors.h>
#include <juce_dsp/juce_dsp.h>
#include "McpServer.h"
#include "HostedPluginManager.h"

class PluginBridgeProcessor : public juce::AudioProcessor,
                               private juce::Timer
{
public:
    PluginBridgeProcessor();
    ~PluginBridgeProcessor() override;

    // AudioProcessor interface
    void prepareToPlay(double sampleRate, int samplesPerBlock) override;
    void releaseResources() override;
    void processBlock(juce::AudioBuffer<float>&, juce::MidiBuffer&) override;

    // Editor
    juce::AudioProcessorEditor* createEditor() override;
    bool hasEditor() const override { return true; }

    // Plugin identity
    const juce::String getName() const override { return "PluginBridge"; }
    bool acceptsMidi() const override { return true; }
    bool producesMidi() const override { return true; }
    double getTailLengthSeconds() const override { return 0.0; }

    // Programs (unused)
    int getNumPrograms() override { return 1; }
    int getCurrentProgram() override { return 0; }
    void setCurrentProgram(int) override {}
    const juce::String getProgramName(int) override { return {}; }
    void changeProgramName(int, const juce::String&) override {}

    // State
    void getStateInformation(juce::MemoryBlock& destData) override;
    void setStateInformation(const void* data, int sizeInBytes) override;

    // Public access
    bool isMcpServerRunning() const;
    int getMcpServerPort() const;
    HostedPluginManager& getHostedPluginManager() { return hostManager; }

    // Load plugin (called on message thread)
    void loadPluginFromFile(const juce::File& file);

    // Plugin scanning — runs on message thread via Timer (safe for NSAlert-showing plugins)
    void startPluginScan();
    bool isScanComplete() const { return scanComplete.load(); }
    bool isScanning() const { return scanning.load(); }
    const juce::Array<juce::PluginDescription>& getScannedPlugins() const { return scannedPlugins; }

private:
    void timerCallback() override;

    HostedPluginManager hostManager;
    McpServer mcpServer;
    static constexpr int kDefaultMcpPort = 16620;

    // Plugin scan state
    juce::Array<juce::PluginDescription> scannedPlugins;
    std::atomic<bool> scanComplete{false};
    std::atomic<bool> scanning{false};

    // Scanner internals (only alive during scan)
    struct ScannerState {
        std::unique_ptr<juce::PluginDirectoryScanner> scanner;
        bool done = false;
    };
    juce::OwnedArray<ScannerState> scanners;
    int currentScannerIndex = 0;
    std::unique_ptr<juce::AudioPluginFormatManager> scanFormatManager;
    std::unique_ptr<juce::KnownPluginList> scanList;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PluginBridgeProcessor)
};