ref: bec88f14df1e254456db2eaaefa414c0859e40aa
parent: 293326e64ab3176edc2d04d1b0f103d168fb4b80
author: Matthew Wang <mjw7@princeton.edu>
date: Mon Mar 16 12:29:53 EDT 2020
remove branching from tri, square, and saw ticks
--- a/LEAF/Inc/leaf-oscillators.h
+++ b/LEAF/Inc/leaf-oscillators.h
@@ -15,6 +15,7 @@
#include "leaf-math.h"
#include "leaf-mempool.h"
+#include "leaf-tables.h"
#include "leaf-filters.h"
/*!
@@ -31,7 +32,6 @@
// Underlying phasor
float phase;
float inc,freq;
-
} _tCycle;
typedef _tCycle* tCycle;
@@ -99,7 +99,8 @@
// Underlying phasor
float phase;
float inc,freq;
-
+ TableName oct;
+ float w;
} _tTriangle;
typedef _tTriangle* tTriangle;
@@ -154,7 +155,7 @@
@param osc A pointer to the relevant tTriangle.
@param freq The frequency to set the oscillator to.
*/
- int tTriangle_setFreq (tTriangle* const osc, float freq);
+ void tTriangle_setFreq (tTriangle* const osc, float freq);
/*! @} */
@@ -166,7 +167,8 @@
// Underlying phasor
float phase;
float inc,freq;
-
+ TableName oct;
+ float w;
} _tSquare;
typedef _tSquare* tSquare;
@@ -221,7 +223,7 @@
@param osc A pointer to the relevant tSquare.
@param freq The frequency to set the oscillator to.
*/
- int tSquare_setFreq (tSquare* const osc, float freq);
+ void tSquare_setFreq (tSquare* const osc, float freq);
/*! @} */
@@ -233,7 +235,8 @@
// Underlying phasor
float phase;
float inc,freq;
-
+ TableName oct;
+ float w;
} _tSawtooth;
typedef _tSawtooth* tSawtooth;
@@ -288,7 +291,7 @@
@param osc A pointer to the relevant tSawtooth.
@param freq The frequency to set the oscillator to.
*/
- int tSawtooth_setFreq (tSawtooth* const osc, float freq);
+ void tSawtooth_setFreq (tSawtooth* const osc, float freq);
/*! @} */
@@ -354,7 +357,7 @@
@param osc A pointer to the relevant tPhasor.
@param freq The frequency to set the oscillator to.
*/
- int tPhasor_setFreq (tPhasor* const osc, float freq);
+ void tPhasor_setFreq (tPhasor* const osc, float freq);
/*! @} */
--- a/LEAF/Src/leaf-oscillators.c
+++ b/LEAF/Src/leaf-oscillators.c
@@ -6,13 +6,11 @@
#if _WIN32 || _WIN64
-#include "..\Inc\leaf-tables.h"
#include "..\Inc\leaf-oscillators.h"
#include "..\leaf.h"
#else
-#include "../Inc/leaf-tables.h"
#include "../Inc/leaf-oscillators.h"
#include "../leaf.h"
@@ -51,7 +49,10 @@
{
_tCycle* c = *cy;
- c->freq = freq;
+ 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;
}
@@ -109,6 +110,7 @@
c->inc = 0.0f;
c->phase = 0.0f;
+ tTriangle_setFreq(cy, 220);
}
void tTriangle_freeFromPool (tTriangle* const cy, tMempool* const mp)
@@ -119,16 +121,22 @@
mpool_free(c, m);
}
-int tTriangle_setFreq(tTriangle* const cy, float freq)
+void tTriangle_setFreq(tTriangle* const cy, float freq)
{
_tTriangle* c = *cy;
- if (freq < 0.0f) freq = 0.0f;
+ if (freq < 0.0f) c->freq = 0.0f;
+ else if (freq > 20480.0f) c->freq = 20480.0f;
+ else c->freq = freq;
- c->freq = freq;
- c->inc = freq * leaf.invSampleRate;
+ c->inc = c->freq * leaf.invSampleRate;
- return 0;
+ c->w = c->freq * INV_20;
+ for (c->oct = 0; c->w > 2.0f; c->oct++)
+ {
+ c->w = 0.5f * c->w;
+ }
+ c->w = 2.0f - c->w;
}
@@ -141,71 +149,13 @@
while (c->phase >= 1.0f) c->phase -= 1.0f;
float out = 0.0f;
- float w;
int idx = (int)(c->phase * TRI_TABLE_SIZE);
// Wavetable synthesis
+ out = __leaf_table_triangle[c->oct+1][idx] +
+ (__leaf_table_triangle[c->oct][idx] - __leaf_table_triangle[c->oct+1][idx]) * c->w;
- if (c->freq <= 20.0f)
- {
- out = __leaf_table_triangle[T20][idx];
- }
- else if (c->freq <= 40.0f)
- {
- w = ((40.0f - c->freq) * INV_20);
- out = (__leaf_table_triangle[T20][idx] * w) + (__leaf_table_triangle[T40][idx] * (1.0f - w));
- }
- else if (c->freq <= 80.0f)
- {
- w = ((80.0f - c->freq) * INV_40);
- out = (__leaf_table_triangle[T40][idx] * w) + (__leaf_table_triangle[T80][idx] * (1.0f - w));
- }
- else if (c->freq <= 160.0f)
- {
- w = ((160.0f - c->freq) * INV_80);
- out = (__leaf_table_triangle[T80][idx] * w) + (__leaf_table_triangle[T160][idx] * (1.0f - w));
- }
- else if (c->freq <= 320.0f)
- {
- w = ((320.0f - c->freq) * INV_160);
- out = (__leaf_table_triangle[T160][idx] * w) + (__leaf_table_triangle[T320][idx] * (1.0f - w));
- }
- else if (c->freq <= 640.0f)
- {
- w = ((640.0f - c->freq) * INV_320);
- out = (__leaf_table_triangle[T320][idx] * w) + (__leaf_table_triangle[T640][idx] * (1.0f - w));
- }
- else if (c->freq <= 1280.0f)
- {
- w = ((1280.0f - c->freq) * INV_640);
- out = (__leaf_table_triangle[T640][idx] * w) + (__leaf_table_triangle[T1280][idx] * (1.0f - w));
- }
- else if (c->freq <= 2560.0f)
- {
- w = ((2560.0f - c->freq) * INV_1280);
- out = (__leaf_table_triangle[T1280][idx] * w) + (__leaf_table_triangle[T2560][idx] * (1.0f - w));
- }
- else if (c->freq <= 5120.0f)
- {
- w = ((5120.0f - c->freq) * INV_2560);
- out = (__leaf_table_triangle[T2560][idx] * w) + (__leaf_table_triangle[T5120][idx] * (1.0f - w));
- }
- else if (c->freq <= 10240.0f)
- {
- w = ((10240.0f - c->freq) * INV_5120);
- out = (__leaf_table_triangle[T5120][idx] * w) + (__leaf_table_triangle[T10240][idx] * (1.0f - w));
- }
- else if (c->freq <= 20480.0f)
- {
- w = ((20480.0f - c->freq) * INV_10240);
- out = (__leaf_table_triangle[T10240][idx] * w) + (__leaf_table_triangle[T20480][idx] * (1.0f - w));
- }
- else
- {
- out = __leaf_table_triangle[T20480][idx];
- }
-
return out;
}
@@ -235,6 +185,7 @@
c->inc = 0.0f;
c->phase = 0.0f;
+ tSquare_setFreq(cy, 220);
}
void tSquare_freeFromPool(tSquare* const cy, tMempool* const mp)
@@ -245,16 +196,22 @@
mpool_free(c, m);
}
-int tSquare_setFreq(tSquare* const cy, float freq)
+void tSquare_setFreq(tSquare* const cy, float freq)
{
_tSquare* c = *cy;
- if (freq < 0.0f) freq = 0.0f;
+ if (freq < 0.0f) c->freq = 0.0f;
+ else if (freq > 20480.0f) c->freq = 20480.0f;
+ else c->freq = freq;
- c->freq = freq;
- c->inc = freq * leaf.invSampleRate;
+ c->inc = c->freq * leaf.invSampleRate;
- return 0;
+ c->w = c->freq * INV_20;
+ for (c->oct = 0; c->w > 2.0f; c->oct++)
+ {
+ c->w = 0.5f * c->w;
+ }
+ c->w = 2.0f - c->w;
}
float tSquare_tick(tSquare* const cy)
@@ -266,70 +223,13 @@
while (c->phase >= 1.0f) c->phase -= 1.0f;
float out = 0.0f;
- float w = 0.0f;
+
int idx = (int)(c->phase * TRI_TABLE_SIZE);
// Wavetable synthesis
+ out = __leaf_table_squarewave[c->oct+1][idx] +
+ (__leaf_table_squarewave[c->oct][idx] - __leaf_table_squarewave[c->oct+1][idx]) * c->w;
- if (c->freq <= 20.0f)
- {
- out = __leaf_table_squarewave[T20][idx];
- }
- else if (c->freq <= 40.0f)
- {
- w = ((40.0f - c->freq) * INV_20);
- out = (__leaf_table_squarewave[T20][idx] * w) + (__leaf_table_squarewave[T40][idx] * (1.0f - w));
- }
- else if (c->freq <= 80.0f)
- {
- w = ((80.0f - c->freq) * INV_40);
- out = (__leaf_table_squarewave[T40][idx] * w) + (__leaf_table_squarewave[T80][idx] * (1.0f - w));
- }
- else if (c->freq <= 160.0f)
- {
- w = ((160.0f - c->freq) * INV_80);
- out = (__leaf_table_squarewave[T80][idx] * w) + (__leaf_table_squarewave[T160][idx] * (1.0f - w));
- }
- else if (c->freq <= 320.0f)
- {
- w = ((320.0f - c->freq) * INV_160);
- out = (__leaf_table_squarewave[T160][idx] * w) + (__leaf_table_squarewave[T320][idx] * (1.0f - w));
- }
- else if (c->freq <= 640.0f)
- {
- w = ((640.0f - c->freq) * INV_320);
- out = (__leaf_table_squarewave[T320][idx] * w) + (__leaf_table_squarewave[T640][idx] * (1.0f - w));
- }
- else if (c->freq <= 1280.0f)
- {
- w = ((1280.0f - c->freq) * INV_640);
- out = (__leaf_table_squarewave[T640][idx] * w) + (__leaf_table_squarewave[T1280][idx] * (1.0f - w));
- }
- else if (c->freq <= 2560.0f)
- {
- w = ((2560.0f - c->freq) * INV_1280);
- out = (__leaf_table_squarewave[T1280][idx] * w) + (__leaf_table_squarewave[T2560][idx] * (1.0f - w));
- }
- else if (c->freq <= 5120.0f)
- {
- w = ((5120.0f - c->freq) * INV_2560);
- out = (__leaf_table_squarewave[T2560][idx] * w) + (__leaf_table_squarewave[T5120][idx] * (1.0f - w));
- }
- else if (c->freq <= 10240.0f)
- {
- w = ((10240.0f - c->freq) * INV_5120);
- out = (__leaf_table_squarewave[T5120][idx] * w) + (__leaf_table_squarewave[T10240][idx] * (1.0f - w));
- }
- else if (c->freq <= 20480.0f)
- {
- w = ((20480.0f - c->freq) * INV_10240);
- out = (__leaf_table_squarewave[T10240][idx] * w) + (__leaf_table_squarewave[T20480][idx] * (1.0f - w));
- }
- else
- {
- out = __leaf_table_squarewave[T20480][idx];
- }
-
return out;
}
@@ -359,6 +259,7 @@
c->inc = 0.0f;
c->phase = 0.0f;
+ tSawtooth_setFreq(cy, 220);
}
void tSawtooth_freeFromPool (tSawtooth* const cy, tMempool* const mp)
@@ -369,16 +270,22 @@
mpool_free(c, m);
}
-int tSawtooth_setFreq(tSawtooth* const cy, float freq)
+void tSawtooth_setFreq(tSawtooth* const cy, float freq)
{
_tSawtooth* c = *cy;
- if (freq < 0.0f) freq = 0.0f;
+ if (freq < 0.0f) c->freq = 0.0f;
+ else if (freq > 20480.0f) c->freq = 20480.0f;
+ else c->freq = freq;
- c->freq = freq;
- c->inc = freq * leaf.invSampleRate;
+ c->inc = c->freq * leaf.invSampleRate;
- return 0;
+ c->w = c->freq * INV_20;
+ for (c->oct = 0; c->w > 2.0f; c->oct++)
+ {
+ c->w = 0.5f * c->w;
+ }
+ c->w = 2.0f - c->w;
}
float tSawtooth_tick(tSawtooth* const cy)
@@ -390,71 +297,13 @@
while (c->phase >= 1.0f) c->phase -= 1.0f;
float out = 0.0f;
- float w;
int idx = (int)(c->phase * TRI_TABLE_SIZE);
// Wavetable synthesis
+ out = __leaf_table_sawtooth[c->oct+1][idx] +
+ (__leaf_table_sawtooth[c->oct][idx] - __leaf_table_sawtooth[c->oct+1][idx]) * c->w;
- if (c->freq <= 20.0f)
- {
- out = __leaf_table_sawtooth[T20][idx];
- }
- else if (c->freq <= 40.0f)
- {
- w = ((40.0f - c->freq) * INV_20);
- out = (__leaf_table_sawtooth[T20][idx] * w) + (__leaf_table_sawtooth[T40][idx] * (1.0f - w));
- }
- else if (c->freq <= 80.0f)
- {
- w = ((80.0f - c->freq) * INV_40);
- out = (__leaf_table_sawtooth[T40][idx] * w) + (__leaf_table_sawtooth[T80][idx] * (1.0f - w));
- }
- else if (c->freq <= 160.0f)
- {
- w = ((160.0f - c->freq) * INV_80);
- out = (__leaf_table_sawtooth[T80][idx] * w) + (__leaf_table_sawtooth[T160][idx] * (1.0f - w));
- }
- else if (c->freq <= 320.0f)
- {
- w = ((320.0f - c->freq) * INV_160);
- out = (__leaf_table_sawtooth[T160][idx] * w) + (__leaf_table_sawtooth[T320][idx] * (1.0f - w));
- }
- else if (c->freq <= 640.0f)
- {
- w = ((640.0f - c->freq) * INV_320);
- out = (__leaf_table_sawtooth[T320][idx] * w) + (__leaf_table_sawtooth[T640][idx] * (1.0f - w));
- }
- else if (c->freq <= 1280.0f)
- {
- w = ((1280.0f - c->freq) * INV_640);
- out = (__leaf_table_sawtooth[T640][idx] * w) + (__leaf_table_sawtooth[T1280][idx] * (1.0f - w));
- }
- else if (c->freq <= 2560.0f)
- {
- w = ((2560.0f - c->freq) * INV_1280);
- out = (__leaf_table_sawtooth[T1280][idx] * w) + (__leaf_table_sawtooth[T2560][idx] * (1.0f - w));
- }
- else if (c->freq <= 5120.0f)
- {
- w = ((5120.0f - c->freq) * INV_2560);
- out = (__leaf_table_sawtooth[T2560][idx] * w) + (__leaf_table_sawtooth[T5120][idx] * (1.0f - w));
- }
- else if (c->freq <= 10240.0f)
- {
- w = ((10240.0f - c->freq) * INV_5120);
- out = (__leaf_table_sawtooth[T5120][idx] * w) + (__leaf_table_sawtooth[T10240][idx] * (1.0f - w));
- }
- else if (c->freq <= 20480.0f)
- {
- w = ((20480.0f - c->freq) * INV_10240);
- out = (__leaf_table_sawtooth[T10240][idx] * w) + (__leaf_table_sawtooth[T20480][idx] * (1.0f - w));
- }
- else
- {
- out = __leaf_table_sawtooth[T20480][idx];
- }
-
return out;
}
@@ -501,16 +350,15 @@
mpool_free(p, m);
}
-int tPhasor_setFreq(tPhasor* const ph, float freq)
+void tPhasor_setFreq(tPhasor* const ph, float freq)
{
_tPhasor* p = *ph;
- if (freq < 0.0f) freq = 0.0f;
+ if (freq < 0.0f) p->freq = 0.0f;
+ else if (freq > 20480.0f) p->freq = 20480.0f;
+ else p->freq = freq;
- p->freq = freq;
p->inc = freq * leaf.invSampleRate;
-
- return 0;
}
float tPhasor_tick(tPhasor* const ph)
--- a/LEAF_JUCEPlugin/Source/MyTest.cpp
+++ b/LEAF_JUCEPlugin/Source/MyTest.cpp
@@ -27,6 +27,8 @@
tAutotune at;
+tTriangle tri;
+
float gain;
float freq;
float dtime;
@@ -43,9 +45,11 @@
{
LEAF_init(sampleRate, blockSize, memory, MSIZE, &getRandomFloat);
- tNoise_init(&noise, WhiteNoise);
+ tTriangle_init(&tri);
- tAutotune_init(&at, 1, 1024, 512);
+// tNoise_init(&noise, WhiteNoise);
+//
+// tAutotune_init(&at, 1, 1024, 512);
// tSVF_init(&bp1, SVFTypeBandpass, 100, 4.0f);
// tSVF_init(&bp2, SVFTypeBandpass, 1000, 4.0f);
@@ -75,9 +79,12 @@
// return tSampler_tick(&samp);
- tAutotune_setFreq(&at, 440.0f, 0);
+// tAutotune_setFreq(&at, 440.0f, 0);
- return tAutotune_tick(&at, input)[0];
+// return tAutotune_tick(&at, input)[0];
+
+ tTriangle_setFreq(&tri, x);
+ return tTriangle_tick(&tri);
}
int firstFrame = 1;
@@ -93,7 +100,7 @@
float val = getSliderValue("mod freq");
- x = val * 3.5f + 0.5f;
+ x = val * -40000.0f + 20000;
// a = val * tBuffer_getBufferLength(&buff);