1 /* -----------------------------------------------------------------------------
2  *
3  * Giada - Your Hardcore Loopmachine
4  *
5  * -----------------------------------------------------------------------------
6  *
7  * Copyright (C) 2010-2020 Giovanni A. Zuliani | Monocasual
8  *
9  * This file is part of Giada - Your Hardcore Loopmachine.
10  *
11  * Giada - Your Hardcore Loopmachine is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation, either
14  * version 3 of the License, or (at your option) any later version.
15  *
16  * Giada - Your Hardcore Loopmachine is distributed in the hope that it
17  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
18  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with Giada - Your Hardcore Loopmachine. If not, see
23  * <http://www.gnu.org/licenses/>.
24  *
25  * -------------------------------------------------------------------------- */
26 
27 
28 #ifdef WITH_VST
29 
30 #ifndef G_PLUGIN_H
31 #define G_PLUGIN_H
32 
33 
34 #include <vector>
35 #include "deps/juce-config.h"
36 #include "core/plugins/pluginHost.h"
37 #include "core/plugins/pluginState.h"
38 #include "core/midiLearnParam.h"
39 #include "core/const.h"
40 
41 
42 namespace giada {
43 namespace m
44 {
45 class Plugin : private juce::ComponentListener
46 {
47 public:
48 
49 	Plugin(ID id, const std::string& UID);
50 	Plugin(ID id, std::unique_ptr<juce::AudioPluginInstance> p, double samplerate, int buffersize);
51 	Plugin(const Plugin& o);
52 	~Plugin();
53 
54 	/* getUniqueId
55 	Returns a string-based UID. */
56 
57 	std::string getUniqueId() const;
58 	std::string getName() const;
59 	bool hasEditor() const;
60 	int getNumParameters() const;
61 	float getParameter(int index) const;
62 	std::string getParameterName(int index) const;
63 	std::string getParameterText(int index) const;
64 	std::string getParameterLabel(int index) const;
65 	bool isSuspended() const;
66 	bool isBypassed() const;
67 	int getNumPrograms() const;
68 	int getCurrentProgram() const;
69 	std::string getProgramName(int index) const;
70 	void setParameter(int index, float value) const;
71 	void setCurrentProgram(int index) const;
72 	bool acceptsMidi() const;
73 	PluginState getState() const;
74 	juce::AudioProcessorEditor* createEditor() const;
75 
76 	/* process
77 	Process the plug-in with audio and MIDI data. The audio buffer is a reference:
78 	it has to be altered by the plug-in itself. Conversely, the MIDI buffer must
79 	be passed by copy: each plug-in must receive its own copy of the event set, so
80 	that any attempt to change/clear the MIDI buffer will only modify the local
81 	copy. */
82 
83 	void process(juce::AudioBuffer<float>& b, juce::MidiBuffer m);
84 
85 	void setState(PluginState p);
86 	void setBypass(bool b);
87 
88 	/* id
89 	Unique identifier. */
90 
91 	ID id;
92 
93 	/* midiInParams
94 	A vector of MidiLearnParam's for controlling plug-in parameters with
95 	external hardware. */
96 
97 	std::vector<MidiLearnParam> midiInParams;
98 
99 	/* valid
100 	A missing plug-in is loaded anyway, yet marked as 'invalid'. */
101 
102 	bool valid;
103 
104 	std::function<void(int w, int h)> onEditorResize;
105 
106 private:
107 
108 #ifdef G_OS_WINDOWS
109 	/* Fuck... */
110 	#undef IN
111 	#undef OUT
112 #endif
113 
114 	enum class BusType
115 	{
116 		IN = true, OUT = false
117 	};
118 
119 	/* JUCE overrides. */
120 
121 	void componentMovedOrResized(juce::Component& c, bool moved, bool resized) override;
122 
123 	juce::AudioProcessor::Bus* getMainBus(BusType b) const;
124 
125 	/* countMainOutChannels
126 	Returns the current channel layout for the main output bus. */
127 
128 	int countMainOutChannels() const;
129 
130 	std::unique_ptr<juce::AudioPluginInstance> m_plugin;
131 	juce::AudioBuffer<float>                   m_buffer;
132 
133 	std::atomic<bool> m_bypass;
134 
135 	/* UID
136 	The original UID, used for missing plugins. */
137 
138 	std::string m_UID;
139 };
140 }} // giada::m::
141 
142 #endif
143 
144 #endif // #ifdef WITH_VST
145