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.

Envelope.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Envelope.cpp - Envelope implementation
  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 "Envelope.h"
  19. #include "../Params/EnvelopeParams.h"
  20. Envelope::Envelope(EnvelopeParams *envpars, float basefreq)
  21. {
  22. int i;
  23. envpoints = envpars->Penvpoints;
  24. if(envpoints > MAX_ENVELOPE_POINTS)
  25. envpoints = MAX_ENVELOPE_POINTS;
  26. envsustain = (envpars->Penvsustain == 0) ? -1 : envpars->Penvsustain;
  27. forcedrelase = envpars->Pforcedrelease;
  28. envstretch = powf(440.0f / basefreq, envpars->Penvstretch / 64.0f);
  29. linearenvelope = envpars->Plinearenvelope;
  30. if(envpars->Pfreemode == 0)
  31. envpars->converttofree();
  32. float bufferdt = synth->buffersize_f / synth->samplerate_f;
  33. int mode = envpars->Envmode;
  34. //for amplitude envelopes
  35. if((mode == 1) && (linearenvelope == 0))
  36. mode = 2; //change to log envelope
  37. if((mode == 2) && (linearenvelope != 0))
  38. mode = 1; //change to linear
  39. for(i = 0; i < MAX_ENVELOPE_POINTS; ++i) {
  40. float tmp = envpars->getdt(i) / 1000.0f * envstretch;
  41. if(tmp > bufferdt)
  42. envdt[i] = bufferdt / tmp;
  43. else
  44. envdt[i] = 2.0f; //any value larger than 1
  45. switch(mode) {
  46. case 2:
  47. envval[i] = (1.0f - envpars->Penvval[i] / 127.0f) * -40;
  48. break;
  49. case 3:
  50. envval[i] =
  51. (powf(2, 6.0f
  52. * fabs(envpars->Penvval[i]
  53. - 64.0f) / 64.0f) - 1.0f) * 100.0f;
  54. if(envpars->Penvval[i] < 64)
  55. envval[i] = -envval[i];
  56. break;
  57. case 4:
  58. envval[i] = (envpars->Penvval[i] - 64.0f) / 64.0f * 6.0f; //6 octaves (filtru)
  59. break;
  60. case 5:
  61. envval[i] = (envpars->Penvval[i] - 64.0f) / 64.0f * 10;
  62. break;
  63. default:
  64. envval[i] = envpars->Penvval[i] / 127.0f;
  65. }
  66. }
  67. envdt[0] = 1.0f;
  68. currentpoint = 1; //the envelope starts from 1
  69. keyreleased = false;
  70. t = 0.0f;
  71. envfinish = false;
  72. inct = envdt[1];
  73. envoutval = 0.0f;
  74. }
  75. Envelope::~Envelope()
  76. {}
  77. /*
  78. * Relase the key (note envelope)
  79. */
  80. void Envelope::relasekey()
  81. {
  82. if(keyreleased)
  83. return;
  84. keyreleased = true;
  85. if(forcedrelase != 0)
  86. t = 0.0f;
  87. }
  88. /*
  89. * Envelope Output
  90. */
  91. float Envelope::envout()
  92. {
  93. float out;
  94. if(envfinish) { //if the envelope is finished
  95. envoutval = envval[envpoints - 1];
  96. return envoutval;
  97. }
  98. if((currentpoint == envsustain + 1) && !keyreleased) { //if it is sustaining now
  99. envoutval = envval[envsustain];
  100. return envoutval;
  101. }
  102. if(keyreleased && (forcedrelase != 0)) { //do the forced release
  103. int tmp = (envsustain < 0) ? (envpoints - 1) : (envsustain + 1); //if there is no sustain point, use the last point for release
  104. if(envdt[tmp] < 0.00000001f)
  105. out = envval[tmp];
  106. else
  107. out = envoutval + (envval[tmp] - envoutval) * t;
  108. t += envdt[tmp] * envstretch;
  109. if(t >= 1.0f) {
  110. currentpoint = envsustain + 2;
  111. forcedrelase = 0;
  112. t = 0.0f;
  113. inct = envdt[currentpoint];
  114. if((currentpoint >= envpoints) || (envsustain < 0))
  115. envfinish = true;
  116. }
  117. return out;
  118. }
  119. if(inct >= 1.0f)
  120. out = envval[currentpoint];
  121. else
  122. out = envval[currentpoint - 1]
  123. + (envval[currentpoint] - envval[currentpoint - 1]) * t;
  124. t += inct;
  125. if(t >= 1.0f) {
  126. if(currentpoint >= envpoints - 1)
  127. envfinish = true;
  128. else
  129. currentpoint++;
  130. t = 0.0f;
  131. inct = envdt[currentpoint];
  132. }
  133. envoutval = out;
  134. return out;
  135. }
  136. /*
  137. * Envelope Output (dB)
  138. */
  139. float Envelope::envout_dB()
  140. {
  141. float out;
  142. if(linearenvelope != 0)
  143. return envout();
  144. if((currentpoint == 1) && (!keyreleased || (forcedrelase == 0))) { //first point is always lineary interpolated
  145. float v1 = dB2rap(envval[0]);
  146. float v2 = dB2rap(envval[1]);
  147. out = v1 + (v2 - v1) * t;
  148. t += inct;
  149. if(t >= 1.0f) {
  150. t = 0.0f;
  151. inct = envdt[2];
  152. currentpoint++;
  153. out = v2;
  154. }
  155. if(out > 0.001f)
  156. envoutval = rap2dB(out);
  157. else
  158. envoutval = MIN_ENVELOPE_DB;
  159. }
  160. else
  161. out = dB2rap(envout());
  162. return out;
  163. }
  164. bool Envelope::finished() const
  165. {
  166. return envfinish;
  167. }