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.

129 lines
4.2KB

  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. // Simple AD envelope - adapted from Peaks' multistage envelope.
  28. #include "streams/envelope.h"
  29. #include "stmlib/utils/dsp.h"
  30. #include "streams/resources.h"
  31. #include "streams/gain.h"
  32. #include <algorithm>
  33. namespace streams {
  34. using namespace std;
  35. using namespace stmlib;
  36. void Envelope::Init() {
  37. fill(&shape_[0], &shape_[kMaxNumSegments], ENV_SHAPE_LINEAR);
  38. set_ad(0, 8192);
  39. segment_ = num_segments_;
  40. phase_ = 0;
  41. phase_increment_ = 0;
  42. start_value_ = 0;
  43. value_ = 0;
  44. rate_modulation_ = 0;
  45. gate_level_ = 0;
  46. gate_ = false;
  47. hard_reset_ = false;
  48. attack_ = 0;
  49. decay_ = 0;
  50. }
  51. void Envelope::Process(
  52. int16_t audio,
  53. int16_t excite,
  54. uint16_t* gain,
  55. uint16_t* frequency) {
  56. // Smooth frequency amount parameters.
  57. frequency_amount_ += (target_frequency_amount_ - frequency_amount_) >> 8;
  58. frequency_offset_ += (target_frequency_offset_ - frequency_offset_) >> 8;
  59. bool trigger = false;
  60. bool release = false;
  61. if (gate_ == false) {
  62. if (excite > kSchmittTriggerThreshold) {
  63. trigger = true;
  64. gate_ = true;
  65. set_hard_reset(false);
  66. }
  67. } else {
  68. if (excite < (kSchmittTriggerThreshold >> 1)) {
  69. gate_ = false;
  70. release = false;
  71. } else {
  72. // Track the level of the signal while the GATE is held.
  73. gate_level_ += (excite - gate_level_) >> 8;
  74. }
  75. }
  76. if (trigger) {
  77. start_value_ = (segment_ == num_segments_ || hard_reset_)
  78. ? level_[0]
  79. : value_;
  80. segment_ = 0;
  81. phase_ = 0;
  82. } else if (release && sustain_point_) {
  83. start_value_ = value_;
  84. segment_ = sustain_point_;
  85. phase_ = 0;
  86. } else if (phase_ < phase_increment_) {
  87. start_value_ = level_[segment_ + 1];
  88. ++segment_;
  89. phase_ = 0;
  90. }
  91. bool done = segment_ == num_segments_;
  92. bool sustained = sustain_point_ && segment_ == sustain_point_ && gate_;
  93. uint32_t increment = sustained || done ? 0 : lut_env_increments[time_[segment_] >> 8];
  94. // Modulates the envelope rate by the actual excitation pulse.
  95. rate_modulation_ += (static_cast<int32_t>(excite > kSchmittTriggerThreshold ? excite : 0) - rate_modulation_) >> 12;
  96. increment += static_cast<int32_t>(increment >> 7) * (rate_modulation_ >> 7);
  97. phase_increment_ = increment;
  98. int32_t a = start_value_;
  99. int32_t b = level_[segment_ + 1];
  100. uint16_t t = Interpolate824(
  101. lookup_table_table[LUT_ENV_LINEAR + shape_[segment_]], phase_);
  102. value_ = a + ((b - a) * (t >> 1) >> 15);
  103. phase_ += phase_increment_;
  104. // Applies a variable amount of distortion, depending on the level.
  105. int32_t compressed = 32767 - ((32767 - value_) * (32767 - value_) >> 15);
  106. compressed = 32767 - ((32767 - compressed) * (32767 - compressed) >> 15);
  107. int32_t scaled = value_ + ((compressed - value_) * gate_level_ >> 15);
  108. scaled = scaled * (28672 + (gate_level_ >> 3)) >> 15;
  109. *gain = scaled * kAboveUnityGain >> 15;
  110. *frequency = frequency_offset_ + (scaled * frequency_amount_ >> 15);
  111. }
  112. } // namespace streams