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.

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