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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 <stdio.h>
  20. #include <cmath>
  21. SUBnoteParameters::SUBnoteParameters():Presets()
  22. {
  23. setpresettype("Psubsynth");
  24. AmpEnvelope = new EnvelopeParams(64, 1);
  25. AmpEnvelope->ADSRinit_dB(0, 40, 127, 25);
  26. FreqEnvelope = new EnvelopeParams(64, 0);
  27. FreqEnvelope->ASRinit(30, 50, 64, 60);
  28. BandWidthEnvelope = new EnvelopeParams(64, 0);
  29. BandWidthEnvelope->ASRinit_bw(100, 70, 64, 60);
  30. GlobalFilter = new FilterParams(2, 80, 40);
  31. GlobalFilterEnvelope = new EnvelopeParams(0, 1);
  32. GlobalFilterEnvelope->ADSRinit_filter(64, 40, 64, 70, 60, 64);
  33. defaults();
  34. }
  35. void SUBnoteParameters::defaults()
  36. {
  37. PVolume = 96;
  38. PPanning = 64;
  39. PAmpVelocityScaleFunction = 90;
  40. Pfixedfreq = 0;
  41. PfixedfreqET = 0;
  42. Pnumstages = 2;
  43. Pbandwidth = 40;
  44. Phmagtype = 0;
  45. Pbwscale = 64;
  46. Pstereo = 1;
  47. Pstart = 1;
  48. PDetune = 8192;
  49. PCoarseDetune = 0;
  50. PDetuneType = 1;
  51. PFreqEnvelopeEnabled = 0;
  52. PBandWidthEnvelopeEnabled = 0;
  53. POvertoneSpread.type = 0;
  54. POvertoneSpread.par1 = 0;
  55. POvertoneSpread.par2 = 0;
  56. POvertoneSpread.par3 = 0;
  57. updateFrequencyMultipliers();
  58. for(int n = 0; n < MAX_SUB_HARMONICS; ++n) {
  59. Phmag[n] = 0;
  60. Phrelbw[n] = 64;
  61. }
  62. Phmag[0] = 127;
  63. PGlobalFilterEnabled = 0;
  64. PGlobalFilterVelocityScale = 64;
  65. PGlobalFilterVelocityScaleFunction = 64;
  66. AmpEnvelope->defaults();
  67. FreqEnvelope->defaults();
  68. BandWidthEnvelope->defaults();
  69. GlobalFilter->defaults();
  70. GlobalFilterEnvelope->defaults();
  71. }
  72. SUBnoteParameters::~SUBnoteParameters()
  73. {
  74. delete (AmpEnvelope);
  75. delete (FreqEnvelope);
  76. delete (BandWidthEnvelope);
  77. delete (GlobalFilter);
  78. delete (GlobalFilterEnvelope);
  79. }
  80. void SUBnoteParameters::add2XML(XMLwrapper *xml)
  81. {
  82. xml->addpar("num_stages", Pnumstages);
  83. xml->addpar("harmonic_mag_type", Phmagtype);
  84. xml->addpar("start", Pstart);
  85. xml->beginbranch("HARMONICS");
  86. for(int i = 0; i < MAX_SUB_HARMONICS; ++i) {
  87. if((Phmag[i] == 0) && (xml->minimal))
  88. continue;
  89. xml->beginbranch("HARMONIC", i);
  90. xml->addpar("mag", Phmag[i]);
  91. xml->addpar("relbw", Phrelbw[i]);
  92. xml->endbranch();
  93. }
  94. xml->endbranch();
  95. xml->beginbranch("AMPLITUDE_PARAMETERS");
  96. xml->addparbool("stereo", Pstereo);
  97. xml->addpar("volume", PVolume);
  98. xml->addpar("panning", PPanning);
  99. xml->addpar("velocity_sensing", PAmpVelocityScaleFunction);
  100. xml->beginbranch("AMPLITUDE_ENVELOPE");
  101. AmpEnvelope->add2XML(xml);
  102. xml->endbranch();
  103. xml->endbranch();
  104. xml->beginbranch("FREQUENCY_PARAMETERS");
  105. xml->addparbool("fixed_freq", Pfixedfreq);
  106. xml->addpar("fixed_freq_et", PfixedfreqET);
  107. xml->addpar("detune", PDetune);
  108. xml->addpar("coarse_detune", PCoarseDetune);
  109. xml->addpar("overtone_spread_type", POvertoneSpread.type);
  110. xml->addpar("overtone_spread_par1", POvertoneSpread.par1);
  111. xml->addpar("overtone_spread_par2", POvertoneSpread.par2);
  112. xml->addpar("overtone_spread_par3", POvertoneSpread.par3);
  113. xml->addpar("detune_type", PDetuneType);
  114. xml->addpar("bandwidth", Pbandwidth);
  115. xml->addpar("bandwidth_scale", Pbwscale);
  116. xml->addparbool("freq_envelope_enabled", PFreqEnvelopeEnabled);
  117. if((PFreqEnvelopeEnabled != 0) || (!xml->minimal)) {
  118. xml->beginbranch("FREQUENCY_ENVELOPE");
  119. FreqEnvelope->add2XML(xml);
  120. xml->endbranch();
  121. }
  122. xml->addparbool("band_width_envelope_enabled", PBandWidthEnvelopeEnabled);
  123. if((PBandWidthEnvelopeEnabled != 0) || (!xml->minimal)) {
  124. xml->beginbranch("BANDWIDTH_ENVELOPE");
  125. BandWidthEnvelope->add2XML(xml);
  126. xml->endbranch();
  127. }
  128. xml->endbranch();
  129. xml->beginbranch("FILTER_PARAMETERS");
  130. xml->addparbool("enabled", PGlobalFilterEnabled);
  131. if((PGlobalFilterEnabled != 0) || (!xml->minimal)) {
  132. xml->beginbranch("FILTER");
  133. GlobalFilter->add2XML(xml);
  134. xml->endbranch();
  135. xml->addpar("filter_velocity_sensing",
  136. PGlobalFilterVelocityScaleFunction);
  137. xml->addpar("filter_velocity_sensing_amplitude",
  138. PGlobalFilterVelocityScale);
  139. xml->beginbranch("FILTER_ENVELOPE");
  140. GlobalFilterEnvelope->add2XML(xml);
  141. xml->endbranch();
  142. }
  143. xml->endbranch();
  144. }
  145. void SUBnoteParameters::updateFrequencyMultipliers(void) {
  146. float par1 = POvertoneSpread.par1 / 255.0f;
  147. float par1pow = powf(10.0f,
  148. -(1.0f - POvertoneSpread.par1 / 255.0f) * 3.0f);
  149. float par2 = POvertoneSpread.par2 / 255.0f;
  150. float par3 = 1.0f - POvertoneSpread.par3 / 255.0f;
  151. float result;
  152. float tmp = 0.0f;
  153. int thresh = 0;
  154. for(int n = 0; n < MAX_SUB_HARMONICS; ++n) {
  155. float n1 = n + 1.0f;
  156. switch(POvertoneSpread.type) {
  157. case 1:
  158. thresh = (int)(100.0f * par2 * par2) + 1;
  159. if (n1 < thresh)
  160. result = n1;
  161. else
  162. result = n1 + 8.0f * (n1 - thresh) * par1pow;
  163. break;
  164. case 2:
  165. thresh = (int)(100.0f * par2 * par2) + 1;
  166. if (n1 < thresh)
  167. result = n1;
  168. else
  169. result = n1 + 0.9f * (thresh - n1) * par1pow;
  170. break;
  171. case 3:
  172. tmp = par1pow * 100.0f + 1.0f;
  173. result = powf(n / tmp, 1.0f - 0.8f * par2) * tmp + 1.0f;
  174. break;
  175. case 4:
  176. result = n * (1.0f - par1pow) +
  177. powf(0.1f * n, 3.0f * par2 + 1.0f) *
  178. 10.0f * par1pow + 1.0f;
  179. break;
  180. case 5:
  181. result = n1 + 2.0f * sinf(n * par2 * par2 * PI * 0.999f) *
  182. sqrt(par1pow);
  183. break;
  184. case 6:
  185. tmp = powf(2.0f * par2, 2.0f) + 0.1f;
  186. result = n * powf(par1 * powf(0.8f * n, tmp) + 1.0f, tmp) +
  187. 1.0f;
  188. break;
  189. case 7:
  190. result = (n1 + par1) / (par1 + 1);
  191. break;
  192. default:
  193. result = n1;
  194. }
  195. float iresult = floor(result + 0.5f);
  196. POvertoneFreqMult[n] = iresult + par3 * (result - iresult);
  197. }
  198. }
  199. void SUBnoteParameters::getfromXML(XMLwrapper *xml)
  200. {
  201. Pnumstages = xml->getpar127("num_stages", Pnumstages);
  202. Phmagtype = xml->getpar127("harmonic_mag_type", Phmagtype);
  203. Pstart = xml->getpar127("start", Pstart);
  204. if(xml->enterbranch("HARMONICS")) {
  205. Phmag[0] = 0;
  206. for(int i = 0; i < MAX_SUB_HARMONICS; ++i) {
  207. if(xml->enterbranch("HARMONIC", i) == 0)
  208. continue;
  209. Phmag[i] = xml->getpar127("mag", Phmag[i]);
  210. Phrelbw[i] = xml->getpar127("relbw", Phrelbw[i]);
  211. xml->exitbranch();
  212. }
  213. xml->exitbranch();
  214. }
  215. if(xml->enterbranch("AMPLITUDE_PARAMETERS")) {
  216. Pstereo = xml->getparbool("stereo", Pstereo);
  217. PVolume = xml->getpar127("volume", PVolume);
  218. PPanning = xml->getpar127("panning", PPanning);
  219. PAmpVelocityScaleFunction = xml->getpar127("velocity_sensing",
  220. PAmpVelocityScaleFunction);
  221. if(xml->enterbranch("AMPLITUDE_ENVELOPE")) {
  222. AmpEnvelope->getfromXML(xml);
  223. xml->exitbranch();
  224. }
  225. xml->exitbranch();
  226. }
  227. if(xml->enterbranch("FREQUENCY_PARAMETERS")) {
  228. Pfixedfreq = xml->getparbool("fixed_freq", Pfixedfreq);
  229. PfixedfreqET = xml->getpar127("fixed_freq_et", PfixedfreqET);
  230. PDetune = xml->getpar("detune", PDetune, 0, 16383);
  231. PCoarseDetune = xml->getpar("coarse_detune", PCoarseDetune, 0, 16383);
  232. POvertoneSpread.type =
  233. xml->getpar127("overtone_spread_type", POvertoneSpread.type);
  234. POvertoneSpread.par1 =
  235. xml->getpar("overtone_spread_par1", POvertoneSpread.par1, 0, 255);
  236. POvertoneSpread.par2 =
  237. xml->getpar("overtone_spread_par2", POvertoneSpread.par2, 0, 255);
  238. POvertoneSpread.par3 =
  239. xml->getpar("overtone_spread_par3", POvertoneSpread.par3, 0, 255);
  240. updateFrequencyMultipliers();
  241. PDetuneType = xml->getpar127("detune_type", PDetuneType);
  242. Pbandwidth = xml->getpar127("bandwidth", Pbandwidth);
  243. Pbwscale = xml->getpar127("bandwidth_scale", Pbwscale);
  244. PFreqEnvelopeEnabled = xml->getparbool("freq_envelope_enabled",
  245. PFreqEnvelopeEnabled);
  246. if(xml->enterbranch("FREQUENCY_ENVELOPE")) {
  247. FreqEnvelope->getfromXML(xml);
  248. xml->exitbranch();
  249. }
  250. PBandWidthEnvelopeEnabled = xml->getparbool(
  251. "band_width_envelope_enabled",
  252. PBandWidthEnvelopeEnabled);
  253. if(xml->enterbranch("BANDWIDTH_ENVELOPE")) {
  254. BandWidthEnvelope->getfromXML(xml);
  255. xml->exitbranch();
  256. }
  257. xml->exitbranch();
  258. }
  259. if(xml->enterbranch("FILTER_PARAMETERS")) {
  260. PGlobalFilterEnabled = xml->getparbool("enabled", PGlobalFilterEnabled);
  261. if(xml->enterbranch("FILTER")) {
  262. GlobalFilter->getfromXML(xml);
  263. xml->exitbranch();
  264. }
  265. PGlobalFilterVelocityScaleFunction = xml->getpar127(
  266. "filter_velocity_sensing",
  267. PGlobalFilterVelocityScaleFunction);
  268. PGlobalFilterVelocityScale = xml->getpar127(
  269. "filter_velocity_sensing_amplitude",
  270. PGlobalFilterVelocityScale);
  271. if(xml->enterbranch("FILTER_ENVELOPE")) {
  272. GlobalFilterEnvelope->getfromXML(xml);
  273. xml->exitbranch();
  274. }
  275. xml->exitbranch();
  276. }
  277. }