1 // Copyright 2016 Emilie Gillet.
2 //
3 // Author: Emilie Gillet (emilie.o.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. "Lite" version of the implementation used in Rings.
28 
29 #ifndef PLAITS_DSP_PHYSICAL_MODELLING_STRING_H_
30 #define PLAITS_DSP_PHYSICAL_MODELLING_STRING_H_
31 
32 #include <algorithm>
33 
34 #include "stmlib/stmlib.h"
35 
36 #include "stmlib/dsp/filter.h"
37 #include "stmlib/utils/buffer_allocator.h"
38 
39 #include "plaits/dsp/physical_modelling/delay_line.h"
40 
41 namespace plaits {
42 
43 const size_t kDelayLineSize = 1024;
44 
45 enum StringNonLinearity {
46   STRING_NON_LINEARITY_CURVED_BRIDGE,
47   STRING_NON_LINEARITY_DISPERSION
48 };
49 
50 class String {
51  public:
String()52   String() { }
~String()53   ~String() { }
54 
55   void Init(stmlib::BufferAllocator* allocator);
56   void Reset();
57   void Process(
58       float f0,
59       float non_linearity_amount,
60       float brightness,
61       float damping,
62       const float* in,
63       float* out,
64       size_t size);
65 
66  private:
67   template<StringNonLinearity non_linearity>
68   void ProcessInternal(
69       float f0,
70       float non_linearity_amount,
71       float brightness,
72       float damping,
73       const float* in,
74       float* out,
75       size_t size);
76 
77   DelayLine<float, kDelayLineSize> string_;
78   DelayLine<float, kDelayLineSize / 4> stretch_;
79 
80   stmlib::Svf iir_damping_filter_;
81   stmlib::DCBlocker dc_blocker_;
82 
83   float delay_;
84   float dispersion_noise_;
85   float curved_bridge_;
86 
87   // Very crappy linear interpolation upsampler used for low pitches that
88   // do not fit the delay line. Rarely used.
89   float src_phase_;
90   float out_sample_[2];
91 
92   DISALLOW_COPY_AND_ASSIGN(String);
93 };
94 
95 }  // namespace plaits
96 
97 #endif  // PLAITS_DSP_PHYSICAL_MODELLING_STRING_H_
98