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.

102 lines
2.7KB

  1. #pragma once
  2. #include "common.hpp"
  3. #include "math.hpp"
  4. #include <jansson.h>
  5. namespace rack {
  6. namespace app {
  7. struct ParamQuantity;
  8. } // namespace app
  9. namespace engine {
  10. struct ParamQuantityFactory {
  11. virtual ~ParamQuantityFactory() {}
  12. virtual app::ParamQuantity *create() = 0;
  13. };
  14. struct Param {
  15. /** Unstable API. Use setValue() and getValue() instead. */
  16. float value = 0.f;
  17. /** The minimum allowed value. */
  18. float minValue = 0.f;
  19. /** The maximum allowed value. Must be greater than minValue. */
  20. float maxValue = 1.f;
  21. /** The initial value. */
  22. float defaultValue = 0.f;
  23. /** The name of the parameter, using sentence capitalization.
  24. e.g. "Frequency", "Pulse width", "Alternative mode"
  25. */
  26. std::string label;
  27. /** The numerical unit of measurement appended to the value.
  28. Use a space before units to separate the numerical value from the number (e.g. " semitones", " Hz", " V"), unless the unit should have no space (e.g. "%", "ยบ").
  29. */
  30. std::string unit;
  31. /** Set to 0 for linear, positive for exponential, negative for logarithmic. */
  32. float displayBase = 0.f;
  33. float displayMultiplier = 1.f;
  34. float displayOffset = 0.f;
  35. /** An optional one-sentence description of the parameter. */
  36. std::string description;
  37. ParamQuantityFactory *paramQuantityFactory = NULL;
  38. /** Determines whether this Param will be randomized when the user requests to randomize the Module. */
  39. bool randomizable = true;
  40. ~Param() {
  41. if (paramQuantityFactory)
  42. delete paramQuantityFactory;
  43. }
  44. template<class TParamQuantity = app::ParamQuantity>
  45. void config(float minValue, float maxValue, float defaultValue, std::string label = "", std::string unit = "", float displayBase = 0.f, float displayMultiplier = 1.f, float displayOffset = 0.f) {
  46. this->value = defaultValue;
  47. this->minValue = minValue;
  48. this->maxValue = maxValue;
  49. this->defaultValue = defaultValue;
  50. if (!label.empty())
  51. this->label = label;
  52. this->unit = unit;
  53. this->displayBase = displayBase;
  54. this->displayMultiplier = displayMultiplier;
  55. this->displayOffset = displayOffset;
  56. struct TParamQuantityFactory : ParamQuantityFactory {
  57. app::ParamQuantity *create() override {return new TParamQuantity;}
  58. };
  59. if (paramQuantityFactory)
  60. delete paramQuantityFactory;
  61. paramQuantityFactory = new TParamQuantityFactory;
  62. }
  63. float getValue() {
  64. return value;
  65. }
  66. /* Clamps and sets the value. */
  67. void setValue(float value) {
  68. this->value = math::clamp(value, minValue, maxValue);
  69. }
  70. /** Returns whether the Param has finite range between minValue and maxValue. */
  71. bool isBounded() {
  72. return std::isfinite(minValue) && std::isfinite(maxValue);
  73. }
  74. json_t *toJson();
  75. void fromJson(json_t *rootJ);
  76. void reset();
  77. void randomize();
  78. };
  79. } // namespace engine
  80. } // namespace rack