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.

1685 lines
46KB

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