DPF Plugin examples
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.

237 lines
7.2KB

  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. Plugin to show how to get some basic information sent to the UI.
  21. */
  22. class LatencyExamplePlugin : public Plugin
  23. {
  24. public:
  25. LatencyExamplePlugin()
  26. : Plugin(1, 0, 0), // 1 parameter
  27. fLatency(1.0f),
  28. fLatencyInFrames(0),
  29. fBuffer(nullptr),
  30. fBufferPos(0)
  31. {
  32. // allocates buffer
  33. sampleRateChanged(getSampleRate());
  34. }
  35. ~LatencyExamplePlugin() override
  36. {
  37. delete[] fBuffer;
  38. }
  39. protected:
  40. /* --------------------------------------------------------------------------------------------------------
  41. * Information */
  42. /**
  43. Get the plugin label.
  44. This label is a short restricted name consisting of only _, a-z, A-Z and 0-9 characters.
  45. */
  46. const char* getLabel() const override
  47. {
  48. return "Latency";
  49. }
  50. /**
  51. Get the plugin author/maker.
  52. */
  53. const char* getMaker() const override
  54. {
  55. return "DISTRHO";
  56. }
  57. /**
  58. Get the plugin license name (a single line of text).
  59. For commercial plugins this should return some short copyright information.
  60. */
  61. const char* getLicense() const override
  62. {
  63. return "ISC";
  64. }
  65. /**
  66. Get the plugin version, in hexadecimal.
  67. TODO format to be defined
  68. */
  69. uint32_t getVersion() const override
  70. {
  71. return 0x1000;
  72. }
  73. /**
  74. Get the plugin unique Id.
  75. This value is used by LADSPA, DSSI and VST plugin formats.
  76. */
  77. int64_t getUniqueId() const override
  78. {
  79. return d_cconst('d', 'L', 'a', 't');
  80. }
  81. /* --------------------------------------------------------------------------------------------------------
  82. * Init */
  83. /**
  84. Initialize the parameter @a index.
  85. This function will be called once, shortly after the plugin is created.
  86. */
  87. void initParameter(uint32_t index, Parameter& parameter) override
  88. {
  89. if (index != 0)
  90. return;
  91. parameter.hints = kParameterIsAutomable;
  92. parameter.name = "Latency";
  93. parameter.symbol = "latency";
  94. parameter.unit = "s";
  95. parameter.ranges.def = 1.0f;
  96. parameter.ranges.min = 0.0f;
  97. parameter.ranges.max = 5.0f;
  98. }
  99. /* --------------------------------------------------------------------------------------------------------
  100. * Internal data */
  101. /**
  102. Get the current value of a parameter.
  103. The host may call this function from any context, including realtime processing.
  104. */
  105. float getParameterValue(uint32_t index) const override
  106. {
  107. if (index != 0)
  108. return 0.0f;
  109. return fLatency;
  110. }
  111. /**
  112. Change a parameter value.
  113. The host may call this function from any context, including realtime processing.
  114. When a parameter is marked as automable, you must ensure no non-realtime operations are performed.
  115. @note This function will only be called for parameter inputs.
  116. */
  117. void setParameterValue(uint32_t index, float value) override
  118. {
  119. if (index != 0)
  120. return;
  121. fLatency = value;
  122. fLatencyInFrames = value*getSampleRate();
  123. setLatency(fLatencyInFrames);
  124. }
  125. /* --------------------------------------------------------------------------------------------------------
  126. * Audio/MIDI Processing */
  127. /**
  128. Run/process function for plugins without MIDI input.
  129. @note Some parameters might be null if there are no audio inputs or outputs.
  130. */
  131. void run(const float** inputs, float** outputs, uint32_t frames) override
  132. {
  133. const float* const in = inputs[0];
  134. /* */ float* const out = outputs[0];
  135. if (fLatencyInFrames == 0)
  136. {
  137. if (out != in)
  138. std::memcpy(out, in, sizeof(float)*frames);
  139. return;
  140. }
  141. // Put the new audio in the buffer.
  142. std::memcpy(fBuffer+fBufferPos, in, sizeof(float)*frames);
  143. fBufferPos += frames;
  144. // buffer is not filled enough yet
  145. if (fBufferPos < fLatencyInFrames+frames)
  146. {
  147. // silence output
  148. std::memset(out, 0, sizeof(float)*frames);
  149. }
  150. // buffer is ready to copy
  151. else
  152. {
  153. // copy latency buffer to output
  154. const uint32_t readPos = fBufferPos-fLatencyInFrames-frames;
  155. std::memcpy(out, fBuffer+readPos, sizeof(float)*frames);
  156. // move latency buffer back by some frames
  157. std::memmove(fBuffer, fBuffer+frames, sizeof(float)*fBufferPos);
  158. fBufferPos -= frames;
  159. }
  160. }
  161. /* --------------------------------------------------------------------------------------------------------
  162. * Callbacks (optional) */
  163. /**
  164. Optional callback to inform the plugin about a sample rate change.
  165. This function will only be called when the plugin is deactivated.
  166. */
  167. void sampleRateChanged(double newSampleRate) override
  168. {
  169. if (fBuffer != nullptr)
  170. delete[] fBuffer;
  171. const uint32_t maxFrames = newSampleRate*6; // 6 seconds
  172. fBuffer = new float[maxFrames];
  173. std::memset(fBuffer, 0, sizeof(float)*maxFrames);
  174. fLatencyInFrames = fLatency*newSampleRate;
  175. fBufferPos = 0;
  176. }
  177. // -------------------------------------------------------------------------------------------------------
  178. private:
  179. // Parameters
  180. float fLatency;
  181. uint32_t fLatencyInFrames;
  182. // Buffer for previous audio, size depends on sample rate
  183. float* fBuffer;
  184. uint32_t fBufferPos;
  185. /**
  186. Set our plugin class as non-copyable and add a leak detector just in case.
  187. */
  188. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LatencyExamplePlugin)
  189. };
  190. /* ------------------------------------------------------------------------------------------------------------
  191. * Plugin entry point, called by DPF to create a new plugin instance. */
  192. Plugin* createPlugin()
  193. {
  194. return new LatencyExamplePlugin();
  195. }
  196. // -----------------------------------------------------------------------------------------------------------
  197. END_NAMESPACE_DISTRHO