FadeLaws.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * FadeLaws.h
  3. * ----------
  4. * Purpose: Various fade law implementations for sample and pattern fading / interpolation
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include "mpt/base/numbers.hpp"
  12. #include <cmath>
  13. OPENMPT_NAMESPACE_BEGIN
  14. namespace Fade
  15. {
  16. enum Law
  17. {
  18. kLinear,
  19. kPow,
  20. kSqrt,
  21. kLog,
  22. kQuarterSine,
  23. kHalfSine,
  24. };
  25. // Maps fade curve position in [0,1] to value in [0,1]
  26. typedef double (*Func) (double pos);
  27. inline double LinearFunc(double pos)
  28. { return pos; }
  29. inline double PowFunc(double pos)
  30. { return pos * pos; }
  31. inline double SqrtFunc(double pos)
  32. { return std::sqrt(pos); }
  33. inline double LogFunc(double pos)
  34. { return std::log10(1.0 + pos * 99.0) * 0.5; }
  35. inline double QuarterSineFunc(double pos)
  36. { return std::sin((0.5 * mpt::numbers::pi) * pos); }
  37. inline double HalfSineFunc(double pos)
  38. { return (1.0 + std::cos(mpt::numbers::pi + mpt::numbers::pi * pos)) * 0.5; }
  39. inline Func GetFadeFunc(Law fadeLaw)
  40. {
  41. switch(fadeLaw)
  42. {
  43. default:
  44. case Fade::kLinear: return LinearFunc;
  45. case Fade::kPow: return PowFunc;
  46. case Fade::kSqrt: return SqrtFunc;
  47. case Fade::kLog: return LogFunc;
  48. case Fade::kQuarterSine: return QuarterSineFunc;
  49. case Fade::kHalfSine: return HalfSineFunc;
  50. }
  51. }
  52. }
  53. OPENMPT_NAMESPACE_END