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.

ADnote.cpp 67KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. ADnote.cpp - The "additive" synthesizer
  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 <cmath>
  18. #include <cstdlib>
  19. #include <cstdio>
  20. #include <cstring>
  21. #include <cassert>
  22. #include <stdint.h>
  23. #include "../globals.h"
  24. #include "../Misc/Util.h"
  25. #include "../DSP/Filter.h"
  26. #include "OscilGen.h"
  27. #include "ADnote.h"
  28. ADnote::ADnote(ADnoteParameters *pars,
  29. Controller *ctl_,
  30. float freq,
  31. float velocity,
  32. int portamento_,
  33. int midinote_,
  34. bool besilent)
  35. :SynthNote(freq, velocity, portamento_, midinote_, besilent)
  36. {
  37. tmpwavel = new float [synth->buffersize];
  38. tmpwaver = new float [synth->buffersize];
  39. bypassl = new float [synth->buffersize];
  40. bypassr = new float [synth->buffersize];
  41. partparams = pars;
  42. ctl = ctl_;
  43. portamento = portamento_;
  44. midinote = midinote_;
  45. NoteEnabled = ON;
  46. basefreq = freq;
  47. if(velocity > 1.0f)
  48. velocity = 1.0f;
  49. this->velocity = velocity;
  50. time = 0.0f;
  51. stereo = pars->GlobalPar.PStereo;
  52. NoteGlobalPar.Detune = getdetune(pars->GlobalPar.PDetuneType,
  53. pars->GlobalPar.PCoarseDetune,
  54. pars->GlobalPar.PDetune);
  55. bandwidthDetuneMultiplier = pars->getBandwidthDetuneMultiplier();
  56. if(pars->GlobalPar.PPanning == 0)
  57. NoteGlobalPar.Panning = RND;
  58. else
  59. NoteGlobalPar.Panning = pars->GlobalPar.PPanning / 128.0f;
  60. NoteGlobalPar.FilterCenterPitch = pars->GlobalPar.GlobalFilter->getfreq() //center freq
  61. + pars->GlobalPar.PFilterVelocityScale
  62. / 127.0f * 6.0f //velocity sensing
  63. * (VelF(velocity,
  64. pars->GlobalPar.
  65. PFilterVelocityScaleFunction) - 1);
  66. if(pars->GlobalPar.PPunchStrength != 0) {
  67. NoteGlobalPar.Punch.Enabled = 1;
  68. NoteGlobalPar.Punch.t = 1.0f; //start from 1.0f and to 0.0f
  69. NoteGlobalPar.Punch.initialvalue =
  70. ((powf(10, 1.5f * pars->GlobalPar.PPunchStrength / 127.0f) - 1.0f)
  71. * VelF(velocity,
  72. pars->GlobalPar.PPunchVelocitySensing));
  73. float time =
  74. powf(10, 3.0f * pars->GlobalPar.PPunchTime / 127.0f) / 10000.0f; //0.1f .. 100 ms
  75. float stretch = powf(440.0f / freq,
  76. pars->GlobalPar.PPunchStretch / 64.0f);
  77. NoteGlobalPar.Punch.dt = 1.0f / (time * synth->samplerate_f * stretch);
  78. }
  79. else
  80. NoteGlobalPar.Punch.Enabled = 0;
  81. for(int nvoice = 0; nvoice < NUM_VOICES; ++nvoice) {
  82. pars->VoicePar[nvoice].OscilSmp->newrandseed(prng());
  83. NoteVoicePar[nvoice].OscilSmp = NULL;
  84. NoteVoicePar[nvoice].FMSmp = NULL;
  85. NoteVoicePar[nvoice].VoiceOut = NULL;
  86. NoteVoicePar[nvoice].FMVoice = -1;
  87. unison_size[nvoice] = 1;
  88. if(pars->VoicePar[nvoice].Enabled == 0) {
  89. NoteVoicePar[nvoice].Enabled = OFF;
  90. continue; //the voice is disabled
  91. }
  92. unison_stereo_spread[nvoice] =
  93. pars->VoicePar[nvoice].Unison_stereo_spread / 127.0f;
  94. int unison = pars->VoicePar[nvoice].Unison_size;
  95. if(unison < 1)
  96. unison = 1;
  97. //compute unison
  98. unison_size[nvoice] = unison;
  99. unison_base_freq_rap[nvoice] = new float[unison];
  100. unison_freq_rap[nvoice] = new float[unison];
  101. unison_invert_phase[nvoice] = new bool[unison];
  102. float unison_spread = pars->getUnisonFrequencySpreadCents(
  103. nvoice);
  104. float unison_real_spread = powf(2.0f, (unison_spread * 0.5f) / 1200.0f);
  105. float unison_vibratto_a = pars->VoicePar[nvoice].Unison_vibratto
  106. / 127.0f; //0.0f .. 1.0f
  107. switch(unison) {
  108. case 1:
  109. unison_base_freq_rap[nvoice][0] = 1.0f; //if the unison is not used, always make the only subvoice to have the default note
  110. break;
  111. case 2: { //unison for 2 subvoices
  112. unison_base_freq_rap[nvoice][0] = 1.0f / unison_real_spread;
  113. unison_base_freq_rap[nvoice][1] = unison_real_spread;
  114. };
  115. break;
  116. default: { //unison for more than 2 subvoices
  117. float unison_values[unison];
  118. float min = -1e-6, max = 1e-6;
  119. for(int k = 0; k < unison; ++k) {
  120. float step = (k / (float) (unison - 1)) * 2.0f - 1.0f; //this makes the unison spread more uniform
  121. float val = step + (RND * 2.0f - 1.0f) / (unison - 1);
  122. unison_values[k] = val;
  123. if (min > val) {
  124. min = val;
  125. }
  126. if (max < val) {
  127. max = val;
  128. }
  129. }
  130. float diff = max - min;
  131. for(int k = 0; k < unison; ++k) {
  132. unison_values[k] =
  133. (unison_values[k] - (max + min) * 0.5f) / diff; //the lowest value will be -1 and the highest will be 1
  134. unison_base_freq_rap[nvoice][k] =
  135. powf(2.0f, (unison_spread * unison_values[k]) / 1200);
  136. }
  137. };
  138. }
  139. //unison vibrattos
  140. if(unison > 1)
  141. for(int k = 0; k < unison; ++k) //reduce the frequency difference for larger vibrattos
  142. unison_base_freq_rap[nvoice][k] = 1.0f
  143. + (unison_base_freq_rap[
  144. nvoice][k] - 1.0f)
  145. * (1.0f - unison_vibratto_a);
  146. unison_vibratto[nvoice].step = new float[unison];
  147. unison_vibratto[nvoice].position = new float[unison];
  148. unison_vibratto[nvoice].amplitude =
  149. (unison_real_spread - 1.0f) * unison_vibratto_a;
  150. float increments_per_second = synth->samplerate_f / synth->buffersize_f;
  151. float vibratto_base_period = 0.25f
  152. * powf(
  153. 2.0f,
  154. (1.0f
  155. - pars->VoicePar[nvoice].
  156. Unison_vibratto_speed
  157. / 127.0f) * 4.0f);
  158. for(int k = 0; k < unison; ++k) {
  159. unison_vibratto[nvoice].position[k] = RND * 1.8f - 0.9f;
  160. //make period to vary randomly from 50% to 200% vibratto base period
  161. float vibratto_period = vibratto_base_period
  162. * powf(2.0f, RND * 2.0f - 1.0f);
  163. float m = 4.0f / (vibratto_period * increments_per_second);
  164. if(RND < 0.5f)
  165. m = -m;
  166. unison_vibratto[nvoice].step[k] = m;
  167. }
  168. if(unison == 1) { //no vibratto for a single voice
  169. unison_vibratto[nvoice].step[0] = 0.0f;
  170. unison_vibratto[nvoice].position[0] = 0.0f;
  171. unison_vibratto[nvoice].amplitude = 0.0f;
  172. }
  173. //phase invert for unison
  174. unison_invert_phase[nvoice][0] = false;
  175. if(unison != 1) {
  176. int inv = pars->VoicePar[nvoice].Unison_invert_phase;
  177. switch(inv) {
  178. case 0: for(int k = 0; k < unison; ++k)
  179. unison_invert_phase[nvoice][k] = false;
  180. break;
  181. case 1: for(int k = 0; k < unison; ++k)
  182. unison_invert_phase[nvoice][k] = (RND > 0.5f);
  183. break;
  184. default: for(int k = 0; k < unison; ++k)
  185. unison_invert_phase[nvoice][k] =
  186. (k % inv == 0) ? true : false;
  187. break;
  188. }
  189. }
  190. oscfreqhi[nvoice] = new int[unison];
  191. oscfreqlo[nvoice] = new float[unison];
  192. oscfreqhiFM[nvoice] = new unsigned int[unison];
  193. oscfreqloFM[nvoice] = new float[unison];
  194. oscposhi[nvoice] = new int[unison];
  195. oscposlo[nvoice] = new float[unison];
  196. oscposhiFM[nvoice] = new unsigned int[unison];
  197. oscposloFM[nvoice] = new float[unison];
  198. NoteVoicePar[nvoice].Enabled = ON;
  199. NoteVoicePar[nvoice].fixedfreq = pars->VoicePar[nvoice].Pfixedfreq;
  200. NoteVoicePar[nvoice].fixedfreqET = pars->VoicePar[nvoice].PfixedfreqET;
  201. //use the Globalpars.detunetype if the detunetype is 0
  202. if(pars->VoicePar[nvoice].PDetuneType != 0) {
  203. NoteVoicePar[nvoice].Detune = getdetune(
  204. pars->VoicePar[nvoice].PDetuneType,
  205. pars->VoicePar[nvoice].
  206. PCoarseDetune,
  207. 8192); //coarse detune
  208. NoteVoicePar[nvoice].FineDetune = getdetune(
  209. pars->VoicePar[nvoice].PDetuneType,
  210. 0,
  211. pars->VoicePar[nvoice].PDetune); //fine detune
  212. }
  213. else {
  214. NoteVoicePar[nvoice].Detune = getdetune(
  215. pars->GlobalPar.PDetuneType,
  216. pars->VoicePar[nvoice].
  217. PCoarseDetune,
  218. 8192); //coarse detune
  219. NoteVoicePar[nvoice].FineDetune = getdetune(
  220. pars->GlobalPar.PDetuneType,
  221. 0,
  222. pars->VoicePar[nvoice].PDetune); //fine detune
  223. }
  224. if(pars->VoicePar[nvoice].PFMDetuneType != 0)
  225. NoteVoicePar[nvoice].FMDetune = getdetune(
  226. pars->VoicePar[nvoice].PFMDetuneType,
  227. pars->VoicePar[nvoice].
  228. PFMCoarseDetune,
  229. pars->VoicePar[nvoice].PFMDetune);
  230. else
  231. NoteVoicePar[nvoice].FMDetune = getdetune(
  232. pars->GlobalPar.PDetuneType,
  233. pars->VoicePar[nvoice].
  234. PFMCoarseDetune,
  235. pars->VoicePar[nvoice].PFMDetune);
  236. for(int k = 0; k < unison; ++k) {
  237. oscposhi[nvoice][k] = 0;
  238. oscposlo[nvoice][k] = 0.0f;
  239. oscposhiFM[nvoice][k] = 0;
  240. oscposloFM[nvoice][k] = 0.0f;
  241. }
  242. //the extra points contains the first point
  243. NoteVoicePar[nvoice].OscilSmp =
  244. new float[synth->oscilsize + OSCIL_SMP_EXTRA_SAMPLES];
  245. //Get the voice's oscil or external's voice oscil
  246. int vc = nvoice;
  247. if(pars->VoicePar[nvoice].Pextoscil != -1)
  248. vc = pars->VoicePar[nvoice].Pextoscil;
  249. if(!pars->GlobalPar.Hrandgrouping)
  250. pars->VoicePar[vc].OscilSmp->newrandseed(prng());
  251. int oscposhi_start =
  252. pars->VoicePar[vc].OscilSmp->get(NoteVoicePar[nvoice].OscilSmp,
  253. getvoicebasefreq(nvoice),
  254. pars->VoicePar[nvoice].Presonance);
  255. //I store the first elments to the last position for speedups
  256. for(int i = 0; i < OSCIL_SMP_EXTRA_SAMPLES; ++i)
  257. NoteVoicePar[nvoice].OscilSmp[synth->oscilsize
  258. + i] =
  259. NoteVoicePar[nvoice].OscilSmp[i];
  260. oscposhi_start +=
  261. (int)((pars->VoicePar[nvoice].Poscilphase
  262. - 64.0f) / 128.0f * synth->oscilsize + synth->oscilsize * 4);
  263. oscposhi_start %= synth->oscilsize;
  264. for(int k = 0; k < unison; ++k) {
  265. oscposhi[nvoice][k] = oscposhi_start;
  266. //put random starting point for other subvoices
  267. oscposhi_start =
  268. (int)(RND * pars->VoicePar[nvoice].Unison_phase_randomness /
  269. 127.0f * (synth->oscilsize - 1));
  270. }
  271. NoteVoicePar[nvoice].FreqLfo = NULL;
  272. NoteVoicePar[nvoice].FreqEnvelope = NULL;
  273. NoteVoicePar[nvoice].AmpLfo = NULL;
  274. NoteVoicePar[nvoice].AmpEnvelope = NULL;
  275. NoteVoicePar[nvoice].VoiceFilterL = NULL;
  276. NoteVoicePar[nvoice].VoiceFilterR = NULL;
  277. NoteVoicePar[nvoice].FilterEnvelope = NULL;
  278. NoteVoicePar[nvoice].FilterLfo = NULL;
  279. NoteVoicePar[nvoice].FilterCenterPitch =
  280. pars->VoicePar[nvoice].VoiceFilter->getfreq();
  281. NoteVoicePar[nvoice].filterbypass =
  282. pars->VoicePar[nvoice].Pfilterbypass;
  283. switch(pars->VoicePar[nvoice].PFMEnabled) {
  284. case 1:
  285. NoteVoicePar[nvoice].FMEnabled = MORPH;
  286. break;
  287. case 2:
  288. NoteVoicePar[nvoice].FMEnabled = RING_MOD;
  289. break;
  290. case 3:
  291. NoteVoicePar[nvoice].FMEnabled = PHASE_MOD;
  292. break;
  293. case 4:
  294. NoteVoicePar[nvoice].FMEnabled = FREQ_MOD;
  295. break;
  296. case 5:
  297. NoteVoicePar[nvoice].FMEnabled = PITCH_MOD;
  298. break;
  299. default:
  300. NoteVoicePar[nvoice].FMEnabled = NONE;
  301. }
  302. NoteVoicePar[nvoice].FMVoice = pars->VoicePar[nvoice].PFMVoice;
  303. NoteVoicePar[nvoice].FMFreqEnvelope = NULL;
  304. NoteVoicePar[nvoice].FMAmpEnvelope = NULL;
  305. //Compute the Voice's modulator volume (incl. damping)
  306. float fmvoldamp = powf(440.0f / getvoicebasefreq(
  307. nvoice),
  308. pars->VoicePar[nvoice].PFMVolumeDamp / 64.0f
  309. - 1.0f);
  310. switch(NoteVoicePar[nvoice].FMEnabled) {
  311. case PHASE_MOD:
  312. fmvoldamp =
  313. powf(440.0f / getvoicebasefreq(
  314. nvoice), pars->VoicePar[nvoice].PFMVolumeDamp
  315. / 64.0f);
  316. NoteVoicePar[nvoice].FMVolume =
  317. (expf(pars->VoicePar[nvoice].PFMVolume / 127.0f
  318. * FM_AMP_MULTIPLIER) - 1.0f) * fmvoldamp * 4.0f;
  319. break;
  320. case FREQ_MOD:
  321. NoteVoicePar[nvoice].FMVolume =
  322. (expf(pars->VoicePar[nvoice].PFMVolume / 127.0f
  323. * FM_AMP_MULTIPLIER) - 1.0f) * fmvoldamp * 4.0f;
  324. break;
  325. // case PITCH_MOD:NoteVoicePar[nvoice].FMVolume=(pars->VoicePar[nvoice].PFMVolume/127.0f*8.0f)*fmvoldamp;//???????????
  326. // break;
  327. default:
  328. if(fmvoldamp > 1.0f)
  329. fmvoldamp = 1.0f;
  330. NoteVoicePar[nvoice].FMVolume =
  331. pars->VoicePar[nvoice].PFMVolume
  332. / 127.0f * fmvoldamp;
  333. }
  334. //Voice's modulator velocity sensing
  335. NoteVoicePar[nvoice].FMVolume *=
  336. VelF(velocity,
  337. partparams->VoicePar[nvoice].PFMVelocityScaleFunction);
  338. FMoldsmp[nvoice] = new float [unison];
  339. for(int k = 0; k < unison; ++k)
  340. FMoldsmp[nvoice][k] = 0.0f; //this is for FM (integration)
  341. firsttick[nvoice] = 1;
  342. NoteVoicePar[nvoice].DelayTicks =
  343. (int)((expf(pars->VoicePar[nvoice].PDelay / 127.0f
  344. * logf(50.0f))
  345. - 1.0f) / synth->buffersize_f / 10.0f * synth->samplerate_f);
  346. }
  347. max_unison = 1;
  348. for(int nvoice = 0; nvoice < NUM_VOICES; ++nvoice)
  349. if(unison_size[nvoice] > max_unison)
  350. max_unison = unison_size[nvoice];
  351. tmpwave_unison = new float *[max_unison];
  352. for(int k = 0; k < max_unison; ++k) {
  353. tmpwave_unison[k] = new float[synth->buffersize];
  354. memset(tmpwave_unison[k], 0, synth->bufferbytes);
  355. }
  356. initparameters();
  357. }
  358. // ADlegatonote: This function is (mostly) a copy of ADnote(...) and
  359. // initparameters() stuck together with some lines removed so that it
  360. // only alter the already playing note (to perform legato). It is
  361. // possible I left stuff that is not required for this.
  362. void ADnote::legatonote(float freq, float velocity, int portamento_,
  363. int midinote_, bool externcall)
  364. {
  365. ADnoteParameters *pars = partparams;
  366. // Manage legato stuff
  367. if(legato.update(freq, velocity, portamento_, midinote_, externcall))
  368. return;
  369. portamento = portamento_;
  370. midinote = midinote_;
  371. basefreq = freq;
  372. if(velocity > 1.0f)
  373. velocity = 1.0f;
  374. this->velocity = velocity;
  375. NoteGlobalPar.Detune = getdetune(pars->GlobalPar.PDetuneType,
  376. pars->GlobalPar.PCoarseDetune,
  377. pars->GlobalPar.PDetune);
  378. bandwidthDetuneMultiplier = pars->getBandwidthDetuneMultiplier();
  379. if(pars->GlobalPar.PPanning == 0)
  380. NoteGlobalPar.Panning = RND;
  381. else
  382. NoteGlobalPar.Panning = pars->GlobalPar.PPanning / 128.0f;
  383. //center freq
  384. NoteGlobalPar.FilterCenterPitch = pars->GlobalPar.GlobalFilter->getfreq()
  385. + pars->GlobalPar.PFilterVelocityScale
  386. / 127.0f * 6.0f //velocity sensing
  387. * (VelF(velocity,
  388. pars->GlobalPar.
  389. PFilterVelocityScaleFunction) - 1);
  390. for(int nvoice = 0; nvoice < NUM_VOICES; ++nvoice) {
  391. if(NoteVoicePar[nvoice].Enabled == OFF)
  392. continue; //(gf) Stay the same as first note in legato.
  393. NoteVoicePar[nvoice].fixedfreq = pars->VoicePar[nvoice].Pfixedfreq;
  394. NoteVoicePar[nvoice].fixedfreqET = pars->VoicePar[nvoice].PfixedfreqET;
  395. //use the Globalpars.detunetype if the detunetype is 0
  396. if(pars->VoicePar[nvoice].PDetuneType != 0) {
  397. NoteVoicePar[nvoice].Detune = getdetune(
  398. pars->VoicePar[nvoice].PDetuneType,
  399. pars->VoicePar[nvoice].PCoarseDetune,
  400. 8192); //coarse detune
  401. NoteVoicePar[nvoice].FineDetune = getdetune(
  402. pars->VoicePar[nvoice].PDetuneType,
  403. 0,
  404. pars->VoicePar[nvoice].PDetune); //fine detune
  405. }
  406. else {
  407. NoteVoicePar[nvoice].Detune = getdetune(
  408. pars->GlobalPar.PDetuneType,
  409. pars->VoicePar[nvoice].PCoarseDetune,
  410. 8192); //coarse detune
  411. NoteVoicePar[nvoice].FineDetune = getdetune(
  412. pars->GlobalPar.PDetuneType,
  413. 0,
  414. pars->VoicePar[nvoice].PDetune); //fine detune
  415. }
  416. if(pars->VoicePar[nvoice].PFMDetuneType != 0)
  417. NoteVoicePar[nvoice].FMDetune = getdetune(
  418. pars->VoicePar[nvoice].PFMDetuneType,
  419. pars->VoicePar[nvoice].PFMCoarseDetune,
  420. pars->VoicePar[nvoice].PFMDetune);
  421. else
  422. NoteVoicePar[nvoice].FMDetune = getdetune(
  423. pars->GlobalPar.PDetuneType,
  424. pars->VoicePar[nvoice].PFMCoarseDetune,
  425. pars->VoicePar[nvoice].PFMDetune);
  426. //Get the voice's oscil or external's voice oscil
  427. int vc = nvoice;
  428. if(pars->VoicePar[nvoice].Pextoscil != -1)
  429. vc = pars->VoicePar[nvoice].Pextoscil;
  430. if(!pars->GlobalPar.Hrandgrouping)
  431. pars->VoicePar[vc].OscilSmp->newrandseed(prng());
  432. pars->VoicePar[vc].OscilSmp->get(NoteVoicePar[nvoice].OscilSmp,
  433. getvoicebasefreq(nvoice),
  434. pars->VoicePar[nvoice].Presonance); //(gf)Modif of the above line.
  435. //I store the first elments to the last position for speedups
  436. for(int i = 0; i < OSCIL_SMP_EXTRA_SAMPLES; ++i)
  437. NoteVoicePar[nvoice].OscilSmp[synth->oscilsize
  438. + i] =
  439. NoteVoicePar[nvoice].OscilSmp[i];
  440. NoteVoicePar[nvoice].FilterCenterPitch =
  441. pars->VoicePar[nvoice].VoiceFilter->getfreq();
  442. NoteVoicePar[nvoice].filterbypass =
  443. pars->VoicePar[nvoice].Pfilterbypass;
  444. NoteVoicePar[nvoice].FMVoice = pars->VoicePar[nvoice].PFMVoice;
  445. //Compute the Voice's modulator volume (incl. damping)
  446. float fmvoldamp = powf(440.0f / getvoicebasefreq(nvoice),
  447. pars->VoicePar[nvoice].PFMVolumeDamp / 64.0f
  448. - 1.0f);
  449. switch(NoteVoicePar[nvoice].FMEnabled) {
  450. case PHASE_MOD:
  451. fmvoldamp =
  452. powf(440.0f / getvoicebasefreq(
  453. nvoice), pars->VoicePar[nvoice].PFMVolumeDamp
  454. / 64.0f);
  455. NoteVoicePar[nvoice].FMVolume =
  456. (expf(pars->VoicePar[nvoice].PFMVolume / 127.0f
  457. * FM_AMP_MULTIPLIER) - 1.0f) * fmvoldamp * 4.0f;
  458. break;
  459. case FREQ_MOD:
  460. NoteVoicePar[nvoice].FMVolume =
  461. (expf(pars->VoicePar[nvoice].PFMVolume / 127.0f
  462. * FM_AMP_MULTIPLIER) - 1.0f) * fmvoldamp * 4.0f;
  463. break;
  464. // case PITCH_MOD:NoteVoicePar[nvoice].FMVolume=(pars->VoicePar[nvoice].PFMVolume/127.0f*8.0f)*fmvoldamp;//???????????
  465. // break;
  466. default:
  467. if(fmvoldamp > 1.0f)
  468. fmvoldamp = 1.0f;
  469. NoteVoicePar[nvoice].FMVolume =
  470. pars->VoicePar[nvoice].PFMVolume
  471. / 127.0f * fmvoldamp;
  472. }
  473. //Voice's modulator velocity sensing
  474. NoteVoicePar[nvoice].FMVolume *=
  475. VelF(velocity,
  476. partparams->VoicePar[nvoice].PFMVelocityScaleFunction);
  477. NoteVoicePar[nvoice].DelayTicks =
  478. (int)((expf(pars->VoicePar[nvoice].PDelay / 127.0f
  479. * logf(50.0f))
  480. - 1.0f) / synth->buffersize_f / 10.0f * synth->samplerate_f);
  481. }
  482. /// initparameters();
  483. ///////////////
  484. // Altered content of initparameters():
  485. int tmp[NUM_VOICES];
  486. NoteGlobalPar.Volume = 4.0f
  487. * powf(0.1f, 3.0f
  488. * (1.0f - partparams->GlobalPar.PVolume
  489. / 96.0f)) //-60 dB .. 0 dB
  490. * VelF(
  491. velocity,
  492. partparams->GlobalPar.
  493. PAmpVelocityScaleFunction); //velocity sensing
  494. globalnewamplitude = NoteGlobalPar.Volume
  495. * NoteGlobalPar.AmpEnvelope->envout_dB()
  496. * NoteGlobalPar.AmpLfo->amplfoout();
  497. NoteGlobalPar.FilterQ = partparams->GlobalPar.GlobalFilter->getq();
  498. NoteGlobalPar.FilterFreqTracking =
  499. partparams->GlobalPar.GlobalFilter->getfreqtracking(basefreq);
  500. // Forbids the Modulation Voice to be greater or equal than voice
  501. for(int i = 0; i < NUM_VOICES; ++i)
  502. if(NoteVoicePar[i].FMVoice >= i)
  503. NoteVoicePar[i].FMVoice = -1;
  504. // Voice Parameter init
  505. for(unsigned nvoice = 0; nvoice < NUM_VOICES; ++nvoice) {
  506. if(NoteVoicePar[nvoice].Enabled == 0)
  507. continue;
  508. NoteVoicePar[nvoice].noisetype = partparams->VoicePar[nvoice].Type;
  509. /* Voice Amplitude Parameters Init */
  510. NoteVoicePar[nvoice].Volume =
  511. powf(0.1f, 3.0f
  512. * (1.0f - partparams->VoicePar[nvoice].PVolume / 127.0f)) // -60 dB .. 0 dB
  513. * VelF(velocity,
  514. partparams->VoicePar[nvoice].PAmpVelocityScaleFunction); //velocity
  515. if(partparams->VoicePar[nvoice].PVolumeminus != 0)
  516. NoteVoicePar[nvoice].Volume = -NoteVoicePar[nvoice].Volume;
  517. if(partparams->VoicePar[nvoice].PPanning == 0)
  518. NoteVoicePar[nvoice].Panning = RND; // random panning
  519. else
  520. NoteVoicePar[nvoice].Panning =
  521. partparams->VoicePar[nvoice].PPanning / 128.0f;
  522. newamplitude[nvoice] = 1.0f;
  523. if((partparams->VoicePar[nvoice].PAmpEnvelopeEnabled != 0)
  524. && (NoteVoicePar[nvoice].AmpEnvelope != NULL))
  525. newamplitude[nvoice] *= NoteVoicePar[nvoice].AmpEnvelope->envout_dB();
  526. if((partparams->VoicePar[nvoice].PAmpLfoEnabled != 0)
  527. && (NoteVoicePar[nvoice].AmpLfo != NULL))
  528. newamplitude[nvoice] *= NoteVoicePar[nvoice].AmpLfo->amplfoout();
  529. NoteVoicePar[nvoice].FilterFreqTracking =
  530. partparams->VoicePar[nvoice].VoiceFilter->getfreqtracking(basefreq);
  531. /* Voice Modulation Parameters Init */
  532. if((NoteVoicePar[nvoice].FMEnabled != NONE)
  533. && (NoteVoicePar[nvoice].FMVoice < 0)) {
  534. partparams->VoicePar[nvoice].FMSmp->newrandseed(prng());
  535. //Perform Anti-aliasing only on MORPH or RING MODULATION
  536. int vc = nvoice;
  537. if(partparams->VoicePar[nvoice].PextFMoscil != -1)
  538. vc = partparams->VoicePar[nvoice].PextFMoscil;
  539. if(!partparams->GlobalPar.Hrandgrouping)
  540. partparams->VoicePar[vc].FMSmp->newrandseed(prng());
  541. for(int i = 0; i < OSCIL_SMP_EXTRA_SAMPLES; ++i)
  542. NoteVoicePar[nvoice].FMSmp[synth->oscilsize + i] =
  543. NoteVoicePar[nvoice].FMSmp[i];
  544. }
  545. FMnewamplitude[nvoice] = NoteVoicePar[nvoice].FMVolume
  546. * ctl->fmamp.relamp;
  547. if((partparams->VoicePar[nvoice].PFMAmpEnvelopeEnabled != 0)
  548. && (NoteVoicePar[nvoice].FMAmpEnvelope != NULL))
  549. FMnewamplitude[nvoice] *=
  550. NoteVoicePar[nvoice].FMAmpEnvelope->envout_dB();
  551. }
  552. for(int nvoice = 0; nvoice < NUM_VOICES; ++nvoice) {
  553. for(unsigned i = nvoice + 1; i < NUM_VOICES; ++i)
  554. tmp[i] = 0;
  555. for(unsigned i = nvoice + 1; i < NUM_VOICES; ++i)
  556. if((NoteVoicePar[i].FMVoice == nvoice) && (tmp[i] == 0))
  557. tmp[i] = 1;
  558. }
  559. }
  560. /*
  561. * Kill a voice of ADnote
  562. */
  563. void ADnote::KillVoice(int nvoice)
  564. {
  565. delete [] oscfreqhi[nvoice];
  566. delete [] oscfreqlo[nvoice];
  567. delete [] oscfreqhiFM[nvoice];
  568. delete [] oscfreqloFM[nvoice];
  569. delete [] oscposhi[nvoice];
  570. delete [] oscposlo[nvoice];
  571. delete [] oscposhiFM[nvoice];
  572. delete [] oscposloFM[nvoice];
  573. delete [] unison_base_freq_rap[nvoice];
  574. delete [] unison_freq_rap[nvoice];
  575. delete [] unison_invert_phase[nvoice];
  576. delete [] FMoldsmp[nvoice];
  577. delete [] unison_vibratto[nvoice].step;
  578. delete [] unison_vibratto[nvoice].position;
  579. NoteVoicePar[nvoice].kill();
  580. }
  581. /*
  582. * Kill the note
  583. */
  584. void ADnote::KillNote()
  585. {
  586. for(unsigned nvoice = 0; nvoice < NUM_VOICES; ++nvoice) {
  587. if(NoteVoicePar[nvoice].Enabled == ON)
  588. KillVoice(nvoice);
  589. if(NoteVoicePar[nvoice].VoiceOut)
  590. delete NoteVoicePar[nvoice].VoiceOut;
  591. NoteVoicePar[nvoice].VoiceOut = NULL;
  592. }
  593. NoteGlobalPar.kill();
  594. NoteEnabled = OFF;
  595. }
  596. ADnote::~ADnote()
  597. {
  598. if(NoteEnabled == ON)
  599. KillNote();
  600. delete [] tmpwavel;
  601. delete [] tmpwaver;
  602. delete [] bypassl;
  603. delete [] bypassr;
  604. for(int k = 0; k < max_unison; ++k)
  605. delete[] tmpwave_unison[k];
  606. delete[] tmpwave_unison;
  607. }
  608. /*
  609. * Init the parameters
  610. */
  611. void ADnote::initparameters()
  612. {
  613. int tmp[NUM_VOICES];
  614. // Global Parameters
  615. NoteGlobalPar.initparameters(partparams->GlobalPar, basefreq, velocity,
  616. stereo);
  617. NoteGlobalPar.AmpEnvelope->envout_dB(); //discard the first envelope output
  618. globalnewamplitude = NoteGlobalPar.Volume
  619. * NoteGlobalPar.AmpEnvelope->envout_dB()
  620. * NoteGlobalPar.AmpLfo->amplfoout();
  621. // Forbids the Modulation Voice to be greater or equal than voice
  622. for(int i = 0; i < NUM_VOICES; ++i)
  623. if(NoteVoicePar[i].FMVoice >= i)
  624. NoteVoicePar[i].FMVoice = -1;
  625. // Voice Parameter init
  626. for(int nvoice = 0; nvoice < NUM_VOICES; ++nvoice) {
  627. Voice &vce = NoteVoicePar[nvoice];
  628. ADnoteVoiceParam &param = partparams->VoicePar[nvoice];
  629. if(vce.Enabled == 0)
  630. continue;
  631. vce.noisetype = param.Type;
  632. /* Voice Amplitude Parameters Init */
  633. vce.Volume = powf(0.1f, 3.0f * (1.0f - param.PVolume / 127.0f)) // -60dB..0dB
  634. * VelF(velocity, param.PAmpVelocityScaleFunction);
  635. if(param.PVolumeminus)
  636. vce.Volume = -vce.Volume;
  637. if(param.PPanning == 0)
  638. vce.Panning = RND; // random panning
  639. else
  640. vce.Panning = param.PPanning / 128.0f;
  641. newamplitude[nvoice] = 1.0f;
  642. if(param.PAmpEnvelopeEnabled) {
  643. vce.AmpEnvelope = new Envelope(param.AmpEnvelope, basefreq);
  644. vce.AmpEnvelope->envout_dB(); //discard the first envelope sample
  645. newamplitude[nvoice] *= vce.AmpEnvelope->envout_dB();
  646. }
  647. if(param.PAmpLfoEnabled) {
  648. vce.AmpLfo = new LFO(param.AmpLfo, basefreq);
  649. newamplitude[nvoice] *= vce.AmpLfo->amplfoout();
  650. }
  651. /* Voice Frequency Parameters Init */
  652. if(param.PFreqEnvelopeEnabled != 0)
  653. vce.FreqEnvelope = new Envelope(param.FreqEnvelope, basefreq);
  654. if(param.PFreqLfoEnabled != 0)
  655. vce.FreqLfo = new LFO(param.FreqLfo, basefreq);
  656. /* Voice Filter Parameters Init */
  657. if(param.PFilterEnabled != 0) {
  658. vce.VoiceFilterL = Filter::generate(param.VoiceFilter);
  659. vce.VoiceFilterR = Filter::generate(param.VoiceFilter);
  660. }
  661. if(param.PFilterEnvelopeEnabled != 0)
  662. vce.FilterEnvelope = new Envelope(param.FilterEnvelope, basefreq);
  663. if(param.PFilterLfoEnabled != 0)
  664. vce.FilterLfo = new LFO(param.FilterLfo, basefreq);
  665. vce.FilterFreqTracking =
  666. param.VoiceFilter->getfreqtracking(basefreq);
  667. /* Voice Modulation Parameters Init */
  668. if((vce.FMEnabled != NONE) && (vce.FMVoice < 0)) {
  669. param.FMSmp->newrandseed(prng());
  670. vce.FMSmp = new float[synth->oscilsize + OSCIL_SMP_EXTRA_SAMPLES];
  671. //Perform Anti-aliasing only on MORPH or RING MODULATION
  672. int vc = nvoice;
  673. if(param.PextFMoscil != -1)
  674. vc = param.PextFMoscil;
  675. float tmp = 1.0f;
  676. if((partparams->VoicePar[vc].FMSmp->Padaptiveharmonics != 0)
  677. || (vce.FMEnabled == MORPH)
  678. || (vce.FMEnabled == RING_MOD))
  679. tmp = getFMvoicebasefreq(nvoice);
  680. if(!partparams->GlobalPar.Hrandgrouping)
  681. partparams->VoicePar[vc].FMSmp->newrandseed(prng());
  682. for(int k = 0; k < unison_size[nvoice]; ++k)
  683. oscposhiFM[nvoice][k] = (oscposhi[nvoice][k]
  684. + partparams->VoicePar[vc].FMSmp->get(
  685. vce.FMSmp, tmp))
  686. % synth->oscilsize;
  687. for(int i = 0; i < OSCIL_SMP_EXTRA_SAMPLES; ++i)
  688. vce.FMSmp[synth->oscilsize + i] = vce.FMSmp[i];
  689. int oscposhiFM_add =
  690. (int)((param.PFMoscilphase
  691. - 64.0f) / 128.0f * synth->oscilsize
  692. + synth->oscilsize * 4);
  693. for(int k = 0; k < unison_size[nvoice]; ++k) {
  694. oscposhiFM[nvoice][k] += oscposhiFM_add;
  695. oscposhiFM[nvoice][k] %= synth->oscilsize;
  696. }
  697. }
  698. if(param.PFMFreqEnvelopeEnabled != 0)
  699. vce.FMFreqEnvelope = new Envelope(param.FMFreqEnvelope, basefreq);
  700. FMnewamplitude[nvoice] = vce.FMVolume * ctl->fmamp.relamp;
  701. if(param.PFMAmpEnvelopeEnabled != 0) {
  702. vce.FMAmpEnvelope = new Envelope(param.FMAmpEnvelope,
  703. basefreq);
  704. FMnewamplitude[nvoice] *= vce.FMAmpEnvelope->envout_dB();
  705. }
  706. }
  707. for(int nvoice = 0; nvoice < NUM_VOICES; ++nvoice) {
  708. for(int i = nvoice + 1; i < NUM_VOICES; ++i)
  709. tmp[i] = 0;
  710. for(int i = nvoice + 1; i < NUM_VOICES; ++i)
  711. if((NoteVoicePar[i].FMVoice == nvoice) && (tmp[i] == 0)) {
  712. NoteVoicePar[nvoice].VoiceOut = new float[synth->buffersize];
  713. tmp[i] = 1;
  714. }
  715. if(NoteVoicePar[nvoice].VoiceOut)
  716. memset(NoteVoicePar[nvoice].VoiceOut, 0, synth->bufferbytes);
  717. }
  718. }
  719. /*
  720. * Computes the relative frequency of each unison voice and it's vibratto
  721. * This must be called before setfreq* functions
  722. */
  723. void ADnote::compute_unison_freq_rap(int nvoice) {
  724. if(unison_size[nvoice] == 1) { //no unison
  725. unison_freq_rap[nvoice][0] = 1.0f;
  726. return;
  727. }
  728. float relbw = ctl->bandwidth.relbw * bandwidthDetuneMultiplier;
  729. for(int k = 0; k < unison_size[nvoice]; ++k) {
  730. float pos = unison_vibratto[nvoice].position[k];
  731. float step = unison_vibratto[nvoice].step[k];
  732. pos += step;
  733. if(pos <= -1.0f) {
  734. pos = -1.0f;
  735. step = -step;
  736. }
  737. if(pos >= 1.0f) {
  738. pos = 1.0f;
  739. step = -step;
  740. }
  741. float vibratto_val = (pos - 0.333333333f * pos * pos * pos) * 1.5f; //make the vibratto lfo smoother
  742. unison_freq_rap[nvoice][k] = 1.0f
  743. + ((unison_base_freq_rap[nvoice][k]
  744. - 1.0f) + vibratto_val
  745. * unison_vibratto[nvoice].amplitude)
  746. * relbw;
  747. unison_vibratto[nvoice].position[k] = pos;
  748. step = unison_vibratto[nvoice].step[k] = step;
  749. }
  750. }
  751. /*
  752. * Computes the frequency of an oscillator
  753. */
  754. void ADnote::setfreq(int nvoice, float in_freq)
  755. {
  756. for(int k = 0; k < unison_size[nvoice]; ++k) {
  757. float freq = fabs(in_freq) * unison_freq_rap[nvoice][k];
  758. float speed = freq * synth->oscilsize_f / synth->samplerate_f;
  759. if(speed > synth->oscilsize_f)
  760. speed = synth->oscilsize_f;
  761. F2I(speed, oscfreqhi[nvoice][k]);
  762. oscfreqlo[nvoice][k] = speed - floor(speed);
  763. }
  764. }
  765. /*
  766. * Computes the frequency of an modullator oscillator
  767. */
  768. void ADnote::setfreqFM(int nvoice, float in_freq)
  769. {
  770. for(int k = 0; k < unison_size[nvoice]; ++k) {
  771. float freq = fabs(in_freq) * unison_freq_rap[nvoice][k];
  772. float speed = freq * synth->oscilsize_f / synth->samplerate_f;
  773. if(speed > synth->samplerate_f)
  774. speed = synth->samplerate_f;
  775. F2I(speed, oscfreqhiFM[nvoice][k]);
  776. oscfreqloFM[nvoice][k] = speed - floor(speed);
  777. }
  778. }
  779. /*
  780. * Get Voice base frequency
  781. */
  782. float ADnote::getvoicebasefreq(int nvoice) const
  783. {
  784. float detune = NoteVoicePar[nvoice].Detune / 100.0f
  785. + NoteVoicePar[nvoice].FineDetune / 100.0f
  786. * ctl->bandwidth.relbw * bandwidthDetuneMultiplier
  787. + NoteGlobalPar.Detune / 100.0f;
  788. if(NoteVoicePar[nvoice].fixedfreq == 0)
  789. return this->basefreq * powf(2, detune / 12.0f);
  790. else { //the fixed freq is enabled
  791. float fixedfreq = 440.0f;
  792. int fixedfreqET = NoteVoicePar[nvoice].fixedfreqET;
  793. if(fixedfreqET != 0) { //if the frequency varies according the keyboard note
  794. float tmp =
  795. (midinote
  796. - 69.0f) / 12.0f
  797. * (powf(2.0f, (fixedfreqET - 1) / 63.0f) - 1.0f);
  798. if(fixedfreqET <= 64)
  799. fixedfreq *= powf(2.0f, tmp);
  800. else
  801. fixedfreq *= powf(3.0f, tmp);
  802. }
  803. return fixedfreq * powf(2.0f, detune / 12.0f);
  804. }
  805. }
  806. /*
  807. * Get Voice's Modullator base frequency
  808. */
  809. float ADnote::getFMvoicebasefreq(int nvoice) const
  810. {
  811. float detune = NoteVoicePar[nvoice].FMDetune / 100.0f;
  812. return getvoicebasefreq(nvoice) * powf(2, detune / 12.0f);
  813. }
  814. /*
  815. * Computes all the parameters for each tick
  816. */
  817. void ADnote::computecurrentparameters()
  818. {
  819. int nvoice;
  820. float voicefreq, voicepitch, filterpitch, filterfreq, FMfreq,
  821. FMrelativepitch, globalpitch, globalfilterpitch;
  822. globalpitch = 0.01f * (NoteGlobalPar.FreqEnvelope->envout()
  823. + NoteGlobalPar.FreqLfo->lfoout()
  824. * ctl->modwheel.relmod);
  825. globaloldamplitude = globalnewamplitude;
  826. globalnewamplitude = NoteGlobalPar.Volume
  827. * NoteGlobalPar.AmpEnvelope->envout_dB()
  828. * NoteGlobalPar.AmpLfo->amplfoout();
  829. globalfilterpitch = NoteGlobalPar.FilterEnvelope->envout()
  830. + NoteGlobalPar.FilterLfo->lfoout()
  831. + NoteGlobalPar.FilterCenterPitch;
  832. float tmpfilterfreq = globalfilterpitch + ctl->filtercutoff.relfreq
  833. + NoteGlobalPar.FilterFreqTracking;
  834. tmpfilterfreq = Filter::getrealfreq(tmpfilterfreq);
  835. float globalfilterq = NoteGlobalPar.FilterQ * ctl->filterq.relq;
  836. NoteGlobalPar.GlobalFilterL->setfreq_and_q(tmpfilterfreq, globalfilterq);
  837. if(stereo != 0)
  838. NoteGlobalPar.GlobalFilterR->setfreq_and_q(tmpfilterfreq, globalfilterq);
  839. //compute the portamento, if it is used by this note
  840. float portamentofreqrap = 1.0f;
  841. if(portamento != 0) { //this voice use portamento
  842. portamentofreqrap = ctl->portamento.freqrap;
  843. if(ctl->portamento.used == 0) //the portamento has finished
  844. portamento = 0; //this note is no longer "portamented"
  845. }
  846. //compute parameters for all voices
  847. for(nvoice = 0; nvoice < NUM_VOICES; ++nvoice) {
  848. if(NoteVoicePar[nvoice].Enabled != ON)
  849. continue;
  850. NoteVoicePar[nvoice].DelayTicks -= 1;
  851. if(NoteVoicePar[nvoice].DelayTicks > 0)
  852. continue;
  853. compute_unison_freq_rap(nvoice);
  854. /*******************/
  855. /* Voice Amplitude */
  856. /*******************/
  857. oldamplitude[nvoice] = newamplitude[nvoice];
  858. newamplitude[nvoice] = 1.0f;
  859. if(NoteVoicePar[nvoice].AmpEnvelope != NULL)
  860. newamplitude[nvoice] *= NoteVoicePar[nvoice].AmpEnvelope->envout_dB();
  861. if(NoteVoicePar[nvoice].AmpLfo != NULL)
  862. newamplitude[nvoice] *= NoteVoicePar[nvoice].AmpLfo->amplfoout();
  863. /****************/
  864. /* Voice Filter */
  865. /****************/
  866. if(NoteVoicePar[nvoice].VoiceFilterL != NULL) {
  867. filterpitch = NoteVoicePar[nvoice].FilterCenterPitch;
  868. if(NoteVoicePar[nvoice].FilterEnvelope != NULL)
  869. filterpitch += NoteVoicePar[nvoice].FilterEnvelope->envout();
  870. if(NoteVoicePar[nvoice].FilterLfo != NULL)
  871. filterpitch += NoteVoicePar[nvoice].FilterLfo->lfoout();
  872. filterfreq = filterpitch + NoteVoicePar[nvoice].FilterFreqTracking;
  873. filterfreq = Filter::getrealfreq(filterfreq);
  874. NoteVoicePar[nvoice].VoiceFilterL->setfreq(filterfreq);
  875. if(stereo && NoteVoicePar[nvoice].VoiceFilterR)
  876. NoteVoicePar[nvoice].VoiceFilterR->setfreq(filterfreq);
  877. }
  878. if(NoteVoicePar[nvoice].noisetype == 0) { //compute only if the voice isn't noise
  879. /*******************/
  880. /* Voice Frequency */
  881. /*******************/
  882. voicepitch = 0.0f;
  883. if(NoteVoicePar[nvoice].FreqLfo != NULL)
  884. voicepitch += NoteVoicePar[nvoice].FreqLfo->lfoout() / 100.0f
  885. * ctl->bandwidth.relbw;
  886. if(NoteVoicePar[nvoice].FreqEnvelope != NULL)
  887. voicepitch += NoteVoicePar[nvoice].FreqEnvelope->envout()
  888. / 100.0f;
  889. voicefreq = getvoicebasefreq(nvoice)
  890. * powf(2, (voicepitch + globalpitch) / 12.0f); //Hz frequency
  891. voicefreq *= ctl->pitchwheel.relfreq; //change the frequency by the controller
  892. setfreq(nvoice, voicefreq * portamentofreqrap);
  893. /***************/
  894. /* Modulator */
  895. /***************/
  896. if(NoteVoicePar[nvoice].FMEnabled != NONE) {
  897. FMrelativepitch = NoteVoicePar[nvoice].FMDetune / 100.0f;
  898. if(NoteVoicePar[nvoice].FMFreqEnvelope != NULL)
  899. FMrelativepitch +=
  900. NoteVoicePar[nvoice].FMFreqEnvelope->envout() / 100;
  901. FMfreq =
  902. powf(2.0f, FMrelativepitch
  903. / 12.0f) * voicefreq * portamentofreqrap;
  904. setfreqFM(nvoice, FMfreq);
  905. FMoldamplitude[nvoice] = FMnewamplitude[nvoice];
  906. FMnewamplitude[nvoice] = NoteVoicePar[nvoice].FMVolume
  907. * ctl->fmamp.relamp;
  908. if(NoteVoicePar[nvoice].FMAmpEnvelope != NULL)
  909. FMnewamplitude[nvoice] *=
  910. NoteVoicePar[nvoice].FMAmpEnvelope->envout_dB();
  911. }
  912. }
  913. }
  914. time += synth->buffersize_f / synth->samplerate_f;
  915. }
  916. /*
  917. * Fadein in a way that removes clicks but keep sound "punchy"
  918. */
  919. inline void ADnote::fadein(float *smps) const
  920. {
  921. int zerocrossings = 0;
  922. for(int i = 1; i < synth->buffersize; ++i)
  923. if((smps[i - 1] < 0.0f) && (smps[i] > 0.0f))
  924. zerocrossings++; //this is only the possitive crossings
  925. float tmp = (synth->buffersize_f - 1.0f) / (zerocrossings + 1) / 3.0f;
  926. if(tmp < 8.0f)
  927. tmp = 8.0f;
  928. int n;
  929. F2I(tmp, n); //how many samples is the fade-in
  930. if(n > synth->buffersize)
  931. n = synth->buffersize;
  932. for(int i = 0; i < n; ++i) { //fade-in
  933. float tmp = 0.5f - cosf((float)i / (float) n * PI) * 0.5f;
  934. smps[i] *= tmp;
  935. }
  936. }
  937. /*
  938. * Computes the Oscillator (Without Modulation) - LinearInterpolation
  939. */
  940. /* As the code here is a bit odd due to optimization, here is what happens
  941. * First the current possition and frequency are retrieved from the running
  942. * state. These are broken up into high and low portions to indicate how many
  943. * samples are skipped in one step and how many fractional samples are skipped.
  944. * Outside of this method the fractional samples are just handled with floating
  945. * point code, but that's a bit slower than it needs to be. In this code the low
  946. * portions are known to exist between 0.0 and 1.0 and it is known that they are
  947. * stored in single precision floating point IEEE numbers. This implies that
  948. * a maximum of 24 bits are significant. The below code does your standard
  949. * linear interpolation that you'll see throughout this codebase, but by
  950. * sticking to integers for tracking the overflow of the low portion, around 15%
  951. * of the execution time was shaved off in the ADnote test.
  952. */
  953. inline void ADnote::ComputeVoiceOscillator_LinearInterpolation(int nvoice)
  954. {
  955. for(int k = 0; k < unison_size[nvoice]; ++k) {
  956. int poshi = oscposhi[nvoice][k];
  957. int poslo = oscposlo[nvoice][k] * (1<<24);
  958. int freqhi = oscfreqhi[nvoice][k];
  959. int freqlo = oscfreqlo[nvoice][k] * (1<<24);
  960. float *smps = NoteVoicePar[nvoice].OscilSmp;
  961. float *tw = tmpwave_unison[k];
  962. assert(oscfreqlo[nvoice][k] < 1.0f);
  963. for(int i = 0; i < synth->buffersize; ++i) {
  964. tw[i] = (smps[poshi] * ((1<<24) - poslo) + smps[poshi + 1] * poslo)/(1.0f*(1<<24));
  965. poslo += freqlo;
  966. poshi += freqhi + (poslo>>24);
  967. poslo &= 0xffffff;
  968. poshi &= synth->oscilsize - 1;
  969. }
  970. oscposhi[nvoice][k] = poshi;
  971. oscposlo[nvoice][k] = poslo/(1.0f*(1<<24));
  972. }
  973. }
  974. /*
  975. * Computes the Oscillator (Without Modulation) - CubicInterpolation
  976. *
  977. The differences from the Linear are to little to deserve to be used. This is because I am using a large synth->oscilsize (>512)
  978. inline void ADnote::ComputeVoiceOscillator_CubicInterpolation(int nvoice){
  979. int i,poshi;
  980. float poslo;
  981. poshi=oscposhi[nvoice];
  982. poslo=oscposlo[nvoice];
  983. float *smps=NoteVoicePar[nvoice].OscilSmp;
  984. float xm1,x0,x1,x2,a,b,c;
  985. for (i=0;i<synth->buffersize;i++){
  986. xm1=smps[poshi];
  987. x0=smps[poshi+1];
  988. x1=smps[poshi+2];
  989. x2=smps[poshi+3];
  990. a=(3.0f * (x0-x1) - xm1 + x2) / 2.0f;
  991. b = 2.0f*x1 + xm1 - (5.0f*x0 + x2) / 2.0f;
  992. c = (x1 - xm1) / 2.0f;
  993. tmpwave[i]=(((a * poslo) + b) * poslo + c) * poslo + x0;
  994. printf("a\n");
  995. //tmpwave[i]=smps[poshi]*(1.0f-poslo)+smps[poshi+1]*poslo;
  996. poslo+=oscfreqlo[nvoice];
  997. if (poslo>=1.0f) {
  998. poslo-=1.0f;
  999. poshi++;
  1000. };
  1001. poshi+=oscfreqhi[nvoice];
  1002. poshi&=synth->oscilsize-1;
  1003. };
  1004. oscposhi[nvoice]=poshi;
  1005. oscposlo[nvoice]=poslo;
  1006. };
  1007. */
  1008. /*
  1009. * Computes the Oscillator (Morphing)
  1010. */
  1011. inline void ADnote::ComputeVoiceOscillatorMorph(int nvoice)
  1012. {
  1013. int i;
  1014. float amp;
  1015. ComputeVoiceOscillator_LinearInterpolation(nvoice);
  1016. if(FMnewamplitude[nvoice] > 1.0f)
  1017. FMnewamplitude[nvoice] = 1.0f;
  1018. if(FMoldamplitude[nvoice] > 1.0f)
  1019. FMoldamplitude[nvoice] = 1.0f;
  1020. if(NoteVoicePar[nvoice].FMVoice >= 0) {
  1021. //if I use VoiceOut[] as modullator
  1022. int FMVoice = NoteVoicePar[nvoice].FMVoice;
  1023. for(int k = 0; k < unison_size[nvoice]; ++k) {
  1024. float *tw = tmpwave_unison[k];
  1025. for(i = 0; i < synth->buffersize; ++i) {
  1026. amp = INTERPOLATE_AMPLITUDE(FMoldamplitude[nvoice],
  1027. FMnewamplitude[nvoice],
  1028. i,
  1029. synth->buffersize);
  1030. tw[i] = tw[i]
  1031. * (1.0f
  1032. - amp) + amp * NoteVoicePar[FMVoice].VoiceOut[i];
  1033. }
  1034. }
  1035. }
  1036. else
  1037. for(int k = 0; k < unison_size[nvoice]; ++k) {
  1038. int poshiFM = oscposhiFM[nvoice][k];
  1039. float posloFM = oscposloFM[nvoice][k];
  1040. int freqhiFM = oscfreqhiFM[nvoice][k];
  1041. float freqloFM = oscfreqloFM[nvoice][k];
  1042. float *tw = tmpwave_unison[k];
  1043. for(i = 0; i < synth->buffersize; ++i) {
  1044. amp = INTERPOLATE_AMPLITUDE(FMoldamplitude[nvoice],
  1045. FMnewamplitude[nvoice],
  1046. i,
  1047. synth->buffersize);
  1048. tw[i] = tw[i] * (1.0f - amp) + amp
  1049. * (NoteVoicePar[nvoice].FMSmp[poshiFM] * (1 - posloFM)
  1050. + NoteVoicePar[nvoice].FMSmp[poshiFM + 1] * posloFM);
  1051. posloFM += freqloFM;
  1052. if(posloFM >= 1.0f) {
  1053. posloFM -= 1.0f;
  1054. poshiFM++;
  1055. }
  1056. poshiFM += freqhiFM;
  1057. poshiFM &= synth->oscilsize - 1;
  1058. }
  1059. oscposhiFM[nvoice][k] = poshiFM;
  1060. oscposloFM[nvoice][k] = posloFM;
  1061. }
  1062. }
  1063. /*
  1064. * Computes the Oscillator (Ring Modulation)
  1065. */
  1066. inline void ADnote::ComputeVoiceOscillatorRingModulation(int nvoice)
  1067. {
  1068. int i;
  1069. float amp;
  1070. ComputeVoiceOscillator_LinearInterpolation(nvoice);
  1071. if(FMnewamplitude[nvoice] > 1.0f)
  1072. FMnewamplitude[nvoice] = 1.0f;
  1073. if(FMoldamplitude[nvoice] > 1.0f)
  1074. FMoldamplitude[nvoice] = 1.0f;
  1075. if(NoteVoicePar[nvoice].FMVoice >= 0)
  1076. // if I use VoiceOut[] as modullator
  1077. for(int k = 0; k < unison_size[nvoice]; ++k) {
  1078. float *tw = tmpwave_unison[k];
  1079. for(i = 0; i < synth->buffersize; ++i) {
  1080. amp = INTERPOLATE_AMPLITUDE(FMoldamplitude[nvoice],
  1081. FMnewamplitude[nvoice],
  1082. i,
  1083. synth->buffersize);
  1084. int FMVoice = NoteVoicePar[nvoice].FMVoice;
  1085. tw[i] *= (1.0f - amp) + amp * NoteVoicePar[FMVoice].VoiceOut[i];
  1086. }
  1087. }
  1088. else
  1089. for(int k = 0; k < unison_size[nvoice]; ++k) {
  1090. int poshiFM = oscposhiFM[nvoice][k];
  1091. float posloFM = oscposloFM[nvoice][k];
  1092. int freqhiFM = oscfreqhiFM[nvoice][k];
  1093. float freqloFM = oscfreqloFM[nvoice][k];
  1094. float *tw = tmpwave_unison[k];
  1095. for(i = 0; i < synth->buffersize; ++i) {
  1096. amp = INTERPOLATE_AMPLITUDE(FMoldamplitude[nvoice],
  1097. FMnewamplitude[nvoice],
  1098. i,
  1099. synth->buffersize);
  1100. tw[i] *= (NoteVoicePar[nvoice].FMSmp[poshiFM] * (1.0f - posloFM)
  1101. + NoteVoicePar[nvoice].FMSmp[poshiFM
  1102. + 1] * posloFM) * amp
  1103. + (1.0f - amp);
  1104. posloFM += freqloFM;
  1105. if(posloFM >= 1.0f) {
  1106. posloFM -= 1.0f;
  1107. poshiFM++;
  1108. }
  1109. poshiFM += freqhiFM;
  1110. poshiFM &= synth->oscilsize - 1;
  1111. }
  1112. oscposhiFM[nvoice][k] = poshiFM;
  1113. oscposloFM[nvoice][k] = posloFM;
  1114. }
  1115. }
  1116. /*
  1117. * Computes the Oscillator (Phase Modulation or Frequency Modulation)
  1118. */
  1119. inline void ADnote::ComputeVoiceOscillatorFrequencyModulation(int nvoice,
  1120. int FMmode)
  1121. {
  1122. int carposhi = 0;
  1123. int i, FMmodfreqhi = 0;
  1124. float FMmodfreqlo = 0, carposlo = 0;
  1125. if(NoteVoicePar[nvoice].FMVoice >= 0)
  1126. //if I use VoiceOut[] as modulator
  1127. for(int k = 0; k < unison_size[nvoice]; ++k) {
  1128. float *tw = tmpwave_unison[k];
  1129. memcpy(tw, NoteVoicePar[NoteVoicePar[nvoice].FMVoice].VoiceOut,
  1130. synth->bufferbytes);
  1131. }
  1132. else
  1133. //Compute the modulator and store it in tmpwave_unison[][]
  1134. for(int k = 0; k < unison_size[nvoice]; ++k) {
  1135. int poshiFM = oscposhiFM[nvoice][k];
  1136. float posloFM = oscposloFM[nvoice][k];
  1137. int freqhiFM = oscfreqhiFM[nvoice][k];
  1138. float freqloFM = oscfreqloFM[nvoice][k];
  1139. float *tw = tmpwave_unison[k];
  1140. for(i = 0; i < synth->buffersize; ++i) {
  1141. tw[i] =
  1142. (NoteVoicePar[nvoice].FMSmp[poshiFM] * (1.0f - posloFM)
  1143. + NoteVoicePar[nvoice].FMSmp[poshiFM + 1] * posloFM);
  1144. posloFM += freqloFM;
  1145. if(posloFM >= 1.0f) {
  1146. posloFM = fmod(posloFM, 1.0f);
  1147. poshiFM++;
  1148. }
  1149. poshiFM += freqhiFM;
  1150. poshiFM &= synth->oscilsize - 1;
  1151. }
  1152. oscposhiFM[nvoice][k] = poshiFM;
  1153. oscposloFM[nvoice][k] = posloFM;
  1154. }
  1155. // Amplitude interpolation
  1156. if(ABOVE_AMPLITUDE_THRESHOLD(FMoldamplitude[nvoice],
  1157. FMnewamplitude[nvoice]))
  1158. for(int k = 0; k < unison_size[nvoice]; ++k) {
  1159. float *tw = tmpwave_unison[k];
  1160. for(i = 0; i < synth->buffersize; ++i)
  1161. tw[i] *= INTERPOLATE_AMPLITUDE(FMoldamplitude[nvoice],
  1162. FMnewamplitude[nvoice],
  1163. i,
  1164. synth->buffersize);
  1165. }
  1166. else
  1167. for(int k = 0; k < unison_size[nvoice]; ++k) {
  1168. float *tw = tmpwave_unison[k];
  1169. for(i = 0; i < synth->buffersize; ++i)
  1170. tw[i] *= FMnewamplitude[nvoice];
  1171. }
  1172. //normalize: makes all sample-rates, oscil_sizes to produce same sound
  1173. if(FMmode != 0) { //Frequency modulation
  1174. float normalize = synth->oscilsize_f / 262144.0f * 44100.0f
  1175. / synth->samplerate_f;
  1176. for(int k = 0; k < unison_size[nvoice]; ++k) {
  1177. float *tw = tmpwave_unison[k];
  1178. float fmold = FMoldsmp[nvoice][k];
  1179. for(i = 0; i < synth->buffersize; ++i) {
  1180. fmold = fmod(fmold + tw[i] * normalize, synth->oscilsize);
  1181. tw[i] = fmold;
  1182. }
  1183. FMoldsmp[nvoice][k] = fmold;
  1184. }
  1185. }
  1186. else { //Phase modulation
  1187. float normalize = synth->oscilsize_f / 262144.0f;
  1188. for(int k = 0; k < unison_size[nvoice]; ++k) {
  1189. float *tw = tmpwave_unison[k];
  1190. for(i = 0; i < synth->buffersize; ++i)
  1191. tw[i] *= normalize;
  1192. }
  1193. }
  1194. //do the modulation
  1195. for(int k = 0; k < unison_size[nvoice]; ++k) {
  1196. float *tw = tmpwave_unison[k];
  1197. int poshi = oscposhi[nvoice][k];
  1198. float poslo = oscposlo[nvoice][k];
  1199. int freqhi = oscfreqhi[nvoice][k];
  1200. float freqlo = oscfreqlo[nvoice][k];
  1201. for(i = 0; i < synth->buffersize; ++i) {
  1202. F2I(tw[i], FMmodfreqhi);
  1203. FMmodfreqlo = fmod(tw[i] + 0.0000000001f, 1.0f);
  1204. if(FMmodfreqhi < 0)
  1205. FMmodfreqlo++;
  1206. //carrier
  1207. carposhi = poshi + FMmodfreqhi;
  1208. carposlo = poslo + FMmodfreqlo;
  1209. if(carposlo >= 1.0f) {
  1210. carposhi++;
  1211. carposlo = fmod(carposlo, 1.0f);
  1212. }
  1213. carposhi &= (synth->oscilsize - 1);
  1214. tw[i] = NoteVoicePar[nvoice].OscilSmp[carposhi]
  1215. * (1.0f - carposlo)
  1216. + NoteVoicePar[nvoice].OscilSmp[carposhi
  1217. + 1] * carposlo;
  1218. poslo += freqlo;
  1219. if(poslo >= 1.0f) {
  1220. poslo = fmod(poslo, 1.0f);
  1221. poshi++;
  1222. }
  1223. poshi += freqhi;
  1224. poshi &= synth->oscilsize - 1;
  1225. }
  1226. oscposhi[nvoice][k] = poshi;
  1227. oscposlo[nvoice][k] = poslo;
  1228. }
  1229. }
  1230. /*Calculeaza Oscilatorul cu PITCH MODULATION*/
  1231. inline void ADnote::ComputeVoiceOscillatorPitchModulation(int /*nvoice*/)
  1232. {
  1233. //TODO
  1234. }
  1235. /*
  1236. * Computes the Noise
  1237. */
  1238. inline void ADnote::ComputeVoiceNoise(int nvoice)
  1239. {
  1240. for(int k = 0; k < unison_size[nvoice]; ++k) {
  1241. float *tw = tmpwave_unison[k];
  1242. for(int i = 0; i < synth->buffersize; ++i)
  1243. tw[i] = RND * 2.0f - 1.0f;
  1244. }
  1245. }
  1246. /*
  1247. * Compute the ADnote samples
  1248. * Returns 0 if the note is finished
  1249. */
  1250. int ADnote::noteout(float *outl, float *outr)
  1251. {
  1252. memcpy(outl, denormalkillbuf, synth->bufferbytes);
  1253. memcpy(outr, denormalkillbuf, synth->bufferbytes);
  1254. if(NoteEnabled == OFF)
  1255. return 0;
  1256. memset(bypassl, 0, synth->bufferbytes);
  1257. memset(bypassr, 0, synth->bufferbytes);
  1258. computecurrentparameters();
  1259. for(unsigned nvoice = 0; nvoice < NUM_VOICES; ++nvoice) {
  1260. if((NoteVoicePar[nvoice].Enabled != ON)
  1261. || (NoteVoicePar[nvoice].DelayTicks > 0))
  1262. continue;
  1263. if(NoteVoicePar[nvoice].noisetype == 0) //voice mode=sound
  1264. switch(NoteVoicePar[nvoice].FMEnabled) {
  1265. case MORPH:
  1266. ComputeVoiceOscillatorMorph(nvoice);
  1267. break;
  1268. case RING_MOD:
  1269. ComputeVoiceOscillatorRingModulation(nvoice);
  1270. break;
  1271. case PHASE_MOD:
  1272. ComputeVoiceOscillatorFrequencyModulation(nvoice, 0);
  1273. break;
  1274. case FREQ_MOD:
  1275. ComputeVoiceOscillatorFrequencyModulation(nvoice, 1);
  1276. break;
  1277. //case PITCH_MOD:ComputeVoiceOscillatorPitchModulation(nvoice);break;
  1278. default:
  1279. ComputeVoiceOscillator_LinearInterpolation(nvoice);
  1280. //if (config.cfg.Interpolation) ComputeVoiceOscillator_CubicInterpolation(nvoice);
  1281. }
  1282. else
  1283. ComputeVoiceNoise(nvoice);
  1284. // Voice Processing
  1285. //mix subvoices into voice
  1286. memset(tmpwavel, 0, synth->bufferbytes);
  1287. if(stereo)
  1288. memset(tmpwaver, 0, synth->bufferbytes);
  1289. for(int k = 0; k < unison_size[nvoice]; ++k) {
  1290. float *tw = tmpwave_unison[k];
  1291. if(stereo) {
  1292. float stereo_pos = 0;
  1293. if(unison_size[nvoice] > 1)
  1294. stereo_pos = k
  1295. / (float)(unison_size[nvoice]
  1296. - 1) * 2.0f - 1.0f;
  1297. float stereo_spread = unison_stereo_spread[nvoice] * 2.0f; //between 0 and 2.0f
  1298. if(stereo_spread > 1.0f) {
  1299. float stereo_pos_1 = (stereo_pos >= 0.0f) ? 1.0f : -1.0f;
  1300. stereo_pos =
  1301. (2.0f
  1302. - stereo_spread) * stereo_pos
  1303. + (stereo_spread - 1.0f) * stereo_pos_1;
  1304. }
  1305. else
  1306. stereo_pos *= stereo_spread;
  1307. if(unison_size[nvoice] == 1)
  1308. stereo_pos = 0.0f;
  1309. float panning = (stereo_pos + 1.0f) * 0.5f;
  1310. float lvol = (1.0f - panning) * 2.0f;
  1311. if(lvol > 1.0f)
  1312. lvol = 1.0f;
  1313. float rvol = panning * 2.0f;
  1314. if(rvol > 1.0f)
  1315. rvol = 1.0f;
  1316. if(unison_invert_phase[nvoice][k]) {
  1317. lvol = -lvol;
  1318. rvol = -rvol;
  1319. }
  1320. for(int i = 0; i < synth->buffersize; ++i)
  1321. tmpwavel[i] += tw[i] * lvol;
  1322. for(int i = 0; i < synth->buffersize; ++i)
  1323. tmpwaver[i] += tw[i] * rvol;
  1324. }
  1325. else
  1326. for(int i = 0; i < synth->buffersize; ++i)
  1327. tmpwavel[i] += tw[i];
  1328. }
  1329. float unison_amplitude = 1.0f / sqrt(unison_size[nvoice]); //reduce the amplitude for large unison sizes
  1330. // Amplitude
  1331. float oldam = oldamplitude[nvoice] * unison_amplitude;
  1332. float newam = newamplitude[nvoice] * unison_amplitude;
  1333. if(ABOVE_AMPLITUDE_THRESHOLD(oldam, newam)) {
  1334. int rest = synth->buffersize;
  1335. //test if the amplitude if raising and the difference is high
  1336. if((newam > oldam) && ((newam - oldam) > 0.25f)) {
  1337. rest = 10;
  1338. if(rest > synth->buffersize)
  1339. rest = synth->buffersize;
  1340. for(int i = 0; i < synth->buffersize - rest; ++i)
  1341. tmpwavel[i] *= oldam;
  1342. if(stereo)
  1343. for(int i = 0; i < synth->buffersize - rest; ++i)
  1344. tmpwaver[i] *= oldam;
  1345. }
  1346. // Amplitude interpolation
  1347. for(int i = 0; i < rest; ++i) {
  1348. float amp = INTERPOLATE_AMPLITUDE(oldam, newam, i, rest);
  1349. tmpwavel[i + (synth->buffersize - rest)] *= amp;
  1350. if(stereo)
  1351. tmpwaver[i + (synth->buffersize - rest)] *= amp;
  1352. }
  1353. }
  1354. else {
  1355. for(int i = 0; i < synth->buffersize; ++i)
  1356. tmpwavel[i] *= newam;
  1357. if(stereo)
  1358. for(int i = 0; i < synth->buffersize; ++i)
  1359. tmpwaver[i] *= newam;
  1360. }
  1361. // Fade in
  1362. if(firsttick[nvoice] != 0) {
  1363. fadein(&tmpwavel[0]);
  1364. if(stereo)
  1365. fadein(&tmpwaver[0]);
  1366. firsttick[nvoice] = 0;
  1367. }
  1368. // Filter
  1369. if(NoteVoicePar[nvoice].VoiceFilterL != NULL)
  1370. NoteVoicePar[nvoice].VoiceFilterL->filterout(&tmpwavel[0]);
  1371. if((stereo) && (NoteVoicePar[nvoice].VoiceFilterR != NULL))
  1372. NoteVoicePar[nvoice].VoiceFilterR->filterout(&tmpwaver[0]);
  1373. //check if the amplitude envelope is finished, if yes, the voice will be fadeout
  1374. if(NoteVoicePar[nvoice].AmpEnvelope != NULL)
  1375. if(NoteVoicePar[nvoice].AmpEnvelope->finished() != 0) {
  1376. for(int i = 0; i < synth->buffersize; ++i)
  1377. tmpwavel[i] *= 1.0f - (float)i / synth->buffersize_f;
  1378. if(stereo)
  1379. for(int i = 0; i < synth->buffersize; ++i)
  1380. tmpwaver[i] *= 1.0f - (float)i / synth->buffersize_f;
  1381. }
  1382. //the voice is killed later
  1383. // Put the ADnote samples in VoiceOut (without appling Global volume, because I wish to use this voice as a modullator)
  1384. if(NoteVoicePar[nvoice].VoiceOut != NULL) {
  1385. if(stereo)
  1386. for(int i = 0; i < synth->buffersize; ++i)
  1387. NoteVoicePar[nvoice].VoiceOut[i] = tmpwavel[i]
  1388. + tmpwaver[i];
  1389. else //mono
  1390. for(int i = 0; i < synth->buffersize; ++i)
  1391. NoteVoicePar[nvoice].VoiceOut[i] = tmpwavel[i];
  1392. }
  1393. // Add the voice that do not bypass the filter to out
  1394. if(NoteVoicePar[nvoice].filterbypass == 0) { //no bypass
  1395. if(stereo)
  1396. for(int i = 0; i < synth->buffersize; ++i) { //stereo
  1397. outl[i] += tmpwavel[i] * NoteVoicePar[nvoice].Volume
  1398. * NoteVoicePar[nvoice].Panning * 2.0f;
  1399. outr[i] += tmpwaver[i] * NoteVoicePar[nvoice].Volume
  1400. * (1.0f - NoteVoicePar[nvoice].Panning) * 2.0f;
  1401. }
  1402. else
  1403. for(int i = 0; i < synth->buffersize; ++i) //mono
  1404. outl[i] += tmpwavel[i] * NoteVoicePar[nvoice].Volume;
  1405. }
  1406. else { //bypass the filter
  1407. if(stereo)
  1408. for(int i = 0; i < synth->buffersize; ++i) { //stereo
  1409. bypassl[i] += tmpwavel[i] * NoteVoicePar[nvoice].Volume
  1410. * NoteVoicePar[nvoice].Panning * 2.0f;
  1411. bypassr[i] += tmpwaver[i] * NoteVoicePar[nvoice].Volume
  1412. * (1.0f
  1413. - NoteVoicePar[nvoice].Panning) * 2.0f;
  1414. }
  1415. else
  1416. for(int i = 0; i < synth->buffersize; ++i) //mono
  1417. bypassl[i] += tmpwavel[i] * NoteVoicePar[nvoice].Volume;
  1418. }
  1419. // chech if there is necesary to proces the voice longer (if the Amplitude envelope isn't finished)
  1420. if(NoteVoicePar[nvoice].AmpEnvelope != NULL)
  1421. if(NoteVoicePar[nvoice].AmpEnvelope->finished() != 0)
  1422. KillVoice(nvoice);
  1423. }
  1424. //Processing Global parameters
  1425. NoteGlobalPar.GlobalFilterL->filterout(&outl[0]);
  1426. if(stereo == 0) { //set the right channel=left channel
  1427. memcpy(outr, outl, synth->bufferbytes);
  1428. memcpy(bypassr, bypassl, synth->bufferbytes);
  1429. }
  1430. else
  1431. NoteGlobalPar.GlobalFilterR->filterout(&outr[0]);
  1432. for(int i = 0; i < synth->buffersize; ++i) {
  1433. outl[i] += bypassl[i];
  1434. outr[i] += bypassr[i];
  1435. }
  1436. if(ABOVE_AMPLITUDE_THRESHOLD(globaloldamplitude, globalnewamplitude))
  1437. // Amplitude Interpolation
  1438. for(int i = 0; i < synth->buffersize; ++i) {
  1439. float tmpvol = INTERPOLATE_AMPLITUDE(globaloldamplitude,
  1440. globalnewamplitude,
  1441. i,
  1442. synth->buffersize);
  1443. outl[i] *= tmpvol * NoteGlobalPar.Panning;
  1444. outr[i] *= tmpvol * (1.0f - NoteGlobalPar.Panning);
  1445. }
  1446. else
  1447. for(int i = 0; i < synth->buffersize; ++i) {
  1448. outl[i] *= globalnewamplitude * NoteGlobalPar.Panning;
  1449. outr[i] *= globalnewamplitude * (1.0f - NoteGlobalPar.Panning);
  1450. }
  1451. //Apply the punch
  1452. if(NoteGlobalPar.Punch.Enabled != 0)
  1453. for(int i = 0; i < synth->buffersize; ++i) {
  1454. float punchamp = NoteGlobalPar.Punch.initialvalue
  1455. * NoteGlobalPar.Punch.t + 1.0f;
  1456. outl[i] *= punchamp;
  1457. outr[i] *= punchamp;
  1458. NoteGlobalPar.Punch.t -= NoteGlobalPar.Punch.dt;
  1459. if(NoteGlobalPar.Punch.t < 0.0f) {
  1460. NoteGlobalPar.Punch.Enabled = 0;
  1461. break;
  1462. }
  1463. }
  1464. // Apply legato-specific sound signal modifications
  1465. legato.apply(*this, outl, outr);
  1466. // Check if the global amplitude is finished.
  1467. // If it does, disable the note
  1468. if(NoteGlobalPar.AmpEnvelope->finished()) {
  1469. for(int i = 0; i < synth->buffersize; ++i) { //fade-out
  1470. float tmp = 1.0f - (float)i / synth->buffersize_f;
  1471. outl[i] *= tmp;
  1472. outr[i] *= tmp;
  1473. }
  1474. KillNote();
  1475. }
  1476. return 1;
  1477. }
  1478. /*
  1479. * Relase the key (NoteOff)
  1480. */
  1481. void ADnote::relasekey()
  1482. {
  1483. for(int nvoice = 0; nvoice < NUM_VOICES; ++nvoice)
  1484. NoteVoicePar[nvoice].releasekey();
  1485. NoteGlobalPar.FreqEnvelope->relasekey();
  1486. NoteGlobalPar.FilterEnvelope->relasekey();
  1487. NoteGlobalPar.AmpEnvelope->relasekey();
  1488. }
  1489. /*
  1490. * Check if the note is finished
  1491. */
  1492. int ADnote::finished() const
  1493. {
  1494. if(NoteEnabled == ON)
  1495. return 0;
  1496. else
  1497. return 1;
  1498. }
  1499. void ADnote::Voice::releasekey()
  1500. {
  1501. if(!Enabled)
  1502. return;
  1503. if(AmpEnvelope)
  1504. AmpEnvelope->relasekey();
  1505. if(FreqEnvelope)
  1506. FreqEnvelope->relasekey();
  1507. if(FilterEnvelope)
  1508. FilterEnvelope->relasekey();
  1509. if(FMFreqEnvelope)
  1510. FMFreqEnvelope->relasekey();
  1511. if(FMAmpEnvelope)
  1512. FMAmpEnvelope->relasekey();
  1513. }
  1514. template<class T>
  1515. static inline void nullify(T &t) {delete t; t = NULL; }
  1516. template<class T>
  1517. static inline void arrayNullify(T &t) {delete [] t; t = NULL; }
  1518. void ADnote::Voice::kill()
  1519. {
  1520. arrayNullify(OscilSmp);
  1521. nullify(FreqEnvelope);
  1522. nullify(FreqLfo);
  1523. nullify(AmpEnvelope);
  1524. nullify(AmpLfo);
  1525. nullify(VoiceFilterL);
  1526. nullify(VoiceFilterR);
  1527. nullify(FilterEnvelope);
  1528. nullify(FilterLfo);
  1529. nullify(FMFreqEnvelope);
  1530. nullify(FMAmpEnvelope);
  1531. if((FMEnabled != NONE) && (FMVoice < 0)) {
  1532. delete[] FMSmp;
  1533. FMSmp = NULL;
  1534. }
  1535. if(VoiceOut)
  1536. memset(VoiceOut, 0, synth->bufferbytes);
  1537. //do not delete, yet: perhaps is used by another voice
  1538. Enabled = OFF;
  1539. }
  1540. void ADnote::Global::kill()
  1541. {
  1542. nullify(FreqEnvelope);
  1543. nullify(FreqLfo);
  1544. nullify(AmpEnvelope);
  1545. nullify(AmpLfo);
  1546. nullify(GlobalFilterL);
  1547. nullify(GlobalFilterR);
  1548. nullify(FilterEnvelope);
  1549. nullify(FilterLfo);
  1550. }
  1551. void ADnote::Global::initparameters(const ADnoteGlobalParam &param,
  1552. float basefreq, float velocity,
  1553. bool stereo)
  1554. {
  1555. FreqEnvelope = new Envelope(param.FreqEnvelope, basefreq);
  1556. FreqLfo = new LFO(param.FreqLfo, basefreq);
  1557. AmpEnvelope = new Envelope(param.AmpEnvelope, basefreq);
  1558. AmpLfo = new LFO(param.AmpLfo, basefreq);
  1559. Volume = 4.0f * powf(0.1f, 3.0f * (1.0f - param.PVolume / 96.0f)) //-60 dB .. 0 dB
  1560. * VelF(velocity, param.PAmpVelocityScaleFunction); //sensing
  1561. GlobalFilterL = Filter::generate(param.GlobalFilter);
  1562. if(stereo)
  1563. GlobalFilterR = Filter::generate(param.GlobalFilter);
  1564. else
  1565. GlobalFilterR = NULL;
  1566. FilterEnvelope = new Envelope(param.FilterEnvelope, basefreq);
  1567. FilterLfo = new LFO(param.FilterLfo, basefreq);
  1568. FilterQ = param.GlobalFilter->getq();
  1569. FilterFreqTracking = param.GlobalFilter->getfreqtracking(basefreq);
  1570. }