DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
8.3KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
  4. * Copyright (C) 2020 Takamitsu Endo
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  7. * or without fee is hereby granted, provided that the above copyright notice and this
  8. * permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  11. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  12. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  15. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "DistrhoPlugin.hpp"
  18. START_NAMESPACE_DISTRHO
  19. // -----------------------------------------------------------------------------------------------------------
  20. static constexpr const float kMaxHoldTime = 1.0f;
  21. /**
  22. Simple plugin to demonstrate how to modify input/output port type in DPF.
  23. The plugin outputs sample & hold (S&H) value of input signal.
  24. User can specify hold time via parameter and/or Hold Time CV port.
  25. */
  26. class ExamplePluginCVPort : public Plugin
  27. {
  28. public:
  29. ExamplePluginCVPort()
  30. : Plugin(1, 0, 0), // 1 parameters, 0 programs, 0 states
  31. counter(0),
  32. holdTime(0.0f),
  33. holdValue(0.0f),
  34. sampleRate(getSampleRate()) {}
  35. protected:
  36. /* --------------------------------------------------------------------------------------------------------
  37. * Information */
  38. /**
  39. Get the plugin label.
  40. A plugin label follows the same rules as Parameter::symbol, with the exception that it can start with numbers.
  41. */
  42. const char* getLabel() const override
  43. {
  44. return "CVPort";
  45. }
  46. /**
  47. Get an extensive comment/description about the plugin.
  48. */
  49. const char* getDescription() const override
  50. {
  51. return "Simple plugin with CVPort.\nThe plugin does sample & hold processing.";
  52. }
  53. /**
  54. Get the plugin author/maker.
  55. */
  56. const char* getMaker() const override
  57. {
  58. return "DISTRHO";
  59. }
  60. /**
  61. Get the plugin homepage.
  62. */
  63. const char* getHomePage() const override
  64. {
  65. return "https://github.com/DISTRHO/DPF";
  66. }
  67. /**
  68. Get the plugin license name (a single line of text).
  69. For commercial plugins this should return some short copyright information.
  70. */
  71. const char* getLicense() const override
  72. {
  73. return "ISC";
  74. }
  75. /**
  76. Get the plugin version, in hexadecimal.
  77. */
  78. uint32_t getVersion() const override
  79. {
  80. return d_version(1, 0, 0);
  81. }
  82. /**
  83. Get the plugin unique Id.
  84. This value is used by LADSPA, DSSI and VST plugin formats.
  85. */
  86. int64_t getUniqueId() const override
  87. {
  88. return d_cconst('d', 'C', 'V', 'P');
  89. }
  90. /* --------------------------------------------------------------------------------------------------------
  91. * Init */
  92. /**
  93. Initialize the audio port @a index.@n
  94. This function will be called once, shortly after the plugin is created.
  95. */
  96. void initAudioPort(bool input, uint32_t index, AudioPort& port) override
  97. {
  98. /**
  99. Note that index is independent for input and output.
  100. In other words, input port index starts from 0 and output port index also starts from 0.
  101. */
  102. if (input)
  103. {
  104. switch (index)
  105. {
  106. case 0:
  107. // Audio port doesn't need to specify port.hints.
  108. port.name = "Audio Input";
  109. port.symbol = "audio_in";
  110. return;
  111. case 1:
  112. port.hints = kAudioPortIsCV;
  113. port.name = "Hold Time";
  114. port.symbol = "hold_time";
  115. return;
  116. }
  117. // Add more conditions here when increasing DISTRHO_PLUGIN_NUM_INPUTS.
  118. }
  119. else
  120. {
  121. switch (index)
  122. {
  123. case 0:
  124. port.hints = kAudioPortIsCV;
  125. port.name = "CV Output";
  126. port.symbol = "cv_out";
  127. return;
  128. }
  129. // Add more conditions here when increasing DISTRHO_PLUGIN_NUM_OUTPUTS.
  130. }
  131. // It shouldn't reach here, but just in case if index is greater than 0.
  132. Plugin::initAudioPort(input, index, port);
  133. }
  134. /**
  135. Initialize the parameter @a index.
  136. This function will be called once, shortly after the plugin is created.
  137. */
  138. void initParameter(uint32_t index, Parameter& parameter) override
  139. {
  140. if (index != 0)
  141. return;
  142. parameter.name = "Hold Time";
  143. parameter.symbol = "hold_time";
  144. parameter.hints = kParameterIsAutomatable|kParameterIsLogarithmic;
  145. parameter.ranges.min = 0.0f;
  146. parameter.ranges.max = kMaxHoldTime;
  147. parameter.ranges.def = 0.1f;
  148. }
  149. /* --------------------------------------------------------------------------------------------------------
  150. * Internal data */
  151. /**
  152. Get the current value of a parameter.
  153. */
  154. float getParameterValue(uint32_t index) const override
  155. {
  156. if (index != 0)
  157. return 0.0f;
  158. return holdTime;
  159. }
  160. /**
  161. Change a parameter value.
  162. */
  163. void setParameterValue(uint32_t index, float value) override
  164. {
  165. if (index != 0)
  166. return;
  167. holdTime = value;
  168. counter = uint32_t(holdTime * sampleRate);
  169. }
  170. /* --------------------------------------------------------------------------------------------------------
  171. * Process */
  172. /**
  173. Run/process function for plugins without MIDI input.
  174. */
  175. void run(const float** inputs, float** outputs, uint32_t frames) override
  176. {
  177. float cv, time;
  178. /**
  179. - inputs[0] is input audio port.
  180. - inputs[1] is hold time CV port.
  181. - outputs[0] is output CV port.
  182. */
  183. const float* const audioIn = inputs[0];
  184. const float* const holdCV = inputs[1];
  185. float* const cvOut = outputs[0];
  186. for (uint32_t i = 0; i < frames; ++i)
  187. {
  188. if (counter == 0)
  189. {
  190. cv = holdCV[i] > 0.0f ? holdCV[i] : 0.0f;
  191. time = holdTime + cv;
  192. if (time > kMaxHoldTime)
  193. time = kMaxHoldTime;
  194. counter = static_cast<uint32_t>(time * sampleRate + 0.5f);
  195. holdValue = audioIn[i]; // Refresh hold value.
  196. }
  197. else
  198. {
  199. --counter;
  200. }
  201. cvOut[i] = holdValue;
  202. }
  203. }
  204. /* --------------------------------------------------------------------------------------------------------
  205. * Callbacks (optional) */
  206. /**
  207. Optional callback to inform the plugin about a sample rate change.@n
  208. This function will only be called when the plugin is deactivated.
  209. */
  210. void sampleRateChanged(double newSampleRate) override
  211. {
  212. sampleRate = newSampleRate;
  213. counter = static_cast<uint32_t>(holdTime * sampleRate + 0.5f);
  214. }
  215. // -------------------------------------------------------------------------------------------------------
  216. private:
  217. uint32_t counter; // Hold time in samples. Used to count hold time.
  218. float holdTime; // Hold time in seconds.
  219. float holdValue;
  220. float sampleRate;
  221. /**
  222. Set our plugin class as non-copyable and add a leak detector just in case.
  223. */
  224. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExamplePluginCVPort)
  225. };
  226. /* ------------------------------------------------------------------------------------------------------------
  227. * Plugin entry point, called by DPF to create a new plugin instance. */
  228. Plugin* createPlugin()
  229. {
  230. return new ExamplePluginCVPort();
  231. }
  232. // -----------------------------------------------------------------------------------------------------------
  233. END_NAMESPACE_DISTRHO