-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdelay.cpp
358 lines (289 loc) · 10.2 KB
/
delay.cpp
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* delay.cpp
Computer Music Toolkit - a library of LADSPA plugins. Copyright (C)
2000-2002 Richard W.E. Furse. The author may be contacted at
richard@muse.demon.co.uk.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public Licence as
published by the Free Software Foundation; either version 2 of the
Licence, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA. */
/*****************************************************************************/
/* This module provides delays and delays with feedback. A variety of
maximum delay times are available. (The plugins reserve different
amounts of memory space on this basis.) */
/*****************************************************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
/*****************************************************************************/
#include "cmt.h"
/*****************************************************************************/
#define DELAY_TYPE_COUNT 2
#define DELAY_LENGTH_COUNT 5
/*****************************************************************************/
#define LIMIT_BETWEEN(x, a, b) \
(((x) < a) ? a : (((x) > b) ? b : (x)))
/*****************************************************************************/
#define DL_DELAY_LENGTH 0
#define DL_DRY_WET 1
#define DL_INPUT 2
#define DL_OUTPUT 3
/* Present only on feedback delays: */
#define DL_FEEDBACK 4
static void activateDelayLine(LADSPA_Handle Instance);
static void runSimpleDelayLine(LADSPA_Handle Instance,
unsigned long SampleCount);
static void runFeedbackDelayLine(LADSPA_Handle Instance,
unsigned long SampleCount);
/** This class is used to implement delay line plugins. Different
maximum delay times are supported as are both echo and feedback
delays. */
class DelayLine : public CMT_PluginInstance {
private:
LADSPA_Data m_fSampleRate;
LADSPA_Data m_fMaximumDelay;
LADSPA_Data * m_pfBuffer;
/** Buffer size, a power of two. */
unsigned long m_lBufferSize;
/** Write pointer in buffer. */
unsigned long m_lWritePointer;
friend void activateDelayLine(LADSPA_Handle Instance);
friend void runSimpleDelayLine(LADSPA_Handle Instance,
unsigned long SampleCount);
friend void runFeedbackDelayLine(LADSPA_Handle Instance,
unsigned long SampleCount);
public:
DelayLine(const unsigned long lSampleRate,
const LADSPA_Data fMaximumDelay)
: CMT_PluginInstance(4),
m_fSampleRate(LADSPA_Data(lSampleRate)),
m_fMaximumDelay(fMaximumDelay) {
/* Buffer size is a power of two bigger than max delay time. */
unsigned long lMinimumBufferSize
= (unsigned long)((LADSPA_Data)lSampleRate * m_fMaximumDelay);
m_lBufferSize = 1;
while (m_lBufferSize < lMinimumBufferSize)
m_lBufferSize <<= 1;
m_pfBuffer = new LADSPA_Data[m_lBufferSize];
}
~DelayLine() {
delete [] m_pfBuffer;
}
};
/*****************************************************************************/
/* Initialise and activate a plugin instance. */
static void
activateDelayLine(LADSPA_Handle Instance) {
DelayLine * poDelayLine = (DelayLine *)Instance;
/* Need to reset the delay history in this function rather than
instantiate() in case deactivate() followed by activate() have
been called to reinitialise a delay line. */
memset(poDelayLine->m_pfBuffer,
0,
sizeof(LADSPA_Data) * poDelayLine->m_lBufferSize);
poDelayLine->m_lWritePointer = 0;
}
/*****************************************************************************/
/* Run a delay line instance for a block of SampleCount samples. */
static void
runSimpleDelayLine(LADSPA_Handle Instance,
unsigned long SampleCount) {
DelayLine * poDelayLine = (DelayLine *)Instance;
unsigned long lBufferSizeMinusOne = poDelayLine->m_lBufferSize - 1;
unsigned long lDelay = (unsigned long)
(LIMIT_BETWEEN(*(poDelayLine->m_ppfPorts[DL_DELAY_LENGTH]),
0,
poDelayLine->m_fMaximumDelay)
* poDelayLine->m_fSampleRate);
LADSPA_Data * pfInput
= poDelayLine->m_ppfPorts[DL_INPUT];
LADSPA_Data * pfOutput
= poDelayLine->m_ppfPorts[DL_OUTPUT];
LADSPA_Data * pfBuffer
= poDelayLine->m_pfBuffer;
unsigned long lBufferWriteOffset
= poDelayLine->m_lWritePointer;
unsigned long lBufferReadOffset
= lBufferWriteOffset + poDelayLine->m_lBufferSize - lDelay;
LADSPA_Data fWet
= LIMIT_BETWEEN(*(poDelayLine->m_ppfPorts[DL_DRY_WET]),
0,
1);
LADSPA_Data fDry
= 1 - fWet;
for (unsigned long lSampleIndex = 0;
lSampleIndex < SampleCount;
lSampleIndex++) {
LADSPA_Data fInputSample = *(pfInput++);
*(pfOutput++) = (fDry * fInputSample
+ fWet * pfBuffer[((lSampleIndex + lBufferReadOffset)
& lBufferSizeMinusOne)]);
pfBuffer[((lSampleIndex + lBufferWriteOffset)
& lBufferSizeMinusOne)] = fInputSample;
}
poDelayLine->m_lWritePointer
= ((poDelayLine->m_lWritePointer + SampleCount)
& lBufferSizeMinusOne);
}
/*****************************************************************************/
/** Run a feedback delay line instance for a block of SampleCount samples. */
static void
runFeedbackDelayLine(LADSPA_Handle Instance,
unsigned long SampleCount) {
DelayLine * poDelayLine = (DelayLine *)Instance;
unsigned long lBufferSizeMinusOne = poDelayLine->m_lBufferSize - 1;
unsigned long lDelay = (unsigned long)
(LIMIT_BETWEEN(*(poDelayLine->m_ppfPorts[DL_DELAY_LENGTH]),
0,
poDelayLine->m_fMaximumDelay)
* poDelayLine->m_fSampleRate);
LADSPA_Data * pfInput
= poDelayLine->m_ppfPorts[DL_INPUT];
LADSPA_Data * pfOutput
= poDelayLine->m_ppfPorts[DL_OUTPUT];
LADSPA_Data * pfBuffer
= poDelayLine->m_pfBuffer;
unsigned long lBufferWriteOffset
= poDelayLine->m_lWritePointer;
unsigned long lBufferReadOffset
= lBufferWriteOffset + poDelayLine->m_lBufferSize - lDelay;
LADSPA_Data fWet
= LIMIT_BETWEEN(*(poDelayLine->m_ppfPorts[DL_DRY_WET]),
0,
1);
LADSPA_Data fDry
= 1 - fWet;
LADSPA_Data fFeedback
= LIMIT_BETWEEN(*(poDelayLine->m_ppfPorts[DL_FEEDBACK]),
-1,
1);
for (unsigned long lSampleIndex = 0;
lSampleIndex < SampleCount;
lSampleIndex++) {
LADSPA_Data fInputSample = *(pfInput++);
LADSPA_Data &fDelayedSample = pfBuffer[((lSampleIndex + lBufferReadOffset)
& lBufferSizeMinusOne)];
*(pfOutput++) = (fDry * fInputSample + fWet * fDelayedSample);
pfBuffer[((lSampleIndex + lBufferWriteOffset)
& lBufferSizeMinusOne)]
= fInputSample + fDelayedSample * fFeedback;
}
poDelayLine->m_lWritePointer
= ((poDelayLine->m_lWritePointer + SampleCount)
& lBufferSizeMinusOne);
}
/*****************************************************************************/
template <long lMaximumDelayMilliseconds>
static LADSPA_Handle
CMT_Delay_Instantiate(const LADSPA_Descriptor * Descriptor,
unsigned long SampleRate) {
return new DelayLine(SampleRate,
LADSPA_Data(lMaximumDelayMilliseconds
* 0.001));
}
/*****************************************************************************/
void
initialise_delay() {
CMT_Descriptor * psDescriptor;
const char * apcDelayTypeNames[DELAY_TYPE_COUNT] = {
"Echo",
"Feedback"
};
const char * apcDelayTypeLabels[DELAY_TYPE_COUNT] = {
"delay",
"fbdelay"
};
LADSPA_Run_Function afRunFunctions[DELAY_TYPE_COUNT] = {
runSimpleDelayLine,
runFeedbackDelayLine
};
LADSPA_Data afMaximumDelays[DELAY_LENGTH_COUNT] = {
0.01,
0.1,
1,
5,
60
};
LADSPA_Instantiate_Function afInstantiateFunctions[DELAY_LENGTH_COUNT] = {
CMT_Delay_Instantiate<10>,
CMT_Delay_Instantiate<100>,
CMT_Delay_Instantiate<1000>,
CMT_Delay_Instantiate<5000>,
CMT_Delay_Instantiate<60000>
};
for (long lDelayTypeIndex = 0;
lDelayTypeIndex < DELAY_TYPE_COUNT;
lDelayTypeIndex++) {
for (long lDelayLengthIndex = 0;
lDelayLengthIndex < DELAY_LENGTH_COUNT;
lDelayLengthIndex++) {
long lPluginIndex
= lDelayTypeIndex * DELAY_LENGTH_COUNT + lDelayLengthIndex;
char acLabel[100];
sprintf(acLabel,
"%s_%gs",
apcDelayTypeLabels[lDelayTypeIndex],
afMaximumDelays[lDelayLengthIndex]);
char acName[100];
sprintf(acName,
"%s Delay Line (Maximum Delay %gs)",
apcDelayTypeNames[lDelayTypeIndex],
afMaximumDelays[lDelayLengthIndex]);
psDescriptor = new CMT_Descriptor
(1053 + lPluginIndex,
acLabel,
LADSPA_PROPERTY_HARD_RT_CAPABLE,
acName,
CMT_MAKER("Richard W.E. Furse"),
CMT_COPYRIGHT("2000-2002", "Richard W.E. Furse"),
NULL,
afInstantiateFunctions[lDelayLengthIndex],
activateDelayLine,
afRunFunctions[lDelayTypeIndex],
NULL,
NULL,
NULL);
psDescriptor->addPort
(LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL,
"Delay (Seconds)",
(LADSPA_HINT_BOUNDED_BELOW
| LADSPA_HINT_BOUNDED_ABOVE
| LADSPA_HINT_DEFAULT_1),
0,
afMaximumDelays[lDelayLengthIndex]);
psDescriptor->addPort
(LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL,
"Dry/Wet Balance",
(LADSPA_HINT_BOUNDED_BELOW
| LADSPA_HINT_BOUNDED_ABOVE
| LADSPA_HINT_DEFAULT_MIDDLE),
0,
1);
psDescriptor->addPort
(LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO,
"Input");
psDescriptor->addPort
(LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO,
"Output");
if (lDelayTypeIndex == 1)
psDescriptor->addPort
(LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL,
"Feedback",
(LADSPA_HINT_BOUNDED_BELOW
| LADSPA_HINT_BOUNDED_ABOVE
| LADSPA_HINT_DEFAULT_HIGH),
-1,
1);
registerNewPluginDescriptor(psDescriptor);
}
}
}
/*****************************************************************************/
/* EOF */