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.

470 lines
16KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Controller.cpp - (Midi) Controllers implementation
  4. Copyright (C) 2002-2005 Nasca Octavian Paul
  5. Author: Nasca Octavian Paul
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. */
  11. #include "Controller.h"
  12. #include "../Misc/Util.h"
  13. #include "../Misc/Time.h"
  14. #include "../Misc/XMLwrapper.h"
  15. #include <cmath>
  16. #include <cstdio>
  17. #include <rtosc/ports.h>
  18. #include <rtosc/port-sugar.h>
  19. using namespace rtosc;
  20. #define rObject Controller
  21. #undef rChangeCb
  22. #define rChangeCb if (obj->time) { obj->last_update_timestamp = obj->time->time(); }
  23. const rtosc::Ports Controller::ports = {
  24. rParamZyn(panning.depth, rShort("pan.d"), "Depth of Panning MIDI Control"),
  25. rParamZyn(filtercutoff.depth, rShort("fc.d"), "Depth of Filter Cutoff MIDI Control"),
  26. rParamZyn(filterq.depth, rShort("fq.d"), "Depth of Filter Q MIDI Control"),
  27. rParamZyn(bandwidth.depth, rShort("bw.d"), "Depth of Bandwidth MIDI Control"),
  28. rToggle(bandwidth.exponential, rShort("bw.exp"), "Bandwidth Exponential Mode"),
  29. rParamZyn(modwheel.depth, rShort("mdw.d"), "Depth of Modwheel MIDI Control"),
  30. rToggle(modwheel.exponential, rShort("mdw.exp"), "Modwheel Exponential Mode"),
  31. rToggle(pitchwheel.is_split, "If PitchWheel Has unified bendrange or not"),
  32. rParamI(pitchwheel.bendrange, rShort("pch.d"), "Range of MIDI Pitch Wheel"),
  33. rParamI(pitchwheel.bendrange_down, "Lower Range of MIDI Pitch Wheel"),
  34. rToggle(expression.receive, rShort("exp.rcv"), "Expression MIDI Receive"),
  35. rToggle(fmamp.receive, rShort("fma.rcv"), "FM amplitude MIDI Receive"),
  36. rToggle(volume.receive, rShort("vol.rcv"), "Volume MIDI Receive"),
  37. rToggle(sustain.receive, rShort("sus.rcv"), "Sustain MIDI Receive"),
  38. rToggle(portamento.receive, rShort("prt.rcv"), "Portamento MIDI Receive"),
  39. rToggle(portamento.portamento, "Portamento Enable"),
  40. rParamZyn(portamento.time, rShort("time"), "Portamento Length"),
  41. rToggle(portamento.proportional, rShort("propt."), "Whether the portamento time is proportional"
  42. "to the size of the interval between two notes."),
  43. rParamZyn(portamento.propRate, rShort("scale"), "Portamento proportional scale"),
  44. rParamZyn(portamento.propDepth, rShort("depth"), "Portamento proportional depth"),
  45. rParamZyn(portamento.pitchthresh, rShort("thresh"), "Threshold for portamento"),
  46. rToggle(portamento.pitchthreshtype, rShort("tr.type"), "Type of threshold"),
  47. rParamZyn(portamento.updowntimestretch, rShort("up/dwn"), "Relative length of glide up vs glide down"),
  48. rParamZyn(resonancecenter.depth, rShort("rfc.d"), "Resonance Center MIDI Depth"),
  49. rParamZyn(resonancebandwidth.depth, rShort("rbw.d"), "Resonance Bandwidth MIDI Depth"),
  50. rToggle(NRPN.receive, "NRPN MIDI Enable"),
  51. rAction(defaults),
  52. };
  53. #undef rChangeCb
  54. Controller::Controller(const SYNTH_T &synth_, const AbsTime *time_)
  55. :time(time_), last_update_timestamp(0), synth(synth_)
  56. {
  57. defaults();
  58. resetall();
  59. }
  60. Controller &Controller::operator=(const Controller &c)
  61. {
  62. //just pretend that undefined behavior doesn't exist...
  63. memcpy(this, &c, sizeof(Controller));
  64. return *this;
  65. }
  66. Controller::~Controller()
  67. {}
  68. void Controller::defaults()
  69. {
  70. pitchwheel.bendrange = 200; //2 halftones
  71. pitchwheel.bendrange_down = 0; //Unused by default
  72. pitchwheel.is_split = false;
  73. expression.receive = 1;
  74. panning.depth = 64;
  75. filtercutoff.depth = 64;
  76. filterq.depth = 64;
  77. bandwidth.depth = 64;
  78. bandwidth.exponential = 0;
  79. modwheel.depth = 80;
  80. modwheel.exponential = 0;
  81. fmamp.receive = 1;
  82. volume.receive = 1;
  83. sustain.receive = 1;
  84. NRPN.receive = 1;
  85. portamento.portamento = 0;
  86. portamento.used = 0;
  87. portamento.proportional = 0;
  88. portamento.propRate = 80;
  89. portamento.propDepth = 90;
  90. portamento.receive = 1;
  91. portamento.time = 64;
  92. portamento.updowntimestretch = 64;
  93. portamento.pitchthresh = 3;
  94. portamento.pitchthreshtype = 1;
  95. portamento.noteusing = -1;
  96. resonancecenter.depth = 64;
  97. resonancebandwidth.depth = 64;
  98. initportamento(440.0f, 440.0f, false); // Now has a third argument
  99. setportamento(0);
  100. }
  101. void Controller::resetall()
  102. {
  103. setpitchwheel(0); //center
  104. setexpression(127);
  105. setpanning(64);
  106. setfiltercutoff(64);
  107. setfilterq(64);
  108. setbandwidth(64);
  109. setmodwheel(64);
  110. setfmamp(127);
  111. setvolume(127);
  112. setsustain(0);
  113. setresonancecenter(64);
  114. setresonancebw(64);
  115. //reset the NRPN
  116. NRPN.parhi = -1;
  117. NRPN.parlo = -1;
  118. NRPN.valhi = -1;
  119. NRPN.vallo = -1;
  120. }
  121. void Controller::setpitchwheel(int value)
  122. {
  123. pitchwheel.data = value;
  124. float cents = value / 8192.0f;
  125. if(pitchwheel.is_split && cents < 0)
  126. cents *= pitchwheel.bendrange_down;
  127. else
  128. cents *= pitchwheel.bendrange;
  129. pitchwheel.relfreq = powf(2, cents / 1200.0f);
  130. //fprintf(stderr,"%ld %ld -> %.3f\n",pitchwheel.bendrange,pitchwheel.data,pitchwheel.relfreq);fflush(stderr);
  131. }
  132. void Controller::setexpression(int value)
  133. {
  134. expression.data = value;
  135. if(expression.receive != 0)
  136. expression.relvolume = value / 127.0f;
  137. else
  138. expression.relvolume = 1.0f;
  139. }
  140. void Controller::setpanning(int value)
  141. {
  142. panning.data = value;
  143. panning.pan = (value / 128.0f - 0.5f) * (panning.depth / 64.0f);
  144. }
  145. void Controller::setfiltercutoff(int value)
  146. {
  147. filtercutoff.data = value;
  148. filtercutoff.relfreq =
  149. (value - 64.0f) * filtercutoff.depth / 4096.0f * 3.321928f; //3.3219f..=ln2(10)
  150. }
  151. void Controller::setfilterq(int value)
  152. {
  153. filterq.data = value;
  154. filterq.relq = powf(30.0f, (value - 64.0f) / 64.0f * (filterq.depth / 64.0f));
  155. }
  156. void Controller::setbandwidth(int value)
  157. {
  158. bandwidth.data = value;
  159. if(bandwidth.exponential == 0) {
  160. float tmp = powf(25.0f, powf(bandwidth.depth / 127.0f, 1.5f)) - 1.0f;
  161. if((value < 64) && (bandwidth.depth >= 64))
  162. tmp = 1.0f;
  163. bandwidth.relbw = (value / 64.0f - 1.0f) * tmp + 1.0f;
  164. if(bandwidth.relbw < 0.01f)
  165. bandwidth.relbw = 0.01f;
  166. }
  167. else
  168. bandwidth.relbw =
  169. powf(25.0f, (value - 64.0f) / 64.0f * (bandwidth.depth / 64.0f));
  170. ;
  171. }
  172. void Controller::setmodwheel(int value)
  173. {
  174. modwheel.data = value;
  175. if(modwheel.exponential == 0) {
  176. float tmp =
  177. powf(25.0f, powf(modwheel.depth / 127.0f, 1.5f) * 2.0f) / 25.0f;
  178. if((value < 64) && (modwheel.depth >= 64))
  179. tmp = 1.0f;
  180. modwheel.relmod = (value / 64.0f - 1.0f) * tmp + 1.0f;
  181. if(modwheel.relmod < 0.0f)
  182. modwheel.relmod = 0.0f;
  183. }
  184. else
  185. modwheel.relmod =
  186. powf(25.0f, (value - 64.0f) / 64.0f * (modwheel.depth / 80.0f));
  187. }
  188. void Controller::setfmamp(int value)
  189. {
  190. fmamp.data = value;
  191. fmamp.relamp = value / 127.0f;
  192. if(fmamp.receive != 0)
  193. fmamp.relamp = value / 127.0f;
  194. else
  195. fmamp.relamp = 1.0f;
  196. }
  197. void Controller::setvolume(int value)
  198. {
  199. volume.data = value;
  200. if(volume.receive != 0)
  201. volume.volume = powf(0.1f, (127 - value) / 127.0f * 2.0f);
  202. else
  203. volume.volume = 1.0f;
  204. }
  205. void Controller::setsustain(int value)
  206. {
  207. sustain.data = value;
  208. if(sustain.receive != 0)
  209. sustain.sustain = ((value < 64) ? 0 : 1);
  210. else
  211. sustain.sustain = 0;
  212. }
  213. void Controller::setportamento(int value)
  214. {
  215. portamento.data = value;
  216. if(portamento.receive != 0)
  217. portamento.portamento = ((value < 64) ? 0 : 1);
  218. }
  219. int Controller::initportamento(float oldfreq,
  220. float newfreq,
  221. bool legatoflag)
  222. {
  223. portamento.x = 0.0f;
  224. if(legatoflag) { // Legato in progress
  225. if(portamento.portamento == 0)
  226. return 0;
  227. }
  228. else // No legato, do the original if...return
  229. if((portamento.used != 0) || (portamento.portamento == 0))
  230. return 0;
  231. ;
  232. float portamentotime = powf(100.0f, portamento.time / 127.0f) / 50.0f; //portamento time in seconds
  233. if(portamento.proportional) {
  234. //If there is a min(float,float) and a max(float,float) then they
  235. //could be used here
  236. //Linear functors could also make this nicer
  237. if(oldfreq > newfreq) //2 is the center of propRate
  238. portamentotime *=
  239. powf(oldfreq / newfreq
  240. / (portamento.propRate / 127.0f * 3 + .05),
  241. (portamento.propDepth / 127.0f * 1.6f + .2));
  242. else //1 is the center of propDepth
  243. portamentotime *=
  244. powf(newfreq / oldfreq
  245. / (portamento.propRate / 127.0f * 3 + .05),
  246. (portamento.propDepth / 127.0f * 1.6f + .2));
  247. }
  248. if((portamento.updowntimestretch >= 64) && (newfreq < oldfreq)) {
  249. if(portamento.updowntimestretch == 127)
  250. return 0;
  251. portamentotime *= powf(0.1f,
  252. (portamento.updowntimestretch - 64) / 63.0f);
  253. }
  254. if((portamento.updowntimestretch < 64) && (newfreq > oldfreq)) {
  255. if(portamento.updowntimestretch == 0)
  256. return 0;
  257. portamentotime *= powf(0.1f,
  258. (64.0f - portamento.updowntimestretch) / 64.0f);
  259. }
  260. //printf("%f->%f : Time %f\n",oldfreq,newfreq,portamentotime);
  261. portamento.dx = synth.buffersize_f / (portamentotime * synth.samplerate_f);
  262. portamento.origfreqrap = oldfreq / newfreq;
  263. float tmprap = ((portamento.origfreqrap > 1.0f) ?
  264. (portamento.origfreqrap) :
  265. (1.0f / portamento.origfreqrap));
  266. float thresholdrap = powf(2.0f, portamento.pitchthresh / 12.0f);
  267. if((portamento.pitchthreshtype == 0) && (tmprap - 0.00001f > thresholdrap))
  268. return 0;
  269. if((portamento.pitchthreshtype == 1) && (tmprap + 0.00001f < thresholdrap))
  270. return 0;
  271. portamento.used = 1;
  272. portamento.freqrap = portamento.origfreqrap;
  273. return 1;
  274. }
  275. void Controller::updateportamento()
  276. {
  277. if(portamento.used == 0)
  278. return;
  279. portamento.x += portamento.dx;
  280. if(portamento.x > 1.0f) {
  281. portamento.x = 1.0f;
  282. portamento.used = 0;
  283. }
  284. portamento.freqrap =
  285. (1.0f - portamento.x) * portamento.origfreqrap + portamento.x;
  286. }
  287. void Controller::setresonancecenter(int value)
  288. {
  289. resonancecenter.data = value;
  290. resonancecenter.relcenter =
  291. powf(3.0f, (value - 64.0f) / 64.0f * (resonancecenter.depth / 64.0f));
  292. }
  293. void Controller::setresonancebw(int value)
  294. {
  295. resonancebandwidth.data = value;
  296. resonancebandwidth.relbw =
  297. powf(1.5f, (value - 64.0f) / 64.0f * (resonancebandwidth.depth / 127.0f));
  298. }
  299. //Returns 0 if there is NRPN or 1 if there is not
  300. int Controller::getnrpn(int *parhi, int *parlo, int *valhi, int *vallo)
  301. {
  302. if(NRPN.receive == 0)
  303. return 1;
  304. if((NRPN.parhi < 0) || (NRPN.parlo < 0) || (NRPN.valhi < 0)
  305. || (NRPN.vallo < 0))
  306. return 1;
  307. *parhi = NRPN.parhi;
  308. *parlo = NRPN.parlo;
  309. *valhi = NRPN.valhi;
  310. *vallo = NRPN.vallo;
  311. return 0;
  312. }
  313. void Controller::setparameternumber(unsigned int type, int value)
  314. {
  315. switch(type) {
  316. case C_nrpnhi:
  317. NRPN.parhi = value;
  318. NRPN.valhi = -1;
  319. NRPN.vallo = -1; //clear the values
  320. break;
  321. case C_nrpnlo:
  322. NRPN.parlo = value;
  323. NRPN.valhi = -1;
  324. NRPN.vallo = -1; //clear the values
  325. break;
  326. case C_dataentryhi:
  327. if((NRPN.parhi >= 0) && (NRPN.parlo >= 0))
  328. NRPN.valhi = value;
  329. break;
  330. case C_dataentrylo:
  331. if((NRPN.parhi >= 0) && (NRPN.parlo >= 0))
  332. NRPN.vallo = value;
  333. break;
  334. }
  335. }
  336. void Controller::add2XML(XMLwrapper& xml)
  337. {
  338. xml.addpar("pitchwheel_bendrange", pitchwheel.bendrange);
  339. xml.addpar("pitchwheel_bendrange_down", pitchwheel.bendrange_down);
  340. xml.addparbool("pitchwheel_split", pitchwheel.is_split);
  341. xml.addparbool("expression_receive", expression.receive);
  342. xml.addpar("panning_depth", panning.depth);
  343. xml.addpar("filter_cutoff_depth", filtercutoff.depth);
  344. xml.addpar("filter_q_depth", filterq.depth);
  345. xml.addpar("bandwidth_depth", bandwidth.depth);
  346. xml.addpar("mod_wheel_depth", modwheel.depth);
  347. xml.addparbool("mod_wheel_exponential", modwheel.exponential);
  348. xml.addparbool("fm_amp_receive", fmamp.receive);
  349. xml.addparbool("volume_receive", volume.receive);
  350. xml.addparbool("sustain_receive", sustain.receive);
  351. xml.addparbool("portamento_receive", portamento.receive);
  352. xml.addpar("portamento_time", portamento.time);
  353. xml.addpar("portamento_pitchthresh", portamento.pitchthresh);
  354. xml.addpar("portamento_pitchthreshtype", portamento.pitchthreshtype);
  355. xml.addpar("portamento_portamento", portamento.portamento);
  356. xml.addpar("portamento_updowntimestretch", portamento.updowntimestretch);
  357. xml.addpar("portamento_proportional", portamento.proportional);
  358. xml.addpar("portamento_proprate", portamento.propRate);
  359. xml.addpar("portamento_propdepth", portamento.propDepth);
  360. xml.addpar("resonance_center_depth", resonancecenter.depth);
  361. xml.addpar("resonance_bandwidth_depth", resonancebandwidth.depth);
  362. }
  363. void Controller::getfromXML(XMLwrapper& xml)
  364. {
  365. pitchwheel.bendrange = xml.getpar("pitchwheel_bendrange",
  366. pitchwheel.bendrange,
  367. -6400,
  368. 6400);
  369. pitchwheel.bendrange_down = xml.getpar("pitchwheel_bendrange_down",
  370. pitchwheel.bendrange_down,
  371. -6400,
  372. 6400);
  373. pitchwheel.is_split = xml.getparbool("pitchwheel_split",
  374. pitchwheel.is_split);
  375. expression.receive = xml.getparbool("expression_receive",
  376. expression.receive);
  377. panning.depth = xml.getpar127("panning_depth", panning.depth);
  378. filtercutoff.depth = xml.getpar127("filter_cutoff_depth",
  379. filtercutoff.depth);
  380. filterq.depth = xml.getpar127("filter_q_depth", filterq.depth);
  381. bandwidth.depth = xml.getpar127("bandwidth_depth", bandwidth.depth);
  382. modwheel.depth = xml.getpar127("mod_wheel_depth", modwheel.depth);
  383. modwheel.exponential = xml.getparbool("mod_wheel_exponential",
  384. modwheel.exponential);
  385. fmamp.receive = xml.getparbool("fm_amp_receive",
  386. fmamp.receive);
  387. volume.receive = xml.getparbool("volume_receive",
  388. volume.receive);
  389. sustain.receive = xml.getparbool("sustain_receive",
  390. sustain.receive);
  391. portamento.receive = xml.getparbool("portamento_receive",
  392. portamento.receive);
  393. portamento.time = xml.getpar127("portamento_time",
  394. portamento.time);
  395. portamento.pitchthresh = xml.getpar127("portamento_pitchthresh",
  396. portamento.pitchthresh);
  397. portamento.pitchthreshtype = xml.getpar127("portamento_pitchthreshtype",
  398. portamento.pitchthreshtype);
  399. portamento.portamento = xml.getpar127("portamento_portamento",
  400. portamento.portamento);
  401. portamento.updowntimestretch = xml.getpar127(
  402. "portamento_updowntimestretch",
  403. portamento.updowntimestretch);
  404. portamento.proportional = xml.getpar127("portamento_proportional",
  405. portamento.proportional);
  406. portamento.propRate = xml.getpar127("portamento_proprate",
  407. portamento.propRate);
  408. portamento.propDepth = xml.getpar127("portamento_propdepth",
  409. portamento.propDepth);
  410. resonancecenter.depth = xml.getpar127("resonance_center_depth",
  411. resonancecenter.depth);
  412. resonancebandwidth.depth = xml.getpar127("resonance_bandwidth_depth",
  413. resonancebandwidth.depth);
  414. }