PluginSubstitutes.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #pragma once
  2. // LYNX substitutes for missing third-party plugins (docs/PLUGIN_FALLBACK.md).
  3. //
  4. // When a project (.flp or .lynx) uses a third-party plugin that isn't installed here, LYNX can put one of its own
  5. // built-in effects "in the role of" that plugin: a reverb for a reverb, a delay for a delay... The slot keeps the
  6. // ORIGINAL plugin reference byte for byte (EffectSlot::substituteFor), so the project saves it back unchanged and
  7. // reopens with the real plugin on a machine that has it.
  8. //
  9. // What stands in for what is DATA: a text table (built in, extendable with registerPluginSubstitutes) of entries
  10. // that name the third-party product (VST2 unique IDs, VST3 class IDs, CLAP ids, plugin / file names, Waves shell
  11. // sub-plugin names), the LYNX effect, a default preset and, where the saved state is readable, a parameter map.
  12. // Parameter maps only use layouts that are visible in the saved data itself (VST2 float parameters, the attributes of
  13. // a JUCE XML state, Waves' "RealWorld" parameter lists) and meanings known from public documentation or our own
  14. // black-box measurements; never decompiled third-party code. Without a readable state the default preset is used.
  15. //
  16. // Does not depend on JUCE so it can be tested in isolation.
  17. #include <array>
  18. #include <cstdint>
  19. #include <map>
  20. #include <optional>
  21. #include <string>
  22. #include <string_view>
  23. #include <utility>
  24. #include <vector>
  25. #include "core/Song.h"
  26. namespace lynx
  27. {
  28. // One parameter of the substitute taken from the original plugin's saved state.
  29. struct SubstituteParamMap
  30. {
  31. enum class Source : std::uint8_t
  32. {
  33. vst2, // FL's VST2 state (parseFlVst2State): float parameter `index` of the current program, 0..1
  34. xml, // attribute `attribute` of the first element of a JUCE XML state ("VC2!" block or plain "<?xml")
  35. waves // value `index` of the active setup of a Waves "PresetChunkXMLTree" (real-world units)
  36. };
  37. Source source = Source::vst2;
  38. int index = 0;
  39. std::string attribute;
  40. // target: a parameter of the LYNX effect (by position), or the slot's dry/wet (`toSlotMix`)
  41. int target = 0;
  42. bool toSlotMix = false;
  43. // linear map, clamped: in [inLo, inHi] -> out [outLo, outHi] (0..1 normalised parameter)
  44. float inLo = 0.0f, inHi = 1.0f, outLo = 0.0f, outHi = 1.0f;
  45. // or a point list ("| curve x:y x:y …": piecewise linear, clamped at the ends; "| steps x:y …": the y of the last
  46. // point whose x ≤ the value — enumerations), x ascending
  47. enum class Shape : std::uint8_t { linear, curve, steps };
  48. Shape shape = Shape::linear;
  49. std::vector<std::pair<float, float>> points;
  50. };
  51. struct SubstituteEntry
  52. {
  53. std::string key; // stable id of the entry ("valhalla-vintageverb")
  54. std::string product; // the third-party product, for display ("ValhallaVintageVerb")
  55. std::string vendor;
  56. bool instrument = false; // reserved: the loader substitutes effects only (instrument entries are not applied yet)
  57. // what identifies the product
  58. std::vector<std::uint32_t> vst2Ids;
  59. std::vector<std::array<std::uint8_t, 16>> vst3ClassIds; // TUID as laid out in memory (FL's record 0x34)
  60. std::vector<std::string> clapIds;
  61. std::vector<std::string> names; // pluginNameKey of plugin / file names ("valhallavintageverb")
  62. std::vector<std::string> wavesNames; // pluginNameKey of a WaveShell sub-plugin's <PluginName> ("rcomp")
  63. // the LYNX effect and its default preset
  64. EffectKind effect = EffectKind::eq;
  65. std::vector<std::pair<int, float>> preset; // parameter position -> normalised value, over makeEffectSlot (effect)
  66. float mix = 1.0f; // slot dry/wet of the substitute
  67. std::vector<SubstituteParamMap> params;
  68. // a built-in state translator (core/FabFilterStates.h: "pro-c3", "pro-mb", "pro-g", "saturn2") run after the
  69. // maps; it reads the vendor's state layout we observed black-box and only acts when that layout matches
  70. std::string translator;
  71. // VST2 parameters per program (AEffect::numParams), needed to find the current program in a state saved with
  72. // "all programs" (FL doesn't store the count); 0 = unknown (then only program 0 can be read from such a state)
  73. int vst2Params = 0;
  74. };
  75. // What a substitute is matched on and translated from: the original plugin's identity and its own saved state.
  76. struct PluginIdentity
  77. {
  78. ForeignPlugin::Format format = ForeignPlugin::Format::none;
  79. std::string name, path;
  80. std::uint32_t vst2Id = 0;
  81. std::array<std::uint8_t, 16> classId {};
  82. bool hasClassId = false;
  83. std::string clapId;
  84. // VST2: FL's state layout (the 0x35 record / LYNX's VST2 host state); VST3: the component state; CLAP: the raw state
  85. std::vector<std::uint8_t> state;
  86. };
  87. // The identity of an .flp plugin (FL's wrapper record); for VST3 the state is the component part of FL's state.
  88. PluginIdentity pluginIdentity (const ForeignPlugin& plugin);
  89. // Parses entries in the table format (docs/PLUGIN_FALLBACK.md). Lines it can't use are skipped and counted in
  90. // `errors` (when given). Entries whose LYNX effect name is unknown are dropped (counted as errors).
  91. std::vector<SubstituteEntry> parseSubstituteTable (std::string_view text, int* errors = nullptr);
  92. // The table in use: the built-in entries plus the registered ones (later registrations win on the same key).
  93. const std::vector<SubstituteEntry>& pluginSubstitutes();
  94. // Adds entries (table text) — the hook other LYNX modules use to offer themselves as substitutes. Returns how many
  95. // entries were added. Call it from the UI/message thread before projects open.
  96. int registerPluginSubstitutes (std::string_view tableText);
  97. // The entry standing in for `plugin` (effect or instrument), or null. Matches by VST2 ID, VST3 class ID, CLAP id,
  98. // Waves sub-plugin name (read from the state), then by plugin / file name (a WaveShell file name never matches
  99. // by name: the shell holds many plugins).
  100. const SubstituteEntry* findPluginSubstitute (const PluginIdentity& plugin, bool instrument);
  101. // The built-in effect slot standing in for the plugin: the entry's preset, then the parameters the saved state lets
  102. // us translate. `translated` (when given) gets how many parameters came from the state.
  103. EffectSlot makeSubstituteSlot (const SubstituteEntry& entry, const PluginIdentity* original = nullptr, int* translated = nullptr);
  104. // Replaces a plugin effect slot (kind == plugin) by its substitute, keeping the original reference in
  105. // `substituteFor` and the slot's on/off, dry/wet (multiplied by the substitute's) and side-chain routing.
  106. // False (slot unchanged) when no entry matches. `identity` is the original's identity (pluginIdentity for .flp).
  107. bool substituteEffectSlot (EffectSlot& slot, const PluginIdentity& identity);
  108. // What the whole project says about the settings a substitute can't read from its own original (round 113): the key
  109. // and scale of the song, taken from the pitch correctors whose saved state is readable (Auto-Tune Pro). A pitch
  110. // corrector whose own key is unknown (Waves Tune, an unreadable state) then corrects to the project's key instead of
  111. // guessing one. Round 120: Waves Tune Real-Time states name their scale too (wavesTuneScale).
  112. struct SubstituteContext
  113. {
  114. int key = -1; // 0 = C … 11 = B; −1 = unknown
  115. int scale = -1; // LYNX Tune's scale index (vocal::Scale); −1 = unknown
  116. bool knowsKey() const noexcept { return key >= 0 && scale >= 0; }
  117. };
  118. // The context of a song: every plugin slot (hosted, unresolved or already substituted) whose original state names a
  119. // key; the most common key / scale wins (ties: the first slot's).
  120. SubstituteContext substituteContextOf (const Song& song);
  121. // As above, using `context` for what the original's own state doesn't tell.
  122. bool substituteEffectSlot (EffectSlot& slot, const PluginIdentity& identity, const SubstituteContext& context);
  123. // The key and scale an Auto-Tune Pro component state was saved with (its <Custom> element's ScaleGivenKey /
  124. // ScaleGivenScale), as LYNX Tune's key index and scale (vocal::Scale index); nullopt when not readable or a scale
  125. // LYNX Tune has no equivalent for. Exposed for the tests.
  126. std::optional<std::pair<int, int>> autoTuneProKey (const std::vector<std::uint8_t>& state);
  127. // The scale a Waves Tune Real-Time state was saved with (round 120): the active setup's <Scale> element (its root and
  128. // the twelve note statuses above it, "x" = excluded). `scale` is the LYNX Tune scale whose notes on `key` are exactly
  129. // these, else vocal::Scale::custom with `mask` (bit k = pitch class k, absolute) holding them. nullopt when absent.
  130. // The parameters an Auto-Tune Pro state holds (its ParamsData: X<n> = parameter n in real units; [EXEC] round 120:
  131. // 1 scale, 2 key, 4 retune speed ms, 61 humanize, 90 flex-tune, 62 natural vibrato dB). Empty when not readable.
  132. std::map<int, double> autoTuneProParams (const std::vector<std::uint8_t>& state);
  133. // Round 128: the active scale's per-note edits of an Auto-Tune Pro state (legacy <Scales> block): {bypass, remove},
  134. // bit k = absolute pitch class k. `lynxScale` = vocal::Scale major / minor / chromatic (others: nullopt).
  135. std::optional<std::pair<std::uint16_t, std::uint16_t>> autoTuneProNoteEdits (const std::vector<std::uint8_t>& state, int key, int lynxScale);
  136. struct PitchCorrectorScale
  137. {
  138. int key = 0;
  139. int scale = 0;
  140. std::uint16_t mask = 0xfff;
  141. };
  142. std::optional<PitchCorrectorScale> wavesTuneScale (const std::vector<std::uint8_t>& state);
  143. // The key / scale of any readable pitch-corrector state (Auto-Tune Pro, Waves Tune Real-Time), as above.
  144. std::optional<PitchCorrectorScale> pitchCorrectorScale (const std::vector<std::uint8_t>& state);
  145. // Fresh Air's two "air" amounts (the two floats of its saved state, 0..1: mid and high air); nullopt when the state
  146. // doesn't have that layout. Exposed for the tests.
  147. std::optional<std::pair<float, float>> freshAirAmounts (const std::vector<std::uint8_t>& state);
  148. // A substitute slot back to its original plugin (kind = plugin, plugin = substituteFor). False if not a substitute.
  149. bool restoreOriginalPlugin (EffectSlot& slot);
  150. // Display name of the original plugin of a substitute slot ("" when not a substitute).
  151. std::string substituteOriginalName (const EffectSlot& slot);
  152. // --- state readers (exposed for tests) --------------------------------------------------------------------------
  153. // The attributes of the first element of the JUCE XML state inside `state` (after "VC2!" or the first "<?xml").
  154. std::vector<std::pair<std::string, std::string>> juceXmlStateAttributes (const std::vector<std::uint8_t>& state);
  155. // The <PluginName> of a Waves preset chunk ("" when not a Waves state).
  156. std::string wavesPluginName (const std::vector<std::uint8_t>& state);
  157. // The name of the preset the original plugin was saved with (round 135): a Valhalla state's `presetName` attribute,
  158. // a Waves chunk's <Preset Name="...">; "" when none is stored.
  159. std::string originalPresetName (const std::vector<std::uint8_t>& state);
  160. // The "RealWorld" parameter values of the active setup of a Waves preset chunk ("*" = unused -> NaN).
  161. std::vector<double> wavesRealWorldParams (const std::vector<std::uint8_t>& state);
  162. // Round 135: the FNV-1a hash of a Soundtoys state's PRESET payload (nullopt when not one), and how many of her
  163. // Soundtoys states (read black-box in the official trials) are translated by it.
  164. std::optional<std::uint64_t> soundtoysStateHash (const std::vector<std::uint8_t>& state);
  165. int knownSoundtoysStates() noexcept;
  166. // --- lossless text form of an .flp plugin reference (.lynx keeps it for substitutes) ------------------------------
  167. // Base64 of a versioned binary record of every ForeignPlugin field; foreignFromText(foreignToText(p)) == p, the
  168. // state byte for byte.
  169. std::string foreignToText (const ForeignPlugin& plugin);
  170. bool foreignFromText (const std::string& text, ForeignPlugin& out);
  171. std::string base64Encode (const std::uint8_t* data, std::size_t size);
  172. bool base64Decode (const std::string& text, std::vector<std::uint8_t>& out);
  173. // Round 136: the FNV-1a 64 hash of a Soundtoys state's encoded preset (the bytes after "PRESET =", without the final ';').
  174. // LYNX does not decode those presets; her saved states are recognised by this hash (core/SoundtoysStates.inc).
  175. std::uint64_t soundtoysPresetHash (const std::vector<std::uint8_t>& state);
  176. } // namespace lynx