Mercurial > hg > beaglert
comparison examples/10-Instruments/d-box/ADSR.cpp @ 464:8fcfbfb32aa0 prerelease
Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
| author | Robert Jack <robert.h.jack@gmail.com> | 
|---|---|
| date | Mon, 20 Jun 2016 16:20:38 +0100 | 
| parents | |
| children | 
   comparison
  equal
  deleted
  inserted
  replaced
| 463:c47709e8b5c9 | 464:8fcfbfb32aa0 | 
|---|---|
| 1 // | |
| 2 // ADSR.cpp | |
| 3 // | |
| 4 // Created by Nigel Redmon on 12/18/12. | |
| 5 // EarLevel Engineering: earlevel.com | |
| 6 // Copyright 2012 Nigel Redmon | |
| 7 // | |
| 8 // For a complete explanation of the ADSR envelope generator and code, | |
| 9 // read the series of articles by the author, starting here: | |
| 10 // http://www.earlevel.com/main/2013/06/01/envelope-generators/ | |
| 11 // | |
| 12 // License: | |
| 13 // | |
| 14 // This source code is provided as is, without warranty. | |
| 15 // You may copy and distribute verbatim copies of this document. | |
| 16 // You may modify and use this source code to create binary code for your own purposes, free or commercial. | |
| 17 // | |
| 18 | |
| 19 #include "ADSR.h" | |
| 20 #include <math.h> | |
| 21 | |
| 22 | |
| 23 ADSR::ADSR(void) { | |
| 24 reset(); | |
| 25 setAttackRate(0); | |
| 26 setDecayRate(0); | |
| 27 setReleaseRate(0); | |
| 28 setSustainLevel(1.0); | |
| 29 setTargetRatioA(0.3); | |
| 30 setTargetRatioDR(0.0001); | |
| 31 } | |
| 32 | |
| 33 ADSR::~ADSR(void) { | |
| 34 } | |
| 35 | |
| 36 void ADSR::setAttackRate(float rate) { | |
| 37 attackRate = rate; | |
| 38 attackCoef = calcCoef(rate, targetRatioA); | |
| 39 attackBase = (1.0 + targetRatioA) * (1.0 - attackCoef); | |
| 40 } | |
| 41 | |
| 42 void ADSR::setDecayRate(float rate) { | |
| 43 decayRate = rate; | |
| 44 decayCoef = calcCoef(rate, targetRatioDR); | |
| 45 decayBase = (sustainLevel - targetRatioDR) * (1.0 - decayCoef); | |
| 46 } | |
| 47 | |
| 48 void ADSR::setReleaseRate(float rate) { | |
| 49 releaseRate = rate; | |
| 50 releaseCoef = calcCoef(rate, targetRatioDR); | |
| 51 releaseBase = -targetRatioDR * (1.0 - releaseCoef); | |
| 52 } | |
| 53 | |
| 54 float ADSR::calcCoef(float rate, float targetRatio) { | |
| 55 return exp(-log((1.0 + targetRatio) / targetRatio) / rate); | |
| 56 } | |
| 57 | |
| 58 void ADSR::setSustainLevel(float level) { | |
| 59 sustainLevel = level; | |
| 60 decayBase = (sustainLevel - targetRatioDR) * (1.0 - decayCoef); | |
| 61 } | |
| 62 | |
| 63 void ADSR::setTargetRatioA(float targetRatio) { | |
| 64 if (targetRatio < 0.000000001) | |
| 65 targetRatio = 0.000000001; // -180 dB | |
| 66 targetRatioA = targetRatio; | |
| 67 attackBase = (1.0 + targetRatioA) * (1.0 - attackCoef); | |
| 68 } | |
| 69 | |
| 70 void ADSR::setTargetRatioDR(float targetRatio) { | |
| 71 if (targetRatio < 0.000000001) | |
| 72 targetRatio = 0.000000001; // -180 dB | |
| 73 targetRatioDR = targetRatio; | |
| 74 decayBase = (sustainLevel - targetRatioDR) * (1.0 - decayCoef); | |
| 75 releaseBase = -targetRatioDR * (1.0 - releaseCoef); | |
| 76 } | 
