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.

1122 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 float freqsum = idfreq + ibasefreq;
  600. const int spfreq = (int)freqsum;
  601. const float fspfreq = freqsum - spfreq;
  602. if(spfreq <= 0)
  603. continue;
  604. if(spfreq >= size - 1)
  605. break;
  606. spectrum[spfreq] += amp * profile[i] * rap
  607. * (1.0f - fspfreq);
  608. spectrum[spfreq + 1] += amp * profile[i] * rap * fspfreq;
  609. }
  610. }
  611. }
  612. }
  613. /*
  614. * Generates the long spectrum for non-Bandwidth modes (only amplitudes are generated; phases will be random)
  615. */
  616. void PADnoteParameters::generatespectrum_otherModes(float *spectrum,
  617. int size,
  618. float basefreq)
  619. {
  620. float harmonics[synth.oscilsize];
  621. memset(spectrum, 0, sizeof(float) * size);
  622. memset(harmonics, 0, sizeof(float) * synth.oscilsize);
  623. //get the harmonic structure from the oscillator (I am using the frequency amplitudes, only)
  624. oscilgen->get(harmonics, basefreq, false);
  625. //normalize
  626. normalize_max(harmonics, synth.oscilsize / 2);
  627. for(int nh = 1; nh < synth.oscilsize / 2; ++nh) { //for each harmonic
  628. const float realfreq = getNhr(nh) * basefreq;
  629. //take care of interpolation if frequency decreases
  630. if(realfreq > synth.samplerate_f * 0.49999f)
  631. break;
  632. if(realfreq < 20.0f)
  633. break;
  634. float amp = harmonics[nh - 1];
  635. if(resonance->Penabled)
  636. amp *= resonance->getfreqresponse(realfreq);
  637. const int cfreq = realfreq / (synth.samplerate_f * 0.5f) * size;
  638. spectrum[cfreq] = amp + 1e-9;
  639. }
  640. //In continous mode the spectrum gets additional interpolation between the
  641. //spectral peaks
  642. if(Pmode != 1) { //continous mode
  643. int old = 0;
  644. for(int k = 1; k < size; ++k)
  645. if((spectrum[k] > 1e-10) || (k == (size - 1))) {
  646. const int delta = k - old;
  647. const float val1 = spectrum[old];
  648. const float val2 = spectrum[k];
  649. const float idelta = 1.0f / delta;
  650. for(int i = 0; i < delta; ++i) {
  651. const float x = idelta * i;
  652. spectrum[old + i] = val1 * (1.0f - x) + val2 * x;
  653. }
  654. old = k;
  655. }
  656. }
  657. }
  658. /*
  659. * Applies the parameters (i.e. computes all the samples, based on parameters);
  660. */
  661. void PADnoteParameters::applyparameters()
  662. {
  663. applyparameters([]{return false;});
  664. }
  665. void PADnoteParameters::applyparameters(std::function<bool()> do_abort)
  666. {
  667. if(do_abort())
  668. return;
  669. unsigned max = 0;
  670. sampleGenerator([&max,this]
  671. (unsigned N, PADnoteParameters::Sample &smp) {
  672. delete[] sample[N].smp;
  673. sample[N] = smp;
  674. max = max < N ? N : max;
  675. },
  676. do_abort);
  677. //Delete remaining unused samples
  678. for(unsigned i = max; i < PAD_MAX_SAMPLES; ++i)
  679. deletesample(i);
  680. }
  681. //Requires
  682. // - Pquality.samplesize
  683. // - Pquality.basenote
  684. // - Pquality.oct
  685. // - Pquality.smpoct
  686. // - spectrum at various frequencies (oodles of data)
  687. void PADnoteParameters::sampleGenerator(PADnoteParameters::callback cb,
  688. std::function<bool()> do_abort)
  689. {
  690. const int samplesize = (((int) 1) << (Pquality.samplesize + 14));
  691. const int spectrumsize = samplesize / 2;
  692. float *spectrum = new float[spectrumsize];
  693. const int profilesize = 512;
  694. float profile[profilesize];
  695. const float bwadjust = getprofile(profile, profilesize);
  696. float basefreq = 65.406f * powf(2.0f, Pquality.basenote / 2);
  697. if(Pquality.basenote % 2 == 1)
  698. basefreq *= 1.5f;
  699. int samplemax = Pquality.oct + 1;
  700. int smpoct = Pquality.smpoct;
  701. if(Pquality.smpoct == 5)
  702. smpoct = 6;
  703. if(Pquality.smpoct == 6)
  704. smpoct = 12;
  705. if(smpoct != 0)
  706. samplemax *= smpoct;
  707. else
  708. samplemax = samplemax / 2 + 1;
  709. if(samplemax == 0)
  710. samplemax = 1;
  711. //prepare a BIG FFT
  712. FFTwrapper *fft = new FFTwrapper(samplesize);
  713. fft_t *fftfreqs = new fft_t[samplesize / 2];
  714. //this is used to compute frequency relation to the base frequency
  715. float adj[samplemax];
  716. for(int nsample = 0; nsample < samplemax; ++nsample)
  717. adj[nsample] = (Pquality.oct + 1.0f) * (float)nsample / samplemax;
  718. for(int nsample = 0; nsample < samplemax; ++nsample) {
  719. if(do_abort())
  720. goto exit;
  721. const float basefreqadjust =
  722. powf(2.0f, adj[nsample] - adj[samplemax - 1] * 0.5f);
  723. if(Pmode == 0)
  724. generatespectrum_bandwidthMode(spectrum,
  725. spectrumsize,
  726. basefreq * basefreqadjust,
  727. profile,
  728. profilesize,
  729. bwadjust);
  730. else
  731. generatespectrum_otherModes(spectrum, spectrumsize,
  732. basefreq * basefreqadjust);
  733. //the last samples contains the first samples
  734. //(used for linear/cubic interpolation)
  735. const int extra_samples = 5;
  736. PADnoteParameters::Sample newsample;
  737. newsample.smp = new float[samplesize + extra_samples];
  738. newsample.smp[0] = 0.0f;
  739. for(int i = 1; i < spectrumsize; ++i) //randomize the phases
  740. fftfreqs[i] = FFTpolar(spectrum[i], (float)RND * 2 * PI);
  741. //that's all; here is the only ifft for the whole sample;
  742. //no windows are used ;-)
  743. fft->freqs2smps(fftfreqs, newsample.smp);
  744. //normalize(rms)
  745. float rms = 0.0f;
  746. for(int i = 0; i < samplesize; ++i)
  747. rms += newsample.smp[i] * newsample.smp[i];
  748. rms = sqrt(rms);
  749. if(rms < 0.000001f)
  750. rms = 1.0f;
  751. rms *= sqrt(262144.0f / samplesize);//262144=2^18
  752. for(int i = 0; i < samplesize; ++i)
  753. newsample.smp[i] *= 1.0f / rms * 50.0f;
  754. //prepare extra samples used by the linear or cubic interpolation
  755. for(int i = 0; i < extra_samples; ++i)
  756. newsample.smp[i + samplesize] = newsample.smp[i];
  757. //yield new sample
  758. newsample.size = samplesize;
  759. newsample.basefreq = basefreq * basefreqadjust;
  760. cb(nsample, newsample);
  761. }
  762. exit:
  763. //Cleanup
  764. delete (fft);
  765. delete[] fftfreqs;
  766. delete[] spectrum;
  767. }
  768. void PADnoteParameters::export2wav(std::string basefilename)
  769. {
  770. applyparameters();
  771. basefilename += "_PADsynth_";
  772. for(int k = 0; k < PAD_MAX_SAMPLES; ++k) {
  773. if(sample[k].smp == NULL)
  774. continue;
  775. char tmpstr[20];
  776. snprintf(tmpstr, 20, "_%02d", k + 1);
  777. std::string filename = basefilename + std::string(tmpstr) + ".wav";
  778. WavFile wav(filename, synth.samplerate, 1);
  779. if(wav.good()) {
  780. int nsmps = sample[k].size;
  781. short int *smps = new short int[nsmps];
  782. for(int i = 0; i < nsmps; ++i)
  783. smps[i] = (short int)(sample[k].smp[i] * 32767.0f);
  784. wav.writeMonoSamples(nsmps, smps);
  785. }
  786. }
  787. }
  788. void PADnoteParameters::add2XML(XMLwrapper *xml)
  789. {
  790. xml->setPadSynth(true);
  791. xml->addparbool("stereo", PStereo);
  792. xml->addpar("mode", Pmode);
  793. xml->addpar("bandwidth", Pbandwidth);
  794. xml->addpar("bandwidth_scale", Pbwscale);
  795. xml->beginbranch("HARMONIC_PROFILE");
  796. xml->addpar("base_type", Php.base.type);
  797. xml->addpar("base_par1", Php.base.par1);
  798. xml->addpar("frequency_multiplier", Php.freqmult);
  799. xml->addpar("modulator_par1", Php.modulator.par1);
  800. xml->addpar("modulator_frequency", Php.modulator.freq);
  801. xml->addpar("width", Php.width);
  802. xml->addpar("amplitude_multiplier_type", Php.amp.type);
  803. xml->addpar("amplitude_multiplier_mode", Php.amp.mode);
  804. xml->addpar("amplitude_multiplier_par1", Php.amp.par1);
  805. xml->addpar("amplitude_multiplier_par2", Php.amp.par2);
  806. xml->addparbool("autoscale", Php.autoscale);
  807. xml->addpar("one_half", Php.onehalf);
  808. xml->endbranch();
  809. xml->beginbranch("OSCIL");
  810. oscilgen->add2XML(xml);
  811. xml->endbranch();
  812. xml->beginbranch("RESONANCE");
  813. resonance->add2XML(xml);
  814. xml->endbranch();
  815. xml->beginbranch("HARMONIC_POSITION");
  816. xml->addpar("type", Phrpos.type);
  817. xml->addpar("parameter1", Phrpos.par1);
  818. xml->addpar("parameter2", Phrpos.par2);
  819. xml->addpar("parameter3", Phrpos.par3);
  820. xml->endbranch();
  821. xml->beginbranch("SAMPLE_QUALITY");
  822. xml->addpar("samplesize", Pquality.samplesize);
  823. xml->addpar("basenote", Pquality.basenote);
  824. xml->addpar("octaves", Pquality.oct);
  825. xml->addpar("samples_per_octave", Pquality.smpoct);
  826. xml->endbranch();
  827. xml->beginbranch("AMPLITUDE_PARAMETERS");
  828. xml->addpar("volume", PVolume);
  829. xml->addpar("panning", PPanning);
  830. xml->addpar("velocity_sensing", PAmpVelocityScaleFunction);
  831. xml->addpar("fadein_adjustment", Fadein_adjustment);
  832. xml->addpar("punch_strength", PPunchStrength);
  833. xml->addpar("punch_time", PPunchTime);
  834. xml->addpar("punch_stretch", PPunchStretch);
  835. xml->addpar("punch_velocity_sensing", PPunchVelocitySensing);
  836. xml->beginbranch("AMPLITUDE_ENVELOPE");
  837. AmpEnvelope->add2XML(xml);
  838. xml->endbranch();
  839. xml->beginbranch("AMPLITUDE_LFO");
  840. AmpLfo->add2XML(xml);
  841. xml->endbranch();
  842. xml->endbranch();
  843. xml->beginbranch("FREQUENCY_PARAMETERS");
  844. xml->addpar("fixed_freq", Pfixedfreq);
  845. xml->addpar("fixed_freq_et", PfixedfreqET);
  846. xml->addpar("detune", PDetune);
  847. xml->addpar("coarse_detune", PCoarseDetune);
  848. xml->addpar("detune_type", PDetuneType);
  849. xml->beginbranch("FREQUENCY_ENVELOPE");
  850. FreqEnvelope->add2XML(xml);
  851. xml->endbranch();
  852. xml->beginbranch("FREQUENCY_LFO");
  853. FreqLfo->add2XML(xml);
  854. xml->endbranch();
  855. xml->endbranch();
  856. xml->beginbranch("FILTER_PARAMETERS");
  857. xml->addpar("velocity_sensing_amplitude", PFilterVelocityScale);
  858. xml->addpar("velocity_sensing", PFilterVelocityScaleFunction);
  859. xml->beginbranch("FILTER");
  860. GlobalFilter->add2XML(xml);
  861. xml->endbranch();
  862. xml->beginbranch("FILTER_ENVELOPE");
  863. FilterEnvelope->add2XML(xml);
  864. xml->endbranch();
  865. xml->beginbranch("FILTER_LFO");
  866. FilterLfo->add2XML(xml);
  867. xml->endbranch();
  868. xml->endbranch();
  869. }
  870. void PADnoteParameters::getfromXML(XMLwrapper *xml)
  871. {
  872. PStereo = xml->getparbool("stereo", PStereo);
  873. Pmode = xml->getpar127("mode", 0);
  874. Pbandwidth = xml->getpar("bandwidth", Pbandwidth, 0, 1000);
  875. Pbwscale = xml->getpar127("bandwidth_scale", Pbwscale);
  876. if(xml->enterbranch("HARMONIC_PROFILE")) {
  877. Php.base.type = xml->getpar127("base_type", Php.base.type);
  878. Php.base.par1 = xml->getpar127("base_par1", Php.base.par1);
  879. Php.freqmult = xml->getpar127("frequency_multiplier",
  880. Php.freqmult);
  881. Php.modulator.par1 = xml->getpar127("modulator_par1",
  882. Php.modulator.par1);
  883. Php.modulator.freq = xml->getpar127("modulator_frequency",
  884. Php.modulator.freq);
  885. Php.width = xml->getpar127("width", Php.width);
  886. Php.amp.type = xml->getpar127("amplitude_multiplier_type",
  887. Php.amp.type);
  888. Php.amp.mode = xml->getpar127("amplitude_multiplier_mode",
  889. Php.amp.mode);
  890. Php.amp.par1 = xml->getpar127("amplitude_multiplier_par1",
  891. Php.amp.par1);
  892. Php.amp.par2 = xml->getpar127("amplitude_multiplier_par2",
  893. Php.amp.par2);
  894. Php.autoscale = xml->getparbool("autoscale", Php.autoscale);
  895. Php.onehalf = xml->getpar127("one_half", Php.onehalf);
  896. xml->exitbranch();
  897. }
  898. if(xml->enterbranch("OSCIL")) {
  899. oscilgen->getfromXML(xml);
  900. xml->exitbranch();
  901. }
  902. if(xml->enterbranch("RESONANCE")) {
  903. resonance->getfromXML(xml);
  904. xml->exitbranch();
  905. }
  906. if(xml->enterbranch("HARMONIC_POSITION")) {
  907. Phrpos.type = xml->getpar127("type", Phrpos.type);
  908. Phrpos.par1 = xml->getpar("parameter1", Phrpos.par1, 0, 255);
  909. Phrpos.par2 = xml->getpar("parameter2", Phrpos.par2, 0, 255);
  910. Phrpos.par3 = xml->getpar("parameter3", Phrpos.par3, 0, 255);
  911. xml->exitbranch();
  912. }
  913. if(xml->enterbranch("SAMPLE_QUALITY")) {
  914. Pquality.samplesize = xml->getpar127("samplesize", Pquality.samplesize);
  915. Pquality.basenote = xml->getpar127("basenote", Pquality.basenote);
  916. Pquality.oct = xml->getpar127("octaves", Pquality.oct);
  917. Pquality.smpoct = xml->getpar127("samples_per_octave",
  918. Pquality.smpoct);
  919. xml->exitbranch();
  920. }
  921. if(xml->enterbranch("AMPLITUDE_PARAMETERS")) {
  922. PVolume = xml->getpar127("volume", PVolume);
  923. PPanning = xml->getpar127("panning", PPanning);
  924. PAmpVelocityScaleFunction = xml->getpar127("velocity_sensing",
  925. PAmpVelocityScaleFunction);
  926. Fadein_adjustment = xml->getpar127("fadein_adjustment", Fadein_adjustment);
  927. PPunchStrength = xml->getpar127("punch_strength", PPunchStrength);
  928. PPunchTime = xml->getpar127("punch_time", PPunchTime);
  929. PPunchStretch = xml->getpar127("punch_stretch", PPunchStretch);
  930. PPunchVelocitySensing = xml->getpar127("punch_velocity_sensing",
  931. PPunchVelocitySensing);
  932. xml->enterbranch("AMPLITUDE_ENVELOPE");
  933. AmpEnvelope->getfromXML(xml);
  934. xml->exitbranch();
  935. xml->enterbranch("AMPLITUDE_LFO");
  936. AmpLfo->getfromXML(xml);
  937. xml->exitbranch();
  938. xml->exitbranch();
  939. }
  940. if(xml->enterbranch("FREQUENCY_PARAMETERS")) {
  941. Pfixedfreq = xml->getpar127("fixed_freq", Pfixedfreq);
  942. PfixedfreqET = xml->getpar127("fixed_freq_et", PfixedfreqET);
  943. PDetune = xml->getpar("detune", PDetune, 0, 16383);
  944. PCoarseDetune = xml->getpar("coarse_detune", PCoarseDetune, 0, 16383);
  945. PDetuneType = xml->getpar127("detune_type", PDetuneType);
  946. xml->enterbranch("FREQUENCY_ENVELOPE");
  947. FreqEnvelope->getfromXML(xml);
  948. xml->exitbranch();
  949. xml->enterbranch("FREQUENCY_LFO");
  950. FreqLfo->getfromXML(xml);
  951. xml->exitbranch();
  952. xml->exitbranch();
  953. }
  954. if(xml->enterbranch("FILTER_PARAMETERS")) {
  955. PFilterVelocityScale = xml->getpar127("velocity_sensing_amplitude",
  956. PFilterVelocityScale);
  957. PFilterVelocityScaleFunction = xml->getpar127(
  958. "velocity_sensing",
  959. PFilterVelocityScaleFunction);
  960. xml->enterbranch("FILTER");
  961. GlobalFilter->getfromXML(xml);
  962. xml->exitbranch();
  963. xml->enterbranch("FILTER_ENVELOPE");
  964. FilterEnvelope->getfromXML(xml);
  965. xml->exitbranch();
  966. xml->enterbranch("FILTER_LFO");
  967. FilterLfo->getfromXML(xml);
  968. xml->exitbranch();
  969. xml->exitbranch();
  970. }
  971. }