FruityVocoder.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. // FL Studio's Fruity Vocoder as an exact LYNX mixer effect (docs/analise/REA_rodada70.md, completing REA_rodada20 §4.1;
  3. // clean-room: rebuilt from the observed behaviour of the plugin, no Image-Line code).
  4. //
  5. // The left input is the modulator and the right input the carrier (swapped by the "swap" option). The modulator goes
  6. // through an analysis bank of 4…128 band-passes (1…3 identical biquads in cascade), each followed by an envelope
  7. // follower; the carrier goes through a synthesis bank built from the same band edges (shifted by Formant, mirrored by
  8. // Invert); the output is the sum of the filtered carrier bands times their envelopes, plus the modulator and carrier
  9. // levels of the MIX section. It is mono: the same signal leaves on both sides.
  10. //
  11. // The LYNX slot (EffectKind::vocoder) keeps the controls as its parameters (see Effects.cpp: 0 bands, 1 order,
  12. // 2 listen, 3 swap, 4…15 FL's twelve automatable controls, 16 LYNX's side-input mode) and the per-band levels of the
  13. // analysis bank in EffectSlot::state as FL's own saved state (0xD5 record).
  14. #include <array>
  15. #include <cstdint>
  16. #include <optional>
  17. #include <vector>
  18. namespace lynx::fvocoder
  19. {
  20. constexpr int maxBands = 128;
  21. constexpr int maxOrder = 3;
  22. constexpr int controls = 12; // FL's automatable controls (raw integers)
  23. constexpr int blockFrames = 512; // longer host blocks are split into calls of this size
  24. enum Control
  25. {
  26. freqMin = 0, // 0…65536
  27. freqMax = 1, // 0…65536
  28. freqScale = 2, // 0…128
  29. invert = 3, // 0/1
  30. formant = 4, // −1200…1200 cents
  31. bandwidth = 5, // 5…200 %
  32. attack = 6, // 10…1000 ms
  33. release = 7, // 10…1000 ms
  34. hold = 8, // 0/1
  35. modLevel = 9, // 0…160
  36. carLevel = 10, // 0…160
  37. wetLevel = 11 // 0…160
  38. };
  39. int controlMin (int c) noexcept;
  40. int controlMax (int c) noexcept;
  41. int controlDefault (int c) noexcept;
  42. // The raw control a 0…1 knob (the LYNX slot parameter, (raw − min)/(max − min) within the knob range) stands for.
  43. // FL's state may hold raw values outside the knob range, and the plugin uses them as they are (REA_rodada78 §1): while
  44. // the knob still shows the saved value (clamped to its range), the saved raw value is kept.
  45. int rawFromKnob (float knob, int control, int savedRaw) noexcept;
  46. struct State
  47. {
  48. int bands = 16, order = 2, listen = 2; // bands 4·2^k (4…128), order 1…3, listen 0 modulator / 1 carrier / 2 mix
  49. bool swap = false;
  50. std::array<float, maxBands> levels {}; // analysis band levels (1 = 0 dB); FL resets them to 1 when the band count changes
  51. std::array<int, controls> c { 0, 65536, 64, 0, 0, 50, 20, 100, 0, 0, 0, 128 };
  52. State() { levels.fill (1.0f); }
  53. };
  54. // FL's saved state: i32 version (≥ 2), i32 bands, i32 order, i32 listen, u8 swap, bands × f32 levels, 12 × i32 controls.
  55. std::optional<State> parse (const std::vector<std::uint8_t>& data);
  56. std::vector<std::uint8_t> serialise (const State& s);
  57. bool validBands (int n) noexcept;
  58. // Frequency (Hz) of FreqMin / FreqMax for a raw control value, as the plugin computes it.
  59. float edgeHz (int raw) noexcept;
  60. // Level of the MIX section (modulator / carrier / wet) for a raw 0…160 value.
  61. float mixLevel (int raw) noexcept;
  62. class Engine
  63. {
  64. public:
  65. void prepare (double sampleRate) noexcept;
  66. void reset() noexcept; // states and envelopes to zero (a freshly loaded plugin)
  67. void setState (const State& s) noexcept; // configuration + controls (states kept unless the band count changes)
  68. const State& state() const noexcept { return st; }
  69. // mono modulator and carrier in, mono out (out may alias neither input); any n
  70. void process (const float* mod, const float* car, float* out, int n) noexcept;
  71. // FL's own I/O: stereo in place, L = modulator and R = carrier (or swapped), mono result on both sides
  72. void processStereo (float* left, float* right, int n) noexcept;
  73. private:
  74. struct Biquad
  75. {
  76. double b0 = 0, b1 = 0, b2 = 0, a1 = 0, a2 = 0;
  77. };
  78. struct Band
  79. {
  80. float hz = 0.0f, octaves = 0.0f;
  81. Biquad k;
  82. std::array<std::array<double, 4>, maxOrder> z {}; // per stage: x1, x2, y1, y2
  83. float env = 0.0f, gain = 0.0f;
  84. bool silent = false, active = false;
  85. };
  86. void configure() noexcept;
  87. void buildBank (std::array<Band, maxBands>& bank, bool synthesis) noexcept;
  88. void design (Band& b) const noexcept;
  89. void filter (Band& b, const float* in, float* out, int n) noexcept;
  90. void block (const float* mod, const float* car, float* out, int n) noexcept;
  91. State st;
  92. int rate = 44100;
  93. double radPerHz = 0.0;
  94. float fMin = 10.0f, fMax = 16000.0f, scale = 100.0f, top = 20000.0f, bwPerCent = 0.0f, bandGain = 30.0f;
  95. float gMod = 0.0f, gCar = 0.0f, gWet = 1.0f;
  96. std::array<float, 2> coef { 0.0f, 0.0f }; // attack, release (per-sample pole)
  97. std::array<Band, maxBands> analysis {}, synthesis {};
  98. std::array<float, blockFrames + 1> work {}, carWork {}, acc {};
  99. std::array<float, blockFrames> modBuf {}, carBuf {};
  100. };
  101. } // namespace lynx::fvocoder