shithub: leaf

Download patch

ref: 67b440210f238511a2f30b1da044a6f5457d5b32
parent: 187dd1832155f35005a8fd968b1aa99801a3b4d8
author: Davis Polito <davispolito1@gmail.com>
date: Thu Aug 18 12:36:54 EDT 2022

Add simpler LFOS

--- a/leaf/Inc/leaf-oscillators.h
+++ b/leaf/Inc/leaf-oscillators.h
@@ -1237,6 +1237,116 @@
 //    void    tWaveOscS_setIndexTable(tWaveOscS* const osc, int i, float* table, int size);
     void    tWaveOscS_setSampleRate (tWaveOscS* const osc, float sr);
     
+
+
+
+     //==============================================================================
+    
+    /*!
+     @defgroup tIntphasor tIntPhasor
+     @ingroup oscillators
+     @brief Aliasing phasor.
+     @{
+     
+     @fn void    tIntPhasor_init        (tIntPhasor* const osc, LEAF* const leaf)
+     @brief Initialize a tIntPhasor to the default mempool of a LEAF instance.
+     @param osc A pointer to the tIntPhasor to initialize.
+     @param leaf A pointer to the leaf instance.
+     
+     @fn void    tIntPhasor_initToPool  (tIntPhasor* const osc, tMempool* const)
+     @brief Initialize a tIntPhasor to a specified mempool.
+     @param osc A pointer to the tIntPhasor to initialize.
+     @param mempool A pointer to the tMempool to use.
+     
+     @fn void    tIntPhasor_free        (tIntPhasor* const osc)
+     @brief Free a tIntPhasor from its mempool.
+     @param osc A pointer to the tIntPhasor to free.
+     
+     @fn float   tIntPhasor_tick        (tIntPhasor* const osc)
+     @brief
+     @param osc A pointer to the relevant tIntPhasor.
+     
+     @fn void    tIntPhasor_setFreq     (tIntPhasor* const osc, float freq)
+     @brief
+     @param osc A pointer to the relevant tIntPhasor.
+     
+     @} */
+    
+    typedef struct _tIntPhasor
+    {
+        
+        tMempool mempool;
+        uint32_t phase;
+        uint32_t inc, freq;
+        uint32_t mask;
+        uint8_t phaseDidReset;
+        uint32_t invSampleRateTimesTwoTo32;
+    } _tIntPhasor;
+    
+    typedef _tIntPhasor* tIntPhasor;
+    
+    void    tIntPhasor_init        (tIntPhasor* const osc, LEAF* const leaf);
+    void    tIntPhasor_initToPool  (tIntPhasor* const osc, tMempool* const);
+    void    tIntPhasor_free        (tIntPhasor* const osc);
+    
+    float   tIntPhasor_tick        (tIntPhasor* const osc);
+    void    tIntPhasor_setFreq     (tIntPhasor* const osc, float freq);
+    void    tIntPhasor_setSampleRate (tIntPhasor* const osc, float sr);
+    
+    void    tIntPhasor_setPhase(tIntPhasor* const cy, float phase);
+    
+         //==============================================================================
+    
+    /*!
+     @defgroup tSquareLFO tSquareLFO
+     @ingroup oscillators
+     @brief Aliasing phasor.
+     @{
+     
+     @fn void    tSquareLFO_init        (tSquareLFO* const osc, LEAF* const leaf)
+     @brief Initialize a tSquareLFO to the default mempool of a LEAF instance.
+     @param osc A pointer to the tSquareLFO to initialize.
+     @param leaf A pointer to the leaf instance.
+     
+     @fn void    tSquareLFO_initToPool  (tSquareLFO* const osc, tMempool* const)
+     @brief Initialize a tSquareLFO to a specified mempool.
+     @param osc A pointer to the tSquareLFO to initialize.
+     @param mempool A pointer to the tMempool to use.
+     
+     @fn void    tSquareLFO_free        (tSquareLFO* const osc)
+     @brief Free a tSquareLFO from its mempool.
+     @param osc A pointer to the tSquareLFO to free.
+     
+     @fn float   tSquareLFO_tick        (tSquareLFO* const osc)
+     @brief
+     @param osc A pointer to the relevant tSquareLFO.
+     
+     @fn void    tSquareLFO_setFreq     (tSquareLFO* const osc, float freq)
+     @brief
+     @param osc A pointer to the relevant tSquareLFO.
+     
+     @} */
+    
+    typedef struct _tSquareLFO
+    {
+        
+        tMempool mempool;
+        float pulsewidth;
+        tIntPhasor phasor;
+        tIntPhasor invPhasor;
+    } _tSquareLFO;
+    
+    typedef _tSquareLFO* tSquareLFO;
+    
+    void    tSquareLFO_init        (tSquareLFO* const osc, LEAF* const leaf);
+    void    tSquareLFO_initToPool  (tSquareLFO* const osc, tMempool* const);
+    void    tSquareLFO_free        (tSquareLFO* const osc);
+    
+    float   tSquareLFO_tick        (tSquareLFO* const osc);
+    void    tSquareLFO_setFreq     (tSquareLFO* const osc, float freq);
+    void    tSquareLFO_setSampleRate (tSquareLFO* const osc, float sr);
+    void    tSquareLFO_setPulseWidth (tSquareLFO* const cy, float pw);
+    
 #ifdef __cplusplus
 }
 #endif
