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.

436 lines
14KB

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