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.

187 lines
5.7KB

  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. // Vactrol.
  28. #include "streams/vactrol.h"
  29. #include "stmlib/utils/dsp.h"
  30. #include "streams/gain.h"
  31. #include "streams/resources.h"
  32. namespace streams {
  33. using namespace stmlib;
  34. void Vactrol::Init() {
  35. state_[0] = 0;
  36. state_[1] = 0;
  37. state_[2] = 0;
  38. state_[3] = 0;
  39. excite_ = 0;
  40. }
  41. void Vactrol::Process(
  42. int16_t audio,
  43. int16_t excite,
  44. uint16_t* gain,
  45. uint16_t* frequency) {
  46. // Smooth frequency amount parameters.
  47. frequency_amount_ += (target_frequency_amount_ - frequency_amount_) >> 8;
  48. frequency_offset_ += (target_frequency_offset_ - frequency_offset_) >> 8;
  49. int32_t input;
  50. int32_t error;
  51. int64_t coefficient = 0;
  52. if (excite < 0) {
  53. excite = 0;
  54. }
  55. // Simple plucked mode.
  56. if (plucked_) {
  57. if (gate_ == false) {
  58. if (excite > kSchmittTriggerThreshold) {
  59. gate_ = true;
  60. state_[0] = 32767 << 16;
  61. state_[1] = 32767 << 16;
  62. }
  63. } else {
  64. if (excite < (kSchmittTriggerThreshold >> 1)) {
  65. gate_ = false;
  66. }
  67. }
  68. // Filter the excitation pulses.
  69. state_[0] -= static_cast<int64_t>(
  70. state_[0]) * fast_decay_coefficient_ >> 31;
  71. state_[1] -= static_cast<int64_t>(
  72. state_[1]) * decay_coefficient_ >> 31;
  73. // VCF envelope.
  74. error = state_[0] - state_[2];
  75. coefficient = error > 0
  76. ? fast_attack_coefficient_ : fast_decay_coefficient_;
  77. state_[2] += static_cast<int64_t>(error) * coefficient >> 31;
  78. // VCA envelope.
  79. error = state_[1] - state_[3];
  80. coefficient = error > 0 ? fast_attack_coefficient_ : decay_coefficient_;
  81. // Increase the duration of the tail
  82. int64_t strength = error > 0 ? error : -error;
  83. coefficient = (coefficient >> 1) + (coefficient * strength >> 31);
  84. state_[3] += static_cast<int64_t>(error) * coefficient >> 31;
  85. uint16_t vcf_amount = state_[2] >> 16;
  86. uint16_t vca_mount = Interpolate1022(wav_gompertz, (state_[3] >> 2) * 3);
  87. *gain = kAboveUnityGain * vca_mount >> 15;
  88. *frequency = frequency_offset_ + \
  89. (frequency_amount_ * vcf_amount >> 15);
  90. return;
  91. }
  92. // Low-pass filter the negative edges to prevent fast pulse to immediately
  93. // decay before the vactrol has started reacting. This allows the EXCITE
  94. // input to be used for both controlling the vactrol or just plucking it
  95. // from a trigger.
  96. error = excite - excite_;
  97. coefficient = error > 0 ? (1 << 30) : (decay_coefficient_ << 1);
  98. excite_ += static_cast<int64_t>(error) * coefficient >> 31;
  99. excite = excite_;
  100. input = frequency_offset_;
  101. input += frequency_amount_ >> 1;
  102. input = (65535 + input) >> 1;
  103. input *= excite;
  104. state_[3] += static_cast<int64_t>(input - state_[3]) * 67976239 >> 31;
  105. error = input - state_[0];
  106. coefficient = 0;
  107. if (error > 0) {
  108. if (state_[1] > 0) {
  109. coefficient = attack_coefficient_;
  110. // Increase attack time when the photocell has been desensitized.
  111. coefficient += coefficient * (255 - (state_[2] >> 23)) >> 6;
  112. } else {
  113. coefficient = fast_attack_coefficient_;
  114. }
  115. } else {
  116. if (state_[1] < 0) {
  117. coefficient = decay_coefficient_;
  118. } else {
  119. coefficient = fast_decay_coefficient_;
  120. }
  121. }
  122. // First order.
  123. state_[0] += static_cast<int64_t>(error) * coefficient >> 31;
  124. // Second order.
  125. state_[1] += static_cast<int64_t>(error - state_[1]) * coefficient >> 31;
  126. // Memory effect.
  127. int32_t sensitivity = state_[0];
  128. if (sensitivity > (1 << 28)) {
  129. sensitivity = 1 << 31;
  130. } else {
  131. sensitivity <<= 3;
  132. }
  133. error = sensitivity - state_[2];
  134. if (error > 0) {
  135. // Get into the "sensitized" state in 1s.
  136. state_[2] += static_cast<int64_t>(error) * 138132 >> 31;
  137. } else {
  138. // Get out of the "sensitized" state in 60s.
  139. state_[2] += static_cast<int64_t>(error) * 1151 >> 31;
  140. }
  141. // Apply non-linearity.
  142. int32_t index = state_[0] >> 1;
  143. // A little hack to add overshoot...
  144. index += (state_[3] >> 15) * (state_[1] >> 15) >> 1;
  145. if (index < 0) {
  146. index = 0;
  147. } else if (index >= (1 << 30)) {
  148. index = (1 << 30) - 1;
  149. }
  150. uint16_t amplitude = index < 536870912
  151. ? Interpolate1022(wav_gompertz, static_cast<uint32_t>(index) << 3)
  152. : 32767;
  153. uint16_t cutoff = index >> 14;
  154. if (cutoff >= 32767) cutoff = 32767;
  155. cutoff = cutoff * cutoff >> 15;
  156. *gain = kAboveUnityGain * amplitude >> 15;
  157. *frequency = frequency_offset_ + \
  158. (frequency_amount_ * cutoff >> 15);
  159. }
  160. } // namespace streams