Guest User

Untitled

a guest
Jan 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. float lerp(float a, float b, float f)
  2. {
  3. return a + f * (b - a);
  4. }
  5.  
  6. class Envelope {
  7. public:
  8. float attack, decay, sustain, release, start;
  9. int period;
  10.  
  11. // attack, decay and release are in millis
  12. // sustain is an amplitude (float of 0-1)
  13.  
  14. Envelope (float a, float d, float s, float r) {
  15. start = millis();
  16.  
  17. attack = a;
  18. decay = d;
  19. sustain = s;
  20. release = r;
  21. }
  22.  
  23. // You have to specify how long the period is (the time from
  24. // noteOn to noteOff) in millis
  25. void trigger (int p) {
  26. start = millis();
  27. period = p;
  28. }
  29.  
  30. float value () {
  31. int t = millis() - start;
  32.  
  33. if (t < attack) {
  34. return lerp (0, 1, 1.0 / attack * t);
  35. } else if (t < attack + decay) {
  36. return lerp (1, sustain, 1.0 / decay * (t - attack));
  37. } else if (t < period - release) {
  38. return sustain;
  39. } else if (t < period) {
  40. return lerp (sustain, 0, 1.0 / release * (t - period + release ));
  41. } else {
  42. return 0;
  43. }
  44. }
  45. };
Add Comment
Please, Sign In to add comment