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 51KB

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