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.

107 lines
2.1KB

  1. #include "app/ParamQuantity.hpp"
  2. namespace rack {
  3. Param *ParamQuantity::getParam() {
  4. assert(module);
  5. return &module->params[paramId];
  6. }
  7. void ParamQuantity::commitSnap() {
  8. // TODO
  9. }
  10. void ParamQuantity::setValue(float value) {
  11. if (!module)
  12. return;
  13. value = math::clamp(value, getMinValue(), getMaxValue());
  14. // TODO Smooth
  15. // TODO Snap
  16. getParam()->value = value;
  17. }
  18. float ParamQuantity::getValue() {
  19. if (!module)
  20. return 0.f;
  21. return getParam()->value;
  22. }
  23. float ParamQuantity::getMinValue() {
  24. if (!module)
  25. return -1.f;
  26. return getParam()->minValue;
  27. }
  28. float ParamQuantity::getMaxValue() {
  29. if (!module)
  30. return 1.f;
  31. return getParam()->maxValue;
  32. }
  33. float ParamQuantity::getDefaultValue() {
  34. if (!module)
  35. return 0.f;
  36. return getParam()->defaultValue;
  37. }
  38. float ParamQuantity::getDisplayValue() {
  39. if (!module)
  40. return Quantity::getDisplayValue();
  41. if (getParam()->displayBase == 0.f) {
  42. // Linear
  43. return getValue() * getParam()->displayMultiplier;
  44. }
  45. else if (getParam()->displayBase == 1.f) {
  46. // Fixed (special case of exponential)
  47. return getParam()->displayMultiplier;
  48. }
  49. else {
  50. // Exponential
  51. return std::pow(getParam()->displayBase, getValue()) * getParam()->displayMultiplier;
  52. }
  53. }
  54. void ParamQuantity::setDisplayValue(float displayValue) {
  55. if (!module)
  56. return;
  57. if (getParam()->displayBase == 0.f) {
  58. // Linear
  59. setValue(displayValue / getParam()->displayMultiplier);
  60. }
  61. else if (getParam()->displayBase == 1.f) {
  62. // Fixed
  63. setValue(getParam()->displayMultiplier);
  64. }
  65. else {
  66. // Exponential
  67. setValue(std::log(displayValue / getParam()->displayMultiplier) / std::log(getParam()->displayBase));
  68. }
  69. }
  70. int ParamQuantity::getDisplayPrecision() {
  71. if (!module)
  72. return Quantity::getDisplayPrecision();
  73. float displayValue = getDisplayValue();
  74. if (displayValue == 0.f)
  75. return 0;
  76. float log = std::log10(std::abs(getDisplayValue()));
  77. return (int) std::ceil(math::clamp(-log + 3.f, 0.f, 6.f));
  78. }
  79. std::string ParamQuantity::getLabel() {
  80. if (!module)
  81. return Quantity::getLabel();
  82. return getParam()->label;
  83. }
  84. std::string ParamQuantity::getUnit() {
  85. if (!module)
  86. return Quantity::getUnit();
  87. return getParam()->unit;
  88. }
  89. } // namespace rack