shithub: leaf

Download patch

ref: 6da3c6430ec4b9e8998dc385c484e9bca9523120
parent: e4e7610e5af4de4fe392fe5353f61bdafb33d7c1
author: Matthew Wang <mjw7@princeton.edu>
date: Tue Jan 26 12:40:07 EST 2021

twaveset adjustments:

--- a/TestPlugin/Source/MyTest.cpp
+++ b/TestPlugin/Source/MyTest.cpp
@@ -76,13 +76,15 @@
     tPhasor_init(&phasor, &leaf);
     tPhasor_setFreq(&phasor, 220.f);
     
-    float const* set[4];
+    float* set[4];
     set[0] = __leaf_table_sinewave;
     set[1] = __leaf_table_triangle[0];
     set[2] = __leaf_table_squarewave[0];
     set[3] = __leaf_table_sawtooth[0];
+    int sizes[4];
+    for (int i = 0; i < 4; i++) sizes[i] = 2048;
     
-    tWaveset_init(&ws, set, 4, 2048, 10000.f, &leaf);
+    tWaveset_init(&ws, set, 4, sizes, 10000.f, &leaf);
     tWaveset_setIndexGain(&ws, 0, -1.0f);
     
     lastLoadedAudioSize = 0;
@@ -129,7 +131,6 @@
     val = getSliderValue("slider3");
 //    tRetune_setPitchFactor(&retune, val * 3.0f + 0.5f, 2);
         
-    
     if (lastLoadedAudioSize < loadedAudio.size())
     {
         int i = (loadedAudio.size() - 1) % numWavetables;
--- a/TestPlugin/Source/UIComponent.h
+++ b/TestPlugin/Source/UIComponent.h
@@ -71,7 +71,6 @@
     OwnedArray<ComboBox>    comboBoxes;
     
     juce::AudioFormatManager formatManager;
-    std::unique_ptr<juce::AudioFormatReaderSource> readerSource;
     
     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UIComponent)
 };
--- a/leaf/Inc/leaf-oscillators.h
+++ b/leaf/Inc/leaf-oscillators.h
@@ -969,6 +969,7 @@
         float baseFreq, invBaseFreq;
         float inc, freq;
         float phase;
+        float phaseOffset;
         
         int oct;
         float w;
@@ -979,13 +980,14 @@
     
     typedef _tWavetable* tWavetable;
     
-    void    tWavetable_init(tWavetable* const osc, const float* table, int size, float maxFreq, LEAF* const leaf);
-    void    tWavetable_initToPool(tWavetable* const osc, const float* table, int size, float maxFreq, tMempool* const mempool);
+    void    tWavetable_init(tWavetable* const osc, float* table, int size, float maxFreq, LEAF* const leaf);
+    void    tWavetable_initToPool(tWavetable* const osc, float* table, int size, float maxFreq, tMempool* const mempool);
     void    tWavetable_free(tWavetable* const osc);
     
     float   tWavetable_tick(tWavetable* const osc);
     void    tWavetable_setFreq(tWavetable* const osc, float freq);
     void    tWavetable_setAntiAliasing(tWavetable* const osc, float aa);
+    void    tWavetable_setPhaseOffset(tWavetable* const osc, float phase);
     
     //==============================================================================
     
@@ -1050,8 +1052,8 @@
     
     typedef _tCompactWavetable* tCompactWavetable;
     
-    void    tCompactWavetable_init(tCompactWavetable* const osc, const float* table, int size, float maxFreq, LEAF* const leaf);
-    void    tCompactWavetable_initToPool(tCompactWavetable* const osc, const float* table, int size, float maxFreq, tMempool* const mempool);
+    void    tCompactWavetable_init(tCompactWavetable* const osc, float* table, int size, float maxFreq, LEAF* const leaf);
+    void    tCompactWavetable_initToPool(tCompactWavetable* const osc, float* table, int size, float maxFreq, tMempool* const mempool);
     void    tCompactWavetable_free(tCompactWavetable* const osc);
     
     float   tCompactWavetable_tick(tCompactWavetable* const osc);
