External plugins for 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.

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