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.

91 lines
2.3KB

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