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.

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