@@ -1111,13 +1113,14 @@
         tWavetable* wt;
         int n;
         float* g;
+        float* p;
         float index;
     } _tWaveset;
     
     typedef _tWaveset* tWaveset;
     
-    void    tWaveset_init(tWaveset* const osc, const float** tables, int n, int size, float maxFreq, LEAF* const leaf);
-    void    tWaveset_initToPool(tWaveset* const osc, const float** tables, int n, int size, float maxFreq, tMempool* const mempool);
+    void    tWaveset_init(tWaveset* const osc, float** tables, int n, int* sizes, float maxFreq, LEAF* const leaf);
+    void    tWaveset_initToPool(tWaveset* const osc, float** tables, int n, int* sizes, float maxFreq, tMempool* const mempool);
     void    tWaveset_free(tWaveset* const osc);
     
     float   tWaveset_tick(tWaveset* const osc);
@@ -1125,6 +1128,7 @@
     void    tWaveset_setAntiAliasing(tWaveset* const osc, float aa);
     void    tWaveset_setIndex(tWaveset* const osc, float index);
     void    tWaveset_setIndexGain(tWaveset* const osc, int i, float gain);
+    void    tWaveset_setIndexPhase(tWaveset* const osc, int i, float phase);
 
     
 #ifdef __cplusplus
--- a/leaf/Src/leaf-oscillators.c
+++ b/leaf/Src/leaf-oscillators.c
@@ -1704,12 +1704,12 @@
     c->inc -= (int)c->inc;
 }
 
-void tWavetable_init(tWavetable* const cy, const float* table, int size, float maxFreq, LEAF* const leaf)
+void tWavetable_init(tWavetable* const cy, float* table, int size, float maxFreq, LEAF* const leaf)
 {
     tWavetable_initToPool(cy, table, size, maxFreq, &leaf->mempool);
 }
 
