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.

504 lines
17KB

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