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.

1909 lines
70KB

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