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.

85 lines
2.0KB

  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. ~Param() {
  32. if (paramQuantityFactory)
  33. delete paramQuantityFactory;
  34. }
  35. template<class TParamQuantity = ParamQuantity>
  36. 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) {
  37. this->value = defaultValue;
  38. this->minValue = minValue;
  39. this->maxValue = maxValue;
  40. this->defaultValue = defaultValue;
  41. if (!label.empty())
  42. this->label = label;
  43. this->unit = unit;
  44. this->displayBase = displayBase;
  45. this->displayMultiplier = displayMultiplier;
  46. this->displayOffset = displayOffset;
  47. struct TParamQuantityFactory : ParamQuantityFactory {
  48. ParamQuantity *create() override {return new TParamQuantity;}
  49. };
  50. if (paramQuantityFactory)
  51. delete paramQuantityFactory;
  52. paramQuantityFactory = new TParamQuantityFactory;
  53. }
  54. float getValue() {
  55. return value;
  56. }
  57. void setValue(float value) {
  58. this->value = value;
  59. }
  60. bool isBounded();
  61. json_t *toJson();
  62. void fromJson(json_t *rootJ);
  63. void reset();
  64. void randomize();
  65. };
  66. } // namespace rack