shithub: leaf

Download patch

ref: a71835e6d17ecd00bf6d37fa1e4560f4507a6c23
parent: 1a8d5375b811315a2c3878035a27fd7852a8b168
author: Matthew Wang <Matthew@MacBook-Pro.local>
date: Sat Dec 7 02:42:48 EST 2019

add efficient exp2 estimate

--- a/LEAF/Inc/leaf-math.h
+++ b/LEAF/Inc/leaf-math.h
@@ -1,11 +1,11 @@
 /*==============================================================================
+ 
+ leaf-math.h
+ Created: 22 Jan 2017 7:02:56pm
+ Author:  Michael R Mulshine
+ 
+ ==============================================================================*/
 
-    leaf-math.h
-    Created: 22 Jan 2017 7:02:56pm
-    Author:  Michael R Mulshine
-
-==============================================================================*/
-
 #ifndef LEAF_MATH_H_INCLUDED
 #define LEAF_MATH_H_INCLUDED
 
@@ -12,38 +12,38 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
-
+    
 #include "leaf-global.h"
 #include "math.h"
 #include "stdint.h"
 #include "stdlib.h"
-
-//==============================================================================
-
-//==============================================================================
-
-typedef enum oBool
-{
-    OTRUE  = 1,
-    OFALSE = 0
-}oBool;
     
-// Allows for bitwise operations on floats
-union unholy_t { /* a union between a float and an integer */
-    float f;
-    int i;
-};
-
+    //==============================================================================
+    
+    //==============================================================================
+    
+    typedef enum oBool
+    {
+        OTRUE  = 1,
+        OFALSE = 0
+    }oBool;
+    
+    // Allows for bitwise operations on floats
+    union unholy_t { /* a union between a float and an integer */
+        float f;
+        int i;
+    };
+    
 #define SQRT8 2.82842712475f
 #define WSCALE 1.30612244898f
 #define PI              (3.14159265358979f)
 #define TWO_PI          (2 * PI)
-
+    
 #define VSF             1.0e-38f
-
+    
 #define MAX_DELAY       8192
 #define INV_128         0.0078125f
-
+    
 #define INV_20         0.05f
 #define INV_40         0.025f
 #define INV_80         0.0125f
@@ -55,96 +55,98 @@
 #define INV_5120       0.0001953125f
 #define INV_10240      0.00009765625f
 #define INV_20480      0.000048828125f
-
-
-#define INV_TWELVE 				0.0833333333f
-#define INV_440 					0.0022727273f
-
-#define LOG2 							0.3010299957f
-#define INV_LOG2 					3.321928095f
-
-#define SOS_M 						343.0f
-
-#define TWO_TO_7 					128.f
-#define INV_TWO_TO_7 			0.0078125f
-#define TWO_TO_8 					256.f
-#define INV_TWO_TO_8 			0.00390625f
-#define TWO_TO_5 					32.0f
-#define INV_TWO_TO_5 			0.03125f
-#define TWO_TO_12 				4096.f
-#define INV_TWO_TO_12 		0.00024414062f
-#define TWO_TO_15 				32768.f
-#define TWO_TO_16 				65536.f
-#define INV_TWO_TO_15 		0.00003051757f
-#define INV_TWO_TO_16 		0.00001525878f
+    
+    
+#define INV_TWELVE                 0.0833333333f
+#define INV_440                     0.0022727273f
+    
+#define LOG2                             0.3010299957f
+#define INV_LOG2                     3.321928095f
+    
+#define SOS_M                         343.0f
+    
+#define TWO_TO_7                     128.f
+#define INV_TWO_TO_7             0.0078125f
+#define TWO_TO_8                     256.f
+#define INV_TWO_TO_8             0.00390625f
+#define TWO_TO_5                     32.0f
+#define INV_TWO_TO_5             0.03125f
+#define TWO_TO_12                 4096.f
+#define INV_TWO_TO_12         0.00024414062f
+#define TWO_TO_15                 32768.f
+#define TWO_TO_16                 65536.f
+#define INV_TWO_TO_15         0.00003051757f
+#define INV_TWO_TO_16         0.00001525878f
 #define TWO_TO_16_MINUS_ONE 65535.0f
