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.

41 lines
788B

  1. #include "engine/Param.hpp"
  2. #include "random.hpp"
  3. #include "math.hpp"
  4. namespace rack {
  5. json_t *Param::toJson() {
  6. json_t *rootJ = json_object();
  7. float v = 0.f;
  8. // Infinite params should serialize to 0
  9. if (std::isfinite(minValue) && std::isfinite(maxValue))
  10. v = value;
  11. json_object_set_new(rootJ, "value", json_real(v));
  12. return rootJ;
  13. }
  14. void Param::fromJson(json_t *rootJ) {
  15. json_t *valueJ = json_object_get(rootJ, "value");
  16. if (valueJ)
  17. value = json_number_value(valueJ);
  18. }
  19. void Param::reset() {
  20. if (std::isfinite(minValue) && std::isfinite(maxValue)) {
  21. value = defaultValue;
  22. }
  23. }
  24. void Param::randomize() {
  25. if (std::isfinite(minValue) && std::isfinite(maxValue)) {
  26. value = math::rescale(random::uniform(), 0.f, 1.f, minValue, maxValue);
  27. }
  28. }
  29. } // namespace rack