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.

Param.hpp 2.5KB

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