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.

1726 lines
48KB

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