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.

124 lines
2.5KB

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