leaf-math.h
1 /*==============================================================================
2 
3  leaf-math.h
4  Created: 22 Jan 2017 7:02:56pm
5  Author: Michael R Mulshine
6 
7  ==============================================================================*/
8 
9 #ifndef LEAF_MATH_H_INCLUDED
10 #define LEAF_MATH_H_INCLUDED
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 #include "leaf-global.h"
17 #include "math.h"
18 #include "stdint.h"
19 #include "stdlib.h"
20 #include "limits.h"
21 
22  //==============================================================================
23 
24  //==============================================================================
25 
31  // Allows for bitwise operations on floats
32  union unholy_t { /* a union between a float and an integer */
33  float f;
34  int i;
35  };
36 
37 #define SQRT8 2.82842712475f
38 #define LEAF_SQRT2 1.41421356237f
39 #define WSCALE 1.30612244898f
40 #define PI (3.14159265358979f)
41 #define TWO_PI (6.28318530717958f)
42 #define HALF_PI (1.570796326794897f)
43 
44 #define VSF 1.0e-38f
45 
46 #define MAX_DELAY 8192
47 #define INV_128 0.0078125f
48 
49 #define INV_20 0.05f
50 #define INV_40 0.025f
51 #define INV_80 0.0125f
52 #define INV_160 0.00625f
53 #define INV_320 0.003125f
54 #define INV_640 0.0015625f
55 #define INV_1280 0.00078125f
56 #define INV_2560 0.000390625f
57 #define INV_5120 0.0001953125f
58 #define INV_10240 0.00009765625f
59 #define INV_20480 0.000048828125f
60 
61 
62 #define INV_TWELVE 0.0833333333f
63 #define INV_440 0.0022727273f
64 
65 #define LOG2 0.3010299957f
66 #define INV_LOG2 3.321928095f
67 
68 #define SOS_M 343.0f
69 #define TWO_TO_5 32.0f
70 #define INV_TWO_TO_5 0.03125f
71 #define TWO_TO_7 128.f
72 #define INV_TWO_TO_7 0.0078125f
73 #define TWO_TO_8 256.f
74 #define INV_TWO_TO_8 0.00390625f
75 #define TWO_TO_9 512.f
76 #define INV_TWO_TO_9 0.001953125f
77 #define TWO_TO_10 1024.f
78 #define INV_TWO_TO_10 0.0009765625f
79 #define TWO_TO_11 2048.f
80 #define INV_TWO_TO_11 0.00048828125f
81 #define TWO_TO_12 4096.f
82 #define INV_TWO_TO_12 0.00024414062f
83 #define TWO_TO_15 32768.f
84 #define TWO_TO_16 65536.f
85 #define INV_TWO_TO_15 0.00003051757f
86 #define INV_TWO_TO_16 0.00001525878f
87 #define TWO_TO_16_MINUS_ONE 65535.0f
88 #define TWO_TO_23 8388608.0f
89 #define INV_TWO_TO_23 0.00000011920929f
90 #define TWO_TO_31 2147483648.0f
91 #define INV_TWO_TO_31 0.000000000465661f
92 #define TWO_TO_32 4294967296.0f
93 #define INV_TWO_TO_32 0.000000000232831f
94 
95 #define ONE_OVER_SQRT2 0.707106781186548f
96 
97 #define LOGTEN 2.302585092994
98  float log2f_approx(float X);
99  float log2f_approx2(float x);
100 
101  // Jones shaper
102  float LEAF_shaper (float input, float m_drive);
103  float LEAF_reedTable (float input, float offset, float slope);
104 
105  float LEAF_reduct (float input, float ratio);
106  float LEAF_round (float input, float rnd);
107  float LEAF_bitwise_xor(float input, uint32_t op);
108 
109  float LEAF_reduct (float input, float ratio);
110  float LEAF_round (float input, float rnd);
111  float LEAF_bitwise_xor(float input, uint32_t op);
112 
113  float LEAF_clip (float min, float val, float max);
114  int LEAF_clipInt (int min, int val, int max);
115  float LEAF_softClip (float val, float thresh);
116  int LEAF_isPrime (uint64_t number );
117 
118  float LEAF_midiToFrequency (float f);
119  float LEAF_frequencyToMidi(float f);
120 
121  void LEAF_generate_sine (float* buffer, int size);
122  void LEAF_generate_sawtooth (float* buffer, float basefreq, int size, LEAF* const leaf);
123  void LEAF_generate_triangle (float* buffer, float basefreq, int size, LEAF* const leaf);
124  void LEAF_generate_square (float* buffer, float basefreq, int size, LEAF* const leaf);
125 
126  // dope af
127  float LEAF_chebyshevT(float in, int n);
128  float LEAF_CompoundChebyshevT(float in, int n, float* amps);
129 
130  // Hermite interpolation
131  float LEAF_interpolate_hermite (float A, float B, float C, float D, float t);
132  float LEAF_interpolate_hermite_x(float yy0, float yy1, float yy2, float yy3, float xx);
133  float LEAF_interpolation_linear (float A, float B, float t);
134 
135  float interpolate3max(float *buf, const int peakindex);
136  float interpolate3phase(float *buf, const int peakindex);
137 
138 
139  float fastcosf(float fAngle);
140 
141  float fastercosf(float fAngle);
142 
143  float fasttanf (float fAngle);
144 
145 
146  float fastertanf(float fAngle);
147 
148 
149  // alternative implementation for abs()
150  // REQUIRES: 32 bit integers
151  int fastabs_int(int in);
152 
153  // alternative implementation for abs()
154  // REQUIRES: 32 bit floats
155  float fastabsf(float f);
156 
157  float fastexp2f(float f);
158 
159  float fastPowf(float a, float b) ;
160  double fastPow(double a, double b);
161 
162 
163  void LEAF_crossfade(float fade, float* volumes);
164 
165  float LEAF_tanh(float x);
166  float LEAF_tanhNoClip(float x);
167  float fast_tanh(float x);
168  float fast_tanh2(float x);
169  float fast_tanh3(float x);
170  float fast_tanh4(float x);
171 
172  //0.001 base gives a good curve that goes from 1 to near zero
173  //1000 gives a good curve from -1.0 to 0.0
174  void LEAF_generate_exp(float* buffer, float base, float start, float end, float offset, int size);
175 
176 
177  void LEAF_generate_atodb(float* buffer, int size);
178  void LEAF_generate_atodbPositiveClipped(float* buffer, float lowerThreshold, float range, int size);
179 
180 
181  float LEAF_poly_blep(float t, float dt);
182  float LEAF_midiToFrequency(float f);
183 
184 
185  float fast_mtof(float f);
186 
187  double fastexp(double x);
188 
189  float fastexpf(float x);
190 
191  double fasterexp(double x);
192 
193  float fasterexpf(float x);
194 
195  float fastsqrtf(float x);
196 
197  float mtof(float f);
198 
199  float fast_mtof(float f);
200 
201  float faster_mtof(float f);
202 
203  float ftom(float f);
204 
205  float powtodb(float f);
206 
207  float rmstodb(float f);
208 
209  float dbtopow(float f);
210 
211  float dbtorms(float f);
212 
213  float atodb(float a);
214 
215  float dbtoa(float db);
216 
217  float fastdbtoa(float db);
218 
219  float fasterdbtoa(float db);
220 
221  float maximum (float num1, float num2);
222 
223  float minimum (float num1, float num2);
224 
225  // built in compiler popcount functions should be faster but we want this to be portable
226  // could try to add some define that call the correct function depending on compiler
227  // or let the user pointer popcount() to whatever they want
228  // something to look into...
229  int popcount(unsigned int x);
230 
231  float median3f(float a, float b, float c);
232 
233  void place_step_dd(float *buffer, int index, float phase, float w, float scale);
234 
235  void place_slope_dd(float *buffer, int index, float phase, float w, float slope_delta);
236 
239  //==============================================================================
240 
241 #ifdef __cplusplus
242 }
243 #endif
244 
245 #endif // LEAF_MATH_H_INCLUDED
246 
247 //==============================================================================
248 
LEAF
Struct for an instance of LEAF.
Definition: leaf-global.h:31