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.

145 lines
3.8KB

  1. // Copyright 2014 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // AD envelope for the string synth.
  28. #ifndef ELEMENTS_DSP_STRING_SYNTH_ENVELOPE_H_
  29. #define ELEMENTS_DSP_STRING_SYNTH_ENVELOPE_H_
  30. #include "stmlib/stmlib.h"
  31. namespace rings {
  32. enum EnvelopeShape {
  33. ENVELOPE_SHAPE_LINEAR,
  34. ENVELOPE_SHAPE_QUARTIC
  35. };
  36. enum EnvelopeFlags {
  37. ENVELOPE_FLAG_RISING_EDGE = 1,
  38. ENVELOPE_FLAG_FALLING_EDGE = 2,
  39. ENVELOPE_FLAG_GATE = 4
  40. };
  41. class StringSynthEnvelope {
  42. public:
  43. StringSynthEnvelope() { }
  44. ~StringSynthEnvelope() { }
  45. void Init() {
  46. set_ad(0.1f, 0.001f);
  47. segment_ = num_segments_;
  48. phase_ = 0.0f;
  49. start_value_ = 0.0f;
  50. value_ = 0.0f;
  51. }
  52. inline float Process(uint8_t flags) {
  53. if (flags & ENVELOPE_FLAG_RISING_EDGE) {
  54. start_value_ = segment_ == num_segments_ ? level_[0] : value_;
  55. segment_ = 0;
  56. phase_ = 0.0f;
  57. } else if (flags & ENVELOPE_FLAG_FALLING_EDGE && sustain_point_) {
  58. start_value_ = value_;
  59. segment_ = sustain_point_;
  60. phase_ = 0.0f;
  61. } else if (phase_ >= 1.0f) {
  62. start_value_ = level_[segment_ + 1];
  63. ++segment_;
  64. phase_ = 0.0f;
  65. }
  66. bool done = segment_ == num_segments_;
  67. bool sustained = sustain_point_ && segment_ == sustain_point_ &&
  68. flags & ENVELOPE_FLAG_GATE;
  69. float phase_increment = 0.0f;
  70. if (!sustained && !done) {
  71. phase_increment = rate_[segment_];
  72. }
  73. float t = phase_;
  74. if (shape_[segment_] == ENVELOPE_SHAPE_QUARTIC) {
  75. t = 1.0f - t;
  76. t *= t;
  77. t *= t;
  78. t = 1.0f - t;
  79. }
  80. phase_ += phase_increment;
  81. value_ = start_value_ + (level_[segment_ + 1] - start_value_) * t;
  82. return value_;
  83. }
  84. inline void set_ad(float attack, float decay) {
  85. num_segments_ = 2;
  86. sustain_point_ = 0;
  87. level_[0] = 0.0f;
  88. level_[1] = 1.0f;
  89. level_[2] = 0.0f;
  90. rate_[0] = attack;
  91. rate_[1] = decay;
  92. shape_[0] = ENVELOPE_SHAPE_LINEAR;
  93. shape_[1] = ENVELOPE_SHAPE_QUARTIC;
  94. }
  95. inline void set_ar(float attack, float decay) {
  96. num_segments_ = 2;
  97. sustain_point_ = 1;
  98. level_[0] = 0.0f;
  99. level_[1] = 1.0f;
  100. level_[2] = 0.0f;
  101. rate_[0] = attack;
  102. rate_[1] = decay;
  103. shape_[0] = ENVELOPE_SHAPE_LINEAR;
  104. shape_[1] = ENVELOPE_SHAPE_LINEAR;
  105. }
  106. private:
  107. float level_[4];
  108. float rate_[4];
  109. EnvelopeShape shape_[4];
  110. int16_t segment_;
  111. float start_value_;
  112. float value_;
  113. float phase_;
  114. uint16_t num_segments_;
  115. uint16_t sustain_point_;
  116. DISALLOW_COPY_AND_ASSIGN(StringSynthEnvelope);
  117. };
  118. } // namespace rings
  119. #endif // ELEMENTS_DSP_STRING_SYNTH_ENVELOPE_H_