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.

1223 lines
39KB

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