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.

483 lines
16KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. SUBnoteParameters.cpp - Parameters for SUBnote (SUBsynth)
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include "../globals.h"
  18. #include "SUBnoteParameters.h"
  19. #include "EnvelopeParams.h"
  20. #include "FilterParams.h"
  21. #include "../Misc/Util.h"
  22. #include <cstdio>
  23. #include <cmath>
  24. #include <rtosc/ports.h>
  25. #include <rtosc/port-sugar.h>
  26. #define rObject SUBnoteParameters
  27. using namespace rtosc;
  28. static const rtosc::Ports SUBnotePorts = {
  29. rSelf(SUBnoteParameters),
  30. rPaste,
  31. rToggle(Pstereo, "Stereo Enable"),
  32. rParamZyn(PVolume, "Volume"),
  33. rParamZyn(PPanning, "Left Right Panning"),
  34. rParamZyn(PAmpVelocityScaleFunction, "Amplitude Velocity Sensing function"),
  35. rParamI(PDetune, "Detune in detune type units"),
  36. rParamI(PCoarseDetune, "Coarse Detune"),
  37. //Real values needed
  38. rOption(PDetuneType, rOptions("100 cents", "200 cents", "500 cents"), "Detune Scale"),
  39. rToggle(PFreqEnvelopeEnabled, "Enable for Frequency Envelope"),
  40. rToggle(PBandWidthEnvelopeEnabled, "Enable for Bandwidth Envelope"),
  41. rToggle(PGlobalFilterEnabled, "Enable for Global Filter"),
  42. rParamZyn(PGlobalFilterVelocityScale, "Filter Velocity Magnitude"),
  43. rParamZyn(PGlobalFilterVelocityScaleFunction, "Filter Velocity Function Shape"),
  44. //rRecur(FreqEnvelope, EnvelopeParams),
  45. //rToggle(),//continue
  46. rToggle(Pfixedfreq, "Base frequency fixed frequency enable"),
  47. rParamZyn(PfixedfreqET, "Equal temeperate control for fixed frequency operation"),
  48. #undef rChangeCb
  49. #define rChangeCb obj->updateFrequencyMultipliers();
  50. rParamI(POvertoneSpread.type, rMap(min, 0), rMap(max, 7),
  51. "Spread of harmonic frequencies"),
  52. rParamI(POvertoneSpread.par1, rMap(min, 0), rMap(max, 255),
  53. "Overtone Parameter"),
  54. rParamI(POvertoneSpread.par2, rMap(min, 0), rMap(max, 255),
  55. "Overtone Parameter"),
  56. rParamI(POvertoneSpread.par3, rMap(min, 0), rMap(max, 255),
  57. "Overtone Parameter"),
  58. #undef rChangeCb
  59. #define rChangeCb
  60. rParamZyn(Pnumstages, rMap(min, 1), rMap(max, 5), "Number of filter stages"),
  61. rParamZyn(Pbandwidth, "Bandwidth of filters"),
  62. rParamZyn(Phmagtype, "How the magnitudes are computed (0=linear,1=-60dB,2=-60dB)"),
  63. rArray(Phmag, MAX_SUB_HARMONICS, "Harmonic magnitudes"),
  64. rArray(Phrelbw, MAX_SUB_HARMONICS, "Relative bandwidth"),
  65. rParamZyn(Pbwscale, "Bandwidth scaling with frequency"),
  66. rRecurp(AmpEnvelope, "Amplitude envelope"),
  67. rRecurp(FreqEnvelope, "Frequency Envelope"),
  68. rRecurp(BandWidthEnvelope, "Bandwidth Envelope"),
  69. rRecurp(GlobalFilterEnvelope, "Post Filter Envelope"),
  70. rRecurp(GlobalFilter, "Post Filter"),
  71. rOption(Pstart, rOptions("zero", "random", "ones"), "How harmonics are initialized"),
  72. {"clear:", rDoc("Reset all harmonics to equal bandwidth/zero amplitude"), NULL, [](const char *, RtData &d)
  73. {
  74. SUBnoteParameters *obj = (SUBnoteParameters *)d.obj;
  75. for(int i=0; i<MAX_SUB_HARMONICS; ++i) {
  76. obj->Phmag[i] = 0;
  77. obj->Phrelbw[i] = 64;
  78. }
  79. obj->Phmag[0] = 127;
  80. }},
  81. {"detunevalue:", rDoc("Get note detune value"), NULL, [](const char *, RtData &d)
  82. {
  83. SUBnoteParameters *obj = (SUBnoteParameters *)d.obj;
  84. d.reply(d.loc, "f", getdetune(obj->PDetuneType, 0, obj->PDetune));
  85. }},
  86. //weird stuff for PCoarseDetune
  87. {"octave::c:i", rProp(parameter) rDoc("Note octave shift"), NULL,
  88. [](const char *msg, RtData &d)
  89. {
  90. SUBnoteParameters *obj = (SUBnoteParameters *)d.obj;
  91. if(!rtosc_narguments(msg)) {
  92. int k=obj->PCoarseDetune/1024;
  93. if (k>=8) k-=16;
  94. d.reply(d.loc, "i", k);
  95. } else {
  96. int k=(int) rtosc_argument(msg, 0).i;
  97. if (k<0) k+=16;
  98. obj->PCoarseDetune = k*1024 + obj->PCoarseDetune%1024;
  99. }
  100. }},
  101. {"coarsedetune::c:i", rProp(parameter) rDoc("Note coarse detune"), NULL,
  102. [](const char *msg, RtData &d)
  103. {
  104. SUBnoteParameters *obj = (SUBnoteParameters *)d.obj;
  105. if(!rtosc_narguments(msg)) {
  106. int k=obj->PCoarseDetune%1024;
  107. if (k>=512) k-=1024;
  108. d.reply(d.loc, "i", k);
  109. } else {
  110. int k=(int) rtosc_argument(msg, 0).i;
  111. if (k<0) k+=1024;
  112. obj->PCoarseDetune = k + (obj->PCoarseDetune/1024)*1024;
  113. }
  114. }},
  115. };
  116. const rtosc::Ports &SUBnoteParameters::ports = SUBnotePorts;
  117. SUBnoteParameters::SUBnoteParameters():Presets()
  118. {
  119. setpresettype("Psubsynth");
  120. AmpEnvelope = new EnvelopeParams(64, 1);
  121. AmpEnvelope->ADSRinit_dB(0, 40, 127, 25);
  122. FreqEnvelope = new EnvelopeParams(64, 0);
  123. FreqEnvelope->ASRinit(30, 50, 64, 60);
  124. BandWidthEnvelope = new EnvelopeParams(64, 0);
  125. BandWidthEnvelope->ASRinit_bw(100, 70, 64, 60);
  126. GlobalFilter = new FilterParams(2, 80, 40);
  127. GlobalFilterEnvelope = new EnvelopeParams(0, 1);
  128. GlobalFilterEnvelope->ADSRinit_filter(64, 40, 64, 70, 60, 64);
  129. defaults();
  130. }
  131. void SUBnoteParameters::defaults()
  132. {
  133. PVolume = 96;
  134. PPanning = 64;
  135. PAmpVelocityScaleFunction = 90;
  136. Pfixedfreq = 0;
  137. PfixedfreqET = 0;
  138. Pnumstages = 2;
  139. Pbandwidth = 40;
  140. Phmagtype = 0;
  141. Pbwscale = 64;
  142. Pstereo = 1;
  143. Pstart = 1;
  144. PDetune = 8192;
  145. PCoarseDetune = 0;
  146. PDetuneType = 1;
  147. PFreqEnvelopeEnabled = 0;
  148. PBandWidthEnvelopeEnabled = 0;
  149. POvertoneSpread.type = 0;
  150. POvertoneSpread.par1 = 0;
  151. POvertoneSpread.par2 = 0;
  152. POvertoneSpread.par3 = 0;
  153. updateFrequencyMultipliers();
  154. for(int n = 0; n < MAX_SUB_HARMONICS; ++n) {
  155. Phmag[n] = 0;
  156. Phrelbw[n] = 64;
  157. }
  158. Phmag[0] = 127;
  159. PGlobalFilterEnabled = 0;
  160. PGlobalFilterVelocityScale = 64;
  161. PGlobalFilterVelocityScaleFunction = 64;
  162. AmpEnvelope->defaults();
  163. FreqEnvelope->defaults();
  164. BandWidthEnvelope->defaults();
  165. GlobalFilter->defaults();
  166. GlobalFilterEnvelope->defaults();
  167. }
  168. SUBnoteParameters::~SUBnoteParameters()
  169. {
  170. delete (AmpEnvelope);
  171. delete (FreqEnvelope);
  172. delete (BandWidthEnvelope);
  173. delete (GlobalFilter);
  174. delete (GlobalFilterEnvelope);
  175. }
  176. void SUBnoteParameters::add2XML(XMLwrapper *xml)
  177. {
  178. xml->addpar("num_stages", Pnumstages);
  179. xml->addpar("harmonic_mag_type", Phmagtype);
  180. xml->addpar("start", Pstart);
  181. xml->beginbranch("HARMONICS");
  182. for(int i = 0; i < MAX_SUB_HARMONICS; ++i) {
  183. if((Phmag[i] == 0) && (xml->minimal))
  184. continue;
  185. xml->beginbranch("HARMONIC", i);
  186. xml->addpar("mag", Phmag[i]);
  187. xml->addpar("relbw", Phrelbw[i]);
  188. xml->endbranch();
  189. }
  190. xml->endbranch();
  191. xml->beginbranch("AMPLITUDE_PARAMETERS");
  192. xml->addparbool("stereo", Pstereo);
  193. xml->addpar("volume", PVolume);
  194. xml->addpar("panning", PPanning);
  195. xml->addpar("velocity_sensing", PAmpVelocityScaleFunction);
  196. xml->beginbranch("AMPLITUDE_ENVELOPE");
  197. AmpEnvelope->add2XML(xml);
  198. xml->endbranch();
  199. xml->endbranch();
  200. xml->beginbranch("FREQUENCY_PARAMETERS");
  201. xml->addparbool("fixed_freq", Pfixedfreq);
  202. xml->addpar("fixed_freq_et", PfixedfreqET);
  203. xml->addpar("detune", PDetune);
  204. xml->addpar("coarse_detune", PCoarseDetune);
  205. xml->addpar("overtone_spread_type", POvertoneSpread.type);
  206. xml->addpar("overtone_spread_par1", POvertoneSpread.par1);
  207. xml->addpar("overtone_spread_par2", POvertoneSpread.par2);
  208. xml->addpar("overtone_spread_par3", POvertoneSpread.par3);
  209. xml->addpar("detune_type", PDetuneType);
  210. xml->addpar("bandwidth", Pbandwidth);
  211. xml->addpar("bandwidth_scale", Pbwscale);
  212. xml->addparbool("freq_envelope_enabled", PFreqEnvelopeEnabled);
  213. if((PFreqEnvelopeEnabled != 0) || (!xml->minimal)) {
  214. xml->beginbranch("FREQUENCY_ENVELOPE");
  215. FreqEnvelope->add2XML(xml);
  216. xml->endbranch();
  217. }
  218. xml->addparbool("band_width_envelope_enabled", PBandWidthEnvelopeEnabled);
  219. if((PBandWidthEnvelopeEnabled != 0) || (!xml->minimal)) {
  220. xml->beginbranch("BANDWIDTH_ENVELOPE");
  221. BandWidthEnvelope->add2XML(xml);
  222. xml->endbranch();
  223. }
  224. xml->endbranch();
  225. xml->beginbranch("FILTER_PARAMETERS");
  226. xml->addparbool("enabled", PGlobalFilterEnabled);
  227. if((PGlobalFilterEnabled != 0) || (!xml->minimal)) {
  228. xml->beginbranch("FILTER");
  229. GlobalFilter->add2XML(xml);
  230. xml->endbranch();
  231. xml->addpar("filter_velocity_sensing",
  232. PGlobalFilterVelocityScaleFunction);
  233. xml->addpar("filter_velocity_sensing_amplitude",
  234. PGlobalFilterVelocityScale);
  235. xml->beginbranch("FILTER_ENVELOPE");
  236. GlobalFilterEnvelope->add2XML(xml);
  237. xml->endbranch();
  238. }
  239. xml->endbranch();
  240. }
  241. void SUBnoteParameters::updateFrequencyMultipliers(void) {
  242. float par1 = POvertoneSpread.par1 / 255.0f;
  243. float par1pow = powf(10.0f,
  244. -(1.0f - POvertoneSpread.par1 / 255.0f) * 3.0f);
  245. float par2 = POvertoneSpread.par2 / 255.0f;
  246. float par3 = 1.0f - POvertoneSpread.par3 / 255.0f;
  247. float result;
  248. float tmp = 0.0f;
  249. int thresh = 0;
  250. for(int n = 0; n < MAX_SUB_HARMONICS; ++n) {
  251. float n1 = n + 1.0f;
  252. switch(POvertoneSpread.type) {
  253. case 1:
  254. thresh = (int)(100.0f * par2 * par2) + 1;
  255. if (n1 < thresh)
  256. result = n1;
  257. else
  258. result = n1 + 8.0f * (n1 - thresh) * par1pow;
  259. break;
  260. case 2:
  261. thresh = (int)(100.0f * par2 * par2) + 1;
  262. if (n1 < thresh)
  263. result = n1;
  264. else
  265. result = n1 + 0.9f * (thresh - n1) * par1pow;
  266. break;
  267. case 3:
  268. tmp = par1pow * 100.0f + 1.0f;
  269. result = powf(n / tmp, 1.0f - 0.8f * par2) * tmp + 1.0f;
  270. break;
  271. case 4:
  272. result = n * (1.0f - par1pow) +
  273. powf(0.1f * n, 3.0f * par2 + 1.0f) *
  274. 10.0f * par1pow + 1.0f;
  275. break;
  276. case 5:
  277. result = n1 + 2.0f * sinf(n * par2 * par2 * PI * 0.999f) *
  278. sqrt(par1pow);
  279. break;
  280. case 6:
  281. tmp = powf(2.0f * par2, 2.0f) + 0.1f;
  282. result = n * powf(par1 * powf(0.8f * n, tmp) + 1.0f, tmp) +
  283. 1.0f;
  284. break;
  285. case 7:
  286. result = (n1 + par1) / (par1 + 1);
  287. break;
  288. default:
  289. result = n1;
  290. }
  291. float iresult = floor(result + 0.5f);
  292. POvertoneFreqMult[n] = iresult + par3 * (result - iresult);
  293. }
  294. }
  295. #define doPaste(x) this->x = sub.x;
  296. #define doPPaste(x) this->x->paste(*sub.x);
  297. void SUBnoteParameters::paste(SUBnoteParameters &sub)
  298. {
  299. doPaste(Pstereo);
  300. doPaste(PVolume);
  301. doPaste(PPanning);
  302. doPaste(PAmpVelocityScaleFunction);
  303. doPPaste(AmpEnvelope);
  304. //Frequency Parameters
  305. doPaste(PDetune);
  306. doPaste(PCoarseDetune);
  307. doPaste(PDetuneType);
  308. doPaste(PFreqEnvelopeEnabled);
  309. doPPaste(FreqEnvelope);
  310. doPaste(PBandWidthEnvelopeEnabled);
  311. doPPaste(BandWidthEnvelope);
  312. //Filter Parameters (Global)
  313. doPaste(PGlobalFilterEnabled);
  314. doPPaste(GlobalFilter);
  315. doPaste(PGlobalFilterVelocityScale);
  316. doPaste(PGlobalFilterVelocityScaleFunction);
  317. doPPaste(GlobalFilterEnvelope);
  318. //Other Parameters
  319. doPaste(Pfixedfreq);
  320. doPaste(PfixedfreqET);
  321. doPaste(POvertoneSpread.type);
  322. doPaste(POvertoneSpread.par1);
  323. doPaste(POvertoneSpread.par2);
  324. doPaste(POvertoneSpread.par3);
  325. for(int i=0; i<MAX_SUB_HARMONICS; ++i)
  326. doPaste(POvertoneFreqMult[i]);
  327. doPaste(Pnumstages);
  328. doPaste(Pbandwidth);
  329. doPaste(Phmagtype);
  330. for(int i=0; i<MAX_SUB_HARMONICS; ++i) {
  331. doPaste(Phmag[i]);
  332. doPaste(Phrelbw[i]);
  333. }
  334. doPaste(Pbwscale);
  335. doPaste(Pstart);
  336. }
  337. void SUBnoteParameters::getfromXML(XMLwrapper *xml)
  338. {
  339. Pnumstages = xml->getpar127("num_stages", Pnumstages);
  340. Phmagtype = xml->getpar127("harmonic_mag_type", Phmagtype);
  341. Pstart = xml->getpar127("start", Pstart);
  342. if(xml->enterbranch("HARMONICS")) {
  343. Phmag[0] = 0;
  344. for(int i = 0; i < MAX_SUB_HARMONICS; ++i) {
  345. if(xml->enterbranch("HARMONIC", i) == 0)
  346. continue;
  347. Phmag[i] = xml->getpar127("mag", Phmag[i]);
  348. Phrelbw[i] = xml->getpar127("relbw", Phrelbw[i]);
  349. xml->exitbranch();
  350. }
  351. xml->exitbranch();
  352. }
  353. if(xml->enterbranch("AMPLITUDE_PARAMETERS")) {
  354. Pstereo = xml->getparbool("stereo", Pstereo);
  355. PVolume = xml->getpar127("volume", PVolume);
  356. PPanning = xml->getpar127("panning", PPanning);
  357. PAmpVelocityScaleFunction = xml->getpar127("velocity_sensing",
  358. PAmpVelocityScaleFunction);
  359. if(xml->enterbranch("AMPLITUDE_ENVELOPE")) {
  360. AmpEnvelope->getfromXML(xml);
  361. xml->exitbranch();
  362. }
  363. xml->exitbranch();
  364. }
  365. if(xml->enterbranch("FREQUENCY_PARAMETERS")) {
  366. Pfixedfreq = xml->getparbool("fixed_freq", Pfixedfreq);
  367. PfixedfreqET = xml->getpar127("fixed_freq_et", PfixedfreqET);
  368. PDetune = xml->getpar("detune", PDetune, 0, 16383);
  369. PCoarseDetune = xml->getpar("coarse_detune", PCoarseDetune, 0, 16383);
  370. POvertoneSpread.type =
  371. xml->getpar127("overtone_spread_type", POvertoneSpread.type);
  372. POvertoneSpread.par1 =
  373. xml->getpar("overtone_spread_par1", POvertoneSpread.par1, 0, 255);
  374. POvertoneSpread.par2 =
  375. xml->getpar("overtone_spread_par2", POvertoneSpread.par2, 0, 255);
  376. POvertoneSpread.par3 =
  377. xml->getpar("overtone_spread_par3", POvertoneSpread.par3, 0, 255);
  378. updateFrequencyMultipliers();
  379. PDetuneType = xml->getpar127("detune_type", PDetuneType);
  380. Pbandwidth = xml->getpar127("bandwidth", Pbandwidth);
  381. Pbwscale = xml->getpar127("bandwidth_scale", Pbwscale);
  382. PFreqEnvelopeEnabled = xml->getparbool("freq_envelope_enabled",
  383. PFreqEnvelopeEnabled);
  384. if(xml->enterbranch("FREQUENCY_ENVELOPE")) {
  385. FreqEnvelope->getfromXML(xml);
  386. xml->exitbranch();
  387. }
  388. PBandWidthEnvelopeEnabled = xml->getparbool(
  389. "band_width_envelope_enabled",
  390. PBandWidthEnvelopeEnabled);
  391. if(xml->enterbranch("BANDWIDTH_ENVELOPE")) {
  392. BandWidthEnvelope->getfromXML(xml);
  393. xml->exitbranch();
  394. }
  395. xml->exitbranch();
  396. }
  397. if(xml->enterbranch("FILTER_PARAMETERS")) {
  398. PGlobalFilterEnabled = xml->getparbool("enabled", PGlobalFilterEnabled);
  399. if(xml->enterbranch("FILTER")) {
  400. GlobalFilter->getfromXML(xml);
  401. xml->exitbranch();
  402. }
  403. PGlobalFilterVelocityScaleFunction = xml->getpar127(
  404. "filter_velocity_sensing",
  405. PGlobalFilterVelocityScaleFunction);
  406. PGlobalFilterVelocityScale = xml->getpar127(
  407. "filter_velocity_sensing_amplitude",
  408. PGlobalFilterVelocityScale);
  409. if(xml->enterbranch("FILTER_ENVELOPE")) {
  410. GlobalFilterEnvelope->getfromXML(xml);
  411. xml->exitbranch();
  412. }
  413. xml->exitbranch();
  414. }
  415. }