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.

219 lines
7.4KB

  1. // Copyright 2015 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. // Comb filter / KS string.
  28. #include "rings/dsp/string.h"
  29. #include <cmath>
  30. #include "stmlib/dsp/dsp.h"
  31. #include "stmlib/dsp/parameter_interpolator.h"
  32. #include "stmlib/dsp/units.h"
  33. #include "stmlib/utils/random.h"
  34. #include "rings/resources.h"
  35. namespace rings {
  36. using namespace std;
  37. using namespace stmlib;
  38. void String::Init(bool enable_dispersion) {
  39. enable_dispersion_ = enable_dispersion;
  40. string_.Init();
  41. stretch_.Init();
  42. fir_damping_filter_.Init();
  43. iir_damping_filter_.Init();
  44. set_frequency(220.0f / kSampleRate);
  45. set_dispersion(0.25f);
  46. set_brightness(0.5f);
  47. set_damping(0.3f);
  48. set_position(0.8f);
  49. delay_ = 1.0f / frequency_;
  50. clamped_position_ = 0.0f;
  51. previous_dispersion_ = 0.0f;
  52. dispersion_noise_ = 0.0f;
  53. curved_bridge_ = 0.0f;
  54. previous_damping_compensation_ = 0.0f;
  55. out_sample_[0] = out_sample_[1] = 0.0f;
  56. aux_sample_[0] = aux_sample_[1] = 0.0f;
  57. dc_blocker_.Init(1.0f - 20.0f / kSampleRate);
  58. }
  59. template<bool enable_dispersion>
  60. void String::ProcessInternal(
  61. const float* in,
  62. float* out,
  63. float* aux,
  64. size_t size) {
  65. float delay = 1.0f / frequency_;
  66. CONSTRAIN(delay, 4.0f, kDelayLineSize - 4.0f);
  67. // If there is not enough delay time in the delay line, we play at the
  68. // lowest possible note and we upsample on the fly with a shitty linear
  69. // interpolator. We don't care because it's a corner case (f0 < 11.7Hz)
  70. float src_ratio = delay * frequency_;
  71. if (src_ratio >= 0.9999f) {
  72. // When we are above 11.7 Hz, we make sure that the linear interpolator
  73. // does not get in the way.
  74. src_phase_ = 1.0f;
  75. src_ratio = 1.0f;
  76. }
  77. float clamped_position = 0.5f - 0.98f * fabs(position_ - 0.5f);
  78. // Linearly interpolate all comb-related CV parameters for each sample.
  79. ParameterInterpolator delay_modulation(
  80. &delay_, delay, size);
  81. ParameterInterpolator position_modulation(
  82. &clamped_position_, clamped_position, size);
  83. ParameterInterpolator dispersion_modulation(
  84. &previous_dispersion_, dispersion_, size);
  85. // For damping/absorption, the interpolation is done in the filter code.
  86. float lf_damping = damping_ * (2.0f - damping_);
  87. float rt60 = 0.07f * SemitonesToRatio(lf_damping * 96.0f) * kSampleRate;
  88. float rt60_base_2_12 = max(-120.0f * delay / src_ratio / rt60, -127.0f);
  89. float damping_coefficient = SemitonesToRatio(rt60_base_2_12);
  90. float brightness = brightness_ * brightness_;
  91. float noise_filter = SemitonesToRatio((brightness_ - 1.0f) * 48.0f);
  92. float damping_cutoff = min(
  93. 24.0f + damping_ * damping_ * 48.0f + brightness_ * brightness_ * 24.0f,
  94. 84.0f);
  95. float damping_f = min(frequency_ * SemitonesToRatio(damping_cutoff), 0.499f);
  96. // Crossfade to infinite decay.
  97. if (damping_ >= 0.95f) {
  98. float to_infinite = 20.0f * (damping_ - 0.95f);
  99. damping_coefficient += to_infinite * (1.0f - damping_coefficient);
  100. brightness += to_infinite * (1.0f - brightness);
  101. damping_f += to_infinite * (0.4999f - damping_f);
  102. damping_cutoff += to_infinite * (128.0f - damping_cutoff);
  103. }
  104. fir_damping_filter_.Configure(damping_coefficient, brightness, size);
  105. iir_damping_filter_.set_f_q<FREQUENCY_ACCURATE>(damping_f, 0.5f);
  106. ParameterInterpolator damping_compensation_modulation(
  107. &previous_damping_compensation_,
  108. 1.0f - Interpolate(lut_svf_shift, damping_cutoff, 1.0f),
  109. size);
  110. while (size--) {
  111. src_phase_ += src_ratio;
  112. if (src_phase_ > 1.0f) {
  113. src_phase_ -= 1.0f;
  114. float delay = delay_modulation.Next();
  115. float comb_delay = delay * position_modulation.Next();
  116. #ifndef MIC_W
  117. delay *= damping_compensation_modulation.Next(); // IIR delay.
  118. #endif // MIC_W
  119. delay -= 1.0f; // FIR delay.
  120. float s = 0.0f;
  121. if (enable_dispersion) {
  122. float noise = 2.0f * Random::GetFloat() - 1.0f;
  123. noise *= 1.0f / (0.2f + noise_filter);
  124. dispersion_noise_ += noise_filter * (noise - dispersion_noise_);
  125. float dispersion = dispersion_modulation.Next();
  126. float stretch_point = dispersion <= 0.0f
  127. ? 0.0f
  128. : dispersion * (2.0f - dispersion) * 0.475f;
  129. float noise_amount = dispersion > 0.75f
  130. ? 4.0f * (dispersion - 0.75f)
  131. : 0.0f;
  132. float bridge_curving = dispersion < 0.0f
  133. ? -dispersion
  134. : 0.0f;
  135. noise_amount = noise_amount * noise_amount * 0.025f;
  136. float ac_blocking_amount = bridge_curving;
  137. bridge_curving = bridge_curving * bridge_curving * 0.01f;
  138. float ap_gain = -0.618f * dispersion / (0.15f + fabs(dispersion));
  139. float delay_fm = 1.0f;
  140. delay_fm += dispersion_noise_ * noise_amount;
  141. delay_fm -= curved_bridge_ * bridge_curving;
  142. delay *= delay_fm;
  143. float ap_delay = delay * stretch_point;
  144. float main_delay = delay - ap_delay;
  145. if (ap_delay >= 4.0f && main_delay >= 4.0f) {
  146. s = string_.ReadHermite(main_delay);
  147. s = stretch_.Allpass(s, ap_delay, ap_gain);
  148. } else {
  149. s = string_.ReadHermite(delay);
  150. }
  151. float s_ac = s;
  152. dc_blocker_.Process(&s_ac, 1);
  153. s += ac_blocking_amount * (s_ac - s);
  154. float value = fabs(s) - 0.025f;
  155. float sign = s > 0.0f ? 1.0f : -1.5f;
  156. curved_bridge_ = (fabs(value) + value) * sign;
  157. } else {
  158. s = string_.ReadHermite(delay);
  159. }
  160. s += *in; // When f0 < 11.7 Hz, causes ugly bitcrushing on the input!
  161. s = fir_damping_filter_.Process(s);
  162. #ifndef MIC_W
  163. s = iir_damping_filter_.Process<FILTER_MODE_LOW_PASS>(s);
  164. #endif // MIC_W
  165. string_.Write(s);
  166. out_sample_[1] = out_sample_[0];
  167. aux_sample_[1] = aux_sample_[0];
  168. out_sample_[0] = s;
  169. aux_sample_[0] = string_.Read(comb_delay);
  170. }
  171. *out++ += Crossfade(out_sample_[1], out_sample_[0], src_phase_);
  172. *aux++ += Crossfade(aux_sample_[1], aux_sample_[0], src_phase_);
  173. in++;
  174. }
  175. }
  176. void String::Process(const float* in, float* out, float* aux, size_t size) {
  177. if (enable_dispersion_) {
  178. ProcessInternal<true>(in, out, aux, size);
  179. } else {
  180. ProcessInternal<false>(in, out, aux, size);
  181. }
  182. }
  183. } // namespace rings