-void tWavetable_initToPool(tWavetable* const cy, const float* table, int size, float maxFreq, tMempool* const mp)
+void tWavetable_initToPool(tWavetable* const cy, float* table, int size, float maxFreq, tMempool* const mp)
 {
     _tMempool* m = *mp;
     _tWavetable* c = *cy = (_tWavetable*) mpool_alloc(sizeof(_tWavetable), m);
@@ -1797,11 +1797,11 @@
     
     // Phasor increment
     c->phase += c->inc;
-    if (c->phase >= 1.0f) c->phase -= 1.0f;
-    if (c->phase < 0.0f) c->phase += 1.0f;
+    if (c->phase + c->phaseOffset >= 1.0f) c->phase -= 1.0f;
+    if (c->phase + c->phaseOffset < 0.0f) c->phase += 1.0f;
     
     // Wavetable synthesis
-    temp = c->size * c->phase;
+    temp = c->size * (c->phase + c->phaseOffset);
     
     idx = (int)temp;
     frac = temp - (float)idx;
@@ -1849,19 +1849,23 @@
 void tWavetable_setAntiAliasing(tWavetable* const cy, float aa)
 {
     _tWavetable* c = *cy;
-    
     c->aa = aa;
     tWavetable_setFreq(cy, c->freq);
 }
 
+void tWavetable_setPhaseOffset(tWavetable* const cy, float phase)
+{
+    _tWavetable* c = *cy;
+    c->phaseOffset = phase - (int)phase;
+}
 
 
-void tCompactWavetable_init(tCompactWavetable* const cy, const float* table, int size, float maxFreq, LEAF* const leaf)
+void tCompactWavetable_init(tCompactWavetable* const cy, float* table, int size, float maxFreq, LEAF* const leaf)
 {
     tCompactWavetable_initToPool(cy, table, size, maxFreq, &leaf->mempool);
 }
 
-void tCompactWavetable_initToPool(tCompactWavetable* const cy, const float* table, int size, float maxFreq, tMempool* const mp)
+void tCompactWavetable_initToPool(tCompactWavetable* const cy, float* table, int size, float maxFreq, tMempool* const mp)
 {
     _tMempool* m = *mp;
     _tCompactWavetable* c = *cy = (_tCompactWavetable*) mpool_alloc(sizeof(_tCompactWavetable), m);
@@ -2007,27 +2011,38 @@
 //================================================================================================
 //================================================================================================
 
-void    tWaveset_init(tWaveset* const cy, const float** tables, int n, int size, float maxFreq, LEAF* const leaf)
+void    tWaveset_init(tWaveset* const cy, float** tables, int n, int* sizes, float maxFreq, LEAF* const leaf)
 {
-    tWaveset_initToPool(cy, tables, n, size, maxFreq, &leaf->mempool);
+    tWaveset_initToPool(cy, tables, n, sizes, maxFreq, &leaf->mempool);
 }
 
-void    tWaveset_initToPool(tWaveset* const cy, const float** tables, int n, int size, float maxFreq, tMempool* const mp)
+void    tWaveset_initToPool(tWaveset* const cy, float** tables, int n, int* sizes, float maxFreq, tMempool* const mp)
 {
     _tMempool* m = *mp;
     _tWaveset* c = *cy = (_tWaveset*) mpool_alloc(sizeof(_tWaveset), m);
     c->mempool = m;
     
-    c->n = n;
+    c->n = 0;
+    for (int t = 0; t < n; ++t)
+    {
+        if (sizes[t] > 0) c->n++;
+    }
     
-    c->g = (float*) mpool_alloc(sizeof(tWavetable) * c->n, m);
     c->wt = (tWavetable*) mpool_alloc(sizeof(tWavetable) * c->n, m);
-    for (int i = 0; i < c->n; ++i)
+    
+    int i = 0;
+    for (int t = 0; t < n; ++t)
     {
-        c->g[i] = 1.0f;
-        tWavetable_initToPool(&c->wt[i], tables[i], size, maxFreq, mp);
+        if (sizes[t] > 0)
+        {
+            tWavetable_initToPool(&c->wt[i], tables[t], sizes[t], maxFreq, mp);
+            i++;
+        }
     }
     
+    c->g = (float*) mpool_alloc(sizeof(tWavetable) * c->n, m);
+    for (int i = 0; i < c->n; ++i)  c->g[i] = 1.0f;
+    
     c->index = 0.0f;
 }
 
@@ -2039,6 +2054,7 @@
     {
         tWavetable_free(&c->wt[i]);
     }
+    mpool_free((char*)c->g, c->mempool);
     mpool_free((char*)c->wt, c->mempool);
     mpool_free((char*)c, c->mempool);
 }
@@ -2047,14 +2063,14 @@
 {
     _tWaveset* c = *cy;
     
-    float p = c->index * (c->n - 1);
+    float f = c->index * (c->n - 1);
     
-    int o1 = (int)p;
+    int o1 = (int)f;
     int o2 = o1 + 1;
     if (c->index >= 1.0f) o2 = o1;
-    float mix = p - o1;
+    float mix = f - o1;
     
-    float s0, s1, s2;
+    float s0 = 0.f, s1 = 0.f, s2 = 0.f;
     for (int i = 0; i < c->n; ++i)
     {
         s0 = tWavetable_tick(&c->wt[i]) * c->g[i];
@@ -2093,5 +2109,13 @@
 void    tWaveset_setIndexGain(tWaveset* const cy, int i, float gain)
 {
     _tWaveset* c = *cy;
+    if (i >= c->n) return;
     c->g[i] = gain;
+}
+
+void    tWaveset_setIndexPhase(tWaveset* const cy, int i, float phase)
+{
+    _tWaveset* c = *cy;
+    if (i >= c->n) return;
+    tWavetable_setPhaseOffset(&c->wt[i], phase);
 }