shithub: leaf

Download patch

ref: 8acb64c973bb8793d51f2af5fb3d79b3a4518940
parent: 8190e1637f5dbe57388711c6c994ed3be13d3474
author: Jeffrey Snyder <jeffsnyder@jeffreys-mbp.mynetworksettings.com>
date: Wed Aug 24 20:07:18 EDT 2022

fixed triangle lfo

--- a/leaf/Inc/leaf-oscillators.h
+++ b/leaf/Inc/leaf-oscillators.h
@@ -1472,10 +1472,8 @@
         tMempool mempool;
         int32_t phase;
         int32_t inc;
-        int32_t prevPhase;
         float freq;
-        uint8_t phaseDidReset;
-        uint32_t invSampleRate;
+        float invSampleRate;
         float invSampleRateTimesTwoTo32;
     } _tTriLFO;
     
--- a/leaf/Src/leaf-analysis.c
+++ b/leaf/Src/leaf-analysis.c
@@ -1822,6 +1822,7 @@
     collector->_fundamental._period = -1;
     collector->_fundamental._periodicity = 0.0f;
     collector->_fundamental._harmonic = 0;
+    collector->_first_period = 0.01f;
 }
 
 static inline float sub_collector_period_of(_sub_collector* collector, _auto_correlation_info info)
--- a/leaf/Src/leaf-envelopes.c
+++ b/leaf/Src/leaf-envelopes.c
@@ -1316,8 +1316,8 @@
     
     smooth->curr = val;
     smooth->dest = val;
-    if (factor < 0) factor = 0;
-    if (factor > 1) factor = 1;
+    if (factor < 0.0f) factor = 0.0f;
+    if (factor > 1.0f) factor = 1.0f;
     smooth->baseFactor = factor;
     smooth->factor = factor;
     smooth->oneminusfactor = 1.0f - factor;
@@ -1334,9 +1334,9 @@
 {   // factor is usually a value between 0 and 0.1. Lower value is slower. 0.01 for example gives you a smoothing time of about 10ms
     _tExpSmooth* smooth = *expsmooth;
     
-    if (factor < 0)
-        factor = 0;
-    else if (factor > 1) factor = 1;
+    if (factor < 0.0f)
+        factor = 0.0f;
+    else if (factor > 1.0f) factor = 1.0f;
     smooth->baseFactor = factor;
     smooth->factor = powf(factor, 44100.f * smooth->invSampleRate);
     smooth->oneminusfactor = 1.0f - factor;
--- a/leaf/Src/leaf-oscillators.c
+++ b/leaf/Src/leaf-oscillators.c
@@ -1269,7 +1269,6 @@
     
     sync = c->sync;
 
-
     p = c->_p;  /* phase [0, 1) */
     w = c->_w;  /* phase increment */
     b = c->_b;  /* duty cycle (0, 1) */
@@ -1482,6 +1481,7 @@
 void tMBTriangle_setWidth(tMBTriangle* const osc, float w)
 {
     _tMBTriangle* c = *osc;
+    w = LEAF_clip(0.0f, w, 0.99f);
     c->waveform = w;
 }
 
@@ -1846,10 +1846,13 @@
 		 }
 
 
-	   /* now place reset DD for pulse */
+
 		if (sw > 0)
 		{
-			if (k) {
+			/* now place reset DD for saw*/
+			place_step_dd(c->_f, j, p, sw, p_at_reset * sawShape);
+            /* now place reset DD for pulse */
+            if (k) {
 				place_step_dd(c->_f, j, p, sw, 1.0f * shape);
 				k = 0;
 				x = 0.5f;
@@ -1862,6 +1865,9 @@
 		}
 		else if (sw < 0)
 		{
+	        /* now place reset DD for saw*/
+			place_step_dd(c->_f, j, 1.0f - p, -sw, -p_at_reset * sawShape);
+			 /* now place reset DD for pulse */
 			if (!k) {
 				place_step_dd(c->_f, j, 1.0f - p, -sw, -1.0f * shape);
 				k = 1;
@@ -1873,12 +1879,8 @@
 				x = 0.5f;
 			}
 		}
-        /* now place reset DD for saw*/
-        if (sw > 0)
-            place_step_dd(c->_f, j, p, sw, p_at_reset * sawShape);
-        else if (sw < 0)
-            place_step_dd(c->_f, j, 1.0f - p, -sw, -p_at_reset * sawShape);
 
+
     }
 
 
@@ -2835,7 +2837,6 @@
     
     c->phase    =  0;
     c->invSampleRateTimesTwoTo32 = (leaf->invSampleRate * TWO_TO_32);
-    c->mask = 0x7FFFFFFF;
 }
 
 void    tIntPhasor_free (tIntPhasor* const cy)
@@ -2850,7 +2851,7 @@
 {
     _tIntPhasor* c = *cy;
     // Phasor increment
-    c->phase = (c->phase + c->inc);//& c->mask;
+    c->phase = (c->phase + c->inc);
     
     return c->phase * INV_TWO_TO_32; 
 }
@@ -2891,10 +2892,9 @@
     _tMempool* m = *mp;
     _tSquareLFO* c = *cy = (_tSquareLFO*) mpool_alloc(sizeof(_tSquareLFO), m);
     c->mempool = m;
-    LEAF* leaf = c->mempool->leaf;
     tIntPhasor_initToPool(&c->phasor,mp);
     tIntPhasor_initToPool(&c->invPhasor,mp); 
-    tSquareLFO_setPulseWidth(cy, 0.5);
+    tSquareLFO_setPulseWidth(cy, 0.5f);
 }
 
 void    tSquareLFO_free (tSquareLFO* const cy)
