-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDel.h
More file actions
259 lines (228 loc) · 7.86 KB
/
Del.h
File metadata and controls
259 lines (228 loc) · 7.86 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* 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 = nullptr;
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
bool usePSRAM = false;
public:
/** Constructor.
* Create but don't setup delay.
* To use, setMaxDecayTime() must be called to initiate the audio buffer.
*/
Del() : delayBuffer(nullptr) {};
/** 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) {
if (delayBuffer) { delete[] delayBuffer; delayBuffer = nullptr; }
maxDelayTime_ms = max((unsigned int)0, maxDelayTime);
const unsigned int MAX_SAFE_DELAY_MS = 4294967295U / SAMPLE_RATE; // ~97,000ms at 44.1kHz
if (maxDelayTime_ms > MAX_SAFE_DELAY_MS) {
Serial.print("WARNING: Delay time ");
Serial.print(maxDelayTime_ms);
Serial.print("ms exceeds safe maximum ");
Serial.print(MAX_SAFE_DELAY_MS);
Serial.println("ms, capping to maximum");
maxDelayTime_ms = MAX_SAFE_DELAY_MS;
}
delayBufferSize_samples = maxDelayTime_ms * SAMPLE_RATE * 0.001;
#if IS_ESP32()
// Try PSRAM allocation with size checking (silent for delay buffers)
delayBuffer = psramAllocInt16(delayBufferSize_samples, nullptr);
if (!delayBuffer) {
// Fallback to regular RAM
delayBuffer = new int16_t[delayBufferSize_samples];
}
#else
delayBuffer = new int16_t[delayBufferSize_samples];
#endif
if (!delayBuffer) {
Serial.println("ERROR: Del buffer allocation failed!");
delayBufferSize_samples = 0;
maxDelayTime_ms = 0;
return;
}
empty();
}
/** Constructor.
@param maxDelayTime The size of the delay buffer, in milliseconds.
*/
Del(unsigned int maxDelayTime) {
setMaxDelayTime(maxDelayTime);
}
~Del() {
if (delayBuffer) { delete[] delayBuffer; delayBuffer = nullptr; }
}
/** 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.
* Automatically allocates buffer if not yet allocated or if too small.
*/
void setTime(float msDur) {
msDur = max(0.0f, msDur);
// Auto-allocate buffer if needed (not allocated or too small)
if (!delayBuffer || msDur > maxDelayTime_ms) {
// Add 10% headroom to reduce reallocations from small time changes
unsigned int newMaxTime = (unsigned int)(msDur * 1.1f) + 1;
setMaxDelayTime(newMaxTime);
}
delayTime_ms = min(maxDelayTime_ms - 1.0f, msDur);
delayTime_samples = (unsigned int)(msDur * SAMPLE_RATE * 0.001f);
}
/** Return the delay duration in milliseconds */
float getTime() {
return delayTime_ms;
}
/** Set delay time via scan rate (BBD.h API compatibility)
* @param rate Scan rate 0.01-1.0, where 1.0 = ~93ms, 0.1 = ~930ms
*/
void setScanRate(float rate) {
const float BASE_DELAY_MS = 92.88f; // 4096 samples at 44100Hz
rate = max(0.01f, min(1.0f, rate));
setTime(BASE_DELAY_MS / rate);
}
/** Return current scan rate (BBD.h API compatibility) */
float getScanRate() {
const float BASE_DELAY_MS = 92.88f;
if (delayTime_ms < BASE_DELAY_MS) return 1.0f;
return max(0.01f, BASE_DELAY_MS / delayTime_ms);
}
/** Specify the delay feedback level, from 0.0 to 1.0 */
void setLevel(float level) {
delayLevel = min((int32_t)1024, max((int32_t)0, (int32_t)(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((int32_t)1024, max((int32_t)0, (int32_t)(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() {
if (!delayBuffer) return;
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.98f; // prevent runaway
}
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
* @param pos The delay offset in samples, can be positive or negative
*/
inline
int16_t read(int pos) {
if (!delayBuffer) return 0;
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;
}
/** Write to the buffer and increment the write index
* @param inValue The signal input.
*/
inline
void write(int inValue) {
if (!delayBuffer) return;
if (delayBufferSize_samples == 0) return; // Prevent divide by zero
delayBuffer[writePos] = min(MAX_16, max(MIN_16, inValue));
writePos = (writePos + 1) % delayBufferSize_samples;
}
};
#endif /* DEL_H_ */