shithub: leaf

Download patch

ref: 217246c6b3aa54a3e1b2a9306cd2da979f24a361
parent: 376abf2e92f5f3d6dcb5a0c6a3ac0ff13d555d28
author: Matthew Wang <mjw7@princeton.edu>
date: Wed May 6 12:45:27 EDT 2020

tSine oscillator added, generates table on init

--- a/LEAF/Inc/leaf-oscillators.h
+++ b/LEAF/Inc/leaf-oscillators.h
@@ -90,6 +90,72 @@
     
     /*! @} */
     
+    //==============================================================================
+    
+    typedef struct _tSine
+    {
+        float* sine;
+        int size;
+        float phase;
+        float inc,freq;
+    } _tSine;
+    
+    typedef _tSine* tSine;
+    
+    /*!
+     * @defgroup tsine tSine
+     * @ingroup oscillators
+     * @brief A cycle/sine waveform oscillator.
+     * @{
+     */
+    
+    //! Initialize a tSine to the default LEAF mempool.
+    /*!
+     @param osc A pointer to the tSine to be initialized.
+     */
+    void    tSine_init         (tSine* const osc, int size);
+    
+    
+    //! Free a tSine from the default LEAF mempool.
+    /*!
+     @param osc A pointer to the tSine to be freed.
+     */
+    void    tSine_free         (tSine* const osc);
+    
+    
+    //! Initialize a tSine to a specified mempool.
+    /*!
+     @param osc A pointer to the tSine to be initialized.
+     @param pool A pointer to the tMempool to which the tSine should be initialized.
+     */
+    void    tSine_initToPool   (tSine* const osc, int size, tMempool* const pool);
+    
+    
+    //! Free a tSine from a specified mempool.
+    /*!
+     @param osc A pointer to the tSine to be freed.
+     @param pool A pointer to the tMempool from which the tSine should be freed.
+     */
+    void    tSine_freeFromPool (tSine* const osc, tMempool* const pool);
+    
+    
+    //! Tick a tSine oscillator.
+    /*!
+     @param osc A pointer to the relevant tSine.
+     @return The ticked sample as a float from -1 to 1.
+     */
+    float   tSine_tick         (tSine* const osc);
+    
+    
+    //! Set the frequency of a tSine oscillator.
+    /*!
+     @param osc A pointer to the relevant tSine.
+     @param freq The frequency to set the oscillator to.
+     */
+    void    tSine_setFreq      (tSine* const osc, float freq);
+    
+    /*! @} */
+    
     
     //==============================================================================
     
--- a/LEAF/Inc/leaf-sampling.h
+++ b/LEAF/Inc/leaf-sampling.h
@@ -137,6 +137,7 @@
     
     void    tSampler_setStart           (tSampler* const, int32_t start);
     void    tSampler_setEnd             (tSampler* const, int32_t end);
+    void    tSampler_setLength             (tSampler* const, int32_t length);
     
     static void handleStartEndChange    (tSampler* const sp);
     
