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.

394 lines
12KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
  4. * Copyright (C) 2020 Takamitsu Endo
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  7. * or without fee is hereby granted, provided that the above copyright notice and this
  8. * permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  11. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  12. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  15. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "DistrhoPlugin.hpp"
  18. START_NAMESPACE_DISTRHO
  19. // -----------------------------------------------------------------------------------------------------------
  20. /**
  21. 1-pole lowpass filter to smooth out parameters and envelopes.
  22. This filter is guaranteed not to overshoot.
  23. */
  24. class Smoother {
  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?
  38. https://dsp.stackexchange.com/questions/54086/single-pole-iir-low-pass-filter-which-is-the-correct-formula-for-the-decay-coe
  39. */
  40. void setCutoff(const float sampleRate, const float cutoffHz)
  41. {
  42. double omega_c = 2.0 * M_PI * cutoffHz / sampleRate;
  43. double y = 1.0 - std::cos(omega_c);
  44. kp = float(-y + std::sqrt((y + 2.0) * y));
  45. }
  46. inline float process(const float input)
  47. {
  48. return value += kp * (input - value);
  49. }
  50. };
  51. // -----------------------------------------------------------------------------------------------------------
  52. /**
  53. Plugin that demonstrates tempo sync in DPF.
  54. The tempo sync implementation is on the first if branch in run() method.
  55. */
  56. class ExamplePluginMetronome : public Plugin
  57. {
  58. public:
  59. ExamplePluginMetronome()
  60. : Plugin(4, 0, 0), // 4 parameters, 0 programs, 0 states
  61. sampleRate(getSampleRate()),
  62. counter(0),
  63. wasPlaying(false),
  64. phase(0.0f),
  65. envelope(1.0f),
  66. decay(0.0f),
  67. gain(0.5f),
  68. semitone(72),
  69. cent(0),
  70. decayTime(0.2f)
  71. {
  72. sampleRateChanged(sampleRate);
  73. }
  74. protected:
  75. /* --------------------------------------------------------------------------------------------------------
  76. * Information */
  77. /**
  78. Get the plugin label.
  79. A plugin label follows the same rules as Parameter::symbol, with the exception that it can start with numbers.
  80. */
  81. const char* getLabel() const override
  82. {
  83. return "Metronome";
  84. }
  85. /**
  86. Get an extensive comment/description about the plugin.
  87. */
  88. const char* getDescription() const override
  89. {
  90. return "Simple metronome plugin which outputs impulse at the start of every beat.";
  91. }
  92. /**
  93. Get the plugin author/maker.
  94. */
  95. const char* getMaker() const override
  96. {
  97. return "DISTRHO";
  98. }
  99. /**
  100. Get the plugin homepage.
  101. */
  102. const char* getHomePage() const override
  103. {
  104. return "https://github.com/DISTRHO/DPF";
  105. }
  106. /**
  107. Get the plugin license name (a single line of text).
  108. For commercial plugins this should return some short copyright information.
  109. */
  110. const char* getLicense() const override
  111. {
  112. return "ISC";
  113. }
  114. /**
  115. Get the plugin version, in hexadecimal.
  116. */
  117. uint32_t getVersion() const override
  118. {
  119. return d_version(1, 0, 0);
  120. }
  121. /**
  122. Get the plugin unique Id.
  123. This value is used by LADSPA, DSSI and VST plugin formats.
  124. */
  125. int64_t getUniqueId() const override
  126. {
  127. return d_cconst('d', 'M', 'e', 't');
  128. }
  129. /* --------------------------------------------------------------------------------------------------------
  130. * Init */
  131. /**
  132. Initialize the audio port @a index.@n
  133. This function will be called once, shortly after the plugin is created.
  134. */
  135. void initAudioPort(bool input, uint32_t index, AudioPort& port) override
  136. {
  137. // treat meter audio ports as stereo
  138. port.groupId = kPortGroupMono;
  139. // everything else is as default
  140. Plugin::initAudioPort(input, index, port);
  141. }
  142. /**
  143. Initialize the parameter @a index.
  144. This function will be called once, shortly after the plugin is created.
  145. */
  146. void initParameter(uint32_t index, Parameter& parameter) override
  147. {
  148. parameter.hints = kParameterIsAutomatable;
  149. switch (index)
  150. {
  151. case 0:
  152. parameter.name = "Gain";
  153. parameter.hints |= kParameterIsLogarithmic;
  154. parameter.ranges.min = 0.001f;
  155. parameter.ranges.max = 1.0f;
  156. parameter.ranges.def = 0.5f;
  157. break;
  158. case 1:
  159. parameter.name = "DecayTime";
  160. parameter.hints |= kParameterIsLogarithmic;
  161. parameter.ranges.min = 0.001f;
  162. parameter.ranges.max = 1.0f;
  163. parameter.ranges.def = 0.2f;
  164. break;
  165. case 2:
  166. parameter.name = "Semitone";
  167. parameter.hints |= kParameterIsInteger;
  168. parameter.ranges.min = 0;
  169. parameter.ranges.max = 127;
  170. parameter.ranges.def = 72;
  171. break;
  172. case 3:
  173. parameter.name = "Cent";
  174. parameter.hints |= kParameterIsInteger;
  175. parameter.ranges.min = -100;
  176. parameter.ranges.max = 100;
  177. parameter.ranges.def = 0;
  178. break;
  179. }
  180. parameter.symbol = parameter.name;
  181. }
  182. /* --------------------------------------------------------------------------------------------------------
  183. * Internal data */
  184. /**
  185. Get the current value of a parameter.
  186. */
  187. float getParameterValue(uint32_t index) const override
  188. {
  189. switch (index)
  190. {
  191. case 0:
  192. return gain;
  193. case 1:
  194. return decayTime;
  195. case 2:
  196. return semitone;
  197. case 3:
  198. return cent;
  199. }
  200. return 0.0f;
  201. }
  202. /**
  203. Change a parameter value.
  204. */
  205. void setParameterValue(uint32_t index, float value) override
  206. {
  207. switch (index)
  208. {
  209. case 0:
  210. gain = value;
  211. break;
  212. case 1:
  213. decayTime = value;
  214. break;
  215. case 2:
  216. semitone = value;
  217. break;
  218. case 3:
  219. cent = value;
  220. break;
  221. }
  222. }
  223. /* --------------------------------------------------------------------------------------------------------
  224. * Process */
  225. /**
  226. Activate this plugin.
  227. We use this to reset our filter states.
  228. */
  229. void activate() override
  230. {
  231. deltaPhaseSmoother.value = 0.0f;
  232. envelopeSmoother.value = 0.0f;
  233. gainSmoother.value = gain;
  234. }
  235. /**
  236. Run/process function for plugins without MIDI input.
  237. `inputs` is commented out because this plugin has no inputs.
  238. */
  239. void run(const float** /* inputs */, float** outputs, uint32_t frames) override
  240. {
  241. const TimePosition& timePos(getTimePosition());
  242. float* const output = outputs[0];
  243. if (timePos.playing && timePos.bbt.valid)
  244. {
  245. // Better to use double when manipulating time.
  246. double secondsPerBeat = 60.0 / timePos.bbt.beatsPerMinute;
  247. double framesPerBeat = sampleRate * secondsPerBeat;
  248. double beatFraction = timePos.bbt.tick / timePos.bbt.ticksPerBeat;
  249. // If beatFraction is zero, next beat is exactly at the start of currenct cycle.
  250. // Otherwise, reset counter to the frames to the next beat.
  251. counter = d_isZero(beatFraction)
  252. ? 0
  253. : static_cast<uint32_t>(framesPerBeat * (1.0 - beatFraction));
  254. // Compute deltaPhase in normalized frequency.
  255. // semitone is midi note number, which is A4 (440Hz at standard tuning) at 69.
  256. // Frequency goes up to 1 octave higher at the start of bar.
  257. float frequency = 440.0f * std::pow(2.0f, (100.0f * (semitone - 69.0f) + cent) / 1200.0f);
  258. float deltaPhase = frequency / sampleRate;
  259. float octave = timePos.bbt.beat == 1 ? 2.0f : 1.0f;
  260. // Envelope reaches 1e-5 at decayTime after triggering.
  261. decay = std::pow(1e-5, 1.0 / (decayTime * sampleRate));
  262. // Reset phase and frequency at the start of transpose.
  263. if (!wasPlaying)
  264. {
  265. phase = 0.0f;
  266. deltaPhaseSmoother.value = deltaPhase;
  267. envelopeSmoother.value = 0.0f;
  268. gainSmoother.value = 0.0f;
  269. }
  270. for (uint32_t i = 0; i < frames; ++i)
  271. {
  272. if (counter <= 0)
  273. {
  274. envelope = 1.0f;
  275. counter = static_cast<uint32_t>(framesPerBeat + 0.5);
  276. octave = (!wasPlaying || timePos.bbt.beat == static_cast<int32_t>(timePos.bbt.beatsPerBar)) ? 2.0f
  277. : 1.0f;
  278. }
  279. --counter;
  280. envelope *= decay;
  281. phase += octave * deltaPhaseSmoother.process(deltaPhase);
  282. phase -= std::floor(phase);
  283. output[i] = gainSmoother.process(gain)
  284. * envelopeSmoother.process(envelope)
  285. * std::sin(float(2.0 * M_PI) * phase);
  286. }
  287. }
  288. else
  289. {
  290. // Stop metronome if not playing or timePos.bbt is invalid.
  291. std::memset(output, 0, sizeof(float)*frames);
  292. }
  293. wasPlaying = timePos.playing;
  294. }
  295. /* --------------------------------------------------------------------------------------------------------
  296. * Callbacks (optional) */
  297. /**
  298. Optional callback to inform the plugin about a sample rate change.
  299. This function will only be called when the plugin is deactivated.
  300. */
  301. void sampleRateChanged(double newSampleRate) override
  302. {
  303. sampleRate = newSampleRate;
  304. // Cutoff value was tuned manually.
  305. deltaPhaseSmoother.setCutoff(sampleRate, 100.0f);
  306. gainSmoother.setCutoff(sampleRate, 500.0f);
  307. envelopeSmoother.setCutoff(sampleRate, 250.0f);
  308. }
  309. // -------------------------------------------------------------------------------------------------------
  310. private:
  311. float sampleRate;
  312. uint32_t counter; // Stores number of frames to the next beat.
  313. bool wasPlaying; // Used to reset phase and frequency at the start of transpose.
  314. float phase; // Sine wave phase. Normalized in [0, 1).
  315. float envelope; // Current value of gain envelope.
  316. float decay; // Coefficient to decay envelope in a frame.
  317. Smoother deltaPhaseSmoother;
  318. Smoother envelopeSmoother;
  319. Smoother gainSmoother;
  320. // Parameters.
  321. float gain;
  322. float semitone;
  323. float cent;
  324. float decayTime;
  325. /**
  326. Set our plugin class as non-copyable and add a leak detector just in case.
  327. */
  328. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExamplePluginMetronome)
  329. };
  330. /* ------------------------------------------------------------------------------------------------------------
  331. * Plugin entry point, called by DPF to create a new plugin instance. */
  332. Plugin* createPlugin()
  333. {
  334. return new ExamplePluginMetronome();
  335. }
  336. // -----------------------------------------------------------------------------------------------------------
  337. END_NAMESPACE_DISTRHO