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.

368 lines
12KB

  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. 1-pole lowpass filter to smooth out parameters and envelopes.
  21. This filter is guaranteed not to overshoot.
  22. */
  23. class Smoother {
  24. private:
  25. float kp;
  26. public:
  27. float value;
  28. Smoother()
  29. : kp(0.0f),
  30. value(0.0f) {}
  31. /**
  32. Set kp from cutoff frequency in Hz.
  33. For derivation, see the answer of Matt L. on the url below. Equation 3 is used.
  34. Computation is done on double for accuracy. When using float, kp will be inaccurate
  35. if the cutoffHz is below around 3.0 to 4.0 Hz.
  36. Reference:
  37. - [Single-pole IIR low-pass filter - which is the correct formula for the decay coefficient? - Signal Processing Stack Exchange](https://dsp.stackexchange.com/questions/54086/single-pole-iir-low-pass-filter-which-is-the-correct-formula-for-the-decay-coe)
  38. */
  39. void setCutoff(float sampleRate, float cutoffHz) {
  40. double omega_c = 2.0 * M_PI * cutoffHz / sampleRate;
  41. double y = 1.0 - cos(omega_c);
  42. kp = float(-y + sqrt((y + 2.0) * y));
  43. }
  44. float process(float input) {
  45. return value += kp * (input - value);
  46. }
  47. };
  48. // -----------------------------------------------------------------------------------------------------------
  49. /**
  50. Plugin that demonstrates tempo sync in DPF.
  51. The tempo sync implementation is on the first if branch in run() method.
  52. */
  53. class ExamplePluginMetronome : public Plugin
  54. {
  55. private:
  56. enum ParameterIndex : uint32_t {
  57. pGain,
  58. pDecayTime,
  59. pSemitone,
  60. pCent,
  61. N_PARAMETERS,
  62. };
  63. public:
  64. ExamplePluginMetronome()
  65. : Plugin(N_PARAMETERS, 0, 0), // 4 parameters, 0 programs, 0 states
  66. sampleRate(44100.0f),
  67. counter(0),
  68. phase(0.0f),
  69. decay(0.0f),
  70. gain(0.5f),
  71. semitone(72),
  72. cent(0),
  73. decayTime(0.2f)
  74. {
  75. sampleRateChanged(sampleRate);
  76. }
  77. protected:
  78. /* --------------------------------------------------------------------------------------------------------
  79. * Information */
  80. /**
  81. Get the plugin label.
  82. A plugin label follows the same rules as Parameter::symbol, with the exception that it can start with numbers.
  83. */
  84. const char* getLabel() const override
  85. {
  86. return "Metronome";
  87. }
  88. /**
  89. Get an extensive comment/description about the plugin.
  90. */
  91. const char* getDescription() const override
  92. {
  93. return "Simple metronome plugin which outputs impulse at the start of every beat.";
  94. }
  95. /**
  96. Get the plugin author/maker.
  97. */
  98. const char* getMaker() const override
  99. {
  100. return "DISTRHO";
  101. }
  102. /**
  103. Get the plugin homepage.
  104. */
  105. const char* getHomePage() const override
  106. {
  107. return "https://github.com/DISTRHO/DPF";
  108. }
  109. /**
  110. Get the plugin license name (a single line of text).
  111. For commercial plugins this should return some short copyright information.
  112. */
  113. const char* getLicense() const override
  114. {
  115. return "ISC";
  116. }
  117. /**
  118. Get the plugin version, in hexadecimal.
  119. */
  120. uint32_t getVersion() const override
  121. {
  122. return d_version(1, 0, 0);
  123. }
  124. /**
  125. Get the plugin unique Id.
  126. This value is used by LADSPA, DSSI and VST plugin formats.
  127. */
  128. int64_t getUniqueId() const override
  129. {
  130. return d_cconst('d', 'M', 'e', 't');
  131. }
  132. /* --------------------------------------------------------------------------------------------------------
  133. * Init */
  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. parameter.hints = kParameterIsAutomable;
  141. switch (index)
  142. {
  143. case pGain:
  144. parameter.name = "Gain";
  145. parameter.hints |= kParameterIsLogarithmic;
  146. parameter.ranges.min = 0.0f;
  147. parameter.ranges.max = 1.0f;
  148. parameter.ranges.def = 0.5f;
  149. break;
  150. case pDecayTime:
  151. parameter.name = "DecayTime";
  152. parameter.hints |= kParameterIsLogarithmic;
  153. parameter.ranges.min = 0.001f;
  154. parameter.ranges.max = 1.0f;
  155. parameter.ranges.def = 0.2f;
  156. break;
  157. case pSemitone:
  158. parameter.name = "Semitone";
  159. parameter.hints |= kParameterIsInteger;
  160. parameter.ranges.min = 0;
  161. parameter.ranges.max = 127;
  162. parameter.ranges.def = 72;
  163. break;
  164. case pCent:
  165. parameter.name = "Cent";
  166. parameter.hints |= kParameterIsInteger;
  167. parameter.ranges.min = -100;
  168. parameter.ranges.max = 100;
  169. parameter.ranges.def = 0;
  170. break;
  171. }
  172. parameter.symbol = parameter.name;
  173. }
  174. /* --------------------------------------------------------------------------------------------------------
  175. * Internal data */
  176. /**
  177. Get the current value of a parameter.
  178. */
  179. float getParameterValue(uint32_t index) const override
  180. {
  181. switch (index)
  182. {
  183. case pGain:
  184. return gain;
  185. case pDecayTime:
  186. return decayTime;
  187. case pSemitone:
  188. return semitone;
  189. case pCent:
  190. return cent;
  191. }
  192. return 0.0f;
  193. }
  194. /**
  195. Change a parameter value.
  196. */
  197. void setParameterValue(uint32_t index, float value) override
  198. {
  199. switch (index)
  200. {
  201. case pGain:
  202. gain = value;
  203. break;
  204. case pDecayTime:
  205. decayTime = value;
  206. break;
  207. case pSemitone:
  208. semitone = value;
  209. break;
  210. case pCent:
  211. cent = value;
  212. break;
  213. }
  214. }
  215. /* --------------------------------------------------------------------------------------------------------
  216. * Process */
  217. /**
  218. Run/process function for plugins without MIDI input.
  219. `inputs` is commented out because this plugin has no inputs.
  220. */
  221. void run(const float** /* inputs */, float** outputs, uint32_t frames) override
  222. {
  223. const TimePosition& timePos(getTimePosition());
  224. if (timePos.playing && timePos.bbt.valid) {
  225. // Better to use double when manipulating time.
  226. double secondsPerBeat = 60.0 / timePos.bbt.beatsPerMinute;
  227. double framesPerBeat = sampleRate * secondsPerBeat;
  228. double beatFraction = timePos.bbt.tick / timePos.bbt.ticksPerBeat;
  229. // If beatFraction == 0.0, next beat is exactly at the start of currenct cycle.
  230. // Otherwise, reset counter to the frames to the next beat.
  231. counter = beatFraction == 0.0
  232. ? 0
  233. : static_cast<uint32_t>(framesPerBeat * (1.0 - beatFraction));
  234. // Compute deltaPhase in normalized frequency.
  235. // semitone is midi note number, which is A4 (440Hz at standard tuning) at 69.
  236. // Frequency goes up to 1 octave higher at the start of bar.
  237. float frequency = 440.0f * powf(2.0f, (100.0f * (semitone - 69.0f) + cent) / 1200.0f);
  238. float deltaPhase = frequency / sampleRate;
  239. float octave = timePos.bbt.beat == 1 ? 2.0f : 1.0f;
  240. // Envelope reaches 1e-5 at decayTime after triggering.
  241. decay = pow(1e-5, 1.0 / (decayTime * sampleRate));
  242. // Reset phase and frequency at the start of transpose.
  243. if (!wasPlaying) {
  244. phase = 0.0f;
  245. deltaPhaseSmoother.value = deltaPhase;
  246. gainSmoother.value = 1.0f;
  247. envelopeSmoother.value = 0.0f;
  248. }
  249. for (uint32_t i = 0; i < frames; ++i) {
  250. if (counter <= 0) {
  251. envelope = 1.0f;
  252. counter = uint32_t(framesPerBeat);
  253. octave = !wasPlaying || timePos.bbt.beat == timePos.bbt.beatsPerBar ? 2.0f : 1.0f;
  254. }
  255. --counter;
  256. envelope *= decay;
  257. phase += octave * deltaPhaseSmoother.process(deltaPhase);
  258. phase -= floorf(phase);
  259. outputs[0][i] = gainSmoother.process(gain)
  260. * envelopeSmoother.process(envelope)
  261. * sinf(float(2.0 * M_PI) * phase);
  262. }
  263. } else {
  264. // Stop metronome if not playing or timePos.bbt is invalid.
  265. for (uint32_t i = 0; i < frames; ++i) outputs[0][i] = 0.0f;
  266. }
  267. wasPlaying = timePos.playing;
  268. }
  269. /* --------------------------------------------------------------------------------------------------------
  270. * Callbacks (optional) */
  271. /**
  272. Optional callback to inform the plugin about a sample rate change.
  273. This function will only be called when the plugin is deactivated.
  274. */
  275. void sampleRateChanged(double newSampleRate) override
  276. {
  277. sampleRate = newSampleRate;
  278. // Cutoff value was tuned manually.
  279. deltaPhaseSmoother.setCutoff(sampleRate, 100.0f);
  280. gainSmoother.setCutoff(sampleRate, 500.0f);
  281. envelopeSmoother.setCutoff(sampleRate, 250.0f);
  282. }
  283. // -------------------------------------------------------------------------------------------------------
  284. private:
  285. float sampleRate;
  286. uint32_t counter; // Stores number of frames to the next beat.
  287. bool wasPlaying; // Used to reset phase and frequency at the start of transpose.
  288. float phase; // Sine wave phase. Normalized in [0, 1).
  289. float envelope; // Current value of gain envelope.
  290. float decay; // Coefficient to decay envelope in a frame.
  291. Smoother deltaPhaseSmoother;
  292. Smoother gainSmoother;
  293. Smoother envelopeSmoother;
  294. // Parameters.
  295. float gain;
  296. float semitone;
  297. float cent;
  298. float decayTime;
  299. /**
  300. Set our plugin class as non-copyable and add a leak detector just in case.
  301. */
  302. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExamplePluginMetronome)
  303. };
  304. /* ------------------------------------------------------------------------------------------------------------
  305. * Plugin entry point, called by DPF to create a new plugin instance. */
  306. Plugin* createPlugin()
  307. {
  308. return new ExamplePluginMetronome();
  309. }
  310. // -----------------------------------------------------------------------------------------------------------
  311. END_NAMESPACE_DISTRHO