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.

118 lines
2.2KB

  1. #include <engine/ParamQuantity.hpp>
  2. #include <app.hpp>
  3. #include <engine/Engine.hpp>
  4. namespace rack {
  5. namespace engine {
  6. engine::Param* ParamQuantity::getParam() {
  7. assert(module);
  8. return &module->params[paramId];
  9. }
  10. void ParamQuantity::setSmoothValue(float smoothValue) {
  11. if (!module)
  12. return;
  13. smoothValue = math::clampSafe(smoothValue, getMinValue(), getMaxValue());
  14. APP->engine->setSmoothParam(module, paramId, smoothValue);
  15. }
  16. float ParamQuantity::getSmoothValue() {
  17. if (!module)
  18. return 0.f;
  19. return APP->engine->getSmoothParam(module, paramId);
  20. }
  21. void ParamQuantity::setValue(float value) {
  22. if (!module)
  23. return;
  24. value = math::clampSafe(value, getMinValue(), getMaxValue());
  25. APP->engine->setParam(module, paramId, value);
  26. }
  27. float ParamQuantity::getValue() {
  28. if (!module)
  29. return 0.f;
  30. return APP->engine->getParam(module, paramId);
  31. }
  32. float ParamQuantity::getMinValue() {
  33. return minValue;
  34. }
  35. float ParamQuantity::getMaxValue() {
  36. return maxValue;
  37. }
  38. float ParamQuantity::getDefaultValue() {
  39. return defaultValue;
  40. }
  41. float ParamQuantity::getDisplayValue() {
  42. if (!module)
  43. return Quantity::getDisplayValue();
  44. float v = getSmoothValue();
  45. if (displayBase == 0.f) {
  46. // Linear
  47. // v is unchanged
  48. }
  49. else if (displayBase < 0.f) {
  50. // Logarithmic
  51. v = std::log(v) / std::log(-displayBase);
  52. }
  53. else {
  54. // Exponential
  55. v = std::pow(displayBase, v);
  56. }
  57. return v * displayMultiplier + displayOffset;
  58. }
  59. void ParamQuantity::setDisplayValue(float displayValue) {
  60. if (!module)
  61. return;
  62. float v = displayValue - displayOffset;
  63. if (displayMultiplier == 0.f)
  64. v = 0.f;
  65. else
  66. v /= displayMultiplier;
  67. if (displayBase == 0.f) {
  68. // Linear
  69. // v is unchanged
  70. }
  71. else if (displayBase < 0.f) {
  72. // Logarithmic
  73. v = std::pow(-displayBase, v);
  74. }
  75. else {
  76. // Exponential
  77. v = std::log(v) / std::log(displayBase);
  78. }
  79. setValue(v);
  80. }
  81. int ParamQuantity::getDisplayPrecision() {
  82. return Quantity::getDisplayPrecision();
  83. }
  84. std::string ParamQuantity::getDisplayValueString() {
  85. return Quantity::getDisplayValueString();
  86. }
  87. void ParamQuantity::setDisplayValueString(std::string s) {
  88. Quantity::setDisplayValueString(s);
  89. }
  90. std::string ParamQuantity::getLabel() {
  91. return label;
  92. }
  93. std::string ParamQuantity::getUnit() {
  94. return unit;
  95. }
  96. } // namespace engine
  97. } // namespace rack