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