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.

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