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.

PADnote.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 <math.h>
  18. #include "PADnote.h"
  19. #include "../Misc/Config.h"
  20. #include "../DSP/Filter.h"
  21. PADnote::PADnote(PADnoteParameters *parameters,
  22. Controller *ctl_,
  23. float freq,
  24. float velocity,
  25. int portamento_,
  26. int midinote,
  27. bool besilent)
  28. :SynthNote(freq, velocity, portamento_, midinote, besilent)
  29. {
  30. pars = parameters;
  31. ctl = ctl_;
  32. firsttime = true;
  33. setup(freq, velocity, portamento_, midinote);
  34. }
  35. void PADnote::setup(float freq,
  36. float velocity,
  37. int portamento_,
  38. int midinote,
  39. bool legato)
  40. {
  41. portamento = portamento_;
  42. this->velocity = velocity;
  43. finished_ = false;
  44. if(pars->Pfixedfreq == 0)
  45. basefreq = freq;
  46. else {
  47. basefreq = 440.0f;
  48. int fixedfreqET = pars->PfixedfreqET;
  49. if(fixedfreqET != 0) { //if the frequency varies according the keyboard note
  50. float tmp =
  51. (midinote
  52. - 69.0f) / 12.0f
  53. * (powf(2.0f, (fixedfreqET - 1) / 63.0f) - 1.0f);
  54. if(fixedfreqET <= 64)
  55. basefreq *= powf(2.0f, tmp);
  56. else
  57. basefreq *= powf(3.0f, tmp);
  58. }
  59. }
  60. firsttime = true;
  61. released = false;
  62. realfreq = basefreq;
  63. if(!legato)
  64. NoteGlobalPar.Detune = getdetune(pars->PDetuneType, pars->PCoarseDetune,
  65. pars->PDetune);
  66. //find out the closest note
  67. float logfreq = logf(basefreq * powf(2.0f, NoteGlobalPar.Detune / 1200.0f));
  68. float mindist = fabs(logfreq - logf(pars->sample[0].basefreq + 0.0001f));
  69. nsample = 0;
  70. for(int i = 1; i < PAD_MAX_SAMPLES; ++i) {
  71. if(pars->sample[i].smp == NULL)
  72. break;
  73. float dist = fabs(logfreq - logf(pars->sample[i].basefreq + 0.0001f));
  74. if(dist < mindist) {
  75. nsample = i;
  76. mindist = dist;
  77. }
  78. }
  79. int size = pars->sample[nsample].size;
  80. if(size == 0)
  81. size = 1;
  82. if(!legato) { //not sure
  83. poshi_l = (int)(RND * (size - 1));
  84. if(pars->PStereo != 0)
  85. poshi_r = (poshi_l + size / 2) % size;
  86. else
  87. poshi_r = poshi_l;
  88. poslo = 0.0f;
  89. }
  90. if(pars->PPanning == 0)
  91. NoteGlobalPar.Panning = RND;
  92. else
  93. NoteGlobalPar.Panning = pars->PPanning / 128.0f;
  94. NoteGlobalPar.FilterCenterPitch = pars->GlobalFilter->getfreq() //center freq
  95. + pars->PFilterVelocityScale / 127.0f
  96. * 6.0f //velocity sensing
  97. * (VelF(velocity,
  98. pars->
  99. PFilterVelocityScaleFunction) - 1);
  100. if(!legato) {
  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. float time =
  109. powf(10, 3.0f * pars->PPunchTime / 127.0f) / 10000.0f; //0.1f .. 100 ms
  110. 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 = new Envelope(pars->FreqEnvelope, basefreq);
  117. NoteGlobalPar.FreqLfo = new LFO(pars->FreqLfo, basefreq);
  118. NoteGlobalPar.AmpEnvelope = new Envelope(pars->AmpEnvelope, basefreq);
  119. NoteGlobalPar.AmpLfo = new LFO(pars->AmpLfo, basefreq);
  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(pars->GlobalFilter);
  131. NoteGlobalPar.GlobalFilterR = Filter::generate(pars->GlobalFilter);
  132. NoteGlobalPar.FilterEnvelope = new Envelope(pars->FilterEnvelope,
  133. basefreq);
  134. NoteGlobalPar.FilterLfo = new LFO(pars->FilterLfo, basefreq);
  135. }
  136. NoteGlobalPar.FilterQ = pars->GlobalFilter->getq();
  137. NoteGlobalPar.FilterFreqTracking = pars->GlobalFilter->getfreqtracking(
  138. basefreq);
  139. if(pars->sample[nsample].smp == NULL) {
  140. finished_ = true;
  141. return;
  142. }
  143. }
  144. void PADnote::legatonote(float freq,
  145. float velocity,
  146. int portamento_,
  147. int midinote,
  148. bool externcall)
  149. {
  150. // Manage legato stuff
  151. if(legato.update(freq, velocity, portamento_, midinote, externcall))
  152. return;
  153. setup(freq, velocity, portamento_, midinote, true);
  154. }
  155. PADnote::~PADnote()
  156. {
  157. delete (NoteGlobalPar.FreqEnvelope);
  158. delete (NoteGlobalPar.FreqLfo);
  159. delete (NoteGlobalPar.AmpEnvelope);
  160. delete (NoteGlobalPar.AmpLfo);
  161. delete (NoteGlobalPar.GlobalFilterL);
  162. delete (NoteGlobalPar.GlobalFilterR);
  163. delete (NoteGlobalPar.FilterEnvelope);
  164. delete (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 != 0) { //this voice use portamento
  206. portamentofreqrap = ctl->portamento.freqrap;
  207. if(ctl->portamento.used == 0) //the portamento has finished
  208. portamento = 0; //this note is no longer "portamented"
  209. ;
  210. }
  211. realfreq = basefreq * portamentofreqrap
  212. * powf(2.0f, globalpitch / 12.0f) * ctl->pitchwheel.relfreq;
  213. }
  214. int PADnote::Compute_Linear(float *outl,
  215. float *outr,
  216. int freqhi,
  217. float freqlo)
  218. {
  219. float *smps = pars->sample[nsample].smp;
  220. if(smps == NULL) {
  221. finished_ = true;
  222. return 1;
  223. }
  224. int size = pars->sample[nsample].size;
  225. for(int i = 0; i < synth->buffersize; ++i) {
  226. poshi_l += freqhi;
  227. poshi_r += freqhi;
  228. poslo += freqlo;
  229. if(poslo >= 1.0f) {
  230. poshi_l += 1;
  231. poshi_r += 1;
  232. poslo -= 1.0f;
  233. }
  234. if(poshi_l >= size)
  235. poshi_l %= size;
  236. if(poshi_r >= size)
  237. poshi_r %= size;
  238. outl[i] = smps[poshi_l] * (1.0f - poslo) + smps[poshi_l + 1] * poslo;
  239. outr[i] = smps[poshi_r] * (1.0f - poslo) + smps[poshi_r + 1] * poslo;
  240. }
  241. return 1;
  242. }
  243. int PADnote::Compute_Cubic(float *outl,
  244. float *outr,
  245. int freqhi,
  246. float freqlo)
  247. {
  248. float *smps = pars->sample[nsample].smp;
  249. if(smps == NULL) {
  250. finished_ = true;
  251. return 1;
  252. }
  253. int size = pars->sample[nsample].size;
  254. float xm1, x0, x1, x2, a, b, c;
  255. for(int i = 0; i < synth->buffersize; ++i) {
  256. poshi_l += freqhi;
  257. poshi_r += freqhi;
  258. poslo += freqlo;
  259. if(poslo >= 1.0f) {
  260. poshi_l += 1;
  261. poshi_r += 1;
  262. poslo -= 1.0f;
  263. }
  264. if(poshi_l >= size)
  265. poshi_l %= size;
  266. if(poshi_r >= size)
  267. poshi_r %= size;
  268. //left
  269. xm1 = smps[poshi_l];
  270. x0 = smps[poshi_l + 1];
  271. x1 = smps[poshi_l + 2];
  272. x2 = smps[poshi_l + 3];
  273. a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;
  274. b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;
  275. c = (x1 - xm1) * 0.5f;
  276. outl[i] = (((a * poslo) + b) * poslo + c) * poslo + x0;
  277. //right
  278. xm1 = smps[poshi_r];
  279. x0 = smps[poshi_r + 1];
  280. x1 = smps[poshi_r + 2];
  281. x2 = smps[poshi_r + 3];
  282. a = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;
  283. b = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;
  284. c = (x1 - xm1) * 0.5f;
  285. outr[i] = (((a * poslo) + b) * poslo + c) * poslo + x0;
  286. }
  287. return 1;
  288. }
  289. int PADnote::noteout(float *outl, float *outr)
  290. {
  291. computecurrentparameters();
  292. float *smps = pars->sample[nsample].smp;
  293. if(smps == NULL) {
  294. for(int i = 0; i < synth->buffersize; ++i) {
  295. outl[i] = 0.0f;
  296. outr[i] = 0.0f;
  297. }
  298. return 1;
  299. }
  300. float smpfreq = pars->sample[nsample].basefreq;
  301. float freqrap = realfreq / smpfreq;
  302. int freqhi = (int) (floor(freqrap));
  303. float freqlo = freqrap - floor(freqrap);
  304. if(config.cfg.Interpolation)
  305. Compute_Cubic(outl, outr, freqhi, freqlo);
  306. else
  307. Compute_Linear(outl, outr, freqhi, freqlo);
  308. if(firsttime) {
  309. fadein(outl);
  310. fadein(outr);
  311. firsttime = false;
  312. }
  313. NoteGlobalPar.GlobalFilterL->filterout(outl);
  314. NoteGlobalPar.GlobalFilterR->filterout(outr);
  315. //Apply the punch
  316. if(NoteGlobalPar.Punch.Enabled != 0)
  317. for(int i = 0; i < synth->buffersize; ++i) {
  318. float punchamp = NoteGlobalPar.Punch.initialvalue
  319. * NoteGlobalPar.Punch.t + 1.0f;
  320. outl[i] *= punchamp;
  321. outr[i] *= punchamp;
  322. NoteGlobalPar.Punch.t -= NoteGlobalPar.Punch.dt;
  323. if(NoteGlobalPar.Punch.t < 0.0f) {
  324. NoteGlobalPar.Punch.Enabled = 0;
  325. break;
  326. }
  327. }
  328. if(ABOVE_AMPLITUDE_THRESHOLD(globaloldamplitude, globalnewamplitude))
  329. // Amplitude Interpolation
  330. for(int i = 0; i < synth->buffersize; ++i) {
  331. float tmpvol = INTERPOLATE_AMPLITUDE(globaloldamplitude,
  332. globalnewamplitude,
  333. i,
  334. synth->buffersize);
  335. outl[i] *= tmpvol * NoteGlobalPar.Panning;
  336. outr[i] *= tmpvol * (1.0f - NoteGlobalPar.Panning);
  337. }
  338. else
  339. for(int i = 0; i < synth->buffersize; ++i) {
  340. outl[i] *= globalnewamplitude * NoteGlobalPar.Panning;
  341. outr[i] *= globalnewamplitude * (1.0f - NoteGlobalPar.Panning);
  342. }
  343. // Apply legato-specific sound signal modifications
  344. legato.apply(*this, outl, outr);
  345. // Check if the global amplitude is finished.
  346. // If it does, disable the note
  347. if(NoteGlobalPar.AmpEnvelope->finished() != 0) {
  348. for(int i = 0; i < synth->buffersize; ++i) { //fade-out
  349. float tmp = 1.0f - (float)i / synth->buffersize_f;
  350. outl[i] *= tmp;
  351. outr[i] *= tmp;
  352. }
  353. finished_ = 1;
  354. }
  355. return 1;
  356. }
  357. int PADnote::finished() const
  358. {
  359. return finished_;
  360. }
  361. void PADnote::relasekey()
  362. {
  363. NoteGlobalPar.FreqEnvelope->relasekey();
  364. NoteGlobalPar.FilterEnvelope->relasekey();
  365. NoteGlobalPar.AmpEnvelope->relasekey();
  366. }