-#define TWO_TO_23		8388608.0f
-#define INV_TWO_TO_23	0.00000011920929f
-#define TWO_TO_31		2147483648.0f
-#define INV_TWO_TO_31	0.000000000465661f
-#define TWO_TO_32		4294967296.0f
-#define INV_TWO_TO_32	0.000000000232831f
-
-// Jones shaper
-float LEAF_shaper     (float input, float m_drive);
-float LEAF_reedTable  (float input, float offset, float slope);
+#define TWO_TO_23        8388608.0f
+#define INV_TWO_TO_23    0.00000011920929f
+#define TWO_TO_31        2147483648.0f
+#define INV_TWO_TO_31    0.000000000465661f
+#define TWO_TO_32        4294967296.0f
+#define INV_TWO_TO_32    0.000000000232831f
     
-float LEAF_reduct (float input, float ratio);
-float LEAF_round (float input, float rnd);
-float LEAF_bitwise_xor(float input, uint32_t op);
-
-float LEAF_reduct (float input, float ratio);
-float LEAF_round (float input, float rnd);
-float LEAF_bitwise_xor(float input, uint32_t op);
-
-float       LEAF_clip               (float min, float val, float max);
-int         LEAF_clipInt            (int min, int val, int max);
-float   	LEAF_softClip		    (float val, float thresh);
-oBool       LEAF_isPrime            (uint64_t number );
-
-float       LEAF_midiToFrequency    (float f);
-float       LEAF_frequencyToMidi(float f);
-
-void        LEAF_generate_sine     (float* buffer, int size);
-void        LEAF_generate_sawtooth (float* buffer, float basefreq, int size);
-void        LEAF_generate_triangle (float* buffer, float basefreq, int size);
-void        LEAF_generate_square   (float* buffer, float basefreq, int size);
-
-// dope af
-float       LEAF_chebyshevT(float in, int n);
-float       LEAF_CompoundChebyshevT(float in, int n, float* amps);
-
-// Hermite interpolation
-float LEAF_interpolate_hermite (float A, float B, float C, float D, float t);
-float LEAF_interpolation_linear (float A, float B, float t);
-
-float interpolate3max(float *buf, const int peakindex);
-float interpolate3phase(float *buf, const int peakindex);
-
-// alternative implementation for abs()
-// REQUIRES: 32 bit integers
-int fastabs_int(int in);
-
-// alternative implementation for abs()
-// REQUIRES: 32 bit floats
-float fastabs(float f);
+    // Jones shaper
+    float LEAF_shaper     (float input, float m_drive);
+    float LEAF_reedTable  (float input, float offset, float slope);
     
+    float LEAF_reduct (float input, float ratio);
+    float LEAF_round (float input, float rnd);
+    float LEAF_bitwise_xor(float input, uint32_t op);
+    
+    float LEAF_reduct (float input, float ratio);
+    float LEAF_round (float input, float rnd);
+    float LEAF_bitwise_xor(float input, uint32_t op);
+    
+    float       LEAF_clip               (float min, float val, float max);
+    int         LEAF_clipInt            (int min, int val, int max);
+    float       LEAF_softClip            (float val, float thresh);
+    oBool       LEAF_isPrime            (uint64_t number );
+    
+    float       LEAF_midiToFrequency    (float f);
+    float       LEAF_frequencyToMidi(float f);
+    
+    void        LEAF_generate_sine     (float* buffer, int size);
+    void        LEAF_generate_sawtooth (float* buffer, float basefreq, int size);
+    void        LEAF_generate_triangle (float* buffer, float basefreq, int size);
+    void        LEAF_generate_square   (float* buffer, float basefreq, int size);
+    
+    // dope af
+    float       LEAF_chebyshevT(float in, int n);
+    float       LEAF_CompoundChebyshevT(float in, int n, float* amps);
+    
+    // Hermite interpolation
+    float LEAF_interpolate_hermite (float A, float B, float C, float D, float t);
+    float LEAF_interpolation_linear (float A, float B, float t);
+    
+    float interpolate3max(float *buf, const int peakindex);
+    float interpolate3phase(float *buf, const int peakindex);
+    
+    // alternative implementation for abs()
+    // REQUIRES: 32 bit integers
+    int fastabs_int(int in);
+    
+    // alternative implementation for abs()
+    // REQUIRES: 32 bit floats
+    float fastabs(float f);
+    
+    float fastexp2(float f);
+    
 #define LOGTEN 2.302585092994
