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
/** Helpful defines, functions, and other utilities for use in/with daisysp modules.
*/
#if !defined(DSY_CUSTOM_DSP_H) && defined __cplusplus
#define DSY_CUSTOM_DSP_H
#include <math.h>
#include "basicmaths.h"
#undef min
#undef max
#undef abs
#undef exp
#undef sqrt
#undef pow
#undef log
#undef rand
#undef clamp
/** 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))
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_MATH_CM7
asm("vmaxnm.f32 %[d], %[n], %[m]" : [d] "=t"(r) : [n] "t"(a), [m] "t"(b) :);
#else
r = (a > b) ? a : b;
#endif // ARM_MATH_CM7
return r;
}
inline float fmin(float a, float b)
{
float r;
#ifdef ARM_MATH_CM7
asm("vminnm.f32 %[d], %[n], %[m]" : [d] "=t"(r) : [n] "t"(a), [m] "t"(b) :);
#else
r = (a < b) ? a : b;
#endif // ARM_MATH_CM7
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
*/
#if 0
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;
}
#else
#define fastpower(f, n) (fast_powf(f, n))
#endif
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)
*/
#if 0
inline float pow10f(float f)
{
return expf(2.302585092994046f * f);
}
#else
#define pow10f(x) (fast_powf(10.f, x))
#endif
/* 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
*/
#if 0
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);
}
#else
#define fastlog2f(x) fast_log2f(x)
#endif
#if 0
inline float fastlog10f(float f)
{
return fastlog2f(f) * 0.3010299956639812f;
}
#else
#define fastlog10f(x) fast_log10f(x)
#endif
/** 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);
}
/** 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(!isnormal(x) && x != 0)
{
#ifdef 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);
flip = val < 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))));
}
} // namespace daisysp
#endif