1 // Copyright 2014 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 // Delay line.
28 
29 #ifndef STMLIB_DSP_DELAY_LINE_H_
30 #define STMLIB_DSP_DELAY_LINE_H_
31 
32 #include "stmlib/stmlib.h"
33 #include "stmlib/dsp/dsp.h"
34 
35 #include <algorithm>
36 
37 namespace stmlib {
38 
39 template<typename T, size_t max_delay>
40 class DelayLine {
41  public:
DelayLine()42   DelayLine() { }
~DelayLine()43   ~DelayLine() { }
44 
Init()45   void Init() {
46     Reset();
47   }
48 
Reset()49   void Reset() {
50     std::fill(&line_[0], &line_[max_delay], T(0));
51     delay_ = 1;
52     write_ptr_ = 0;
53   }
54 
set_delay(size_t delay)55   inline void set_delay(size_t delay) {
56     delay_ = delay;
57   }
58 
Write(const T sample)59   inline void Write(const T sample) {
60     line_[write_ptr_] = sample;
61     write_ptr_ = (write_ptr_ - 1 + max_delay) % max_delay;
62   }
63 
Allpass(const T sample,size_t delay,const T coefficient)64   inline const T Allpass(const T sample, size_t delay, const T coefficient) {
65     T read = line_[(write_ptr_ + delay) % max_delay];
66     T write = sample + coefficient * read;
67     Write(write);
68     return -write * coefficient + read;
69   }
70 
WriteRead(const T sample,float delay)71   inline const T WriteRead(const T sample, float delay) {
72     Write(sample);
73     return Read(delay);
74   }
75 
Read()76   inline const T Read() const {
77     return line_[(write_ptr_ + delay_) % max_delay];
78   }
79 
Read(size_t delay)80   inline const T Read(size_t delay) const {
81     return line_[(write_ptr_ + delay) % max_delay];
82   }
83 
Read(float delay)84   inline const T Read(float delay) const {
85     MAKE_INTEGRAL_FRACTIONAL(delay)
86     const T a = line_[(write_ptr_ + delay_integral) % max_delay];
87     const T b = line_[(write_ptr_ + delay_integral + 1) % max_delay];
88     return a + (b - a) * delay_fractional;
89   }
90 
ReadHermite(float delay)91   inline const T ReadHermite(float delay) const {
92     MAKE_INTEGRAL_FRACTIONAL(delay)
93     int32_t t = (write_ptr_ + delay_integral + max_delay);
94     const T xm1 = line_[(t - 1) % max_delay];
95     const T x0 = line_[(t) % max_delay];
96     const T x1 = line_[(t + 1) % max_delay];
97     const T x2 = line_[(t + 2) % max_delay];
98     const float c = (x1 - xm1) * 0.5f;
99     const float v = x0 - x1;
100     const float w = c + v;
101     const float a = w + v + (x2 - x0) * 0.5f;
102     const float b_neg = w + a;
103     const float f = delay_fractional;
104     return (((a * f) - b_neg) * f + c) * f + x0;
105   }
106 
107  private:
108   size_t write_ptr_;
109   size_t delay_;
110   T line_[max_delay];
111 
112   DISALLOW_COPY_AND_ASSIGN(DelayLine);
113 };
114 
115 }  // namespace stmlib
116 
117 #endif  // STMLIB_DSP_DELAY_LINE_H_
118 
119 
120 
121 
122