| @@ -234,11 +234,13 @@ void tADSR_initToPool (tADSR* const adsrenv, float attack, float decay, fl · 233 unmodified lines |
| 234 | 234 | attack = 8192.0f; |
| 235 | 235 | if (attack < 0.0f) |
| 236 | 236 | attack = 0.0f; |
| 237 | + adsr->attack = attack; |
| 237 | 238 | |
| 238 | 239 | if (decay > 8192.0f) |
| 239 | 240 | decay = 8192.0f; |
| 240 | 241 | if (decay < 0.0f) |
| 241 | 242 | decay = 0.0f; |
| 243 | + adsr->decay = decay; |
| 242 | 244 | |
| 243 | 245 | if (sustain > 1.0f) |
| 244 | 246 | sustain = 1.0f; |
| @@ -249,6 +251,7 @@ void tADSR_initToPool (tADSR* const adsrenv, float attack, float decay, fl · 4 unmodified lines |
| 249 | 251 | release = 8192.0f; |
| 250 | 252 | if (release < 0.0f) |
| 251 | 253 | release = 0.0f; |
| 254 | + adsr->release = release; |
| 252 | 255 | |
| 253 | 256 | int16_t attackIndex = ((int16_t)(attack * 8.0f))-1; |
| 254 | 257 | 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 |
| 279 | 282 | adsr->releaseInc = adsr->inc_buff[releaseIndex]; |
| 280 | 283 | adsr->rampInc = adsr->inc_buff[rampIndex]; |
| 281 | 284 | |
| 285 | + adsr->baseLeakFactor = 1.0f; |
| 282 | 286 | adsr->leakFactor = 1.0f; |
| 287 | + adsr->invSampleRate = adsr->mempool->leaf->invSampleRate; |
| 283 | 288 | } |
| 284 | 289 | |
| 285 | 290 | void tADSR_free (tADSR* const adsrenv) |
| @@ -293,6 +298,8 @@ void tADSR_setAttack(tADSR* const adsrenv, float attack) · 7 unmodified lines |
| 293 | 298 | _tADSR* adsr = *adsrenv; |
| 294 | 299 | |
| 295 | 300 | int32_t attackIndex; |
| 301 | + |
| 302 | + adsr->attack = attack; |
| 296 | 303 | |
| 297 | 304 | if (attack < 0.0f) { |
| 298 | 305 | attackIndex = 0.0f; |
| @@ -302,7 +309,7 @@ void tADSR_setAttack(tADSR* const adsrenv, float attack) · 3 unmodified lines |
| 302 | 309 | attackIndex = ((int32_t)(8192.0f * 8.0f))-1; |
| 303 | 310 | } |
| 304 | 311 | |
| 305 | | − adsr->attackInc = adsr->inc_buff[attackIndex]; |
| 312 | + adsr->attackInc = adsr->inc_buff[attackIndex] * (44100.f * adsr->invSampleRate); |
| 306 | 313 | } |
| 307 | 314 | |
| 308 | 315 | void tADSR_setDecay(tADSR* const adsrenv, float decay) |
| @@ -310,6 +317,8 @@ void tADSR_setDecay(tADSR* const adsrenv, float decay) · 1 unmodified line |
| 310 | 317 | _tADSR* adsr = *adsrenv; |
| 311 | 318 | |
| 312 | 319 | int32_t decayIndex; |
| 320 | + |
| 321 | + adsr->decay = decay; |
| 313 | 322 | |
| 314 | 323 | if (decay < 0.0f) { |
| 315 | 324 | decayIndex = 0.0f; |
| @@ -319,7 +328,7 @@ void tADSR_setDecay(tADSR* const adsrenv, float decay) · 3 unmodified lines |
| 319 | 328 | decayIndex = ((int32_t)(8192.0f * 8.0f)) - 1; |
| 320 | 329 | } |
| 321 | 330 | |
| 322 | | − adsr->decayInc = adsr->inc_buff[decayIndex]; |
| 331 | + adsr->decayInc = adsr->inc_buff[decayIndex] * (44100.f * adsr->invSampleRate); |
| 323 | 332 | } |
| 324 | 333 | |
| 325 | 334 | void tADSR_setSustain(tADSR* const adsrenv, float sustain) |
| @@ -336,6 +345,8 @@ void tADSR_setRelease(tADSR* const adsrenv, float release) · 10 unmodified lines |
| 336 | 345 | _tADSR* adsr = *adsrenv; |
| 337 | 346 | |
| 338 | 347 | int32_t releaseIndex; |
| 348 | + |
| 349 | + adsr->release = release; |
| 339 | 350 | |
| 340 | 351 | if (release < 0.0f) { |
| 341 | 352 | releaseIndex = 0.0f; |
| @@ -345,16 +356,15 @@ void tADSR_setRelease(tADSR* const adsrenv, float release) · 3 unmodified lines |
| 345 | 356 | releaseIndex = ((int32_t)(8192.0f * 8.0f)) - 1; |
| 346 | 357 | } |
| 347 | 358 | |
| 348 | | − adsr->releaseInc = adsr->inc_buff[releaseIndex]; |
| 359 | + adsr->releaseInc = adsr->inc_buff[releaseIndex] * (44100.f * adsr->invSampleRate); |
| 349 | 360 | } |
| 350 | 361 | |
| 351 | 362 | // 0.999999 is slow leak, 0.9 is fast leak |
| 352 | 363 | void tADSR_setLeakFactor(tADSR* const adsrenv, float leakFactor) |
| 353 | 364 | { |
| 354 | 365 | _tADSR* adsr = *adsrenv; |
| 355 | | − |
| 356 | | − |
| 357 | | − adsr->leakFactor = leakFactor; |
| 366 | + adsr->baseLeakFactor = leakFactor; |
| 367 | + adsr->leakFactor = powf(leakFactor, 44100.0f * adsr->invSampleRate); |
| 358 | 368 | } |
| 359 | 369 | |
| 360 | 370 | void tADSR_on(tADSR* const adsrenv, float velocity) |
| @@ -482,6 +492,19 @@ float tADSR_tick(tADSR* const adsrenv) · 121 unmodified lines |
| 482 | 492 | |
| 483 | 493 | return adsr->next; |
| 484 | 494 | } |
| 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 | + |
| 485 | 508 | #endif // LEAF_INCLUDE_ADSR_TABLES |
| 486 | 509 | |
| 487 | 510 | |
| @@ -535,7 +558,11 @@ void tADSRS_initToPool (tADSRS* const adsrenv, float attack, float decay, · 47 unmodified lines |
| 535 | 558 | adsr->factor = 0.01f; |
| 536 | 559 | adsr->oneMinusFactor = 0.99f; |
| 537 | 560 | adsr->output = 0.0f; |
| 561 | + |
| 562 | + adsr->baseLeakFactor = 1.0f; |
| 538 | 563 | adsr->leakFactor = 1.0f; |
| 564 | + |
| 565 | + adsr->invSampleRate = leaf->invSampleRate; |
| 539 | 566 | } |
| 540 | 567 | |
| 541 | 568 | void tADSRS_free (tADSRS* const adsrenv) |
| @@ -586,7 +613,8 @@ void tADSRS_setRelease(tADSRS* const adsrenv, float release) · 44 unmodified lines |
| 586 | 613 | void tADSRS_setLeakFactor(tADSRS* const adsrenv, float leakFactor) |
| 587 | 614 | { |
| 588 | 615 | _tADSRS* adsr = *adsrenv; |
| 589 | | − adsr->leakFactor = leakFactor; |
| 616 | + adsr->baseLeakFactor = leakFactor; |
| 617 | + adsr->leakFactor = powf(leakFactor, 44100.0f * adsr->invSampleRate); |
| 590 | 618 | } |
| 591 | 619 | |
| 592 | 620 | void tADSRS_on(tADSRS* const adsrenv, float velocity) |
| @@ -650,10 +678,12 @@ void tADSRS_setSampleRate(tADSRS* const adsrenv, float sr) · 57 unmodified lines |
| 650 | 678 | |
| 651 | 679 | adsr->sampleRate = sr; |
| 652 | 680 | adsr->sampleRateInMs = adsr->sampleRate * 0.001f; |
| 681 | + adsr->invSampleRate = 1.0f/sr; |
| 653 | 682 | |
| 654 | 683 | tADSRS_setAttack(adsrenv, adsr->attack); |
| 655 | 684 | tADSRS_setDecay(adsrenv, adsr->decay); |
| 656 | 685 | tADSRS_setRelease(adsrenv, adsr->release); |
| 686 | + tADSRS_setLeakFactor(adsrenv, adsr->baseLeakFactor); |
| 657 | 687 | } |
| 658 | 688 | |
| 659 | 689 | //================================================================================ |
| @@ -710,7 +740,9 @@ void tADSRT_initToPool (tADSRT* const adsrenv, float attack, float decay, · 50 unmodified lines |
| 710 | 740 | adsr->releaseInc = adsr->bufferSizeDividedBySampleRateInMs / release; |
| 711 | 741 | adsr->rampInc = adsr->bufferSizeDividedBySampleRateInMs / 8.0f; |
| 712 | 742 | |
| 743 | + adsr->baseLeakFactor = 1.0f; |
| 713 | 744 | adsr->leakFactor = 1.0f; |
| 745 | + adsr->invSampleRate = leaf->invSampleRate; |
| 714 | 746 | } |
| 715 | 747 | |
| 716 | 748 | void tADSRT_free (tADSRT* const adsrenv) |
| @@ -768,7 +800,8 @@ void tADSRT_setRelease(tADSRT* const adsrenv, float release) · 51 unmodified lines |
| 768 | 800 | void tADSRT_setLeakFactor(tADSRT* const adsrenv, float leakFactor) |
| 769 | 801 | { |
| 770 | 802 | _tADSRT* adsr = *adsrenv; |
| 771 | | − adsr->leakFactor = leakFactor; |
| 803 | + adsr->baseLeakFactor = leakFactor; |
| 804 | + adsr->leakFactor = powf(leakFactor, 44100.0f * adsr->invSampleRate);; |
| 772 | 805 | } |
| 773 | 806 | |
| 774 | 807 | void tADSRT_on(tADSRT* const adsrenv, float velocity) |
| @@ -1010,16 +1043,18 @@ float tADSRT_tickNoInterp(tADSRT* const adsrenv) · 235 unmodified lines |
| 1010 | 1043 | return adsr->next; |
| 1011 | 1044 | } |
| 1012 | 1045 | |
| 1013 | | −void tADSRT_setSampleRate (tADSRT* const adsrenv, float sr) |
| 1046 | +void tADSRT_setSampleRate(tADSRT* const adsrenv, float sr) |
| 1014 | 1047 | { |
| 1015 | 1048 | _tADSRT* adsr = *adsrenv; |
| 1016 | 1049 | |
| 1017 | 1050 | adsr->sampleRate = sr; |
| 1051 | + adsr->invSampleRate = 1.0f/sr; |
| 1018 | 1052 | adsr->bufferSizeDividedBySampleRateInMs = adsr->buff_size / (adsr->sampleRate * 0.001f); |
| 1019 | 1053 | adsr->attackInc = adsr->bufferSizeDividedBySampleRateInMs / adsr->attack; |
| 1020 | 1054 | adsr->decayInc = adsr->bufferSizeDividedBySampleRateInMs / adsr->decay; |
| 1021 | 1055 | adsr->releaseInc = adsr->bufferSizeDividedBySampleRateInMs / adsr->release; |
| 1022 | 1056 | adsr->rampInc = adsr->bufferSizeDividedBySampleRateInMs / 8.0f; |
| 1057 | + adsr->leakFactor = powf(adsr->baseLeakFactor, 44100.0f * adsr->invSampleRate); |
| 1023 | 1058 | } |
| 1024 | 1059 | |
| 1025 | 1060 | /////----------------- |
| @@ -1279,12 +1314,13 @@ void tExpSmooth_initToPool (tExpSmooth* const expsmooth, float val, float f · 253 unmodified lines |
| 1279 | 1314 | _tExpSmooth* smooth = *expsmooth = (_tExpSmooth*) mpool_alloc(sizeof(_tExpSmooth), m); |
| 1280 | 1315 | smooth->mempool = m; |
| 1281 | 1316 | |
| 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; |
| 1288 | 1324 | } |
| 1289 | 1325 | |
| 1290 | 1326 | void tExpSmooth_free (tExpSmooth* const expsmooth) |
| @@ -1297,12 +1333,12 @@ void tExpSmooth_setFactor(tExpSmooth* const expsmooth, float factor) · 6 unmodified lines |
| 1297 | 1333 | { // 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 |
| 1298 | 1334 | _tExpSmooth* smooth = *expsmooth; |
| 1299 | 1335 | |
| 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; |
| 1306 | 1342 | } |
| 1307 | 1343 | |
| 1308 | 1344 | void tExpSmooth_setDest(tExpSmooth* const expsmooth, float dest) |
| @@ -1337,6 +1373,14 @@ float tExpSmooth_sample(tExpSmooth* const expsmooth) · 28 unmodified lines |
| 1337 | 1373 | return smooth->curr; |
| 1338 | 1374 | } |
| 1339 | 1375 | |
| 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 | + |
| 1340 | 1384 | //tSlide is based on the max/msp slide~ object |
| 1341 | 1385 | //// |
| 1342 | 1386 | |