-
Notifications
You must be signed in to change notification settings - Fork 9
/
delay.h
48 lines (43 loc) · 1.01 KB
/
delay.h
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
class Delay {
uint8_t delayline[16000];
uint16_t pos;
uint16_t posmax;
uint8_t feedback;
public:
void Init() {
posmax = 16000;
// initialize the delay line
for (uint8_t i = 0; i < 16000; i++) {
delayline[i] = 128;
}
}
void SetTime(uint16_t posmax_) {
posmax = posmax_;
if (pos > posmax) {
pos = 0;
}
}
void SetFeedback(uint8_t feedback_) { feedback = feedback_; }
uint8_t Update(uint8_t audio_now_) {
// add delayed audio
int16_t audio_now = (audio_now_ + (delayline[pos] - 128));
if (audio_now > 255) {
audio_now = 255;
} else if (audio_now < 0) {
audio_now = 0;
}
// determine next delay
int16_t next_delay = (audio_now - 128) * feedback / 100 + 128;
if (next_delay > 255) {
next_delay = 255;
} else if (next_delay < 0) {
next_delay = 0;
}
delayline[pos] = next_delay;
pos++;
if (pos > posmax) {
pos = 0;
}
return audio_now;
}
};