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.

112 lines
2.1KB

  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. if (!label.empty())
  34. s += label + ": ";
  35. s += getDisplayValueString();
  36. s += getUnit();
  37. return s;
  38. }
  39. void Quantity::reset() {
  40. setValue(getDefaultValue());
  41. }
  42. void Quantity::randomize() {
  43. if (isBounded())
  44. setScaledValue(random::uniform());
  45. }
  46. bool Quantity::isMin() {
  47. return getValue() <= getMinValue();
  48. }
  49. bool Quantity::isMax() {
  50. return getValue() >= getMaxValue();
  51. }
  52. void Quantity::setMin() {
  53. setValue(getMinValue());
  54. }
  55. void Quantity::setMax() {
  56. setValue(getMaxValue());
  57. }
  58. void Quantity::setScaledValue(float scaledValue) {
  59. if (!isBounded())
  60. setValue(scaledValue);
  61. else
  62. setValue(math::rescale(scaledValue, 0.f, 1.f, getMinValue(), getMaxValue()));
  63. }
  64. float Quantity::getScaledValue() {
  65. if (!isBounded())
  66. return getValue();
  67. else if (getMinValue() == getMaxValue())
  68. return 0.f;
  69. else
  70. return math::rescale(getValue(), getMinValue(), getMaxValue(), 0.f, 1.f);
  71. }
  72. float Quantity::getRange() {
  73. return getMaxValue() - getMinValue();
  74. }
  75. bool Quantity::isBounded() {
  76. return std::isfinite(getMinValue()) && std::isfinite(getMaxValue());
  77. }
  78. void Quantity::moveValue(float deltaValue) {
  79. setValue(getValue() + deltaValue);
  80. }
  81. void Quantity::moveScaledValue(float deltaScaledValue) {
  82. if (!isBounded())
  83. moveValue(deltaScaledValue);
  84. else
  85. moveValue(deltaScaledValue * getRange());
  86. }
  87. } // namespace rack