Audio plugin host https://kx.studio/carla
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.

DistrhoPluginNekobi.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * DISTRHO Nekobi Plugin, based on Nekobee by Sean Bolton and others.
  3. * Copyright (C) 2004 Sean Bolton and others
  4. * Copyright (C) 2013-2015 Filipe Coelho <falktx@falktx.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * For a full copy of the GNU General Public License see the LICENSE file.
  17. */
  18. #include "DistrhoPluginNekobi.hpp"
  19. extern "C" {
  20. #include "nekobee-src/nekobee_synth.c"
  21. #include "nekobee-src/nekobee_voice.c"
  22. #include "nekobee-src/nekobee_voice_render.c"
  23. #include "nekobee-src/minblep_tables.c"
  24. // -----------------------------------------------------------------------
  25. // mutual exclusion
  26. bool dssp_voicelist_mutex_trylock(nekobee_synth_t* const synth)
  27. {
  28. /* Attempt the mutex lock */
  29. if (pthread_mutex_trylock(&synth->voicelist_mutex) != 0)
  30. {
  31. synth->voicelist_mutex_grab_failed = 1;
  32. return false;
  33. }
  34. /* Clean up if a previous mutex grab failed */
  35. if (synth->voicelist_mutex_grab_failed)
  36. {
  37. nekobee_synth_all_voices_off(synth);
  38. synth->voicelist_mutex_grab_failed = 0;
  39. }
  40. return true;
  41. }
  42. bool dssp_voicelist_mutex_unlock(nekobee_synth_t* const synth)
  43. {
  44. return (pthread_mutex_unlock(&synth->voicelist_mutex) == 0);
  45. }
  46. // -----------------------------------------------------------------------
  47. // nekobee_handle_raw_event
  48. void nekobee_handle_raw_event(nekobee_synth_t* const synth, const uint8_t size, const uint8_t* const data)
  49. {
  50. if (size != 3)
  51. return;
  52. switch (data[0] & 0xf0)
  53. {
  54. case 0x80:
  55. nekobee_synth_note_off(synth, data[1], data[2]);
  56. break;
  57. case 0x90:
  58. if (data[2] > 0)
  59. nekobee_synth_note_on(synth, data[1], data[2]);
  60. else
  61. nekobee_synth_note_off(synth, data[1], 64); /* shouldn't happen, but... */
  62. break;
  63. case 0xB0:
  64. nekobee_synth_control_change(synth, data[1], data[2]);
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. } /* extern "C" */
  71. START_NAMESPACE_DISTRHO
  72. // -----------------------------------------------------------------------
  73. DistrhoPluginNekobi::DistrhoPluginNekobi()
  74. : Plugin(paramCount, 0, 0) // 0 programs, 0 states
  75. {
  76. nekobee_init_tables();
  77. // init synth
  78. fSynth.sample_rate = getSampleRate();
  79. fSynth.deltat = 1.0f / (float)getSampleRate();
  80. fSynth.nugget_remains = 0;
  81. fSynth.note_id = 0;
  82. fSynth.polyphony = XSYNTH_DEFAULT_POLYPHONY;
  83. fSynth.voices = XSYNTH_DEFAULT_POLYPHONY;
  84. fSynth.monophonic = XSYNTH_MONO_MODE_ONCE;
  85. fSynth.glide = 0;
  86. fSynth.last_noteon_pitch = 0.0f;
  87. fSynth.vcf_accent = 0.0f;
  88. fSynth.vca_accent = 0.0f;
  89. for (int i=0; i<8; ++i)
  90. fSynth.held_keys[i] = -1;
  91. fSynth.voice = nekobee_voice_new();
  92. fSynth.voicelist_mutex_grab_failed = 0;
  93. pthread_mutex_init(&fSynth.voicelist_mutex, nullptr);
  94. fSynth.channel_pressure = 0;
  95. fSynth.pitch_wheel_sensitivity = 0;
  96. fSynth.pitch_wheel = 0;
  97. for (int i=0; i<128; ++i)
  98. {
  99. fSynth.key_pressure[i] = 0;
  100. fSynth.cc[i] = 0;
  101. }
  102. fSynth.cc[7] = 127; // full volume
  103. fSynth.mod_wheel = 1.0f;
  104. fSynth.pitch_bend = 1.0f;
  105. fSynth.cc_volume = 1.0f;
  106. // Default values
  107. fParams.waveform = 0.0f;
  108. fParams.tuning = 0.0f;
  109. fParams.cutoff = 25.0f;
  110. fParams.resonance = 25.0f;
  111. fParams.envMod = 50.0f;
  112. fParams.decay = 75.0f;
  113. fParams.accent = 25.0f;
  114. fParams.volume = 75.0f;
  115. // Internal stuff
  116. fSynth.waveform = 0.0f;
  117. fSynth.tuning = 1.0f;
  118. fSynth.cutoff = 5.0f;
  119. fSynth.resonance = 0.8f;
  120. fSynth.envmod = 0.3f;
  121. fSynth.decay = 0.0002f;
  122. fSynth.accent = 0.3f;
  123. fSynth.volume = 0.75f;
  124. // reset
  125. deactivate();
  126. }
  127. DistrhoPluginNekobi::~DistrhoPluginNekobi()
  128. {
  129. std::free(fSynth.voice);
  130. }
  131. // -----------------------------------------------------------------------
  132. // Init
  133. void DistrhoPluginNekobi::initParameter(uint32_t index, Parameter& parameter)
  134. {
  135. switch (index)
  136. {
  137. case paramWaveform:
  138. parameter.hints = kParameterIsAutomable|kParameterIsBoolean;
  139. parameter.name = "Waveform";
  140. parameter.symbol = "waveform";
  141. parameter.ranges.def = 0.0f;
  142. parameter.ranges.min = 0.0f;
  143. parameter.ranges.max = 1.0f;
  144. break;
  145. case paramTuning:
  146. parameter.hints = kParameterIsAutomable; // was 0.5 <-> 2.0, log
  147. parameter.name = "Tuning";
  148. parameter.symbol = "tuning";
  149. parameter.ranges.def = 0.0f;
  150. parameter.ranges.min = -12.0f;
  151. parameter.ranges.max = 12.0f;
  152. break;
  153. case paramCutoff:
  154. parameter.hints = kParameterIsAutomable; // modified x2.5
  155. parameter.name = "Cutoff";
  156. parameter.symbol = "cutoff";
  157. parameter.unit = "%";
  158. parameter.ranges.def = 25.0f;
  159. parameter.ranges.min = 0.0f;
  160. parameter.ranges.max = 100.0f;
  161. break;
  162. case paramResonance:
  163. parameter.hints = kParameterIsAutomable; // modified x100
  164. parameter.name = "VCF Resonance";
  165. parameter.symbol = "resonance";
  166. parameter.unit = "%";
  167. parameter.ranges.def = 25.0f;
  168. parameter.ranges.min = 0.0f;
  169. parameter.ranges.max = 95.0f;
  170. break;
  171. case paramEnvMod:
  172. parameter.hints = kParameterIsAutomable; // modified x100
  173. parameter.name = "Env Mod";
  174. parameter.symbol = "env_mod";
  175. parameter.unit = "%";
  176. parameter.ranges.def = 50.0f;
  177. parameter.ranges.min = 0.0f;
  178. parameter.ranges.max = 100.0f;
  179. break;
  180. case paramDecay:
  181. parameter.hints = kParameterIsAutomable; // was 0.000009 <-> 0.0005, log
  182. parameter.name = "Decay";
  183. parameter.symbol = "decay";
  184. parameter.unit = "%";
  185. parameter.ranges.def = 75.0f;
  186. parameter.ranges.min = 0.0f;
  187. parameter.ranges.max = 100.0f;
  188. break;
  189. case paramAccent:
  190. parameter.hints = kParameterIsAutomable; // modified x100
  191. parameter.name = "Accent";
  192. parameter.symbol = "accent";
  193. parameter.unit = "%";
  194. parameter.ranges.def = 25.0f;
  195. parameter.ranges.min = 0.0f;
  196. parameter.ranges.max = 100.0f;
  197. break;
  198. case paramVolume:
  199. parameter.hints = kParameterIsAutomable; // modified x100
  200. parameter.name = "Volume";
  201. parameter.symbol = "volume";
  202. parameter.unit = "%";
  203. parameter.ranges.def = 75.0f;
  204. parameter.ranges.min = 0.0f;
  205. parameter.ranges.max = 100.0f;
  206. break;
  207. }
  208. }
  209. // -----------------------------------------------------------------------
  210. // Internal data
  211. float DistrhoPluginNekobi::getParameterValue(uint32_t index) const
  212. {
  213. switch (index)
  214. {
  215. case paramWaveform:
  216. return fParams.waveform;
  217. case paramTuning:
  218. return fParams.tuning;
  219. case paramCutoff:
  220. return fParams.cutoff;
  221. case paramResonance:
  222. return fParams.resonance;
  223. case paramEnvMod:
  224. return fParams.envMod;
  225. case paramDecay:
  226. return fParams.decay;
  227. case paramAccent:
  228. return fParams.accent;
  229. case paramVolume:
  230. return fParams.volume;
  231. }
  232. return 0.0f;
  233. }
  234. void DistrhoPluginNekobi::setParameterValue(uint32_t index, float value)
  235. {
  236. switch (index)
  237. {
  238. case paramWaveform:
  239. fParams.waveform = value;
  240. fSynth.waveform = value;
  241. DISTRHO_SAFE_ASSERT(fSynth.waveform == 0.0f || fSynth.waveform == 1.0f);
  242. break;
  243. case paramTuning:
  244. fParams.tuning = value;
  245. fSynth.tuning = (value+12.0f)/24.0f * 1.5 + 0.5f; // FIXME: log?
  246. DISTRHO_SAFE_ASSERT(fSynth.tuning >= 0.5f && fSynth.tuning <= 2.0f);
  247. break;
  248. case paramCutoff:
  249. fParams.cutoff = value;
  250. fSynth.cutoff = value/2.5f;
  251. DISTRHO_SAFE_ASSERT(fSynth.cutoff >= 0.0f && fSynth.cutoff <= 40.0f);
  252. break;
  253. case paramResonance:
  254. fParams.resonance = value;
  255. fSynth.resonance = value/100.0f;
  256. DISTRHO_SAFE_ASSERT(fSynth.resonance >= 0.0f && fSynth.resonance <= 0.95f);
  257. break;
  258. case paramEnvMod:
  259. fParams.envMod = value;
  260. fSynth.envmod = value/100.0f;
  261. DISTRHO_SAFE_ASSERT(fSynth.envmod >= 0.0f && fSynth.envmod <= 1.0f);
  262. break;
  263. case paramDecay:
  264. fParams.decay = value;
  265. fSynth.decay = value/100.0f * 0.000491f + 0.000009f; // FIXME: log?
  266. DISTRHO_SAFE_ASSERT(fSynth.decay >= 0.000009f && fSynth.decay <= 0.0005f);
  267. break;
  268. case paramAccent:
  269. fParams.accent = value;
  270. fSynth.accent = value/100.0f;
  271. DISTRHO_SAFE_ASSERT(fSynth.accent >= 0.0f && fSynth.accent <= 1.0f);
  272. break;
  273. case paramVolume:
  274. fParams.volume = value;
  275. fSynth.volume = value/100.0f;
  276. DISTRHO_SAFE_ASSERT(fSynth.volume >= 0.0f && fSynth.volume <= 1.0f);
  277. break;
  278. }
  279. }
  280. // -----------------------------------------------------------------------
  281. // Process
  282. void DistrhoPluginNekobi::activate()
  283. {
  284. fSynth.nugget_remains = 0;
  285. fSynth.note_id = 0;
  286. if (fSynth.voice != nullptr)
  287. nekobee_synth_all_voices_off(&fSynth);
  288. }
  289. void DistrhoPluginNekobi::deactivate()
  290. {
  291. if (fSynth.voice != nullptr)
  292. nekobee_synth_all_voices_off(&fSynth);
  293. }
  294. void DistrhoPluginNekobi::run(const float**, float** outputs, uint32_t frames, const MidiEvent* midiEvents, uint32_t midiEventCount)
  295. {
  296. uint32_t framesDone = 0;
  297. uint32_t curEventIndex = 0;
  298. uint32_t burstSize;
  299. float* out = outputs[0];
  300. if (fSynth.voice == nullptr || ! dssp_voicelist_mutex_trylock(&fSynth))
  301. {
  302. std::memset(out, 0, sizeof(float)*frames);
  303. return;
  304. }
  305. while (framesDone < frames)
  306. {
  307. if (fSynth.nugget_remains == 0)
  308. fSynth.nugget_remains = XSYNTH_NUGGET_SIZE;
  309. /* process any ready events */
  310. while (curEventIndex < midiEventCount && framesDone == midiEvents[curEventIndex].frame)
  311. {
  312. if (midiEvents[curEventIndex].size > MidiEvent::kDataSize)
  313. continue;
  314. nekobee_handle_raw_event(&fSynth, midiEvents[curEventIndex].size, midiEvents[curEventIndex].data);
  315. curEventIndex++;
  316. }
  317. /* calculate the sample count (burstSize) for the next nekobee_voice_render() call to be the smallest of:
  318. * - control calculation quantization size (XSYNTH_NUGGET_SIZE, in samples)
  319. * - the number of samples remaining in an already-begun nugget (synth->nugget_remains)
  320. * - the number of samples until the next event is ready
  321. * - the number of samples left in this run
  322. */
  323. burstSize = XSYNTH_NUGGET_SIZE;
  324. /* we're still in the middle of a nugget, so reduce the burst size
  325. * to end when the nugget ends */
  326. if (fSynth.nugget_remains < burstSize)
  327. burstSize = fSynth.nugget_remains;
  328. /* reduce burst size to end when next event is ready */
  329. if (curEventIndex < midiEventCount && midiEvents[curEventIndex].frame - framesDone < burstSize)
  330. burstSize = midiEvents[curEventIndex].frame - framesDone;
  331. /* reduce burst size to end at end of this run */
  332. if (frames - framesDone < burstSize)
  333. burstSize = frames - framesDone;
  334. /* render the burst */
  335. nekobee_synth_render_voices(&fSynth, out + framesDone, burstSize, (burstSize == fSynth.nugget_remains));
  336. framesDone += burstSize;
  337. fSynth.nugget_remains -= burstSize;
  338. }
  339. dssp_voicelist_mutex_unlock(&fSynth);
  340. }
  341. // -----------------------------------------------------------------------
  342. Plugin* createPlugin()
  343. {
  344. return new DistrhoPluginNekobi();
  345. }
  346. // -----------------------------------------------------------------------
  347. END_NAMESPACE_DISTRHO