FormulaController.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. // FL's Fruity Formula Controller (docs/analise/REA_rodada133.md §3): a mixer "controller" effect whose output (0..1)
  3. // is a formula of its three knobs a, b, c and of the song position, linked to any parameter like the Peak
  4. // Controller. The expression language is FL's (its plugin embeds an expression parser): the operators, their
  5. // precedence and every function below follow the plugin's own definitions [DECOMP], case-insensitive.
  6. //
  7. // precedence (tightest first): unary - + and postfix ! % · ^ · * / div mod fmod · + - · > >= <= < <> = in · not ·
  8. // or and xor shr shl
  9. // comparisons give 1/0 (= and <> with a 1e-30 tolerance); not/and/or/xor/shl/shr work on Round()ed integers;
  10. // Round is the banker's rounding of FL's compiler; If(a, b, c) = Round(a) <> 0 ? b : c; Case(a, b, c) = a = 1 ? b : c.
  11. #include <cstdint>
  12. #include <memory>
  13. #include <optional>
  14. #include <string>
  15. #include <vector>
  16. namespace lynx
  17. {
  18. class Effect;
  19. }
  20. namespace lynx::formula
  21. {
  22. // What a formula can read besides its knobs.
  23. struct Inputs
  24. {
  25. double a = 0.0, b = 0.0, c = 0.0;
  26. double songTime = 0.0; // song position in quarter notes
  27. double tempo = 120.0; // GetTempo
  28. double timeMs = 0.0; // Time: system time in ms
  29. double date = 0.0; // Date: days since 1899-12-30 (fraction = time of day)
  30. double mouseX = 0.0, mouseY = 0.0; // 0..1 across the screen
  31. std::uint32_t* randSeed = nullptr; // Rand's generator state (nullptr = a fixed sequence)
  32. };
  33. // FL's SeededRand: the "psdes" hash of the value's 64 bits, as a float in [0, 1).
  34. float seededRand (double seed) noexcept;
  35. // FL's Tension(value, rate).
  36. double tension (double value, double rate) noexcept;
  37. // FL's Fold (folds a signal back into 0..1) and Warp(a, b).
  38. double fold (double value) noexcept;
  39. double warp (double value, double amount) noexcept;
  40. // The banker's rounding of FL's compiler (0.5 → 0, 1.5 → 2).
  41. double roundHalfEven (double v) noexcept;
  42. class Program
  43. {
  44. public:
  45. // nullptr and the message ("Unknown identifier 'x'", "Missing )"…) when the formula doesn't compile.
  46. static std::shared_ptr<const Program> compile (const std::string& text, std::string* error = nullptr);
  47. double evaluate (const Inputs& in) const noexcept;
  48. // Whether it reads the song position / the clock (its output moves on its own).
  49. bool usesTime() const noexcept { return timed; }
  50. bool usesDate() const noexcept { return dated; }
  51. struct Node
  52. {
  53. int op = 0;
  54. double value = 0.0;
  55. int args[3] { -1, -1, -1 };
  56. };
  57. private:
  58. std::vector<Node> nodes;
  59. int root = -1;
  60. bool timed = false, dated = false;
  61. double eval (int node, const Inputs& in) const noexcept;
  62. friend struct Compiler;
  63. };
  64. // FL's saved state (the plugin's 0xD5 record): i32 version, 3 × i32 knobs a, b, c (0…65536), the formula as a
  65. // short string (u8 length + ANSI), then the description (v ≤ 2: i32 length + RTF; v ≥ 4: plain UTF-16 text).
  66. struct State
  67. {
  68. int version = 2;
  69. float a = 0.0f, b = 0.0f, c = 0.0f; // 0..1
  70. std::string formula = "a+b+c";
  71. };
  72. std::optional<State> parseState (const std::vector<std::uint8_t>& data);
  73. // The state LYNX saves (version 2, no description) — what FL's own reader takes.
  74. std::vector<std::uint8_t> writeState (const State& state);
  75. // The formula of a slot's state ("a+b+c" when it has none, as FL's default preset).
  76. std::string formulaOf (const std::vector<std::uint8_t>& state);
  77. // The mixer effect: audio passes through; output 0 = clamp (formula (params[0..2] = a, b, c), 0, 1), evaluated once
  78. // per block. A formula that doesn't compile outputs 0 (FL: "make sure you pressed Compile").
  79. std::unique_ptr<Effect> makeEffect (const std::vector<std::uint8_t>& state);
  80. // The mouse position the formulas see (MouseX / MouseY, 0..1 of the screen); set by the UI.
  81. void setMousePosition (double x, double y) noexcept;
  82. } // namespace lynx::formula