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.

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