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.

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