Showing the changes to LEAF/Src_cpp/leaf-math.cpp (1 of 147 files). Show all
Unified · Split
| @@ -0,0 +1,149 @@ | |||
| 1 | /* | ||
| 2 | ============================================================================== | ||
| 3 | |||
| 4 | LEAFMath.c | ||
| 5 | Created: 22 Jan 2017 7:02:56pm | ||
| 6 | Author: Michael R Mulshine | ||
| 7 | |||
| 8 | ============================================================================== | ||
| 9 | */ | ||
| 10 | |||
| 11 | #if _WIN32 || _WIN64 | ||
| 12 | |||
| 13 | #include "..\Inc\leaf-math.h" | ||
| 14 | #include "..\Inc\leaf-wavetables.h" | ||
| 15 | |||
| 16 | #else | ||
| 17 | |||
| 18 | #include "../Inc/leaf-math.h" | ||
| 19 | #include "../Inc/leaf-wavetables.h" | ||
| 20 | |||
| 21 | #endif | ||
| 22 | |||
| 23 | // The C-embedded Audio Library. | ||
| 24 | #define TWO_TO_16 65536.f | ||
| 25 | |||
| 26 | #define EXPONENTIAL_TABLE_SIZE 65536 | ||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | // dope af | ||
| 33 | float LEAF_chebyshevT(float in, int n){ | ||
| 34 | if (n == 0) return 1; | ||
| 35 | else if (n == 1) return in; | ||
| 36 | else return 2.0f * in * LEAF_chebyshevT(in, n-1) - LEAF_chebyshevT(in, n-2); | ||
| 37 | } | ||
| 38 | |||
| 39 | #if !(_WIN32 || _WIN64) | ||
| 40 | float LEAF_CompoundChebyshevT(float in, int n, float* amps){ | ||
| 41 | float T[n+1]; | ||
| 42 | T[0] = 1.0f; | ||
| 43 | T[1] = in; | ||
| 44 | for (int i = 2; i <= n; ++i) | ||
| 45 | T[i] = 2*in*T[i-1] - T[i-2]; | ||
| 46 | float out = 0; | ||
| 47 | float amp = 0; | ||
| 48 | for (int i = 0; i < n; ++i){ | ||
| 49 | out += amps[i]*T[i+1]; | ||
| 50 | amp += amps[i]; | ||
| 51 | } | ||
| 52 | return out / amp ; | ||
| 53 | } | ||
| 54 | #endif | ||
| 55 | |||
| 56 | float LEAF_frequencyToMidi(float f) | ||
| 57 | { | ||
| 58 | return (69.0f + 12.0f * log2(f * INV_440)); | ||
| 59 | } | ||
| 60 | |||
| 61 | // Erbe shaper | ||
| 62 | float LEAF_shaper(float input, float m_drive) | ||
| 63 | { | ||
| 64 | float fx = input * 2.0f; // prescale | ||
| 65 | float w, c, xc, xc2, xc4; | ||
| 66 | |||
| 67 | xc = LEAF_clip(-SQRT8, fx, SQRT8); | ||
| 68 | xc2 = xc*xc; | ||
| 69 | c = 0.5f*fx*(3.0f - (xc2)); | ||
| 70 | xc4 = xc2 * xc2; | ||
| 71 | w = (1.0f - xc2*0.25f + xc4*0.015625f) * WSCALE; | ||
| 72 | float shaperOut = w*(c+ 0.05f*xc2)*(m_drive + 0.75f); | ||
| 73 | shaperOut *= 0.5f; // post_scale | ||
| 74 | return shaperOut; | ||
| 75 | } | ||
| 76 | |||
| 77 | float LEAF_reedTable(float input, float offset, float slope) | ||
| 78 | { | ||
| 79 | float output = offset + (slope * input); | ||
| 80 | if ( output > 1.0f) output = 1.0f; | ||
| 81 | if ( output < -1.0f) output = -1.0f; | ||
| 82 | return output; | ||
| 83 | } | ||
| 84 | |||
| 85 | float LEAF_softClip(float val, float thresh) | ||
| 86 | { | ||
| 87 | float x; | ||
| 88 | |||
| 89 | if(val > thresh) | ||
| 90 | { | ||
| 91 | x = thresh / val; | ||
| 92 | return (1.0f - x) * (1.0f - thresh) + thresh; | ||
| 93 | } | ||
| 94 | else if(val < -thresh) | ||
| 95 | { | ||
| 96 | x = -thresh / val; | ||
| 97 | return -((1.0f - x) * (1.0f - thresh) + thresh); | ||
| 98 | } | ||
| 99 | else | ||
| 100 | { | ||
| 101 | return val; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | float LEAF_clip(float min, float val, float max) | ||
| 106 | { | ||
| 107 | |||
| 108 | if (val < min) { | ||
| 109 | return min; | ||
| 110 | } else if (val > max) { | ||
| 111 | return max; | ||
| 112 | } else { | ||
| 113 | return val; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | oBool LEAF_isPrime(uint64_t number ) | ||
| 118 | { | ||
| 119 | if ( number == 2 ) return OTRUE; | ||
| 120 | if ( number & 1 ) { | ||
| 121 | for ( int i=3; i<(int)sqrt((double)number)+1; i+=2 ) | ||
| 122 | if ( (number % i) == 0 ) return OFALSE; | ||
| 123 | return OTRUE; // prime | ||
| 124 | } | ||
| 125 | else return OFALSE; // even | ||
| 126 | } | ||
| 127 | |||
| 128 | // Adapted from MusicDSP: http://www.musicdsp.org/showone.php?id=238 | ||
| 129 | float LEAF_tanh(float x) | ||
| 130 | { | ||
| 131 | |||
| 132 | if( x < -3 ) | ||
| 133 | return -1; | ||
| 134 | else if( x > 3 ) | ||
| 135 | return 1; | ||
| 136 | else | ||
| 137 | return x * ( 27 + x * x ) / ( 27 + 9 * x * x ); | ||
| 138 | } | ||
| 139 | |||
| 140 | //----------------------------------------------------------------------------- | ||
| 141 | // name: mtof() | ||
| 142 | // desc: midi to freq, from PD source | ||
| 143 | //----------------------------------------------------------------------------- | ||
| 144 | float LEAF_midiToFrequency(float f) | ||
| 145 | { | ||
| 146 | if( f <= -1500.0f ) return (0); | ||
| 147 | else if( f > 1499.0f ) return (LEAF_midiToFrequency(1499.0f)); | ||
| 148 | else return ( powf(2.0f, (f - 69.0f) / 12.0f) * 440.0f ); | ||
| 149 | } | ||