|
| 1 | +/** Helpful defines, functions, and other utilities for use in/with daisysp modules. |
| 2 | +*/ |
| 3 | +#if !defined(DSY_CUSTOM_DSP_H) && defined __cplusplus |
| 4 | +#define DSY_CUSTOM_DSP_H |
| 5 | +#include <math.h> |
| 6 | + |
| 7 | +#include "basicmaths.h" |
| 8 | +#undef min |
| 9 | +#undef max |
| 10 | +#undef abs |
| 11 | +#undef sin |
| 12 | +#undef cos |
| 13 | +#undef exp |
| 14 | +#undef sqrt |
| 15 | +#undef pow |
| 16 | +#undef log |
| 17 | +#undef log10 |
| 18 | +#undef rand |
| 19 | + |
| 20 | +/** PIs |
| 21 | +*/ |
| 22 | +#define PI_F 3.1415927410125732421875f |
| 23 | +#define TWOPI_F (2.0f * PI_F) |
| 24 | +#define HALFPI_F (PI_F * 0.5f) |
| 25 | +#define DSY_MIN(in, mn) (in < mn ? in : mn) |
| 26 | +#define DSY_MAX(in, mx) (in > mx ? in : mx) |
| 27 | +#define DSY_CLAMP(in, mn, mx) (DSY_MIN(DSY_MAX(in, mn), mx)) |
| 28 | + |
| 29 | +namespace daisysp |
| 30 | +{ |
| 31 | +//Avoids division for random floats. e.g. rand() * kRandFrac |
| 32 | +static constexpr float kRandFrac = 1.f / (float)RAND_MAX; |
| 33 | + |
| 34 | +//Convert from semitones to other units. e.g. 2 ^ (kOneTwelfth * x) |
| 35 | +static constexpr float kOneTwelfth = 1.f / 12.f; |
| 36 | + |
| 37 | +/** efficient floating point min/max |
| 38 | +c/o stephen mccaul |
| 39 | +*/ |
| 40 | +inline float fmax(float a, float b) |
| 41 | +{ |
| 42 | + float r; |
| 43 | +#ifdef ARM_MATH_CM7 |
| 44 | + asm("vmaxnm.f32 %[d], %[n], %[m]" : [d] "=t"(r) : [n] "t"(a), [m] "t"(b) :); |
| 45 | +#else |
| 46 | + r = (a > b) ? a : b; |
| 47 | +#endif // ARM_MATH_CM7 |
| 48 | + return r; |
| 49 | +} |
| 50 | + |
| 51 | +inline float fmin(float a, float b) |
| 52 | +{ |
| 53 | + float r; |
| 54 | +#ifdef ARM_MATH_CM7 |
| 55 | + asm("vminnm.f32 %[d], %[n], %[m]" : [d] "=t"(r) : [n] "t"(a), [m] "t"(b) :); |
| 56 | +#else |
| 57 | + r = (a < b) ? a : b; |
| 58 | +#endif // ARM_MATH_CM7 |
| 59 | + return r; |
| 60 | +} |
| 61 | + |
| 62 | +/** quick fp clamp |
| 63 | +*/ |
| 64 | +inline float fclamp(float in, float min, float max) |
| 65 | +{ |
| 66 | + return fmin(fmax(in, min), max); |
| 67 | +} |
| 68 | + |
| 69 | +/** From Musicdsp.org "Fast power and root estimates for 32bit floats) |
| 70 | +Original code by Stefan Stenzel |
| 71 | +These are approximations |
| 72 | +*/ |
| 73 | +#if 0 |
| 74 | +inline float fastpower(float f, int n) |
| 75 | +{ |
| 76 | + long *lp, l; |
| 77 | + lp = (long *)(&f); |
| 78 | + l = *lp; |
| 79 | + l -= 0x3F800000; |
| 80 | + l <<= (n - 1); |
| 81 | + l += 0x3F800000; |
| 82 | + *lp = l; |
| 83 | + return f; |
| 84 | +} |
| 85 | +#else |
| 86 | +#define fastpower(f, n) (fast_powf(f, n)) |
| 87 | +#endif |
| 88 | + |
| 89 | +inline float fastroot(float f, int n) |
| 90 | +{ |
| 91 | + long *lp, l; |
| 92 | + lp = (long *)(&f); |
| 93 | + l = *lp; |
| 94 | + l -= 0x3F800000; |
| 95 | + l >>= (n = 1); |
| 96 | + l += 0x3F800000; |
| 97 | + *lp = l; |
| 98 | + return f; |
| 99 | +} |
| 100 | + |
| 101 | +/** From http://openaudio.blogspot.com/2017/02/faster-log10-and-pow.html |
| 102 | +No approximation, pow10f(x) gives a 90% speed increase over powf(10.f, x) |
| 103 | +*/ |
| 104 | +#if 0 |
| 105 | +inline float pow10f(float f) |
| 106 | +{ |
| 107 | + return expf(2.302585092994046f * f); |
| 108 | +} |
| 109 | +#else |
| 110 | +#define pow10f(x) (fast_powf(10.f, x)) |
| 111 | +#endif |
| 112 | + |
| 113 | +/* Original code for fastlog2f by Dr. Paul Beckmann from the ARM community forum, adapted from the CMSIS-DSP library |
| 114 | +About 25% performance increase over std::log10f |
| 115 | +*/ |
| 116 | +#if 0 |
| 117 | +inline float fastlog2f(float f) |
| 118 | +{ |
| 119 | + float frac; |
| 120 | + int exp; |
| 121 | + frac = frexpf(fabsf(f), &exp); |
| 122 | + f = 1.23149591368684f; |
| 123 | + f *= frac; |
| 124 | + f += -4.11852516267426f; |
| 125 | + f *= frac; |
| 126 | + f += 6.02197014179219f; |
| 127 | + f *= frac; |
| 128 | + f += -3.13396450166353f; |
| 129 | + f += exp; |
| 130 | + return (f); |
| 131 | +} |
| 132 | +#else |
| 133 | +#define fastlog2f(x) fast_log2f(x) |
| 134 | +#endif |
| 135 | + |
| 136 | +#if 0 |
| 137 | +inline float fastlog10f(float f) |
| 138 | +{ |
| 139 | + return fastlog2f(f) * 0.3010299956639812f; |
| 140 | +} |
| 141 | +#else |
| 142 | +#define fastlog10f(x) fast_log10f(x) |
| 143 | +#endif |
| 144 | + |
| 145 | +/** Midi to frequency helper |
| 146 | +*/ |
| 147 | +inline float mtof(float m) |
| 148 | +{ |
| 149 | + return powf(2, (m - 69.0f) / 12.0f) * 440.0f; |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | +/** one pole lpf |
| 154 | +out is passed by reference, and must be retained between |
| 155 | +calls to properly filter the signal |
| 156 | +coeff can be calculated: |
| 157 | +coeff = 1.0 / (time * sample_rate) ; where time is in seconds |
| 158 | +*/ |
| 159 | +inline void fonepole(float &out, float in, float coeff) |
| 160 | +{ |
| 161 | + out += coeff * (in - out); |
| 162 | +} |
| 163 | + |
| 164 | +/** Simple 3-point median filter |
| 165 | +c/o stephen mccaul |
| 166 | +*/ |
| 167 | +template <typename T> |
| 168 | +T median(T a, T b, T c) |
| 169 | +{ |
| 170 | + return (b < a) ? (b < c) ? (c < a) ? c : a : b |
| 171 | + : (a < c) ? (c < b) ? c : b : a; |
| 172 | +} |
| 173 | + |
| 174 | +/** Ported from pichenettes/eurorack/plaits/dsp/oscillator/oscillator.h |
| 175 | +*/ |
| 176 | +inline float ThisBlepSample(float t) |
| 177 | +{ |
| 178 | + return 0.5f * t * t; |
| 179 | +} |
| 180 | + |
| 181 | +/** Ported from pichenettes/eurorack/plaits/dsp/oscillator/oscillator.h |
| 182 | +*/ |
| 183 | +inline float NextBlepSample(float t) |
| 184 | +{ |
| 185 | + t = 1.0f - t; |
| 186 | + return -0.5f * t * t; |
| 187 | +} |
| 188 | + |
| 189 | +/** Ported from pichenettes/eurorack/plaits/dsp/oscillator/oscillator.h |
| 190 | +*/ |
| 191 | +inline float NextIntegratedBlepSample(float t) |
| 192 | +{ |
| 193 | + const float t1 = 0.5f * t; |
| 194 | + const float t2 = t1 * t1; |
| 195 | + const float t4 = t2 * t2; |
| 196 | + return 0.1875f - t1 + 1.5f * t2 - t4; |
| 197 | +} |
| 198 | + |
| 199 | +/** Ported from pichenettes/eurorack/plaits/dsp/oscillator/oscillator.h |
| 200 | +*/ |
| 201 | +inline float ThisIntegratedBlepSample(float t) |
| 202 | +{ |
| 203 | + return NextIntegratedBlepSample(1.0f - t); |
| 204 | +} |
| 205 | + |
| 206 | +/** Soft Limiting function ported extracted from pichenettes/stmlib */ |
| 207 | +inline float SoftLimit(float x) |
| 208 | +{ |
| 209 | + return x * (27.f + x * x) / (27.f + 9.f * x * x); |
| 210 | +} |
| 211 | + |
| 212 | +/** Soft Clipping function extracted from pichenettes/stmlib */ |
| 213 | +inline float SoftClip(float x) |
| 214 | +{ |
| 215 | + if(x < -3.0f) |
| 216 | + return -1.0f; |
| 217 | + else if(x > 3.0f) |
| 218 | + return 1.0f; |
| 219 | + else |
| 220 | + return SoftLimit(x); |
| 221 | +} |
| 222 | + |
| 223 | +/** Quick check for Invalid float values (NaN, Inf, out of range) |
| 224 | + ** \param x value passed by reference, replaced by y if invalid. |
| 225 | + ** \param y value to replace x if invalidity is found. |
| 226 | + ** |
| 227 | + ** When DEBUG is true in the build, this will halt |
| 228 | + ** execution for tracing the reason for the invalidity. */ |
| 229 | +inline void TestFloat(float &x, float y = 0.f) |
| 230 | +{ |
| 231 | + if(!isnormal(x) && x != 0) |
| 232 | + { |
| 233 | +#ifdef DEBUG |
| 234 | + asm("bkpt 255"); |
| 235 | +#else |
| 236 | + x = y; |
| 237 | +#endif |
| 238 | + } |
| 239 | +} |
| 240 | + |
| 241 | +/** Based on soft saturate from: |
| 242 | +[musicdsp.org](musicdsp.org/en/latest/Effects/42-soft-saturation.html) |
| 243 | +Bram de Jong (2002-01-17) |
| 244 | +This still needs to be tested/fixed. Definitely does some weird stuff |
| 245 | +described as: |
| 246 | +x < a: |
| 247 | + f(x) = x |
| 248 | +x > a: |
| 249 | + f(x) = a + (x-a)/(1+((x-a)/(1-a))^2) |
| 250 | +x > 1: |
| 251 | + f(x) = (a + 1)/2 |
| 252 | +*/ |
| 253 | +inline float soft_saturate(float in, float thresh) |
| 254 | +{ |
| 255 | + bool flip; |
| 256 | + float val, out; |
| 257 | + //val = fabsf(in); |
| 258 | + flip = val < 0.0f; |
| 259 | + val = flip ? -in : in; |
| 260 | + if(val < thresh) |
| 261 | + { |
| 262 | + out = in; |
| 263 | + } |
| 264 | + else if(val > 1.0f) |
| 265 | + { |
| 266 | + out = (thresh + 1.0f) / 2.0f; |
| 267 | + if(flip) |
| 268 | + out *= -1.0f; |
| 269 | + } |
| 270 | + else if(val > thresh) |
| 271 | + { |
| 272 | + float temp; |
| 273 | + temp = (val - thresh) / (1 - thresh); |
| 274 | + out = thresh + (val - thresh) / (1.0f + (temp * temp)); |
| 275 | + if(flip) |
| 276 | + out *= -1.0f; |
| 277 | + } |
| 278 | + return out; |
| 279 | + // return val < thresh |
| 280 | + // ? val |
| 281 | + // : val > 1.0f |
| 282 | + // ? (thresh + 1.0f) / 2.0f |
| 283 | + // : thresh |
| 284 | + // + (val - thresh) |
| 285 | + // / (1.0f |
| 286 | + // + (((val - thresh) / (1.0f - thresh)) |
| 287 | + // * ((val - thresh) / (1.0f - thresh)))); |
| 288 | +} |
| 289 | +} // namespace daisysp |
| 290 | +#endif |
0 commit comments