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.

406 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 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 GPL.txt file
  17. */
  18. #include "DistrhoPluginNekobi.hpp"
  19. #include "CarlaUtils.hpp"
  20. extern "C" {
  21. #include "nekobee-src/nekobee_synth.c"
  22. #include "nekobee-src/nekobee_voice.c"
  23. #include "nekobee-src/nekobee_voice_render.c"
  24. #include "nekobee-src/minblep_tables.c"
  25. /* ---- mutual exclusion ---- */
  26. bool dssp_voicelist_mutex_trylock(nekobee_synth_t* 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_lock(nekobee_synth_t* synth)
  43. {
  44. return (pthread_mutex_lock(&synth->voicelist_mutex) == 0);
  45. }
  46. bool dssp_voicelist_mutex_unlock(nekobee_synth_t *synth)
  47. {
  48. return (pthread_mutex_unlock(&synth->voicelist_mutex) == 0);
  49. }
  50. /*
  51. * nekobee_handle_raw_event
  52. */
  53. void nekobee_handle_raw_event(nekobee_synth_t* synth, uint8_t size, const uint8_t* data)
  54. {
  55. if (size != 3)
  56. return;
  57. switch (data[0] & 0xf0)
  58. {
  59. case 0x80:
  60. nekobee_synth_note_off(synth, data[1], data[2]);
  61. break;
  62. case 0x90:
  63. if (data[2] > 0)
  64. nekobee_synth_note_on(synth, data[1], data[2]);
  65. else
  66. nekobee_synth_note_off(synth, data[1], 64); /* shouldn't happen, but... */
  67. break;
  68. case 0xB0:
  69. nekobee_synth_control_change(synth, data[1], data[2]);
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. } /* extern "C" */
  76. START_NAMESPACE_DISTRHO
  77. // -------------------------------------------------
  78. DistrhoPluginNekobi::DistrhoPluginNekobi()
  79. : Plugin(paramCount, 0, 0) // 0 programs, 0 states
  80. {
  81. nekobee_init_tables();
  82. // init synth
  83. fSynth = new nekobee_synth_t;
  84. fSynth->sample_rate = d_sampleRate();
  85. fSynth->deltat = 1.0f / (float)d_sampleRate();
  86. fSynth->nugget_remains = 0;
  87. fSynth->note_id = 0;
  88. fSynth->polyphony = XSYNTH_DEFAULT_POLYPHONY;
  89. fSynth->voices = XSYNTH_DEFAULT_POLYPHONY;
  90. fSynth->monophonic = XSYNTH_MONO_MODE_ONCE;
  91. fSynth->glide = 0;
  92. fSynth->last_noteon_pitch = 0.0f;
  93. fSynth->vcf_accent = 0.0f;
  94. fSynth->vca_accent = 0.0f;
  95. for (int i=0; i<8; ++i)
  96. fSynth->held_keys[i] = -1;
  97. fSynth->voice = nekobee_voice_new();
  98. fSynth->voicelist_mutex_grab_failed = 0;
  99. pthread_mutex_init(&fSynth->voicelist_mutex, NULL);
  100. fSynth->channel_pressure = 0;
  101. fSynth->pitch_wheel_sensitivity = 0;
  102. fSynth->pitch_wheel = 0;
  103. for (int i=0; i<128; ++i)
  104. {
  105. fSynth->key_pressure[i] = 0;
  106. fSynth->cc[i] = 0;
  107. }
  108. fSynth->cc[7] = 127; // full volume
  109. fSynth->mod_wheel = 1.0f;
  110. fSynth->pitch_bend = 1.0f;
  111. fSynth->cc_volume = 1.0f;
  112. // Default values
  113. fParams.waveform = 0.0f;
  114. fParams.tuning = 0.0f;
  115. fParams.cutoff = 25.0f;
  116. fParams.resonance = 25.0f;
  117. fParams.envMod = 50.0f;
  118. fParams.decay = 75.0f;
  119. fParams.accent = 25.0f;
  120. fParams.volume = 75.0f;
  121. // Internal stuff
  122. fSynth->waveform = 0.0f;
  123. fSynth->tuning = 1.0f;
  124. fSynth->cutoff = 5.0f;
  125. fSynth->resonance = 0.8f;
  126. fSynth->envmod = 0.3f;
  127. fSynth->decay = 0.0002f;
  128. fSynth->accent = 0.3f;
  129. fSynth->volume = 0.75f;
  130. // reset
  131. d_deactivate();
  132. }
  133. DistrhoPluginNekobi::~DistrhoPluginNekobi()
  134. {
  135. free(fSynth->voice);
  136. delete fSynth;
  137. }
  138. // -------------------------------------------------
  139. // Init
  140. void DistrhoPluginNekobi::d_initParameter(uint32_t index, Parameter& parameter)
  141. {
  142. switch (index)
  143. {
  144. case paramWaveform:
  145. parameter.hints = PARAMETER_IS_AUTOMABLE|PARAMETER_IS_BOOLEAN;
  146. parameter.name = "Waveform";
  147. parameter.symbol = "waveform";
  148. parameter.ranges.def = 0.0f;
  149. parameter.ranges.min = 0.0f;
  150. parameter.ranges.max = 1.0f;
  151. break;
  152. case paramTuning:
  153. parameter.hints = PARAMETER_IS_AUTOMABLE; // was 0.5 <-> 2.0, log
  154. parameter.name = "Tuning";
  155. parameter.symbol = "tuning";
  156. parameter.ranges.def = 0.0f;
  157. parameter.ranges.min = -12.0f;
  158. parameter.ranges.max = 12.0f;
  159. break;
  160. case paramCutoff:
  161. parameter.hints = PARAMETER_IS_AUTOMABLE; // modified x2.5
  162. parameter.name = "Cutoff";
  163. parameter.symbol = "cutoff";
  164. parameter.unit = "%";
  165. parameter.ranges.def = 25.0f;
  166. parameter.ranges.min = 0.0f;
  167. parameter.ranges.max = 100.0f;
  168. break;
  169. case paramResonance:
  170. parameter.hints = PARAMETER_IS_AUTOMABLE; // modified x100
  171. parameter.name = "VCF Resonance";
  172. parameter.symbol = "resonance";
  173. parameter.unit = "%";
  174. parameter.ranges.def = 25.0f;
  175. parameter.ranges.min = 0.0f;
  176. parameter.ranges.max = 95.0f;
  177. break;
  178. case paramEnvMod:
  179. parameter.hints = PARAMETER_IS_AUTOMABLE; // modified x100
  180. parameter.name = "Env Mod";
  181. parameter.symbol = "env_mod";
  182. parameter.unit = "%";
  183. parameter.ranges.def = 50.0f;
  184. parameter.ranges.min = 0.0f;
  185. parameter.ranges.max = 100.0f;
  186. break;
  187. case paramDecay:
  188. parameter.hints = PARAMETER_IS_AUTOMABLE; // was 0.000009 <-> 0.0005, log
  189. parameter.name = "Decay";
  190. parameter.symbol = "decay";
  191. parameter.unit = "%";
  192. parameter.ranges.def = 75.0f;
  193. parameter.ranges.min = 0.0f;
  194. parameter.ranges.max = 100.0f;
  195. break;
  196. case paramAccent:
  197. parameter.hints = PARAMETER_IS_AUTOMABLE; // modified x100
  198. parameter.name = "Accent";
  199. parameter.symbol = "accent";
  200. parameter.unit = "%";
  201. parameter.ranges.def = 25.0f;
  202. parameter.ranges.min = 0.0f;
  203. parameter.ranges.max = 100.0f;
  204. break;
  205. case paramVolume:
  206. parameter.hints = PARAMETER_IS_AUTOMABLE; // modified x100
  207. parameter.name = "Volume";
  208. parameter.symbol = "volume";
  209. parameter.unit = "%";
  210. parameter.ranges.def = 75.0f;
  211. parameter.ranges.min = 0.0f;
  212. parameter.ranges.max = 100.0f;
  213. break;
  214. }
  215. }
  216. // -------------------------------------------------
  217. // Internal data
  218. float DistrhoPluginNekobi::d_parameterValue(uint32_t index)
  219. {
  220. switch (index)
  221. {
  222. case paramWaveform:
  223. return fParams.waveform;
  224. case paramTuning:
  225. return fParams.tuning;
  226. case paramCutoff:
  227. return fParams.cutoff;
  228. case paramResonance:
  229. return fParams.resonance;
  230. case paramEnvMod:
  231. return fParams.envMod;
  232. case paramDecay:
  233. return fParams.decay;
  234. case paramAccent:
  235. return fParams.accent;
  236. case paramVolume:
  237. return fParams.volume;
  238. }
  239. return 0.0f;
  240. }
  241. void DistrhoPluginNekobi::d_setParameterValue(uint32_t index, float value)
  242. {
  243. switch (index)
  244. {
  245. case paramWaveform:
  246. fParams.waveform = value;
  247. fSynth->waveform = value;
  248. CARLA_SAFE_ASSERT_INT2(fSynth->waveform == 0.0f || fSynth->waveform == 1.0f, fSynth->waveform, value);
  249. break;
  250. case paramTuning:
  251. fParams.tuning = value;
  252. fSynth->tuning = (value+12.0f)/24.0f * 1.5 + 0.5f; // FIXME: log?
  253. CARLA_SAFE_ASSERT_INT2(fSynth->tuning >= 0.5f && fSynth->tuning <= 2.0f, fSynth->tuning, value);
  254. break;
  255. case paramCutoff:
  256. fParams.cutoff = value;
  257. fSynth->cutoff = value/2.5f;
  258. CARLA_SAFE_ASSERT_INT2(fSynth->cutoff >= 0.0f && fSynth->cutoff <= 40.0f, fSynth->cutoff, value);
  259. break;
  260. case paramResonance:
  261. fParams.resonance = value;
  262. fSynth->resonance = value/100.0f;
  263. CARLA_SAFE_ASSERT_INT2(fSynth->resonance >= 0.0f && fSynth->resonance <= 0.95f, fSynth->resonance, value);
  264. break;
  265. case paramEnvMod:
  266. fParams.envMod = value;
  267. fSynth->envmod = value/100.0f;
  268. CARLA_SAFE_ASSERT_INT2(fSynth->envmod >= 0.0f && fSynth->envmod <= 1.0f, fSynth->envmod, value);
  269. break;
  270. case paramDecay:
  271. fParams.decay = value;
  272. fSynth->decay = value/100.0f * 0.000491f + 0.000009f; // FIXME: log?
  273. CARLA_SAFE_ASSERT_INT2(fSynth->decay >= 0.000009f && fSynth->decay <= 0.0005f, fSynth->decay, value);
  274. break;
  275. case paramAccent:
  276. fParams.accent = value;
  277. fSynth->accent = value/100.0f;
  278. CARLA_SAFE_ASSERT_INT2(fSynth->accent >= 0.0f && fSynth->accent <= 1.0f, fSynth->accent, value);
  279. break;
  280. case paramVolume:
  281. fParams.volume = value;
  282. fSynth->volume = value/100.0f;
  283. CARLA_SAFE_ASSERT_INT2(fSynth->volume >= 0.0f && fSynth->volume <= 1.0f, fSynth->volume, value);
  284. break;
  285. }
  286. }
  287. // -------------------------------------------------
  288. // Process
  289. void DistrhoPluginNekobi::d_activate()
  290. {
  291. fSynth->nugget_remains = 0;
  292. fSynth->note_id = 0;
  293. if (fSynth->voice != nullptr)
  294. nekobee_synth_all_voices_off(fSynth);
  295. }
  296. void DistrhoPluginNekobi::d_deactivate()
  297. {
  298. if (fSynth->voice != nullptr)
  299. nekobee_synth_all_voices_off(fSynth);
  300. }
  301. void DistrhoPluginNekobi::d_run(float**, float** outputs, uint32_t frames, uint32_t midiEventCount, const MidiEvent* midiEvents)
  302. {
  303. uint32_t framesDone = 0;
  304. uint32_t curEventIndex = 0;
  305. uint32_t burstSize;
  306. float* out = outputs[0];
  307. if (fSynth->voice == nullptr || ! dssp_voicelist_mutex_trylock(fSynth))
  308. {
  309. for (uint32_t i=0; i < frames; ++i)
  310. *out++ = 0.0f;
  311. return;
  312. }
  313. while (framesDone < frames)
  314. {
  315. if (fSynth->nugget_remains == 0)
  316. fSynth->nugget_remains = XSYNTH_NUGGET_SIZE;
  317. /* process any ready events */
  318. while (curEventIndex < midiEventCount && framesDone == midiEvents[curEventIndex].frame)
  319. {
  320. nekobee_handle_raw_event(fSynth, midiEvents[curEventIndex].size, midiEvents[curEventIndex].buf);
  321. curEventIndex++;
  322. }
  323. /* calculate the sample count (burstSize) for the next nekobee_voice_render() call to be the smallest of:
  324. * - control calculation quantization size (XSYNTH_NUGGET_SIZE, in samples)
  325. * - the number of samples remaining in an already-begun nugget (synth->nugget_remains)
  326. * - the number of samples until the next event is ready
  327. * - the number of samples left in this run
  328. */
  329. burstSize = XSYNTH_NUGGET_SIZE;
  330. /* we're still in the middle of a nugget, so reduce the burst size
  331. * to end when the nugget ends */
  332. if (fSynth->nugget_remains < burstSize)
  333. burstSize = fSynth->nugget_remains;
  334. /* reduce burst size to end when next event is ready */
  335. if (curEventIndex < midiEventCount && midiEvents[curEventIndex].frame - framesDone < burstSize)
  336. burstSize = midiEvents[curEventIndex].frame - framesDone;
  337. /* reduce burst size to end at end of this run */
  338. if (frames - framesDone < burstSize)
  339. burstSize = frames - framesDone;
  340. /* render the burst */
  341. nekobee_synth_render_voices(fSynth, out + framesDone, burstSize, (burstSize == fSynth->nugget_remains));
  342. framesDone += burstSize;
  343. fSynth->nugget_remains -= burstSize;
  344. }
  345. dssp_voicelist_mutex_unlock(fSynth);
  346. }
  347. // -------------------------------------------------
  348. Plugin* createPlugin()
  349. {
  350. return new DistrhoPluginNekobi();
  351. }
  352. // -------------------------------------------------
  353. END_NAMESPACE_DISTRHO