DISTRHO Plugin Framework
DistrhoPlugin.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2022 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_HPP_INCLUDED
18 #define DISTRHO_PLUGIN_HPP_INCLUDED
19 
20 #include "extra/String.hpp"
21 #include "extra/LeakDetector.hpp"
22 #include "src/DistrhoPluginChecks.h"
23 
25 
26 /* ------------------------------------------------------------------------------------------------------------
27  * Audio Port Hints */
28 
29 /**
30  @defgroup AudioPortHints Audio Port Hints
31 
32  Various audio port hints.
33  @see AudioPort::hints
34  @{
35  */
36 
37 /**
38  Audio port can be used as control voltage (LV2 and JACK standalone only).
39  */
40 static const uint32_t kAudioPortIsCV = 0x1;
41 
42 /**
43  Audio port should be used as sidechan (LV2 and VST3 only).
44  This hint should not be used with CV style ports.
45  @note non-sidechain audio ports must exist in the plugin if this flag is set.
46  */
47 static const uint32_t kAudioPortIsSidechain = 0x2;
48 
49 /**
50  CV port has bipolar range (-1 to +1, or -5 to +5 if scaled).
51  This is merely a hint to tell the host what value range to expect.
52  */
53 static const uint32_t kCVPortHasBipolarRange = 0x10;
54 
55 /**
56  CV port has negative unipolar range (-1 to 0, or -10 to 0 if scaled).
57  This is merely a hint to tell the host what value range to expect.
58  */
59 static const uint32_t kCVPortHasNegativeUnipolarRange = 0x20;
60 
61 /**
62  CV port has positive unipolar range (0 to +1, or 0 to +10 if scaled).
63  This is merely a hint to tell the host what value range to expect.
64  */
65 static const uint32_t kCVPortHasPositiveUnipolarRange = 0x40;
66 
67 /**
68  CV port has scaled range to match real values (-5 to +5v bipolar, +/-10 to 0v unipolar).
69  One other range flag is required if this flag is set.
70 
71  When enabled, this makes the port a mod:CVPort, compatible with the MOD Devices platform.
72  */
73 static const uint32_t kCVPortHasScaledRange = 0x80;
74 
75 /** @} */
76 
77 /* ------------------------------------------------------------------------------------------------------------
78  * Parameter Hints */
79 
80 /**
81  @defgroup ParameterHints Parameter Hints
82 
83  Various parameter hints.
84  @see Parameter::hints
85  @{
86  */
87 
88 /**
89  Parameter is automatable (real-time safe).
90  @see Plugin::setParameterValue(uint32_t, float)
91  */
92 static const uint32_t kParameterIsAutomatable = 0x01;
93 
94 /** It was a typo, sorry.. */
95 DISTRHO_DEPRECATED_BY("kParameterIsAutomatable")
97 
98 /**
99  Parameter value is boolean.@n
100  It's always at either minimum or maximum value.
101  */
102 static const uint32_t kParameterIsBoolean = 0x02;
103 
104 /**
105  Parameter value is integer.
106  */
107 static const uint32_t kParameterIsInteger = 0x04;
108 
109 /**
110  Parameter value is logarithmic.
111  */
112 static const uint32_t kParameterIsLogarithmic = 0x08;
113 
114 /**
115  Parameter is of output type.@n
116  When unset, parameter is assumed to be of input type.
117 
118  Parameter inputs are changed by the host and typically should not be changed by the plugin.@n
119  One exception is when changing programs, see Plugin::loadProgram().@n
120  The other exception is with parameter change requests, see Plugin::requestParameterValueChange().@n
121  Outputs are changed by the plugin and never modified by the host.
122 
123  If you are targetting VST2, make sure to order your parameters so that all inputs are before any outputs.
124  */
125 static const uint32_t kParameterIsOutput = 0x10;
126 
127 /**
128  Parameter value is a trigger.@n
129  This means the value resets back to its default after each process/run call.@n
130  Cannot be used for output parameters.
131 
132  @note Only officially supported under LV2. For other formats DPF simulates the behaviour.
133 */
134 static const uint32_t kParameterIsTrigger = 0x20 | kParameterIsBoolean;
135 
136 /** @} */
137 
138 /* ------------------------------------------------------------------------------------------------------------
139  * State Hints */
140 
141 /**
142  @defgroup StateHints State Hints
143 
144  Various state hints.
145  @see State::hints
146  @{
147  */
148 
149 /**
150  State is visible and readable by hosts that support string-type plugin parameters.
151  */
152 static const uint32_t kStateIsHostReadable = 0x01;
153 
154 /**
155  State is writable by the host, allowing users to arbitrarily change the state.@n
156  For obvious reasons a writable state is also readable by the host.
157  */
158 static const uint32_t kStateIsHostWritable = 0x02 | kStateIsHostReadable;
159 
160 /**
161  State is a filename path instead of a regular string.@n
162  The readable and writable hints are required for filenames to work, and thus are automatically set.
163  */
164 static const uint32_t kStateIsFilenamePath = 0x04 | kStateIsHostWritable;
165 
166 /**
167  State is a base64 encoded string.
168  */
169 static const uint32_t kStateIsBase64Blob = 0x08;
170 
171 /**
172  State is for Plugin/DSP side only, meaning there is never a need to notify the UI when it changes.
173  */
174 static const uint32_t kStateIsOnlyForDSP = 0x10;
175 
176 /**
177  State is for UI side only.@n
178  If the DSP and UI are separate and the UI is not available, this property won't be saved.
179  */
180 static const uint32_t kStateIsOnlyForUI = 0x20;
181 
182 /** @} */
183 
184 /* ------------------------------------------------------------------------------------------------------------
185  * Base Plugin structs */
186 
187 /**
188  @defgroup BasePluginStructs Base Plugin Structs
189  @{
190  */
191 
192 /**
193  Parameter designation.@n
194  Allows a parameter to be specially designated for a task, like bypass.
195 
196  Each designation is unique, there must be only one parameter that uses it.@n
197  The use of designated parameters is completely optional.
198 
199  @note Designated parameters have strict ranges.
200  @see ParameterRanges::adjustForDesignation()
201  */
203  /**
204  Null or unset designation.
205  */
207 
208  /**
209  Bypass designation.@n
210  When on (> 0.5f), it means the plugin must run in a bypassed state.
211  */
213 };
214 
215 /**
216  Predefined Port Groups Ids.
217 
218  This enumeration provides a few commonly used groups for convenient use in plugins.
219  For preventing conflicts with user code, negative values are used here.
220  When rolling your own port groups, you MUST start their group ids from 0 and they MUST be sequential.
221 
222  @see PortGroup
223  */
225  /**
226  Null or unset port group.
227  */
228  kPortGroupNone = (uint32_t)-1,
229 
230  /**
231  A single channel audio group.
232  */
233  kPortGroupMono = (uint32_t)-2,
234 
235  /**
236  A 2-channel discrete stereo audio group,
237  where the 1st audio port is the left channel and the 2nd port is the right channel.
238  */
239  kPortGroupStereo = (uint32_t)-3
240 };
241 
242 /**
243  Audio Port.
244 
245  Can be used as CV port by specifying kAudioPortIsCV in hints,@n
246  but this is only supported in LV2 and JACK standalone formats.
247  */
248 struct AudioPort {
249  /**
250  Hints describing this audio port.
251  @see AudioPortHints
252  */
253  uint32_t hints;
254 
255  /**
256  The name of this audio port.@n
257  An audio port name can contain any character, but hosts might have a hard time with non-ascii ones.@n
258  The name doesn't have to be unique within a plugin instance, but it's recommended.
259  */
261 
262  /**
263  The symbol of this audio port.@n
264  An audio port symbol is a short restricted name used as a machine and human readable identifier.@n
265  The first character must be one of _, a-z or A-Z and subsequent characters can be from _, a-z, A-Z and 0-9.
266  @note Audio port and parameter symbols MUST be unique within a plugin instance.
267  */
269 
270  /**
271  The group id that this audio/cv port belongs to.
272  No group is assigned by default.
273 
274  You can use a group from PredefinedPortGroups or roll your own.@n
275  When rolling your own port groups, you MUST start their group ids from 0 and they MUST be sequential.
276  @see PortGroup, Plugin::initPortGroup
277  */
278  uint32_t groupId;
279 
280  /**
281  Default constructor for a regular audio port.
282  */
283  AudioPort() noexcept
284  : hints(0x0),
285  name(),
286  symbol(),
288 };
289 
290 /**
291  Parameter ranges.@n
292  This is used to set the default, minimum and maximum values of a parameter.
293 
294  By default a parameter has 0.0 as minimum, 1.0 as maximum and 0.0 as default.@n
295  When changing this struct values you must ensure maximum > minimum and default is within range.
296  */
298  /**
299  Default value.
300  */
301  float def;
302 
303  /**
304  Minimum value.
305  */
306  float min;
307 
308  /**
309  Maximum value.
310  */
311  float max;
312 
313  /**
314  Default constructor, using 0.0 as default, 0.0 as minimum, 1.0 as maximum.
315  */
316  ParameterRanges() noexcept
317  : def(0.0f),
318  min(0.0f),
319  max(1.0f) {}
320 
321  /**
322  Constructor using custom values.
323  */
324  ParameterRanges(float df, float mn, float mx) noexcept
325  : def(df),
326  min(mn),
327  max(mx) {}
328 
329  /**
330  Fix the default value within range.
331  */
332  void fixDefault() noexcept
333  {
334  fixValue(def);
335  }
336 
337  /**
338  Fix a value within range.
339  */
340  void fixValue(float& value) const noexcept
341  {
342  if (value < min)
343  value = min;
344  else if (value > max)
345  value = max;
346  }
347 
348  /**
349  Get a fixed value within range.
350  */
351  float getFixedValue(const float& value) const noexcept
352  {
353  if (value <= min)
354  return min;
355  if (value >= max)
356  return max;
357  return value;
358  }
359 
360  /**
361  Get a value normalized to 0.0<->1.0.
362  */
363  float getNormalizedValue(const float& value) const noexcept
364  {
365  const float normValue((value - min) / (max - min));
366 
367  if (normValue <= 0.0f)
368  return 0.0f;
369  if (normValue >= 1.0f)
370  return 1.0f;
371  return normValue;
372  }
373 
374  /**
375  Get a value normalized to 0.0<->1.0, fixed within range.
376  */
377  float getFixedAndNormalizedValue(const float& value) const noexcept
378  {
379  if (value <= min)
380  return 0.0f;
381  if (value >= max)
382  return 1.0f;
383 
384  const float normValue((value - min) / (max - min));
385 
386  if (normValue <= 0.0f)
387  return 0.0f;
388  if (normValue >= 1.0f)
389  return 1.0f;
390 
391  return normValue;
392  }
393 
394  /**
395  Get a proper value previously normalized to 0.0<->1.0.
396  */
397  float getUnnormalizedValue(const float& value) const noexcept
398  {
399  if (value <= 0.0f)
400  return min;
401  if (value >= 1.0f)
402  return max;
403 
404  return value * (max - min) + min;
405  }
406 };
407 
408 /**
409  Parameter enumeration value.@n
410  A string representation of a plugin parameter value.@n
411  Used together can be used to give meaning to parameter values, working as an enumeration.
412  */
414  /**
415  Parameter value.
416  */
417  float value;
418 
419  /**
420  String representation of this value.
421  */
423 
424  /**
425  Default constructor, using 0.0 as value and empty label.
426  */
428  : value(0.0f),
429  label() {}
430 
431  /**
432  Constructor using custom values.
433  */
434  ParameterEnumerationValue(float v, const char* l) noexcept
435  : value(v),
436  label(l) {}
437 };
438 
439 /**
440  Collection of parameter enumeration values.@n
441  Handy class to handle the lifetime and count of all enumeration values.
442  */
444  /**
445  Number of elements allocated in @values.
446  */
447  uint8_t count;
448 
449  /**
450  Wherever the host is to be restricted to only use enumeration values.
451 
452  @note This mode is only a hint! Not all hosts and plugin formats support this mode.
453  */
455 
456  /**
457  Array of @ParameterEnumerationValue items.@n
458  This pointer must be null or have been allocated on the heap with `new ParameterEnumerationValue[count]`.
459  */
461 
462  /**
463  Default constructor, for zero enumeration values.
464  */
466  : count(0),
467  restrictedMode(false),
468  values() {}
469 
470  /**
471  Constructor using custom values.@n
472  The pointer to @values must have been allocated on the heap with `new`.
473  */
474  ParameterEnumerationValues(uint32_t c, bool r, ParameterEnumerationValue* v) noexcept
475  : count(c),
476  restrictedMode(r),
477  values(v) {}
478 
479  ~ParameterEnumerationValues() noexcept
480  {
481  count = 0;
482  restrictedMode = false;
483 
484  if (values != nullptr)
485  {
486  delete[] values;
487  values = nullptr;
488  }
489  }
490 
491  DISTRHO_DECLARE_NON_COPYABLE(ParameterEnumerationValues)
492 };
493 
494 /**
495  Parameter.
496  */
497 struct Parameter {
498  /**
499  Hints describing this parameter.
500  @see ParameterHints
501  */
502  uint32_t hints;
503 
504  /**
505  The name of this parameter.@n
506  A parameter name can contain any character, but hosts might have a hard time with non-ascii ones.@n
507  The name doesn't have to be unique within a plugin instance, but it's recommended.
508  */
510 
511  /**
512  The short name of this parameter.@n
513  Used when displaying the parameter name in a very limited space.
514  @note This value is optional, the full name is used when the short one is missing.
515  */
517 
518  /**
519  The symbol of this parameter.@n
520  A parameter symbol is a short restricted name used as a machine and human readable identifier.@n
521  The first character must be one of _, a-z or A-Z and subsequent characters can be from _, a-z, A-Z and 0-9.
522  @note Parameter symbols MUST be unique within a plugin instance.
523  */
525 
526  /**
527  The unit of this parameter.@n
528  This means something like "dB", "kHz" and "ms".@n
529  Can be left blank if a unit does not apply to this parameter.
530  */
532 
533  /**
534  An extensive description/comment about the parameter.
535  @note This value is optional and only used for LV2.
536  */
538 
539  /**
540  Ranges of this parameter.@n
541  The ranges describe the default, minimum and maximum values.
542  */
544 
545  /**
546  Enumeration values.@n
547  Can be used to give meaning to parameter values, working as an enumeration.
548  */
550 
551  /**
552  Designation for this parameter.
553  */
555 
556  /**
557  MIDI CC to use by default on this parameter.@n
558  A value of 0 or 32 (bank change) is considered invalid.@n
559  Must also be less or equal to 120.
560  @note This value is only a hint! Hosts might map it automatically or completely ignore it.
561  */
562  uint8_t midiCC;
563 
564  /**
565  The group id that this parameter belongs to.
566  No group is assigned by default.
567 
568  You can use a group from PredefinedPortGroups or roll your own.@n
569  When rolling your own port groups, you MUST start their group ids from 0 and they MUST be sequential.
570  @see PortGroup, Plugin::initPortGroup
571  */
572  uint32_t groupId;
573 
574  /**
575  Default constructor for a null parameter.
576  */
577  Parameter() noexcept
578  : hints(0x0),
579  name(),
580  shortName(),
581  symbol(),
582  unit(),
583  ranges(),
584  enumValues(),
586  midiCC(0),
588 
589  /**
590  Constructor using custom values.
591  */
592  Parameter(uint32_t h, const char* n, const char* s, const char* u, float def, float min, float max) noexcept
593  : hints(h),
594  name(n),
595  shortName(),
596  symbol(s),
597  unit(u),
598  ranges(def, min, max),
599  enumValues(),
601  midiCC(0),
603 
604  /**
605  Initialize a parameter for a specific designation.
606  */
608  {
609  designation = d;
610 
611  switch (d)
612  {
614  break;
617  name = "Bypass";
618  shortName = "Bypass";
619  symbol = "dpf_bypass";
620  unit = "";
621  midiCC = 0;
623  ranges.def = 0.0f;
624  ranges.min = 0.0f;
625  ranges.max = 1.0f;
626  break;
627  }
628  }
629 };
630 
631 /**
632  Port Group.@n
633  Allows to group together audio/cv ports or parameters.
634 
635  Each unique group MUST have an unique symbol and a name.
636  A group can be applied to both inputs and outputs (at the same time).
637  The same group cannot be used in audio ports and parameters.
638 
639  When both audio and parameter groups are used, audio groups MUST be defined first.
640  That is, group indexes start with audio ports, then parameters.
641 
642  An audio port group logically combines ports which should be considered part of the same stream.@n
643  For example, two audio ports in a group may form a stereo stream.
644 
645  A parameter group provides meta-data to the host to indicate that some parameters belong together.
646 
647  The use of port groups is completely optional.
648 
649  @see Plugin::initPortGroup, AudioPort::group, Parameter::group
650  */
651 struct PortGroup {
652  /**
653  The name of this port group.@n
654  A port group name can contain any character, but hosts might have a hard time with non-ascii ones.@n
655  The name doesn't have to be unique within a plugin instance, but it's recommended.
656  */
658 
659  /**
660  The symbol of this port group.@n
661  A port group symbol is a short restricted name used as a machine and human readable identifier.@n
662  The first character must be one of _, a-z or A-Z and subsequent characters can be from _, a-z, A-Z and 0-9.
663  @note Port group symbols MUST be unique within a plugin instance.
664  */
666 };
667 
668 /**
669  State.
670 
671  In DPF states refer to key:value string pairs, used to store arbitrary non-parameter data.@n
672  By default states are completely internal to the plugin and not visible by the host.@n
673  Flags can be set to allow hosts to see and/or change them.
674 
675  TODO API under construction
676  */
677 struct State {
678  /**
679  Hints describing this state.
680  @note Changing these hints can break compatibility with previously saved data.
681  @see StateHints
682  */
683  uint32_t hints;
684 
685  /**
686  The key or "symbol" of this state.@n
687  A state key is a short restricted name used as a machine and human readable identifier.
688  @note State keys MUST be unique within a plugin instance.
689  TODO define rules for allowed characters, must be usable as URI non-encoded parameters
690  */
692 
693  /**
694  The default value of this state.@n
695  Can be left empty if considered a valid initial state.
696  */
698 
699  /**
700  String representation of this state.
701  */
703 
704  /**
705  An extensive description/comment about this state.
706  @note This value is optional and only used for LV2.
707  */
709 };
710 
711 /**
712  MIDI event.
713  */
714 struct MidiEvent {
715  /**
716  Size of internal data.
717  */
718  static const uint32_t kDataSize = 4;
719 
720  /**
721  Time offset in frames.
722  */
723  uint32_t frame;
724 
725  /**
726  Number of bytes used.
727  */
728  uint32_t size;
729 
730  /**
731  MIDI data.@n
732  If size > kDataSize, dataExt is used (otherwise null).
733 
734  When dataExt is used, the event holder is responsible for
735  keeping the pointer valid during the entirety of the run function.
736  */
737  uint8_t data[kDataSize];
738  const uint8_t* dataExt;
739 };
740 
741 /**
742  Time position.@n
743  The @a playing and @a frame values are always valid.@n
744  BBT values are only valid when @a bbt.valid is true.
745 
746  This struct is inspired by the [JACK Transport API](https://jackaudio.org/api/structjack__position__t.html).
747  */
748 struct TimePosition {
749  /**
750  Wherever the host transport is playing/rolling.
751  */
752  bool playing;
753 
754  /**
755  Current host transport position in frames.
756  @note This value is not always monotonic,
757  with some plugin hosts assigning it based on a source that can accumulate rounding errors.
758  */
759  uint64_t frame;
760 
761  /**
762  Bar-Beat-Tick time position.
763  */
764  struct BarBeatTick {
765  /**
766  Wherever the host transport is using BBT.@n
767  If false you must not read from this struct.
768  */
769  bool valid;
770 
771  /**
772  Current bar.@n
773  Should always be > 0.@n
774  The first bar is bar '1'.
775  */
776  int32_t bar;
777 
778  /**
779  Current beat within bar.@n
780  Should always be > 0 and <= @a beatsPerBar.@n
781  The first beat is beat '1'.
782  */
783  int32_t beat;
784 
785  /**
786  Current tick within beat.@n
787  Should always be >= 0 and < @a ticksPerBeat.@n
788  The first tick is tick '0'.
789  @note Fraction part of tick is only available on some plugin formats.
790  */
791  double tick;
792 
793  /**
794  Number of ticks that have elapsed between frame 0 and the first beat of the current measure.
795  */
796  double barStartTick;
797 
798  /**
799  Time signature "numerator".
800  */
801  float beatsPerBar;
802 
803  /**
804  Time signature "denominator".
805  */
806  float beatType;
807 
808  /**
809  Number of ticks within a beat.@n
810  Usually a moderately large integer with many denominators, such as 1920.0.
811  */
812  double ticksPerBeat;
813 
814  /**
815  Number of beats per minute.
816  */
818 
819  /**
820  Default constructor for a null BBT time position.
821  */
822  BarBeatTick() noexcept
823  : valid(false),
824  bar(0),
825  beat(0),
826  tick(0),
827  barStartTick(0.0),
828  beatsPerBar(0.0f),
829  beatType(0.0f),
830  ticksPerBeat(0.0),
831  beatsPerMinute(0.0) {}
832 
833  /**
834  Reinitialize this position using the default null initialization.
835  */
836  void clear() noexcept
837  {
838  valid = false;
839  bar = 0;
840  beat = 0;
841  tick = 0;
842  barStartTick = 0.0;
843  beatsPerBar = 0.0f;
844  beatType = 0.0f;
845  ticksPerBeat = 0.0;
846  beatsPerMinute = 0.0;
847  }
848  } bbt;
849 
850  /**
851  Default constructor for a time position.
852  */
853  TimePosition() noexcept
854  : playing(false),
855  frame(0),
856  bbt() {}
857 
858  /**
859  Reinitialize this position using the default null initialization.
860  */
861  void clear() noexcept
862  {
863  playing = false;
864  frame = 0;
865  bbt.clear();
866  }
867 };
868 
869 /** @} */
870 
871 /* ------------------------------------------------------------------------------------------------------------
872  * DPF Plugin */
873 
874 /**
875  @defgroup MainClasses Main Classes
876  @{
877  */
878 
879 /**
880  DPF Plugin class from where plugin instances are created.
881 
882  The public methods (Host state) are called from the plugin to get or set host information.@n
883  They can be called from a plugin instance at anytime unless stated otherwise.@n
884  All other methods are to be implemented by the plugin and will be called by the host.
885 
886  Shortly after a plugin instance is created, the various init* functions will be called by the host.@n
887  Host will call activate() before run(), and deactivate() before the plugin instance is destroyed.@n
888  The host may call deactivate right after activate and vice-versa, but never activate/deactivate consecutively.@n
889  There is no limit on how many times run() is called, only that activate/deactivate will be called in between.
890 
891  The buffer size and sample rate values will remain constant between activate and deactivate.@n
892  Buffer size is only a hint though, the host might call run() with a higher or lower number of frames.
893 
894  Some of this class functions are only available according to some macros.
895 
896  DISTRHO_PLUGIN_WANT_PROGRAMS activates program related features.@n
897  When enabled you need to implement initProgramName() and loadProgram().
898 
899  DISTRHO_PLUGIN_WANT_STATE activates internal state features.@n
900  When enabled you need to implement initStateKey() and setState().
901 
902  The process function run() changes wherever DISTRHO_PLUGIN_WANT_MIDI_INPUT is enabled or not.@n
903  When enabled it provides midi input events.
904  */
905 class Plugin
906 {
907 public:
908  /**
909  Plugin class constructor.@n
910  You must set all parameter values to their defaults, matching ParameterRanges::def.
911  */
912  Plugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount);
913 
914  /**
915  Destructor.
916  */
917  virtual ~Plugin();
918 
919  /* --------------------------------------------------------------------------------------------------------
920  * Host state */
921 
922  /**
923  Get the current buffer size that will probably be used during processing, in frames.@n
924  This value will remain constant between activate and deactivate.
925  @note This value is only a hint!@n
926  Hosts might call run() with a higher or lower number of frames.
927  @see bufferSizeChanged(uint32_t)
928  */
929  uint32_t getBufferSize() const noexcept;
930 
931  /**
932  Get the current sample rate that will be used during processing.@n
933  This value will remain constant between activate and deactivate.
934  @see sampleRateChanged(double)
935  */
936  double getSampleRate() const noexcept;
937 
938  /**
939  Get the bundle path where the plugin resides.
940  Can return null if the plugin is not available in a bundle (if it is a single binary).
941  @see getBinaryFilename
942  @see getResourcePath
943  */
944  const char* getBundlePath() const noexcept;
945 
946  /**
947  Check if this plugin instance is a "dummy" one used for plugin meta-data/information export.@n
948  When true no processing will be done, the plugin is created only to extract information.@n
949  In DPF, LADSPA/DSSI, VST2 and VST3 formats create one global instance per plugin binary
950  while LV2 creates one when generating turtle meta-data.
951  */
952  bool isDummyInstance() const noexcept;
953 
954 #if DISTRHO_PLUGIN_WANT_TIMEPOS
955  /**
956  Get the current host transport time position.@n
957  This function should only be called during run().@n
958  You can call this during other times, but the returned position is not guaranteed to be in sync.
959  @note TimePosition is not supported in LADSPA and DSSI plugin formats.
960  */
961  const TimePosition& getTimePosition() const noexcept;
962 #endif
963 
964 #if DISTRHO_PLUGIN_WANT_LATENCY
965  /**
966  Change the plugin audio output latency to @a frames.@n
967  This function should only be called in the constructor, activate() and run().
968  @note This function is only available if DISTRHO_PLUGIN_WANT_LATENCY is enabled.
969  */
970  void setLatency(uint32_t frames) noexcept;
971 #endif
972 
973 #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
974  /**
975  Write a MIDI output event.@n
976  This function must only be called during run().@n
977  Returns false when the host buffer is full, in which case do not call this again until the next run().
978  */
979  bool writeMidiEvent(const MidiEvent& midiEvent) noexcept;
980 #endif
981 
982 #if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST
983  /**
984  Check if parameter value change requests will work with the current plugin host.
985  @note This function is only available if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST is enabled.
986  @see requestParameterValueChange(uint32_t, float)
987  */
988  bool canRequestParameterValueChanges() const noexcept;
989 
990  /**
991  Request a parameter value change from the host.
992  If successful, this function will automatically trigger a parameter update on the UI side as well.
993  This function can fail, for example if the host is busy with the parameter for read-only automation.
994  Some hosts simply do not have this functionality, which can be verified with canRequestParameterValueChanges().
995  @note This function is only available if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST is enabled.
996  */
997  bool requestParameterValueChange(uint32_t index, float value) noexcept;
998 #endif
999 
1000 #if DISTRHO_PLUGIN_WANT_STATE
1001  /**
1002  Set state value and notify the host about the change.@n
1003  This function will call `setState()` and also trigger an update on the UI side as necessary.@n
1004  It must not be called during run.@n
1005  The state must be host readable.
1006  @note this function does nothing on DSSI plugin format, as DSSI only supports UI->DSP messages.
1007 
1008  TODO API under construction
1009  */
1010  bool updateStateValue(const char* key, const char* value) noexcept;
1011 #endif
1012 
1013 protected:
1014  /* --------------------------------------------------------------------------------------------------------
1015  * Information */
1016 
1017  /**
1018  Get the plugin name.@n
1019  Returns DISTRHO_PLUGIN_NAME by default.
1020  */
1021  virtual const char* getName() const { return DISTRHO_PLUGIN_NAME; }
1022 
1023  /**
1024  Get the plugin label.@n
1025  This label is a short restricted name consisting of only _, a-z, A-Z and 0-9 characters.
1026  */
1027  virtual const char* getLabel() const = 0;
1028 
1029  /**
1030  Get an extensive comment/description about the plugin.@n
1031  Optional, returns nothing by default.
1032  */
1033  virtual const char* getDescription() const { return ""; }
1034 
1035  /**
1036  Get the plugin author/maker.
1037  */
1038  virtual const char* getMaker() const = 0;
1039 
1040  /**
1041  Get the plugin homepage.@n
1042  Optional, returns nothing by default.
1043  */
1044  virtual const char* getHomePage() const { return ""; }
1045 
1046  /**
1047  Get the plugin license (a single line of text or a URL).@n
1048  For commercial plugins this should return some short copyright information.
1049  */
1050  virtual const char* getLicense() const = 0;
1051 
1052  /**
1053  Get the plugin version, in hexadecimal.
1054  @see d_version()
1055  */
1056  virtual uint32_t getVersion() const = 0;
1057 
1058  /**
1059  Get the plugin unique Id.@n
1060  This value is used by LADSPA, DSSI and VST plugin formats.
1061  @see d_cconst()
1062  */
1063  virtual int64_t getUniqueId() const = 0;
1064 
1065  /* --------------------------------------------------------------------------------------------------------
1066  * Init */
1067 
1068  /**
1069  Initialize the audio port @a index.@n
1070  This function will be called once, shortly after the plugin is created.
1071  */
1072  virtual void initAudioPort(bool input, uint32_t index, AudioPort& port);
1073 
1074  /**
1075  Initialize the parameter @a index.@n
1076  This function will be called once, shortly after the plugin is created.
1077  */
1078  virtual void initParameter(uint32_t index, Parameter& parameter);
1079 
1080  /**
1081  Initialize the port group @a groupId.@n
1082  This function will be called once,
1083  shortly after the plugin is created and all audio ports and parameters have been enumerated.
1084  */
1085  virtual void initPortGroup(uint32_t groupId, PortGroup& portGroup);
1086 
1087 #if DISTRHO_PLUGIN_WANT_PROGRAMS
1088  /**
1089  Set the name of the program @a index.@n
1090  This function will be called once, shortly after the plugin is created.@n
1091  Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
1092  */
1093  virtual void initProgramName(uint32_t index, String& programName) = 0;
1094 #endif
1095 
1096 #if DISTRHO_PLUGIN_WANT_STATE
1097  /**
1098  Initialize the state @a index.@n
1099  This function will be called once, shortly after the plugin is created.@n
1100  Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
1101  */
1102  virtual void initState(uint32_t index, State& state);
1103 
1104  DISTRHO_DEPRECATED_BY("initState(uint32_t,State&)")
1105  virtual void initState(uint32_t, String&, String&) {}
1106 
1107  DISTRHO_DEPRECATED_BY("initState(uint32_t,State&)")
1108  virtual bool isStateFile(uint32_t) { return false; }
1109 #endif
1110 
1111  /* --------------------------------------------------------------------------------------------------------
1112  * Internal data */
1113 
1114  /**
1115  Get the current value of a parameter.@n
1116  The host may call this function from any context, including realtime processing.
1117  */
1118  virtual float getParameterValue(uint32_t index) const;
1119 
1120  /**
1121  Change a parameter value.@n
1122  The host may call this function from any context, including realtime processing.@n
1123  When a parameter is marked as automatable, you must ensure no non-realtime operations are performed.
1124  @note This function will only be called for parameter inputs.
1125  */
1126  virtual void setParameterValue(uint32_t index, float value);
1127 
1128 #if DISTRHO_PLUGIN_WANT_PROGRAMS
1129  /**
1130  Load a program.@n
1131  The host may call this function from any context, including realtime processing.@n
1132  Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
1133  */
1134  virtual void loadProgram(uint32_t index);
1135 #endif
1136 
1137 #if DISTRHO_PLUGIN_WANT_FULL_STATE
1138  /**
1139  Get the value of an internal state.@n
1140  The host may call this function from any non-realtime context.@n
1141  Must be implemented by your plugin class if DISTRHO_PLUGIN_WANT_FULL_STATE is enabled.
1142  @note The use of this function breaks compatibility with the DSSI format.
1143  */
1144  virtual String getState(const char* key) const;
1145 #endif
1146 
1147 #if DISTRHO_PLUGIN_WANT_STATE
1148  /**
1149  Change an internal state @a key to @a value.@n
1150  Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
1151  */
1152  virtual void setState(const char* key, const char* value);
1153 #endif
1154 
1155  /* --------------------------------------------------------------------------------------------------------
1156  * Audio/MIDI Processing */
1157 
1158  /**
1159  Activate this plugin.
1160  */
1161  virtual void activate() {}
1162 
1163  /**
1164  Deactivate this plugin.
1165  */
1166  virtual void deactivate() {}
1167 
1168 #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
1169  /**
1170  Run/process function for plugins with MIDI input.
1171  @note Some parameters might be null if there are no audio inputs/outputs or MIDI events.
1172  */
1173  virtual void run(const float** inputs, float** outputs, uint32_t frames,
1174  const MidiEvent* midiEvents, uint32_t midiEventCount) = 0;
1175 #else
1176  /**
1177  Run/process function for plugins without MIDI input.
1178  @note Some parameters might be null if there are no audio inputs or outputs.
1179  */
1180  virtual void run(const float** inputs, float** outputs, uint32_t frames) = 0;
1181 #endif
1182 
1183  /* --------------------------------------------------------------------------------------------------------
1184  * Callbacks (optional) */
1185 
1186  /**
1187  Optional callback to inform the plugin about a buffer size change.@n
1188  This function will only be called when the plugin is deactivated.
1189  @note This value is only a hint!@n
1190  Hosts might call run() with a higher or lower number of frames.
1191  @see getBufferSize()
1192  */
1193  virtual void bufferSizeChanged(uint32_t newBufferSize);
1194 
1195  /**
1196  Optional callback to inform the plugin about a sample rate change.@n
1197  This function will only be called when the plugin is deactivated.
1198  @see getSampleRate()
1199  */
1200  virtual void sampleRateChanged(double newSampleRate);
1201 
1202  // -------------------------------------------------------------------------------------------------------
1203 
1204 private:
1205  struct PrivateData;
1206  PrivateData* const pData;
1207  friend class PluginExporter;
1208 
1209  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Plugin)
1210 };
1211 
1212 /** @} */
1213 
1214 /* ------------------------------------------------------------------------------------------------------------
1215  * Create plugin, entry point */
1216 
1217 /**
1218  @defgroup EntryPoints Entry Points
1219  @{
1220  */
1221 
1222 /**
1223  Create an instance of the Plugin class.@n
1224  This is the entry point for DPF plugins.@n
1225  DPF will call this to either create an instance of your plugin for the host
1226  or to fetch some initial information for internal caching.
1227  */
1229 
1230 /** @} */
1231 
1232 // -----------------------------------------------------------------------------------------------------------
1233 
1235 
1236 #endif // DISTRHO_PLUGIN_HPP_INCLUDED
Definition: DistrhoPlugin.hpp:906
virtual const char * getLabel() const =0
bool writeMidiEvent(const MidiEvent &midiEvent) noexcept
virtual void run(const float **inputs, float **outputs, uint32_t frames, const MidiEvent *midiEvents, uint32_t midiEventCount)=0
uint32_t getBufferSize() const noexcept
void setLatency(uint32_t frames) noexcept
virtual void sampleRateChanged(double newSampleRate)
virtual const char * getDescription() const
Definition: DistrhoPlugin.hpp:1033
double getSampleRate() const noexcept
virtual void loadProgram(uint32_t index)
virtual void initProgramName(uint32_t index, String &programName)=0
bool canRequestParameterValueChanges() const noexcept
const char * getBundlePath() const noexcept
virtual const char * getLicense() const =0
virtual void initPortGroup(uint32_t groupId, PortGroup &portGroup)
virtual void initAudioPort(bool input, uint32_t index, AudioPort &port)
virtual void setParameterValue(uint32_t index, float value)
virtual ~Plugin()
virtual const char * getMaker() const =0
virtual String getState(const char *key) const
virtual void bufferSizeChanged(uint32_t newBufferSize)
virtual void deactivate()
Definition: DistrhoPlugin.hpp:1166
virtual void activate()
Definition: DistrhoPlugin.hpp:1161
bool updateStateValue(const char *key, const char *value) noexcept
virtual void initParameter(uint32_t index, Parameter &parameter)
bool isDummyInstance() const noexcept
virtual uint32_t getVersion() const =0
Plugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount)
virtual const char * getHomePage() const
Definition: DistrhoPlugin.hpp:1044
virtual void initState(uint32_t index, State &state)
virtual int64_t getUniqueId() const =0
bool requestParameterValueChange(uint32_t index, float value) noexcept
const TimePosition & getTimePosition() const noexcept
virtual const char * getName() const
Definition: DistrhoPlugin.hpp:1021
virtual float getParameterValue(uint32_t index) const
virtual void setState(const char *key, const char *value)
Definition: String.hpp:31
static const uint32_t kCVPortHasPositiveUnipolarRange
Definition: DistrhoPlugin.hpp:65
static const uint32_t kCVPortHasBipolarRange
Definition: DistrhoPlugin.hpp:53
static const uint32_t kAudioPortIsCV
Definition: DistrhoPlugin.hpp:40
static const uint32_t kCVPortHasScaledRange
Definition: DistrhoPlugin.hpp:73
static const uint32_t kCVPortHasNegativeUnipolarRange
Definition: DistrhoPlugin.hpp:59
static const uint32_t kAudioPortIsSidechain
Definition: DistrhoPlugin.hpp:47
PredefinedPortGroupsIds
Definition: DistrhoPlugin.hpp:224
ParameterDesignation
Definition: DistrhoPlugin.hpp:202
@ kPortGroupMono
Definition: DistrhoPlugin.hpp:233
@ kPortGroupNone
Definition: DistrhoPlugin.hpp:228
@ kPortGroupStereo
Definition: DistrhoPlugin.hpp:239
@ kParameterDesignationBypass
Definition: DistrhoPlugin.hpp:212
@ kParameterDesignationNull
Definition: DistrhoPlugin.hpp:206
Plugin * createPlugin()
#define END_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:834
#define START_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:828
static const uint32_t kParameterIsAutomable
Definition: DistrhoPlugin.hpp:96
static const uint32_t kParameterIsTrigger
Definition: DistrhoPlugin.hpp:134
static const uint32_t kParameterIsOutput
Definition: DistrhoPlugin.hpp:125
static const uint32_t kParameterIsAutomatable
Definition: DistrhoPlugin.hpp:92
static const uint32_t kParameterIsInteger
Definition: DistrhoPlugin.hpp:107
static const uint32_t kParameterIsBoolean
Definition: DistrhoPlugin.hpp:102
static const uint32_t kParameterIsLogarithmic
Definition: DistrhoPlugin.hpp:112
#define DISTRHO_PLUGIN_NAME
Definition: DistrhoInfo.hpp:481
static const uint32_t kStateIsOnlyForDSP
Definition: DistrhoPlugin.hpp:174
static const uint32_t kStateIsHostWritable
Definition: DistrhoPlugin.hpp:158
static const uint32_t kStateIsOnlyForUI
Definition: DistrhoPlugin.hpp:180
static const uint32_t kStateIsHostReadable
Definition: DistrhoPlugin.hpp:152
static const uint32_t kStateIsBase64Blob
Definition: DistrhoPlugin.hpp:169
static const uint32_t kStateIsFilenamePath
Definition: DistrhoPlugin.hpp:164
Definition: DistrhoPlugin.hpp:248
uint32_t groupId
Definition: DistrhoPlugin.hpp:278
AudioPort() noexcept
Definition: DistrhoPlugin.hpp:283
String name
Definition: DistrhoPlugin.hpp:260
String symbol
Definition: DistrhoPlugin.hpp:268
uint32_t hints
Definition: DistrhoPlugin.hpp:253
Definition: DistrhoPlugin.hpp:714
uint32_t size
Definition: DistrhoPlugin.hpp:728
static const uint32_t kDataSize
Definition: DistrhoPlugin.hpp:718
uint8_t data[kDataSize]
Definition: DistrhoPlugin.hpp:737
uint32_t frame
Definition: DistrhoPlugin.hpp:723
Definition: DistrhoPlugin.hpp:413
ParameterEnumerationValue() noexcept
Definition: DistrhoPlugin.hpp:427
float value
Definition: DistrhoPlugin.hpp:417
ParameterEnumerationValue(float v, const char *l) noexcept
Definition: DistrhoPlugin.hpp:434
String label
Definition: DistrhoPlugin.hpp:422
Definition: DistrhoPlugin.hpp:443
bool restrictedMode
Definition: DistrhoPlugin.hpp:454
ParameterEnumerationValues(uint32_t c, bool r, ParameterEnumerationValue *v) noexcept
Definition: DistrhoPlugin.hpp:474
ParameterEnumerationValues() noexcept
Definition: DistrhoPlugin.hpp:465
ParameterEnumerationValue * values
Definition: DistrhoPlugin.hpp:460
uint8_t count
Definition: DistrhoPlugin.hpp:447
Definition: DistrhoPlugin.hpp:297
void fixDefault() noexcept
Definition: DistrhoPlugin.hpp:332
float max
Definition: DistrhoPlugin.hpp:311
void fixValue(float &value) const noexcept
Definition: DistrhoPlugin.hpp:340
float min
Definition: DistrhoPlugin.hpp:306
ParameterRanges(float df, float mn, float mx) noexcept
Definition: DistrhoPlugin.hpp:324
float getFixedAndNormalizedValue(const float &value) const noexcept
Definition: DistrhoPlugin.hpp:377
float getFixedValue(const float &value) const noexcept
Definition: DistrhoPlugin.hpp:351
float getUnnormalizedValue(const float &value) const noexcept
Definition: DistrhoPlugin.hpp:397
float def
Definition: DistrhoPlugin.hpp:301
float getNormalizedValue(const float &value) const noexcept
Definition: DistrhoPlugin.hpp:363
ParameterRanges() noexcept
Definition: DistrhoPlugin.hpp:316
Definition: DistrhoPlugin.hpp:497
Parameter() noexcept
Definition: DistrhoPlugin.hpp:577
String shortName
Definition: DistrhoPlugin.hpp:516
ParameterRanges ranges
Definition: DistrhoPlugin.hpp:543
String unit
Definition: DistrhoPlugin.hpp:531
uint32_t hints
Definition: DistrhoPlugin.hpp:502
ParameterDesignation designation
Definition: DistrhoPlugin.hpp:554
String symbol
Definition: DistrhoPlugin.hpp:524
uint8_t midiCC
Definition: DistrhoPlugin.hpp:562
String description
Definition: DistrhoPlugin.hpp:537
uint32_t groupId
Definition: DistrhoPlugin.hpp:572
ParameterEnumerationValues enumValues
Definition: DistrhoPlugin.hpp:549
void initDesignation(ParameterDesignation d) noexcept
Definition: DistrhoPlugin.hpp:607
String name
Definition: DistrhoPlugin.hpp:509
Parameter(uint32_t h, const char *n, const char *s, const char *u, float def, float min, float max) noexcept
Definition: DistrhoPlugin.hpp:592
Definition: DistrhoPlugin.hpp:651
String symbol
Definition: DistrhoPlugin.hpp:665
String name
Definition: DistrhoPlugin.hpp:657
Definition: DistrhoPlugin.hpp:677
String key
Definition: DistrhoPlugin.hpp:691
String label
Definition: DistrhoPlugin.hpp:702
uint32_t hints
Definition: DistrhoPlugin.hpp:683
String defaultValue
Definition: DistrhoPlugin.hpp:697
String description
Definition: DistrhoPlugin.hpp:708
Definition: DistrhoPlugin.hpp:764
float beatType
Definition: DistrhoPlugin.hpp:806
bool valid
Definition: DistrhoPlugin.hpp:769
float beatsPerBar
Definition: DistrhoPlugin.hpp:801
double barStartTick
Definition: DistrhoPlugin.hpp:796
double ticksPerBeat
Definition: DistrhoPlugin.hpp:812
BarBeatTick() noexcept
Definition: DistrhoPlugin.hpp:822
int32_t bar
Definition: DistrhoPlugin.hpp:776
void clear() noexcept
Definition: DistrhoPlugin.hpp:836
double tick
Definition: DistrhoPlugin.hpp:791
int32_t beat
Definition: DistrhoPlugin.hpp:783
double beatsPerMinute
Definition: DistrhoPlugin.hpp:817
Definition: DistrhoPlugin.hpp:748
bool playing
Definition: DistrhoPlugin.hpp:752
uint64_t frame
Definition: DistrhoPlugin.hpp:759
void clear() noexcept
Definition: DistrhoPlugin.hpp:861
TimePosition() noexcept
Definition: DistrhoPlugin.hpp:853