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.

83 lines
1.9KB

  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. /** An optional one-sentence description of the parameter */
  28. std::string description;
  29. ParamQuantityFactory *paramQuantityFactory = NULL;
  30. ~Param() {
  31. if (paramQuantityFactory)
  32. delete paramQuantityFactory;
  33. }
  34. template<class TParamQuantity = ParamQuantity>
  35. void config(float minValue, float maxValue, float defaultValue, std::string label = "", std::string unit = "", float displayBase = 0.f, float displayMultiplier = 1.f) {
  36. this->value = defaultValue;
  37. this->minValue = minValue;
  38. this->maxValue = maxValue;
  39. this->defaultValue = defaultValue;
  40. if (!label.empty())
  41. this->label = label;
  42. this->unit = unit;
  43. this->displayBase = displayBase;
  44. this->displayMultiplier = displayMultiplier;
  45. struct TParamQuantityFactory : ParamQuantityFactory {
  46. ParamQuantity *create() override {return new TParamQuantity;}
  47. };
  48. if (paramQuantityFactory)
  49. delete paramQuantityFactory;
  50. paramQuantityFactory = new TParamQuantityFactory;
  51. }
  52. float getValue() {
  53. return value;
  54. }
  55. void setValue(float value) {
  56. this->value = value;
  57. }
  58. bool isBounded();
  59. json_t *toJson();
  60. void fromJson(json_t *rootJ);
  61. void reset();
  62. void randomize();
  63. };
  64. } // namespace rack