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.

1841 lines
68KB

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