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.
376  Overloaded function using double precision values.
377  */
378  double getNormalizedValue(const double& value) const noexcept
379  {
380  const double normValue = (value - min) / (max - min);
381 
382  if (normValue <= 0.0)
383  return 0.0;
384  if (normValue >= 1.0)
385  return 1.0;
386  return normValue;
387  }
388 
389  /**
390  Get a value normalized to 0.0<->1.0, fixed within range.
391  */
392  float getFixedAndNormalizedValue(const float& value) const noexcept
393  {
394  if (value <= min)
395  return 0.0f;
396  if (value >= max)
397  return 1.0f;
398 
399  const float normValue = (value - min) / (max - min);
400 
401  if (normValue <= 0.0f)
402  return 0.0f;
403  if (normValue >= 1.0f)
404  return 1.0f;
405 
406  return normValue;
407  }
408 
409  /**
410  Get a value normalized to 0.0<->1.0, fixed within range.
411  Overloaded function using double precision values.
412  */
413  double getFixedAndNormalizedValue(const double& value) const noexcept
414  {
415  if (value <= min)
416  return 0.0;
417  if (value >= max)
418  return 1.0;
419 
420  const double normValue = (value - min) / (max - min);
421 
422  if (normValue <= 0.0)
423  return 0.0;
424  if (normValue >= 1.0)
425  return 1.0;
426 
427  return normValue;
428  }
429 
430  /**
431  Get a proper value previously normalized to 0.0<->1.0.
432  */
433  float getUnnormalizedValue(const float& value) const noexcept
434  {
435  if (value <= 0.0f)
436  return min;
437  if (value >= 1.0f)
438  return max;
439 
440  return value * (max - min) + min;
441  }
442 
443  /**
444  Get a proper value previously normalized to 0.0<->1.0.
445  Overloaded function using double precision values.
446  */
447  double getUnnormalizedValue(const double& value) const noexcept
448  {
449  if (value <= 0.0)
450  return min;
451  if (value >= 1.0)
452  return max;
453 
454  return value * (max - min) + min;
455  }
456 };
457 
458 /**
459  Parameter enumeration value.@n
460  A string representation of a plugin parameter value.@n
461  Used together can be used to give meaning to parameter values, working as an enumeration.
462  */
464  /**
465  Parameter value.
466  */
467  float value;
468 
469  /**
470  String representation of this value.
471  */
473 
474  /**
475  Default constructor, using 0.0 as value and empty label.
476  */
478  : value(0.0f),
479  label() {}
480 
481  /**
482  Constructor using custom values.
483  */
484  ParameterEnumerationValue(float v, const char* l) noexcept
485  : value(v),
486  label(l) {}
487 };
488 
489 /**
490  Collection of parameter enumeration values.@n
491  Handy class to handle the lifetime and count of all enumeration values.
492  */
494  /**
495  Number of elements allocated in @values.
496  */
497  uint8_t count;
498 
499  /**
500  Wherever the host is to be restricted to only use enumeration values.
501 
502  @note This mode is only a hint! Not all hosts and plugin formats support this mode.
503  */
505 
506  /**
507  Array of @ParameterEnumerationValue items.@n
508  This pointer must be null or have been allocated on the heap with `new ParameterEnumerationValue[count]`.
509  */
511 
512  /**
513  Default constructor, for zero enumeration values.
514  */
516  : count(0),
517  restrictedMode(false),
518  values() {}
519 
520  /**
521  Constructor using custom values.@n
522  The pointer to @values must have been allocated on the heap with `new`.
523  */
524  ParameterEnumerationValues(uint32_t c, bool r, ParameterEnumerationValue* v) noexcept
525  : count(c),
526  restrictedMode(r),
527  values(v) {}
528 
529  ~ParameterEnumerationValues() noexcept
530  {
531  count = 0;
532  restrictedMode = false;
533 
534  if (values != nullptr)
535  {
536  delete[] values;
537  values = nullptr;
538  }
539  }
540 
541  DISTRHO_DECLARE_NON_COPYABLE(ParameterEnumerationValues)
542 };
543 
544 /**
545  Parameter.
546  */
547 struct Parameter {
548  /**
549  Hints describing this parameter.
550  @see ParameterHints
551  */
552  uint32_t hints;
553 
554  /**
555  The name of this parameter.@n
556  A parameter name can contain any character, but hosts might have a hard time with non-ascii ones.@n
557  The name doesn't have to be unique within a plugin instance, but it's recommended.
558  */
560 
561  /**
562  The short name of this parameter.@n
563  Used when displaying the parameter name in a very limited space.
564  @note This value is optional, the full name is used when the short one is missing.
565  */
567 
568  /**
569  The symbol of this parameter.@n
570  A parameter symbol is a short restricted name used as a machine and human readable identifier.@n
571  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.
572  @note Parameter symbols MUST be unique within a plugin instance.
573  */
575 
576  /**
577  The unit of this parameter.@n
578  This means something like "dB", "kHz" and "ms".@n
579  Can be left blank if a unit does not apply to this parameter.
580  */
582 
583  /**
584  An extensive description/comment about the parameter.
585  @note This value is optional and only used for LV2.
586  */
588 
589  /**
590  Ranges of this parameter.@n
591  The ranges describe the default, minimum and maximum values.
592  */
594 
595  /**
596  Enumeration values.@n
597  Can be used to give meaning to parameter values, working as an enumeration.
598  */
600 
601  /**
602  Designation for this parameter.
603  */
605 
606  /**
607  MIDI CC to use by default on this parameter.@n
608  A value of 0 or 32 (bank change) is considered invalid.@n
609  Must also be less or equal to 120.
610  @note This value is only a hint! Hosts might map it automatically or completely ignore it.
611  */
612  uint8_t midiCC;
613 
614  /**
615  The group id that this parameter belongs to.
616  No group is assigned by default.
617 
618  You can use a group from PredefinedPortGroups or roll your own.@n
619  When rolling your own port groups, you MUST start their group ids from 0 and they MUST be sequential.
620  @see PortGroup, Plugin::initPortGroup
621  */
622  uint32_t groupId;
623 
624  /**
625  Default constructor for a null parameter.
626  */
627  Parameter() noexcept
628  : hints(0x0),
629  name(),
630  shortName(),
631  symbol(),
632  unit(),
633  ranges(),
634  enumValues(),
636  midiCC(0),
638 
639  /**
640  Constructor using custom values.
641  */
642  Parameter(uint32_t h, const char* n, const char* s, const char* u, float def, float min, float max) noexcept
643  : hints(h),
644  name(n),
645  shortName(),
646  symbol(s),
647  unit(u),
648  ranges(def, min, max),
649  enumValues(),
651  midiCC(0),
653 
654  /**
655  Initialize a parameter for a specific designation.
656  */
658  {
659  designation = d;
660 
661  switch (d)
662  {
664  break;
667  name = "Bypass";
668  shortName = "Bypass";
669  symbol = "dpf_bypass";
670  unit = "";
671  midiCC = 0;
673  ranges.def = 0.0f;
674  ranges.min = 0.0f;
675  ranges.max = 1.0f;
676  break;
677  }
678  }
679 };
680 
681 /**
682  Port Group.@n
683  Allows to group together audio/cv ports or parameters.
684 
685  Each unique group MUST have an unique symbol and a name.
686  A group can be applied to both inputs and outputs (at the same time).
687  The same group cannot be used in audio ports and parameters.
688 
689  When both audio and parameter groups are used, audio groups MUST be defined first.
690  That is, group indexes start with audio ports, then parameters.
691 
692  An audio port group logically combines ports which should be considered part of the same stream.@n
693  For example, two audio ports in a group may form a stereo stream.
694 
695  A parameter group provides meta-data to the host to indicate that some parameters belong together.
696 
697  The use of port groups is completely optional.
698 
699  @see Plugin::initPortGroup, AudioPort::group, Parameter::group
700  */
701 struct PortGroup {
702  /**
703  The name of this port group.@n
704  A port group name can contain any character, but hosts might have a hard time with non-ascii ones.@n
705  The name doesn't have to be unique within a plugin instance, but it's recommended.
706  */
708 
709  /**
710  The symbol of this port group.@n
711  A port group symbol is a short restricted name used as a machine and human readable identifier.@n
712  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.
713  @note Port group symbols MUST be unique within a plugin instance.
714  */
716 };
717 
718 /**
719  State.
720 
721  In DPF states refer to key:value string pairs, used to store arbitrary non-parameter data.@n
722  By default states are completely internal to the plugin and not visible by the host.@n
723  Flags can be set to allow hosts to see and/or change them.
724 
725  TODO API under construction
726  */
727 struct State {
728  /**
729  Hints describing this state.
730  @note Changing these hints can break compatibility with previously saved data.
731  @see StateHints
732  */
733  uint32_t hints;
734 
735  /**
736  The key or "symbol" of this state.@n
737  A state key is a short restricted name used as a machine and human readable identifier.
738  @note State keys MUST be unique within a plugin instance.
739  TODO define rules for allowed characters, must be usable as URI non-encoded parameters
740  */
742 
743  /**
744  The default value of this state.@n
745  Can be left empty if considered a valid initial state.
746  */
748 
749  /**
750  String representation of this state.
751  */
753 
754  /**
755  An extensive description/comment about this state.
756  @note This value is optional and only used for LV2.
757  */
759 
760  /**
761  Default constructor for a null state.
762  */
763  State() noexcept
764  : hints(0x0),
765  key(),
766  defaultValue(),
767  label(),
768  description() {}
769 };
770 
771 /**
772  MIDI event.
773  */
774 struct MidiEvent {
775  /**
776  Size of internal data.
777  */
778  static const uint32_t kDataSize = 4;
779 
780  /**
781  Time offset in frames.
782  */
783  uint32_t frame;
784 
785  /**
786  Number of bytes used.
787  */
788  uint32_t size;
789 
790  /**
791  MIDI data.@n
792  If size > kDataSize, dataExt is used (otherwise null).
793 
794  When dataExt is used, the event holder is responsible for
795  keeping the pointer valid during the entirety of the run function.
796  */
797  uint8_t data[kDataSize];
798  const uint8_t* dataExt;
799 };
800 
801 /**
802  Time position.@n
803  The @a playing and @a frame values are always valid.@n
804  BBT values are only valid when @a bbt.valid is true.
805 
806  This struct is inspired by the [JACK Transport API](https://jackaudio.org/api/structjack__position__t.html).
807  */
808 struct TimePosition {
809  /**
810  Wherever the host transport is playing/rolling.
811  */
812  bool playing;
813 
814  /**
815  Current host transport position in frames.
816  @note This value is not always monotonic,
817  with some plugin hosts assigning it based on a source that can accumulate rounding errors.
818  */
819  uint64_t frame;
820 
821  /**
822  Bar-Beat-Tick time position.
823  */
824  struct BarBeatTick {
825  /**
826  Wherever the host transport is using BBT.@n
827  If false you must not read from this struct.
828  */
829  bool valid;
830 
831  /**
832  Current bar.@n
833  Should always be > 0.@n
834  The first bar is bar '1'.
835  */
836  int32_t bar;
837 
838  /**
839  Current beat within bar.@n
840  Should always be > 0 and <= @a beatsPerBar.@n
841  The first beat is beat '1'.
842  */
843  int32_t beat;
844 
845  /**
846  Current tick within beat.@n
847  Should always be >= 0 and < @a ticksPerBeat.@n
848  The first tick is tick '0'.
849  @note Fraction part of tick is only available on some plugin formats.
850  */
851  double tick;
852 
853  /**
854  Number of ticks that have elapsed between frame 0 and the first beat of the current measure.
855  */
856  double barStartTick;
857 
858  /**
859  Time signature "numerator".
860  */
861  float beatsPerBar;
862 
863  /**
864  Time signature "denominator".
865  */
866  float beatType;
867 
868  /**
869  Number of ticks within a beat.@n
870  Usually a moderately large integer with many denominators, such as 1920.0.
871  */
872  double ticksPerBeat;
873 
874  /**
875  Number of beats per minute.
876  */
878 
879  /**
880  Default constructor for a null BBT time position.
881  */
882  BarBeatTick() noexcept
883  : valid(false),
884  bar(0),
885  beat(0),
886  tick(0),
887  barStartTick(0.0),
888  beatsPerBar(0.0f),
889  beatType(0.0f),
890  ticksPerBeat(0.0),
891  beatsPerMinute(0.0) {}
892 
893  /**
894  Reinitialize this position using the default null initialization.
895  */
896  void clear() noexcept
897  {
898  valid = false;
899  bar = 0;
900  beat = 0;
901  tick = 0;
902  barStartTick = 0.0;
903  beatsPerBar = 0.0f;
904  beatType = 0.0f;
905  ticksPerBeat = 0.0;
906  beatsPerMinute = 0.0;
907  }
908  } bbt;
909 
910  /**
911  Default constructor for a time position.
912  */
913  TimePosition() noexcept
914  : playing(false),
915  frame(0),
916  bbt() {}
917 
918  /**
919  Reinitialize this position using the default null initialization.
920  */
921  void clear() noexcept
922  {
923  playing = false;
924  frame = 0;
925  bbt.clear();
926  }
927 };
928 
929 /** @} */
930 
931 /* ------------------------------------------------------------------------------------------------------------
932  * DPF Plugin */
933 
934 /**
935  @defgroup MainClasses Main Classes
936  @{
937  */
938 
939 /**
940  DPF Plugin class from where plugin instances are created.
941 
942  The public methods (Host state) are called from the plugin to get or set host information.@n
943  They can be called from a plugin instance at anytime unless stated otherwise.@n
944  All other methods are to be implemented by the plugin and will be called by the host.
945 
946  Shortly after a plugin instance is created, the various init* functions will be called by the host.@n
947  Host will call activate() before run(), and deactivate() before the plugin instance is destroyed.@n
948  The host may call deactivate right after activate and vice-versa, but never activate/deactivate consecutively.@n
949  There is no limit on how many times run() is called, only that activate/deactivate will be called in between.
950 
951  The buffer size and sample rate values will remain constant between activate and deactivate.@n
952  Buffer size is only a hint though, the host might call run() with a higher or lower number of frames.
953 
954  Some of this class functions are only available according to some macros.
955 
956  DISTRHO_PLUGIN_WANT_PROGRAMS activates program related features.@n
957  When enabled you need to implement initProgramName() and loadProgram().
958 
959  DISTRHO_PLUGIN_WANT_STATE activates internal state features.@n
960  When enabled you need to implement initStateKey() and setState().
961 
962  The process function run() changes wherever DISTRHO_PLUGIN_WANT_MIDI_INPUT is enabled or not.@n
963  When enabled it provides midi input events.
964  */
965 class Plugin
966 {
967 public:
968  /**
969  Plugin class constructor.@n
970  You must set all parameter values to their defaults, matching ParameterRanges::def.
971  */
972  Plugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount);
973 
974  /**
975  Destructor.
976  */
977  virtual ~Plugin();
978 
979  /* --------------------------------------------------------------------------------------------------------
980  * Host state */
981 
982  /**
983  Get the current buffer size that will probably be used during processing, in frames.@n
984  This value will remain constant between activate and deactivate.
985  @note This value is only a hint!@n
986  Hosts might call run() with a higher or lower number of frames.
987  @see bufferSizeChanged(uint32_t)
988  */
989  uint32_t getBufferSize() const noexcept;
990 
991  /**
992  Get the current sample rate that will be used during processing.@n
993  This value will remain constant between activate and deactivate.
994  @see sampleRateChanged(double)
995  */
996  double getSampleRate() const noexcept;
997 
998  /**
999  Get the bundle path where the plugin resides.
1000  Can return null if the plugin is not available in a bundle (if it is a single binary).
1001  @see getBinaryFilename
1002  @see getResourcePath
1003  */
1004  const char* getBundlePath() const noexcept;
1005 
1006  /**
1007  Check if this plugin instance is a "dummy" one used for plugin meta-data/information export.@n
1008  When true no processing will be done, the plugin is created only to extract information.@n
1009  In DPF, LADSPA/DSSI, VST2 and VST3 formats create one global instance per plugin binary
1010  while LV2 creates one when generating turtle meta-data.
1011  */
1012  bool isDummyInstance() const noexcept;
1013 
1014  /**
1015  Check if this plugin instance is a "selftest" one used for automated plugin tests.@n
1016  To enable this mode build with `DPF_RUNTIME_TESTING` macro defined (i.e. set as compiler build flag),
1017  and run the JACK/Standalone executable with "selftest" as its only and single argument.
1018 
1019  A few basic DSP and UI tests will run in self-test mode, with once instance having this function returning true.@n
1020  You can use this chance to do a few tests of your own as well.
1021  */
1022  bool isSelfTestInstance() const noexcept;
1023 
1024 #if DISTRHO_PLUGIN_WANT_TIMEPOS
1025  /**
1026  Get the current host transport time position.@n
1027  This function should only be called during run().@n
1028  You can call this during other times, but the returned position is not guaranteed to be in sync.
1029  @note TimePosition is not supported in LADSPA and DSSI plugin formats.
1030  */
1031  const TimePosition& getTimePosition() const noexcept;
1032 #endif
1033 
1034 #if DISTRHO_PLUGIN_WANT_LATENCY
1035  /**
1036  Change the plugin audio output latency to @a frames.@n
1037  This function should only be called in the constructor, activate() and run().
1038  @note This function is only available if DISTRHO_PLUGIN_WANT_LATENCY is enabled.
1039  */
1040  void setLatency(uint32_t frames) noexcept;
1041 #endif
1042 
1043 #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
1044  /**
1045  Write a MIDI output event.@n
1046  This function must only be called during run().@n
1047  Returns false when the host buffer is full, in which case do not call this again until the next run().
1048  */
1049  bool writeMidiEvent(const MidiEvent& midiEvent) noexcept;
1050 #endif
1051 
1052 #if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST
1053  /**
1054  Check if parameter value change requests will work with the current plugin host.
1055  @note This function is only available if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST is enabled.
1056  @see requestParameterValueChange(uint32_t, float)
1057  */
1058  bool canRequestParameterValueChanges() const noexcept;
1059 
1060  /**
1061  Request a parameter value change from the host.
1062  If successful, this function will automatically trigger a parameter update on the UI side as well.
1063  This function can fail, for example if the host is busy with the parameter for read-only automation.
1064  Some hosts simply do not have this functionality, which can be verified with canRequestParameterValueChanges().
1065  @note This function is only available if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST is enabled.
1066  */
1067  bool requestParameterValueChange(uint32_t index, float value) noexcept;
1068 #endif
1069 
1070 #if DISTRHO_PLUGIN_WANT_STATE
1071  /**
1072  Set state value and notify the host about the change.@n
1073  This function will call `setState()` and also trigger an update on the UI side as necessary.@n
1074  It must not be called during run.@n
1075  The state must be host readable.
1076  @note this function does nothing on DSSI plugin format, as DSSI only supports UI->DSP messages.
1077 
1078  TODO API under construction
1079  */
1080  bool updateStateValue(const char* key, const char* value) noexcept;
1081 #endif
1082 
1083 protected:
1084  /* --------------------------------------------------------------------------------------------------------
1085  * Information */
1086 
1087  /**
1088  Get the plugin name.@n
1089  Returns DISTRHO_PLUGIN_NAME by default.
1090  */
1091  virtual const char* getName() const { return DISTRHO_PLUGIN_NAME; }
1092 
1093  /**
1094  Get the plugin label.@n
1095  This label is a short restricted name consisting of only _, a-z, A-Z and 0-9 characters.
1096  */
1097  virtual const char* getLabel() const = 0;
1098 
1099  /**
1100  Get an extensive comment/description about the plugin.@n
1101  Optional, returns nothing by default.
1102  */
1103  virtual const char* getDescription() const { return ""; }
1104 
1105  /**
1106  Get the plugin author/maker.
1107  */
1108  virtual const char* getMaker() const = 0;
1109 
1110  /**
1111  Get the plugin homepage.@n
1112  Optional, returns nothing by default.
1113  */
1114  virtual const char* getHomePage() const { return ""; }
1115 
1116  /**
1117  Get the plugin license (a single line of text or a URL).@n
1118  For commercial plugins this should return some short copyright information.
1119  */
1120  virtual const char* getLicense() const = 0;
1121 
1122  /**
1123  Get the plugin version, in hexadecimal.
1124  @see d_version()
1125  */
1126  virtual uint32_t getVersion() const = 0;
1127 
1128  /**
1129  Get the plugin unique Id.@n
1130  This value is used by LADSPA, DSSI and VST plugin formats.
1131  @see d_cconst()
1132  */
1133  virtual int64_t getUniqueId() const = 0;
1134 
1135  /* --------------------------------------------------------------------------------------------------------
1136  * Init */
1137 
1138  /**
1139  Initialize the audio port @a index.@n
1140  This function will be called once, shortly after the plugin is created.
1141  */
1142  virtual void initAudioPort(bool input, uint32_t index, AudioPort& port);
1143 
1144  /**
1145  Initialize the parameter @a index.@n
1146  This function will be called once, shortly after the plugin is created.
1147  */
1148  virtual void initParameter(uint32_t index, Parameter& parameter);
1149 
1150  /**
1151  Initialize the port group @a groupId.@n
1152  This function will be called once,
1153  shortly after the plugin is created and all audio ports and parameters have been enumerated.
1154  */
1155  virtual void initPortGroup(uint32_t groupId, PortGroup& portGroup);
1156 
1157 #if DISTRHO_PLUGIN_WANT_PROGRAMS
1158  /**
1159  Set the name of the program @a index.@n
1160  This function will be called once, shortly after the plugin is created.@n
1161  Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
1162  */
1163  virtual void initProgramName(uint32_t index, String& programName) = 0;
1164 #endif
1165 
1166 #if DISTRHO_PLUGIN_WANT_STATE
1167  /**
1168  Initialize the state @a index.@n
1169  This function will be called once, shortly after the plugin is created.@n
1170  Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
1171  */
1172  virtual void initState(uint32_t index, State& state);
1173 
1174  DISTRHO_DEPRECATED_BY("initState(uint32_t,State&)")
1175  virtual void initState(uint32_t, String&, String&) {}
1176 
1177  DISTRHO_DEPRECATED_BY("initState(uint32_t,State&)")
1178  virtual bool isStateFile(uint32_t) { return false; }
1179 #endif
1180 
1181  /* --------------------------------------------------------------------------------------------------------
1182  * Internal data */
1183 
1184  /**
1185  Get the current value of a parameter.@n
1186  The host may call this function from any context, including realtime processing.
1187  */
1188  virtual float getParameterValue(uint32_t index) const;
1189 
1190  /**
1191  Change a parameter value.@n
1192  The host may call this function from any context, including realtime processing.@n
1193  When a parameter is marked as automatable, you must ensure no non-realtime operations are performed.
1194  @note This function will only be called for parameter inputs.
1195  */
1196  virtual void setParameterValue(uint32_t index, float value);
1197 
1198 #if DISTRHO_PLUGIN_WANT_PROGRAMS
1199  /**
1200  Load a program.@n
1201  The host may call this function from any context, including realtime processing.@n
1202  Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
1203  */
1204  virtual void loadProgram(uint32_t index);
1205 #endif
1206 
1207 #if DISTRHO_PLUGIN_WANT_FULL_STATE
1208  /**
1209  Get the value of an internal state.@n
1210  The host may call this function from any non-realtime context.@n
1211  Must be implemented by your plugin class if DISTRHO_PLUGIN_WANT_FULL_STATE is enabled.
1212  @note The use of this function breaks compatibility with the DSSI format.
1213  */
1214  virtual String getState(const char* key) const;
1215 #endif
1216 
1217 #if DISTRHO_PLUGIN_WANT_STATE
1218  /**
1219  Change an internal state @a key to @a value.@n
1220  Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
1221  */
1222  virtual void setState(const char* key, const char* value);
1223 #endif
1224 
1225  /* --------------------------------------------------------------------------------------------------------
1226  * Audio/MIDI Processing */
1227 
1228  /**
1229  Activate this plugin.
1230  */
1231  virtual void activate() {}
1232 
1233  /**
1234  Deactivate this plugin.
1235  */
1236  virtual void deactivate() {}
1237 
1238 #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
1239  /**
1240  Run/process function for plugins with MIDI input.
1241  @note Some parameters might be null if there are no audio inputs/outputs or MIDI events.
1242  */
1243  virtual void run(const float** inputs, float** outputs, uint32_t frames,
1244  const MidiEvent* midiEvents, uint32_t midiEventCount) = 0;
1245 #else
1246  /**
1247  Run/process function for plugins without MIDI input.
1248  @note Some parameters might be null if there are no audio inputs or outputs.
1249  */
1250  virtual void run(const float** inputs, float** outputs, uint32_t frames) = 0;
1251 #endif
1252 
1253  /* --------------------------------------------------------------------------------------------------------
1254  * Callbacks (optional) */
1255 
1256  /**
1257  Optional callback to inform the plugin about a buffer size change.@n
1258  This function will only be called when the plugin is deactivated.
1259  @note This value is only a hint!@n
1260  Hosts might call run() with a higher or lower number of frames.
1261  @see getBufferSize()
1262  */
1263  virtual void bufferSizeChanged(uint32_t newBufferSize);
1264 
1265  /**
1266  Optional callback to inform the plugin about a sample rate change.@n
1267  This function will only be called when the plugin is deactivated.
1268  @see getSampleRate()
1269  */
1270  virtual void sampleRateChanged(double newSampleRate);
1271 
1272  // -------------------------------------------------------------------------------------------------------
1273 
1274 private:
1275  struct PrivateData;
1276  PrivateData* const pData;
1277  friend class PluginExporter;
1278 
1279  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Plugin)
1280 };
1281 
1282 /** @} */
1283 
1284 /* ------------------------------------------------------------------------------------------------------------
1285  * Create plugin, entry point */
1286 
1287 /**
1288  @defgroup EntryPoints Entry Points
1289  @{
1290  */
1291 
1292 /**
1293  Create an instance of the Plugin class.@n
1294  This is the entry point for DPF plugins.@n
1295  DPF will call this to either create an instance of your plugin for the host
1296  or to fetch some initial information for internal caching.
1297  */
1299 
1300 /** @} */
1301 
1302 // -----------------------------------------------------------------------------------------------------------
1303 
1305 
1306 #endif // DISTRHO_PLUGIN_HPP_INCLUDED
Definition: DistrhoPlugin.hpp:966
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:1103
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()
bool isSelfTestInstance() const noexcept
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:1236
virtual void activate()
Definition: DistrhoPlugin.hpp:1231
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:1114
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:1091
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:920
#define START_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:914
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:488
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:774
uint32_t size
Definition: DistrhoPlugin.hpp:788
static const uint32_t kDataSize
Definition: DistrhoPlugin.hpp:778
uint8_t data[kDataSize]
Definition: DistrhoPlugin.hpp:797
uint32_t frame
Definition: DistrhoPlugin.hpp:783
Definition: DistrhoPlugin.hpp:463
ParameterEnumerationValue() noexcept
Definition: DistrhoPlugin.hpp:477
float value
Definition: DistrhoPlugin.hpp:467
ParameterEnumerationValue(float v, const char *l) noexcept
Definition: DistrhoPlugin.hpp:484
String label
Definition: DistrhoPlugin.hpp:472
Definition: DistrhoPlugin.hpp:493
bool restrictedMode
Definition: DistrhoPlugin.hpp:504
ParameterEnumerationValues(uint32_t c, bool r, ParameterEnumerationValue *v) noexcept
Definition: DistrhoPlugin.hpp:524
ParameterEnumerationValues() noexcept
Definition: DistrhoPlugin.hpp:515
ParameterEnumerationValue * values
Definition: DistrhoPlugin.hpp:510
uint8_t count
Definition: DistrhoPlugin.hpp:497
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:392
float getFixedValue(const float &value) const noexcept
Definition: DistrhoPlugin.hpp:351
float getUnnormalizedValue(const float &value) const noexcept
Definition: DistrhoPlugin.hpp:433
float def
Definition: DistrhoPlugin.hpp:301
double getNormalizedValue(const double &value) const noexcept
Definition: DistrhoPlugin.hpp:378
float getNormalizedValue(const float &value) const noexcept
Definition: DistrhoPlugin.hpp:363
double getFixedAndNormalizedValue(const double &value) const noexcept
Definition: DistrhoPlugin.hpp:413
ParameterRanges() noexcept
Definition: DistrhoPlugin.hpp:316
double getUnnormalizedValue(const double &value) const noexcept
Definition: DistrhoPlugin.hpp:447
Definition: DistrhoPlugin.hpp:547
Parameter() noexcept
Definition: DistrhoPlugin.hpp:627
String shortName
Definition: DistrhoPlugin.hpp:566
ParameterRanges ranges
Definition: DistrhoPlugin.hpp:593
String unit
Definition: DistrhoPlugin.hpp:581
uint32_t hints
Definition: DistrhoPlugin.hpp:552
ParameterDesignation designation
Definition: DistrhoPlugin.hpp:604
String symbol
Definition: DistrhoPlugin.hpp:574
uint8_t midiCC
Definition: DistrhoPlugin.hpp:612
String description
Definition: DistrhoPlugin.hpp:587
uint32_t groupId
Definition: DistrhoPlugin.hpp:622
ParameterEnumerationValues enumValues
Definition: DistrhoPlugin.hpp:599
void initDesignation(ParameterDesignation d) noexcept
Definition: DistrhoPlugin.hpp:657
String name
Definition: DistrhoPlugin.hpp:559
Parameter(uint32_t h, const char *n, const char *s, const char *u, float def, float min, float max) noexcept
Definition: DistrhoPlugin.hpp:642
Definition: DistrhoPlugin.hpp:701
String symbol
Definition: DistrhoPlugin.hpp:715
String name
Definition: DistrhoPlugin.hpp:707
Definition: DistrhoPlugin.hpp:727
String key
Definition: DistrhoPlugin.hpp:741
State() noexcept
Definition: DistrhoPlugin.hpp:763
String label
Definition: DistrhoPlugin.hpp:752
uint32_t hints
Definition: DistrhoPlugin.hpp:733
String defaultValue
Definition: DistrhoPlugin.hpp:747
String description
Definition: DistrhoPlugin.hpp:758
Definition: DistrhoPlugin.hpp:824
float beatType
Definition: DistrhoPlugin.hpp:866
bool valid
Definition: DistrhoPlugin.hpp:829
float beatsPerBar
Definition: DistrhoPlugin.hpp:861
double barStartTick
Definition: DistrhoPlugin.hpp:856
double ticksPerBeat
Definition: DistrhoPlugin.hpp:872
BarBeatTick() noexcept
Definition: DistrhoPlugin.hpp:882
int32_t bar
Definition: DistrhoPlugin.hpp:836
void clear() noexcept
Definition: DistrhoPlugin.hpp:896
double tick
Definition: DistrhoPlugin.hpp:851
int32_t beat
Definition: DistrhoPlugin.hpp:843
double beatsPerMinute
Definition: DistrhoPlugin.hpp:877
Definition: DistrhoPlugin.hpp:808
bool playing
Definition: DistrhoPlugin.hpp:812
uint64_t frame
Definition: DistrhoPlugin.hpp:819
void clear() noexcept
Definition: DistrhoPlugin.hpp:921
TimePosition() noexcept
Definition: DistrhoPlugin.hpp:913