Cleanup
[carla.git] / source / modules / distrho / src / DistrhoPlugin.cpp
blob9c4c9b65a76aa55551aaa3b6a48e26a527d3c75d
1 /*
2 * DISTRHO Plugin Framework (DPF)
3 * Copyright (C) 2012-2022 Filipe Coelho <falktx@falktx.com>
5 * Permission to use, copy, modify, and/or distribute this software for any purpose with
6 * or without fee is hereby granted, provided that the above copyright notice and this
7 * permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10 * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "DistrhoPluginInternal.hpp"
19 START_NAMESPACE_DISTRHO
21 /* ------------------------------------------------------------------------------------------------------------
22 * Static data, see DistrhoPluginInternal.hpp */
24 uint32_t d_nextBufferSize = 0;
25 double d_nextSampleRate = 0.0;
26 const char* d_nextBundlePath = nullptr;
27 bool d_nextPluginIsDummy = false;
28 bool d_nextCanRequestParameterValueChanges = false;
30 /* ------------------------------------------------------------------------------------------------------------
31 * Static fallback data, see DistrhoPluginInternal.hpp */
33 const String PluginExporter::sFallbackString;
34 /* */ AudioPortWithBusId PluginExporter::sFallbackAudioPort;
35 const ParameterRanges PluginExporter::sFallbackRanges;
36 const ParameterEnumerationValues PluginExporter::sFallbackEnumValues;
37 const PortGroupWithId PluginExporter::sFallbackPortGroup;
39 /* ------------------------------------------------------------------------------------------------------------
40 * Plugin */
42 Plugin::Plugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount)
43 : pData(new PrivateData())
45 #if DISTRHO_PLUGIN_NUM_INPUTS+DISTRHO_PLUGIN_NUM_OUTPUTS > 0
46 pData->audioPorts = new AudioPortWithBusId[DISTRHO_PLUGIN_NUM_INPUTS+DISTRHO_PLUGIN_NUM_OUTPUTS];
47 #endif
49 #ifdef DPF_ABORT_ON_ERROR
50 # define DPF_ABORT abort();
51 #else
52 # define DPF_ABORT
53 #endif
55 if (parameterCount > 0)
57 pData->parameterCount = parameterCount;
58 pData->parameters = new Parameter[parameterCount];
61 if (programCount > 0)
63 #if DISTRHO_PLUGIN_WANT_PROGRAMS
64 pData->programCount = programCount;
65 pData->programNames = new String[programCount];
66 #else
67 d_stderr2("DPF warning: Plugins with programs must define `DISTRHO_PLUGIN_WANT_PROGRAMS` to 1");
68 DPF_ABORT
69 #endif
72 if (stateCount > 0)
74 #if DISTRHO_PLUGIN_WANT_STATE
75 pData->stateCount = stateCount;
76 pData->states = new State[stateCount];
77 #else
78 d_stderr2("DPF warning: Plugins with state must define `DISTRHO_PLUGIN_WANT_STATE` to 1");
79 DPF_ABORT
80 #endif
83 #undef DPF_ABORT
86 Plugin::~Plugin()
88 delete pData;
91 /* ------------------------------------------------------------------------------------------------------------
92 * Host state */
94 uint32_t Plugin::getBufferSize() const noexcept
96 return pData->bufferSize;
99 double Plugin::getSampleRate() const noexcept
101 return pData->sampleRate;
104 const char* Plugin::getBundlePath() const noexcept
106 return pData->bundlePath;
109 bool Plugin::isDummyInstance() const noexcept
111 return pData->isDummy;
114 #if DISTRHO_PLUGIN_WANT_TIMEPOS
115 const TimePosition& Plugin::getTimePosition() const noexcept
117 return pData->timePosition;
119 #endif
121 #if DISTRHO_PLUGIN_WANT_LATENCY
122 void Plugin::setLatency(uint32_t frames) noexcept
124 pData->latency = frames;
126 #endif
128 #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
129 bool Plugin::writeMidiEvent(const MidiEvent& midiEvent) noexcept
131 return pData->writeMidiCallback(midiEvent);
133 #endif
135 #if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST
136 bool Plugin::canRequestParameterValueChanges() const noexcept
138 return pData->canRequestParameterValueChanges;
141 bool Plugin::requestParameterValueChange(const uint32_t index, const float value) noexcept
143 return pData->requestParameterValueChangeCallback(index, value);
145 #endif
147 #if DISTRHO_PLUGIN_WANT_STATE
148 bool Plugin::updateStateValue(const char* const key, const char* const value) noexcept
150 return pData->updateStateValueCallback(key, value);
152 #endif
154 /* ------------------------------------------------------------------------------------------------------------
155 * Init */
157 void Plugin::initAudioPort(bool input, uint32_t index, AudioPort& port)
159 if (port.hints & kAudioPortIsCV)
161 port.name = input ? "CV Input " : "CV Output ";
162 port.name += String(index+1);
163 port.symbol = input ? "cv_in_" : "cv_out_";
164 port.symbol += String(index+1);
166 else
168 port.name = input ? "Audio Input " : "Audio Output ";
169 port.name += String(index+1);
170 port.symbol = input ? "audio_in_" : "audio_out_";
171 port.symbol += String(index+1);
175 void Plugin::initParameter(uint32_t, Parameter&) {}
177 void Plugin::initPortGroup(const uint32_t groupId, PortGroup& portGroup)
179 fillInPredefinedPortGroupData(groupId, portGroup);
182 #if DISTRHO_PLUGIN_WANT_PROGRAMS
183 void Plugin::initProgramName(uint32_t, String&) {}
184 #endif
186 #if DISTRHO_PLUGIN_WANT_STATE
187 void Plugin::initState(const uint32_t index, State& state)
189 uint hints = 0x0;
190 String stateKey, defaultStateValue;
192 #if defined(__clang__)
193 #pragma clang diagnostic push
194 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
195 #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
196 #pragma GCC diagnostic push
197 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
198 #endif
199 initState(index, stateKey, defaultStateValue);
200 if (isStateFile(index))
201 hints = kStateIsFilenamePath;
202 #if defined(__clang__)
203 #pragma clang diagnostic pop
204 #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
205 #pragma GCC diagnostic pop
206 #endif
208 state.hints = hints;
209 state.key = stateKey;
210 state.label = stateKey;
211 state.defaultValue = defaultStateValue;
213 #endif
215 /* ------------------------------------------------------------------------------------------------------------
216 * Init */
218 float Plugin::getParameterValue(uint32_t) const { return 0.0f; }
219 void Plugin::setParameterValue(uint32_t, float) {}
221 #if DISTRHO_PLUGIN_WANT_PROGRAMS
222 void Plugin::loadProgram(uint32_t) {}
223 #endif
225 #if DISTRHO_PLUGIN_WANT_FULL_STATE
226 String Plugin::getState(const char*) const { return String(); }
227 #endif
229 #if DISTRHO_PLUGIN_WANT_STATE
230 void Plugin::setState(const char*, const char*) {}
231 #endif
233 /* ------------------------------------------------------------------------------------------------------------
234 * Callbacks (optional) */
236 void Plugin::bufferSizeChanged(uint32_t) {}
237 void Plugin::sampleRateChanged(double) {}
239 // -----------------------------------------------------------------------------------------------------------
241 END_NAMESPACE_DISTRHO