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.

435 lines
14KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. pADnote.cpp - The "pad" 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 "PADnote.h"
  19. #include "../Misc/Config.h"
  20. #include "../Misc/Allocator.h"
  21. #include "../DSP/Filter.h"
  22. #include "../Params/PADnoteParameters.h"
  23. #include "../Params/Controller.h"
  24. #include "../Params/FilterParams.h"
  25. #include "../Misc/Util.h"
  26. PADnote::PADnote(const PADnoteParameters *parameters,
  27. SynthParams pars, const int& interpolation)
  28. :SynthNote(pars), pars(*parameters), interpolation(interpolation)
  29. {
  30. firsttime = true;
  31. setup(pars.frequency, pars.velocity, pars.portamento, pars.note);
  32. }
  33. void PADnote::setup(float freq,
  34. float velocity_,
  35. int portamento_,
  36. int midinote,
  37. bool legato)
  38. {
  39. portamento = portamento_;
  40. velocity = velocity_;
  41. finished_ = false;
  42. if(!pars.Pfixedfreq)
  43. basefreq = freq;
  44. else {
  45. basefreq = 440.0f;
  46. int fixedfreqET = pars.PfixedfreqET;
  47. if(fixedfreqET != 0) { //if the frequency varies according the keyboard note
  48. float tmp =
  49. (midinote
  50. - 69.0f) / 12.0f
  51. * (powf(2.0f, (fixedfreqET - 1) / 63.0f) - 1.0f);
  52. if(fixedfreqET <= 64)
  53. basefreq *= powf(2.0f, tmp);
  54. else
  55. basefreq *= powf(3.0f, tmp);
  56. }
  57. }
  58. firsttime = true;
  59. realfreq = basefreq;
  60. if(!legato)
  61. NoteGlobalPar.Detune = getdetune(pars.PDetuneType, pars.PCoarseDetune,
  62. pars.PDetune);
  63. //find out the closest note
  64. float logfreq = logf(basefreq * powf(2.0f, NoteGlobalPar.Detune / 1200.0f));
  65. float mindist = fabs(logfreq - logf(pars.sample[0].basefreq + 0.0001f));
  66. nsample = 0;
  67. for(int i = 1; i < PAD_MAX_SAMPLES; ++i) {
  68. if(pars.sample[i].smp == NULL)
  69. break;
  70. float dist = fabs(logfreq - logf(pars.sample[i].basefreq + 0.0001f));
  71. if(dist < mindist) {
  72. nsample = i;
  73. mindist = dist;
  74. }
  75. }
  76. int size = pars.sample[nsample].size;
  77. if(size == 0)
  78. size = 1;
  79. if(!legato) { //not sure
  80. poshi_l = (int)(RND * (size - 1));
  81. if(pars.PStereo)
  82. poshi_r = (poshi_l + size / 2) % size;
  83. else
  84. poshi_r = poshi_l;
  85. poslo = 0.0f;
  86. }
  87. if(pars.PPanning == 0)
  88. NoteGlobalPar.Panning = RND;
  89. else
  90. NoteGlobalPar.Panning = pars.PPanning / 128.0f;
  91. NoteGlobalPar.FilterCenterPitch = pars.GlobalFilter->getfreq() //center freq
  92. + pars.PFilterVelocityScale / 127.0f
  93. * 6.0f //velocity sensing
  94. * (VelF(velocity,
  95. pars.
  96. PFilterVelocityScaleFunction) - 1);
  97. if(!legato) {
  98. NoteGlobalPar.Fadein_adjustment =
  99. pars.Fadein_adjustment / (float)FADEIN_ADJUSTMENT_SCALE;
  100. NoteGlobalPar.Fadein_adjustment *= NoteGlobalPar.Fadein_adjustment;
  101. if(pars.PPunchStrength != 0) {
  102. NoteGlobalPar.Punch.Enabled = 1;
  103. NoteGlobalPar.Punch.t = 1.0f; //start from 1.0f and to 0.0f
  104. NoteGlobalPar.Punch.initialvalue =
  105. ((powf(10, 1.5f * pars.PPunchStrength / 127.0f) - 1.0f)
  106. * VelF(velocity,
  107. pars.PPunchVelocitySensing));
  108. const float time =
  109. powf(10, 3.0f * pars.PPunchTime / 127.0f) / 10000.0f; //0.1f .. 100 ms
  110. const float stretch = powf(440.0f / freq, pars.PPunchStretch / 64.0f);
  111. NoteGlobalPar.Punch.dt = 1.0f
  112. / (time * synth.samplerate_f * stretch);
  113. }
  114. else
  115. NoteGlobalPar.Punch.Enabled = 0;
  116. NoteGlobalPar.FreqEnvelope = memory.alloc<Envelope>(*pars.FreqEnvelope, basefreq, synth.dt());
  117. NoteGlobalPar.FreqLfo = memory.alloc<LFO>(*pars.FreqLfo, basefreq, time);
  118. NoteGlobalPar.AmpEnvelope = memory.alloc<Envelope>(*pars.AmpEnvelope, basefreq, synth.dt());
  119. NoteGlobalPar.AmpLfo = memory.alloc<LFO>(*pars.AmpLfo, basefreq, time);
  120. }
  121. NoteGlobalPar.Volume = 4.0f
  122. * powf(0.1f, 3.0f * (1.0f - pars.PVolume / 96.0f)) //-60 dB .. 0 dB
  123. * VelF(velocity, pars.PAmpVelocityScaleFunction); //velocity sensing
  124. NoteGlobalPar.AmpEnvelope->envout_dB(); //discard the first envelope output
  125. globaloldamplitude = globalnewamplitude = NoteGlobalPar.Volume
  126. * NoteGlobalPar.AmpEnvelope->
  127. envout_dB()
  128. * NoteGlobalPar.AmpLfo->amplfoout();
  129. if(!legato) {
  130. NoteGlobalPar.GlobalFilterL = Filter::generate(memory, pars.GlobalFilter,
  131. synth.samplerate, synth.buffersize);
  132. NoteGlobalPar.GlobalFilterR = Filter::generate(memory, pars.GlobalFilter,
  133. synth.samplerate, synth.buffersize);
  134. NoteGlobalPar.FilterEnvelope = memory.alloc<Envelope>(*pars.FilterEnvelope, basefreq, synth.dt());
  135. NoteGlobalPar.FilterLfo = memory.alloc<LFO>(*pars.FilterLfo, basefreq, time);
  136. }
  137. NoteGlobalPar.FilterQ = pars.GlobalFilter->getq();
  138. NoteGlobalPar.FilterFreqTracking = pars.GlobalFilter->getfreqtracking(
  139. basefreq);
  140. if(!pars.sample[nsample].smp) {
  141. finished_ = true;
  142. return;
  143. }
  144. }
  145. SynthNote *PADnote::cloneLegato(void)
  146. {
  147. SynthParams sp{memory, ctl, synth, time, legato.param.freq, velocity,
  148. (bool)portamento, legato.param.midinote, true};
  149. return memory.alloc<PADnote>(&pars, sp, interpolation);
  150. }
  151. void PADnote::legatonote(LegatoParams pars)
  152. {
  153. // Manage legato stuff
  154. if(legato.update(pars))
  155. return;
  156. setup(pars.frequency, pars.velocity, pars.portamento, pars.midinote, true);
  157. }
  158. PADnote::~PADnote()
  159. {
  160. memory.dealloc(NoteGlobalPar.FreqEnvelope);
  161. memory.dealloc(NoteGlobalPar.FreqLfo);
  162. memory.dealloc(NoteGlobalPar.AmpEnvelope);
  163. memory.dealloc(NoteGlobalPar.AmpLfo);
  164. memory.dealloc(NoteGlobalPar.GlobalFilterL);
  165. memory.dealloc(NoteGlobalPar.GlobalFilterR);
  166. memory.dealloc(NoteGlobalPar.FilterEnvelope);
  167. memory.dealloc(NoteGlobalPar.FilterLfo);
  168. }
  169. inline void PADnote::fadein(float *smps)
  170. {
  171. int zerocrossings = 0;
  172. for(int i = 1; i < synth.buffersize; ++i)
  173. if((smps[i - 1] < 0.0f) && (smps[i] > 0.0f))
  174. zerocrossings++; //this is only the possitive crossings
  175. float tmp = (synth.buffersize_f - 1.0f) / (zerocrossings + 1) / 3.0f;
  176. if(tmp < 8.0f)
  177. tmp = 8.0f;
  178. tmp *= NoteGlobalPar.Fadein_adjustment;
  179. int n;
  180. F2I(tmp, n); //how many samples is the fade-in
  181. if(n > synth.buffersize)
  182. n = synth.buffersize;
  183. for(int i = 0; i < n; ++i) { //fade-in
  184. float tmp = 0.5f - cosf((float)i / (float) n * PI) * 0.5f;
  185. smps[i] *= tmp;
  186. }
  187. }
  188. void PADnote::computecurrentparameters()
  189. {
  190. float globalpitch, globalfilterpitch;
  191. globalpitch = 0.01f * (NoteGlobalPar.FreqEnvelope->envout()
  192. + NoteGlobalPar.FreqLfo->lfoout()
  193. * ctl.modwheel.relmod + NoteGlobalPar.Detune);
  194. globaloldamplitude = globalnewamplitude;
  195. globalnewamplitude = NoteGlobalPar.Volume
  196. * NoteGlobalPar.AmpEnvelope->envout_dB()
  197. * NoteGlobalPar.AmpLfo->amplfoout();
  198. globalfilterpitch = NoteGlobalPar.FilterEnvelope->envout()
  199. + NoteGlobalPar.FilterLfo->lfoout()
  200. + NoteGlobalPar.FilterCenterPitch;
  201. float tmpfilterfreq = globalfilterpitch + ctl.filtercutoff.relfreq
  202. + NoteGlobalPar.FilterFreqTracking;
  203. tmpfilterfreq = Filter::getrealfreq(tmpfilterfreq);
  204. float globalfilterq = NoteGlobalPar.FilterQ * ctl.filterq.relq;
  205. NoteGlobalPar.GlobalFilterL->setfreq_and_q(tmpfilterfreq, globalfilterq);
  206. NoteGlobalPar.GlobalFilterR->setfreq_and_q(tmpfilterfreq, globalfilterq);
  207. //compute the portamento, if it is used by this note
  208. float portamentofreqrap = 1.0f;
  209. if(portamento) { //this voice use portamento
  210. portamentofreqrap = ctl.portamento.freqrap;
  211. if(ctl.portamento.used == 0) //the portamento has finished
  212. portamento = false; //this note is no longer "portamented"
  213. }
  214. realfreq = basefreq * portamentofreqrap
  215. * powf(2.0f, globalpitch / 12.0f) * ctl.pitchwheel.relfreq;
  216. }
  217. int PADnote::Compute_Linear(float *outl,
  218. float *outr,
  219. int freqhi,
  220. float freqlo)
  221. {
  222. float *smps = pars.sample[nsample].smp;
  223. if(smps == NULL) {
  224. finished_ = true;
  225. return 1;
  226. }
  227. int size = pars.sample[nsample].size;
  228. for(int i = 0; i < synth.buffersize; ++i) {
  229. poshi_l += freqhi;
  230. poshi_r += freqhi;
  231. poslo += freqlo;
  232. if(poslo >= 1.0f) {
  233. poshi_l += 1;
  234. poshi_r += 1;
  235. poslo -= 1.0f;
  236. }
  237. if(poshi_l >= size)
  238. poshi_l %= size;
  239. if(poshi_r >= size)
  240. poshi_r %= size;
  241. outl[i] = smps[poshi_l] * (1.0f - poslo) + smps[poshi_l + 1] * poslo;
  242. outr[i] = smps[poshi_r] * (1.0f - poslo) + smps[poshi_r + 1] * poslo;
  243. }
  244. return 1;
  245. }
  246. int PADnote::Compute_Cubic(float *outl,
  247. float *outr,
  248. int freqhi,
  249. float freqlo)
  250. {
  251. float *smps = pars.sample[nsample].smp;
  252. if(smps == NULL) {
  253. finished_ = true;
  254. return 1;
  255. }
  256. int size = pars.sample[nsample].size;
  257. float xm1, x0, x1, x2, a, b, c;
  258. for(int i = 0; i < synth.buffersize; ++i) {
  259. poshi_l += freqhi;
  260. poshi_r += freqhi;
  261. poslo += freqlo;
  262. if(poslo >= 1.0f) {
  263. poshi_l += 1;
  264. poshi_r += 1;
  265. poslo -= 1.0f;
  266. }
  267. if(poshi_l >= size)
  268. poshi_l %= size;
  269. if(poshi_r >= size)
  270. poshi_r %= size;
  271. //left
  272. xm1 = smps[poshi_l];
  273. x0 = smps[poshi_l + 1];
  274. x1 = smps[poshi_l + 2];
  275. x2 = smps[poshi_l + 3];
  276. a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;
  277. b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;
  278. c = (x1 - xm1) * 0.5f;
  279. outl[i] = (((a * poslo) + b) * poslo + c) * poslo + x0;
  280. //right
  281. xm1 = smps[poshi_r];
  282. x0 = smps[poshi_r + 1];
  283. x1 = smps[poshi_r + 2];
  284. x2 = smps[poshi_r + 3];
  285. a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;
  286. b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;
  287. c = (x1 - xm1) * 0.5f;
  288. outr[i] = (((a * poslo) + b) * poslo + c) * poslo + x0;
  289. }
  290. return 1;
  291. }
  292. int PADnote::noteout(float *outl, float *outr)
  293. {
  294. computecurrentparameters();
  295. float *smps = pars.sample[nsample].smp;
  296. if(smps == NULL) {
  297. for(int i = 0; i < synth.buffersize; ++i) {
  298. outl[i] = 0.0f;
  299. outr[i] = 0.0f;
  300. }
  301. return 1;
  302. }
  303. float smpfreq = pars.sample[nsample].basefreq;
  304. float freqrap = realfreq / smpfreq;
  305. int freqhi = (int) (floor(freqrap));
  306. float freqlo = freqrap - floor(freqrap);
  307. if(interpolation)
  308. Compute_Cubic(outl, outr, freqhi, freqlo);
  309. else
  310. Compute_Linear(outl, outr, freqhi, freqlo);
  311. if(firsttime) {
  312. fadein(outl);
  313. fadein(outr);
  314. firsttime = false;
  315. }
  316. NoteGlobalPar.GlobalFilterL->filterout(outl);
  317. NoteGlobalPar.GlobalFilterR->filterout(outr);
  318. //Apply the punch
  319. if(NoteGlobalPar.Punch.Enabled != 0)
  320. for(int i = 0; i < synth.buffersize; ++i) {
  321. float punchamp = NoteGlobalPar.Punch.initialvalue
  322. * NoteGlobalPar.Punch.t + 1.0f;
  323. outl[i] *= punchamp;
  324. outr[i] *= punchamp;
  325. NoteGlobalPar.Punch.t -= NoteGlobalPar.Punch.dt;
  326. if(NoteGlobalPar.Punch.t < 0.0f) {
  327. NoteGlobalPar.Punch.Enabled = 0;
  328. break;
  329. }
  330. }
  331. if(ABOVE_AMPLITUDE_THRESHOLD(globaloldamplitude, globalnewamplitude))
  332. // Amplitude Interpolation
  333. for(int i = 0; i < synth.buffersize; ++i) {
  334. float tmpvol = INTERPOLATE_AMPLITUDE(globaloldamplitude,
  335. globalnewamplitude,
  336. i,
  337. synth.buffersize);
  338. outl[i] *= tmpvol * NoteGlobalPar.Panning;
  339. outr[i] *= tmpvol * (1.0f - NoteGlobalPar.Panning);
  340. }
  341. else
  342. for(int i = 0; i < synth.buffersize; ++i) {
  343. outl[i] *= globalnewamplitude * NoteGlobalPar.Panning;
  344. outr[i] *= globalnewamplitude * (1.0f - NoteGlobalPar.Panning);
  345. }
  346. // Apply legato-specific sound signal modifications
  347. legato.apply(*this, outl, outr);
  348. // Check if the global amplitude is finished.
  349. // If it does, disable the note
  350. if(NoteGlobalPar.AmpEnvelope->finished()) {
  351. for(int i = 0; i < synth.buffersize; ++i) { //fade-out
  352. float tmp = 1.0f - (float)i / synth.buffersize_f;
  353. outl[i] *= tmp;
  354. outr[i] *= tmp;
  355. }
  356. finished_ = 1;
  357. }
  358. return 1;
  359. }
  360. int PADnote::finished() const
  361. {
  362. return finished_;
  363. }
  364. void PADnote::releasekey()
  365. {
  366. NoteGlobalPar.FreqEnvelope->releasekey();
  367. NoteGlobalPar.FilterEnvelope->releasekey();
  368. NoteGlobalPar.AmpEnvelope->releasekey();
  369. }