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.

1936 lines
72KB

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