History

Add new LFOs

Davis Polito committed 4 years ago

Commit 1bfe6ff1 parent c353a6b2 files · +298 −6Browse files

Unified · Split

2 files
  1. leaf/Inc/leaf-oscillators.h +98 −1
  2. leaf/Src/leaf-oscillators.c +200 −5
leaf/Inc/leaf-oscillators.h+98−1
@@ -1284,7 +1284,7 @@ extern "C" { · 1283 unmodified lines
1284 float freq;1284 float freq;
1285 int32_t mask;1285 int32_t mask;
1286 uint8_t phaseDidReset;1286 uint8_t phaseDidReset;
1287 int32_t invSampleRateTimesTwoTo32;1287 float invSampleRateTimesTwoTo32;
1288 } _tIntPhasor;1288 } _tIntPhasor;
1289 1289
1290 typedef _tIntPhasor* tIntPhasor;1290 typedef _tIntPhasor* tIntPhasor;
@@ -1350,7 +1350,104 @@ extern "C" { · 59 unmodified lines
1350 void tSquareLFO_setFreq (tSquareLFO* const osc, float freq);1350 void tSquareLFO_setFreq (tSquareLFO* const osc, float freq);
1351 void tSquareLFO_setSampleRate (tSquareLFO* const osc, float sr);1351 void tSquareLFO_setSampleRate (tSquareLFO* const osc, float sr);
1352 void tSquareLFO_setPulseWidth (tSquareLFO* const cy, float pw);1352 void tSquareLFO_setPulseWidth (tSquareLFO* const cy, float pw);
1353 void tSquareLFO_setPhase (tSquareLFO* const cy, float phase);
1354
1355 typedef struct _tSawSquareLFO
1356 {
1357 tMempool mempool;
1358 float shape;
1359 tIntPhasor saw;
1360 tSquareLFO square;
1361 } _tSawSquareLFO;
1362
1363 typedef _tSawSquareLFO* tSawSquareLFO;
1364
1365 void tSawSquareLFO_init (tSawSquareLFO* const osc, LEAF* const leaf);
1366 void tSawSquareLFO_initToPool (tSawSquareLFO* const osc, tMempool* const);
1367 void tSawSquareLFO_free (tSawSquareLFO* const osc);
1368
1369 float tSawSquareLFO_tick (tSawSquareLFO* const osc);
1370 void tSawSquareLFO_setFreq (tSawSquareLFO* const osc, float freq);
1371 void tSawSquareLFO_setSampleRate (tSawSquareLFO* const osc, float sr);
1372 void tSawSquareLFO_setPhase (tSawSquareLFO* const cy, float phase);
1373 void tSawSquareLFO_setShape (tSawSquareLFO* const cy, float shape);
1374
1375 //==============================================================================
1376 /*!
1377 @defgroup tTriLFO tTriLFO
1378 @ingroup oscillators
1379 @brief Aliasing phasor.
1380 @{
1381
1382 @fn void tTriLFO_init (tTriLFO* const osc, LEAF* const leaf)
1383 @brief Initialize a tTriLFO to the default mempool of a LEAF instance.
1384 @param osc A pointer to the tTriLFO to initialize.
1385 @param leaf A pointer to the leaf instance.
1386
1387 @fn void tTriLFO_initToPool (tTriLFO* const osc, tMempool* const)
1388 @brief Initialize a tTriLFO to a specified mempool.
1389 @param osc A pointer to the tTriLFO to initialize.
1390 @param mempool A pointer to the tMempool to use.
1391
1392 @fn void tTriLFO_free (tTriLFO* const osc)
1393 @brief Free a tTriLFO from its mempool.
1394 @param osc A pointer to the tTriLFO to free.
1395
1396 @fn float tTriLFO_tick (tTriLFO* const osc)
1397 @brief
1398 @param osc A pointer to the relevant tTriLFO.
1399
1400 @fn void tTriLFO_setFreq (tTriLFO* const osc, float freq)
1401 @brief
1402 @param osc A pointer to the relevant tTriLFO.
1403 
1404 @} */
1353 1405
1406 typedef struct _tTriLFO
1407 {
1408
1409 tMempool mempool;
1410 uint32_t phase;
1411 uint32_t inc;
1412 uint32_t prevPhase;
1413 float freq;
1414 int32_t mask;
1415 uint8_t phaseDidReset;
1416 uint32_t invSampleRate;
1417 float invSampleRateTimesTwoTo32;
1418 } _tTriLFO;
1419
1420 typedef _tTriLFO* tTriLFO;
1421
1422 void tTriLFO_init (tTriLFO* const osc, LEAF* const leaf);
1423 void tTriLFO_initToPool (tTriLFO* const osc, tMempool* const);
1424 void tTriLFO_free (tTriLFO* const osc);
1425
1426 float tTriLFO_tick (tTriLFO* const osc);
1427 void tTriLFO_setFreq (tTriLFO* const osc, float freq);
1428 void tTriLFO_setSampleRate (tTriLFO* const osc, float sr);
1429
1430 void tTriLFO_setPhase(tTriLFO* const cy, float phase);
1431
1432 typedef struct _tSineTriLFO
1433 {
1434 tMempool mempool;
1435 float shape;
1436 tTriLFO tri;
1437 tCycle sine;
1438 } _tSineTriLFO;
1439
1440 typedef _tSineTriLFO* tSineTriLFO;
1441
1442 void tSineTriLFO_init (tSineTriLFO* const osc, LEAF* const leaf);
1443 void tSineTriLFO_initToPool (tSineTriLFO* const osc, tMempool* const);
1444 void tSineTriLFO_free (tSineTriLFO* const osc);
1445
1446 float tSineTriLFO_tick (tSineTriLFO* const osc);
1447 void tSineTriLFO_setFreq (tSineTriLFO* const osc, float freq);
1448 void tSineTriLFO_setSampleRate (tSineTriLFO* const osc, float sr);
1449 void tSineTriLFO_setPhase (tSineTriLFO* const cy, float phase);
1450 void tSineTriLFO_setShape (tSineTriLFO* const cy, float shape);
1354#ifdef __cplusplus1451#ifdef __cplusplus
1355}1452}
1356#endif1453#endif
leaf/Src/leaf-oscillators.c+200−5
@@ -2594,8 +2594,6 @@ void tIntPhasor_setSampleRate (tIntPhasor* const cy, float sr) · 2593 unmodified lines
2594void tSquareLFO_init(tSquareLFO* const cy, LEAF* const leaf)2594void tSquareLFO_init(tSquareLFO* const cy, LEAF* const leaf)
2595{2595{
2596 tSquareLFO_initToPool(cy, &leaf->mempool);2596 tSquareLFO_initToPool(cy, &leaf->mempool);
2597 _tSquareLFO* c = *cy;
2598
2599}2597}
2600 2598
2601void tSquareLFO_initToPool (tSquareLFO* const cy, tMempool* const mp)2599void tSquareLFO_initToPool (tSquareLFO* const cy, tMempool* const mp)
@@ -2606,9 +2604,7 @@ void tSquareLFO_initToPool (tSquareLFO* const cy, tMempool* const mp) · 4 unmodified lines
2606 LEAF* leaf = c->mempool->leaf;2604 LEAF* leaf = c->mempool->leaf;
2607 tIntPhasor_initToPool(&c->phasor,mp);2605 tIntPhasor_initToPool(&c->phasor,mp);
2608 tIntPhasor_initToPool(&c->invPhasor,mp); 2606 tIntPhasor_initToPool(&c->invPhasor,mp);
2609 2607 tSquareLFO_setPulseWidth(cy, 0.5);
2610
2611 c->pulsewidth = 50.0f;
2612}2608}
2613 2609
2614void tSquareLFO_free (tSquareLFO* const cy)2610void tSquareLFO_free (tSquareLFO* const cy)
@@ -2654,3 +2650,202 @@ void tSquareLFO_setPulseWidth(tSquareLFO* const cy, float pw) · 39 unmodified lines
2654 //c->delay = c->pulsewidth * INV_TWO_TO_32;2650 //c->delay = c->pulsewidth * INV_TWO_TO_32;
2655 tIntPhasor_setPhase(&c->invPhasor, c->pulsewidth + (c->phasor->phase * INV_TWO_TO_32));2651 tIntPhasor_setPhase(&c->invPhasor, c->pulsewidth + (c->phasor->phase * INV_TWO_TO_32));
2656}2652}
2653
2654void tSquareLFO_setPhase(tSquareLFO* const cy, float phase)
2655{
2656 _tSquareLFO *c = *cy;
2657 tIntPhasor_setPhase(&c->phasor, phase);
2658 tIntPhasor_setPhase(&c->invPhasor, c->pulsewidth + (c->phasor->phase * INV_TWO_TO_32));
2659}
2660
2661void tSawSquareLFO_init (tSawSquareLFO* const cy, LEAF* const leaf)
2662{
2663 tSawSquareLFO_initToPool(cy, &leaf->mempool);
2664 _tSawSquareLFO* c = *cy;
2665}
2666
2667void tSawSquareLFO_initToPool (tSawSquareLFO* const cy, tMempool* const mp)
2668{
2669 _tMempool* m = *mp;
2670 _tSawSquareLFO* c = *cy = (_tSawSquareLFO*) mpool_alloc(sizeof(_tSawSquareLFO), m);
2671 c->mempool = m;
2672 LEAF* leaf = c->mempool->leaf;
2673 tSquareLFO_initToPool(&c->square,mp);
2674 tIntPhasor_initToPool(&c->saw,mp);
2675}
2676void tSawSquareLFO_free (tSawSquareLFO* const cy)
2677{
2678 _tSawSquareLFO* c = *cy;
2679 tIntPhasor_free(&c->saw);
2680 tSquareLFO_free(&c->square);
2681 mpool_free((char*)c, c->mempool);
2682}
2683
2684float tSawSquareLFO_tick (tSawSquareLFO* const cy)
2685{
2686 _tSawSquareLFO* c = *cy;
2687 float a = tIntPhasor_tick(&c->saw);
2688 float b = tSquareLFO_tick(&c->square);
2689 return (1 - c->shape) * a + c->shape * b;
2690}
2691void tSawSquareLFO_setFreq (tSawSquareLFO* const cy, float freq)
2692{
2693 _tSawSquareLFO* c = *cy;
2694 tSquareLFO_setFreq(&c->square, freq);
2695 tIntPhasor_setFreq(&c->saw, freq);
2696}
2697void tSawSquareLFO_setSampleRate (tSawSquareLFO* const cy, float sr)
2698{
2699 _tSawSquareLFO* c = *cy;
2700 tSquareLFO_setSampleRate(&c->square, sr);
2701 tIntPhasor_setSampleRate(&c->saw, sr);
2702}
2703void tSawSquareLFO_setPhase (tSawSquareLFO* const cy, float phase)
2704{
2705 _tSawSquareLFO* c = *cy;
2706 tSquareLFO_setPhase(&c->square, phase);
2707 tIntPhasor_setPhase(&c->saw, phase);
2708}
2709
2710
2711void tSawSquareLFO_setShape (tSawSquareLFO* const cy, float shape)
2712{
2713 _tSawSquareLFO* c = *cy;
2714 c->shape = shape;
2715}
2716
2717
2718
2719
2720///tri
2721void tTriLFO_init(tTriLFO* const cy, LEAF* const leaf)
2722{
2723 tTriLFO_initToPool(cy, &leaf->mempool);
2724}
2725
2726void tTriLFO_initToPool (tTriLFO* const cy, tMempool* const mp)
2727{
2728 _tMempool* m = *mp;
2729 _tTriLFO* c = *cy = (_tTriLFO*) mpool_alloc(sizeof(_tTriLFO), m);
2730 c->mempool = m;
2731 LEAF* leaf = c->mempool->leaf;
2732
2733 c->inc = 0;
2734 c->phase = 0;
2735 c->invSampleRate = leaf->invSampleRate;
2736 c->invSampleRateTimesTwoTo32 = (c->invSampleRate * TWO_TO_32);
2737 c->mask = TRI_TABLE_SIZE - 1;
2738 tTriLFO_setFreq(cy, 220);
2739}
2740
2741void tTriLFO_free (tTriLFO* const cy)
2742{
2743 _tTriLFO* c = *cy;
2744
2745 mpool_free((char*)c, c->mempool);
2746}
2747
2748//need to check bounds and wrap table properly to allow through-zero FM
2749float tTriLFO_tick(tTriLFO* const cy)
2750{
2751 _tTriLFO* c = *cy;
2752 uint32_t idx;
2753 float frac, samp0, samp1;
2754 // Phasor increment
2755 c->phase += c->inc;
2756 // Wavetable synthesis
2757 idx = c->phase >> 21;
2758 uint32_t idx2 = (idx + 1) & c->mask;
2759 uint32_t tempFrac = (c->phase & 2097151);
2760 frac = (float)tempFrac * 0.000000476837386f;// 1/2097151 (2097151 is the 21 bits after the 11 bits that represent the main index)
2761
2762 samp0 = __leaf_table_triangle[0][idx];
2763 samp1 = __leaf_table_triangle[0][idx2];
2764 float oct0 = (samp0 + (samp1 - samp0) * frac);
2765
2766
2767 return oct0;
2768}
2769
2770void tTriLFO_setFreq(tTriLFO* const cy, float freq)
2771{
2772 _tTriLFO* c = *cy;
2773
2774 c->freq = freq;
2775 c->inc = freq * c->invSampleRateTimesTwoTo32;
2776}
2777
2778void tTriLFO_setPhase(tTriLFO* const cy, float phase)
2779{
2780 _tTriLFO* c = *cy;
2781
2782 int i = phase;
2783 phase -= i;
2784 c->phase = phase * TWO_TO_32;
2785}
2786
2787void tTriLFO_setSampleRate (tTriLFO* const cy, float sr)
2788{
2789 _tTriLFO* c = *cy;
2790
2791 c->invSampleRateTimesTwoTo32 = (1.0f/sr) * TWO_TO_32;
2792 tTriLFO_setFreq(cy, c->freq);
2793}
2794///sinetri
2795
2796void tSineTriLFO_init (tSineTriLFO* const cy, LEAF* const leaf)
2797{
2798 tSineTriLFO_initToPool(cy, &leaf->mempool);
2799 _tSineTriLFO* c = *cy;
2800}
2801
2802void tSineTriLFO_initToPool (tSineTriLFO* const cy, tMempool* const mp)
2803{
2804 _tMempool* m = *mp;
2805 _tSineTriLFO* c = *cy = (_tSineTriLFO*) mpool_alloc(sizeof(_tSineTriLFO), m);
2806 c->mempool = m;
2807 LEAF* leaf = c->mempool->leaf;
2808 tTriLFO_initToPool(&c->tri,mp);
2809 tCycle_initToPool(&c->sine,mp);
2810
2811}
2812void tSineTriLFO_free (tSineTriLFO* const cy)
2813{
2814 _tSineTriLFO* c = *cy;
2815 tCycle_free(&c->sine);
2816 tTriLFO_free(&c->tri);
2817 mpool_free((char*)c, c->mempool);
2818}
2819
2820float tSineTriLFO_tick (tSineTriLFO* const cy)
2821{
2822 _tSineTriLFO* c = *cy;
2823 float a = tCycle_tick(&c->sine);
2824 float b = tTriLFO_tick(&c->tri);
2825 return (1 - c->shape) * a + c->shape * b;
2826}
2827void tSineTriLFO_setFreq (tSineTriLFO* const cy, float freq)
2828{
2829 _tSineTriLFO* c = *cy;
2830 tTriLFO_setFreq(&c->tri, freq);
2831 tCycle_setFreq(&c->sine, freq);
2832}
2833void tSineTriLFO_setSampleRate (tSineTriLFO* const cy, float sr)
2834{
2835 _tSineTriLFO* c = *cy;
2836 tTriLFO_setSampleRate(&c->tri, sr);
2837 tCycle_setSampleRate(&c->sine, sr);
2838}
2839void tSineTriLFO_setPhase (tSineTriLFO* const cy, float phase)
2840{
2841 _tSineTriLFO* c = *cy;
2842 tTriLFO_setPhase(&c->tri, phase);
2843 tCycle_setPhase(&c->sine, phase);
2844}
2845
2846 void tSineTriLFO_setShape (tSineTriLFO* const cy, float shape)
2847 {
2848 _tSineTriLFO* c = *cy;
2849 c->shape = shape;
2850
2851 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t