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