-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDel.h
201 lines (176 loc) · 5.75 KB
/
Del.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Del.h
*
* An audio delay line class.
*
* by Andrew R. Brown 2022
*
* Based on the Mozzi audio library by Tim Barrass 2012
*
* This file is part of the M16 audio library. Relies on M16.h
*
* M16 is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
*/
#ifndef DEL_H_
#define DEL_H_
class Del {
private:
int16_t * delayBuffer;
unsigned int writePos = 0;
float delayTime_ms = 0.0f;
unsigned int delayTime_samples = 0;
int16_t delayLevel = 1024; // 0 to 1024
float maxDelayTime_ms = 0;
unsigned int delayBufferSize_samples = 0;
bool delayFeedback = false;
int16_t prevOutValue = 0;
byte filtered = 2;
int16_t feedbackLevel = 512; // 0 to 1024
public:
/** Constructor.
* Create but don't setup delay.
* To use, setMaxDecayTime() must be called to initiate the audio buffer.
*/
Del() {};
/** Constructor.
* Create and setup delay.
* @param maxDelayTime The maximum delay time in milliseconds
* @param msDur The initial delay time in milliseconds, up to maxDelayTime
* @param level The delay feedback level, from 0.0 to 1.0
* @param feedback Multitap delay feedback on or off, true or false
*/
Del(unsigned int maxDelayTime, int msDur, float level, bool feedback) {
setMaxDelayTime(maxDelayTime);
setTime(msDur);
setLevel(level);
setFeedback(feedback);
}
/**
* Set the maximum delay time in milliseconds
* @param maxDelayTime The maximum delay time in milliseconds
*/
void setMaxDelayTime(unsigned int maxDelayTime) {
delete[] delayBuffer; // remove any previous memory allocation
maxDelayTime_ms = max((unsigned int)0, maxDelayTime);
delayBufferSize_samples = maxDelayTime_ms * SAMPLE_RATE * 0.001;
delayBuffer = new int16_t[delayBufferSize_samples]; // create a new audio buffer
empty();
}
/** Constructor.
@param maxDelayTime The size of the delay buffer, in milliseconds.
*/
Del(unsigned int maxDelayTime) {
setMaxDelayTime(maxDelayTime);
}
~Del() {
delete[] delayBuffer;
}
/** Return the size of the delay buffer in ms */
float getBufferSize() {
return maxDelayTime_ms;
}
/** Return the delay length in samples */
unsigned int getDelayLength() {
return delayTime_samples;
}
/** Return the size of the delay buffer in samples */
unsigned int getBufferLength() {
return delayBufferSize_samples;
}
/** Specify the delay duration in milliseconds */
void setTime(float msDur) {
delayTime_ms = min(maxDelayTime_ms - 1.0f, max(0.0f, msDur));
delayTime_samples = msDur * SAMPLE_RATE * 0.001;
}
/** Return the delay duration in milliseconds */
float getTime() {
return delayTime_ms;
}
/** Specify the delay feedback level, from 0.0 to 1.0 */
void setLevel(float level) {
delayLevel = min(1024, max(0, (int)(pow(level, 0.8) * 1024.0f)));
}
/** Return the delay level, from 0.0 to 1.0 */
float getLevel() {
return delayLevel * 0.0009765625f;
}
/** Turn delay feedback on or off */
void setFeedback(bool state) {
delayFeedback = state;
}
/** Specify the delay feedback level, from 0.0 to 1.0 */
void setFeedbackLevel(float level) {
setFeedback(true); // ensure feedback is on
feedbackLevel = min(1024, max(0, (int)(pow(level, 0.8) * 1024.0f)));
}
/** Return the delay level, from 0.0 to 1.0 */
float getFeedbackLevel() {
return feedbackLevel * 0.0009765625f;
}
/** Specify the degree of filtering of the delay signal, from 0 (none) to 4 (most dull) */
void setFiltered(byte newVal) {
if (newVal >= 0) filtered = newVal;
}
/** Fill the delay with silence */
void empty() {
for(int i=0; i<delayBufferSize_samples; i++) {
delayBuffer[i] = 0; // zero out the buffer
}
}
/** Input a value to the delay and retrieve the signal delayed by delayTime milliseconds.
* @param inValue The signal input.
*/
inline
int16_t next(int32_t inValue) {
int32_t outValue = 0;
if (delayTime_samples > 0) {
outValue = read();
if (outValue > MAX_16) outValue = MAX_16;
if (outValue < MIN_16) outValue = MIN_16;
}
if (delayFeedback) {
inValue = (inValue + ((outValue * feedbackLevel)>>10)) * 0.9f;
}
if (inValue > MAX_16) inValue = MAX_16;
if (inValue < MIN_16) inValue = MIN_16;
write(inValue);
return outValue;
}
/** Read the buffer at the delayTime without incrementing read/write index */
inline
int16_t read() {
return read(0);
}
/** Read the buffer at the delayTime + an offset without incrementing read/write index
* @pos delay offset in samples, can be positive or negative
*/
inline
int16_t read(int32_t pos) {
int outValue = 0;
int readPos = writePos - delayTime_samples + pos;
if (readPos < 0) readPos += delayBufferSize_samples;
if (readPos >= delayBufferSize_samples) readPos -= delayBufferSize_samples;
outValue = min(MAX_16, max(MIN_16, (int)delayBuffer[readPos]));
if(filtered > 0) {
if (filtered == 1) {
outValue = (outValue + outValue + outValue + prevOutValue)>>2; // smooth
} else if (filtered == 2) {
outValue = (outValue + prevOutValue)>>1; // smooth
} else if (filtered == 3) {
outValue = (outValue + prevOutValue + prevOutValue + prevOutValue)>>2; // smooth
} else outValue = (outValue + prevOutValue + prevOutValue + prevOutValue +
prevOutValue + prevOutValue + prevOutValue + prevOutValue)>>3; // smooth
prevOutValue = outValue;
}
return (outValue * delayLevel)>>10;
}
/** Read the buffer at the delayTime and increment the read/write index
* @param inVal The signal input.
*/
inline
void write(int inValue) {
delayBuffer[writePos] = min(MAX_16, max(MIN_16, inValue));
writePos = (writePos + 1) % delayBufferSize_samples;
}
};
#endif /* DEL_H_ */