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.

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