-
Notifications
You must be signed in to change notification settings - Fork 6
/
AudioEffect.cpp
394 lines (347 loc) · 12 KB
/
AudioEffect.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//-------------------------------------------------------------------------------------------------------
// VST Plug-Ins SDK
// Version 1.0
// © 2003 Steinberg Media Technologies, All Rights Reserved
//
// you should not have to edit this file
// use override methods instead
//-------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#ifndef __AudioEffect__
#include "AudioEffect.hpp"
#endif
#ifndef __AEffEditor__
#include "AEffEditor.hpp"
#endif
//-------------------------------------------------------------------------------------------------------
long dispatchEffectClass (AEffect *e, long opCode, long index, long value, void *ptr, float opt)
{
AudioEffect *ae = (AudioEffect*)(e->object);
if (opCode == effClose)
{
ae->dispatcher (opCode, index, value, ptr, opt);
delete ae;
return 1;
}
return ae->dispatcher (opCode, index, value, ptr, opt);
}
//-------------------------------------------------------------------------------------------------------
float getParameterClass (AEffect *e, long index)
{
AudioEffect *ae = (AudioEffect*)(e->object);
return ae->getParameter (index);
}
//-------------------------------------------------------------------------------------------------------
void setParameterClass (AEffect *e, long index, float value)
{
AudioEffect *ae = (AudioEffect*)(e->object);
ae->setParameter (index, value);
}
//-------------------------------------------------------------------------------------------------------
void processClass (AEffect *e, float **inputs, float **outputs, long sampleFrames)
{
AudioEffect *ae = (AudioEffect*)(e->object);
ae->process (inputs, outputs, sampleFrames);
}
//-------------------------------------------------------------------------------------------------------
void processClassReplacing (AEffect *e, float **inputs, float **outputs, long sampleFrames)
{
AudioEffect *ae = (AudioEffect*)(e->object);
ae->processReplacing (inputs, outputs, sampleFrames);
}
//-------------------------------------------------------------------------------------------------------
// AudioEffect Implementation
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
AudioEffect::AudioEffect (audioMasterCallback audioMaster, long numPrograms, long numParams)
{
this->audioMaster = audioMaster;
editor = 0;
this->numPrograms = numPrograms;
this->numParams = numParams;
curProgram = 0;
memset(&cEffect, 0, sizeof (cEffect));
cEffect.magic = kEffectMagic;
cEffect.dispatcher = dispatchEffectClass;
cEffect.process = processClass;
cEffect.setParameter = setParameterClass;
cEffect.getParameter = getParameterClass;
cEffect.numPrograms = numPrograms;
cEffect.numParams = numParams;
cEffect.numInputs = 1;
cEffect.numOutputs = 2;
cEffect.flags = 0;
cEffect.resvd1 = 0;
cEffect.resvd2 = 0;
cEffect.initialDelay = 0;
cEffect.realQualities = 0;
cEffect.offQualities = 0;
cEffect.ioRatio = 1.f;
cEffect.object = this;
cEffect.user = 0;
cEffect.uniqueID = CCONST ('N', 'o', 'E', 'f'); // you must set this!
cEffect.version = 1;
cEffect.processReplacing = processClassReplacing;
sampleRate = 44100.f;
blockSize = 1024L;
}
//-------------------------------------------------------------------------------------------------------
AudioEffect::~AudioEffect ()
{
if (editor)
delete editor;
}
//-------------------------------------------------------------------------------------------------------
long AudioEffect::dispatcher (long opCode, long index, long value, void *ptr, float opt)
{
long v = 0;
switch (opCode)
{
case effOpen: open (); break;
case effClose: close (); break;
case effSetProgram: if (value < numPrograms) setProgram (value); break;
case effGetProgram: v = getProgram (); break;
case effSetProgramName: setProgramName ((char *)ptr); break;
case effGetProgramName: getProgramName ((char *)ptr); break;
case effGetParamLabel: getParameterLabel (index, (char *)ptr); break;
case effGetParamDisplay: getParameterDisplay (index, (char *)ptr); break;
case effGetParamName: getParameterName (index, (char *)ptr); break;
case effSetSampleRate: setSampleRate (opt); break;
case effSetBlockSize: setBlockSize (value); break;
case effMainsChanged: if (!value) suspend (); else resume (); break;
case effGetVu: v = (long)(getVu () * 32767.); break;
// Editor
case effEditGetRect: if (editor) v = editor->getRect ((ERect **)ptr); break;
case effEditOpen: if (editor) v = editor->open (ptr); break;
case effEditClose: if (editor) editor->close (); break;
case effEditIdle: if (editor) editor->idle (); break;
#if MAC
case effEditDraw: if (editor) editor->draw ((ERect *)ptr); break;
case effEditMouse: if (editor) v = editor->mouse (index, value); break;
case effEditKey: if (editor) v = editor->key (value); break;
case effEditTop: if (editor) editor->top (); break;
case effEditSleep: if (editor) editor->sleep (); break;
#endif
case effIdentify: v = CCONST ('N', 'v', 'E', 'f'); break;
case effGetChunk: v = getChunk ((void**)ptr, index ? true : false); break;
case effSetChunk: v = setChunk (ptr, value, index ? true : false); break;
}
return v;
}
//-------------------------------------------------------------------------------------------------------
long AudioEffect::getMasterVersion ()
{
long version = 1;
if (audioMaster)
{
version = audioMaster (&cEffect, audioMasterVersion, 0, 0, 0, 0);
if (!version) // old
version = 1;
}
return version;
}
//-------------------------------------------------------------------------------------------------------
long AudioEffect::getCurrentUniqueId ()
{
long id = 0;
if (audioMaster)
id = audioMaster (&cEffect, audioMasterCurrentId, 0, 0, 0, 0);
return id;
}
//-------------------------------------------------------------------------------------------------------
void AudioEffect::masterIdle ()
{
if (audioMaster)
audioMaster (&cEffect, audioMasterIdle, 0, 0, 0, 0);
}
//-------------------------------------------------------------------------------------------------------
bool AudioEffect::isInputConnected (long input)
{
long ret = 0;
if (audioMaster)
ret = audioMaster (&cEffect, audioMasterPinConnected, input, 0, 0, 0);
return ret ? false : true; // return value is 0 for true
}
//-------------------------------------------------------------------------------------------------------
bool AudioEffect::isOutputConnected (long output)
{
long ret = 0;
if (audioMaster)
ret = audioMaster (&cEffect, audioMasterPinConnected, output, 1, 0, 0);
return ret ? false : true; // return value is 0 for true
}
//-------------------------------------------------------------------------------------------------------
void AudioEffect::setParameterAutomated (long index, float value)
{
setParameter (index, value);
if (audioMaster)
audioMaster (&cEffect, audioMasterAutomate, index, 0, 0, value); // value is in opt
}
//-------------------------------------------------------------------------------------------------------
// Flags
//-------------------------------------------------------------------------------------------------------
void AudioEffect::hasVu (bool state)
{
if (state)
cEffect.flags |= effFlagsHasVu;
else
cEffect.flags &= ~effFlagsHasVu;
}
//-------------------------------------------------------------------------------------------------------
void AudioEffect::hasClip (bool state)
{
if (state)
cEffect.flags |= effFlagsHasClip;
else
cEffect.flags &= ~effFlagsHasClip;
}
//-------------------------------------------------------------------------------------------------------
void AudioEffect::canMono (bool state)
{
if (state)
cEffect.flags |= effFlagsCanMono;
else
cEffect.flags &= ~effFlagsCanMono;
}
//-------------------------------------------------------------------------------------------------------
void AudioEffect::canProcessReplacing (bool state)
{
if (state)
cEffect.flags |= effFlagsCanReplacing;
else
cEffect.flags &= ~effFlagsCanReplacing;
}
//-------------------------------------------------------------------------------------------------------
void AudioEffect::programsAreChunks (bool state)
{
if (state)
cEffect.flags |= effFlagsProgramChunks;
else
cEffect.flags &= ~effFlagsProgramChunks;
}
//-------------------------------------------------------------------------------------------------------
void AudioEffect::setRealtimeQualities (long qualities)
{
cEffect.realQualities = qualities;
}
//-------------------------------------------------------------------------------------------------------
void AudioEffect::setOfflineQualities (long qualities)
{
cEffect.offQualities = qualities;
}
//-------------------------------------------------------------------------------------------------------
void AudioEffect::setInitialDelay (long delay)
{
cEffect.initialDelay = delay;
}
//-------------------------------------------------------------------------------------------------------
// Strings Conversion
//-------------------------------------------------------------------------------------------------------
void AudioEffect::dB2string (float value, char *text)
{
if (value <= 0)
#if MAC
strcpy (text, "-°");
#else
strcpy (text, "-oo");
#endif
else
float2string ((float)(20. * log10 (value)), text);
}
//-------------------------------------------------------------------------------------------------------
void AudioEffect::Hz2string (float samples, char *text)
{
float sampleRate = getSampleRate ();
if (!samples)
float2string (0, text);
else
float2string (sampleRate / samples, text);
}
//-------------------------------------------------------------------------------------------------------
void AudioEffect::ms2string (float samples, char *text)
{
float2string ((float)(samples * 1000. / getSampleRate ()), text);
}
//-------------------------------------------------------------------------------------------------------
void AudioEffect::float2string (float value, char *text)
{
long c = 0, neg = 0;
char string[32];
char *s;
double v, integ, i10, mantissa, m10, ten = 10.;
v = (double)value;
if (v < 0)
{
neg = 1;
value = -value;
v = -v;
c++;
if (v > 9999999.)
{
strcpy (string, "Huge!");
return;
}
}
else if( v > 99999999.)
{
strcpy (string, "Huge!");
return;
}
s = string + 31;
*s-- = 0;
*s-- = '.';
c++;
integ = floor (v);
i10 = fmod (integ, ten);
*s-- = (char)((long)i10 + '0');
integ /= ten;
c++;
while (integ >= 1. && c < 8)
{
i10 = fmod (integ, ten);
*s-- = (char)((long)i10 + '0');
integ /= ten;
c++;
}
if (neg)
*s-- = '-';
strcpy (text, s + 1);
if (c >= 8)
return;
s = string + 31;
*s-- = 0;
mantissa = fmod (v, 1.);
mantissa *= pow (ten, (double)(8 - c));
while (c < 8)
{
if (mantissa <= 0)
*s-- = '0';
else
{
m10 = fmod (mantissa, ten);
*s-- = (char)((long)m10 + '0');
mantissa /= 10.;
}
c++;
}
strcat (text, s + 1);
}
//-------------------------------------------------------------------------------------------------------
void AudioEffect::long2string (long value, char *text)
{
char string[32];
if (value >= 100000000)
{
strcpy (text, "Huge!");
return;
}
sprintf (string, "%7d", (int)value);
string[8] = 0;
strcpy (text, (char *)string);
}
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------