History

wavetable oscillator

Matthew Wang committed 6 years ago

Commit 9862cbb1 parent fdb31242 files · +129 −0Browse files

Unified · Split

2 files
  1. leaf/Inc/leaf-oscillators.h +62 −0
  2. leaf/Src/leaf-oscillators.c +67 −0
leaf/Inc/leaf-oscillators.h+62−0
@@ -25,7 +25,69 @@ extern "C" { · 24 unmodified lines
2525 @example basic-oscillators.c
2626 An example.
2727 */
28+
29+ //==============================================================================
30+
31+ /*!
32+ @defgroup ttable tTable
33+ @ingroup oscillators
34+ @brief A general wavetable oscillator.
35+ @{
36+ */
37+
38+ /*!
39+ @fn void tTable_init (tTable* const osc)
40+ @brief Initialize a tTable to the default LEAF mempool.
41+ @param osc A pointer to the tTable to be initialized.
42+ */
43+
44+ /*!
45+ @fn void tTable_initToPool (tTable* const osc, tMempool* const mempool)
46+ @brief Initialize a tTable to a specified mempool.
47+ @param osc A pointer to the tTable to be initialized.
48+ @param pool A pointer to the tMempool to which the tTable should be initialized.
49+ */
50+
51+ /*!
52+ @fn void tTable_free (tTable* const osc)
53+ @brief Free a tTable from the default LEAF mempool.
54+ @param osc A pointer to the tTable to be freed.
55+ */
56+
57+ /*!
58+ @fn float tTable_tick (tTable* const osc)
59+ @brief Tick a tTable oscillator.
60+ @param osc A pointer to the relevant tTable.
61+ @return The ticked sample as a float from -1 to 1.
62+ */
63+
64+ /*!
65+ @fn void tTable_setFreq (tTable* const osc, float freq)
66+ @brief Set the frequency of a tTable oscillator.
67+ @param osc A pointer to the relevant tTable.
68+ @param freq The frequency to set the oscillator to.
69+ */
70+
71+ /*! @} */
72+
73+ typedef struct _tTable
74+ {
75+ tMempool mempool;
76+
77+ float* waveTable;
78+ int size;
79+ float inc, freq;
80+ float phase;
81+ } _tTable;
82+
83+ typedef _tTable* tTable;
2884
85+ void tTable_init(tTable* const osc, float* waveTable, int size);
86+ void tTable_initToPool(tTable* const osc, float* waveTable, int size, tMempool* const mempool);
87+ void tTable_free(tTable* const osc);
88+
89+ float tTable_tick(tTable* const osc);
90+ void tTable_setFreq(tTable* const osc, float freq);
2991
3092 //==============================================================================
3193
leaf/Src/leaf-oscillators.c+67−0
@@ -16,6 +16,73 @@ · 15 unmodified lines
1616
1717 #endif
1818
19+// WaveTable
20+void tTable_init(tTable* const cy, float* waveTable, int size)
21+{
22+ tTable_initToPool(cy, waveTable, size, &leaf.mempool);
23+}
24+
25+void tTable_initToPool(tTable* const cy, float* waveTable, int size, tMempool* const mp)
26+{
27+ _tMempool* m = *mp;
28+ _tTable* c = *cy = (_tTable*)mpool_alloc(sizeof(_tTable), m);
29+ c->mempool = m;
30+
31+ c->waveTable = waveTable;
32+ c->size = size;
33+ c->inc = 0.0f;
34+ c->phase = 0.0f;
35+}
36+
37+void tTable_free(tTable* const cy)
38+{
39+ _tTable* c = *cy;
40+
41+ mpool_free((char*)c, c->mempool);
42+}
43+
44+void tTable_setFreq(tTable* const cy, float freq)
45+{
46+ _tTable* c = *cy;
47+
48+ c->freq = freq;
49+ c->inc = freq * leaf.invSampleRate;
50+}
51+
52+float tTable_tick(tTable* const cy)
53+{
54+ _tTable* c = *cy;
55+ float temp;
56+ int intPart;
57+ float fracPart;
58+ float samp0;
59+ float samp1;
60+
61+ // Phasor increment
62+ c->phase += c->inc;
63+ while (c->phase >= 1.0f) c->phase -= 1.0f;
64+ while (c->phase < 0.0f) c->phase += 1.0f;
65+
66+ // Wavetable synthesis
67+
68+ temp = c->size * c->phase;
69+ intPart = (int)temp;
70+ fracPart = temp - (float)intPart;
71+ samp0 = c->waveTable[intPart];
72+ if (++intPart >= c->size) intPart = 0;
73+ samp1 = c->waveTable[intPart];
74+
75+ return (samp0 + (samp1 - samp0) * fracPart);
76+
77+}
78+
79+void tTableSampleRateChanged(tTable* const cy)
80+{
81+ _tTable* c = *cy;
82+
83+ c->inc = c->freq * leaf.invSampleRate;
84+}
85+
1986 // Cycle
2087 void tCycle_init(tCycle* const cy)
2188 {

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