You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.3KB

  1. #pragma once
  2. #include <stdlib.h>
  3. #include "base.hpp"
  4. namespace bogaudio {
  5. namespace dsp {
  6. struct EnvelopeGenerator : Generator {
  7. float _sampleRate;
  8. float _sampleTime;
  9. EnvelopeGenerator(float sampleRate = 1000.0f)
  10. : _sampleRate(sampleRate > 1.0 ? sampleRate : 1.0)
  11. , _sampleTime(1.0f / _sampleRate)
  12. {
  13. }
  14. void setSampleRate(float sampleRate);
  15. virtual void _sampleRateChanged() {}
  16. };
  17. struct ADSR : EnvelopeGenerator {
  18. enum Stage {
  19. STOPPED_STAGE,
  20. ATTACK_STAGE,
  21. DECAY_STAGE,
  22. SUSTAIN_STAGE,
  23. RELEASE_STAGE
  24. };
  25. Stage _stage = STOPPED_STAGE;
  26. bool _gated = false;
  27. float _attack = 0.0f;
  28. float _decay = 0.0f;
  29. float _sustain = 1.0f;
  30. float _release = 0.0f;
  31. float _attackShape;
  32. float _decayShape;
  33. float _releaseShape;
  34. float _stageProgress = 0.0f;
  35. float _releaseLevel = 0.0f;
  36. float _envelope = 0.0f;
  37. ADSR(bool linear = false, float sampleRate = 1000.0f) : EnvelopeGenerator(sampleRate) {
  38. setLinearShape(linear);
  39. }
  40. void reset();
  41. void setGate(bool high);
  42. void setAttack(float seconds);
  43. void setDecay(float seconds);
  44. void setSustain(float level);
  45. void setRelease(float seconds);
  46. void setLinearShape(bool linear);
  47. void setShapes(float attackShape, float decayShape, float releaseShape);
  48. bool isStage(Stage stage) { return _stage == stage; }
  49. float _next() override;
  50. };
  51. } // namespace dsp
  52. } // namespace bogaudio