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.

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