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.

SUBnoteParameters.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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
  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 "../globals.h"
  12. #include "SUBnoteParameters.h"
  13. #include "EnvelopeParams.h"
  14. #include "FilterParams.h"
  15. #include "../Misc/Util.h"
  16. #include "../Misc/Time.h"
  17. #include <cstdio>
  18. #include <cmath>
  19. #include <rtosc/ports.h>
  20. #include <rtosc/port-sugar.h>
  21. #define rObject SUBnoteParameters
  22. using namespace rtosc;
  23. #undef rChangeCb
  24. #define rChangeCb if (obj->time) { obj->last_update_timestamp = obj->time->time(); }
  25. static const rtosc::Ports SUBnotePorts = {
  26. rSelf(SUBnoteParameters),
  27. rPaste,
  28. rToggle(Pstereo, "Stereo Enable"),
  29. rParamZyn(PVolume, "Volume"),
  30. rParamZyn(PPanning, "Left Right Panning"),
  31. rParamZyn(PAmpVelocityScaleFunction, "Amplitude Velocity Sensing function"),
  32. rParamI(PDetune, "Detune in detune type units"),
  33. rParamI(PCoarseDetune, "Coarse Detune"),
  34. //Real values needed
  35. rOption(PDetuneType, rOptions("100 cents", "200 cents", "500 cents"), "Detune Scale"),
  36. rToggle(PFreqEnvelopeEnabled, "Enable for Frequency Envelope"),
  37. rToggle(PBandWidthEnvelopeEnabled, "Enable for Bandwidth Envelope"),
  38. rToggle(PGlobalFilterEnabled, "Enable for Global Filter"),
  39. rParamZyn(PGlobalFilterVelocityScale, "Filter Velocity Magnitude"),
  40. rParamZyn(PGlobalFilterVelocityScaleFunction, "Filter Velocity Function Shape"),
  41. //rRecur(FreqEnvelope, EnvelopeParams),
  42. //rToggle(),//continue
  43. rToggle(Pfixedfreq, "Base frequency fixed frequency enable"),
  44. rParamZyn(PfixedfreqET, "Equal temeperate control for fixed frequency operation"),
  45. rParamZyn(PBendAdjust, "Pitch bend adjustment"),
  46. rParamZyn(POffsetHz, "Voice constant offset"),
  47. #undef rChangeCb
  48. #define rChangeCb obj->updateFrequencyMultipliers(); if (obj->time) { \
  49. obj->last_update_timestamp = obj->time->time(); }
  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 if (obj->time) { obj->last_update_timestamp = obj->time->time(); }
  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. #undef rChangeCb
  117. const rtosc::Ports &SUBnoteParameters::ports = SUBnotePorts;
  118. SUBnoteParameters::SUBnoteParameters(const AbsTime *time_)
  119. : Presets(), time(time_), last_update_timestamp(0)
  120. {
  121. setpresettype("Psubsynth");
  122. AmpEnvelope = new EnvelopeParams(64, 1, time_);
  123. AmpEnvelope->ADSRinit_dB(0, 40, 127, 25);
  124. FreqEnvelope = new EnvelopeParams(64, 0, time_);
  125. FreqEnvelope->ASRinit(30, 50, 64, 60);
  126. BandWidthEnvelope = new EnvelopeParams(64, 0, time_);
  127. BandWidthEnvelope->ASRinit_bw(100, 70, 64, 60);
  128. GlobalFilter = new FilterParams(2, 80, 40, time_);
  129. GlobalFilterEnvelope = new EnvelopeParams(0, 1, time_);
  130. GlobalFilterEnvelope->ADSRinit_filter(64, 40, 64, 70, 60, 64);
  131. defaults();
  132. }
  133. void SUBnoteParameters::defaults()
  134. {
  135. PVolume = 96;
  136. PPanning = 64;
  137. PAmpVelocityScaleFunction = 90;
  138. Pfixedfreq = 0;
  139. PfixedfreqET = 0;
  140. PBendAdjust = 88; // 64 + 24
  141. POffsetHz = 64;
  142. Pnumstages = 2;
  143. Pbandwidth = 40;
  144. Phmagtype = 0;
  145. Pbwscale = 64;
  146. Pstereo = 1;
  147. Pstart = 1;
  148. PDetune = 8192;
  149. PCoarseDetune = 0;
  150. PDetuneType = 1;
  151. PFreqEnvelopeEnabled = 0;
  152. PBandWidthEnvelopeEnabled = 0;
  153. POvertoneSpread.type = 0;
  154. POvertoneSpread.par1 = 0;
  155. POvertoneSpread.par2 = 0;
  156. POvertoneSpread.par3 = 0;
  157. updateFrequencyMultipliers();
  158. for(int n = 0; n < MAX_SUB_HARMONICS; ++n) {
  159. Phmag[n] = 0;
  160. Phrelbw[n] = 64;
  161. }
  162. Phmag[0] = 127;
  163. PGlobalFilterEnabled = 0;
  164. PGlobalFilterVelocityScale = 64;
  165. PGlobalFilterVelocityScaleFunction = 64;
  166. AmpEnvelope->defaults();
  167. FreqEnvelope->defaults();
  168. BandWidthEnvelope->defaults();
  169. GlobalFilter->defaults();
  170. GlobalFilterEnvelope->defaults();
  171. }
  172. SUBnoteParameters::~SUBnoteParameters()
  173. {
  174. delete (AmpEnvelope);
  175. delete (FreqEnvelope);
  176. delete (BandWidthEnvelope);
  177. delete (GlobalFilter);
  178. delete (GlobalFilterEnvelope);
  179. }
  180. void SUBnoteParameters::add2XML(XMLwrapper& xml)
  181. {
  182. xml.addpar("num_stages", Pnumstages);
  183. xml.addpar("harmonic_mag_type", Phmagtype);
  184. xml.addpar("start", Pstart);
  185. xml.beginbranch("HARMONICS");
  186. for(int i = 0; i < MAX_SUB_HARMONICS; ++i) {
  187. if((Phmag[i] == 0) && (xml.minimal))
  188. continue;
  189. xml.beginbranch("HARMONIC", i);
  190. xml.addpar("mag", Phmag[i]);
  191. xml.addpar("relbw", Phrelbw[i]);
  192. xml.endbranch();
  193. }
  194. xml.endbranch();
  195. xml.beginbranch("AMPLITUDE_PARAMETERS");
  196. xml.addparbool("stereo", Pstereo);
  197. xml.addpar("volume", PVolume);
  198. xml.addpar("panning", PPanning);
  199. xml.addpar("velocity_sensing", PAmpVelocityScaleFunction);
  200. xml.beginbranch("AMPLITUDE_ENVELOPE");
  201. AmpEnvelope->add2XML(xml);
  202. xml.endbranch();
  203. xml.endbranch();
  204. xml.beginbranch("FREQUENCY_PARAMETERS");
  205. xml.addparbool("fixed_freq", Pfixedfreq);
  206. xml.addpar("fixed_freq_et", PfixedfreqET);
  207. xml.addpar("bend_adjust", PBendAdjust);
  208. xml.addpar("offset_hz", POffsetHz);
  209. xml.addpar("detune", PDetune);
  210. xml.addpar("coarse_detune", PCoarseDetune);
  211. xml.addpar("overtone_spread_type", POvertoneSpread.type);
  212. xml.addpar("overtone_spread_par1", POvertoneSpread.par1);
  213. xml.addpar("overtone_spread_par2", POvertoneSpread.par2);
  214. xml.addpar("overtone_spread_par3", POvertoneSpread.par3);
  215. xml.addpar("detune_type", PDetuneType);
  216. xml.addpar("bandwidth", Pbandwidth);
  217. xml.addpar("bandwidth_scale", Pbwscale);
  218. xml.addparbool("freq_envelope_enabled", PFreqEnvelopeEnabled);
  219. if((PFreqEnvelopeEnabled != 0) || (!xml.minimal)) {
  220. xml.beginbranch("FREQUENCY_ENVELOPE");
  221. FreqEnvelope->add2XML(xml);
  222. xml.endbranch();
  223. }
  224. xml.addparbool("band_width_envelope_enabled", PBandWidthEnvelopeEnabled);
  225. if((PBandWidthEnvelopeEnabled != 0) || (!xml.minimal)) {
  226. xml.beginbranch("BANDWIDTH_ENVELOPE");
  227. BandWidthEnvelope->add2XML(xml);
  228. xml.endbranch();
  229. }
  230. xml.endbranch();
  231. xml.beginbranch("FILTER_PARAMETERS");
  232. xml.addparbool("enabled", PGlobalFilterEnabled);
  233. if((PGlobalFilterEnabled != 0) || (!xml.minimal)) {
  234. xml.beginbranch("FILTER");
  235. GlobalFilter->add2XML(xml);
  236. xml.endbranch();
  237. xml.addpar("filter_velocity_sensing",
  238. PGlobalFilterVelocityScaleFunction);
  239. xml.addpar("filter_velocity_sensing_amplitude",
  240. PGlobalFilterVelocityScale);
  241. xml.beginbranch("FILTER_ENVELOPE");
  242. GlobalFilterEnvelope->add2XML(xml);
  243. xml.endbranch();
  244. }
  245. xml.endbranch();
  246. }
  247. void SUBnoteParameters::updateFrequencyMultipliers(void) {
  248. float par1 = POvertoneSpread.par1 / 255.0f;
  249. float par1pow = powf(10.0f,
  250. -(1.0f - POvertoneSpread.par1 / 255.0f) * 3.0f);
  251. float par2 = POvertoneSpread.par2 / 255.0f;
  252. float par3 = 1.0f - POvertoneSpread.par3 / 255.0f;
  253. float result;
  254. float tmp = 0.0f;
  255. int thresh = 0;
  256. for(int n = 0; n < MAX_SUB_HARMONICS; ++n) {
  257. float n1 = n + 1.0f;
  258. switch(POvertoneSpread.type) {
  259. case 1:
  260. thresh = (int)(100.0f * par2 * par2) + 1;
  261. if (n1 < thresh)
  262. result = n1;
  263. else
  264. result = n1 + 8.0f * (n1 - thresh) * par1pow;
  265. break;
  266. case 2:
  267. thresh = (int)(100.0f * par2 * par2) + 1;
  268. if (n1 < thresh)
  269. result = n1;
  270. else
  271. result = n1 + 0.9f * (thresh - n1) * par1pow;
  272. break;
  273. case 3:
  274. tmp = par1pow * 100.0f + 1.0f;
  275. result = powf(n / tmp, 1.0f - 0.8f * par2) * tmp + 1.0f;
  276. break;
  277. case 4:
  278. result = n * (1.0f - par1pow) +
  279. powf(0.1f * n, 3.0f * par2 + 1.0f) *
  280. 10.0f * par1pow + 1.0f;
  281. break;
  282. case 5:
  283. result = n1 + 2.0f * sinf(n * par2 * par2 * PI * 0.999f) *
  284. sqrt(par1pow);
  285. break;
  286. case 6:
  287. tmp = powf(2.0f * par2, 2.0f) + 0.1f;
  288. result = n * powf(par1 * powf(0.8f * n, tmp) + 1.0f, tmp) +
  289. 1.0f;
  290. break;
  291. case 7:
  292. result = (n1 + par1) / (par1 + 1);
  293. break;
  294. default:
  295. result = n1;
  296. }
  297. float iresult = floor(result + 0.5f);
  298. POvertoneFreqMult[n] = iresult + par3 * (result - iresult);
  299. }
  300. }
  301. #define doPaste(x) this->x = sub.x;
  302. #define doPPaste(x) this->x->paste(*sub.x);
  303. void SUBnoteParameters::paste(SUBnoteParameters &sub)
  304. {
  305. doPaste(Pstereo);
  306. doPaste(PVolume);
  307. doPaste(PPanning);
  308. doPaste(PAmpVelocityScaleFunction);
  309. doPPaste(AmpEnvelope);
  310. //Frequency Parameters
  311. doPaste(PDetune);
  312. doPaste(PCoarseDetune);
  313. doPaste(PDetuneType);
  314. doPaste(PBendAdjust);
  315. doPaste(POffsetHz);
  316. doPaste(PFreqEnvelopeEnabled);
  317. doPPaste(FreqEnvelope);
  318. doPaste(PBandWidthEnvelopeEnabled);
  319. doPPaste(BandWidthEnvelope);
  320. //Filter Parameters (Global)
  321. doPaste(PGlobalFilterEnabled);
  322. doPPaste(GlobalFilter);
  323. doPaste(PGlobalFilterVelocityScale);
  324. doPaste(PGlobalFilterVelocityScaleFunction);
  325. doPPaste(GlobalFilterEnvelope);
  326. //Other Parameters
  327. doPaste(Pfixedfreq);
  328. doPaste(PfixedfreqET);
  329. doPaste(POvertoneSpread.type);
  330. doPaste(POvertoneSpread.par1);
  331. doPaste(POvertoneSpread.par2);
  332. doPaste(POvertoneSpread.par3);
  333. for(int i=0; i<MAX_SUB_HARMONICS; ++i)
  334. doPaste(POvertoneFreqMult[i]);
  335. doPaste(Pnumstages);
  336. doPaste(Pbandwidth);
  337. doPaste(Phmagtype);
  338. for(int i=0; i<MAX_SUB_HARMONICS; ++i) {
  339. doPaste(Phmag[i]);
  340. doPaste(Phrelbw[i]);
  341. }
  342. doPaste(Pbwscale);
  343. doPaste(Pstart);
  344. if ( time ) {
  345. last_update_timestamp = time->time();
  346. }
  347. }
  348. void SUBnoteParameters::getfromXML(XMLwrapper& xml)
  349. {
  350. Pnumstages = xml.getpar127("num_stages", Pnumstages);
  351. Phmagtype = xml.getpar127("harmonic_mag_type", Phmagtype);
  352. Pstart = xml.getpar127("start", Pstart);
  353. if(xml.enterbranch("HARMONICS")) {
  354. Phmag[0] = 0;
  355. for(int i = 0; i < MAX_SUB_HARMONICS; ++i) {
  356. if(xml.enterbranch("HARMONIC", i) == 0)
  357. continue;
  358. Phmag[i] = xml.getpar127("mag", Phmag[i]);
  359. Phrelbw[i] = xml.getpar127("relbw", Phrelbw[i]);
  360. xml.exitbranch();
  361. }
  362. xml.exitbranch();
  363. }
  364. if(xml.enterbranch("AMPLITUDE_PARAMETERS")) {
  365. Pstereo = xml.getparbool("stereo", Pstereo);
  366. PVolume = xml.getpar127("volume", PVolume);
  367. PPanning = xml.getpar127("panning", PPanning);
  368. PAmpVelocityScaleFunction = xml.getpar127("velocity_sensing",
  369. PAmpVelocityScaleFunction);
  370. if(xml.enterbranch("AMPLITUDE_ENVELOPE")) {
  371. AmpEnvelope->getfromXML(xml);
  372. xml.exitbranch();
  373. }
  374. xml.exitbranch();
  375. }
  376. if(xml.enterbranch("FREQUENCY_PARAMETERS")) {
  377. Pfixedfreq = xml.getparbool("fixed_freq", Pfixedfreq);
  378. PfixedfreqET = xml.getpar127("fixed_freq_et", PfixedfreqET);
  379. PBendAdjust = xml.getpar127("bend_adjust", PBendAdjust);
  380. POffsetHz = xml.getpar127("offset_hz", POffsetHz);
  381. PDetune = xml.getpar("detune", PDetune, 0, 16383);
  382. PCoarseDetune = xml.getpar("coarse_detune", PCoarseDetune, 0, 16383);
  383. POvertoneSpread.type =
  384. xml.getpar127("overtone_spread_type", POvertoneSpread.type);
  385. POvertoneSpread.par1 =
  386. xml.getpar("overtone_spread_par1", POvertoneSpread.par1, 0, 255);
  387. POvertoneSpread.par2 =
  388. xml.getpar("overtone_spread_par2", POvertoneSpread.par2, 0, 255);
  389. POvertoneSpread.par3 =
  390. xml.getpar("overtone_spread_par3", POvertoneSpread.par3, 0, 255);
  391. updateFrequencyMultipliers();
  392. PDetuneType = xml.getpar127("detune_type", PDetuneType);
  393. Pbandwidth = xml.getpar127("bandwidth", Pbandwidth);
  394. Pbwscale = xml.getpar127("bandwidth_scale", Pbwscale);
  395. PFreqEnvelopeEnabled = xml.getparbool("freq_envelope_enabled",
  396. PFreqEnvelopeEnabled);
  397. if(xml.enterbranch("FREQUENCY_ENVELOPE")) {
  398. FreqEnvelope->getfromXML(xml);
  399. xml.exitbranch();
  400. }
  401. PBandWidthEnvelopeEnabled = xml.getparbool(
  402. "band_width_envelope_enabled",
  403. PBandWidthEnvelopeEnabled);
  404. if(xml.enterbranch("BANDWIDTH_ENVELOPE")) {
  405. BandWidthEnvelope->getfromXML(xml);
  406. xml.exitbranch();
  407. }
  408. xml.exitbranch();
  409. }
  410. if(xml.enterbranch("FILTER_PARAMETERS")) {
  411. PGlobalFilterEnabled = xml.getparbool("enabled", PGlobalFilterEnabled);
  412. if(xml.enterbranch("FILTER")) {
  413. GlobalFilter->getfromXML(xml);
  414. xml.exitbranch();
  415. }
  416. PGlobalFilterVelocityScaleFunction = xml.getpar127(
  417. "filter_velocity_sensing",
  418. PGlobalFilterVelocityScaleFunction);
  419. PGlobalFilterVelocityScale = xml.getpar127(
  420. "filter_velocity_sensing_amplitude",
  421. PGlobalFilterVelocityScale);
  422. if(xml.enterbranch("FILTER_ENVELOPE")) {
  423. GlobalFilterEnvelope->getfromXML(xml);
  424. xml.exitbranch();
  425. }
  426. xml.exitbranch();
  427. }
  428. }