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.

431 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. if(pars.PPunchStrength != 0) {
  99. NoteGlobalPar.Punch.Enabled = 1;
  100. NoteGlobalPar.Punch.t = 1.0f; //start from 1.0f and to 0.0f
  101. NoteGlobalPar.Punch.initialvalue =
  102. ((powf(10, 1.5f * pars.PPunchStrength / 127.0f) - 1.0f)
  103. * VelF(velocity,
  104. pars.PPunchVelocitySensing));
  105. const float time =
  106. powf(10, 3.0f * pars.PPunchTime / 127.0f) / 10000.0f; //0.1f .. 100 ms
  107. const float stretch = powf(440.0f / freq, pars.PPunchStretch / 64.0f);
  108. NoteGlobalPar.Punch.dt = 1.0f
  109. / (time * synth.samplerate_f * stretch);
  110. }
  111. else
  112. NoteGlobalPar.Punch.Enabled = 0;
  113. NoteGlobalPar.FreqEnvelope = memory.alloc<Envelope>(*pars.FreqEnvelope, basefreq, synth.dt());
  114. NoteGlobalPar.FreqLfo = memory.alloc<LFO>(*pars.FreqLfo, basefreq, time);
  115. NoteGlobalPar.AmpEnvelope = memory.alloc<Envelope>(*pars.AmpEnvelope, basefreq, synth.dt());
  116. NoteGlobalPar.AmpLfo = memory.alloc<LFO>(*pars.AmpLfo, basefreq, time);
  117. }
  118. NoteGlobalPar.Volume = 4.0f
  119. * powf(0.1f, 3.0f * (1.0f - pars.PVolume / 96.0f)) //-60 dB .. 0 dB
  120. * VelF(velocity, pars.PAmpVelocityScaleFunction); //velocity sensing
  121. NoteGlobalPar.AmpEnvelope->envout_dB(); //discard the first envelope output
  122. globaloldamplitude = globalnewamplitude = NoteGlobalPar.Volume
  123. * NoteGlobalPar.AmpEnvelope->
  124. envout_dB()
  125. * NoteGlobalPar.AmpLfo->amplfoout();
  126. if(!legato) {
  127. NoteGlobalPar.GlobalFilterL = Filter::generate(memory, pars.GlobalFilter,
  128. synth.samplerate, synth.buffersize);
  129. NoteGlobalPar.GlobalFilterR = Filter::generate(memory, pars.GlobalFilter,
  130. synth.samplerate, synth.buffersize);
  131. NoteGlobalPar.FilterEnvelope = memory.alloc<Envelope>(*pars.FilterEnvelope, basefreq, synth.dt());
  132. NoteGlobalPar.FilterLfo = memory.alloc<LFO>(*pars.FilterLfo, basefreq, time);
  133. }
  134. NoteGlobalPar.FilterQ = pars.GlobalFilter->getq();
  135. NoteGlobalPar.FilterFreqTracking = pars.GlobalFilter->getfreqtracking(
  136. basefreq);
  137. if(!pars.sample[nsample].smp) {
  138. finished_ = true;
  139. return;
  140. }
  141. }
  142. SynthNote *PADnote::cloneLegato(void)
  143. {
  144. SynthParams sp{memory, ctl, synth, time, legato.param.freq, velocity,
  145. (bool)portamento, legato.param.midinote, true};
  146. return memory.alloc<PADnote>(&pars, sp, interpolation);
  147. }
  148. void PADnote::legatonote(LegatoParams pars)
  149. {
  150. // Manage legato stuff
  151. if(legato.update(pars))
  152. return;
  153. setup(pars.frequency, pars.velocity, pars.portamento, pars.midinote, true);
  154. }
  155. PADnote::~PADnote()
  156. {
  157. memory.dealloc(NoteGlobalPar.FreqEnvelope);
  158. memory.dealloc(NoteGlobalPar.FreqLfo);
  159. memory.dealloc(NoteGlobalPar.AmpEnvelope);
  160. memory.dealloc(NoteGlobalPar.AmpLfo);
  161. memory.dealloc(NoteGlobalPar.GlobalFilterL);
  162. memory.dealloc(NoteGlobalPar.GlobalFilterR);
  163. memory.dealloc(NoteGlobalPar.FilterEnvelope);
  164. memory.dealloc(NoteGlobalPar.FilterLfo);
  165. }
  166. inline void PADnote::fadein(float *smps)
  167. {
  168. int zerocrossings = 0;
  169. for(int i = 1; i < synth.buffersize; ++i)
  170. if((smps[i - 1] < 0.0f) && (smps[i] > 0.0f))
  171. zerocrossings++; //this is only the possitive crossings
  172. float tmp = (synth.buffersize_f - 1.0f) / (zerocrossings + 1) / 3.0f;
  173. if(tmp < 8.0f)
  174. tmp = 8.0f;
  175. int n;
  176. F2I(tmp, n); //how many samples is the fade-in
  177. if(n > synth.buffersize)
  178. n = synth.buffersize;
  179. for(int i = 0; i < n; ++i) { //fade-in
  180. float tmp = 0.5f - cosf((float)i / (float) n * PI) * 0.5f;
  181. smps[i] *= tmp;
  182. }
  183. }
  184. void PADnote::computecurrentparameters()
  185. {
  186. float globalpitch, globalfilterpitch;
  187. globalpitch = 0.01f * (NoteGlobalPar.FreqEnvelope->envout()
  188. + NoteGlobalPar.FreqLfo->lfoout()
  189. * ctl.modwheel.relmod + NoteGlobalPar.Detune);
  190. globaloldamplitude = globalnewamplitude;
  191. globalnewamplitude = NoteGlobalPar.Volume
  192. * NoteGlobalPar.AmpEnvelope->envout_dB()
  193. * NoteGlobalPar.AmpLfo->amplfoout();
  194. globalfilterpitch = NoteGlobalPar.FilterEnvelope->envout()
  195. + NoteGlobalPar.FilterLfo->lfoout()
  196. + NoteGlobalPar.FilterCenterPitch;
  197. float tmpfilterfreq = globalfilterpitch + ctl.filtercutoff.relfreq
  198. + NoteGlobalPar.FilterFreqTracking;
  199. tmpfilterfreq = Filter::getrealfreq(tmpfilterfreq);
  200. float globalfilterq = NoteGlobalPar.FilterQ * ctl.filterq.relq;
  201. NoteGlobalPar.GlobalFilterL->setfreq_and_q(tmpfilterfreq, globalfilterq);
  202. NoteGlobalPar.GlobalFilterR->setfreq_and_q(tmpfilterfreq, globalfilterq);
  203. //compute the portamento, if it is used by this note
  204. float portamentofreqrap = 1.0f;
  205. if(portamento) { //this voice use portamento
  206. portamentofreqrap = ctl.portamento.freqrap;
  207. if(ctl.portamento.used == 0) //the portamento has finished
  208. portamento = false; //this note is no longer "portamented"
  209. }
  210. realfreq = basefreq * portamentofreqrap
  211. * powf(2.0f, globalpitch / 12.0f) * ctl.pitchwheel.relfreq;
  212. }
  213. int PADnote::Compute_Linear(float *outl,
  214. float *outr,
  215. int freqhi,
  216. float freqlo)
  217. {
  218. float *smps = pars.sample[nsample].smp;
  219. if(smps == NULL) {
  220. finished_ = true;
  221. return 1;
  222. }
  223. int size = pars.sample[nsample].size;
  224. for(int i = 0; i < synth.buffersize; ++i) {
  225. poshi_l += freqhi;
  226. poshi_r += freqhi;
  227. poslo += freqlo;
  228. if(poslo >= 1.0f) {
  229. poshi_l += 1;
  230. poshi_r += 1;
  231. poslo -= 1.0f;
  232. }
  233. if(poshi_l >= size)
  234. poshi_l %= size;
  235. if(poshi_r >= size)
  236. poshi_r %= size;
  237. outl[i] = smps[poshi_l] * (1.0f - poslo) + smps[poshi_l + 1] * poslo;
  238. outr[i] = smps[poshi_r] * (1.0f - poslo) + smps[poshi_r + 1] * poslo;
  239. }
  240. return 1;
  241. }
  242. int PADnote::Compute_Cubic(float *outl,
  243. float *outr,
  244. int freqhi,
  245. float freqlo)
  246. {
  247. float *smps = pars.sample[nsample].smp;
  248. if(smps == NULL) {
  249. finished_ = true;
  250. return 1;
  251. }
  252. int size = pars.sample[nsample].size;
  253. float xm1, x0, x1, x2, a, b, c;
  254. for(int i = 0; i < synth.buffersize; ++i) {
  255. poshi_l += freqhi;
  256. poshi_r += freqhi;
  257. poslo += freqlo;
  258. if(poslo >= 1.0f) {
  259. poshi_l += 1;
  260. poshi_r += 1;
  261. poslo -= 1.0f;
  262. }
  263. if(poshi_l >= size)
  264. poshi_l %= size;
  265. if(poshi_r >= size)
  266. poshi_r %= size;
  267. //left
  268. xm1 = smps[poshi_l];
  269. x0 = smps[poshi_l + 1];
  270. x1 = smps[poshi_l + 2];
  271. x2 = smps[poshi_l + 3];
  272. a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;
  273. b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;
  274. c = (x1 - xm1) * 0.5f;
  275. outl[i] = (((a * poslo) + b) * poslo + c) * poslo + x0;
  276. //right
  277. xm1 = smps[poshi_r];
  278. x0 = smps[poshi_r + 1];
  279. x1 = smps[poshi_r + 2];
  280. x2 = smps[poshi_r + 3];
  281. a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;
  282. b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;
  283. c = (x1 - xm1) * 0.5f;
  284. outr[i] = (((a * poslo) + b) * poslo + c) * poslo + x0;
  285. }
  286. return 1;
  287. }
  288. int PADnote::noteout(float *outl, float *outr)
  289. {
  290. computecurrentparameters();
  291. float *smps = pars.sample[nsample].smp;
  292. if(smps == NULL) {
  293. for(int i = 0; i < synth.buffersize; ++i) {
  294. outl[i] = 0.0f;
  295. outr[i] = 0.0f;
  296. }
  297. return 1;
  298. }
  299. float smpfreq = pars.sample[nsample].basefreq;
  300. float freqrap = realfreq / smpfreq;
  301. int freqhi = (int) (floor(freqrap));
  302. float freqlo = freqrap - floor(freqrap);
  303. if(interpolation)
  304. Compute_Cubic(outl, outr, freqhi, freqlo);
  305. else
  306. Compute_Linear(outl, outr, freqhi, freqlo);
  307. if(firsttime) {
  308. fadein(outl);
  309. fadein(outr);
  310. firsttime = false;
  311. }
  312. NoteGlobalPar.GlobalFilterL->filterout(outl);
  313. NoteGlobalPar.GlobalFilterR->filterout(outr);
  314. //Apply the punch
  315. if(NoteGlobalPar.Punch.Enabled != 0)
  316. for(int i = 0; i < synth.buffersize; ++i) {
  317. float punchamp = NoteGlobalPar.Punch.initialvalue
  318. * NoteGlobalPar.Punch.t + 1.0f;
  319. outl[i] *= punchamp;
  320. outr[i] *= punchamp;
  321. NoteGlobalPar.Punch.t -= NoteGlobalPar.Punch.dt;
  322. if(NoteGlobalPar.Punch.t < 0.0f) {
  323. NoteGlobalPar.Punch.Enabled = 0;
  324. break;
  325. }
  326. }
  327. if(ABOVE_AMPLITUDE_THRESHOLD(globaloldamplitude, globalnewamplitude))
  328. // Amplitude Interpolation
  329. for(int i = 0; i < synth.buffersize; ++i) {
  330. float tmpvol = INTERPOLATE_AMPLITUDE(globaloldamplitude,
  331. globalnewamplitude,
  332. i,
  333. synth.buffersize);
  334. outl[i] *= tmpvol * NoteGlobalPar.Panning;
  335. outr[i] *= tmpvol * (1.0f - NoteGlobalPar.Panning);
  336. }
  337. else
  338. for(int i = 0; i < synth.buffersize; ++i) {
  339. outl[i] *= globalnewamplitude * NoteGlobalPar.Panning;
  340. outr[i] *= globalnewamplitude * (1.0f - NoteGlobalPar.Panning);
  341. }
  342. // Apply legato-specific sound signal modifications
  343. legato.apply(*this, outl, outr);
  344. // Check if the global amplitude is finished.
  345. // If it does, disable the note
  346. if(NoteGlobalPar.AmpEnvelope->finished()) {
  347. for(int i = 0; i < synth.buffersize; ++i) { //fade-out
  348. float tmp = 1.0f - (float)i / synth.buffersize_f;
  349. outl[i] *= tmp;
  350. outr[i] *= tmp;
  351. }
  352. finished_ = 1;
  353. }
  354. return 1;
  355. }
  356. int PADnote::finished() const
  357. {
  358. return finished_;
  359. }
  360. void PADnote::releasekey()
  361. {
  362. NoteGlobalPar.FreqEnvelope->releasekey();
  363. NoteGlobalPar.FilterEnvelope->releasekey();
  364. NoteGlobalPar.AmpEnvelope->releasekey();
  365. }