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.

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