History

factor sample rate into tick based env factors and neuron timestep

Matthew Wang committed 5 years ago

Commit 3eaa22b1 parent 8b254657 files · +101 −40Browse files

Unified · Split

7 files
  1. leaf/Inc/leaf-envelopes.h +15 −6
  2. leaf/Inc/leaf-math.h +3 −0
  3. leaf/Inc/leaf-oscillators.h +2 −1
  4. leaf/Inc/leaf-reverb.h +0 −2
  5. leaf/Src/leaf-envelopes.c +65 −21
  6. leaf/Src/leaf-oscillators.c +12 −3
  7. leaf/Src/leaf-reverb.c +4 −7
leaf/Inc/leaf-envelopes.h+15−6
@@ -164,8 +164,9 @@ extern "C" { · 163 unmodified lines
164164 {
165165
166166 tMempool mempool;
167− float factor, oneminusfactor;
167+ float baseFactor, factor, oneminusfactor;
168168 float curr,dest;
169+ float invSampleRate;
169170 } _tExpSmooth;
170171
171172 typedef _tExpSmooth* tExpSmooth;
@@ -179,7 +180,8 @@ extern "C" { · 7 unmodified lines
179180 void tExpSmooth_setFactor (tExpSmooth* const, float factor);
180181 void tExpSmooth_setDest (tExpSmooth* const, float dest);
181182 void tExpSmooth_setVal (tExpSmooth* const, float val);
182− void tExpSmooth_setValAndDest(tExpSmooth* const expsmooth, float val);
183+ void tExpSmooth_setValAndDest(tExpSmooth* const, float val);
184+ void tExpSmooth_setSampleRate(tExpSmooth* const, float sr);
183185
184186 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
185187
@@ -252,6 +254,8 @@ extern "C" { · 66 unmodified lines
252254
253255 float next;
254256
257+ float attack, decay, release;
258+
255259 float attackInc, decayInc, releaseInc, rampInc;
256260
257261 int inAttack, inDecay, inSustain, inRelease, inRamp;
@@ -260,7 +264,9 @@ extern "C" { · 2 unmodified lines
260264
261265 float attackPhase, decayPhase, releasePhase, rampPhase;
262266
263− float leakFactor;
267+ float baseLeakFactor, leakFactor;
268+
269+ float invSampleRate;
264270 } _tADSR;
265271
266272 typedef _tADSR* tADSR;
@@ -277,6 +283,7 @@ extern "C" { · 10 unmodified lines
277283 void tADSR_setLeakFactor (tADSR* const, float leakFactor);
278284 void tADSR_on (tADSR* const, float velocity);
279285 void tADSR_off (tADSR* const);
286+ void tADSR_setSampleRate (tADSR* const, float sr);
280287
281288 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
282289
@@ -358,7 +365,9 @@ extern "C" { · 75 unmodified lines
358365
359366 float attackPhase, decayPhase, releasePhase, rampPhase;
360367
361− float leakFactor;
368+ float baseLeakFactor, leakFactor;
369+
370+ float invSampleRate;
362371 } _tADSRT;
363372
364373 typedef _tADSRT* tADSRT;
@@ -466,12 +475,12 @@ extern "C" { · 101 unmodified lines
466475 float attackBase;
467476 float decayBase;
468477 float releaseBase;
469− float leakFactor;
478+ float baseLeakFactor, leakFactor;
470479 float targetGainSquared;
471480 float factor;
472481 float oneMinusFactor;
473482 float gain;
474−
483+ float invSampleRate;
475484 } _tADSRS;
476485
477486 typedef _tADSRS* tADSRS;
leaf/Inc/leaf-math.h+3−0
@@ -58,6 +58,9 @@ extern "C" { · 57 unmodified lines
5858 #define INV_10240 0.00009765625f
5959 #define INV_20480 0.000048828125f
6060
61+#define INV_44100 0.00002267573f
62+#define INV_48000 0.00002083333f
63+
6164
6265 #define INV_TWELVE 0.0833333333f
6366 #define INV_440 0.0022727273f
leaf/Inc/leaf-oscillators.h+2−1
@@ -630,7 +630,7 @@ extern "C" { · 629 unmodified lines
630630
631631 float voltage, current;
632632 float timeStep;
633−
633+ float invSampleRate;
634634 float alpha[3];
635635 float beta[3];
636636 float rate[3];
@@ -657,6 +657,7 @@ extern "C" { · 20 unmodified lines
657657 void tNeuron_setV2 (tNeuron* const neuron, float V2);
658658 void tNeuron_setV3 (tNeuron* const neuron, float V3);
659659 void tNeuron_setTimeStep (tNeuron* const neuron, float timestep);
660+ void tNeuron_setSampleRate(tNeuron* const neuron, float sr);
660661
661662 //==============================================================================
662663
leaf/Inc/leaf-reverb.h+0−2
@@ -77,7 +77,6 @@ extern "C" { · 76 unmodified lines
7777
7878 float mix, t60;
7979
80− float inv_441;
8180 float sampleRate;
8281 float invSampleRate;
8382
@@ -152,7 +151,6 @@ extern "C" { · 68 unmodified lines
152151
153152 float mix, t60;
154153
155− float inv_441;
156154 float sampleRate;
157155 float invSampleRate;
158156
leaf/Src/leaf-envelopes.c+65−21
@@ -234,11 +234,13 @@ void tADSR_initToPool (tADSR* const adsrenv, float attack, float decay, fl · 233 unmodified lines
234234 attack = 8192.0f;
235235 if (attack < 0.0f)
236236 attack = 0.0f;
237+ adsr->attack = attack;
237238
238239 if (decay > 8192.0f)
239240 decay = 8192.0f;
240241 if (decay < 0.0f)
241242 decay = 0.0f;
243+ adsr->decay = decay;
242244
243245 if (sustain > 1.0f)
244246 sustain = 1.0f;
@@ -249,6 +251,7 @@ void tADSR_initToPool (tADSR* const adsrenv, float attack, float decay, fl · 4 unmodified lines
249251 release = 8192.0f;
250252 if (release < 0.0f)
251253 release = 0.0f;
254+ adsr->release = release;
252255
253256 int16_t attackIndex = ((int16_t)(attack * 8.0f))-1;
254257 int16_t decayIndex = ((int16_t)(decay * 8.0f))-1;
@@ -279,7 +282,9 @@ void tADSR_initToPool (tADSR* const adsrenv, float attack, float decay, fl · 24 unmodified lines
279282 adsr->releaseInc = adsr->inc_buff[releaseIndex];
280283 adsr->rampInc = adsr->inc_buff[rampIndex];
281284
285+ adsr->baseLeakFactor = 1.0f;
282286 adsr->leakFactor = 1.0f;
287+ adsr->invSampleRate = adsr->mempool->leaf->invSampleRate;
283288 }
284289
285290 void tADSR_free (tADSR* const adsrenv)
@@ -293,6 +298,8 @@ void tADSR_setAttack(tADSR* const adsrenv, float attack) · 7 unmodified lines
293298 _tADSR* adsr = *adsrenv;
294299
295300 int32_t attackIndex;
301+
302+ adsr->attack = attack;
296303
297304 if (attack < 0.0f) {
298305 attackIndex = 0.0f;
@@ -302,7 +309,7 @@ void tADSR_setAttack(tADSR* const adsrenv, float attack) · 3 unmodified lines
302309 attackIndex = ((int32_t)(8192.0f * 8.0f))-1;
303310 }
304311
305− adsr->attackInc = adsr->inc_buff[attackIndex];
312+ adsr->attackInc = adsr->inc_buff[attackIndex] * (44100.f * adsr->invSampleRate);
306313 }
307314
308315 void tADSR_setDecay(tADSR* const adsrenv, float decay)
@@ -310,6 +317,8 @@ void tADSR_setDecay(tADSR* const adsrenv, float decay) · 1 unmodified line
310317 _tADSR* adsr = *adsrenv;
311318
312319 int32_t decayIndex;
320+
321+ adsr->decay = decay;
313322
314323 if (decay < 0.0f) {
315324 decayIndex = 0.0f;
@@ -319,7 +328,7 @@ void tADSR_setDecay(tADSR* const adsrenv, float decay) · 3 unmodified lines
319328 decayIndex = ((int32_t)(8192.0f * 8.0f)) - 1;
320329 }
321330
322− adsr->decayInc = adsr->inc_buff[decayIndex];
331+ adsr->decayInc = adsr->inc_buff[decayIndex] * (44100.f * adsr->invSampleRate);
323332 }
324333
325334 void tADSR_setSustain(tADSR* const adsrenv, float sustain)
@@ -336,6 +345,8 @@ void tADSR_setRelease(tADSR* const adsrenv, float release) · 10 unmodified lines
336345 _tADSR* adsr = *adsrenv;
337346
338347 int32_t releaseIndex;
348+
349+ adsr->release = release;
339350
340351 if (release < 0.0f) {
341352 releaseIndex = 0.0f;
@@ -345,16 +356,15 @@ void tADSR_setRelease(tADSR* const adsrenv, float release) · 3 unmodified lines
345356 releaseIndex = ((int32_t)(8192.0f * 8.0f)) - 1;
346357 }
347358
348− adsr->releaseInc = adsr->inc_buff[releaseIndex];
359+ adsr->releaseInc = adsr->inc_buff[releaseIndex] * (44100.f * adsr->invSampleRate);
349360 }
350361
351362 // 0.999999 is slow leak, 0.9 is fast leak
352363 void tADSR_setLeakFactor(tADSR* const adsrenv, float leakFactor)
353364 {
354365 _tADSR* adsr = *adsrenv;
355−
356−
357− adsr->leakFactor = leakFactor;
366+ adsr->baseLeakFactor = leakFactor;
367+ adsr->leakFactor = powf(leakFactor, 44100.0f * adsr->invSampleRate);
358368 }
359369
360370 void tADSR_on(tADSR* const adsrenv, float velocity)
@@ -482,6 +492,19 @@ float tADSR_tick(tADSR* const adsrenv) · 121 unmodified lines
482492
483493 return adsr->next;
484494 }
495+
496+void tADSR_setSampleRate(tADSR* const adsrenv, float sr)
497+{
498+ _tADSR* adsr = *adsrenv;
499+
500+ adsr->invSampleRate = 1.0f/sr;
501+
502+ tADSR_setAttack(adsrenv, adsr->attack);
503+ tADSR_setDecay(adsrenv, adsr->decay);
504+ tADSR_setRelease(adsrenv, adsr->release);
505+ tADSR_setLeakFactor(adsrenv, adsr->baseLeakFactor);
506+}
507+
485508 #endif // LEAF_INCLUDE_ADSR_TABLES
486509
487510
@@ -535,7 +558,11 @@ void tADSRS_initToPool (tADSRS* const adsrenv, float attack, float decay, · 47 unmodified lines
535558 adsr->factor = 0.01f;
536559 adsr->oneMinusFactor = 0.99f;
537560 adsr->output = 0.0f;
561+
562+ adsr->baseLeakFactor = 1.0f;
538563 adsr->leakFactor = 1.0f;
564+
565+ adsr->invSampleRate = leaf->invSampleRate;
539566 }
540567
541568 void tADSRS_free (tADSRS* const adsrenv)
@@ -586,7 +613,8 @@ void tADSRS_setRelease(tADSRS* const adsrenv, float release) · 44 unmodified lines
586613 void tADSRS_setLeakFactor(tADSRS* const adsrenv, float leakFactor)
587614 {
588615 _tADSRS* adsr = *adsrenv;
589− adsr->leakFactor = leakFactor;
616+ adsr->baseLeakFactor = leakFactor;
617+ adsr->leakFactor = powf(leakFactor, 44100.0f * adsr->invSampleRate);
590618 }
591619
592620 void tADSRS_on(tADSRS* const adsrenv, float velocity)
@@ -650,10 +678,12 @@ void tADSRS_setSampleRate(tADSRS* const adsrenv, float sr) · 57 unmodified lines
650678
651679 adsr->sampleRate = sr;
652680 adsr->sampleRateInMs = adsr->sampleRate * 0.001f;
681+ adsr->invSampleRate = 1.0f/sr;
653682
654683 tADSRS_setAttack(adsrenv, adsr->attack);
655684 tADSRS_setDecay(adsrenv, adsr->decay);
656685 tADSRS_setRelease(adsrenv, adsr->release);
686+ tADSRS_setLeakFactor(adsrenv, adsr->baseLeakFactor);
657687 }
658688
659689 //================================================================================
@@ -710,7 +740,9 @@ void tADSRT_initToPool (tADSRT* const adsrenv, float attack, float decay, · 50 unmodified lines
710740 adsr->releaseInc = adsr->bufferSizeDividedBySampleRateInMs / release;
711741 adsr->rampInc = adsr->bufferSizeDividedBySampleRateInMs / 8.0f;
712742
743+ adsr->baseLeakFactor = 1.0f;
713744 adsr->leakFactor = 1.0f;
745+ adsr->invSampleRate = leaf->invSampleRate;
714746 }
715747
716748 void tADSRT_free (tADSRT* const adsrenv)
@@ -768,7 +800,8 @@ void tADSRT_setRelease(tADSRT* const adsrenv, float release) · 51 unmodified lines
768800 void tADSRT_setLeakFactor(tADSRT* const adsrenv, float leakFactor)
769801 {
770802 _tADSRT* adsr = *adsrenv;
771− adsr->leakFactor = leakFactor;
803+ adsr->baseLeakFactor = leakFactor;
804+ adsr->leakFactor = powf(leakFactor, 44100.0f * adsr->invSampleRate);;
772805 }
773806
774807 void tADSRT_on(tADSRT* const adsrenv, float velocity)
@@ -1010,16 +1043,18 @@ float tADSRT_tickNoInterp(tADSRT* const adsrenv) · 235 unmodified lines
10101043 return adsr->next;
10111044 }
10121045
1013−void tADSRT_setSampleRate (tADSRT* const adsrenv, float sr)
1046+void tADSRT_setSampleRate(tADSRT* const adsrenv, float sr)
10141047 {
10151048 _tADSRT* adsr = *adsrenv;
10161049
10171050 adsr->sampleRate = sr;
1051+ adsr->invSampleRate = 1.0f/sr;
10181052 adsr->bufferSizeDividedBySampleRateInMs = adsr->buff_size / (adsr->sampleRate * 0.001f);
10191053 adsr->attackInc = adsr->bufferSizeDividedBySampleRateInMs / adsr->attack;
10201054 adsr->decayInc = adsr->bufferSizeDividedBySampleRateInMs / adsr->decay;
10211055 adsr->releaseInc = adsr->bufferSizeDividedBySampleRateInMs / adsr->release;
10221056 adsr->rampInc = adsr->bufferSizeDividedBySampleRateInMs / 8.0f;
1057+ adsr->leakFactor = powf(adsr->baseLeakFactor, 44100.0f * adsr->invSampleRate);
10231058 }
10241059
10251060 /////-----------------
@@ -1279,12 +1314,13 @@ void tExpSmooth_initToPool (tExpSmooth* const expsmooth, float val, float f · 253 unmodified lines
12791314 _tExpSmooth* smooth = *expsmooth = (_tExpSmooth*) mpool_alloc(sizeof(_tExpSmooth), m);
12801315 smooth->mempool = m;
12811316
1282− smooth->curr=val;
1283− smooth->dest=val;
1284− if (factor<0) factor=0;
1285− if (factor>1) factor=1;
1286− smooth->factor=factor;
1287− smooth->oneminusfactor=1.0f-factor;
1317+ smooth->curr = val;
1318+ smooth->dest = val;
1319+ if (factor < 0) factor = 0;
1320+ if (factor > 1) factor = 1;
1321+ smooth->factor = factor;
1322+ smooth->oneminusfactor = 1.0f - factor;
1323+ smooth->invSampleRate = smooth->mempool->leaf->invSampleRate;
12881324 }
12891325
12901326 void tExpSmooth_free (tExpSmooth* const expsmooth)
@@ -1297,12 +1333,12 @@ void tExpSmooth_setFactor(tExpSmooth* const expsmooth, float factor) · 6 unmodified lines
12971333 { // factor is usually a value between 0 and 0.1. Lower value is slower. 0.01 for example gives you a smoothing time of about 10ms
12981334 _tExpSmooth* smooth = *expsmooth;
12991335
1300− if (factor<0)
1301− factor=0;
1302− else
1303− if (factor>1) factor=1;
1304− smooth->factor=factor;
1305− smooth->oneminusfactor=1.0f-factor;
1336+ if (factor < 0)
1337+ factor = 0;
1338+ else if (factor > 1) factor = 1;
1339+ smooth->baseFactor = factor;
1340+ smooth->factor = powf(factor, 44100.f * smooth->invSampleRate);
1341+ smooth->oneminusfactor = 1.0f - factor;
13061342 }
13071343
13081344 void tExpSmooth_setDest(tExpSmooth* const expsmooth, float dest)
@@ -1337,6 +1373,14 @@ float tExpSmooth_sample(tExpSmooth* const expsmooth) · 28 unmodified lines
13371373 return smooth->curr;
13381374 }
13391375
1376+void tExpSmooth_setSampleRate(tExpSmooth* const expsmooth, float sr)
1377+{
1378+ _tExpSmooth* smooth = *expsmooth;
1379+ smooth->invSampleRate = 1.0f/sr;
1380+ smooth->factor = powf(smooth->baseFactor, 44100.f * smooth->invSampleRate);
1381+ smooth->oneminusfactor = 1.0f - smooth->factor;
1382+}
1383+
13401384 //tSlide is based on the max/msp slide~ object
13411385 ////
13421386
leaf/Src/leaf-oscillators.c+12−3
@@ -700,12 +700,14 @@ void tNeuron_initToPool (tNeuron* const nr, tMempool* const mp) · 699 unmodified lines
700700 _tMempool* m = *mp;
701701 _tNeuron* n = *nr = (_tNeuron*) mpool_alloc(sizeof(_tNeuron), m);
702702 n->mempool = m;
703+ LEAF* leaf = n->mempool->leaf;
703704
704705 tPoleZero_initToPool(&n->f, mp);
705706
706707 tPoleZero_setBlockZero(&n->f, 0.99f);
707708
708− n->timeStep = 1.0f / 50.0f;
709+ n->invSampleRate = leaf->invSampleRate;
710+ n->timeStep = (44100.0f * n->invSampleRate) / 50.0f;
709711
710712 n->current = 0.0f; // 100.0f for sound
711713 n->voltage = 0.0f;
@@ -742,7 +744,7 @@ void tNeuron_reset(tNeuron* const nr) · 30 unmodified lines
742744
743745 tPoleZero_setBlockZero(&n->f, 0.99f);
744746
745− n->timeStep = 1.0f / 50.0f;
747+ n->timeStep = (44100.0f * n->invSampleRate) / 50.0f;
746748
747749 n->current = 0.0f; // 100.0f for sound
748750 n->voltage = 0.0f;
@@ -786,7 +788,7 @@ void tNeuron_setV3(tNeuron* const nr, float V3) · 37 unmodified lines
786788 void tNeuron_setTimeStep(tNeuron* const nr, float timeStep)
787789 {
788790 _tNeuron* n = *nr;
789− n->timeStep = timeStep;
791+ n->timeStep = (44100.0f * n->invSampleRate) * timeStep;
790792 }
791793
792794 void tNeuron_setK(tNeuron* const nr, float K)
@@ -903,6 +905,13 @@ void tNeuron_setCurrent (tNeuron* const nr, float current) · 110 unmodified lines
903905 n->current = current;
904906 }
905907
908+void tNeuron_setSampleRate (tNeuron* const nr, float sr)
909+{
910+ _tNeuron* n = *nr;
911+ n->invSampleRate = 1.0f/sr;
912+ n->timeStep = (44100.0f * n->invSampleRate) / 50.0f;
913+}
914+
906915 //----------------------------------------------------------------------------------------------------------
907916
908917 void tMBPulse_init(tMBPulse* const osc, LEAF* const leaf)
leaf/Src/leaf-reverb.c+4−7
@@ -33,13 +33,11 @@ void tPRCReverb_initToPool (tPRCReverb* const rev, float t60, tMempool* con · 32 unmodified lines
3333
3434 if (t60 <= 0.0f) t60 = 0.001f;
3535
36− r->inv_441 = 1.0f/44100.0f;
37−
3836 r->sampleRate = leaf->sampleRate;
3937 r->invSampleRate = leaf->invSampleRate;
4038
4139 int lengths[4] = { 341, 613, 1557, 2137 }; // Delay lengths for 44100 Hz sample rate.
42− double scaler = r->sampleRate * r->inv_441;
40+ double scaler = r->sampleRate * INV_44100;
4341
4442 int delay, i;
4543 if (scaler != 1.0f)
@@ -144,7 +142,7 @@ void tPRCReverb_setSampleRate (tPRCReverb* const rev, float sr) · 98 unmodified lines
144142 r->invSampleRate = 1.0f/r->sampleRate;
145143
146144 int lengths[4] = { 341, 613, 1557, 2137 }; // Delay lengths for 44100 Hz sample rate.
147− double scaler = r->sampleRate * r->inv_441;
145+ double scaler = r->sampleRate * INV_44100;
148146
149147 int delay, i;
150148 if (scaler != 1.0f)
@@ -187,12 +185,11 @@ void tNReverb_initToPool (tNReverb* const rev, float t60, tMempool* const · 36 unmodified lines
187185
188186 if (t60 <= 0.0f) t60 = 0.001f;
189187
190− r->inv_441 = 1.0f/44100.0f;
191188 r->sampleRate = leaf->sampleRate;
192189 r->invSampleRate = leaf->invSampleRate;
193190
194191 int lengths[15] = {1433, 1601, 1867, 2053, 2251, 2399, 347, 113, 37, 59, 53, 43, 37, 29, 19}; // Delay lengths for 44100 Hz sample rate.
195− double scaler = r->sampleRate * r->inv_441;// / 25641.0f;
192+ double scaler = r->sampleRate * INV_44100;// / 25641.0f;
196193
197194 int delay, i;
198195
@@ -389,7 +386,7 @@ void tNReverb_setSampleRate (tNReverb* const rev, float sr) · 190 unmodified lines
389386 r->invSampleRate = 1.0f/r->sampleRate;
390387
391388 int lengths[15] = {1433, 1601, 1867, 2053, 2251, 2399, 347, 113, 37, 59, 53, 43, 37, 29, 19}; // Delay lengths for 44100 Hz sample rate.
392− double scaler = r->sampleRate * r->inv_441;// / 25641.0f;
389+ double scaler = r->sampleRate * INV_44100;// / 25641.0f;
393390
394391 int delay, i;
395392

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