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.

425 lines
13KB

  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-2022 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::initAudioPort(bool input, uint32_t index, AudioPort& port)
  134. {
  135. port.groupId = kPortGroupMono;
  136. Plugin::initAudioPort(input, index, port);
  137. }
  138. void DistrhoPluginNekobi::initParameter(uint32_t index, Parameter& parameter)
  139. {
  140. switch (index)
  141. {
  142. case paramWaveform:
  143. parameter.hints = kParameterIsAutomatable|kParameterIsInteger;
  144. parameter.name = "Waveform";
  145. parameter.symbol = "waveform";
  146. parameter.ranges.def = 0.0f;
  147. parameter.ranges.min = 0.0f;
  148. parameter.ranges.max = 1.0f;
  149. parameter.enumValues.count = 2;
  150. parameter.enumValues.restrictedMode = true;
  151. parameter.midiCC = 70; //Sound Variation
  152. {
  153. ParameterEnumerationValue* const enumValues = new ParameterEnumerationValue[2];
  154. enumValues[0].value = 0.0f;
  155. enumValues[0].label = "Square";
  156. enumValues[1].value = 1.0f;
  157. enumValues[1].label = "Triangle";
  158. parameter.enumValues.values = enumValues;
  159. }
  160. break;
  161. case paramTuning:
  162. parameter.hints = kParameterIsAutomatable; // was 0.5 <-> 2.0, log
  163. parameter.name = "Tuning";
  164. parameter.symbol = "tuning";
  165. parameter.ranges.def = 0.0f;
  166. parameter.ranges.min = -12.0f;
  167. parameter.ranges.max = 12.0f;
  168. parameter.midiCC = 75;
  169. break;
  170. case paramCutoff:
  171. parameter.hints = kParameterIsAutomatable; // modified x2.5
  172. parameter.name = "Cutoff";
  173. parameter.symbol = "cutoff";
  174. parameter.unit = "%";
  175. parameter.ranges.def = 25.0f;
  176. parameter.ranges.min = 0.0f;
  177. parameter.ranges.max = 100.0f;
  178. parameter.midiCC = 74;
  179. break;
  180. case paramResonance:
  181. parameter.hints = kParameterIsAutomatable; // modified x100
  182. parameter.name = "VCF Resonance";
  183. parameter.symbol = "resonance";
  184. parameter.unit = "%";
  185. parameter.ranges.def = 25.0f;
  186. parameter.ranges.min = 0.0f;
  187. parameter.ranges.max = 95.0f;
  188. parameter.midiCC = 71;
  189. break;
  190. case paramEnvMod:
  191. parameter.hints = kParameterIsAutomatable; // modified x100
  192. parameter.name = "Env Mod";
  193. parameter.symbol = "env_mod";
  194. parameter.unit = "%";
  195. parameter.ranges.def = 50.0f;
  196. parameter.ranges.min = 0.0f;
  197. parameter.ranges.max = 100.0f;
  198. parameter.midiCC = 1; //Mod Wheel
  199. break;
  200. case paramDecay:
  201. parameter.hints = kParameterIsAutomatable; // was 0.000009 <-> 0.0005, log
  202. parameter.name = "Decay";
  203. parameter.symbol = "decay";
  204. parameter.unit = "%";
  205. parameter.ranges.def = 75.0f;
  206. parameter.ranges.min = 0.0f;
  207. parameter.ranges.max = 100.0f;
  208. parameter.midiCC = 72;
  209. break;
  210. case paramAccent:
  211. parameter.hints = kParameterIsAutomatable; // modified x100
  212. parameter.name = "Accent";
  213. parameter.symbol = "accent";
  214. parameter.unit = "%";
  215. parameter.ranges.def = 25.0f;
  216. parameter.ranges.min = 0.0f;
  217. parameter.ranges.max = 100.0f;
  218. parameter.midiCC = 76;
  219. break;
  220. case paramVolume:
  221. parameter.hints = kParameterIsAutomatable; // modified x100
  222. parameter.name = "Volume";
  223. parameter.symbol = "volume";
  224. parameter.unit = "%";
  225. parameter.ranges.def = 75.0f;
  226. parameter.ranges.min = 0.0f;
  227. parameter.ranges.max = 100.0f;
  228. parameter.midiCC = 7; //Volume
  229. break;
  230. }
  231. }
  232. // -----------------------------------------------------------------------
  233. // Internal data
  234. float DistrhoPluginNekobi::getParameterValue(uint32_t index) const
  235. {
  236. switch (index)
  237. {
  238. case paramWaveform:
  239. return fParams.waveform;
  240. case paramTuning:
  241. return fParams.tuning;
  242. case paramCutoff:
  243. return fParams.cutoff;
  244. case paramResonance:
  245. return fParams.resonance;
  246. case paramEnvMod:
  247. return fParams.envMod;
  248. case paramDecay:
  249. return fParams.decay;
  250. case paramAccent:
  251. return fParams.accent;
  252. case paramVolume:
  253. return fParams.volume;
  254. }
  255. return 0.0f;
  256. }
  257. void DistrhoPluginNekobi::setParameterValue(uint32_t index, float value)
  258. {
  259. switch (index)
  260. {
  261. case paramWaveform:
  262. fParams.waveform = value;
  263. fSynth.waveform = value;
  264. DISTRHO_SAFE_ASSERT(fSynth.waveform == 0.0f || fSynth.waveform == 1.0f);
  265. break;
  266. case paramTuning:
  267. fParams.tuning = value;
  268. fSynth.tuning = (value+12.0f)/24.0f * 1.5 + 0.5f; // FIXME: log?
  269. DISTRHO_SAFE_ASSERT(fSynth.tuning >= 0.5f && fSynth.tuning <= 2.0f);
  270. break;
  271. case paramCutoff:
  272. fParams.cutoff = value;
  273. fSynth.cutoff = value/2.5f;
  274. DISTRHO_SAFE_ASSERT(fSynth.cutoff >= 0.0f && fSynth.cutoff <= 40.0f);
  275. break;
  276. case paramResonance:
  277. fParams.resonance = value;
  278. fSynth.resonance = value/100.0f;
  279. DISTRHO_SAFE_ASSERT(fSynth.resonance >= 0.0f && fSynth.resonance <= 0.95f);
  280. break;
  281. case paramEnvMod:
  282. fParams.envMod = value;
  283. fSynth.envmod = value/100.0f;
  284. DISTRHO_SAFE_ASSERT(fSynth.envmod >= 0.0f && fSynth.envmod <= 1.0f);
  285. break;
  286. case paramDecay:
  287. fParams.decay = value;
  288. fSynth.decay = value/100.0f * 0.000491f + 0.000009f; // FIXME: log?
  289. DISTRHO_SAFE_ASSERT(fSynth.decay >= 0.000009f && fSynth.decay <= 0.0005f);
  290. break;
  291. case paramAccent:
  292. fParams.accent = value;
  293. fSynth.accent = value/100.0f;
  294. DISTRHO_SAFE_ASSERT(fSynth.accent >= 0.0f && fSynth.accent <= 1.0f);
  295. break;
  296. case paramVolume:
  297. fParams.volume = value;
  298. fSynth.volume = value/100.0f;
  299. DISTRHO_SAFE_ASSERT(fSynth.volume >= 0.0f && fSynth.volume <= 1.0f);
  300. break;
  301. }
  302. }
  303. // -----------------------------------------------------------------------
  304. // Process
  305. void DistrhoPluginNekobi::activate()
  306. {
  307. fSynth.nugget_remains = 0;
  308. fSynth.note_id = 0;
  309. if (fSynth.voice != nullptr)
  310. nekobee_synth_all_voices_off(&fSynth);
  311. }
  312. void DistrhoPluginNekobi::deactivate()
  313. {
  314. if (fSynth.voice != nullptr)
  315. nekobee_synth_all_voices_off(&fSynth);
  316. }
  317. void DistrhoPluginNekobi::run(const float**, float** outputs, uint32_t frames, const MidiEvent* midiEvents, uint32_t midiEventCount)
  318. {
  319. uint32_t framesDone = 0;
  320. uint32_t curEventIndex = 0;
  321. uint32_t burstSize;
  322. float* out = outputs[0];
  323. if (fSynth.voice == nullptr || ! dssp_voicelist_mutex_trylock(&fSynth))
  324. {
  325. std::memset(out, 0, sizeof(float)*frames);
  326. return;
  327. }
  328. while (framesDone < frames)
  329. {
  330. if (fSynth.nugget_remains == 0)
  331. fSynth.nugget_remains = XSYNTH_NUGGET_SIZE;
  332. /* process any ready events */
  333. while (curEventIndex < midiEventCount && framesDone == midiEvents[curEventIndex].frame)
  334. {
  335. if (midiEvents[curEventIndex].size > MidiEvent::kDataSize)
  336. continue;
  337. nekobee_handle_raw_event(&fSynth, midiEvents[curEventIndex].size, midiEvents[curEventIndex].data);
  338. curEventIndex++;
  339. }
  340. /* calculate the sample count (burstSize) for the next nekobee_voice_render() call to be the smallest of:
  341. * - control calculation quantization size (XSYNTH_NUGGET_SIZE, in samples)
  342. * - the number of samples remaining in an already-begun nugget (synth->nugget_remains)
  343. * - the number of samples until the next event is ready
  344. * - the number of samples left in this run
  345. */
  346. burstSize = XSYNTH_NUGGET_SIZE;
  347. /* we're still in the middle of a nugget, so reduce the burst size
  348. * to end when the nugget ends */
  349. if (fSynth.nugget_remains < burstSize)
  350. burstSize = fSynth.nugget_remains;
  351. /* reduce burst size to end when next event is ready */
  352. if (curEventIndex < midiEventCount && midiEvents[curEventIndex].frame - framesDone < burstSize)
  353. burstSize = midiEvents[curEventIndex].frame - framesDone;
  354. /* reduce burst size to end at end of this run */
  355. if (frames - framesDone < burstSize)
  356. burstSize = frames - framesDone;
  357. /* render the burst */
  358. nekobee_synth_render_voices(&fSynth, out + framesDone, burstSize, (burstSize == fSynth.nugget_remains));
  359. framesDone += burstSize;
  360. fSynth.nugget_remains -= burstSize;
  361. }
  362. dssp_voicelist_mutex_unlock(&fSynth);
  363. }
  364. // -----------------------------------------------------------------------
  365. Plugin* createPlugin()
  366. {
  367. return new DistrhoPluginNekobi();
  368. }
  369. // -----------------------------------------------------------------------
  370. END_NAMESPACE_DISTRHO