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.

OscilGen.cpp 40KB

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