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 // 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 std::fill(&line_[0], &line_[max_delay], T(0)); 47 delay_ = 1; 48 write_ptr_ = 0; 49 } 50 set_delay(size_t delay)51 inline void set_delay(size_t delay) { 52 delay_ = delay; 53 } 54 Write(const T sample)55 inline void Write(const T sample) { 56 line_[write_ptr_] = sample; 57 write_ptr_ = (write_ptr_ - 1 + max_delay) % max_delay; 58 } 59 Allpass(const T sample,size_t delay,const T coefficient)60 inline const T Allpass(const T sample, size_t delay, const T coefficient) { 61 float read = line_[(write_ptr_ + delay) % max_delay]; 62 float write = sample + coefficient * read; 63 Write(write); 64 return -write * coefficient + read; 65 } 66 WriteRead(const T sample,float delay)67 inline const T WriteRead(const T sample, float delay) { 68 Write(sample); 69 return Read(delay); 70 } 71 Read()72 inline const T Read() const { 73 return line_[(write_ptr_ + delay_) % max_delay]; 74 } 75 Read(size_t delay)76 inline const T Read(size_t delay) const { 77 return line_[(write_ptr_ + delay) % max_delay]; 78 } 79 Read(float delay)80 inline const T Read(float delay) const { 81 MAKE_INTEGRAL_FRACTIONAL(delay) 82 const T a = line_[(write_ptr_ + delay_integral) % max_delay]; 83 const T b = line_[(write_ptr_ + delay_integral + 1) % max_delay]; 84 return a + (b - a) * delay_fractional; 85 } 86 ReadHermite(float delay)87 inline const T ReadHermite(float delay) const { 88 MAKE_INTEGRAL_FRACTIONAL(delay) 89 int32_t t = (write_ptr_ + delay_integral + max_delay); 90 const T xm1 = line_[(t - 1) % max_delay]; 91 const T x0 = line_[(t) % max_delay]; 92 const T x1 = line_[(t + 1) % max_delay]; 93 const T x2 = line_[(t + 2) % max_delay]; 94 const float c = (x1 - xm1) * 0.5f; 95 const float v = x0 - x1; 96 const float w = c + v; 97 const float a = w + v + (x2 - x0) * 0.5f; 98 const float b_neg = w + a; 99 const float f = delay_fractional; 100 return (((a * f) - b_neg) * f + c) * f + x0; 101 102 } 103 104 105 private: 106 size_t write_ptr_; 107 size_t delay_; 108 T line_[max_delay]; 109 110 DISALLOW_COPY_AND_ASSIGN(DelayLine); 111 }; 112 113 } // namespace stmlib 114 115 #endif // STMLIB_DSP_DELAY_LINE_H_ 116 117 118 119 120