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.

200 lines
6.3KB

  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 that demonstrates tempo sync in DPF.
  21. */
  22. class ExamplePluginMetronome : public Plugin
  23. {
  24. public:
  25. ExamplePluginMetronome()
  26. : Plugin(0, 0, 0), // 0 parameters, 0 programs, 0 states
  27. sampleRate(44100.0f),
  28. counter(0) {}
  29. protected:
  30. /* --------------------------------------------------------------------------------------------------------
  31. * Information */
  32. /**
  33. Get the plugin label.
  34. A plugin label follows the same rules as Parameter::symbol, with the exception that it can start with numbers.
  35. */
  36. const char* getLabel() const override
  37. {
  38. return "Metronome";
  39. }
  40. /**
  41. Get an extensive comment/description about the plugin.
  42. */
  43. const char* getDescription() const override
  44. {
  45. return "Simple metronome plugin which outputs impulse at the start of every beat.";
  46. }
  47. /**
  48. Get the plugin author/maker.
  49. */
  50. const char* getMaker() const override
  51. {
  52. return "DISTRHO";
  53. }
  54. /**
  55. Get the plugin homepage.
  56. */
  57. const char* getHomePage() const override
  58. {
  59. return "https://github.com/DISTRHO/DPF";
  60. }
  61. /**
  62. Get the plugin license name (a single line of text).
  63. For commercial plugins this should return some short copyright information.
  64. */
  65. const char* getLicense() const override
  66. {
  67. return "ISC";
  68. }
  69. /**
  70. Get the plugin version, in hexadecimal.
  71. */
  72. uint32_t getVersion() const override
  73. {
  74. return d_version(1, 0, 0);
  75. }
  76. /**
  77. Get the plugin unique Id.
  78. This value is used by LADSPA, DSSI and VST plugin formats.
  79. */
  80. int64_t getUniqueId() const override
  81. {
  82. return d_cconst('d', 'M', 'e', 't');
  83. }
  84. /* --------------------------------------------------------------------------------------------------------
  85. * Init */
  86. /**
  87. Initialize the parameter @a index.
  88. This function will be called once, shortly after the plugin is created.
  89. */
  90. void initParameter(uint32_t /* index */, Parameter& /* parameter */) override
  91. {
  92. }
  93. /* --------------------------------------------------------------------------------------------------------
  94. * Internal data */
  95. /**
  96. Get the current value of a parameter.
  97. */
  98. float getParameterValue(uint32_t /* index */) const override
  99. {
  100. return 0.0f;
  101. }
  102. /**
  103. Change a parameter value.
  104. */
  105. void setParameterValue(uint32_t /* index */, float /* value */) override
  106. {
  107. }
  108. /* --------------------------------------------------------------------------------------------------------
  109. * Process */
  110. /**
  111. Run/process function for plugins without MIDI input.
  112. `inputs` is commented out because this plugin has no inputs.
  113. */
  114. void run(const float** /* inputs */, float** outputs, uint32_t frames) override
  115. {
  116. const TimePosition& timePos(getTimePosition());
  117. if (timePos.playing && timePos.bbt.valid) {
  118. // Better to use double when manipulating time.
  119. double secondsPerBeat = 60.0 / timePos.bbt.beatsPerMinute;
  120. double framesPerBeat = sampleRate * secondsPerBeat;
  121. double beatFraction = fmod(timePos.bbt.barBeat, 1.0);
  122. // If beatFraction == 0.0, next beat is exactly at the start of currenct cycle.
  123. // Otherwise, reset counter to the frames to the next beat.
  124. counter = beatFraction == 0.0
  125. ? 0
  126. : static_cast<uint32_t>(framesPerBeat * (1.0 - beatFraction));
  127. for (uint32_t i = 0; i < frames; ++i) {
  128. if (counter <= 0) {
  129. outputs[0][i] = 1.0f;
  130. counter = uint32_t(framesPerBeat);
  131. } else {
  132. outputs[0][i] = 0.0f;
  133. }
  134. --counter;
  135. }
  136. } else {
  137. // Stop metronome if not playing or timePos.bbt is invalid.
  138. for (uint32_t i = 0; i < frames; ++i) outputs[0][i] = 0.0f;
  139. }
  140. }
  141. /* --------------------------------------------------------------------------------------------------------
  142. * Callbacks (optional) */
  143. /**
  144. Optional callback to inform the plugin about a sample rate change.
  145. This function will only be called when the plugin is deactivated.
  146. */
  147. void sampleRateChanged(double newSampleRate) override
  148. {
  149. sampleRate = newSampleRate;
  150. }
  151. // -------------------------------------------------------------------------------------------------------
  152. private:
  153. float sampleRate;
  154. uint32_t counter; // Stores number of frames to the next beat.
  155. /**
  156. Set our plugin class as non-copyable and add a leak detector just in case.
  157. */
  158. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExamplePluginMetronome)
  159. };
  160. /* ------------------------------------------------------------------------------------------------------------
  161. * Plugin entry point, called by DPF to create a new plugin instance. */
  162. Plugin* createPlugin()
  163. {
  164. return new ExamplePluginMetronome();
  165. }
  166. // -----------------------------------------------------------------------------------------------------------
  167. END_NAMESPACE_DISTRHO