DISTRHO Plugin Framework
DistrhoPluginLV2.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com>
4  *
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.
8  *
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.
15  */
16 
17 #ifndef DISTRHO_PLUGIN_LV2_HPP_INCLUDED
18 #define DISTRHO_PLUGIN_LV2_HPP_INCLUDED
19 
20 #include "DistrhoPlugin.hpp"
21 
22 START_NAMESPACE_DISTRHO
23 
24 /* ------------------------------------------------------------------------------------------------------------
25  * LV2 Audio Port Hints */
26 
27 /**
28  @defgroup LV2AudioPortHints Audio Port Hints
29 
30  Various audio port hints.
31  @see AudioPortHints
32  @see AudioPort::hints
33  @{
34  */
35 
36 /**
37  Audio port can be used as control voltage (LV2 only).
38  */
39 static const uint32_t kAudioPortIsCV = 0x1;
40 
41 /**
42  Audio port should be used as sidechan (LV2 only).
43  */
44 static const uint32_t kAudioPortIsSidechain = 0x2;
45 
46 /** @} */
47 
48 /* ------------------------------------------------------------------------------------------------------------
49  * Parameter Hints */
50 
51 /**
52  @defgroup LV2ParameterHints Parameter Hints
53 
54  Various parameter hints.
55  @see ParameterHints
56  @see Parameter::hints
57  @{
58  */
59 
60 /**
61  Parameter value is a trigger.@n
62  This means the value resets back to its default after each process/run call.@n
63  Cannot be used for output parameters.
64 
65  @note Only officially supported under LV2. For other formats DPF simulates the behaviour.
66 */
67 static const uint32_t kParameterIsTrigger = 0x20 | kParameterIsBoolean;
68 
69 /** @} */
70 
71 /* ------------------------------------------------------------------------------------------------------------
72  * Base Plugin structs */
73 
74 /**
75  @defgroup BasePluginStructs Base Plugin Structs
76  @{
77  */
78 
79 /**
80  Parameter designation.@n
81  Allows a parameter to be specially designated for a task, like bypass.
82 
83  Each designation is unique, there must be only one parameter that uses it.@n
84  The use of designated parameters is completely optional.
85 
86  @note Designated parameters have strict ranges.
87  @see ParameterRanges::adjustForDesignation()
88  */
90  /**
91  Null or unset designation.
92  */
94 
95  /**
96  Bypass designation.@n
97  When on (> 0.5f), it means the plugin must run in a bypassed state.
98  */
100 };
101 
102 /** @} */
103 
104 /* ------------------------------------------------------------------------------------------------------------
105  * DPF Plugin */
106 
107 /**
108  @defgroup MainClasses Main Classes
109  @{
110  */
111 
112 /**
113  DPF Plugin class from where plugin instances are created.
114 
115  The public methods (Host state) are called from the plugin to get or set host information.@n
116  They can be called from a plugin instance at anytime unless stated otherwise.@n
117  All other methods are to be implemented by the plugin and will be called by the host.
118 
119  Shortly after a plugin instance is created, the various init* functions will be called by the host.@n
120  Host will call activate() before run(), and deactivate() before the plugin instance is destroyed.@n
121  The host may call deactivate right after activate and vice-versa, but never activate/deactivate consecutively.@n
122  There is no limit on how many times run() is called, only that activate/deactivate will be called in between.
123 
124  The buffer size and sample rate values will remain constant between activate and deactivate.@n
125  Buffer size is only a hint though, the host might call run() with a higher or lower number of frames.
126 
127  Some of this class functions are only available according to some macros.
128 
129  DISTRHO_PLUGIN_WANT_PROGRAMS activates program related features.@n
130  When enabled you need to implement initProgramName() and loadProgram().
131 
132  DISTRHO_PLUGIN_WANT_STATE activates internal state features.@n
133  When enabled you need to implement initStateKey() and setState().
134 
135  The process function run() changes wherever DISTRHO_PLUGIN_WANT_MIDI_INPUT is enabled or not.@n
136  When enabled it provides midi input events.
137  */
138 class Plugin
139 {
140 public:
141  /**
142  Plugin class constructor.@n
143  You must set all parameter values to their defaults, matching ParameterRanges::def.
144  */
145  Plugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount);
146 
147  /**
148  Destructor.
149  */
150  virtual ~Plugin();
151 
152  /* --------------------------------------------------------------------------------------------------------
153  * Host state */
154 
155  /**
156  Get the current buffer size that will probably be used during processing, in frames.@n
157  This value will remain constant between activate and deactivate.
158  @note This value is only a hint!@n
159  Hosts might call run() with a higher or lower number of frames.
160  @see bufferSizeChanged(uint32_t)
161  */
162  uint32_t getBufferSize() const noexcept;
163 
164  /**
165  Get the current sample rate that will be used during processing.@n
166  This value will remain constant between activate and deactivate.
167  @see sampleRateChanged(double)
168  */
169  double getSampleRate() const noexcept;
170 
171 #if DISTRHO_PLUGIN_WANT_TIMEPOS
172  /**
173  Get the current host transport time position.@n
174  This function should only be called during run().@n
175  You can call this during other times, but the returned position is not guaranteed to be in sync.
176  @note TimePosition is not supported in LADSPA and DSSI plugin formats.
177  */
178  const TimePosition& getTimePosition() const noexcept;
179 #endif
180 
181 #if DISTRHO_PLUGIN_WANT_LATENCY
182  /**
183  Change the plugin audio output latency to @a frames.@n
184  This function should only be called in the constructor, activate() and run().
185  @note This function is only available if DISTRHO_PLUGIN_WANT_LATENCY is enabled.
186  */
187  void setLatency(uint32_t frames) noexcept;
188 #endif
189 
190 #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
191  /**
192  Write a MIDI output event.@n
193  This function must only be called during run().@n
194  Returns false when the host buffer is full, in which case do not call this again until the next run().
195  */
196  bool writeMidiEvent(const MidiEvent& midiEvent) noexcept;
197 #endif
198 
199 protected:
200  /* --------------------------------------------------------------------------------------------------------
201  * Information */
202 
203  /**
204  Get the plugin name.@n
205  Returns DISTRHO_PLUGIN_NAME by default.
206  */
207  virtual const char* getName() const { return DISTRHO_PLUGIN_NAME; }
208 
209  /**
210  Get the plugin label.@n
211  This label is a short restricted name consisting of only _, a-z, A-Z and 0-9 characters.
212  */
213  virtual const char* getLabel() const = 0;
214 
215  /**
216  Get an extensive comment/description about the plugin.@n
217  Optional, returns nothing by default.
218  */
219  virtual const char* getDescription() const { return ""; }
220 
221  /**
222  Get the plugin author/maker.
223  */
224  virtual const char* getMaker() const = 0;
225 
226  /**
227  Get the plugin homepage.@n
228  Optional, returns nothing by default.
229  */
230  virtual const char* getHomePage() const { return ""; }
231 
232  /**
233  Get the plugin license (a single line of text or a URL).@n
234  For commercial plugins this should return some short copyright information.
235  */
236  virtual const char* getLicense() const = 0;
237 
238  /**
239  Get the plugin version, in hexadecimal.
240  @see d_version()
241  */
242  virtual uint32_t getVersion() const = 0;
243 
244  /**
245  Get the plugin unique Id.@n
246  This value is used by LADSPA, DSSI and VST plugin formats.
247  @see d_cconst()
248  */
249  virtual int64_t getUniqueId() const = 0;
250 
251  /* --------------------------------------------------------------------------------------------------------
252  * Init */
253 
254  /**
255  Initialize the audio port @a index.@n
256  This function will be called once, shortly after the plugin is created.
257  */
258  virtual void initAudioPort(bool input, uint32_t index, AudioPort& port);
259 
260  /**
261  Initialize the parameter @a index.@n
262  This function will be called once, shortly after the plugin is created.
263  */
264  virtual void initParameter(uint32_t index, Parameter& parameter) = 0;
265 
266 #if DISTRHO_PLUGIN_WANT_PROGRAMS
267  /**
268  Set the name of the program @a index.@n
269  This function will be called once, shortly after the plugin is created.@n
270  Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
271  */
272  virtual void initProgramName(uint32_t index, String& programName) = 0;
273 #endif
274 
275 #if DISTRHO_PLUGIN_WANT_STATE
276  /**
277  Set the state key and default value of @a index.@n
278  This function will be called once, shortly after the plugin is created.@n
279  Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
280  */
281  virtual void initState(uint32_t index, String& stateKey, String& defaultStateValue) = 0;
282 #endif
283 
284  /* --------------------------------------------------------------------------------------------------------
285  * Internal data */
286 
287  /**
288  Get the current value of a parameter.@n
289  The host may call this function from any context, including realtime processing.
290  */
291  virtual float getParameterValue(uint32_t index) const = 0;
292 
293  /**
294  Change a parameter value.@n
295  The host may call this function from any context, including realtime processing.@n
296  When a parameter is marked as automable, you must ensure no non-realtime operations are performed.
297  @note This function will only be called for parameter inputs.
298  */
299  virtual void setParameterValue(uint32_t index, float value) = 0;
300 
301 #if DISTRHO_PLUGIN_WANT_PROGRAMS
302  /**
303  Load a program.@n
304  The host may call this function from any context, including realtime processing.@n
305  Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
306  */
307  virtual void loadProgram(uint32_t index) = 0;
308 #endif
309 
310 #if DISTRHO_PLUGIN_WANT_FULL_STATE
311  /**
312  Get the value of an internal state.@n
313  The host may call this function from any non-realtime context.@n
314  Must be implemented by your plugin class if DISTRHO_PLUGIN_WANT_FULL_STATE is enabled.
315  @note The use of this function breaks compatibility with the DSSI format.
316  */
317  virtual String getState(const char* key) const = 0;
318 #endif
319 
320 #if DISTRHO_PLUGIN_WANT_STATE
321  /**
322  Change an internal state @a key to @a value.@n
323  Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
324  */
325  virtual void setState(const char* key, const char* value) = 0;
326 #endif
327 
328  /* --------------------------------------------------------------------------------------------------------
329  * Audio/MIDI Processing */
330 
331  /**
332  Activate this plugin.
333  */
334  virtual void activate() {}
335 
336  /**
337  Deactivate this plugin.
338  */
339  virtual void deactivate() {}
340 
341 #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
342  /**
343  Run/process function for plugins with MIDI input.
344  @note Some parameters might be null if there are no audio inputs/outputs or MIDI events.
345  */
346  virtual void run(const float** inputs, float** outputs, uint32_t frames,
347  const MidiEvent* midiEvents, uint32_t midiEventCount) = 0;
348 #else
349  /**
350  Run/process function for plugins without MIDI input.
351  @note Some parameters might be null if there are no audio inputs or outputs.
352  */
353  virtual void run(const float** inputs, float** outputs, uint32_t frames) = 0;
354 #endif
355 
356  /* --------------------------------------------------------------------------------------------------------
357  * Callbacks (optional) */
358 
359  /**
360  Optional callback to inform the plugin about a buffer size change.@n
361  This function will only be called when the plugin is deactivated.
362  @note This value is only a hint!@n
363  Hosts might call run() with a higher or lower number of frames.
364  @see getBufferSize()
365  */
366  virtual void bufferSizeChanged(uint32_t newBufferSize);
367 
368  /**
369  Optional callback to inform the plugin about a sample rate change.@n
370  This function will only be called when the plugin is deactivated.
371  @see getSampleRate()
372  */
373  virtual void sampleRateChanged(double newSampleRate);
374 
375  // -------------------------------------------------------------------------------------------------------
376 
377 private:
378  struct PrivateData;
379  PrivateData* const pData;
380  friend class PluginExporter;
381 
382  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Plugin)
383 };
384 
385 /** @} */
386 
387 /* ------------------------------------------------------------------------------------------------------------
388  * Create plugin, entry point */
389 
390 /**
391  @defgroup EntryPoints Entry Points
392  @{
393  */
394 
395 /**
396  TODO.
397  */
398 extern Plugin* createPlugin();
399 
400 /** @} */
401 
402 // -----------------------------------------------------------------------------------------------------------
403 
404 END_NAMESPACE_DISTRHO
405 
406 #endif // DISTRHO_PLUGIN_HPP_INCLUDED
Plugin::sampleRateChanged
virtual void sampleRateChanged(double newSampleRate)
Plugin::getTimePosition
const TimePosition & getTimePosition() const noexcept
Plugin::deactivate
virtual void deactivate()
Definition: DistrhoPluginLV2.hpp:339
Plugin::getVersion
virtual uint32_t getVersion() const =0
MidiEvent
Definition: DistrhoPlugin.hpp:616
Plugin::setLatency
void setLatency(uint32_t frames) noexcept
String
Definition: String.hpp:30
Plugin::writeMidiEvent
bool writeMidiEvent(const MidiEvent &midiEvent) noexcept
Plugin::getState
virtual String getState(const char *key) const =0
Plugin::getParameterValue
virtual float getParameterValue(uint32_t index) const =0
createPlugin
Plugin * createPlugin()
Plugin::Plugin
Plugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount)
kParameterDesignationNull
@ kParameterDesignationNull
Definition: DistrhoPluginLV2.hpp:93
Plugin::~Plugin
virtual ~Plugin()
Parameter
Definition: DistrhoPlugin.hpp:445
Plugin::getBufferSize
uint32_t getBufferSize() const noexcept
Plugin::getMaker
virtual const char * getMaker() const =0
Plugin::loadProgram
virtual void loadProgram(uint32_t index)=0
DISTRHO_PLUGIN_NAME
#define DISTRHO_PLUGIN_NAME
Definition: DistrhoInfo.hpp:470
Plugin::initProgramName
virtual void initProgramName(uint32_t index, String &programName)=0
kAudioPortIsCV
static const uint32_t kAudioPortIsCV
Definition: DistrhoPluginLV2.hpp:39
Plugin::getLabel
virtual const char * getLabel() const =0
Plugin::bufferSizeChanged
virtual void bufferSizeChanged(uint32_t newBufferSize)
Plugin::initParameter
virtual void initParameter(uint32_t index, Parameter &parameter)=0
kParameterDesignationBypass
@ kParameterDesignationBypass
Definition: DistrhoPluginLV2.hpp:99
Plugin::getDescription
virtual const char * getDescription() const
Definition: DistrhoPluginLV2.hpp:219
Plugin::getName
virtual const char * getName() const
Definition: DistrhoPluginLV2.hpp:207
Plugin::getLicense
virtual const char * getLicense() const =0
kAudioPortIsSidechain
static const uint32_t kAudioPortIsSidechain
Definition: DistrhoPluginLV2.hpp:44
ParameterDesignation
ParameterDesignation
Definition: DistrhoPlugin.hpp:150
kParameterIsTrigger
static const uint32_t kParameterIsTrigger
Definition: DistrhoPluginLV2.hpp:67
Plugin::initAudioPort
virtual void initAudioPort(bool input, uint32_t index, AudioPort &port)
Plugin::getUniqueId
virtual int64_t getUniqueId() const =0
Plugin
Definition: DistrhoPlugin.hpp:802
Plugin::run
virtual void run(const float **inputs, float **outputs, uint32_t frames, const MidiEvent *midiEvents, uint32_t midiEventCount)=0
Plugin::setParameterValue
virtual void setParameterValue(uint32_t index, float value)=0
TimePosition
Definition: DistrhoPlugin.hpp:647
Plugin::getHomePage
virtual const char * getHomePage() const
Definition: DistrhoPluginLV2.hpp:230
kParameterIsBoolean
static const uint32_t kParameterIsBoolean
Definition: DistrhoPlugin.hpp:96
AudioPort
Definition: DistrhoPlugin.hpp:196
Plugin::getSampleRate
double getSampleRate() const noexcept
Plugin::activate
virtual void activate()
Definition: DistrhoPluginLV2.hpp:334
Plugin::initState
virtual void initState(uint32_t index, String &stateKey, String &defaultStateValue)=0
Plugin::setState
virtual void setState(const char *key, const char *value)=0