Envelope.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 
  2. #include "Envelope.h"
  3. #include "../../DFPSR/api/drawAPI.h"
  4. namespace dsr {
  5. EnvelopeSettings::EnvelopeSettings()
  6. : attack(0.0), decay(0.0), sustain(1.0), release(0.0), hold(0.0), rise(0.0), sustainedSmooth(0.0), releasedSmooth(0.0), used(false) {}
  7. EnvelopeSettings::EnvelopeSettings(double attack, double decay, double sustain, double release, double hold, double rise, double sustainedSmooth, double releasedSmooth)
  8. : attack(attack), decay(decay), sustain(sustain), release(release), hold(hold), rise(rise), sustainedSmooth(sustainedSmooth), releasedSmooth(releasedSmooth), used(true) {}
  9. Envelope::Envelope(const EnvelopeSettings &envelopeSettings)
  10. : envelopeSettings(envelopeSettings) {
  11. // Avoiding division by zero using very short fades
  12. double shortestTime = 0.001;
  13. if (this->envelopeSettings.attack < shortestTime) { this->envelopeSettings.attack = shortestTime; }
  14. if (this->envelopeSettings.hold < shortestTime) { this->envelopeSettings.hold = shortestTime; }
  15. if (this->envelopeSettings.decay < shortestTime) { this->envelopeSettings.decay = shortestTime; }
  16. if (this->envelopeSettings.release < shortestTime) { this->envelopeSettings.release = shortestTime; }
  17. }
  18. static double closerLinear(double &ref, double goal, double maxStep) {
  19. double difference;
  20. if (ref + maxStep < goal) {
  21. difference = maxStep;
  22. ref += maxStep;
  23. } else if (ref - maxStep > goal) {
  24. difference = -maxStep;
  25. ref -= maxStep;
  26. } else {
  27. difference = goal - ref;
  28. ref = goal;
  29. }
  30. return difference;
  31. }
  32. // TODO: Pre-calculate divisions to save time.
  33. double Envelope::getVolume(bool sustained, double seconds) {
  34. if (sustained) {
  35. if (state == 0) {
  36. // Attack
  37. this->currentGoal += seconds / this->envelopeSettings.attack;
  38. if (this->currentGoal > 1.0) {
  39. this->currentGoal = 1.0;
  40. state = 1; this->timeSinceChange = 0.0;
  41. }
  42. } else if (state == 1) {
  43. // Hold
  44. if (this->timeSinceChange < this->envelopeSettings.hold) {
  45. this->currentGoal = 1.0;
  46. } else {
  47. state = 2; this->timeSinceChange = 0.0;
  48. }
  49. } else if (state == 2) {
  50. // Decay
  51. this->currentGoal += (this->envelopeSettings.sustain - 1.0) * seconds / this->envelopeSettings.decay;
  52. if (this->currentGoal < this->envelopeSettings.sustain) {
  53. this->currentGoal = this->envelopeSettings.sustain;
  54. state = 3; this->timeSinceChange = 0.0;
  55. }
  56. } else if (state == 3) {
  57. // Sustain / rise
  58. this->currentGoal += this->envelopeSettings.rise * seconds / this->envelopeSettings.decay;
  59. if (this->currentGoal < 0.0) {
  60. this->currentGoal = 0.0;
  61. } else if (this->currentGoal > 1.0) {
  62. this->currentGoal = 1.0;
  63. }
  64. }
  65. } else {
  66. // Release
  67. if (this->lastSustained) {
  68. this->releaseVolume = this->currentGoal;
  69. }
  70. // Linear release, using releaseVolume to calculate the slope needed for the current release time
  71. this->currentGoal -= this->releaseVolume * seconds / this->envelopeSettings.release;
  72. if (this->currentGoal < 0.0) {
  73. this->currentGoal = 0.0;
  74. }
  75. this->lastSustained = false;
  76. }
  77. double smooth = sustained ? this->envelopeSettings.sustainedSmooth : this->envelopeSettings.releasedSmooth;
  78. if (smooth > 0.0) {
  79. // Move faster to the goal the further away it is
  80. double change = seconds / smooth;
  81. if (change > 1.0) { change = 1.0; }
  82. double keep = 1.0 - change;
  83. this->currentVolume = this->currentVolume * keep + this->currentGoal * change;
  84. // Move slowly towards the goal with a fixed speed to finally reach zero and stop sampling the sound
  85. closerLinear(this->currentVolume, this->currentGoal, seconds * 0.01);
  86. } else {
  87. this->currentVolume = this->currentGoal;
  88. }
  89. this->timeSinceChange += seconds;
  90. return this->currentVolume;
  91. }
  92. bool Envelope::done() {
  93. return this->currentVolume <= 0.0000000001 && !this->lastSustained;
  94. }
  95. void soundEngine_drawEnvelope(ImageRgbaU8 target, const IRect &region, const EnvelopeSettings &envelopeSettings, double releaseTime, double viewTime) {
  96. int32_t top = region.top();
  97. int32_t bottom = region.bottom() - 1;
  98. Envelope envelope = Envelope(envelopeSettings);
  99. double secondsPerPixel = viewTime / region.width();
  100. draw_rectangle(target, region, ColorRgbaI32(0, 0, 0, 255));
  101. draw_rectangle(target, IRect(region.left(), region.top(), region.width() * (releaseTime / viewTime), region.height() / 8), ColorRgbaI32(0, 128, 128, 255));
  102. int32_t oldHardY = bottom;
  103. for (int32_t s = 0; s < region.width(); s++) {
  104. int32_t x = s + region.left();
  105. double time = s * secondsPerPixel;
  106. double smoothLevel = envelope.getVolume(time < releaseTime, secondsPerPixel);
  107. double hardLevel = envelope.currentGoal;
  108. if (envelope.done()) {
  109. draw_line(target, x, top, x, (top * 7 + bottom) / 8, ColorRgbaI32(128, 0, 0, 255));
  110. } else {
  111. draw_line(target, x, (top * smoothLevel) + (bottom * (1.0 - smoothLevel)), x, bottom, ColorRgbaI32(64, 64, 0, 255));
  112. int32_t hardY = (top * hardLevel) + (bottom * (1.0 - hardLevel));
  113. draw_line(target, x, oldHardY, x, hardY, ColorRgbaI32(255, 255, 255, 255));
  114. oldHardY = hardY;
  115. }
  116. }
  117. }
  118. }