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.

87 lines
2.1KB

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