Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float lerp(float a, float b, float f)
- {
- return a + f * (b - a);
- }
- class Envelope {
- public:
- float attack, decay, sustain, release, start;
- int period;
- // attack, decay and release are in millis
- // sustain is an amplitude (float of 0-1)
- Envelope (float a, float d, float s, float r) {
- start = millis();
- attack = a;
- decay = d;
- sustain = s;
- release = r;
- }
- // You have to specify how long the period is (the time from
- // noteOn to noteOff) in millis
- void trigger (int p) {
- start = millis();
- period = p;
- }
- float value () {
- int t = millis() - start;
- if (t < attack) {
- return lerp (0, 1, 1.0 / attack * t);
- } else if (t < attack + decay) {
- return lerp (1, sustain, 1.0 / decay * (t - attack));
- } else if (t < period - release) {
- return sustain;
- } else if (t < period) {
- return lerp (sustain, 0, 1.0 / release * (t - period + release ));
- } else {
- return 0;
- }
- }
- };
Add Comment
Please, Sign In to add comment