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.

117 lines
2.2KB

  1. #include <tinyexpr.h>
  2. #include <Quantity.hpp>
  3. #include <string.hpp>
  4. namespace rack {
  5. float Quantity::getDisplayValue() {
  6. return getValue();
  7. }
  8. void Quantity::setDisplayValue(float displayValue) {
  9. setValue(displayValue);
  10. }
  11. int Quantity::getDisplayPrecision() {
  12. return 5;
  13. }
  14. std::string Quantity::getDisplayValueString() {
  15. float v = getDisplayValue();
  16. if (v == INFINITY)
  17. return "∞";
  18. else if (v == -INFINITY)
  19. return "-∞";
  20. else if (std::isnan(v))
  21. return "NaN";
  22. return string::f("%.*g", getDisplayPrecision(), math::normalizeZero(v));
  23. }
  24. void Quantity::setDisplayValueString(std::string s) {
  25. double result = te_interp(s.c_str(), NULL);
  26. if (std::isfinite(result)) {
  27. setDisplayValue(result);
  28. }
  29. }
  30. std::string Quantity::getString() {
  31. std::string s;
  32. std::string label = getLabel();
  33. std::string valueString = getDisplayValueString() + getUnit();
  34. s += label;
  35. if (label != "" && valueString != "")
  36. s += ": ";
  37. s += valueString;
  38. return s;
  39. }
  40. void Quantity::reset() {
  41. setValue(getDefaultValue());
  42. }
  43. void Quantity::randomize() {
  44. if (isBounded())
  45. setScaledValue(random::uniform());
  46. }
  47. bool Quantity::isMin() {
  48. return getValue() <= getMinValue();
  49. }
  50. bool Quantity::isMax() {
  51. return getValue() >= getMaxValue();
  52. }
  53. void Quantity::setMin() {
  54. setValue(getMinValue());
  55. }
  56. void Quantity::setMax() {
  57. setValue(getMaxValue());
  58. }
  59. void Quantity::toggle() {
  60. setValue(isMin() ? getMaxValue() : getMinValue());
  61. }
  62. void Quantity::setScaledValue(float scaledValue) {
  63. if (!isBounded())
  64. setValue(scaledValue);
  65. else
  66. setValue(math::rescale(scaledValue, 0.f, 1.f, getMinValue(), getMaxValue()));
  67. }
  68. float Quantity::getScaledValue() {
  69. if (!isBounded())
  70. return getValue();
  71. else if (getMinValue() == getMaxValue())
  72. return 0.f;
  73. else
  74. return math::rescale(getValue(), getMinValue(), getMaxValue(), 0.f, 1.f);
  75. }
  76. float Quantity::getRange() {
  77. return getMaxValue() - getMinValue();
  78. }
  79. bool Quantity::isBounded() {
  80. return std::isfinite(getMinValue()) && std::isfinite(getMaxValue());
  81. }
  82. void Quantity::moveValue(float deltaValue) {
  83. setValue(getValue() + deltaValue);
  84. }
  85. void Quantity::moveScaledValue(float deltaScaledValue) {
  86. if (!isBounded())
  87. moveValue(deltaScaledValue);
  88. else
  89. moveValue(deltaScaledValue * getRange());
  90. }
  91. } // namespace rack