3 #if !defined(DSY_CUSTOM_DSP_H) && defined __cplusplus
4 #define DSY_CUSTOM_DSP_H
20 #define PI_F 3.1415927410125732421875f
21 #define TWOPI_F (2.0f * PI_F)
22 #define HALFPI_F (PI_F * 0.5f)
23 #define DSY_MIN(in, mn) (in < mn ? in : mn)
24 #define DSY_MAX(in, mx) (in > mx ? in : mx)
25 #define DSY_CLAMP(in, mn, mx) (DSY_MIN(DSY_MAX(in, mn), mx))
30 static constexpr
float kRandFrac = 1.f / (float)RAND_MAX;
33 static constexpr
float kOneTwelfth = 1.f / 12.f;
38 inline float fmax(
float a,
float b)
42 asm(
"vmaxnm.f32 %[d], %[n], %[m]" : [d]
"=t"(r) : [n]
"t"(a), [m]
"t"(b) :);
49 inline float fmin(
float a,
float b)
53 asm(
"vminnm.f32 %[d], %[n], %[m]" : [d]
"=t"(r) : [n]
"t"(a), [m]
"t"(b) :);
62 inline float fclamp(
float in,
float min,
float max)
64 return fmin(fmax(in,
min),
max);
72 inline float fastpower(
float f,
int n)
84 #define fastpower(f, n) (fast_powf(f, n))
87 inline float fastroot(
float f,
int n)
103 inline float pow10f(
float f)
105 return expf(2.302585092994046f * f);
108 #define pow10f(x) (fast_powf(10.f, x))
115 inline float fastlog2f(
float f)
119 frac = frexpf(fabsf(f), &exp);
120 f = 1.23149591368684f;
122 f += -4.11852516267426f;
124 f += 6.02197014179219f;
126 f += -3.13396450166353f;
131 #define fastlog2f(x) fast_log2f(x)
135 inline float fastlog10f(
float f)
137 return fastlog2f(f) * 0.3010299956639812f;
140 #define fastlog10f(x) fast_log10f(x)
145 inline float mtof(
float m)
147 return powf(2, (m - 69.0f) / 12.0f) * 440.0f;
157 inline void fonepole(
float &out,
float in,
float coeff)
159 out += coeff * (in - out);
165 template <
typename T>
166 T median(T a, T b, T c)
168 return (b < a) ? (b < c) ? (c < a) ? c : a : b
169 : (a < c) ? (c < b) ? c : b : a;
174 inline float ThisBlepSample(
float t)
181 inline float NextBlepSample(
float t)
184 return -0.5f * t * t;
189 inline float NextIntegratedBlepSample(
float t)
191 const float t1 = 0.5f * t;
192 const float t2 = t1 * t1;
193 const float t4 = t2 * t2;
194 return 0.1875f - t1 + 1.5f * t2 - t4;
199 inline float ThisIntegratedBlepSample(
float t)
201 return NextIntegratedBlepSample(1.0f - t);
205 inline float SoftLimit(
float x)
207 return x * (27.f + x * x) / (27.f + 9.f * x * x);
211 inline float SoftClip(
float x)
227 inline void TestFloat(
float &x,
float y = 0.f)
229 if(!isnormal(x) && x != 0)
251 inline float soft_saturate(
float in,
float thresh)
257 val = flip ? -in : in;
264 out = (thresh + 1.0f) / 2.0f;
268 else if(val > thresh)
271 temp = (val - thresh) / (1 - thresh);
272 out = thresh + (val - thresh) / (1.0f + (temp * temp));