-
-float mtof(float f);
-
-float ftom(float f);
-
-float powtodb(float f);
-
-float rmstodb(float f);
-
-float dbtopow(float f);
-
-float dbtorms(float f);
-
-//==============================================================================
     
+    float mtof(float f);
+    
+    float ftom(float f);
+    
+    float powtodb(float f);
+    
+    float rmstodb(float f);
+    
+    float dbtopow(float f);
+    
+    float dbtorms(float f);
+    
+    //==============================================================================
+    
 #ifdef __cplusplus
 }
 #endif
@@ -152,3 +154,4 @@
 #endif  // LEAF_MATH_H_INCLUDED
 
 //==============================================================================
+
--- a/LEAF/Src/leaf-math.c
+++ b/LEAF/Src/leaf-math.c
@@ -73,6 +73,37 @@
     return alias.f;
 }
 
+// fast floating-point exp2 function taken from Robert Bristow Johnson's
+// post in the music-dsp list on Date: Tue, 02 Sep 2014 16:50:11 -0400
+float fastexp2(float x)
+{
+    if (x >= -127.0)
+    {
+        register float accumulator, xPower;
+        register union {float f; int32_t i;} xBits;
+        
+        xBits.i = (int32_t)(x + 4096.0f) - 4096L;               /* integer part */
+        x -= (float)(xBits.i);                                             /* fractional part */
+        
+        accumulator = 1.0f + 0.69303212081966f*x;
+        xPower = x*x;
+        accumulator += 0.24137976293709f*xPower;
+        xPower *= x;
+        accumulator += 0.05203236900844f*xPower;
+        xPower *= x;
+        accumulator += 0.01355574723481f*xPower;
+        
+        xBits.i += 127;                                                    /* bias integer part */
+        xBits.i<<= 23;                                                     /* move biased int part into exponent bits */
+        
+        return accumulator * xBits.f;
+    }
+    else
+    {
+        return 0.0f;
+    }
+}
+
 // dope af
 float LEAF_chebyshevT(float in, int n){
 	if (n == 0) return 1;
--- a/LEAF_JUCEPlugin/Source/MyTest.cpp
+++ b/LEAF_JUCEPlugin/Source/MyTest.cpp
@@ -28,6 +28,8 @@
 float dtime;
 bool buttonState;
 int ratio = 2;
+int x = 0;
+int y = 0;
 
 #define MSIZE 500000
 char memory[MSIZE];
@@ -62,15 +64,23 @@
 float   LEAFTest_tick            (float input)
 {
     float sample = tCycle_tick(&sine);
-    float* retuneOut = tRetune_tick(&retune, sample);
-    float* autotuneOut = tAutotune_tick(&autotune, sample);
-    float r1 = retuneOut[0];
-    //float r2 = retuneOut[1];
-    float a1 = autotuneOut[6];
-    float a2 = autotuneOut[7];
-    sample = a1 + a2;
+//    float* retuneOut = tRetune_tick(&retune, sample);
+//    float* autotuneOut = tAutotune_tick(&autotune, sample);
+//    float r1 = retuneOut[0];
+//    //float r2 = retuneOut[1];
+//    float a1 = autotuneOut[6];
+//    float a2 = autotuneOut[7];
+//    sample = a1 + a2;
+//
+//    sample = tTalkbox_tick(&test, tNoise_tick(&noise), input);
     
-    sample = tTalkbox_tick(&test, tNoise_tick(&noise), input);
+    // do more calls based on 2nd slider
+    for (int i = 0; i < x; i++)
+    {
+        // do fast if 3rd slider is all the way up
+        if (y) float exp2 = exp2f(x);
+        else float fexp2 = fastexp2(x);
+    }
     
     return sample * 0.25f;
 }
@@ -84,9 +94,13 @@
     tRetune_setPitchFactor(&retune, val*4.0f + 0.5f, 0);
     tAutotune_setFreq(&autotune, val*5000.0f + 50.0f, 7);
     
+    x = (val*5000);
+    
     val = getSliderValue("mod depth");
     tRetune_setPitchFactor(&retune, val*4.0f + 0.5f, 1);
     tAutotune_setFreq(&autotune, val*5000.0f + 50.0f, 6);
+    
+    y = val;
 }
 
 void    LEAFTest_controllerInput (int cnum, float cval)