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.

114 lines
2.4KB

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