Newer
Older
music / app / src / main / cpp / effects / Envelope.cpp
  1. #include <android/log.h>
  2. #include "../Instrument.h"
  3. #include "Envelope.h"
  4.  
  5. void Envelope::initialize(AudioHost *host) {
  6. attackIncrement = 1 / attack / host->sampleRate;
  7. delayIncrement = 1 / delay / host->sampleRate;
  8. releaseIncrement = 1 / release / host->sampleRate;
  9. }
  10.  
  11. void Envelope::startNote() {
  12. phase = ATTACK;
  13. }
  14.  
  15. void Envelope::endNote() {
  16. phase = RELEASE;
  17. }
  18.  
  19. void Envelope::doRender(uint32_t sampleCount) {
  20. for (int i = 0; i < sampleCount; ++i) {
  21. switch (phase) {
  22. case ATTACK:
  23. value += attackIncrement;
  24. if (value > 1) {
  25. value = 1;
  26. phase = DELAY;
  27. }
  28. break;
  29. case DELAY:
  30. value -= delayIncrement;
  31. if (value < sustain) {
  32. value = sustain;
  33. phase = SUSTAIN;
  34. }
  35. break;
  36. case RELEASE:
  37. value -= releaseIncrement;
  38. if (value < 0) {
  39. value = 0;
  40. phase = NONE;
  41. }
  42. break;
  43. }
  44. buffer[i] = value;
  45. }
  46. }