@@ -2913,7 +2913,7 @@
     float a = tIntPhasor_tick(&c->phasor);
     float b = tIntPhasor_tick(&c->invPhasor);
     float tmp = ((a - b)) + c->pulsewidth - 1.0f;
-    return 2 * tmp;
+    return 2.0f * tmp;
 }
 
 void     tSquareLFO_setFreq(tSquareLFO* const cy, float freq)
@@ -2937,7 +2937,6 @@
     _tSquareLFO *c = *cy;
 
     c->pulsewidth = pw;
-    //c->delay = c->pulsewidth * INV_TWO_TO_32;
     tIntPhasor_setPhase(&c->invPhasor, c->pulsewidth + (c->phasor->phase * INV_TWO_TO_32));
 }
 
@@ -2951,7 +2950,7 @@
 void    tSawSquareLFO_init        (tSawSquareLFO* const cy, LEAF* const leaf)
 {
     tSawSquareLFO_initToPool(cy, &leaf->mempool);
-    _tSawSquareLFO* c = *cy; 
+
 }
 
 void    tSawSquareLFO_initToPool  (tSawSquareLFO* const cy, tMempool* const mp)
@@ -2959,7 +2958,6 @@
     _tMempool* m = *mp;
     _tSawSquareLFO* c = *cy = (_tSawSquareLFO*) mpool_alloc(sizeof(_tSawSquareLFO), m);
     c->mempool = m;
-    LEAF* leaf = c->mempool->leaf;
     tSquareLFO_initToPool(&c->square,mp);
     tIntPhasor_initToPool(&c->saw,mp); 
 }
@@ -3024,8 +3022,7 @@
     c->phase    =  0;
     c->invSampleRate = leaf->invSampleRate;
     c->invSampleRateTimesTwoTo32 = (c->invSampleRate * TWO_TO_32);
-    //c->mask = TRI_TABLE_SIZE - 1;
-    tTriLFO_setFreq(cy, 220);
+    tTriLFO_setFreq(cy, 220.0f);
 }
 
 void    tTriLFO_free (tTriLFO* const cy)
@@ -3039,35 +3036,16 @@
 float   tTriLFO_tick(tTriLFO* const cy)
 {
     _tTriLFO* c = *cy;
-    //uint32_t idx;
-    //float frac, samp0, samp1;
-    // Phasor increment
     c->phase += c->inc;
     
     //bitmask fun
-    //
     int32_t shiftedPhase = c->phase + 1073741824; // offset by 1/4" wave by adding 2^30 to get things in phase with the other LFO oscillators
-    uint32_t mask = shiftedPhase >> 31;
-    int32_t val2 = shiftedPhase + mask;
-    int32_t test = val2 ^ mask;
-    float output =( ((float)test * INV_TWO_TO_31)-0.5f) * 2.0f;
+    uint32_t mask = shiftedPhase >> 31; //get the sign bit
+    shiftedPhase = shiftedPhase + mask; // add 1 if negative, zero if positive, to balance
+    shiftedPhase = shiftedPhase ^ mask; //invert the value to get absolute value of integer
+    float output = (((float)shiftedPhase * INV_TWO_TO_31) - 0.5f) * 2.0f; //scale it to -1.0f to 1.0f float
     return output;
 
-    /*
-    // Wavetable synthesis
-    idx = c->phase >> 21;
-    uint32_t idx2 = (idx + 1) & c->mask;
-    uint32_t tempFrac = (c->phase & 2097151);
-    frac = (float)tempFrac * 0.000000476837386f;// 1/2097151 (2097151 is the 21 bits after the 11 bits that represent the main index)
-    
-    samp0 = __leaf_table_triangle[0][idx];
-    samp1 = __leaf_table_triangle[0][idx2];
-    float oct0 = (samp0 + (samp1 - samp0) * frac);
-   
-    
-    return oct0;
-     */
-
 }
 
 void     tTriLFO_setFreq(tTriLFO* const cy, float freq)
@@ -3090,8 +3068,8 @@
 void     tTriLFO_setSampleRate (tTriLFO* const cy, float sr)
 {
     _tTriLFO* c = *cy;
-    
-    c->invSampleRateTimesTwoTo32 = (1.0f/sr) * TWO_TO_32;
+    c->invSampleRate = (1.0f/sr);
+    c->invSampleRateTimesTwoTo32 = c->invSampleRate * TWO_TO_32;
     tTriLFO_setFreq(cy, c->freq);
 }
 ///sinetri
@@ -3099,7 +3077,6 @@
 void    tSineTriLFO_init        (tSineTriLFO* const cy, LEAF* const leaf)
 {
     tSineTriLFO_initToPool(cy, &leaf->mempool);
-    _tSineTriLFO* c = *cy; 
 }
 
 void    tSineTriLFO_initToPool  (tSineTriLFO* const cy, tMempool* const mp)
@@ -3107,7 +3084,6 @@
     _tMempool* m = *mp;
     _tSineTriLFO* c = *cy = (_tSineTriLFO*) mpool_alloc(sizeof(_tSineTriLFO), m);
     c->mempool = m;
-    LEAF* leaf = c->mempool->leaf;
     tTriLFO_initToPool(&c->tri,mp);
     tCycle_initToPool(&c->sine,mp); 
     
--- a/leaf/Src/leaf-sampling.c
+++ b/leaf/Src/leaf-sampling.c
@@ -1281,9 +1281,9 @@
     int start, end, length;
     float* buff;
     int    j;
-    float  syncin;
+    //float  syncin;
     float  a, p, w, z;
-    syncin  = c->syncin;
+   // syncin  = c->syncin;
         
     start = c->start;
     end = c->end;