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.

1766 lines
49KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. OscilGen.cpp - Waveform generator for ADnote
  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 "OscilGen.h"
  12. #include "../DSP/FFTwrapper.h"
  13. #include "../Synth/Resonance.h"
  14. #include "../Misc/WaveShapeSmps.h"
  15. #include <cassert>
  16. #include <cstdlib>
  17. #include <cmath>
  18. #include <cstdio>
  19. #include <cstddef>
  20. #include <unistd.h>
  21. #include <rtosc/ports.h>
  22. #include <rtosc/port-sugar.h>
  23. pthread_t main_thread;
  24. #define rObject OscilGen
  25. const rtosc::Ports OscilGen::non_realtime_ports = {
  26. rSelf(OscilGen),
  27. rPaste,
  28. //TODO ensure min/max
  29. rOption(Phmagtype,
  30. rOptions(linear,dB scale (-40),
  31. dB scale (-60), dB scale (-80),
  32. dB scale (-100)),
  33. "Type of magnitude for harmonics"),
  34. rOption(Pcurrentbasefunc,
  35. rOptions(sine, triangle, pulse, saw, power, gauss,
  36. diode, abssine, pulsesine, stretchsine,
  37. chirp, absstretchsine, chebyshev, sqr,
  38. spike, circle), rOpt(127,use-as-base waveform),
  39. "Base Waveform for harmonics"),
  40. rParamZyn(Pbasefuncpar,
  41. "Morph between possible base function shapes "
  42. "(e.g. rising sawtooth vs a falling sawtooth)"),
  43. rOption(Pbasefuncmodulation,
  44. rOptions(None, Rev, Sine, Power, Chop),
  45. "Modulation applied to Base function spectra"),
  46. rParamZyn(Pbasefuncmodulationpar1,
  47. "Base function modulation parameter"),
  48. rParamZyn(Pbasefuncmodulationpar2,
  49. "Base function modulation parameter"),
  50. rParamZyn(Pbasefuncmodulationpar3,
  51. "Base function modulation parameter"),
  52. rParamZyn(Pwaveshaping, "Degree Of Waveshaping"),
  53. rOption(Pwaveshapingfunction,
  54. rOptions(Undistorted,
  55. Arctangent, Asymmetric, Pow, Sine, Quantisize,
  56. Zigzag, Limiter, Upper Limiter, Lower Limiter,
  57. Inverse Limiter, Clip, Asym2, Pow2, sigmoid),
  58. "Shape of distortion to be applied"),
  59. rOption(Pfiltertype, rOptions(No Filter,
  60. lp, hp1, hp1b, bp1, bs1, lp2, hp2, bp2, bs2,
  61. cos, sin, low_shelf, s), "Harmonic Filter"),
  62. rParamZyn(Pfilterpar1, "Filter parameter"),
  63. rParamZyn(Pfilterpar2, "Filter parameter"),
  64. rToggle(Pfilterbeforews, "Filter before waveshaping spectra;"
  65. "When enabled oscilfilter(freqs); then waveshape(freqs);, "
  66. "otherwise waveshape(freqs); then oscilfilter(freqs);"),
  67. rOption(Psatype, rOptions(None, Pow, ThrsD, ThrsU),
  68. "Spectral Adjustment Type"),
  69. rParamZyn(Psapar, "Spectral Adjustment Parameter"),
  70. rParamI(Pharmonicshift, "Amount of shift on harmonics"),
  71. rToggle(Pharmonicshiftfirst, "If harmonics are shifted before waveshaping/filtering"),
  72. rOption(Pmodulation, rOptions(None, Rev, Sine, Power),
  73. "Frequency Modulation To Combined Spectra"),
  74. rParamZyn(Pmodulationpar1, "modulation parameter"),
  75. rParamZyn(Pmodulationpar2, "modulation parameter"),
  76. rParamZyn(Pmodulationpar3, "modulation parameter"),
  77. //TODO update to rArray and test
  78. {"phase#128::c:i", rProp(parameter) rLinear(0,127) rDoc("Sets harmonic phase"),
  79. NULL, [](const char *m, rtosc::RtData &d) {
  80. const char *mm = m;
  81. while(*mm && !isdigit(*mm)) ++mm;
  82. unsigned char &phase = ((OscilGen*)d.obj)->Phphase[atoi(mm)];
  83. if(!rtosc_narguments(m))
  84. d.reply(d.loc, "c", phase);
  85. else
  86. phase = rtosc_argument(m,0).i;
  87. }},
  88. //TODO update to rArray and test
  89. {"magnitude#128::c:i", rProp(parameter) rLinear(0,127) rDoc("Sets harmonic magnitude"),
  90. NULL, [](const char *m, rtosc::RtData &d) {
  91. //printf("I'm at '%s'\n", d.loc);
  92. const char *mm = m;
  93. while(*mm && !isdigit(*mm)) ++mm;
  94. unsigned char &mag = ((OscilGen*)d.obj)->Phmag[atoi(mm)];
  95. if(!rtosc_narguments(m))
  96. d.reply(d.loc, "c", mag);
  97. else {
  98. mag = rtosc_argument(m,0).i;
  99. //printf("setting magnitude\n\n");
  100. //XXX hack hack
  101. char *repath = strdup(d.loc);
  102. char *edit = rindex(repath, '/')+1;
  103. strcpy(edit, "prepare");
  104. OscilGen &o = *((OscilGen*)d.obj);
  105. fft_t *data = new fft_t[o.synth.oscilsize / 2];
  106. o.prepare(data);
  107. // fprintf(stderr, "sending '%p' of fft data\n", data);
  108. d.chain(repath, "b", sizeof(fft_t*), &data);
  109. o.pendingfreqs = data;
  110. }
  111. }},
  112. {"base-spectrum:", rProp(non-realtime) rDoc("Returns spectrum of base waveshape"),
  113. NULL, [](const char *, rtosc::RtData &d) {
  114. OscilGen &o = *((OscilGen*)d.obj);
  115. const unsigned n = o.synth.oscilsize / 2;
  116. float *spc = new float[n];
  117. memset(spc, 0, 4*n);
  118. ((OscilGen*)d.obj)->getspectrum(n,spc,1);
  119. d.reply(d.loc, "b", n*sizeof(float), spc);
  120. delete[] spc;
  121. }},
  122. {"base-waveform:", rProp(non-realtime) rDoc("Returns base waveshape points"),
  123. NULL, [](const char *, rtosc::RtData &d) {
  124. OscilGen &o = *((OscilGen*)d.obj);
  125. const unsigned n = o.synth.oscilsize;
  126. float *smps = new float[n];
  127. memset(smps, 0, 4*n);
  128. ((OscilGen*)d.obj)->getcurrentbasefunction(smps);
  129. d.reply(d.loc, "b", n*sizeof(float), smps);
  130. delete[] smps;
  131. }},
  132. {"prepare:", rProp(non-realtime) rDoc("Performs setup operation to oscillator"),
  133. NULL, [](const char *, rtosc::RtData &d) {
  134. //fprintf(stderr, "prepare: got a message from '%s'\n", m);
  135. OscilGen &o = *(OscilGen*)d.obj;
  136. fft_t *data = new fft_t[o.synth.oscilsize / 2];
  137. o.prepare(data);
  138. // fprintf(stderr, "sending '%p' of fft data\n", data);
  139. d.chain(d.loc, "b", sizeof(fft_t*), &data);
  140. o.pendingfreqs = data;
  141. }},
  142. {"convert2sine:", rProp(non-realtime) rDoc("Translates waveform into FS"),
  143. NULL, [](const char *, rtosc::RtData &d) {
  144. ((OscilGen*)d.obj)->convert2sine();
  145. }},
  146. {"use-as-base:", rProp(non-realtime) rDoc("Translates current waveform into base"),
  147. NULL, [](const char *, rtosc::RtData &d) {
  148. ((OscilGen*)d.obj)->useasbase();
  149. }}};
  150. #define rForwardCb [](const char *msg, rtosc::RtData &d) {\
  151. printf("fowarding...\n"); d.forward();}
  152. const rtosc::Ports OscilGen::realtime_ports{
  153. rSelf(OscilGen),
  154. rPresetType,
  155. rParamZyn(Prand, "Oscilator Phase Randomness: smaller than 0 is \""
  156. "group\", larger than 0 is for each harmonic"),
  157. rParamZyn(Pamprandpower,
  158. "Variance of harmonic randomness"),
  159. rOption(Pamprandtype, rOptions(None, Pow, Sin),
  160. "Harmonic random distribution to select from"),
  161. rOption(Padaptiveharmonics,
  162. rOptions(OFF, ON, Square, 2xSub, 2xAdd, 3xSub, 3xAdd, 4xSub, 4xAdd),
  163. "Adaptive Harmonics Mode"),
  164. rParamI(Padaptiveharmonicsbasefreq, rLinear(0,255),
  165. "Base frequency of adaptive harmonic (30..3000Hz)"),
  166. rParamI(Padaptiveharmonicspower,rLinear(0,200),
  167. "Adaptive Harmonic Strength"),
  168. rParamZyn(Padaptiveharmonicspar,
  169. "Adaptive Harmonics Postprocessing Power"),
  170. {"waveform:", rDoc("Returns waveform points"),
  171. NULL, [](const char *, rtosc::RtData &d) {
  172. OscilGen &o = *((OscilGen*)d.obj);
  173. const unsigned n = o.synth.oscilsize;
  174. float *smps = new float[n];
  175. memset(smps, 0, 4*n);
  176. //printf("%d\n", o->needPrepare());
  177. o.get(smps,-1.0);
  178. //printf("wave: %f %f %f %f\n", smps[0], smps[1], smps[2], smps[3]);
  179. d.reply(d.loc, "b", n*sizeof(float), smps);
  180. delete[] smps;
  181. }},
  182. {"spectrum:", rDoc("Returns spectrum of waveform"),
  183. NULL, [](const char *, rtosc::RtData &d) {
  184. OscilGen &o = *((OscilGen*)d.obj);
  185. const unsigned n = o.synth.oscilsize / 2;
  186. float *spc = new float[n];
  187. memset(spc, 0, 4*n);
  188. ((OscilGen*)d.obj)->getspectrum(n,spc,0);
  189. d.reply(d.loc, "b", n*sizeof(float), spc);
  190. delete[] spc;
  191. }},
  192. {"prepare:b", rProp(internal) rProp(realtime) rProp(pointer) rDoc("Sets prepared fft data"),
  193. NULL, [](const char *m, rtosc::RtData &d) {
  194. // fprintf(stderr, "prepare:b got a message from '%s'\n", m);
  195. OscilGen &o = *(OscilGen*)d.obj;
  196. assert(rtosc_argument(m,0).b.len == sizeof(void*));
  197. d.reply("/free", "sb", "fft_t", sizeof(void*), &o.oscilFFTfreqs);
  198. assert(o.oscilFFTfreqs !=*(fft_t**)rtosc_argument(m,0).b.data);
  199. o.oscilFFTfreqs = *(fft_t**)rtosc_argument(m,0).b.data;
  200. }},
  201. };
  202. const rtosc::MergePorts OscilGen::ports{
  203. &OscilGen::realtime_ports,
  204. &OscilGen::non_realtime_ports
  205. };
  206. //operations on FFTfreqs
  207. inline void clearAll(fft_t *freqs, int oscilsize)
  208. {
  209. memset(freqs, 0, oscilsize / 2 * sizeof(fft_t));
  210. }
  211. inline void clearDC(fft_t *freqs)
  212. {
  213. freqs[0] = fft_t(0.0f, 0.0f);
  214. }
  215. //return magnitude squared
  216. inline float normal(const fft_t *freqs, off_t x)
  217. {
  218. return norm(freqs[x]);
  219. }
  220. //return magnitude
  221. inline float abs(const fft_t *freqs, off_t x)
  222. {
  223. return abs(freqs[x]);
  224. }
  225. //return angle aka phase from a sine (not cosine wave)
  226. inline float arg(const fft_t *freqs, off_t x)
  227. {
  228. const fft_t tmp(freqs[x].imag(), freqs[x].real());
  229. return arg(tmp);
  230. }
  231. /**
  232. * Take frequency spectrum and ensure values are normalized based upon
  233. * magnitude to 0<=x<=1
  234. */
  235. void normalize(fft_t *freqs, int oscilsize)
  236. {
  237. float normMax = 0.0f;
  238. for(int i = 0; i < oscilsize / 2; ++i) {
  239. //magnitude squared
  240. const float norm = normal(freqs, i);
  241. if(normMax < norm)
  242. normMax = norm;
  243. }
  244. const float max = sqrt(normMax);
  245. if(max < 1e-8) //data is all ~zero, do not amplify noise
  246. return;
  247. for(int i = 0; i < oscilsize / 2; ++i)
  248. freqs[i] /= max;
  249. }
  250. //Full RMS normalize
  251. void rmsNormalize(fft_t *freqs, int oscilsize)
  252. {
  253. float sum = 0.0f;
  254. for(int i = 1; i < oscilsize / 2; ++i)
  255. sum += normal(freqs, i);
  256. if(sum < 0.000001f)
  257. return; //data is all ~zero, do not amplify noise
  258. const float gain = 1.0f / sqrt(sum);
  259. for(int i = 1; i < oscilsize / 2; ++i)
  260. freqs[i] *= gain;
  261. }
  262. #define DIFF(par) (old ## par != P ## par)
  263. OscilGen::OscilGen(const SYNTH_T &synth_, FFTwrapper *fft_, Resonance *res_)
  264. :Presets(), synth(synth_)
  265. {
  266. //assert(fft_);
  267. setpresettype("Poscilgen");
  268. fft = fft_;
  269. res = res_;
  270. tmpsmps = new float[synth.oscilsize];
  271. outoscilFFTfreqs = new fft_t[synth.oscilsize / 2];
  272. oscilFFTfreqs = new fft_t[synth.oscilsize / 2];
  273. basefuncFFTfreqs = new fft_t[synth.oscilsize / 2];
  274. cachedbasefunc = new float[synth.oscilsize];
  275. cachedbasevalid = false;
  276. pendingfreqs = oscilFFTfreqs;
  277. randseed = 1;
  278. ADvsPAD = false;
  279. defaults();
  280. }
  281. OscilGen::~OscilGen()
  282. {
  283. delete[] tmpsmps;
  284. delete[] outoscilFFTfreqs;
  285. delete[] basefuncFFTfreqs;
  286. delete[] oscilFFTfreqs;
  287. delete[] cachedbasefunc;
  288. }
  289. void OscilGen::defaults()
  290. {
  291. oldbasefunc = 0;
  292. oldbasepar = 64;
  293. oldhmagtype = 0;
  294. oldwaveshapingfunction = 0;
  295. oldwaveshaping = 64;
  296. oldbasefuncmodulation = 0;
  297. oldharmonicshift = 0;
  298. oldbasefuncmodulationpar1 = 0;
  299. oldbasefuncmodulationpar2 = 0;
  300. oldbasefuncmodulationpar3 = 0;
  301. oldmodulation = 0;
  302. oldmodulationpar1 = 0;
  303. oldmodulationpar2 = 0;
  304. oldmodulationpar3 = 0;
  305. for(int i = 0; i < MAX_AD_HARMONICS; ++i) {
  306. hmag[i] = 0.0f;
  307. hphase[i] = 0.0f;
  308. Phmag[i] = 64;
  309. Phphase[i] = 64;
  310. }
  311. Phmag[0] = 127;
  312. Phmagtype = 0;
  313. if(ADvsPAD)
  314. Prand = 127; //max phase randomness (usefull if the oscil will be imported to a ADsynth from a PADsynth
  315. else
  316. Prand = 64; //no randomness
  317. Pcurrentbasefunc = 0;
  318. Pbasefuncpar = 64;
  319. Pbasefuncmodulation = 0;
  320. Pbasefuncmodulationpar1 = 64;
  321. Pbasefuncmodulationpar2 = 64;
  322. Pbasefuncmodulationpar3 = 32;
  323. Pmodulation = 0;
  324. Pmodulationpar1 = 64;
  325. Pmodulationpar2 = 64;
  326. Pmodulationpar3 = 32;
  327. Pwaveshapingfunction = 0;
  328. Pwaveshaping = 64;
  329. Pfiltertype = 0;
  330. Pfilterpar1 = 64;
  331. Pfilterpar2 = 64;
  332. Pfilterbeforews = 0;
  333. Psatype = 0;
  334. Psapar = 64;
  335. Pamprandpower = 64;
  336. Pamprandtype = 0;
  337. Pharmonicshift = 0;
  338. Pharmonicshiftfirst = 0;
  339. Padaptiveharmonics = 0;
  340. Padaptiveharmonicspower = 100;
  341. Padaptiveharmonicsbasefreq = 128;
  342. Padaptiveharmonicspar = 50;
  343. clearAll(oscilFFTfreqs, synth.oscilsize);
  344. clearAll(basefuncFFTfreqs, synth.oscilsize);
  345. oscilprepared = 0;
  346. oldfilterpars = 0;
  347. oldsapars = 0;
  348. prepare();
  349. }
  350. void OscilGen::convert2sine()
  351. {
  352. float mag[MAX_AD_HARMONICS], phase[MAX_AD_HARMONICS];
  353. float oscil[synth.oscilsize];
  354. fft_t *freqs = new fft_t[synth.oscilsize / 2];
  355. get(oscil, -1.0f);
  356. FFTwrapper *fft = new FFTwrapper(synth.oscilsize);
  357. fft->smps2freqs(oscil, freqs);
  358. delete (fft);
  359. normalize(freqs, synth.oscilsize);
  360. mag[0] = 0;
  361. phase[0] = 0;
  362. for(int i = 0; i < MAX_AD_HARMONICS; ++i) {
  363. mag[i] = abs(freqs, i + 1);
  364. phase[i] = arg(freqs, i + 1);
  365. }
  366. defaults();
  367. for(int i = 0; i < MAX_AD_HARMONICS - 1; ++i) {
  368. float newmag = mag[i];
  369. float newphase = phase[i];
  370. Phmag[i] = (int) ((newmag) * 63.0f) + 64;
  371. Phphase[i] = 64 - (int) (64.0f * newphase / PI);
  372. if(Phphase[i] > 127)
  373. Phphase[i] = 127;
  374. if(Phmag[i] == 64)
  375. Phphase[i] = 64;
  376. }
  377. delete[] freqs;
  378. prepare();
  379. }
  380. float OscilGen::userfunc(float x)
  381. {
  382. if (!fft)
  383. return 0;
  384. if (!cachedbasevalid) {
  385. fft->freqs2smps(basefuncFFTfreqs, cachedbasefunc);
  386. cachedbasevalid = true;
  387. }
  388. return cinterpolate(cachedbasefunc,
  389. synth.oscilsize,
  390. synth.oscilsize * (x + 1) - 1);
  391. }
  392. /*
  393. * Get the base function
  394. */
  395. void OscilGen::getbasefunction(float *smps)
  396. {
  397. float par = (Pbasefuncpar + 0.5f) / 128.0f;
  398. if(Pbasefuncpar == 64)
  399. par = 0.5f;
  400. float p1 = Pbasefuncmodulationpar1 / 127.0f,
  401. p2 = Pbasefuncmodulationpar2 / 127.0f,
  402. p3 = Pbasefuncmodulationpar3 / 127.0f;
  403. switch(Pbasefuncmodulation) {
  404. case 1:
  405. p1 = (powf(2, p1 * 5.0f) - 1.0f) / 10.0f;
  406. p3 = floor(powf(2, p3 * 5.0f) - 1.0f);
  407. if(p3 < 0.9999f)
  408. p3 = -1.0f;
  409. break;
  410. case 2:
  411. p1 = (powf(2, p1 * 5.0f) - 1.0f) / 10.0f;
  412. p3 = 1.0f + floor(powf(2, p3 * 5.0f) - 1.0f);
  413. break;
  414. case 3:
  415. p1 = (powf(2, p1 * 7.0f) - 1.0f) / 10.0f;
  416. p3 = 0.01f + (powf(2, p3 * 16.0f) - 1.0f) / 10.0f;
  417. break;
  418. }
  419. base_func func = getBaseFunction(Pcurrentbasefunc);
  420. for(int i = 0; i < synth.oscilsize; ++i) {
  421. float t = i * 1.0f / synth.oscilsize;
  422. switch(Pbasefuncmodulation) {
  423. case 1: //rev
  424. t = t * p3 + sinf((t + p2) * 2.0f * PI) * p1;
  425. break;
  426. case 2: //sine
  427. t += sinf( (t * p3 + p2) * 2.0f * PI) * p1;
  428. break;
  429. case 3: //power
  430. t += powf((1.0f - cosf((t + p2) * 2.0f * PI)) * 0.5f, p3) * p1;
  431. break;
  432. case 4: //chop
  433. t = t * (powf(2.0, Pbasefuncmodulationpar1/32.0 +
  434. Pbasefuncmodulationpar2/2048.0)) + p3;
  435. }
  436. t = t - floor(t);
  437. if(func)
  438. smps[i] = func(t, par);
  439. else if (Pcurrentbasefunc == 0)
  440. smps[i] = -sinf(2.0f * PI * i / synth.oscilsize);
  441. else
  442. smps[i] = userfunc(t);
  443. }
  444. }
  445. /*
  446. * Filter the oscillator
  447. */
  448. void OscilGen::oscilfilter(fft_t *freqs)
  449. {
  450. if(Pfiltertype == 0)
  451. return;
  452. const float par = 1.0f - Pfilterpar1 / 128.0f;
  453. const float par2 = Pfilterpar2 / 127.0f;
  454. filter_func filter = getFilter(Pfiltertype);
  455. for(int i = 1; i < synth.oscilsize / 2; ++i)
  456. freqs[i] *= filter(i, par, par2);
  457. normalize(freqs, synth.oscilsize);
  458. }
  459. /*
  460. * Change the base function
  461. */
  462. void OscilGen::changebasefunction(void)
  463. {
  464. if(Pcurrentbasefunc != 0) {
  465. getbasefunction(tmpsmps);
  466. if(fft)
  467. fft->smps2freqs(tmpsmps, basefuncFFTfreqs);
  468. clearDC(basefuncFFTfreqs);
  469. }
  470. else //in this case basefuncFFTfreqs are not used
  471. clearAll(basefuncFFTfreqs, synth.oscilsize);
  472. oscilprepared = 0;
  473. oldbasefunc = Pcurrentbasefunc;
  474. oldbasepar = Pbasefuncpar;
  475. oldbasefuncmodulation = Pbasefuncmodulation;
  476. oldbasefuncmodulationpar1 = Pbasefuncmodulationpar1;
  477. oldbasefuncmodulationpar2 = Pbasefuncmodulationpar2;
  478. oldbasefuncmodulationpar3 = Pbasefuncmodulationpar3;
  479. }
  480. inline void normalize(float *smps, size_t N)
  481. {
  482. //Find max
  483. float max = 0.0f;
  484. for(size_t i = 0; i < N; ++i)
  485. if(max < fabs(smps[i]))
  486. max = fabs(smps[i]);
  487. if(max < 0.00001f)
  488. max = 1.0f;
  489. //Normalize to +-1
  490. for(size_t i = 0; i < N; ++i)
  491. smps[i] /= max;
  492. }
  493. /*
  494. * Waveshape
  495. */
  496. void OscilGen::waveshape(fft_t *freqs)
  497. {
  498. oldwaveshapingfunction = Pwaveshapingfunction;
  499. oldwaveshaping = Pwaveshaping;
  500. if(Pwaveshapingfunction == 0)
  501. return;
  502. clearDC(freqs);
  503. //reduce the amplitude of the freqs near the nyquist
  504. for(int i = 1; i < synth.oscilsize / 8; ++i) {
  505. float gain = i / (synth.oscilsize / 8.0f);
  506. freqs[synth.oscilsize / 2 - i] *= gain;
  507. }
  508. fft->freqs2smps(freqs, tmpsmps);
  509. //Normalize
  510. normalize(tmpsmps, synth.oscilsize);
  511. //Do the waveshaping
  512. waveShapeSmps(synth.oscilsize, tmpsmps, Pwaveshapingfunction, Pwaveshaping);
  513. fft->smps2freqs(tmpsmps, freqs); //perform FFT
  514. }
  515. /*
  516. * Do the Frequency Modulation of the Oscil
  517. */
  518. void OscilGen::modulation(fft_t *freqs)
  519. {
  520. oldmodulation = Pmodulation;
  521. oldmodulationpar1 = Pmodulationpar1;
  522. oldmodulationpar2 = Pmodulationpar2;
  523. oldmodulationpar3 = Pmodulationpar3;
  524. if(Pmodulation == 0)
  525. return;
  526. float modulationpar1 = Pmodulationpar1 / 127.0f,
  527. modulationpar2 = 0.5f - Pmodulationpar2 / 127.0f,
  528. modulationpar3 = Pmodulationpar3 / 127.0f;
  529. switch(Pmodulation) {
  530. case 1:
  531. modulationpar1 = (powf(2, modulationpar1 * 7.0f) - 1.0f) / 100.0f;
  532. modulationpar3 = floor((powf(2, modulationpar3 * 5.0f) - 1.0f));
  533. if(modulationpar3 < 0.9999f)
  534. modulationpar3 = -1.0f;
  535. break;
  536. case 2:
  537. modulationpar1 = (powf(2, modulationpar1 * 7.0f) - 1.0f) / 100.0f;
  538. modulationpar3 = 1.0f
  539. + floor((powf(2, modulationpar3 * 5.0f) - 1.0f));
  540. break;
  541. case 3:
  542. modulationpar1 = (powf(2, modulationpar1 * 9.0f) - 1.0f) / 100.0f;
  543. modulationpar3 = 0.01f
  544. + (powf(2, modulationpar3 * 16.0f) - 1.0f) / 10.0f;
  545. break;
  546. }
  547. clearDC(freqs); //remove the DC
  548. //reduce the amplitude of the freqs near the nyquist
  549. for(int i = 1; i < synth.oscilsize / 8; ++i) {
  550. const float tmp = i / (synth.oscilsize / 8.0f);
  551. freqs[synth.oscilsize / 2 - i] *= tmp;
  552. }
  553. fft->freqs2smps(freqs, tmpsmps);
  554. const int extra_points = 2;
  555. float *in = new float[synth.oscilsize + extra_points];
  556. //Normalize
  557. normalize(tmpsmps, synth.oscilsize);
  558. for(int i = 0; i < synth.oscilsize; ++i)
  559. in[i] = tmpsmps[i];
  560. for(int i = 0; i < extra_points; ++i)
  561. in[i + synth.oscilsize] = tmpsmps[i];
  562. //Do the modulation
  563. for(int i = 0; i < synth.oscilsize; ++i) {
  564. float t = i * 1.0f / synth.oscilsize;
  565. switch(Pmodulation) {
  566. case 1:
  567. t = t * modulationpar3
  568. + sinf((t + modulationpar2) * 2.0f * PI) * modulationpar1; //rev
  569. break;
  570. case 2:
  571. t = t
  572. + sinf((t * modulationpar3
  573. + modulationpar2) * 2.0f * PI) * modulationpar1; //sine
  574. break;
  575. case 3:
  576. t = t + powf((1.0f - cosf(
  577. (t + modulationpar2) * 2.0f * PI)) * 0.5f,
  578. modulationpar3) * modulationpar1; //power
  579. break;
  580. }
  581. t = (t - floor(t)) * synth.oscilsize;
  582. const int poshi = (int) t;
  583. const float poslo = t - floor(t);
  584. tmpsmps[i] = in[poshi] * (1.0f - poslo) + in[poshi + 1] * poslo;
  585. }
  586. delete [] in;
  587. fft->smps2freqs(tmpsmps, freqs); //perform FFT
  588. }
  589. /*
  590. * Adjust the spectrum
  591. */
  592. void OscilGen::spectrumadjust(fft_t *freqs)
  593. {
  594. if(Psatype == 0)
  595. return;
  596. float par = Psapar / 127.0f;
  597. switch(Psatype) {
  598. case 1:
  599. par = 1.0f - par * 2.0f;
  600. if(par >= 0.0f)
  601. par = powf(5.0f, par);
  602. else
  603. par = powf(8.0f, par);
  604. break;
  605. case 2:
  606. par = powf(10.0f, (1.0f - par) * 3.0f) * 0.001f;
  607. break;
  608. case 3:
  609. par = powf(10.0f, (1.0f - par) * 3.0f) * 0.001f;
  610. break;
  611. }
  612. normalize(freqs, synth.oscilsize);
  613. for(int i = 0; i < synth.oscilsize / 2; ++i) {
  614. float mag = abs(freqs, i);
  615. float phase = M_PI_2 - arg(freqs, i);
  616. switch(Psatype) {
  617. case 1:
  618. mag = powf(mag, par);
  619. break;
  620. case 2:
  621. if(mag < par)
  622. mag = 0.0f;
  623. break;
  624. case 3:
  625. mag /= par;
  626. if(mag > 1.0f)
  627. mag = 1.0f;
  628. break;
  629. }
  630. freqs[i] = FFTpolar<fftw_real>(mag, phase);
  631. }
  632. }
  633. void OscilGen::shiftharmonics(fft_t *freqs)
  634. {
  635. if(Pharmonicshift == 0)
  636. return;
  637. int harmonicshift = -Pharmonicshift;
  638. fft_t h;
  639. if(harmonicshift > 0)
  640. for(int i = synth.oscilsize / 2 - 2; i >= 0; i--) {
  641. int oldh = i - harmonicshift;
  642. if(oldh < 0)
  643. h = 0.0f;
  644. else
  645. h = freqs[oldh + 1];
  646. freqs[i + 1] = h;
  647. }
  648. else
  649. for(int i = 0; i < synth.oscilsize / 2 - 1; ++i) {
  650. int oldh = i + abs(harmonicshift);
  651. if(oldh >= (synth.oscilsize / 2 - 1))
  652. h = 0.0f;
  653. else {
  654. h = freqs[oldh + 1];
  655. if(abs(h) < 0.000001f)
  656. h = 0.0f;
  657. }
  658. freqs[i + 1] = h;
  659. }
  660. clearDC(freqs);
  661. }
  662. /*
  663. * Prepare the Oscillator
  664. */
  665. void OscilGen::prepare(void)
  666. {
  667. prepare(oscilFFTfreqs);
  668. }
  669. void OscilGen::prepare(fft_t *freqs)
  670. {
  671. if((oldbasepar != Pbasefuncpar) || (oldbasefunc != Pcurrentbasefunc)
  672. || DIFF(basefuncmodulation) || DIFF(basefuncmodulationpar1)
  673. || DIFF(basefuncmodulationpar2) || DIFF(basefuncmodulationpar3))
  674. changebasefunction();
  675. for(int i = 0; i < MAX_AD_HARMONICS; ++i)
  676. hphase[i] = (Phphase[i] - 64.0f) / 64.0f * PI / (i + 1);
  677. for(int i = 0; i < MAX_AD_HARMONICS; ++i) {
  678. const float hmagnew = 1.0f - fabs(Phmag[i] / 64.0f - 1.0f);
  679. switch(Phmagtype) {
  680. case 1:
  681. hmag[i] = expf(hmagnew * logf(0.01f));
  682. break;
  683. case 2:
  684. hmag[i] = expf(hmagnew * logf(0.001f));
  685. break;
  686. case 3:
  687. hmag[i] = expf(hmagnew * logf(0.0001f));
  688. break;
  689. case 4:
  690. hmag[i] = expf(hmagnew * logf(0.00001f));
  691. break;
  692. default:
  693. hmag[i] = 1.0f - hmagnew;
  694. break;
  695. }
  696. if(Phmag[i] < 64)
  697. hmag[i] = -hmag[i];
  698. }
  699. //remove the harmonics where Phmag[i]==64
  700. for(int i = 0; i < MAX_AD_HARMONICS; ++i)
  701. if(Phmag[i] == 64)
  702. hmag[i] = 0.0f;
  703. clearAll(freqs, synth.oscilsize);
  704. if(Pcurrentbasefunc == 0) //the sine case
  705. for(int i = 0; i < MAX_AD_HARMONICS - 1; ++i) {
  706. freqs[i + 1] =
  707. std::complex<float>(-hmag[i] * sinf(hphase[i] * (i + 1)) / 2.0f,
  708. hmag[i] * cosf(hphase[i] * (i + 1)) / 2.0f);
  709. }
  710. else
  711. for(int j = 0; j < MAX_AD_HARMONICS; ++j) {
  712. if(Phmag[j] == 64)
  713. continue;
  714. for(int i = 1; i < synth.oscilsize / 2; ++i) {
  715. int k = i * (j + 1);
  716. if(k >= synth.oscilsize / 2)
  717. break;
  718. freqs[k] += basefuncFFTfreqs[i] * FFTpolar<fftw_real>(
  719. hmag[j],
  720. hphase[j] * k);
  721. }
  722. }
  723. if(Pharmonicshiftfirst != 0)
  724. shiftharmonics(freqs);
  725. if(Pfilterbeforews) {
  726. oscilfilter(freqs);
  727. waveshape(freqs);
  728. } else {
  729. waveshape(freqs);
  730. oscilfilter(freqs);
  731. }
  732. modulation(freqs);
  733. spectrumadjust(freqs);
  734. if(Pharmonicshiftfirst == 0)
  735. shiftharmonics(freqs);
  736. clearDC(freqs);
  737. oldhmagtype = Phmagtype;
  738. oldharmonicshift = Pharmonicshift + Pharmonicshiftfirst * 256;
  739. oscilprepared = 1;
  740. }
  741. fft_t operator*(float a, fft_t b)
  742. {
  743. return std::complex<float>(a*b.real(), a*b.imag());
  744. }
  745. void OscilGen::adaptiveharmonic(fft_t *f, float freq)
  746. {
  747. if(Padaptiveharmonics == 0 /*||(freq<1.0f)*/)
  748. return;
  749. if(freq < 1.0f)
  750. freq = 440.0f;
  751. fft_t *inf = new fft_t[synth.oscilsize / 2];
  752. for(int i = 0; i < synth.oscilsize / 2; ++i)
  753. inf[i] = f[i];
  754. clearAll(f, synth.oscilsize);
  755. clearDC(inf);
  756. float basefreq = 30.0f * powf(10.0f, Padaptiveharmonicsbasefreq / 128.0f);
  757. float power = (Padaptiveharmonicspower + 1.0f) / 101.0f;
  758. float rap = freq / basefreq;
  759. rap = powf(rap, power);
  760. bool down = false;
  761. if(rap > 1.0f) {
  762. rap = 1.0f / rap;
  763. down = true;
  764. }
  765. for(int i = 0; i < synth.oscilsize / 2 - 2; ++i) {
  766. const int high = (int)(i * rap);
  767. const float low = fmod(i * rap, 1.0f);
  768. if(high >= (synth.oscilsize / 2 - 2))
  769. break;
  770. if(down) {
  771. f[high] += (1.0f - low) * inf[i];
  772. f[high + 1] += low * inf[i];
  773. }
  774. else {
  775. f[i] = (1.0f - low) * inf[high] + low * inf[high + 1];
  776. }
  777. }
  778. if(!down)//corect the aplitude of the first harmonic
  779. f[0] *= rap;
  780. f[1] += f[0];
  781. clearDC(f);
  782. delete[] inf;
  783. }
  784. void OscilGen::adaptiveharmonicpostprocess(fft_t *f, int size)
  785. {
  786. if(Padaptiveharmonics <= 1)
  787. return;
  788. fft_t *inf = new fft_t[size];
  789. float par = Padaptiveharmonicspar * 0.01f;
  790. par = 1.0f - powf((1.0f - par), 1.5f);
  791. for(int i = 0; i < size; ++i) {
  792. inf[i] = f[i] * double(par);
  793. f[i] *= (1.0f - par);
  794. }
  795. if(Padaptiveharmonics == 2) { //2n+1
  796. for(int i = 0; i < size; ++i)
  797. if((i % 2) == 0)
  798. f[i] += inf[i]; //i=0 first harmonic,etc.
  799. }
  800. else { //other ways
  801. int nh = (Padaptiveharmonics - 3) / 2 + 2;
  802. int sub_vs_add = (Padaptiveharmonics - 3) % 2;
  803. if(sub_vs_add == 0) {
  804. for(int i = 0; i < size; ++i)
  805. if(((i + 1) % nh) == 0)
  806. f[i] += inf[i];
  807. }
  808. else
  809. for(int i = 0; i < size / nh - 1; ++i)
  810. f[(i + 1) * nh - 1] += inf[i];
  811. }
  812. delete [] inf;
  813. }
  814. void OscilGen::newrandseed(unsigned int randseed)
  815. {
  816. this->randseed = randseed;
  817. }
  818. bool OscilGen::needPrepare(void)
  819. {
  820. bool outdated = false;
  821. //Check function parameters
  822. if((oldbasepar != Pbasefuncpar) || (oldbasefunc != Pcurrentbasefunc)
  823. || DIFF(hmagtype) || DIFF(waveshaping) || DIFF(waveshapingfunction))
  824. outdated = true;
  825. //Check filter parameters
  826. if(oldfilterpars != Pfiltertype * 256 + Pfilterpar1 + Pfilterpar2 * 65536
  827. + Pfilterbeforews * 16777216) {
  828. outdated = true;
  829. oldfilterpars = Pfiltertype * 256 + Pfilterpar1 + Pfilterpar2 * 65536
  830. + Pfilterbeforews * 16777216;
  831. }
  832. //Check spectrum adjustments
  833. if(oldsapars != Psatype * 256 + Psapar) {
  834. outdated = true;
  835. oldsapars = Psatype * 256 + Psapar;
  836. }
  837. //Check function modulation
  838. if(DIFF(basefuncmodulation) || DIFF(basefuncmodulationpar1)
  839. || DIFF(basefuncmodulationpar2) || DIFF(basefuncmodulationpar3))
  840. outdated = true;
  841. //Check overall modulation
  842. if(DIFF(modulation) || DIFF(modulationpar1)
  843. || DIFF(modulationpar2) || DIFF(modulationpar3))
  844. outdated = true;
  845. //Check harmonic shifts
  846. if(oldharmonicshift != Pharmonicshift + Pharmonicshiftfirst * 256)
  847. outdated = true;
  848. return outdated == true || oscilprepared == false;
  849. }
  850. /*
  851. * Get the oscillator function
  852. */
  853. short int OscilGen::get(float *smps, float freqHz, int resonance)
  854. {
  855. if(needPrepare())
  856. prepare();
  857. fft_t *input = freqHz > 0.0f ? oscilFFTfreqs : pendingfreqs;
  858. int outpos =
  859. (int)((RND * 2.0f
  860. - 1.0f) * synth.oscilsize_f * (Prand - 64.0f) / 64.0f);
  861. outpos = (outpos + 2 * synth.oscilsize) % synth.oscilsize;
  862. clearAll(outoscilFFTfreqs, synth.oscilsize);
  863. int nyquist = (int)(0.5f * synth.samplerate_f / fabs(freqHz)) + 2;
  864. if(ADvsPAD)
  865. nyquist = (int)(synth.oscilsize / 2);
  866. if(nyquist > synth.oscilsize / 2)
  867. nyquist = synth.oscilsize / 2;
  868. //Process harmonics
  869. {
  870. int realnyquist = nyquist;
  871. if(Padaptiveharmonics != 0)
  872. nyquist = synth.oscilsize / 2;
  873. for(int i = 1; i < nyquist - 1; ++i)
  874. outoscilFFTfreqs[i] = input[i];
  875. adaptiveharmonic(outoscilFFTfreqs, freqHz);
  876. adaptiveharmonicpostprocess(&outoscilFFTfreqs[1],
  877. synth.oscilsize / 2 - 1);
  878. nyquist = realnyquist;
  879. }
  880. if(Padaptiveharmonics) //do the antialiasing in the case of adaptive harmonics
  881. for(int i = nyquist; i < synth.oscilsize / 2; ++i)
  882. outoscilFFTfreqs[i] = fft_t(0.0f, 0.0f);
  883. // Randomness (each harmonic), the block type is computed
  884. // in ADnote by setting start position according to this setting
  885. if((Prand > 64) && (freqHz >= 0.0f) && (!ADvsPAD)) {
  886. const float rnd = PI * powf((Prand - 64.0f) / 64.0f, 2.0f);
  887. for(int i = 1; i < nyquist - 1; ++i) //to Nyquist only for AntiAliasing
  888. outoscilFFTfreqs[i] *=
  889. FFTpolar<fftw_real>(1.0f, (float)(rnd * i * RND));
  890. }
  891. //Harmonic Amplitude Randomness
  892. if((freqHz > 0.1f) && (!ADvsPAD)) {
  893. unsigned int realrnd = prng();
  894. sprng(randseed);
  895. float power = Pamprandpower / 127.0f;
  896. float normalize = 1.0f / (1.2f - power);
  897. switch(Pamprandtype) {
  898. case 1:
  899. power = power * 2.0f - 0.5f;
  900. power = powf(15.0f, power);
  901. for(int i = 1; i < nyquist - 1; ++i)
  902. outoscilFFTfreqs[i] *= powf(RND, power) * normalize;
  903. break;
  904. case 2:
  905. power = power * 2.0f - 0.5f;
  906. power = powf(15.0f, power) * 2.0f;
  907. float rndfreq = 2 * PI * RND;
  908. for(int i = 1; i < nyquist - 1; ++i)
  909. outoscilFFTfreqs[i] *= powf(fabs(sinf(i * rndfreq)), power)
  910. * normalize;
  911. break;
  912. }
  913. sprng(realrnd + 1);
  914. }
  915. if((freqHz > 0.1f) && (resonance != 0))
  916. res->applyres(nyquist - 1, outoscilFFTfreqs, freqHz);
  917. rmsNormalize(outoscilFFTfreqs, synth.oscilsize);
  918. if((ADvsPAD) && (freqHz > 0.1f)) //in this case the smps will contain the freqs
  919. for(int i = 1; i < synth.oscilsize / 2; ++i)
  920. smps[i - 1] = abs(outoscilFFTfreqs, i);
  921. else {
  922. fft->freqs2smps(outoscilFFTfreqs, smps);
  923. for(int i = 0; i < synth.oscilsize; ++i)
  924. smps[i] *= 0.25f; //correct the amplitude
  925. }
  926. if(Prand < 64)
  927. return outpos;
  928. else
  929. return 0;
  930. }
  931. ///*
  932. // * Get the oscillator function's harmonics
  933. // */
  934. //void OscilGen::getPad(float *smps, float freqHz)
  935. //{
  936. // if(needPrepare())
  937. // prepare();
  938. //
  939. // clearAll(outoscilFFTfreqs);
  940. //
  941. // const int nyquist = (synth.oscilsize / 2);
  942. //
  943. // //Process harmonics
  944. // for(int i = 1; i < nyquist - 1; ++i)
  945. // outoscilFFTfreqs[i] = oscilFFTfreqs[i];
  946. //
  947. // adaptiveharmonic(outoscilFFTfreqs, freqHz);
  948. // adaptiveharmonicpostprocess(&outoscilFFTfreqs[1], nyquist - 1);
  949. //
  950. // rmsNormalize(outoscilFFTfreqs);
  951. //
  952. // for(int i = 1; i < nyquist; ++i)
  953. // smps[i - 1] = abs(outoscilFFTfreqs, i);
  954. //}
  955. //
  956. /*
  957. * Get the spectrum of the oscillator for the UI
  958. */
  959. void OscilGen::getspectrum(int n, float *spc, int what)
  960. {
  961. if(n > synth.oscilsize / 2)
  962. n = synth.oscilsize / 2;
  963. for(int i = 1; i < n; ++i) {
  964. if(what == 0)
  965. spc[i] = abs(pendingfreqs, i);
  966. else {
  967. if(Pcurrentbasefunc == 0)
  968. spc[i] = ((i == 1) ? (1.0f) : (0.0f));
  969. else
  970. spc[i] = abs(basefuncFFTfreqs, i);
  971. }
  972. }
  973. spc[0]=0;
  974. if(what == 0) {
  975. for(int i = 0; i < n; ++i)
  976. outoscilFFTfreqs[i] = fft_t(spc[i], spc[i]);
  977. memset(outoscilFFTfreqs + n, 0,
  978. (synth.oscilsize / 2 - n) * sizeof(fft_t));
  979. adaptiveharmonic(outoscilFFTfreqs, 0.0f);
  980. adaptiveharmonicpostprocess(outoscilFFTfreqs, n - 1);
  981. for(int i = 0; i < n; ++i)
  982. spc[i] = outoscilFFTfreqs[i].imag();
  983. }
  984. }
  985. /*
  986. * Convert the oscillator as base function
  987. */
  988. void OscilGen::useasbase()
  989. {
  990. for(int i = 0; i < synth.oscilsize / 2; ++i)
  991. basefuncFFTfreqs[i] = oscilFFTfreqs[i];
  992. oldbasefunc = Pcurrentbasefunc = 127;
  993. prepare();
  994. cachedbasevalid = false;
  995. }
  996. /*
  997. * Get the base function for UI
  998. */
  999. void OscilGen::getcurrentbasefunction(float *smps)
  1000. {
  1001. if(Pcurrentbasefunc != 0)
  1002. fft->freqs2smps(basefuncFFTfreqs, smps);
  1003. else
  1004. getbasefunction(smps); //the sine case
  1005. }
  1006. #define COPY(y) this->y = o.y
  1007. void OscilGen::paste(OscilGen &o)
  1008. {
  1009. //XXX Figure out a better implementation of this sensitive to RT issues...
  1010. for(int i=0; i<MAX_AD_HARMONICS; ++i) {
  1011. COPY(Phmag[i]);
  1012. COPY(Phphase[i]);
  1013. }
  1014. COPY(Phmagtype);
  1015. COPY(Pcurrentbasefunc);
  1016. COPY(Pbasefuncpar);
  1017. COPY(Pbasefuncmodulation);
  1018. COPY(Pbasefuncmodulationpar1);
  1019. COPY(Pbasefuncmodulationpar2);
  1020. COPY(Pbasefuncmodulationpar3);
  1021. COPY(Pwaveshaping);
  1022. COPY(Pwaveshapingfunction);
  1023. COPY(Pfiltertype);
  1024. COPY(Pfilterpar1);
  1025. COPY(Pfilterpar2);
  1026. COPY(Pfilterbeforews);
  1027. COPY(Psatype);
  1028. COPY(Psapar);
  1029. COPY(Pharmonicshift);
  1030. COPY(Pharmonicshiftfirst);
  1031. COPY(Pmodulation);
  1032. COPY(Pmodulationpar1);
  1033. COPY(Pmodulationpar2);
  1034. COPY(Pmodulationpar3);
  1035. COPY(Prand);
  1036. COPY(Pamprandpower);
  1037. COPY(Pamprandtype);
  1038. COPY(Padaptiveharmonics);
  1039. COPY(Padaptiveharmonicsbasefreq);
  1040. COPY(Padaptiveharmonicspower);
  1041. COPY(Padaptiveharmonicspar);
  1042. if(this->Pcurrentbasefunc)
  1043. changebasefunction();
  1044. this->prepare();
  1045. }
  1046. #undef COPY
  1047. void OscilGen::add2XML(XMLwrapper& xml)
  1048. {
  1049. xml.addpar("harmonic_mag_type", Phmagtype);
  1050. xml.addpar("base_function", Pcurrentbasefunc);
  1051. xml.addpar("base_function_par", Pbasefuncpar);
  1052. xml.addpar("base_function_modulation", Pbasefuncmodulation);
  1053. xml.addpar("base_function_modulation_par1", Pbasefuncmodulationpar1);
  1054. xml.addpar("base_function_modulation_par2", Pbasefuncmodulationpar2);
  1055. xml.addpar("base_function_modulation_par3", Pbasefuncmodulationpar3);
  1056. xml.addpar("modulation", Pmodulation);
  1057. xml.addpar("modulation_par1", Pmodulationpar1);
  1058. xml.addpar("modulation_par2", Pmodulationpar2);
  1059. xml.addpar("modulation_par3", Pmodulationpar3);
  1060. xml.addpar("wave_shaping", Pwaveshaping);
  1061. xml.addpar("wave_shaping_function", Pwaveshapingfunction);
  1062. xml.addpar("filter_type", Pfiltertype);
  1063. xml.addpar("filter_par1", Pfilterpar1);
  1064. xml.addpar("filter_par2", Pfilterpar2);
  1065. xml.addpar("filter_before_wave_shaping", Pfilterbeforews);
  1066. xml.addpar("spectrum_adjust_type", Psatype);
  1067. xml.addpar("spectrum_adjust_par", Psapar);
  1068. xml.addpar("rand", Prand);
  1069. xml.addpar("amp_rand_type", Pamprandtype);
  1070. xml.addpar("amp_rand_power", Pamprandpower);
  1071. xml.addpar("harmonic_shift", Pharmonicshift);
  1072. xml.addparbool("harmonic_shift_first", Pharmonicshiftfirst);
  1073. xml.addpar("adaptive_harmonics", Padaptiveharmonics);
  1074. xml.addpar("adaptive_harmonics_base_frequency", Padaptiveharmonicsbasefreq);
  1075. xml.addpar("adaptive_harmonics_power", Padaptiveharmonicspower);
  1076. xml.addpar("adaptive_harmonics_par", Padaptiveharmonicspar);
  1077. xml.beginbranch("HARMONICS");
  1078. for(int n = 0; n < MAX_AD_HARMONICS; ++n) {
  1079. if((Phmag[n] == 64) && (Phphase[n] == 64))
  1080. continue;
  1081. xml.beginbranch("HARMONIC", n + 1);
  1082. xml.addpar("mag", Phmag[n]);
  1083. xml.addpar("phase", Phphase[n]);
  1084. xml.endbranch();
  1085. }
  1086. xml.endbranch();
  1087. if(Pcurrentbasefunc == 127) {
  1088. normalize(basefuncFFTfreqs, synth.oscilsize);
  1089. xml.beginbranch("BASE_FUNCTION");
  1090. for(int i = 1; i < synth.oscilsize / 2; ++i) {
  1091. float xc = basefuncFFTfreqs[i].real();
  1092. float xs = basefuncFFTfreqs[i].imag();
  1093. if((fabs(xs) > 1e-6f) || (fabs(xc) > 1e-6f)) {
  1094. xml.beginbranch("BF_HARMONIC", i);
  1095. xml.addparreal("cos", xc);
  1096. xml.addparreal("sin", xs);
  1097. xml.endbranch();
  1098. }
  1099. }
  1100. xml.endbranch();
  1101. }
  1102. }
  1103. void OscilGen::getfromXML(XMLwrapper& xml)
  1104. {
  1105. Phmagtype = xml.getpar127("harmonic_mag_type", Phmagtype);
  1106. Pcurrentbasefunc = xml.getpar127("base_function", Pcurrentbasefunc);
  1107. Pbasefuncpar = xml.getpar127("base_function_par", Pbasefuncpar);
  1108. Pbasefuncmodulation = xml.getpar127("base_function_modulation",
  1109. Pbasefuncmodulation);
  1110. Pbasefuncmodulationpar1 = xml.getpar127("base_function_modulation_par1",
  1111. Pbasefuncmodulationpar1);
  1112. Pbasefuncmodulationpar2 = xml.getpar127("base_function_modulation_par2",
  1113. Pbasefuncmodulationpar2);
  1114. Pbasefuncmodulationpar3 = xml.getpar127("base_function_modulation_par3",
  1115. Pbasefuncmodulationpar3);
  1116. Pmodulation = xml.getpar127("modulation", Pmodulation);
  1117. Pmodulationpar1 = xml.getpar127("modulation_par1",
  1118. Pmodulationpar1);
  1119. Pmodulationpar2 = xml.getpar127("modulation_par2",
  1120. Pmodulationpar2);
  1121. Pmodulationpar3 = xml.getpar127("modulation_par3",
  1122. Pmodulationpar3);
  1123. Pwaveshaping = xml.getpar127("wave_shaping", Pwaveshaping);
  1124. Pwaveshapingfunction = xml.getpar127("wave_shaping_function",
  1125. Pwaveshapingfunction);
  1126. Pfiltertype = xml.getpar127("filter_type", Pfiltertype);
  1127. Pfilterpar1 = xml.getpar127("filter_par1", Pfilterpar1);
  1128. Pfilterpar2 = xml.getpar127("filter_par2", Pfilterpar2);
  1129. Pfilterbeforews = xml.getpar127("filter_before_wave_shaping",
  1130. Pfilterbeforews);
  1131. Psatype = xml.getpar127("spectrum_adjust_type", Psatype);
  1132. Psapar = xml.getpar127("spectrum_adjust_par", Psapar);
  1133. Prand = xml.getpar127("rand", Prand);
  1134. Pamprandtype = xml.getpar127("amp_rand_type", Pamprandtype);
  1135. Pamprandpower = xml.getpar127("amp_rand_power", Pamprandpower);
  1136. Pharmonicshift = xml.getpar("harmonic_shift",
  1137. Pharmonicshift,
  1138. -64,
  1139. 64);
  1140. Pharmonicshiftfirst = xml.getparbool("harmonic_shift_first",
  1141. Pharmonicshiftfirst);
  1142. Padaptiveharmonics = xml.getpar("adaptive_harmonics",
  1143. Padaptiveharmonics,
  1144. 0,
  1145. 127);
  1146. Padaptiveharmonicsbasefreq = xml.getpar(
  1147. "adaptive_harmonics_base_frequency",
  1148. Padaptiveharmonicsbasefreq,
  1149. 0,
  1150. 255);
  1151. Padaptiveharmonicspower = xml.getpar("adaptive_harmonics_power",
  1152. Padaptiveharmonicspower,
  1153. 0,
  1154. 200);
  1155. Padaptiveharmonicspar = xml.getpar("adaptive_harmonics_par",
  1156. Padaptiveharmonicspar,
  1157. 0,
  1158. 100);
  1159. if(xml.enterbranch("HARMONICS")) {
  1160. Phmag[0] = 64;
  1161. Phphase[0] = 64;
  1162. for(int n = 0; n < MAX_AD_HARMONICS; ++n) {
  1163. if(xml.enterbranch("HARMONIC", n + 1) == 0)
  1164. continue;
  1165. Phmag[n] = xml.getpar127("mag", 64);
  1166. Phphase[n] = xml.getpar127("phase", 64);
  1167. xml.exitbranch();
  1168. }
  1169. xml.exitbranch();
  1170. }
  1171. if(Pcurrentbasefunc != 0)
  1172. changebasefunction();
  1173. if(xml.enterbranch("BASE_FUNCTION")) {
  1174. for(int i = 1; i < synth.oscilsize / 2; ++i)
  1175. if(xml.enterbranch("BF_HARMONIC", i)) {
  1176. basefuncFFTfreqs[i] =
  1177. std::complex<float>(xml.getparreal("cos", 0.0f),
  1178. xml.getparreal("sin", 0.0f));
  1179. xml.exitbranch();
  1180. }
  1181. xml.exitbranch();
  1182. clearDC(basefuncFFTfreqs);
  1183. normalize(basefuncFFTfreqs, synth.oscilsize);
  1184. cachedbasevalid = false;
  1185. }}
  1186. //Define basic functions
  1187. #define FUNC(b) float basefunc_ ## b(float x, float a)
  1188. FUNC(pulse)
  1189. {
  1190. return (fmod(x, 1.0f) < a) ? -1.0f : 1.0f;
  1191. }
  1192. FUNC(saw)
  1193. {
  1194. if(a < 0.00001f)
  1195. a = 0.00001f;
  1196. else
  1197. if(a > 0.99999f)
  1198. a = 0.99999f;
  1199. x = fmod(x, 1);
  1200. if(x < a)
  1201. return x / a * 2.0f - 1.0f;
  1202. else
  1203. return (1.0f - x) / (1.0f - a) * 2.0f - 1.0f;
  1204. }
  1205. FUNC(triangle)
  1206. {
  1207. x = fmod(x + 0.25f, 1);
  1208. a = 1 - a;
  1209. if(a < 0.00001f)
  1210. a = 0.00001f;
  1211. if(x < 0.5f)
  1212. x = x * 4 - 1.0f;
  1213. else
  1214. x = (1.0f - x) * 4 - 1.0f;
  1215. x /= -a;
  1216. if(x < -1.0f)
  1217. x = -1.0f;
  1218. if(x > 1.0f)
  1219. x = 1.0f;
  1220. return x;
  1221. }
  1222. FUNC(power)
  1223. {
  1224. x = fmod(x, 1);
  1225. if(a < 0.00001f)
  1226. a = 0.00001f;
  1227. else
  1228. if(a > 0.99999f)
  1229. a = 0.99999f;
  1230. return powf(x, expf((a - 0.5f) * 10.0f)) * 2.0f - 1.0f;
  1231. }
  1232. FUNC(gauss)
  1233. {
  1234. x = fmod(x, 1) * 2.0f - 1.0f;
  1235. if(a < 0.00001f)
  1236. a = 0.00001f;
  1237. return expf(-x * x * (expf(a * 8) + 5.0f)) * 2.0f - 1.0f;
  1238. }
  1239. FUNC(diode)
  1240. {
  1241. if(a < 0.00001f)
  1242. a = 0.00001f;
  1243. else
  1244. if(a > 0.99999f)
  1245. a = 0.99999f;
  1246. a = a * 2.0f - 1.0f;
  1247. x = cosf((x + 0.5f) * 2.0f * PI) - a;
  1248. if(x < 0.0f)
  1249. x = 0.0f;
  1250. return x / (1.0f - a) * 2 - 1.0f;
  1251. }
  1252. FUNC(abssine)
  1253. {
  1254. x = fmod(x, 1);
  1255. if(a < 0.00001f)
  1256. a = 0.00001f;
  1257. else
  1258. if(a > 0.99999f)
  1259. a = 0.99999f;
  1260. return sinf(powf(x, expf((a - 0.5f) * 5.0f)) * PI) * 2.0f - 1.0f;
  1261. }
  1262. FUNC(pulsesine)
  1263. {
  1264. if(a < 0.00001f)
  1265. a = 0.00001f;
  1266. x = (fmod(x, 1) - 0.5f) * expf((a - 0.5f) * logf(128));
  1267. if(x < -0.5f)
  1268. x = -0.5f;
  1269. else
  1270. if(x > 0.5f)
  1271. x = 0.5f;
  1272. x = sinf(x * PI * 2.0f);
  1273. return x;
  1274. }
  1275. FUNC(stretchsine)
  1276. {
  1277. x = fmod(x + 0.5f, 1) * 2.0f - 1.0f;
  1278. a = (a - 0.5f) * 4;
  1279. if(a > 0.0f)
  1280. a *= 2;
  1281. a = powf(3.0f, a);
  1282. float b = powf(fabs(x), a);
  1283. if(x < 0)
  1284. b = -b;
  1285. return -sinf(b * PI);
  1286. }
  1287. FUNC(chirp)
  1288. {
  1289. x = fmod(x, 1.0f) * 2.0f * PI;
  1290. a = (a - 0.5f) * 4;
  1291. if(a < 0.0f)
  1292. a *= 2.0f;
  1293. a = powf(3.0f, a);
  1294. return sinf(x / 2.0f) * sinf(a * x * x);
  1295. }
  1296. FUNC(absstretchsine)
  1297. {
  1298. x = fmod(x + 0.5f, 1) * 2.0f - 1.0f;
  1299. a = (a - 0.5f) * 9;
  1300. a = powf(3.0f, a);
  1301. float b = powf(fabs(x), a);
  1302. if(x < 0)
  1303. b = -b;
  1304. return -powf(sinf(b * PI), 2);
  1305. }
  1306. FUNC(chebyshev)
  1307. {
  1308. a = a * a * a * 30.0f + 1.0f;
  1309. return cosf(acosf(x * 2.0f - 1.0f) * a);
  1310. }
  1311. FUNC(sqr)
  1312. {
  1313. a = a * a * a * a * 160.0f + 0.001f;
  1314. return -atanf(sinf(x * 2.0f * PI) * a);
  1315. }
  1316. FUNC(spike)
  1317. {
  1318. float b = a * 0.66666; // the width of the range: if a == 0.5, b == 0.33333
  1319. if(x < 0.5) {
  1320. if(x < (0.5 - (b / 2.0)))
  1321. return 0.0;
  1322. else {
  1323. x = (x + (b / 2) - 0.5) * (2.0 / b); // shift to zero, and expand to range from 0 to 1
  1324. return x * (2.0 / b); // this is the slope: 1 / (b / 2)
  1325. }
  1326. }
  1327. else {
  1328. if(x > (0.5 + (b / 2.0)))
  1329. return 0.0;
  1330. else {
  1331. x = (x - 0.5) * (2.0 / b);
  1332. return (1 - x) * (2.0 / b);
  1333. }
  1334. }
  1335. }
  1336. FUNC(circle)
  1337. {
  1338. // a is parameter: 0 -> 0.5 -> 1 // O.5 = circle
  1339. float b, y;
  1340. b = 2 - (a * 2); // b goes from 2 to 0
  1341. x = x * 4;
  1342. if(x < 2) {
  1343. x = x - 1; // x goes from -1 to 1
  1344. if((x < -b) || (x > b))
  1345. y = 0;
  1346. else
  1347. y = sqrt(1 - (pow(x, 2) / pow(b, 2))); // normally * a^2, but a stays 1
  1348. }
  1349. else {
  1350. x = x - 3; // x goes from -1 to 1 as well
  1351. if((x < -b) || (x > b))
  1352. y = 0;
  1353. else
  1354. y = -sqrt(1 - (pow(x, 2) / pow(b, 2)));
  1355. }
  1356. return y;
  1357. }
  1358. typedef float (*base_func)(float, float);
  1359. base_func getBaseFunction(unsigned char func)
  1360. {
  1361. if(!func)
  1362. return NULL;
  1363. if(func == 127) //should be the custom wave
  1364. return NULL;
  1365. func--;
  1366. assert(func < 15);
  1367. base_func functions[] = {
  1368. basefunc_triangle,
  1369. basefunc_pulse,
  1370. basefunc_saw,
  1371. basefunc_power,
  1372. basefunc_gauss,
  1373. basefunc_diode,
  1374. basefunc_abssine,
  1375. basefunc_pulsesine,
  1376. basefunc_stretchsine,
  1377. basefunc_chirp,
  1378. basefunc_absstretchsine,
  1379. basefunc_chebyshev,
  1380. basefunc_sqr,
  1381. basefunc_spike,
  1382. basefunc_circle,
  1383. };
  1384. return functions[func];
  1385. }
  1386. //And filters
  1387. #define FILTER(x) float osc_ ## x(unsigned int i, float par, float par2)
  1388. FILTER(lp)
  1389. {
  1390. float gain = powf(1.0f - par * par * par * 0.99f, i);
  1391. float tmp = par2 * par2 * par2 * par2 * 0.5f + 0.0001f;
  1392. if(gain < tmp)
  1393. gain = powf(gain, 10.0f) / powf(tmp, 9.0f);
  1394. return gain;
  1395. }
  1396. FILTER(hp1)
  1397. {
  1398. float gain = 1.0f - powf(1.0f - par * par, i + 1);
  1399. return powf(gain, par2 * 2.0f + 0.1f);
  1400. }
  1401. FILTER(hp1b)
  1402. {
  1403. if(par < 0.2f)
  1404. par = par * 0.25f + 0.15f;
  1405. float gain = 1.0f - powf(1.0f - par * par * 0.999f + 0.001f,
  1406. i * 0.05f * i + 1.0f);
  1407. float tmp = powf(5.0f, par2 * 2.0f);
  1408. return powf(gain, tmp);
  1409. }
  1410. FILTER(bp1)
  1411. {
  1412. float gain = i + 1 - powf(2, (1.0f - par) * 7.5f);
  1413. gain = 1.0f / (1.0f + gain * gain / (i + 1.0f));
  1414. float tmp = powf(5.0f, par2 * 2.0f);
  1415. gain = powf(gain, tmp);
  1416. if(gain < 1e-5)
  1417. gain = 1e-5;
  1418. return gain;
  1419. }
  1420. FILTER(bs1)
  1421. {
  1422. float gain = i + 1 - powf(2, (1.0f - par) * 7.5f);
  1423. gain = powf(atanf(gain / (i / 10.0f + 1)) / 1.57f, 6);
  1424. return powf(gain, par2 * par2 * 3.9f + 0.1f);
  1425. }
  1426. FILTER(lp2)
  1427. {
  1428. return (i + 1 >
  1429. powf(2, (1.0f - par) * 10) ? 0.0f : 1.0f) * par2 + (1.0f - par2);
  1430. }
  1431. FILTER(hp2)
  1432. {
  1433. if(par == 1)
  1434. return 1.0f;
  1435. return (i + 1 >
  1436. powf(2, (1.0f - par) * 7) ? 1.0f : 0.0f) * par2 + (1.0f - par2);
  1437. }
  1438. FILTER(bp2)
  1439. {
  1440. return (fabs(powf(2,
  1441. (1.0f
  1442. - par)
  1443. * 7)
  1444. - i) > i / 2 + 1 ? 0.0f : 1.0f) * par2 + (1.0f - par2);
  1445. }
  1446. FILTER(bs2)
  1447. {
  1448. return (fabs(powf(2,
  1449. (1.0f
  1450. - par)
  1451. * 7)
  1452. - i) < i / 2 + 1 ? 0.0f : 1.0f) * par2 + (1.0f - par2);
  1453. }
  1454. bool floatEq(float a, float b)
  1455. {
  1456. const float fudge = .01;
  1457. return a + fudge > b && a - fudge < b;
  1458. }
  1459. FILTER(cos)
  1460. {
  1461. float tmp = powf(5.0f, par2 * 2.0f - 1.0f);
  1462. tmp = powf(i / 32.0f, tmp) * 32.0f;
  1463. if(floatEq(par2 * 127.0f, 64.0f))
  1464. tmp = i;
  1465. float gain = cosf(par * par * PI / 2.0f * tmp);
  1466. gain *= gain;
  1467. return gain;
  1468. }
  1469. FILTER(sin)
  1470. {
  1471. float tmp = powf(5.0f, par2 * 2.0f - 1.0f);
  1472. tmp = powf(i / 32.0f, tmp) * 32.0f;
  1473. if(floatEq(par2 * 127.0f, 64.0f))
  1474. tmp = i;
  1475. float gain = sinf(par * par * PI / 2.0f * tmp);
  1476. gain *= gain;
  1477. return gain;
  1478. }
  1479. FILTER(low_shelf)
  1480. {
  1481. float p2 = 1.0f - par + 0.2f;
  1482. float x = i / (64.0f * p2 * p2);
  1483. if(x < 0.0f)
  1484. x = 0.0f;
  1485. else
  1486. if(x > 1.0f)
  1487. x = 1.0f;
  1488. float tmp = powf(1.0f - par2, 2.0f);
  1489. return cosf(x * PI) * (1.0f - tmp) + 1.01f + tmp;
  1490. }
  1491. FILTER(s)
  1492. {
  1493. unsigned int tmp = (int) (powf(2.0f, (1.0f - par) * 7.2f));
  1494. float gain = 1.0f;
  1495. if(i == tmp)
  1496. gain = powf(2.0f, par2 * par2 * 8.0f);
  1497. return gain;
  1498. }
  1499. #undef FILTER
  1500. typedef float (*filter_func)(unsigned int, float, float);
  1501. filter_func getFilter(unsigned char func)
  1502. {
  1503. if(!func)
  1504. return NULL;
  1505. func--;
  1506. assert(func < 13);
  1507. filter_func functions[] = {
  1508. osc_lp,
  1509. osc_hp1,
  1510. osc_hp1b,
  1511. osc_bp1,
  1512. osc_bs1,
  1513. osc_lp2,
  1514. osc_hp2,
  1515. osc_bp2,
  1516. osc_bs2,
  1517. osc_cos,
  1518. osc_sin,
  1519. osc_low_shelf,
  1520. osc_s
  1521. };
  1522. return functions[func];
  1523. }