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.

205 lines
5.4KB

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