DaisySP
Loading...
Searching...
No Matches
delayline.h
1/*
2Copyright (c) 2020 Electrosmith, Corp
3
4Use of this source code is governed by an MIT-style
5license that can be found in the LICENSE file or at
6https://opensource.org/licenses/MIT.
7*/
8
9#pragma once
10#ifndef DSY_DELAY_H
11#define DSY_DELAY_H
12#include <stdlib.h>
13#include <stdint.h>
14namespace daisysp
15{
27template <typename T, size_t max_size>
29{
30 public:
31 DelayLine() {}
32 ~DelayLine() {}
35 void Init() { Reset(); }
38 void Reset()
39 {
40 for(size_t i = 0; i < max_size; i++)
41 {
42 line_[i] = T(0);
43 }
44 write_ptr_ = 0;
45 delay_ = 1;
46 }
47
51 inline void SetDelay(size_t delay)
52 {
53 frac_ = 0.0f;
54 delay_ = delay < max_size ? delay : max_size - 1;
55 }
56
60 inline void SetDelay(float delay)
61 {
62 int32_t int_delay = static_cast<int32_t>(delay);
63 frac_ = delay - static_cast<float>(int_delay);
64 delay_ = static_cast<size_t>(int_delay) < max_size ? int_delay
65 : max_size - 1;
66 }
67
70 inline void Write(const T sample)
71 {
72 line_[write_ptr_] = sample;
73 write_ptr_ = (write_ptr_ - 1 + max_size) % max_size;
74 }
75
78 inline const T Read() const
79 {
80 T a = line_[(write_ptr_ + delay_) % max_size];
81 T b = line_[(write_ptr_ + delay_ + 1) % max_size];
82 return a + (b - a) * frac_;
83 }
84
86 inline const T Read(float delay) const
87 {
88 int32_t delay_integral = static_cast<int32_t>(delay);
89 float delay_fractional = delay - static_cast<float>(delay_integral);
90 const T a = line_[(write_ptr_ + delay_integral) % max_size];
91 const T b = line_[(write_ptr_ + delay_integral + 1) % max_size];
92 return a + (b - a) * delay_fractional;
93 }
94
95 inline const T ReadHermite(float delay) const
96 {
97 int32_t delay_integral = static_cast<int32_t>(delay);
98 float delay_fractional = delay - static_cast<float>(delay_integral);
99
100 int32_t t = (write_ptr_ + delay_integral + max_size);
101 const T xm1 = line_[(t - 1) % max_size];
102 const T x0 = line_[(t) % max_size];
103 const T x1 = line_[(t + 1) % max_size];
104 const T x2 = line_[(t + 2) % max_size];
105 const float c = (x1 - xm1) * 0.5f;
106 const float v = x0 - x1;
107 const float w = c + v;
108 const float a = w + v + (x2 - x0) * 0.5f;
109 const float b_neg = w + a;
110 const float f = delay_fractional;
111 return (((a * f) - b_neg) * f + c) * f + x0;
112 }
113
114 inline const T Allpass(const T sample, size_t delay, const T coefficient)
115 {
116 T read = line_[(write_ptr_ + delay) % max_size];
117 T write = sample + coefficient * read;
118 Write(write);
119 return -write * coefficient + read;
120 }
121
122 private:
123 float frac_;
124 size_t write_ptr_;
125 size_t delay_;
126 T line_[max_size];
127};
128} // namespace daisysp
129#endif
Definition delayline.h:29
void SetDelay(size_t delay)
Definition delayline.h:51
const T Read() const
Definition delayline.h:78
void SetDelay(float delay)
Definition delayline.h:60
const T Read(float delay) const
Definition delayline.h:86
void Write(const T sample)
Definition delayline.h:70
void Reset()
Definition delayline.h:38
void Init()
Definition delayline.h:35
FIR Filter implementation, generic and ARM CMSIS DSP based.
Definition adenv.h:16