-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdsp.h
336 lines (307 loc) · 7.86 KB
/
dsp.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
/** Helpful defines, functions, and other utilities for use in/with daisysp modules.
*/
#pragma once
#ifndef DSY_CORE_DSP
#define DSY_CORE_DSP
#include <cassert>
#include <cstdint>
#include <random>
#include <cmath>
/** PIs
*/
#define PI_F 3.1415927410125732421875f
#define TWOPI_F (2.0f * PI_F)
#define HALFPI_F (PI_F * 0.5f)
#define DSY_MIN(in, mn) (in < mn ? in : mn)
#define DSY_MAX(in, mx) (in > mx ? in : mx)
#define DSY_CLAMP(in, mn, mx) (DSY_MIN(DSY_MAX(in, mn), mx))
#define DSY_COUNTOF(_arr) (sizeof(_arr) / sizeof(_arr[0]))
namespace daisysp
{
//Avoids division for random floats. e.g. rand() * kRandFrac
static constexpr float kRandFrac = 1.f / (float)RAND_MAX;
//Convert from semitones to other units. e.g. 2 ^ (kOneTwelfth * x)
static constexpr float kOneTwelfth = 1.f / 12.f;
/** efficient floating point min/max
c/o stephen mccaul
*/
inline float fmax(float a, float b)
{
float r;
#ifdef __arm__
asm("vmaxnm.f32 %[d], %[n], %[m]" : [d] "=t"(r) : [n] "t"(a), [m] "t"(b) :);
#else
r = (a > b) ? a : b;
#endif // __arm__
return r;
}
inline float fmin(float a, float b)
{
float r;
#ifdef __arm__
asm("vminnm.f32 %[d], %[n], %[m]" : [d] "=t"(r) : [n] "t"(a), [m] "t"(b) :);
#else
r = (a < b) ? a : b;
#endif // __arm__
return r;
}
/** quick fp clamp
*/
inline float fclamp(float in, float min, float max)
{
return fmin(fmax(in, min), max);
}
/** From Musicdsp.org "Fast power and root estimates for 32bit floats)
Original code by Stefan Stenzel
These are approximations
*/
inline float fastpower(float f, int n)
{
long *lp, l;
lp = (long *)(&f);
l = *lp;
l -= 0x3F800000;
l <<= (n - 1);
l += 0x3F800000;
*lp = l;
return f;
}
inline float fastroot(float f, int n)
{
long *lp, l;
lp = (long *)(&f);
l = *lp;
l -= 0x3F800000;
l >>= (n = 1);
l += 0x3F800000;
*lp = l;
return f;
}
/** From http://openaudio.blogspot.com/2017/02/faster-log10-and-pow.html
No approximation, pow10f(x) gives a 90% speed increase over powf(10.f, x)
*/
inline float pow10f(float f)
{
return expf(2.302585092994046f * f);
}
/* Original code for fastlog2f by Dr. Paul Beckmann from the ARM community forum, adapted from the CMSIS-DSP library
About 25% performance increase over std::log10f
*/
inline float fastlog2f(float f)
{
float frac;
int exp;
frac = frexpf(fabsf(f), &exp);
f = 1.23149591368684f;
f *= frac;
f += -4.11852516267426f;
f *= frac;
f += 6.02197014179219f;
f *= frac;
f += -3.13396450166353f;
f += exp;
return (f);
}
inline float fastlog10f(float f)
{
return fastlog2f(f) * 0.3010299956639812f;
}
/** Midi to frequency helper
*/
inline float mtof(float m)
{
return powf(2, (m - 69.0f) / 12.0f) * 440.0f;
}
/** one pole lpf
out is passed by reference, and must be retained between
calls to properly filter the signal
coeff can be calculated:
coeff = 1.0 / (time * sample_rate) ; where time is in seconds
*/
inline void fonepole(float &out, float in, float coeff)
{
out += coeff * (in - out);
}
/** Curves to use with the fmap function */
enum class Mapping
{
LINEAR,
EXP,
LOG,
};
/** Maps a float between a specified range, using a specified curve.
*
* \param in a value between 0 to 1 that will be mapped to the new range.
* \param min the new minimum value
* \param max the new maxmimum value
* \param curve a Mapping Value to adjust the response curve of the transformation
* defaults to Linear. @see Mapping
*
* When using the log curve min and max, must be greater than zero.
*
* \retval returns the transformed float within the new range.
*/
inline float
fmap(float in, float min, float max, Mapping curve = Mapping::LINEAR)
{
switch(curve)
{
case Mapping::EXP:
return fclamp(min + (in * in) * (max - min), min, max);
case Mapping::LOG:
{
const float a = 1.f / log10f(max / min);
return fclamp(min * powf(10, in / a), min, max);
}
case Mapping::LINEAR:
default: return fclamp(min + in * (max - min), min, max);
}
}
/** Simple 3-point median filter
c/o stephen mccaul
*/
template <typename T>
T median(T a, T b, T c)
{
return (b < a) ? (b < c) ? (c < a) ? c : a : b
: (a < c) ? (c < b) ? c : b : a;
}
/** Ported from pichenettes/eurorack/plaits/dsp/oscillator/oscillator.h
*/
inline float ThisBlepSample(float t)
{
return 0.5f * t * t;
}
/** Ported from pichenettes/eurorack/plaits/dsp/oscillator/oscillator.h
*/
inline float NextBlepSample(float t)
{
t = 1.0f - t;
return -0.5f * t * t;
}
/** Ported from pichenettes/eurorack/plaits/dsp/oscillator/oscillator.h
*/
inline float NextIntegratedBlepSample(float t)
{
const float t1 = 0.5f * t;
const float t2 = t1 * t1;
const float t4 = t2 * t2;
return 0.1875f - t1 + 1.5f * t2 - t4;
}
/** Ported from pichenettes/eurorack/plaits/dsp/oscillator/oscillator.h
*/
inline float ThisIntegratedBlepSample(float t)
{
return NextIntegratedBlepSample(1.0f - t);
}
/** Soft Limiting function ported extracted from pichenettes/stmlib */
inline float SoftLimit(float x)
{
return x * (27.f + x * x) / (27.f + 9.f * x * x);
}
/** Soft Clipping function extracted from pichenettes/stmlib */
inline float SoftClip(float x)
{
if(x < -3.0f)
return -1.0f;
else if(x > 3.0f)
return 1.0f;
else
return SoftLimit(x);
}
/** Quick check for Invalid float values (NaN, Inf, out of range)
** \param x value passed by reference, replaced by y if invalid.
** \param y value to replace x if invalidity is found.
**
** When DEBUG is true in the build, this will halt
** execution for tracing the reason for the invalidity. */
inline void TestFloat(float &x, float y = 0.f)
{
if(!std::isnormal(x) && x != 0)
{
#if defined(__arm__) && defined(DEBUG)
asm("bkpt 255");
#else
x = y;
#endif
}
}
/** Based on soft saturate from:
[musicdsp.org](musicdsp.org/en/latest/Effects/42-soft-saturation.html)
Bram de Jong (2002-01-17)
This still needs to be tested/fixed. Definitely does some weird stuff
described as:
x < a:
f(x) = x
x > a:
f(x) = a + (x-a)/(1+((x-a)/(1-a))^2)
x > 1:
f(x) = (a + 1)/2
*/
inline float soft_saturate(float in, float thresh)
{
bool flip;
float val, out;
//val = fabsf(in);
out = 0.f;
flip = in < 0.0f;
val = flip ? -in : in;
if(val < thresh)
{
out = in;
}
else if(val > 1.0f)
{
out = (thresh + 1.0f) / 2.0f;
if(flip)
out *= -1.0f;
}
else if(val > thresh)
{
float temp;
temp = (val - thresh) / (1 - thresh);
out = thresh + (val - thresh) / (1.0f + (temp * temp));
if(flip)
out *= -1.0f;
}
return out;
// return val < thresh
// ? val
// : val > 1.0f
// ? (thresh + 1.0f) / 2.0f
// : thresh
// + (val - thresh)
// / (1.0f
// + (((val - thresh) / (1.0f - thresh))
// * ((val - thresh) / (1.0f - thresh))));
}
constexpr bool is_power2(uint32_t x)
{
return ((x - 1) & x) == 0;
}
/** Prior to C++14 constexpr functions were required to be a single return statement.
* So this clause guards against that behavior to allow the library, and this function
* to continue to work with C++11.
* The function itself is not currently (12 May 2021) used within the library itself.
*/
#if __cplusplus <= 201103L
inline uint32_t get_next_power2(uint32_t x)
#else
constexpr uint32_t get_next_power2(uint32_t x)
#endif
{
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;
assert(is_power2(x));
return x;
}
} // namespace daisysp
#endif
#ifdef DSY_CUSTOM_DSP
#include "custom_dsp.h"
#endif