soloud_adsr.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. SoLoud audio engine
  3. Copyright (c) 2013-2021 Jari Komppa
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. */
  19. #ifndef ADSR_H
  20. #define ADSR_H
  21. #include "soloud.h"
  22. namespace SoLoud
  23. {
  24. class ADSR
  25. {
  26. public:
  27. float mA, mD, mS, mR;
  28. ADSR()
  29. {
  30. mA = 0.0f;
  31. mD = 0.0f;
  32. mS = 1.0f;
  33. mR = 0.0f;
  34. }
  35. ADSR(float aA, float aD, float aS, float aR)
  36. {
  37. mA = aA;
  38. mD = aD;
  39. mS = aS;
  40. mR = aR;
  41. }
  42. float val(float aT, float aRelTime)
  43. {
  44. if (aT < mA)
  45. {
  46. return aT / mA;
  47. }
  48. aT -= mA;
  49. if (aT < mD)
  50. {
  51. return 1.0f - ((aT / mD)) * (1.0f - mS);
  52. }
  53. aT -= mD;
  54. if (aT < aRelTime)
  55. return mS;
  56. aT -= aRelTime;
  57. if (aT >= mR)
  58. {
  59. return 0.0f;
  60. }
  61. return (1.0f - aT / mR) * mS;
  62. }
  63. };
  64. };
  65. #endif