-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDelayLine.h
More file actions
87 lines (73 loc) · 1.54 KB
/
Copy pathDelayLine.h
File metadata and controls
87 lines (73 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma once
#include "Commons.h"
#include "Interpolator.h"
#include <stdint.h>
class DelayLine
{
private:
FloatArray buffer_;
uint32_t size_, writeIndex_, delay_;
public:
DelayLine(uint32_t size)
{
size_ = size;
buffer_ = FloatArray::create(size_);
delay_ = size_ - 1;
writeIndex_ = 0;
}
~DelayLine()
{
FloatArray::destroy(buffer_);
}
static DelayLine* create(uint32_t size)
{
return new DelayLine(size);
}
static void destroy(DelayLine* line)
{
delete line;
}
void clear()
{
buffer_.clear();
}
void setDelay(uint32_t delay)
{
delay_ = delay;
}
inline float readAt(int index)
{
int i = writeIndex_ - index - 1;
if (i < 0)
{
i += size_;
}
return Clamp(buffer_[i], -3.f, 3.f);
}
inline float read(float index)
{
size_t idx = (size_t)index;
float y0 = readAt(idx);
float y1 = readAt(idx + 1);
float frac = index - idx;
return Interpolator::linear(y0, y1, frac);
}
inline float read(float index1, float index2, float x)
{
float v = read(index1);
if (x == 0)
{
return v;
}
return v * (1.f - x) + read(index2) * x;
}
inline void write(float value, int stride = 1)
{
buffer_[writeIndex_] = value;
writeIndex_ += stride;
if (writeIndex_ >= size_)
{
writeIndex_ -= size_;
}
}
};