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.

333 lines
9.6KB

  1. /*
  2. * DISTRHO Kars Plugin, based on karplong by Chris Cannam.
  3. * Copyright (C) 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 "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. /**
  145. Process a batch of events untill no more are available.
  146. You must not read any more values from this class after this function returns false.
  147. */
  148. bool nextEvent()
  149. {
  150. if (remainingMidiEventCount == 0)
  151. {
  152. if (remainingFrames == 0)
  153. return false;
  154. for (uint32_t i=0; i<DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  155. outputs[i] += frames;
  156. if (midiEventCount != 0)
  157. midiEvents += midiEventCount;
  158. frames = remainingFrames;
  159. midiEvents = nullptr;
  160. midiEventCount = 0;
  161. remainingFrames = 0;
  162. return true;
  163. }
  164. const uint32_t eventFrame = midiEvents[0].frame;
  165. DISTRHO_SAFE_ASSERT_RETURN(eventFrame < remainingFrames, false);
  166. midiEventCount = 1;
  167. for (uint32_t i=1; i<remainingMidiEventCount; ++i)
  168. {
  169. if (midiEvents[i].frame != eventFrame)
  170. {
  171. midiEventCount = i;
  172. break;
  173. }
  174. }
  175. frames = remainingFrames - eventFrame;
  176. remainingFrames -= frames;
  177. remainingMidiEventCount -= midiEventCount;
  178. return true;
  179. }
  180. private:
  181. /** @internal */
  182. uint32_t remainingFrames;
  183. uint32_t remainingMidiEventCount;
  184. };
  185. void DistrhoPluginKars::run(const float**, float** outputs, uint32_t frames, const MidiEvent* midiEvents, uint32_t midiEventCount)
  186. {
  187. uint8_t note, velo;
  188. std::memset(outputs[0], 0, sizeof(float)*frames);
  189. for (AudioMidiSyncHelper amsh(outputs, frames, midiEvents, midiEventCount); amsh.nextEvent();)
  190. {
  191. for (uint32_t i=0; i<amsh.midiEventCount; ++i)
  192. {
  193. if (amsh.midiEvents[i].size > MidiEvent::kDataSize)
  194. continue;
  195. const uint8_t* data = amsh.midiEvents[i].data;
  196. const uint8_t status = data[0] & 0xF0;
  197. switch (status)
  198. {
  199. case 0x90:
  200. note = data[1];
  201. velo = data[2];
  202. DISTRHO_SAFE_ASSERT_BREAK(note < 128); // kMaxNotes
  203. if (velo > 0)
  204. {
  205. fNotes[note].on = fBlockStart + amsh.midiEvents[i].frame;
  206. fNotes[note].off = kNoteNull;
  207. fNotes[note].velocity = velo;
  208. break;
  209. }
  210. // fall through
  211. case 0x80:
  212. note = data[1];
  213. DISTRHO_SAFE_ASSERT_BREAK(note < 128); // kMaxNotes
  214. fNotes[note].off = fBlockStart + amsh.midiEvents[i].frame;
  215. break;
  216. }
  217. }
  218. float* const out = amsh.outputs[0];
  219. for (int i=kMaxNotes; --i >= 0;)
  220. {
  221. if (fNotes[i].on != kNoteNull)
  222. addSamples(out, i, amsh.frames);
  223. }
  224. }
  225. fBlockStart += frames;
  226. }
  227. void DistrhoPluginKars::addSamples(float* out, int voice, uint32_t frames)
  228. {
  229. const uint32_t start = fBlockStart;
  230. Note& note(fNotes[voice]);
  231. if (start < note.on)
  232. return;
  233. if (start == note.on)
  234. {
  235. for (int i=note.sizei; --i >= 0;)
  236. note.wavetable[i] = (float(rand()) / float(RAND_MAX)) * 2.0f - 1.0f;
  237. }
  238. const float vgain = float(note.velocity) / 127.0f;
  239. bool decay;
  240. float gain, sample;
  241. uint32_t index, size;
  242. for (uint32_t i=0, s=start-note.on; i<frames; ++i, ++s)
  243. {
  244. gain = vgain;
  245. if ((! fSustain) && note.off != kNoteNull && note.off < i+start)
  246. {
  247. // reuse index and size to save some performance.
  248. // actual values are release and dist
  249. index = 1 + uint32_t(fRelease * fSampleRate); // release, not index
  250. size = i + start - note.off; // dist, not size
  251. if (size > index)
  252. {
  253. note.on = kNoteNull;
  254. break;
  255. }
  256. gain = gain * float(index - size) / float(index);
  257. }
  258. size = uint32_t(note.sizei);
  259. decay = s > size;
  260. index = s % size;
  261. sample = note.wavetable[index];
  262. if (decay)
  263. {
  264. if (index == 0)
  265. sample += note.wavetable[size-1];
  266. else
  267. sample += note.wavetable[index-1];
  268. note.wavetable[index] = sample/2;
  269. }
  270. out[i] += gain * sample * (fVolume / 100.0f);
  271. }
  272. }
  273. // -----------------------------------------------------------------------
  274. Plugin* createPlugin()
  275. {
  276. return new DistrhoPluginKars();
  277. }
  278. // -----------------------------------------------------------------------
  279. END_NAMESPACE_DISTRHO