-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAudioPluginUtil.h
495 lines (427 loc) · 13 KB
/
AudioPluginUtil.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#pragma once
#include "AudioPluginInterface.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#if PLATFORM_WIN
# include <windows.h>
#else
# include <pthread.h>
# define strcpy_s strcpy
#endif
typedef int (*InternalEffectDefinitionRegistrationCallback)(UnityAudioEffectDefinition& desc);
const float kMaxSampleRate = 22050.0f;
const float kPI = 3.141592653589793f;
const double kPI_double = 3.141592653589793;
inline float FastClip(float x, float minval, float maxval) { return (fabsf(x - minval) - fabsf(x - maxval) + (minval + maxval)) * 0.5f; }
inline float FastMin(float a, float b) { return (a + b - fabsf(a - b)) * 0.5f; }
inline float FastMax(float a, float b) { return (a + b + fabsf(a - b)) * 0.5f; }
inline int FastFloor(float x) { return (int)floorf(x); } // TODO: Optimize
char* strnew(const char* src);
char* tmpstr(int index, const char* fmtstr, ...);
template<typename T>
class UnityComplexNumberT
{
public:
// No constructor because we want to be able to define this inside anonymous unions (this is also why we don't use std::complex<T> here)
inline void Set(T _re, T _im)
{
re = _re;
im = _im;
}
inline void Set(const UnityComplexNumberT<T>& c)
{
re = c.re;
im = c.im;
}
template<typename T1, typename T2, typename T3>
inline static void Scale(const UnityComplexNumberT<T1>& a, T2 b, UnityComplexNumberT<T3>& result)
{
result.re = a.re * b;
result.im = a.im * b;
}
template<typename T1, typename T2, typename T3>
inline static void Mul(const UnityComplexNumberT<T1>& a, const UnityComplexNumberT<T2>& b, UnityComplexNumberT<T3>& result)
{
// Store temporarily in case a or b reference the same memory as result
T3 t = a.re * b.im + a.im * b.re;
result.re = a.re * b.re - a.im * b.im;
result.im = t;
}
template<typename T1, typename T2, typename T3>
inline static void Add(const UnityComplexNumberT<T1>& a, const UnityComplexNumberT<T2>& b, UnityComplexNumberT<T3>& result)
{
result.re = a.re + b.re;
result.im = a.im + b.im;
}
template<typename T1, typename T2, typename T3>
inline static void Sub(const UnityComplexNumberT<T1>& a, const UnityComplexNumberT<T2>& b, UnityComplexNumberT<T3>& result)
{
result.re = a.re - b.re;
result.im = a.im - b.im;
}
template<typename T1, typename T2, typename T3>
inline static void MulAdd(const UnityComplexNumberT<T1>& a, const UnityComplexNumberT<T2>& b, const UnityComplexNumberT<T2>& c, UnityComplexNumberT<T3>& result)
{
// Store temporarily in case a or b reference the same memory as result
T3 t = a.re * b.im + a.im * b.re;
result.re = c.re + a.re * b.re - a.im * b.im;
result.im = c.im + t;
}
inline T Magnitude() const
{
return (T)sqrt(re * re + im * im);
}
inline T Magnitude2() const
{
return re * re + im * im;
}
public:
T re, im;
};
typedef UnityComplexNumberT<float> UnityComplexNumber;
class FFT
{
public:
static void Forward(UnityComplexNumber* data, int numsamples, bool highprecision);
static void Backward(UnityComplexNumber* data, int numsamples, bool highprecision);
};
class FFTAnalyzer : public FFT
{
public:
void Cleanup(); // Assumes zero-initialization
void AnalyzeInput(float* data, int numchannels, int numsamples, float specAlpha);
void AnalyzeOutput(float* data, int numchannels, int numsamples, float specAlpha);
void CheckInitialized();
bool CanBeRead() const;
void ReadBuffer(float* buffer, int numsamples, bool readInputBuffer);
public:
float* window;
float* ibuffer;
float* obuffer;
UnityComplexNumber* cspec;
float* ispec1;
float* ispec2;
float* ospec1;
float* ospec2;
int spectrumSize;
int numSpectraReady;
};
class HistoryBuffer
{
public:
HistoryBuffer();
~HistoryBuffer();
public:
void Init(int _length);
void ReadBuffer(float* buffer, int numsamplesTarget, int numsamplesSource, float offset);
public:
inline void Feed(float sample)
{
// Don't try to optimize this with ++
// The writeindex veriable may be read at the same time, so we don't want intermediate values indexing out of the array.
int w = writeindex + 1;
if (w == length)
w = 0;
data[w] = sample;
writeindex = w;
}
inline void Feed(float* buf, int numsamples, int stride)
{
int w = writeindex;
for (int n = 0; n < numsamples; n++)
{
if (++w == length)
w = 0;
data[w] = buf[n * stride];
}
writeindex = w;
}
public:
int length;
int writeindex;
float* data;
};
template<const int _LENGTH, typename T = float>
class RingBuffer
{
public:
enum { LENGTH = _LENGTH };
volatile int readpos;
volatile int writepos;
T buffer[LENGTH];
inline bool Read(T& val)
{
int r = readpos;
if (r == writepos)
return false;
r = (r == LENGTH - 1) ? 0 : (r + 1);
val = buffer[r];
readpos = r;
return true;
}
inline void Skip(int num)
{
int r = readpos + num;
if (r >= LENGTH)
r -= LENGTH;
readpos = r;
}
inline void SyncWritePos()
{
writepos = readpos;
}
inline bool Feed(const T& input)
{
int w = (writepos == LENGTH - 1) ? 0 : (writepos + 1);
buffer[w] = input;
writepos = w;
return true;
}
inline int GetNumBuffered() const
{
int b = writepos - readpos;
if (b < 0)
b += LENGTH;
return b;
}
inline void Clear()
{
writepos = 0;
readpos = 0;
}
};
class BiquadFilter
{
public:
inline void SetupPeaking(float cutoff, float samplerate, float gain, float Q);
inline void SetupLowShelf(float cutoff, float samplerate, float gain, float Q);
inline void SetupHighShelf(float cutoff, float samplerate, float gain, float Q);
inline void SetupLowpass(float cutoff, float samplerate, float Q);
inline void SetupHighpass(float cutoff, float samplerate, float Q);
public:
inline float Process(float input)
{
float iir = input - a1 * z1 - a2 * z2;
float fir = b0 * iir + b1 * z1 + b2 * z2;
z2 = z1;
z1 = iir;
return fir;
}
inline void StoreCoeffs(float*& data)
{
*data++ = b2;
*data++ = b1;
*data++ = b0;
*data++ = a2;
*data++ = a1;
}
protected:
float a1, a2, b0, b1, b2;
float z1, z2;
};
// The filter coefficient formulae below are taken from Robert Bristow-Johnsons excellent EQ biquad filter cookbook:
// http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
void BiquadFilter::SetupPeaking(float cutoff, float samplerate, float gain, float Q)
{
float w0 = 2.0f * kPI * cutoff / samplerate, A = powf(10.0f, gain * 0.025f), alpha = sinf(w0) / (2.0f * Q), a0;
b0 = 1.0f + alpha * A;
b1 = -2.0f * cosf(w0);
b2 = 1.0f - alpha * A;
a0 = 1.0f + alpha / A;
a1 = -2.0f * cosf(w0);
a2 = 1.0f - alpha / A;
float inv_a0 = 1.0f / a0; a1 *= inv_a0; a2 *= inv_a0; b0 *= inv_a0; b1 *= inv_a0; b2 *= inv_a0;
}
void BiquadFilter::SetupLowShelf(float cutoff, float samplerate, float gain, float Q)
{
float w0 = 2.0f * kPI * cutoff / samplerate, A = powf(10.0f, gain * 0.025f), alpha = sinf(w0) / (2.0f * Q), a0;
b0 = A * ((A + 1.0f) - (A - 1.0f) * cosf(w0) + 2.0f * sqrtf(A) * alpha);
b1 = 2.0f * A * ((A - 1.0f) - (A + 1.0f) * cosf(w0));
b2 = A * ((A + 1.0f) - (A - 1.0f) * cosf(w0) - 2.0f * sqrtf(A) * alpha);
a0 = (A + 1.0f) + (A - 1.0f) * cosf(w0) + 2.0f * sqrtf(A) * alpha;
a1 = -2.0f * ((A - 1.0f) + (A + 1.0f) * cosf(w0));
a2 = (A + 1.0f) + (A - 1.0f) * cosf(w0) - 2.0f * sqrtf(A) * alpha;
float inv_a0 = 1.0f / a0; a1 *= inv_a0; a2 *= inv_a0; b0 *= inv_a0; b1 *= inv_a0; b2 *= inv_a0;
}
void BiquadFilter::SetupHighShelf(float cutoff, float samplerate, float gain, float Q)
{
float w0 = 2.0f * kPI * cutoff / samplerate, A = powf(10.0f, gain * 0.025f), alpha = sinf(w0) / (2.0f * Q), a0;
b0 = A * ((A + 1.0f) + (A - 1.0f) * cosf(w0) + 2.0f * sqrtf(A) * alpha);
b1 = -2.0f * A * ((A - 1.0f) + (A + 1.0f) * cosf(w0));
b2 = A * ((A + 1.0f) + (A - 1.0f) * cosf(w0) - 2.0f * sqrtf(A) * alpha);
a0 = (A + 1.0f) - (A - 1.0f) * cosf(w0) + 2.0f * sqrtf(A) * alpha;
a1 = 2.0f * ((A - 1.0f) - (A + 1.0f) * cosf(w0));
a2 = (A + 1.0f) - (A - 1.0f) * cosf(w0) - 2.0f * sqrtf(A) * alpha;
float inv_a0 = 1.0f / a0; a1 *= inv_a0; a2 *= inv_a0; b0 *= inv_a0; b1 *= inv_a0; b2 *= inv_a0;
}
void BiquadFilter::SetupLowpass(float cutoff, float samplerate, float Q)
{
float w0 = 2.0f * kPI * cutoff / samplerate, alpha = sinf(w0) / (2.0f * Q), a0;
b0 = (1.0f - cosf(w0)) * 0.5f;
b1 = 1.0f - cosf(w0);
b2 = (1.0f - cosf(w0)) * 0.5f;
a0 = 1.0f + alpha;
a1 = -2.0f * cosf(w0);
a2 = 1.0f - alpha;
float inv_a0 = 1.0f / a0; a1 *= inv_a0; a2 *= inv_a0; b0 *= inv_a0; b1 *= inv_a0; b2 *= inv_a0;
}
void BiquadFilter::SetupHighpass(float cutoff, float samplerate, float Q)
{
float w0 = 2.0f * kPI * cutoff / samplerate, alpha = sinf(w0) / (2.0f * Q), a0;
b0 = (1.0f + cosf(w0)) * 0.5f;
b1 = -(1.0f + cosf(w0));
b2 = (1.0f + cosf(w0)) * 0.5f;
a0 = 1.0f + alpha;
a1 = -2.0f * cosf(w0);
a2 = 1.0f - alpha;
float inv_a0 = 1.0f / a0; a1 *= inv_a0; a2 *= inv_a0; b0 *= inv_a0; b1 *= inv_a0; b2 *= inv_a0;
}
class StateVariableFilter
{
public:
float cutoff;
float bandwidth;
public:
inline float ProcessHPF(float input)
{
input += 1.0e-11f; // Kill denormals
lpf += cutoff * bpf;
float hpf = (input - bpf) * bandwidth - lpf;
bpf += cutoff * hpf;
lpf += cutoff * bpf;
hpf = (input - bpf) * bandwidth - lpf;
bpf += cutoff * hpf;
return hpf;
}
inline float ProcessBPF(float input)
{
ProcessHPF(input);
return bpf;
}
inline float ProcessLPF(float input)
{
ProcessHPF(input);
return lpf;
}
public:
float lpf, bpf;
};
class Random
{
public:
inline void Seed(unsigned long _seed)
{
seed = _seed;
}
inline unsigned int Get()
{
seed = (seed * 1664525 + 1013904223) & 0xFFFFFFFF;
return seed ^ (seed >> 16);
}
inline float GetFloat(float minval, float maxval)
{
return minval + (maxval - minval) * (Get() & 0xFFFFFF) * (const float)(1.0f / (float)0xFFFFFF);
}
protected:
unsigned int seed;
};
class NoiseGenerator
{
public:
void Init()
{
level = 0.0f;
delta = 0.0f;
minval = 0.0f;
maxval = 1.0f;
period = 100.0f;
invperiod = 0.01f;
samplesleft = 0;
}
inline void SetRange(float minval, float maxval)
{
this->minval = minval;
this->maxval = maxval;
}
inline void SetPeriod(float period)
{
SetPeriod(period, 1.0f / period);
}
inline void SetPeriod(float period, float invperiod)
{
period = period;
invperiod = invperiod;
}
inline float Sample(Random& random)
{
if (--samplesleft <= 0)
{
samplesleft = (int)period;
delta = (random.GetFloat(minval, maxval) - level) * invperiod;
}
level += delta;
return level;
}
public:
float level;
float delta;
float minval;
float maxval;
float period;
float invperiod;
int samplesleft;
};
class Mutex
{
public:
Mutex();
~Mutex();
public:
bool TryLock();
void Lock();
void Unlock();
protected:
#if PLATFORM_WIN
CRITICAL_SECTION crit_sec;
#else
pthread_mutex_t mutex;
#endif
};
class MutexScopeLock
{
public:
MutexScopeLock(Mutex& _mutex, bool condition = true) : mutex(condition ? &_mutex : NULL) { if (mutex != NULL) mutex->Lock(); }
~MutexScopeLock() { if (mutex != NULL) mutex->Unlock(); }
protected:
Mutex* mutex;
};
void RegisterParameter(
UnityAudioEffectDefinition& desc,
const char* name,
const char* unit,
float minval,
float maxval,
float defaultval,
float displayscale,
float displayexponent,
int enumvalue,
const char* description = NULL
);
void InitParametersFromDefinitions(
InternalEffectDefinitionRegistrationCallback registereffectdefcallback,
float* params
);
void DeclareEffect(
UnityAudioEffectDefinition& desc,
const char* name,
UnityAudioEffect_CreateCallback createcallback,
UnityAudioEffect_ReleaseCallback releasecallback,
UnityAudioEffect_ProcessCallback processcallback,
UnityAudioEffect_SetFloatParameterCallback setfloatparametercallback,
UnityAudioEffect_GetFloatParameterCallback getfloatparametercallback,
UnityAudioEffect_GetFloatBufferCallback getfloatbuffercallback,
InternalEffectDefinitionRegistrationCallback registereffectdefcallback
);