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.

232 lines
7.1KB

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