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.

99 lines
2.6KB

  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. Units that are words should have a space to separate the numerical value from the number (e.g. " semitones", " octaves").
  29. Unit abbreviations and symbols should have no space (e.g. "V", "ms", "%", "ยบ").
  30. */
  31. std::string unit;
  32. /** Set to 0 for linear, positive for exponential, negative for logarithmic. */
  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. ~Param() {
  40. if (paramQuantityFactory)
  41. delete paramQuantityFactory;
  42. }
  43. template<class TParamQuantity = app::ParamQuantity>
  44. 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) {
  45. this->value = defaultValue;
  46. this->minValue = minValue;
  47. this->maxValue = maxValue;
  48. this->defaultValue = defaultValue;
  49. if (!label.empty())
  50. this->label = label;
  51. this->unit = unit;
  52. this->displayBase = displayBase;
  53. this->displayMultiplier = displayMultiplier;
  54. this->displayOffset = displayOffset;
  55. struct TParamQuantityFactory : ParamQuantityFactory {
  56. app::ParamQuantity *create() override {return new TParamQuantity;}
  57. };
  58. if (paramQuantityFactory)
  59. delete paramQuantityFactory;
  60. paramQuantityFactory = new TParamQuantityFactory;
  61. }
  62. float getValue() {
  63. return value;
  64. }
  65. /* Clamps and sets the value. */
  66. void setValue(float value) {
  67. this->value = math::clampSafe(value, minValue, maxValue);
  68. }
  69. /** Returns whether the Param has finite range between minValue and maxValue. */
  70. bool isBounded() {
  71. return std::isfinite(minValue) && std::isfinite(maxValue);
  72. }
  73. json_t *toJson();
  74. void fromJson(json_t *rootJ);
  75. };
  76. } // namespace engine
  77. } // namespace rack