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.

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