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.2KB

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