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.

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