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.

89 lines
2.2KB

  1. #pragma once
  2. #include "common.hpp"
  3. #include "math.hpp"
  4. #include <jansson.h>
  5. namespace rack {
  6. struct ParamQuantity;
  7. struct ParamQuantityFactory {
  8. virtual ~ParamQuantityFactory() {}
  9. virtual ParamQuantity *create() = 0;
  10. };
  11. struct Param {
  12. /** Unstable API. Use set/getValue() instead. */
  13. float value = 0.f;
  14. float minValue = 0.f;
  15. float maxValue = 1.f;
  16. float defaultValue = 0.f;
  17. /** The name of the parameter in sentence capitalization
  18. e.g. "Frequency", "Pulse width", "Alternative mode"
  19. */
  20. std::string label;
  21. /** The numerical unit of measurement
  22. Use a space before non-abbreviations to separate the numerical value.
  23. e.g. " semitones", "Hz", "%", "V"
  24. */
  25. std::string unit;
  26. /** Set to 0 for linear, nonzero for exponential */
  27. float displayBase = 0.f;
  28. float displayMultiplier = 1.f;
  29. float displayOffset = 0.f;
  30. /** An optional one-sentence description of the parameter */
  31. std::string description;
  32. ParamQuantityFactory *paramQuantityFactory = NULL;
  33. /** Determines whether this param will be randomized automatically when the user requests to randomize the module state */
  34. bool randomizable = true;
  35. ~Param() {
  36. if (paramQuantityFactory)
  37. delete paramQuantityFactory;
  38. }
  39. template<class TParamQuantity = ParamQuantity>
  40. 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) {
  41. this->value = defaultValue;
  42. this->minValue = minValue;
  43. this->maxValue = maxValue;
  44. this->defaultValue = defaultValue;
  45. if (!label.empty())
  46. this->label = label;
  47. this->unit = unit;
  48. this->displayBase = displayBase;
  49. this->displayMultiplier = displayMultiplier;
  50. this->displayOffset = displayOffset;
  51. struct TParamQuantityFactory : ParamQuantityFactory {
  52. ParamQuantity *create() override {return new TParamQuantity;}
  53. };
  54. if (paramQuantityFactory)
  55. delete paramQuantityFactory;
  56. paramQuantityFactory = new TParamQuantityFactory;
  57. }
  58. float getValue() {
  59. return value;
  60. }
  61. void setValue(float value) {
  62. this->value = math::clamp(value, minValue, maxValue);
  63. }
  64. bool isBounded();
  65. json_t *toJson();
  66. void fromJson(json_t *rootJ);
  67. void reset();
  68. void randomize();
  69. };
  70. } // namespace rack