--- a/LEAF/Src/leaf-math.c
+++ b/LEAF/Src/leaf-math.c
@@ -262,8 +262,8 @@
 
 float   LEAF_clip(float min, float val, float max)
 {
-    int tempmin = min;
-    int tempmax = max;
+    float tempmin = min;
+    float tempmax = max;
     if (min > max)
     {
         tempmin = max;
--- a/LEAF/Src/leaf-oscillators.c
+++ b/LEAF/Src/leaf-oscillators.c
@@ -92,6 +92,85 @@
 }
 
 //========================================================================
+// Sine
+void    tSine_init(tSine* const cy, int size)
+{
+    tSine_initToPool(cy, size, &leaf.mempool);
+}
+
+void    tSine_free(tSine* const cy)
+{
+    tSine_freeFromPool(cy, &leaf.mempool);
+}
+
+void    tSine_initToPool   (tSine* const cy, int size, tMempool* const mp)
+{
+    _tMempool* m = *mp;
+    _tSine* c = *cy = (_tSine*) mpool_alloc(sizeof(_tSine), m);
+    
+    c->size = size;
+    c->sine = (float*) mpool_alloc(sizeof(float) * c->size, m);
+    LEAF_generate_sine(c->sine, size);
+    c->inc      =  0.0f;
+    c->phase    =  0.0f;
+    
+}
+
+void    tSine_freeFromPool (tSine* const cy, tMempool* const mp)
+{
+    _tMempool* m = *mp;
+    _tSine* c = *cy;
+    
+    mpool_free(c->sine, m);
+    mpool_free(c, m);
+}
+
+void     tSine_setFreq(tSine* const cy, float freq)
+{
+    _tSine* c = *cy;
+    
+    if (freq < 0.0f) c->freq = 0.0f;
+    else if (freq > 20480.0f) c->freq = 20480.0f;
+    else c->freq  = freq;
+    
+    c->inc = freq * leaf.invSampleRate;
+}
+
+float   tSine_tick(tSine* const cy)
+{
+    _tSine* c = *cy;
+    float temp;
+    int intPart;
+    float fracPart;
+    float samp0;
+    float samp1;
+    
+    // Phasor increment
+    c->phase += c->inc;
+    while (c->phase >= 1.0f) c->phase -= 1.0f;
+    while (c->phase < 0.0f) c->phase += 1.0f;
+    
+    // Wavetable synthesis
+    
+    temp = c->size * c->phase;
+    intPart = (int)temp;
+    fracPart = temp - (float)intPart;
+    samp0 = c->sine[intPart];
+    if (++intPart >= c->size) intPart = 0;
+    samp1 = c->sine[intPart];
+    
+    return (samp0 + (samp1 - samp0) * fracPart);
+    
+}
+
+void     tSineSampleRateChanged (tSine* const cy)
+{
+    _tSine* c = *cy;
+    
+    c->inc = c->freq * leaf.invSampleRate;
+}
+
+//========================================================================
 /* Triangle */
 void   tTriangle_init(tTriangle* const cy)
 {
--- a/LEAF/Src/leaf-sampling.c
+++ b/LEAF/Src/leaf-sampling.c
@@ -685,6 +685,13 @@
     p->targetend = -1;
 }
 
+void    tSampler_setLength    (tSampler* const sp, int32_t length)
+{
+    _tSampler* p = *sp;
+    if (length == 0) length = 1;
+    tSampler_setEnd(sp, p->start + length);
+}
+
 void tSampler_setRate      (tSampler* const sp, float rate)
 {
     _tSampler* p = *sp;
--- a/LEAF_JUCEPlugin/Source/MyTest.cpp
+++ b/LEAF_JUCEPlugin/Source/MyTest.cpp
@@ -16,23 +16,8 @@
 static void leaf_pool_dump(void);
 static void run_pool_test(void);
 
-tNoise noise;
-tSVF bp1;
-tSVF bp2;
-tFormantShifter fs;
+tSine sine;
 
-tSampler samp;
-tBuffer buff;
-tEnvelope env;
-
-tAutotune at;
-
-tTriangle tri;
-
-tMinBLEP minblep;
-
-tPhasor phasor;
-
 float gain;
 float freq;
 float dtime;
@@ -48,68 +33,16 @@
 void    LEAFTest_init            (float sampleRate, int blockSize)
 {
     LEAF_init(sampleRate, blockSize, memory, MSIZE, &getRandomFloat);
-    
-    tTriangle_init(&tri);
-    
-    tMinBLEP_init(&minblep);
-    
-    tPhasor_init(&phasor);
-//    tNoise_init(&noise, WhiteNoise);
-//
-//    tAutotune_init(&at, 1, 1024, 512);
-    
-//    tSVF_init(&bp1, SVFTypeBandpass, 100, 4.0f);
-//    tSVF_init(&bp2, SVFTypeBandpass, 1000, 4.0f);
-//
-//    tFormantShifter_init(&fs, 20);
-//
-//    // Init and set record
-//    tBuffer_init (&buff, leaf.sampleRate); // init, 1 second buffer
-//    tBuffer_setRecordMode (&buff, RecordOneShot); // RecordOneShot records once through
-//
-//    // Init and set play
-//    tSampler_init (&samp, &buff); // init, give address of record buffer
-//    tSampler_setMode (&samp, PlayLoop); //set in Loop Mode
-//    tSampler_setRate(&samp, 1.763f); // Rate of 1.0
-}
 
-inline double getSawFall(double angle) {
     
-    angle = fmod(angle + double_Pi, 2*double_Pi); // shift x
-    double sample = angle/double_Pi - double(1); // computer as remainder
-    
-    return sample;
-    
+    tSine_init(&sine, 1000);
+    tSine_setFreq(&sine, 400);
 }
 
 float   LEAFTest_tick            (float input)
 {
-//    float sample = tNoise_tick(&noise);
-//    sample *= 0.5f;
-//    float b = tSVF_tick(&bp1, sample);
-//    b += tSVF_tick(&bp2, sample);
-//
-//    return (tFormantShifter_tick(&fs, input));
-//
-//    tBuffer_tick(&buff, input);
     
-//    return tSampler_tick(&samp);
-    
-//    tAutotune_setFreq(&at, 440.0f, 0);
-    
-//    return tAutotune_tick(&at, input)[0];
-    
-    tPhasor_setFreq(&phasor, y);
-    
-    
-    float sample = tPhasor_tick(&phasor) * 2.0f - 1.0f;
-    
-    if (phasor->phaseDidReset)
-    {
-        tMinBLEP_addBLEP(&minblep, 0.0f, 2, 0.0f);
-    }
-    
-    return tMinBLEP_tick(&minblep, sample);
+    return tSine_tick(&sine);
 }
 
 int firstFrame = 1;