--- a/leaf/Src/leaf-oscillators.c
+++ b/leaf/Src/leaf-oscillators.c
@@ -55,14 +55,14 @@
     // Phasor increment
     c->phase += c->inc;
     // Wavetable synthesis
-    idx = c->phase >> 21;
-    tempFrac = (c->phase & 2097151);
+    idx = c->phase >> 21; //11 bit table 
+    tempFrac = (c->phase & 2097151); //(2^21 - 1) all the lower bits i.e. the remainder of a division by 2^21  (2097151 is the 21 bits after the 11 bits that represent the main index) 
     
     samp0 = __leaf_table_sinewave[idx];
     idx = (idx + 1) & c->mask;
     samp1 = __leaf_table_sinewave[idx];
     
-    return (samp0 + (samp1 - samp0) * ((float)tempFrac * 0.000000476837386f)); // 1/2097151 (2097151 is the 21 bits after the 11 bits that represent the main index)
+    return (samp0 + (samp1 - samp0) * ((float)tempFrac * 0.000000476837386f)); // 1/2097151 
 }
 
 void     tCycle_setFreq(tCycle* const cy, float freq)
@@ -2527,3 +2527,128 @@
 //    tWaveTableS_free(&c->tables[i]);
 //    tWaveTableS_initToPool(&c->tables[i], table, size, c->maxFreq, &c->mempool);
 //}
+
+
+//beep boop adding intphasro
+// Cycle
+void    tIntPhasor_init(tIntPhasor* const cy, LEAF* const leaf)
+{
+    tIntPhasor_initToPool(cy, &leaf->mempool);
+}
+
+void    tIntPhasor_initToPool   (tIntPhasor* const cy, tMempool* const mp)
+{
+    _tMempool* m = *mp;
+    _tIntPhasor* c = *cy = (_tIntPhasor*) mpool_alloc(sizeof(_tIntPhasor), m);
+    c->mempool = m;
+    LEAF* leaf = c->mempool->leaf;
+    
+    c->phase    =  0;
+    c->invSampleRateTimesTwoTo32 = (leaf->invSampleRate * TWO_TO_32);
+    c->mask = 0xFFFFFFFF;
+}
+
+void    tIntPhasor_free (tIntPhasor* const cy)
+{
+    _tIntPhasor* c = *cy;
+    
+    mpool_free((char*)c, c->mempool);
+}
+
+//need to check bounds and wrap table properly to allow through-zero FM
+float   tIntPhasor_tick(tIntPhasor* const cy)
+{
+    _tIntPhasor* c = *cy;
+    // Phasor increment
+    c->phase = (c->phase + c->inc) & c->mask;
+    
+    return c->phase * INV_TWO_TO_32; 
+}
+
+void     tIntPhasor_setFreq(tIntPhasor* const cy, float freq)
+{
+    _tIntPhasor* c = *cy;
+    
+    c->freq  = freq;
+    c->inc = freq * c->invSampleRateTimesTwoTo32;
+}
+
+void    tIntPhasor_setPhase(tIntPhasor* const cy, float phase)
+{
+    _tIntPhasor* c = *cy;
+    
+    int i = phase;
+    phase -= i;
+    c->phase = phase * TWO_TO_32;
+}
+
+void     tIntPhasor_setSampleRate (tIntPhasor* const cy, float sr)
+{
+    _tIntPhasor* c = *cy;
+    
+    c->invSampleRateTimesTwoTo32 = (1.0f/sr) * TWO_TO_32;
+    tIntPhasor_setFreq(cy, c->freq);
+}
+
+//////SQUARE(PUSHER)LFO
+void    tSquareLFO_init(tSquareLFO* const cy, LEAF* const leaf)
+{
+    tSquareLFO_initToPool(cy, &leaf->mempool);
+    _tSquareLFO* c = *cy;
+   
+}
+
+void    tSquareLFO_initToPool   (tSquareLFO* const cy, tMempool* const mp)
+{
+    _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); 
+
+
+    c->pulsewidth = 50.0f;
+}
+
+void    tSquareLFO_free (tSquareLFO* const cy)
+{
+    _tSquareLFO* c = *cy;
+    tIntPhasor_free(&c->phasor);
+    tIntPhasor_free(&c->invPhasor);
+    mpool_free((char*)c, c->mempool);
+}
+
+//need to check bounds and wrap table properly to allow through-zero FM
+float   tSquareLFO_tick(tSquareLFO* const cy)
+{
+    _tSquareLFO* c = *cy;
+    // Phasor increment
+    
+    return tIntPhasor_tick(&c->phasor) + tIntPhasor_tick(&c->invPhasor);
+}
+
+void     tSquareLFO_setFreq(tSquareLFO* const cy, float freq)
+{
+    _tSquareLFO* c = *cy;
+    tIntPhasor_setFreq(&c->phasor,freq);
+    tIntPhasor_setFreq(&c->invPhasor,-freq);
+}
+
+
+
+void     tSquareLFO_setSampleRate (tSquareLFO* const cy, float sr)
+{
+    _tSquareLFO* c = *cy;
+    tIntPhasor_setSampleRate(&c->phasor, sr);
+    tIntPhasor_setSampleRate(&c->invPhasor, sr);
+}
+
+void tSquareLFO_setPulseWidth(tSquareLFO* const cy, float pw)
+{
+    _tSquareLFO *c = *cy;
+
+    c->pulsewidth = pw;
+    //c->delay = c->pulsewidth * INV_TWO_TO_32;
+    tIntPhasor_setPhase(&c->invPhasor, c->pulsewidth);
+}