Collection of DPF-based plugins for packaging
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.

370 lines
11KB

  1. /*
  2. * DISTRHO Kars Plugin, based on karplong by Chris Cannam.
  3. * Copyright (C) 2015-2019 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 "DistrhoPluginKars.hpp"
  17. START_NAMESPACE_DISTRHO
  18. // -----------------------------------------------------------------------
  19. DistrhoPluginKars::DistrhoPluginKars()
  20. : Plugin(paramCount, 0, 0), // 0 programs, 0 states
  21. fSustain(false),
  22. fRelease(0.01),
  23. fVolume(75.0f),
  24. fSampleRate(getSampleRate()),
  25. fBlockStart(0)
  26. {
  27. for (int i=kMaxNotes; --i >= 0;)
  28. {
  29. fNotes[i].voice = i;
  30. fNotes[i].setSampleRate(fSampleRate);
  31. }
  32. }
  33. // -----------------------------------------------------------------------
  34. // Init
  35. void DistrhoPluginKars::initParameter(uint32_t index, Parameter& parameter)
  36. {
  37. switch (index)
  38. {
  39. case paramSustain:
  40. parameter.hints = kParameterIsAutomable|kParameterIsBoolean;
  41. parameter.name = "Sustain";
  42. parameter.symbol = "sustain";
  43. parameter.ranges.def = 0.0f;
  44. parameter.ranges.min = 0.0f;
  45. parameter.ranges.max = 1.0f;
  46. break;
  47. case paramRelease:
  48. parameter.hints = kParameterIsAutomable;
  49. parameter.name = "Release";
  50. parameter.symbol = "release";
  51. parameter.unit = "s";
  52. parameter.ranges.def = 0.01f;
  53. parameter.ranges.min = 0.0f;
  54. parameter.ranges.max = 5.0f;
  55. break;
  56. case paramVolume:
  57. parameter.hints = kParameterIsAutomable;
  58. parameter.name = "Volume";
  59. parameter.symbol = "volume";
  60. parameter.unit = "%";
  61. parameter.ranges.def = 75.0f;
  62. parameter.ranges.min = 0.0f;
  63. parameter.ranges.max = 100.0f;
  64. break;
  65. }
  66. }
  67. // -----------------------------------------------------------------------
  68. // Internal data
  69. float DistrhoPluginKars::getParameterValue(uint32_t index) const
  70. {
  71. switch (index)
  72. {
  73. case paramSustain: return fSustain ? 1.0f : 0.0f;
  74. case paramRelease: return fRelease;
  75. case paramVolume: return fVolume;
  76. }
  77. return 0.0f;
  78. }
  79. void DistrhoPluginKars::setParameterValue(uint32_t index, float value)
  80. {
  81. switch (index)
  82. {
  83. case paramSustain:
  84. fSustain = value > 0.5f;
  85. break;
  86. case paramRelease:
  87. fRelease = value;
  88. break;
  89. case paramVolume:
  90. fVolume = value;
  91. break;
  92. }
  93. }
  94. // -----------------------------------------------------------------------
  95. // Process
  96. void DistrhoPluginKars::activate()
  97. {
  98. fBlockStart = 0;
  99. for (int i=kMaxNotes; --i >= 0;)
  100. {
  101. fNotes[i].on = kNoteNull;
  102. fNotes[i].off = kNoteNull;
  103. fNotes[i].velocity = 0;
  104. }
  105. }
  106. /**
  107. Handy class to help keep audio buffer in sync with incoming MIDI events.
  108. To use it, create a local variable (on the stack) and call nextEvent() until it returns false.
  109. @code
  110. for (AudioMidiSyncHelper amsh(outputs, frames, midiEvents, midiEventCount); amsh.nextEvent();)
  111. {
  112. float* const outL = amsh.outputs[0];
  113. float* const outR = amsh.outputs[1];
  114. for (uint32_t i=0; i<amsh.midiEventCount; ++i)
  115. {
  116. const MidiEvent& ev(amsh.midiEvents[i]);
  117. // ... do something with the midi event
  118. }
  119. renderSynth(outL, outR, amsh.frames);
  120. }
  121. @endcode
  122. Some important notes when using this class:
  123. 1. MidiEvent::frame retains its original value, but it is useless, do not use it.
  124. 2. The class variables names are be the same as the default ones in the run function.
  125. Keep that in mind and try to avoid typos. :)
  126. */
  127. class AudioMidiSyncHelper {
  128. public:
  129. /** Parameters from the run function, adjusted for event sync */
  130. float** outputs;
  131. uint32_t frames;
  132. const MidiEvent* midiEvents;
  133. uint32_t midiEventCount;
  134. /**
  135. Constructor, using values from the run function.
  136. */
  137. AudioMidiSyncHelper(float** const o, uint32_t f, const MidiEvent* m, uint32_t mc)
  138. : outputs(o),
  139. frames(0),
  140. midiEvents(m),
  141. midiEventCount(0),
  142. remainingFrames(f),
  143. remainingMidiEventCount(mc),
  144. totalFramesUsed(0) {}
  145. /**
  146. Process a batch of events untill no more are available.
  147. You must not read any more values from this class after this function returns false.
  148. */
  149. bool nextEvent()
  150. {
  151. // nothing else to do
  152. if (remainingFrames == 0)
  153. return false;
  154. // initial setup, need to find first MIDI event
  155. if (totalFramesUsed == 0)
  156. {
  157. // no MIDI events at all in this process cycle
  158. if (remainingMidiEventCount == 0)
  159. {
  160. frames = remainingFrames;
  161. remainingFrames = 0;
  162. totalFramesUsed += frames;
  163. return true;
  164. }
  165. // render audio until first midi event, if needed
  166. if (const uint32_t firstEventFrame = midiEvents[0].frame)
  167. {
  168. frames = midiEvents[0].frame;
  169. remainingFrames -= frames;
  170. totalFramesUsed += frames;
  171. return true;
  172. }
  173. }
  174. else
  175. {
  176. for (uint32_t i=0; i<DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  177. outputs[i] += frames;
  178. }
  179. // no more MIDI events available
  180. if (remainingMidiEventCount == 0)
  181. {
  182. frames = remainingFrames;
  183. midiEvents = nullptr;
  184. midiEventCount = 0;
  185. remainingFrames = 0;
  186. totalFramesUsed += frames;
  187. return true;
  188. }
  189. // if there were midi events before, increment pointer
  190. if (midiEventCount != 0)
  191. midiEvents += midiEventCount;
  192. const uint32_t firstEventFrame = midiEvents[0].frame;
  193. DISTRHO_SAFE_ASSERT_RETURN((firstEventFrame - frames) < remainingFrames, false);
  194. midiEventCount = 1;
  195. while (midiEventCount < remainingMidiEventCount)
  196. {
  197. if (midiEvents[midiEventCount].frame == firstEventFrame)
  198. ++midiEventCount;
  199. else
  200. break;
  201. }
  202. if (totalFramesUsed != 0)
  203. {
  204. // need to modify timestamp of midi events
  205. MidiEvent* const rwEvents = const_cast<MidiEvent*>(midiEvents);
  206. for (uint32_t i=0; i < midiEventCount; ++i)
  207. rwEvents[i].frame -= totalFramesUsed;
  208. }
  209. frames = remainingFrames - firstEventFrame;
  210. remainingFrames -= frames;
  211. remainingMidiEventCount -= midiEventCount;
  212. totalFramesUsed += frames;
  213. return true;
  214. }
  215. private:
  216. /** @internal */
  217. uint32_t remainingFrames;
  218. uint32_t remainingMidiEventCount;
  219. uint32_t totalFramesUsed;
  220. };
  221. void DistrhoPluginKars::run(const float**, float** outputs, uint32_t frames, const MidiEvent* midiEvents, uint32_t midiEventCount)
  222. {
  223. uint8_t note, velo;
  224. std::memset(outputs[0], 0, sizeof(float)*frames);
  225. for (AudioMidiSyncHelper amsh(outputs, frames, midiEvents, midiEventCount); amsh.nextEvent();)
  226. {
  227. for (uint32_t i=0; i<amsh.midiEventCount; ++i)
  228. {
  229. if (amsh.midiEvents[i].size > MidiEvent::kDataSize)
  230. continue;
  231. const uint8_t* data = amsh.midiEvents[i].data;
  232. const uint8_t status = data[0] & 0xF0;
  233. switch (status)
  234. {
  235. case 0x90:
  236. note = data[1];
  237. velo = data[2];
  238. DISTRHO_SAFE_ASSERT_BREAK(note < 128); // kMaxNotes
  239. if (velo > 0)
  240. {
  241. fNotes[note].on = fBlockStart + amsh.midiEvents[i].frame;
  242. fNotes[note].off = kNoteNull;
  243. fNotes[note].velocity = velo;
  244. break;
  245. }
  246. // fall through
  247. case 0x80:
  248. note = data[1];
  249. DISTRHO_SAFE_ASSERT_BREAK(note < 128); // kMaxNotes
  250. fNotes[note].off = fBlockStart + amsh.midiEvents[i].frame;
  251. break;
  252. }
  253. }
  254. float* const out = amsh.outputs[0];
  255. for (int i=kMaxNotes; --i >= 0;)
  256. {
  257. if (fNotes[i].on != kNoteNull)
  258. addSamples(out, i, amsh.frames);
  259. }
  260. fBlockStart += amsh.frames;
  261. }
  262. }
  263. void DistrhoPluginKars::addSamples(float* out, int voice, uint32_t frames)
  264. {
  265. const uint32_t start = fBlockStart;
  266. Note& note(fNotes[voice]);
  267. if (start < note.on)
  268. return;
  269. if (start == note.on)
  270. {
  271. for (int i=note.sizei; --i >= 0;)
  272. note.wavetable[i] = (float(rand()) / float(RAND_MAX)) * 2.0f - 1.0f;
  273. }
  274. const float vgain = float(note.velocity) / 127.0f;
  275. bool decay;
  276. float gain, sample;
  277. uint32_t index, size;
  278. for (uint32_t i=0, s=start-note.on; i<frames; ++i, ++s)
  279. {
  280. gain = vgain;
  281. if ((! fSustain) && note.off != kNoteNull && note.off < i+start)
  282. {
  283. // reuse index and size to save some performance.
  284. // actual values are release and dist
  285. index = 1 + uint32_t(fRelease * fSampleRate); // release, not index
  286. size = i + start - note.off; // dist, not size
  287. if (size > index)
  288. {
  289. note.on = kNoteNull;
  290. break;
  291. }
  292. gain = gain * float(index - size) / float(index);
  293. }
  294. size = uint32_t(note.sizei);
  295. decay = s > size;
  296. index = s % size;
  297. sample = note.wavetable[index];
  298. if (decay)
  299. {
  300. if (index == 0)
  301. sample += note.wavetable[size-1];
  302. else
  303. sample += note.wavetable[index-1];
  304. note.wavetable[index] = sample/2;
  305. }
  306. out[i] += gain * sample * (fVolume / 100.0f);
  307. }
  308. }
  309. // -----------------------------------------------------------------------
  310. Plugin* createPlugin()
  311. {
  312. return new DistrhoPluginKars();
  313. }
  314. // -----------------------------------------------------------------------
  315. END_NAMESPACE_DISTRHO