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.

245 lines
7.5KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2015 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. #include "DistrhoPlugin.hpp"
  17. START_NAMESPACE_DISTRHO
  18. // -----------------------------------------------------------------------------------------------------------
  19. /**
  20. Simple plugin to demonstrate how to modify input/output port type in DPF.
  21. The plugin outputs sample & hold (S&H) value of input signal.
  22. User can specify hold time via parameter and/or Hold Time CV port.
  23. */
  24. class ExamplePluginCVPort : public Plugin
  25. {
  26. public:
  27. ExamplePluginCVPort()
  28. : Plugin(1, 0, 0), // 1 parameters, 0 programs, 0 states
  29. maxHoldTime(1.0f),
  30. sampleRate(44100.0f),
  31. counter(0),
  32. holdTime(0.0f),
  33. holdValue(0.0f) {}
  34. protected:
  35. /* --------------------------------------------------------------------------------------------------------
  36. * Information */
  37. /**
  38. Get the plugin label.
  39. A plugin label follows the same rules as Parameter::symbol, with the exception that it can start with numbers.
  40. */
  41. const char* getLabel() const override
  42. {
  43. return "CVPort";
  44. }
  45. /**
  46. Get an extensive comment/description about the plugin.
  47. */
  48. const char* getDescription() const override
  49. {
  50. return "Simple plugin with CVPort.\n\
  51. The 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. if (index == 0) {
  105. // Audio port doesn't need to specify port.hints.
  106. port.name = "Audio Input";
  107. port.symbol = "audio_in";
  108. return;
  109. }
  110. else if (index == 1) {
  111. port.hints = kAudioPortIsCV;
  112. port.name = "Hold Time";
  113. port.symbol = "hold_time";
  114. return;
  115. }
  116. // Add more condition here when increasing DISTRHO_PLUGIN_NUM_INPUTS.
  117. }
  118. else
  119. {
  120. if (index == 0) {
  121. port.hints = kAudioPortIsCV;
  122. port.name = "CV Output";
  123. port.symbol = "cv_out";
  124. return;
  125. }
  126. // Add more condition here when increasing DISTRHO_PLUGIN_NUM_OUTPUTS.
  127. }
  128. /**
  129. It shouldn't reach here, but just in case if index is greater than 0.
  130. */
  131. Plugin::initAudioPort(input, index, port);
  132. }
  133. /**
  134. Initialize the parameter @a index.
  135. This function will be called once, shortly after the plugin is created.
  136. */
  137. void initParameter(uint32_t index, Parameter& parameter) override
  138. {
  139. if (index != 0) return;
  140. parameter.name = "Hold Time";
  141. parameter.symbol = "hold_time";
  142. parameter.hints = kParameterIsAutomable|kParameterIsLogarithmic;
  143. parameter.ranges.min = 0.0f;
  144. parameter.ranges.max = maxHoldTime;
  145. parameter.ranges.def = 0.1f;
  146. }
  147. /* --------------------------------------------------------------------------------------------------------
  148. * Internal data */
  149. /**
  150. Get the current value of a parameter.
  151. */
  152. inline float getParameterValue(uint32_t index) const override
  153. {
  154. return index == 0 ? holdTime : 0.0f;
  155. }
  156. /**
  157. Change a parameter value.
  158. */
  159. void setParameterValue(uint32_t index, float value) override
  160. {
  161. if (index != 0) return;
  162. holdTime = value;
  163. counter = uint32_t(holdTime * sampleRate);
  164. }
  165. /* --------------------------------------------------------------------------------------------------------
  166. * Process */
  167. /**
  168. Run/process function for plugins without MIDI input.
  169. */
  170. void run(const float** inputs, float** outputs, uint32_t frames) override
  171. {
  172. /**
  173. - inputs[0] is input audio port.
  174. - inputs[1] is hold time CV port.
  175. - outputs[0] is output CV port.
  176. */
  177. for (uint32_t i = 0; i < frames; ++i) {
  178. if (counter == 0) {
  179. float cv = inputs[1][i] > 0.0f ? inputs[1][i] : 0.0f;
  180. float time = holdTime + cv;
  181. if (time > maxHoldTime)
  182. time = maxHoldTime;
  183. counter = uint32_t(time * sampleRate);
  184. holdValue = inputs[0][i]; // Refresh hold value.
  185. } else {
  186. --counter;
  187. }
  188. outputs[0][i] = holdValue;
  189. }
  190. }
  191. // -------------------------------------------------------------------------------------------------------
  192. private:
  193. const float maxHoldTime;
  194. float sampleRate;
  195. uint32_t counter; // Hold time in samples. Used to count hold time.
  196. float holdTime; // Hold time in seconds.
  197. float holdValue;
  198. /**
  199. Set our plugin class as non-copyable and add a leak detector just in case.
  200. */
  201. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExamplePluginCVPort)
  202. };
  203. /* ------------------------------------------------------------------------------------------------------------
  204. * Plugin entry point, called by DPF to create a new plugin instance. */
  205. Plugin* createPlugin()
  206. {
  207. return new ExamplePluginCVPort();
  208. }
  209. // -----------------------------------------------------------------------------------------------------------
  210. END_NAMESPACE_DISTRHO