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.

1121 lines
37KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. PADnoteParameters.cpp - Parameters for PADnote (PADsynth)
  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 <cmath>
  18. #include "PADnoteParameters.h"
  19. #include "FilterParams.h"
  20. #include "EnvelopeParams.h"
  21. #include "LFOParams.h"
  22. #include "../Synth/Resonance.h"
  23. #include "../Synth/OscilGen.h"
  24. #include "../Misc/WavFile.h"
  25. #include <cstdio>
  26. #include <rtosc/ports.h>
  27. #include <rtosc/port-sugar.h>
  28. using namespace rtosc;
  29. #define rObject PADnoteParameters
  30. static const rtosc::Ports realtime_ports =
  31. {
  32. rRecurp(FreqLfo, "Frequency LFO"),
  33. rRecurp(AmpLfo, "Amplitude LFO"),
  34. rRecurp(FilterLfo, "Filter LFO"),
  35. rRecurp(FreqEnvelope, "Frequency Envelope"),
  36. rRecurp(AmpEnvelope, "Amplitude Envelope"),
  37. rRecurp(FilterEnvelope, "Filter Envelope"),
  38. rRecurp(GlobalFilter, "Post Filter"),
  39. //Volume
  40. rToggle(PStereo, "Stereo/Mono Mode"),
  41. rParamZyn(PPanning, "Left Right Panning"),
  42. rParamZyn(PVolume, "Synth Volume"),
  43. rParamZyn(PAmpVelocityScaleFunction, "Amplitude Velocity Sensing function"),
  44. rParamZyn(Fadein_adjustment, "Adjustment for anti-pop strategy."),
  45. //Punch
  46. rParamZyn(PPunchStrength, "Punch Strength"),
  47. rParamZyn(PPunchTime, "UNKNOWN"),
  48. rParamZyn(PPunchStretch, "How Punch changes with note frequency"),
  49. rParamZyn(PPunchVelocitySensing, "Punch Velocity control"),
  50. //Filter
  51. rParamZyn(PFilterVelocityScale, "Filter Velocity Magnitude"),
  52. rParamZyn(PFilterVelocityScaleFunction, "Filter Velocity Function Shape"),
  53. //Freq
  54. rToggle(Pfixedfreq, "Base frequency fixed frequency enable"),
  55. rParamZyn(PfixedfreqET, "Equal temeperate control for fixed frequency operation"),
  56. rParamI(PDetune, "Fine Detune"),
  57. rParamI(PCoarseDetune, "Coarse Detune"),
  58. rParamZyn(PDetuneType, "Magnitude of Detune"),
  59. {"sample#64:ifb", rProp(internal) rDoc("Nothing to see here"), 0,
  60. [](const char *m, rtosc::RtData &d)
  61. {
  62. PADnoteParameters *p = (PADnoteParameters*)d.obj;
  63. const char *mm = m;
  64. while(!isdigit(*mm))++mm;
  65. unsigned n = atoi(mm);
  66. p->sample[n].size = rtosc_argument(m,0).i;
  67. p->sample[n].basefreq = rtosc_argument(m,1).f;
  68. p->sample[n].smp = *(float**)rtosc_argument(m,2).b.data;
  69. //XXX TODO memory managment (deallocation of smp buffer)
  70. }},
  71. //weird stuff for PCoarseDetune
  72. {"detunevalue:", rMap(unit,cents) rDoc("Get detune value"), NULL,
  73. [](const char *, RtData &d)
  74. {
  75. PADnoteParameters *obj = (PADnoteParameters *)d.obj;
  76. d.reply(d.loc, "f", getdetune(obj->PDetuneType, 0, obj->PDetune));
  77. }},
  78. {"octave::c:i", rProp(parameter) rDoc("Octave note offset"), NULL,
  79. [](const char *msg, RtData &d)
  80. {
  81. PADnoteParameters *obj = (PADnoteParameters *)d.obj;
  82. if(!rtosc_narguments(msg)) {
  83. int k=obj->PCoarseDetune/1024;
  84. if (k>=8) k-=16;
  85. d.reply(d.loc, "i", k);
  86. } else {
  87. int k=(int) rtosc_argument(msg, 0).i;
  88. if (k<0) k+=16;
  89. obj->PCoarseDetune = k*1024 + obj->PCoarseDetune%1024;
  90. }
  91. }},
  92. {"coarsedetune::c:i", rProp(parameter) rDoc("Coarse note detune"), NULL,
  93. [](const char *msg, RtData &d)
  94. {
  95. PADnoteParameters *obj = (PADnoteParameters *)d.obj;
  96. if(!rtosc_narguments(msg)) {
  97. int k=obj->PCoarseDetune%1024;
  98. if (k>=512) k-=1024;
  99. d.reply(d.loc, "i", k);
  100. } else {
  101. int k=(int) rtosc_argument(msg, 0).i;
  102. if (k<0) k+=1024;
  103. obj->PCoarseDetune = k + (obj->PCoarseDetune/1024)*1024;
  104. }
  105. }},
  106. };
  107. static const rtosc::Ports non_realtime_ports =
  108. {
  109. //Harmonic Source Distribution
  110. rRecurp(oscilgen, "Oscillator"),
  111. rRecurp(resonance, "Resonance"),
  112. //Harmonic Shape
  113. rOption(Pmode, rMap(min, 0), rMap(max, 2), rOptions(bandwidth,discrete,continious),
  114. "Harmonic Distribution Model"),
  115. rOption(Php.base.type, rOptions(Gaussian, Rectanglar, Double Exponential),
  116. "Harmonic profile shape"),
  117. rParamZyn(Php.base.par1, "Harmonic shape distribution parameter"),
  118. rParamZyn(Php.freqmult, "Frequency multiplier on distribution"),
  119. rParamZyn(Php.modulator.par1, "Distribution modulator parameter"),
  120. rParamZyn(Php.modulator.freq, "Frequency of modulator parameter"),
  121. rParamZyn(Php.width, "Width of base harmonic"),
  122. rOption(Php.amp.mode, rOptions(Sum, Mult, Div1, Div2),
  123. "Amplitude harmonic multiplier type"),
  124. //Harmonic Modulation
  125. rOption(Php.amp.type, rOptions(Off, Gauss, Sine, Flat),
  126. "Type of amplitude multipler"),
  127. rParamZyn(Php.amp.par1, "Amplitude multiplier parameter"),
  128. rParamZyn(Php.amp.par2, "Amplitude multiplier parameter"),
  129. rToggle(Php.autoscale, "Autoscaling Harmonics"),
  130. rOption(Php.onehalf,
  131. rOptions(Full, Upper Half, Lower Half),
  132. "Harmonic cutoff model"),
  133. //Harmonic Bandwidth
  134. rOption(Pbwscale,
  135. rOptions(Normal,
  136. EqualHz, Quater,
  137. Half, 75%, 150%,
  138. Double, Inv. Half),
  139. "Bandwidth scaling"),
  140. //Harmonic Position Modulation
  141. rOption(Phrpos.type,
  142. rOptions(Harmonic, ShiftU, ShiftL, PowerU, PowerL, Sine,
  143. Power, Shift),
  144. "Harmonic Overtone shifting mode"),
  145. rParamZyn(Phrpos.par1, "Harmonic position parameter"),
  146. rParamZyn(Phrpos.par2, "Harmonic position parameter"),
  147. rParamZyn(Phrpos.par3, "Harmonic position parameter"),
  148. //Quality
  149. rOption(Pquality.samplesize,
  150. rOptions(16k (Tiny), 32k, 64k (Small), 128k,
  151. 256k (Normal), 512k, 1M (Big)),
  152. "Size of each wavetable element"),
  153. rOption(Pquality.basenote,
  154. rOptions( C-2, G-2, C-3, G-3, C-4,
  155. G-4, C-5, G-5, G-6,),
  156. "Base note for wavetable"),
  157. rOption(Pquality.smpoct,
  158. rOptions(0.5, 1, 2, 3, 4, 6, 12),
  159. "Samples per octave"),
  160. rParamI(Pquality.oct, rLinear(0,7),
  161. "Number of octaves to sample (above the first sample"),
  162. {"Pbandwidth::i", rProp(parameter) rLinear(0,1000) rDoc("Bandwith Of Harmonics"), NULL,
  163. [](const char *msg, rtosc::RtData &d) {
  164. PADnoteParameters *p = ((PADnoteParameters*)d.obj);
  165. if(rtosc_narguments(msg)) {
  166. p->setPbandwidth(rtosc_argument(msg, 0).i);
  167. } else {
  168. d.reply(d.loc, "i", p->Pbandwidth);
  169. }}},
  170. {"bandwidthvalue:", rMap(unit, cents) rDoc("Get Bandwidth"), NULL,
  171. [](const char *, rtosc::RtData &d) {
  172. PADnoteParameters *p = ((PADnoteParameters*)d.obj);
  173. d.reply(d.loc, "f", p->setPbandwidth(p->Pbandwidth));
  174. }},
  175. {"nhr:", rProp(non-realtime) rDoc("Returns the harmonic shifts"),
  176. NULL, [](const char *, rtosc::RtData &d) {
  177. PADnoteParameters *p = ((PADnoteParameters*)d.obj);
  178. const unsigned n = p->synth.oscilsize / 2;
  179. float *tmp = new float[n];
  180. *tmp = 0;
  181. for(unsigned i=1; i<n; ++i)
  182. tmp[i] = p->getNhr(i);
  183. d.reply(d.loc, "b", n*sizeof(float), tmp);
  184. delete[] tmp;}},
  185. {"profile:i", rProp(non-realtime) rDoc("UI display of the harmonic profile"),
  186. NULL, [](const char *m, rtosc::RtData &d) {
  187. PADnoteParameters *p = ((PADnoteParameters*)d.obj);
  188. const int n = rtosc_argument(m, 0).i;
  189. if(n<=0)
  190. return;
  191. float *tmp = new float[n];
  192. float realbw = p->getprofile(tmp, n);
  193. d.reply(d.loc, "b", n*sizeof(float), tmp);
  194. d.reply(d.loc, "i", realbw);
  195. delete[] tmp;}},
  196. };
  197. const rtosc::Ports &PADnoteParameters::non_realtime_ports = ::non_realtime_ports;
  198. const rtosc::Ports &PADnoteParameters::realtime_ports = ::realtime_ports;
  199. const rtosc::MergePorts PADnoteParameters::ports =
  200. {
  201. &non_realtime_ports,
  202. &realtime_ports
  203. };
  204. PADnoteParameters::PADnoteParameters(const SYNTH_T &synth_, FFTwrapper *fft_)
  205. :Presets(), synth(synth_)
  206. {
  207. setpresettype("Ppadsynth");
  208. fft = fft_;
  209. resonance = new Resonance();
  210. oscilgen = new OscilGen(synth, fft_, resonance);
  211. oscilgen->ADvsPAD = true;
  212. FreqEnvelope = new EnvelopeParams(0, 0);
  213. FreqEnvelope->ASRinit(64, 50, 64, 60);
  214. FreqLfo = new LFOParams(70, 0, 64, 0, 0, 0, 0, 0);
  215. AmpEnvelope = new EnvelopeParams(64, 1);
  216. AmpEnvelope->ADSRinit_dB(0, 40, 127, 25);
  217. AmpLfo = new LFOParams(80, 0, 64, 0, 0, 0, 0, 1);
  218. GlobalFilter = new FilterParams(2, 94, 40);
  219. FilterEnvelope = new EnvelopeParams(0, 1);
  220. FilterEnvelope->ADSRinit_filter(64, 40, 64, 70, 60, 64);
  221. FilterLfo = new LFOParams(80, 0, 64, 0, 0, 0, 0, 2);
  222. for(int i = 0; i < PAD_MAX_SAMPLES; ++i)
  223. sample[i].smp = NULL;
  224. defaults();
  225. }
  226. PADnoteParameters::~PADnoteParameters()
  227. {
  228. deletesamples();
  229. delete (oscilgen);
  230. delete (resonance);
  231. delete (FreqEnvelope);
  232. delete (FreqLfo);
  233. delete (AmpEnvelope);
  234. delete (AmpLfo);
  235. delete (GlobalFilter);
  236. delete (FilterEnvelope);
  237. delete (FilterLfo);
  238. }
  239. void PADnoteParameters::defaults()
  240. {
  241. Pmode = 0;
  242. Php.base.type = 0;
  243. Php.base.par1 = 80;
  244. Php.freqmult = 0;
  245. Php.modulator.par1 = 0;
  246. Php.modulator.freq = 30;
  247. Php.width = 127;
  248. Php.amp.type = 0;
  249. Php.amp.mode = 0;
  250. Php.amp.par1 = 80;
  251. Php.amp.par2 = 64;
  252. Php.autoscale = true;
  253. Php.onehalf = 0;
  254. setPbandwidth(500);
  255. Pbwscale = 0;
  256. resonance->defaults();
  257. oscilgen->defaults();
  258. Phrpos.type = 0;
  259. Phrpos.par1 = 64;
  260. Phrpos.par2 = 64;
  261. Phrpos.par3 = 0;
  262. Pquality.samplesize = 3;
  263. Pquality.basenote = 4;
  264. Pquality.oct = 3;
  265. Pquality.smpoct = 2;
  266. PStereo = 1; //stereo
  267. /* Frequency Global Parameters */
  268. Pfixedfreq = 0;
  269. PfixedfreqET = 0;
  270. PDetune = 8192; //zero
  271. PCoarseDetune = 0;
  272. PDetuneType = 1;
  273. FreqEnvelope->defaults();
  274. FreqLfo->defaults();
  275. /* Amplitude Global Parameters */
  276. PVolume = 90;
  277. PPanning = 64; //center
  278. PAmpVelocityScaleFunction = 64;
  279. AmpEnvelope->defaults();
  280. AmpLfo->defaults();
  281. Fadein_adjustment = FADEIN_ADJUSTMENT_SCALE;
  282. PPunchStrength = 0;
  283. PPunchTime = 60;
  284. PPunchStretch = 64;
  285. PPunchVelocitySensing = 72;
  286. /* Filter Global Parameters*/
  287. PFilterVelocityScale = 64;
  288. PFilterVelocityScaleFunction = 64;
  289. GlobalFilter->defaults();
  290. FilterEnvelope->defaults();
  291. FilterLfo->defaults();
  292. deletesamples();
  293. }
  294. void PADnoteParameters::deletesample(int n)
  295. {
  296. if((n < 0) || (n >= PAD_MAX_SAMPLES))
  297. return;
  298. delete[] sample[n].smp;
  299. sample[n].smp = NULL;
  300. sample[n].size = 0;
  301. sample[n].basefreq = 440.0f;
  302. }
  303. void PADnoteParameters::deletesamples()
  304. {
  305. for(int i = 0; i < PAD_MAX_SAMPLES; ++i)
  306. deletesample(i);
  307. }
  308. /*
  309. * Get the harmonic profile (i.e. the frequency distributio of a single harmonic)
  310. */
  311. float PADnoteParameters::getprofile(float *smp, int size)
  312. {
  313. for(int i = 0; i < size; ++i)
  314. smp[i] = 0.0f;
  315. const int supersample = 16;
  316. float basepar = powf(2.0f, (1.0f - Php.base.par1 / 127.0f) * 12.0f);
  317. float freqmult = floor(powf(2.0f,
  318. Php.freqmult / 127.0f
  319. * 5.0f) + 0.000001f);
  320. float modfreq = floor(powf(2.0f,
  321. Php.modulator.freq / 127.0f
  322. * 5.0f) + 0.000001f);
  323. float modpar1 = powf(Php.modulator.par1 / 127.0f, 4.0f) * 5.0f / sqrt(
  324. modfreq);
  325. float amppar1 =
  326. powf(2.0f, powf(Php.amp.par1 / 127.0f, 2.0f) * 10.0f) - 0.999f;
  327. float amppar2 = (1.0f - Php.amp.par2 / 127.0f) * 0.998f + 0.001f;
  328. float width = powf(150.0f / (Php.width + 22.0f), 2.0f);
  329. for(int i = 0; i < size * supersample; ++i) {
  330. bool makezero = false;
  331. float x = i * 1.0f / (size * (float) supersample);
  332. float origx = x;
  333. //do the sizing (width)
  334. x = (x - 0.5f) * width + 0.5f;
  335. if(x < 0.0f) {
  336. x = 0.0f;
  337. makezero = true;
  338. }
  339. else
  340. if(x > 1.0f) {
  341. x = 1.0f;
  342. makezero = true;
  343. }
  344. //compute the full profile or one half
  345. switch(Php.onehalf) {
  346. case 1:
  347. x = x * 0.5f + 0.5f;
  348. break;
  349. case 2:
  350. x = x * 0.5f;
  351. break;
  352. }
  353. float x_before_freq_mult = x;
  354. //do the frequency multiplier
  355. x *= freqmult;
  356. //do the modulation of the profile
  357. x += sinf(x_before_freq_mult * 3.1415926f * modfreq) * modpar1;
  358. x = fmod(x + 1000.0f, 1.0f) * 2.0f - 1.0f;
  359. //this is the base function of the profile
  360. float f;
  361. switch(Php.base.type) {
  362. case 1:
  363. f = expf(-(x * x) * basepar);
  364. if(f < 0.4f)
  365. f = 0.0f;
  366. else
  367. f = 1.0f;
  368. break;
  369. case 2:
  370. f = expf(-(fabs(x)) * sqrt(basepar));
  371. break;
  372. default:
  373. f = expf(-(x * x) * basepar);
  374. break;
  375. }
  376. if(makezero)
  377. f = 0.0f;
  378. float amp = 1.0f;
  379. origx = origx * 2.0f - 1.0f;
  380. //compute the amplitude multiplier
  381. switch(Php.amp.type) {
  382. case 1:
  383. amp = expf(-(origx * origx) * 10.0f * amppar1);
  384. break;
  385. case 2:
  386. amp = 0.5f
  387. * (1.0f
  388. + cosf(3.1415926f * origx * sqrt(amppar1 * 4.0f + 1.0f)));
  389. break;
  390. case 3:
  391. amp = 1.0f
  392. / (powf(origx * (amppar1 * 2.0f + 0.8f), 14.0f) + 1.0f);
  393. break;
  394. }
  395. //apply the amplitude multiplier
  396. float finalsmp = f;
  397. if(Php.amp.type != 0)
  398. switch(Php.amp.mode) {
  399. case 0:
  400. finalsmp = amp * (1.0f - amppar2) + finalsmp * amppar2;
  401. break;
  402. case 1:
  403. finalsmp *= amp * (1.0f - amppar2) + amppar2;
  404. break;
  405. case 2:
  406. finalsmp = finalsmp
  407. / (amp + powf(amppar2, 4.0f) * 20.0f + 0.0001f);
  408. break;
  409. case 3:
  410. finalsmp = amp
  411. / (finalsmp
  412. + powf(amppar2, 4.0f) * 20.0f + 0.0001f);
  413. break;
  414. }
  415. ;
  416. smp[i / supersample] += finalsmp / supersample;
  417. }
  418. //normalize the profile (make the max. to be equal to 1.0f)
  419. float max = 0.0f;
  420. for(int i = 0; i < size; ++i) {
  421. if(smp[i] < 0.0f)
  422. smp[i] = 0.0f;
  423. if(smp[i] > max)
  424. max = smp[i];
  425. }
  426. if(max < 0.00001f)
  427. max = 1.0f;
  428. for(int i = 0; i < size; ++i)
  429. smp[i] /= max;
  430. if(!Php.autoscale)
  431. return 0.5f;
  432. //compute the estimated perceived bandwidth
  433. float sum = 0.0f;
  434. int i;
  435. for(i = 0; i < size / 2 - 2; ++i) {
  436. sum += smp[i] * smp[i] + smp[size - i - 1] * smp[size - i - 1];
  437. if(sum >= 4.0f)
  438. break;
  439. }
  440. float result = 1.0f - 2.0f * i / (float) size;
  441. return result;
  442. }
  443. /*
  444. * Compute the real bandwidth in cents and returns it
  445. * Also, sets the bandwidth parameter
  446. */
  447. float PADnoteParameters::setPbandwidth(int Pbandwidth)
  448. {
  449. this->Pbandwidth = Pbandwidth;
  450. float result = powf(Pbandwidth / 1000.0f, 1.1f);
  451. result = powf(10.0f, result * 4.0f) * 0.25f;
  452. return result;
  453. }
  454. /*
  455. * Get the harmonic(overtone) position
  456. */
  457. float PADnoteParameters::getNhr(int n)
  458. {
  459. float result = 1.0f;
  460. const float par1 = powf(10.0f, -(1.0f - Phrpos.par1 / 255.0f) * 3.0f);
  461. const float par2 = Phrpos.par2 / 255.0f;
  462. const float n0 = n - 1.0f;
  463. float tmp = 0.0f;
  464. int thresh = 0;
  465. switch(Phrpos.type) {
  466. case 1:
  467. thresh = (int)(par2 * par2 * 100.0f) + 1;
  468. if(n < thresh)
  469. result = n;
  470. else
  471. result = 1.0f + n0 + (n0 - thresh + 1.0f) * par1 * 8.0f;
  472. break;
  473. case 2:
  474. thresh = (int)(par2 * par2 * 100.0f) + 1;
  475. if(n < thresh)
  476. result = n;
  477. else
  478. result = 1.0f + n0 - (n0 - thresh + 1.0f) * par1 * 0.90f;
  479. break;
  480. case 3:
  481. tmp = par1 * 100.0f + 1.0f;
  482. result = powf(n0 / tmp, 1.0f - par2 * 0.8f) * tmp + 1.0f;
  483. break;
  484. case 4:
  485. result = n0
  486. * (1.0f
  487. - par1)
  488. + powf(n0 * 0.1f, par2 * 3.0f
  489. + 1.0f) * par1 * 10.0f + 1.0f;
  490. break;
  491. case 5:
  492. result = n0
  493. + sinf(n0 * par2 * par2 * PI
  494. * 0.999f) * sqrt(par1) * 2.0f + 1.0f;
  495. break;
  496. case 6:
  497. tmp = powf(par2 * 2.0f, 2.0f) + 0.1f;
  498. result = n0 * powf(1.0f + par1 * powf(n0 * 0.8f, tmp), tmp) + 1.0f;
  499. break;
  500. case 7:
  501. result = (n + Phrpos.par1 / 255.0f) / (Phrpos.par1 / 255.0f + 1);
  502. break;
  503. default:
  504. result = n;
  505. break;
  506. }
  507. const float par3 = Phrpos.par3 / 255.0f;
  508. const float iresult = floor(result + 0.5f);
  509. const float dresult = result - iresult;
  510. return iresult + (1.0f - par3) * dresult;
  511. }
  512. //Transform non zero positive signals into ones with a max of one
  513. static void normalize_max(float *f, size_t len)
  514. {
  515. float max = 0.0f;
  516. for(unsigned i = 0; i < len; ++i)
  517. if(f[i] > i)
  518. max = f[i];
  519. if(max > 0.000001f)
  520. for(unsigned i = 0; i < len; ++i)
  521. f[i] /= max;
  522. }
  523. //Translate Bandwidth scale integer into floating point value
  524. static float Pbwscale_translate(char Pbwscale)
  525. {
  526. switch(Pbwscale) {
  527. case 0: return 1.0f;
  528. case 1: return 0.0f;
  529. case 2: return 0.25f;
  530. case 3: return 0.5f;
  531. case 4: return 0.75f;
  532. case 5: return 1.5f;
  533. case 6: return 2.0f;
  534. case 7: return -0.5f;
  535. default: return 1.0;
  536. }
  537. }
  538. /*
  539. * Generates the long spectrum for Bandwidth mode (only amplitudes are generated; phases will be random)
  540. */
  541. //Requires
  542. // - bandwidth scaling power
  543. // - bandwidth
  544. // - oscilator harmonics at various frequences (oodles of data)
  545. // - sampled resonance
  546. void PADnoteParameters::generatespectrum_bandwidthMode(float *spectrum,
  547. int size,
  548. float basefreq,
  549. float *profile,
  550. int profilesize,
  551. float bwadjust)
  552. {
  553. float harmonics[synth.oscilsize];
  554. memset(spectrum, 0, sizeof(float) * size);
  555. memset(harmonics, 0, sizeof(float) * synth.oscilsize);
  556. //get the harmonic structure from the oscillator (I am using the frequency amplitudes, only)
  557. oscilgen->get(harmonics, basefreq, false);
  558. //normalize
  559. normalize_max(harmonics, synth.oscilsize / 2);
  560. //Constants across harmonics
  561. const float power = Pbwscale_translate(Pbwscale);
  562. const float bandwidthcents = setPbandwidth(Pbandwidth);
  563. for(int nh = 1; nh < synth.oscilsize / 2; ++nh) { //for each harmonic
  564. const float realfreq = getNhr(nh) * basefreq;
  565. if(realfreq > synth.samplerate_f * 0.49999f)
  566. break;
  567. if(realfreq < 20.0f)
  568. break;
  569. if(harmonics[nh - 1] < 1e-4)
  570. continue;
  571. //compute the bandwidth of each harmonic
  572. const float bw =
  573. ((powf(2.0f, bandwidthcents / 1200.0f) - 1.0f) * basefreq / bwadjust)
  574. * powf(realfreq / basefreq, power);
  575. const int ibw = (int)((bw / (synth.samplerate_f * 0.5f) * size)) + 1;
  576. float amp = harmonics[nh - 1];
  577. if(resonance->Penabled)
  578. amp *= resonance->getfreqresponse(realfreq);
  579. if(ibw > profilesize) { //if the bandwidth is larger than the profilesize
  580. const float rap = sqrt((float)profilesize / (float)ibw);
  581. const int cfreq =
  582. (int) (realfreq
  583. / (synth.samplerate_f * 0.5f) * size) - ibw / 2;
  584. for(int i = 0; i < ibw; ++i) {
  585. const int src = i * rap * rap;
  586. const int spfreq = i + cfreq;
  587. if(spfreq < 0)
  588. continue;
  589. if(spfreq >= size)
  590. break;
  591. spectrum[spfreq] += amp * profile[src] * rap;
  592. }
  593. }
  594. else { //if the bandwidth is smaller than the profilesize
  595. const float rap = sqrt((float)ibw / (float)profilesize);
  596. const float ibasefreq = realfreq / (synth.samplerate_f * 0.5f) * size;
  597. for(int i = 0; i < profilesize; ++i) {
  598. const float idfreq = (i / (float)profilesize - 0.5f) * ibw;
  599. const int spfreq = (int) (idfreq + ibasefreq);
  600. const float fspfreq = fmodf((float)idfreq + ibasefreq, 1.0f);
  601. if(spfreq <= 0)
  602. continue;
  603. if(spfreq >= size - 1)
  604. break;
  605. spectrum[spfreq] += amp * profile[i] * rap
  606. * (1.0f - fspfreq);
  607. spectrum[spfreq + 1] += amp * profile[i] * rap * fspfreq;
  608. }
  609. }
  610. }
  611. }
  612. /*
  613. * Generates the long spectrum for non-Bandwidth modes (only amplitudes are generated; phases will be random)
  614. */
  615. void PADnoteParameters::generatespectrum_otherModes(float *spectrum,
  616. int size,
  617. float basefreq)
  618. {
  619. float harmonics[synth.oscilsize];
  620. memset(spectrum, 0, sizeof(float) * size);
  621. memset(harmonics, 0, sizeof(float) * synth.oscilsize);
  622. //get the harmonic structure from the oscillator (I am using the frequency amplitudes, only)
  623. oscilgen->get(harmonics, basefreq, false);
  624. //normalize
  625. normalize_max(harmonics, synth.oscilsize / 2);
  626. for(int nh = 1; nh < synth.oscilsize / 2; ++nh) { //for each harmonic
  627. const float realfreq = getNhr(nh) * basefreq;
  628. //take care of interpolation if frequency decreases
  629. if(realfreq > synth.samplerate_f * 0.49999f)
  630. break;
  631. if(realfreq < 20.0f)
  632. break;
  633. float amp = harmonics[nh - 1];
  634. if(resonance->Penabled)
  635. amp *= resonance->getfreqresponse(realfreq);
  636. const int cfreq = realfreq / (synth.samplerate_f * 0.5f) * size;
  637. spectrum[cfreq] = amp + 1e-9;
  638. }
  639. //In continous mode the spectrum gets additional interpolation between the
  640. //spectral peaks
  641. if(Pmode != 1) { //continous mode
  642. int old = 0;
  643. for(int k = 1; k < size; ++k)
  644. if((spectrum[k] > 1e-10) || (k == (size - 1))) {
  645. const int delta = k - old;
  646. const float val1 = spectrum[old];
  647. const float val2 = spectrum[k];
  648. const float idelta = 1.0f / delta;
  649. for(int i = 0; i < delta; ++i) {
  650. const float x = idelta * i;
  651. spectrum[old + i] = val1 * (1.0f - x) + val2 * x;
  652. }
  653. old = k;
  654. }
  655. }
  656. }
  657. /*
  658. * Applies the parameters (i.e. computes all the samples, based on parameters);
  659. */
  660. void PADnoteParameters::applyparameters()
  661. {
  662. applyparameters([]{return false;});
  663. }
  664. void PADnoteParameters::applyparameters(std::function<bool()> do_abort)
  665. {
  666. if(do_abort())
  667. return;
  668. unsigned max = 0;
  669. sampleGenerator([&max,this]
  670. (unsigned N, PADnoteParameters::Sample &smp) {
  671. delete[] sample[N].smp;
  672. sample[N] = smp;
  673. max = max < N ? N : max;
  674. },
  675. do_abort);
  676. //Delete remaining unused samples
  677. for(unsigned i = max; i < PAD_MAX_SAMPLES; ++i)
  678. deletesample(i);
  679. }
  680. //Requires
  681. // - Pquality.samplesize
  682. // - Pquality.basenote
  683. // - Pquality.oct
  684. // - Pquality.smpoct
  685. // - spectrum at various frequencies (oodles of data)
  686. void PADnoteParameters::sampleGenerator(PADnoteParameters::callback cb,
  687. std::function<bool()> do_abort)
  688. {
  689. const int samplesize = (((int) 1) << (Pquality.samplesize + 14));
  690. const int spectrumsize = samplesize / 2;
  691. float *spectrum = new float[spectrumsize];
  692. const int profilesize = 512;
  693. float profile[profilesize];
  694. const float bwadjust = getprofile(profile, profilesize);
  695. float basefreq = 65.406f * powf(2.0f, Pquality.basenote / 2);
  696. if(Pquality.basenote % 2 == 1)
  697. basefreq *= 1.5f;
  698. int samplemax = Pquality.oct + 1;
  699. int smpoct = Pquality.smpoct;
  700. if(Pquality.smpoct == 5)
  701. smpoct = 6;
  702. if(Pquality.smpoct == 6)
  703. smpoct = 12;
  704. if(smpoct != 0)
  705. samplemax *= smpoct;
  706. else
  707. samplemax = samplemax / 2 + 1;
  708. if(samplemax == 0)
  709. samplemax = 1;
  710. //prepare a BIG FFT
  711. FFTwrapper *fft = new FFTwrapper(samplesize);
  712. fft_t *fftfreqs = new fft_t[samplesize / 2];
  713. //this is used to compute frequency relation to the base frequency
  714. float adj[samplemax];
  715. for(int nsample = 0; nsample < samplemax; ++nsample)
  716. adj[nsample] = (Pquality.oct + 1.0f) * (float)nsample / samplemax;
  717. for(int nsample = 0; nsample < samplemax; ++nsample) {
  718. if(do_abort())
  719. goto exit;
  720. const float basefreqadjust =
  721. powf(2.0f, adj[nsample] - adj[samplemax - 1] * 0.5f);
  722. if(Pmode == 0)
  723. generatespectrum_bandwidthMode(spectrum,
  724. spectrumsize,
  725. basefreq * basefreqadjust,
  726. profile,
  727. profilesize,
  728. bwadjust);
  729. else
  730. generatespectrum_otherModes(spectrum, spectrumsize,
  731. basefreq * basefreqadjust);
  732. //the last samples contains the first samples
  733. //(used for linear/cubic interpolation)
  734. const int extra_samples = 5;
  735. PADnoteParameters::Sample newsample;
  736. newsample.smp = new float[samplesize + extra_samples];
  737. newsample.smp[0] = 0.0f;
  738. for(int i = 1; i < spectrumsize; ++i) //randomize the phases
  739. fftfreqs[i] = FFTpolar(spectrum[i], (float)RND * 2 * PI);
  740. //that's all; here is the only ifft for the whole sample;
  741. //no windows are used ;-)
  742. fft->freqs2smps(fftfreqs, newsample.smp);
  743. //normalize(rms)
  744. float rms = 0.0f;
  745. for(int i = 0; i < samplesize; ++i)
  746. rms += newsample.smp[i] * newsample.smp[i];
  747. rms = sqrt(rms);
  748. if(rms < 0.000001f)
  749. rms = 1.0f;
  750. rms *= sqrt(262144.0f / samplesize);//262144=2^18
  751. for(int i = 0; i < samplesize; ++i)
  752. newsample.smp[i] *= 1.0f / rms * 50.0f;
  753. //prepare extra samples used by the linear or cubic interpolation
  754. for(int i = 0; i < extra_samples; ++i)
  755. newsample.smp[i + samplesize] = newsample.smp[i];
  756. //yield new sample
  757. newsample.size = samplesize;
  758. newsample.basefreq = basefreq * basefreqadjust;
  759. cb(nsample, newsample);
  760. }
  761. exit:
  762. //Cleanup
  763. delete (fft);
  764. delete[] fftfreqs;
  765. delete[] spectrum;
  766. }
  767. void PADnoteParameters::export2wav(std::string basefilename)
  768. {
  769. applyparameters();
  770. basefilename += "_PADsynth_";
  771. for(int k = 0; k < PAD_MAX_SAMPLES; ++k) {
  772. if(sample[k].smp == NULL)
  773. continue;
  774. char tmpstr[20];
  775. snprintf(tmpstr, 20, "_%02d", k + 1);
  776. std::string filename = basefilename + std::string(tmpstr) + ".wav";
  777. WavFile wav(filename, synth.samplerate, 1);
  778. if(wav.good()) {
  779. int nsmps = sample[k].size;
  780. short int *smps = new short int[nsmps];
  781. for(int i = 0; i < nsmps; ++i)
  782. smps[i] = (short int)(sample[k].smp[i] * 32767.0f);
  783. wav.writeMonoSamples(nsmps, smps);
  784. }
  785. }
  786. }
  787. void PADnoteParameters::add2XML(XMLwrapper *xml)
  788. {
  789. xml->setPadSynth(true);
  790. xml->addparbool("stereo", PStereo);
  791. xml->addpar("mode", Pmode);
  792. xml->addpar("bandwidth", Pbandwidth);
  793. xml->addpar("bandwidth_scale", Pbwscale);
  794. xml->beginbranch("HARMONIC_PROFILE");
  795. xml->addpar("base_type", Php.base.type);
  796. xml->addpar("base_par1", Php.base.par1);
  797. xml->addpar("frequency_multiplier", Php.freqmult);
  798. xml->addpar("modulator_par1", Php.modulator.par1);
  799. xml->addpar("modulator_frequency", Php.modulator.freq);
  800. xml->addpar("width", Php.width);
  801. xml->addpar("amplitude_multiplier_type", Php.amp.type);
  802. xml->addpar("amplitude_multiplier_mode", Php.amp.mode);
  803. xml->addpar("amplitude_multiplier_par1", Php.amp.par1);
  804. xml->addpar("amplitude_multiplier_par2", Php.amp.par2);
  805. xml->addparbool("autoscale", Php.autoscale);
  806. xml->addpar("one_half", Php.onehalf);
  807. xml->endbranch();
  808. xml->beginbranch("OSCIL");
  809. oscilgen->add2XML(xml);
  810. xml->endbranch();
  811. xml->beginbranch("RESONANCE");
  812. resonance->add2XML(xml);
  813. xml->endbranch();
  814. xml->beginbranch("HARMONIC_POSITION");
  815. xml->addpar("type", Phrpos.type);
  816. xml->addpar("parameter1", Phrpos.par1);
  817. xml->addpar("parameter2", Phrpos.par2);
  818. xml->addpar("parameter3", Phrpos.par3);
  819. xml->endbranch();
  820. xml->beginbranch("SAMPLE_QUALITY");
  821. xml->addpar("samplesize", Pquality.samplesize);
  822. xml->addpar("basenote", Pquality.basenote);
  823. xml->addpar("octaves", Pquality.oct);
  824. xml->addpar("samples_per_octave", Pquality.smpoct);
  825. xml->endbranch();
  826. xml->beginbranch("AMPLITUDE_PARAMETERS");
  827. xml->addpar("volume", PVolume);
  828. xml->addpar("panning", PPanning);
  829. xml->addpar("velocity_sensing", PAmpVelocityScaleFunction);
  830. xml->addpar("fadein_adjustment", Fadein_adjustment);
  831. xml->addpar("punch_strength", PPunchStrength);
  832. xml->addpar("punch_time", PPunchTime);
  833. xml->addpar("punch_stretch", PPunchStretch);
  834. xml->addpar("punch_velocity_sensing", PPunchVelocitySensing);
  835. xml->beginbranch("AMPLITUDE_ENVELOPE");
  836. AmpEnvelope->add2XML(xml);
  837. xml->endbranch();
  838. xml->beginbranch("AMPLITUDE_LFO");
  839. AmpLfo->add2XML(xml);
  840. xml->endbranch();
  841. xml->endbranch();
  842. xml->beginbranch("FREQUENCY_PARAMETERS");
  843. xml->addpar("fixed_freq", Pfixedfreq);
  844. xml->addpar("fixed_freq_et", PfixedfreqET);
  845. xml->addpar("detune", PDetune);
  846. xml->addpar("coarse_detune", PCoarseDetune);
  847. xml->addpar("detune_type", PDetuneType);
  848. xml->beginbranch("FREQUENCY_ENVELOPE");
  849. FreqEnvelope->add2XML(xml);
  850. xml->endbranch();
  851. xml->beginbranch("FREQUENCY_LFO");
  852. FreqLfo->add2XML(xml);
  853. xml->endbranch();
  854. xml->endbranch();
  855. xml->beginbranch("FILTER_PARAMETERS");
  856. xml->addpar("velocity_sensing_amplitude", PFilterVelocityScale);
  857. xml->addpar("velocity_sensing", PFilterVelocityScaleFunction);
  858. xml->beginbranch("FILTER");
  859. GlobalFilter->add2XML(xml);
  860. xml->endbranch();
  861. xml->beginbranch("FILTER_ENVELOPE");
  862. FilterEnvelope->add2XML(xml);
  863. xml->endbranch();
  864. xml->beginbranch("FILTER_LFO");
  865. FilterLfo->add2XML(xml);
  866. xml->endbranch();
  867. xml->endbranch();
  868. }
  869. void PADnoteParameters::getfromXML(XMLwrapper *xml)
  870. {
  871. PStereo = xml->getparbool("stereo", PStereo);
  872. Pmode = xml->getpar127("mode", 0);
  873. Pbandwidth = xml->getpar("bandwidth", Pbandwidth, 0, 1000);
  874. Pbwscale = xml->getpar127("bandwidth_scale", Pbwscale);
  875. if(xml->enterbranch("HARMONIC_PROFILE")) {
  876. Php.base.type = xml->getpar127("base_type", Php.base.type);
  877. Php.base.par1 = xml->getpar127("base_par1", Php.base.par1);
  878. Php.freqmult = xml->getpar127("frequency_multiplier",
  879. Php.freqmult);
  880. Php.modulator.par1 = xml->getpar127("modulator_par1",
  881. Php.modulator.par1);
  882. Php.modulator.freq = xml->getpar127("modulator_frequency",
  883. Php.modulator.freq);
  884. Php.width = xml->getpar127("width", Php.width);
  885. Php.amp.type = xml->getpar127("amplitude_multiplier_type",
  886. Php.amp.type);
  887. Php.amp.mode = xml->getpar127("amplitude_multiplier_mode",
  888. Php.amp.mode);
  889. Php.amp.par1 = xml->getpar127("amplitude_multiplier_par1",
  890. Php.amp.par1);
  891. Php.amp.par2 = xml->getpar127("amplitude_multiplier_par2",
  892. Php.amp.par2);
  893. Php.autoscale = xml->getparbool("autoscale", Php.autoscale);
  894. Php.onehalf = xml->getpar127("one_half", Php.onehalf);
  895. xml->exitbranch();
  896. }
  897. if(xml->enterbranch("OSCIL")) {
  898. oscilgen->getfromXML(xml);
  899. xml->exitbranch();
  900. }
  901. if(xml->enterbranch("RESONANCE")) {
  902. resonance->getfromXML(xml);
  903. xml->exitbranch();
  904. }
  905. if(xml->enterbranch("HARMONIC_POSITION")) {
  906. Phrpos.type = xml->getpar127("type", Phrpos.type);
  907. Phrpos.par1 = xml->getpar("parameter1", Phrpos.par1, 0, 255);
  908. Phrpos.par2 = xml->getpar("parameter2", Phrpos.par2, 0, 255);
  909. Phrpos.par3 = xml->getpar("parameter3", Phrpos.par3, 0, 255);
  910. xml->exitbranch();
  911. }
  912. if(xml->enterbranch("SAMPLE_QUALITY")) {
  913. Pquality.samplesize = xml->getpar127("samplesize", Pquality.samplesize);
  914. Pquality.basenote = xml->getpar127("basenote", Pquality.basenote);
  915. Pquality.oct = xml->getpar127("octaves", Pquality.oct);
  916. Pquality.smpoct = xml->getpar127("samples_per_octave",
  917. Pquality.smpoct);
  918. xml->exitbranch();
  919. }
  920. if(xml->enterbranch("AMPLITUDE_PARAMETERS")) {
  921. PVolume = xml->getpar127("volume", PVolume);
  922. PPanning = xml->getpar127("panning", PPanning);
  923. PAmpVelocityScaleFunction = xml->getpar127("velocity_sensing",
  924. PAmpVelocityScaleFunction);
  925. Fadein_adjustment = xml->getpar127("fadein_adjustment", Fadein_adjustment);
  926. PPunchStrength = xml->getpar127("punch_strength", PPunchStrength);
  927. PPunchTime = xml->getpar127("punch_time", PPunchTime);
  928. PPunchStretch = xml->getpar127("punch_stretch", PPunchStretch);
  929. PPunchVelocitySensing = xml->getpar127("punch_velocity_sensing",
  930. PPunchVelocitySensing);
  931. xml->enterbranch("AMPLITUDE_ENVELOPE");
  932. AmpEnvelope->getfromXML(xml);
  933. xml->exitbranch();
  934. xml->enterbranch("AMPLITUDE_LFO");
  935. AmpLfo->getfromXML(xml);
  936. xml->exitbranch();
  937. xml->exitbranch();
  938. }
  939. if(xml->enterbranch("FREQUENCY_PARAMETERS")) {
  940. Pfixedfreq = xml->getpar127("fixed_freq", Pfixedfreq);
  941. PfixedfreqET = xml->getpar127("fixed_freq_et", PfixedfreqET);
  942. PDetune = xml->getpar("detune", PDetune, 0, 16383);
  943. PCoarseDetune = xml->getpar("coarse_detune", PCoarseDetune, 0, 16383);
  944. PDetuneType = xml->getpar127("detune_type", PDetuneType);
  945. xml->enterbranch("FREQUENCY_ENVELOPE");
  946. FreqEnvelope->getfromXML(xml);
  947. xml->exitbranch();
  948. xml->enterbranch("FREQUENCY_LFO");
  949. FreqLfo->getfromXML(xml);
  950. xml->exitbranch();
  951. xml->exitbranch();
  952. }
  953. if(xml->enterbranch("FILTER_PARAMETERS")) {
  954. PFilterVelocityScale = xml->getpar127("velocity_sensing_amplitude",
  955. PFilterVelocityScale);
  956. PFilterVelocityScaleFunction = xml->getpar127(
  957. "velocity_sensing",
  958. PFilterVelocityScaleFunction);
  959. xml->enterbranch("FILTER");
  960. GlobalFilter->getfromXML(xml);
  961. xml->exitbranch();
  962. xml->enterbranch("FILTER_ENVELOPE");
  963. FilterEnvelope->getfromXML(xml);
  964. xml->exitbranch();
  965. xml->enterbranch("FILTER_LFO");
  966. FilterLfo->getfromXML(xml);
  967. xml->exitbranch();
  968. xml->exitbranch();
  969. }
  970. }