FlEffects.cpp 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436
  1. #include "core/FlEffects.h"
  2. #include "core/ControlSurface.h"
  3. #include "core/XyController.h"
  4. #include "core/FormulaController.h"
  5. #include "core/Reeverb2.h"
  6. #include "core/LuxeVerb.h"
  7. #include "core/ConvolverFx.h"
  8. #include "core/VintagePhaser.h"
  9. #include "core/Vocodex.h"
  10. #include "core/FruityVocoder.h"
  11. #include "core/MaximusFx.h"
  12. #include "core/EquoFx.h"
  13. #include "core/HardcoreFx.h"
  14. #include "core/DistructorFx.h"
  15. #include "core/MultibandDelayFx.h"
  16. #include "core/PitchShifterFl.h"
  17. #include "core/ParametricEqFl.h"
  18. #include "core/FruityDynamics.h"
  19. #include "core/FruityShapers.h"
  20. #include "core/FruityBasicFx.h"
  21. #include "core/FruityFilters.h"
  22. #include "core/FruityDelay.h"
  23. #include "core/StereoFx.h"
  24. #include "core/FrequencySplitter.h"
  25. #include "core/ChorusFx.h"
  26. #include "core/GrossBeat.h"
  27. #include "core/LovePhilter.h"
  28. #include "core/FruityDelayBank.h"
  29. #include "core/FruityMultiband.h"
  30. #include "core/FlangusFastDist.h"
  31. #include "core/FruityPhaser.h"
  32. #include "core/EmphasizerFx.h"
  33. #include "core/FlLegacyFx.h"
  34. #include "core/FreqShifterFl.h"
  35. #include "core/LowLifterFl.h"
  36. #include "core/SytrusEngine.h"
  37. #include "core/FlSynths.h"
  38. #include <algorithm>
  39. #include <functional>
  40. #include <cctype>
  41. #include <cmath>
  42. #include <cstring>
  43. #include "core/Effects.h"
  44. #include "core/FlpImport.h"
  45. #include "core/NewtoneFx.h"
  46. #include "core/VisualFx.h"
  47. #include "core/FlExtraFx.h"
  48. #include "core/Scratcher.h"
  49. #include "core/Transmitter.h"
  50. #include "flp/Flp.h"
  51. namespace lynx
  52. {
  53. namespace
  54. {
  55. std::int32_t i32At (const std::vector<std::uint8_t>& d, std::size_t offset)
  56. {
  57. std::int32_t v = 0;
  58. if (offset + 4 <= d.size())
  59. std::memcpy (&v, d.data() + offset, 4);
  60. return v;
  61. }
  62. // Lk(x) = (k^x − 1)/(k − 1): the gain curves of the Fruity plugins
  63. double curve (double k, double x)
  64. {
  65. return (std::pow (k, std::max (0.0, x)) - 1.0) / (k - 1.0);
  66. }
  67. double toDb (double gain)
  68. {
  69. return 20.0 * std::log10 (std::max (gain, 1.0e-6));
  70. }
  71. // inverse of logRange (lo·(hi/lo)^v)
  72. float fromLog (double value, double lo, double hi)
  73. {
  74. return (float) std::clamp (std::log (std::max (value, lo) / lo) / std::log (hi / lo), 0.0, 1.0);
  75. }
  76. float clamp01 (double v)
  77. {
  78. return (float) std::clamp (v, 0.0, 1.0);
  79. }
  80. // Q of a peak/band-pass from the width in octaves (cookbook)
  81. double qFromOctaves (double octaves)
  82. {
  83. return 1.0 / (2.0 * std::sinh (std::log (2.0) / 2.0 * std::max (0.01, octaves)));
  84. }
  85. // --- Fruity Parametric EQ 2 (REA_rodada7 §1) ------------------------------------------------
  86. bool parametricEq2 (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  87. {
  88. // FL's own engine (REA_rodada50): the slot keeps FL's values and runs the exact Parametric EQ 2
  89. if (const auto exact = peqfl::parsePeq2 (d))
  90. {
  91. peqfl::toParams (*exact, slot.params);
  92. return true;
  93. }
  94. // older approximation (REA_rodada7 §1), for states too short to be FL's
  95. if (d.size() < 4 + 36 * 4)
  96. return false;
  97. const auto version = i32At (d, 0);
  98. const auto mask = version >= 4 && d.size() >= 0x142 ? (std::uint32_t) i32At (d, 0x13E) : 0x7Fu;
  99. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  100. for (int b = 0; b < peq::bands; ++b)
  101. {
  102. const auto gainDb = std::clamp (param (b) / 100.0, -18.0, 18.0);
  103. const auto hz = 20.0 * std::pow (1000.0, std::clamp (param (7 + b), 0, 65536) / 65536.0);
  104. const auto w = std::clamp (param (14 + b), 0, 65536) / 65536.0;
  105. const auto type = param (21 + b);
  106. const auto order = std::clamp (param (28 + b), -3, 3);
  107. const auto on = (mask & (1u << b)) != 0;
  108. peq::BandType t = peq::BandType::off;
  109. double q = 1.0;
  110. switch (on ? type : 0)
  111. {
  112. case 1: t = peq::BandType::highCut; q = 0.1 + 9.9 * std::pow (1.0 - w, 3.0); break;
  113. case 2: t = peq::BandType::bandPass; q = qFromOctaves ((0.1 + 4.9 * w) * 0.708 * std::sqrt (std::abs (order) + 1.0)); break;
  114. case 3: t = peq::BandType::lowCut; q = 0.1 + 9.9 * std::pow (1.0 - w, 3.0); break;
  115. case 4: t = peq::BandType::notch; q = qFromOctaves ((0.1 + 4.9 * w) / (0.708 * std::sqrt (std::abs (order) + 1.0))); break;
  116. case 5: t = peq::BandType::lowShelf; q = 1.0 / 3.0 + 1.7193 * (1.0 - w); break; // Q = shelf slope S
  117. case 6: t = peq::BandType::peak; q = qFromOctaves (0.1 + 4.9 * w); break;
  118. case 7: t = peq::BandType::highShelf; q = 1.0 / 3.0 + 1.7193 * (1.0 - w); break;
  119. default: break;
  120. }
  121. slot.params[(std::size_t) peq::param (b, 0)] = peq::typeValue (t);
  122. slot.params[(std::size_t) peq::param (b, 1)] = peq::hzValue (hz);
  123. slot.params[(std::size_t) peq::param (b, 2)] = peq::gainValue (gainDb);
  124. slot.params[(std::size_t) peq::param (b, 3)] = peq::qValue (q);
  125. // order: |order| + 1 biquads (the "Steep 8" cuts = 4)
  126. slot.params[(std::size_t) (peq::slopeParam + b)] = peq::slopeValue (std::abs (order) + 1);
  127. }
  128. slot.params[peq::outputParam] = peq::gainValue (std::clamp (param (35) / 100.0, -18.0, 18.0));
  129. return true;
  130. }
  131. // --- Fruity Limiter (§2): limiter, or its compressor when only the compressor is active -------
  132. bool limiter (const std::vector<std::uint8_t>& d, EffectSlot& slot, bool forceCompressor = false)
  133. {
  134. if (d.size() < 4 + 18 * 4)
  135. return false;
  136. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  137. auto timeMs = [] (int v) { return 2.0 * (std::pow (501.0, std::clamp (v, 0, 10000) / 10000.0) - 1.0); };
  138. const auto gainDb = toDb (curve (7.0, param (0) / 1000.0));
  139. const auto ceilingDb = toDb (curve (3.0, param (2) / 1000.0));
  140. const auto compThresholdDb = toDb (curve (7.0, param (8) / 1000.0));
  141. const auto ratioRaw = param (9);
  142. const auto ratio = ratioRaw > 0 ? 1.0 + 0.38 * (std::pow (51.0, ratioRaw / 1000.0) - 1.0) : 1.0;
  143. // a ceiling above 0 dB practically disables the limiter; then what you hear is the compressor
  144. if (forceCompressor || (ceilingDb > -0.1 && ratio > 1.05 && compThresholdDb < -0.5))
  145. {
  146. slot = makeEffectSlot (EffectKind::compressor);
  147. slot.params[0] = clamp01 ((compThresholdDb + 60.0) / 60.0);
  148. slot.params[1] = clamp01 ((std::min (ratio, 20.0) - 1.0) / 19.0);
  149. slot.params[2] = fromLog (std::max (0.1, timeMs (param (11))), 0.1, 100.0);
  150. slot.params[3] = fromLog (2.0 * timeMs (param (12)), 10.0, 1000.0);
  151. slot.params[4] = clamp01 (std::max (0.0, gainDb) / 24.0);
  152. return true;
  153. }
  154. slot = makeEffectSlot (EffectKind::limiter);
  155. slot.params[0] = clamp01 ((std::min (ceilingDb, 0.0) + 12.0) / 12.0);
  156. slot.params[1] = clamp01 (std::max (0.0, gainDb) / 24.0);
  157. slot.params[2] = fromLog (2.0 * timeMs (param (5)), 10.0, 1000.0);
  158. return true;
  159. }
  160. // --- Fruity Reeverb 2 (§3) --------------------------------------------------------------------
  161. bool reeverb2Approx (const std::vector<std::uint8_t>& d, EffectSlot& slot, double bpm)
  162. {
  163. const auto version = i32At (d, 0);
  164. const auto count = version == 10001 ? 15 : version == 10000 ? 13 : 0;
  165. if (count == 0 || d.size() < 4 + (std::size_t) count * 4)
  166. return false;
  167. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  168. const auto decay = std::clamp (param (5) / 10.0, 0.1, 20.0);
  169. const auto dampingKhz = param (6) >= 221 ? 22.0 : std::clamp (param (6) / 10.0, 0.5, 22.0);
  170. const auto separation = std::clamp (param (9) / 64.0, -1.0, 1.0);
  171. const auto dry = curve (11.0, param (10) / 128.0);
  172. const auto wet = curve (11.0, param (12) / 256.0);
  173. // LYNX's Freeverb: comb feedback for the requested decay (~30 ms combs)
  174. const auto feedback = std::pow (10.0, -3.0 * 0.03 / decay);
  175. slot.params[0] = clamp01 ((feedback - 0.7) / 0.28);
  176. slot.params[1] = clamp01 (1.0 - std::log (dampingKhz / 0.5) / std::log (22.0 / 0.5)); // more damping with a lower cutoff
  177. slot.params[2] = clamp01 (1.0 + std::min (0.0, separation)); // negative separation narrows
  178. slot.params[3] = clamp01 (wet / 0.25);
  179. slot.params[4] = clamp01 (dry);
  180. // low cut (< 20 Hz = off), high cut (>= 22.1 kHz = off) and predelay
  181. const auto lowCutHz = param (0);
  182. slot.params[5] = lowCutHz < 20 ? 0.0f : std::max (0.002f, fromLog (lowCutHz, 20.0, 3000.0));
  183. slot.params[6] = param (1) >= 221 ? 1.0f : std::min (0.998f, fromLog (param (1) * 100.0, 500.0, 22000.0));
  184. const auto tempoPredelay = d.size() > 4 + (std::size_t) count * 4 && d[4 + (std::size_t) count * 4] != 0;
  185. const auto predelayMs = tempoPredelay ? param (2) / 48.0 * 60000.0 / std::max (1.0, bpm) : param (2) * 1000.0 / 384.0;
  186. slot.params[7] = clamp01 (predelayMs / 1000.0);
  187. return true;
  188. }
  189. // --- Fruity Delay 2 and Delay 3 (§4) ---------------------------------------------------------------
  190. float delayTimeValue (double beats)
  191. {
  192. // the closest built-in delay division (0.25 … 4 beats, 8 divisions)
  193. constexpr double divisions[] = { 0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 3.0, 4.0 };
  194. int best = 0;
  195. for (int i = 1; i < 8; ++i)
  196. if (std::abs (std::log (divisions[i] / beats)) < std::abs (std::log (divisions[best] / beats)))
  197. best = i;
  198. return ((float) best + 0.5f) / 8.0f;
  199. }
  200. bool delay2 (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  201. {
  202. if (d.size() < 32)
  203. return false;
  204. slot.params[0] = delayTimeValue (std::clamp (i32At (d, 0x10), 1, 768) / 48.0);
  205. slot.params[1] = clamp01 (i32At (d, 0x0C) / 127.0 * 0.9);
  206. slot.params[2] = clamp01 (curve (11.0, i32At (d, 0x04) / 128.0) * 0.5);
  207. slot.params[3] = i32At (d, 0x18) == 2 ? 1.0f : 0.0f;
  208. slot.params[4] = clamp01 (1.0 - i32At (d, 0x1C) / 128.0);
  209. return true;
  210. }
  211. bool delay3 (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  212. {
  213. if (d.size() < 4 + 26 * 4)
  214. return false;
  215. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  216. const auto synced = param (2) != 0;
  217. // synced: ticks of 1/96 beat; unsynced the unit is unknown: use 1/4
  218. slot.params[0] = delayTimeValue (synced ? std::clamp (param (4), 1, 768) / 96.0 : 1.0);
  219. slot.params[1] = clamp01 (param (14) / 6000.0 * 0.9);
  220. slot.params[2] = clamp01 (param (23) / 6000.0 * 0.5);
  221. slot.params[3] = param (1) == 2 ? 1.0f : 0.0f;
  222. slot.params[4] = clamp01 (1.0 - param (16) / 6000.0);
  223. return true;
  224. }
  225. // --- Fruity Compressor (§5) ----------------------------------------------------------------------
  226. bool compressor (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  227. {
  228. if (d.size() < 4 + 6 * 4)
  229. return false;
  230. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  231. slot.params[0] = clamp01 ((std::clamp (param (0) / 10.0, -60.0, 0.0) + 60.0) / 60.0);
  232. slot.params[1] = clamp01 ((std::clamp (param (1) / 10.0, 1.0, 20.0) - 1.0) / 19.0);
  233. slot.params[4] = clamp01 (std::clamp (param (2) / 10.0, 0.0, 24.0) / 24.0);
  234. slot.params[2] = fromLog (std::max (0.1, param (3) / 10.0), 0.1, 100.0);
  235. slot.params[3] = fromLog (std::max (10.0, (double) param (4)), 10.0, 1000.0);
  236. return true;
  237. }
  238. // --- REA_rodada8: Maximus, Soundgoodizer, Fast Dist, Fruity Filter, Flangus, Soft Clipper -------
  239. double timeMs (int v) // T(x) = 2·(501^x − 1) ms
  240. {
  241. return 2.0 * (std::pow (501.0, std::clamp (v, 0, 10000) / 10000.0) - 1.0);
  242. }
  243. float compressorAttackValue (double ms) { return fromLog (std::max (0.1, ms), 0.1, 100.0); }
  244. float compressorReleaseValue (double ms) { return fromLog (std::max (10.0, ms), 10.0, 1000.0); }
  245. float limiterReleaseValue (double ms) { return fromLog (std::max (10.0, ms), 10.0, 1000.0); }
  246. float utilityGainValue (double gain);
  247. bool maximus (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  248. {
  249. if (d.size() < 4 + 59 * 4)
  250. return false;
  251. const auto header = (std::uint32_t) i32At (d, 0);
  252. const auto version = header > 0xFFFFu ? (int) (header >> 16) : (int) header;
  253. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  254. // the master section (band 3) represents the effect; the bands are multiband (LYNX has none)
  255. constexpr int master = 7 + 13 * 3;
  256. const auto mode = param (master);
  257. const auto preDb = toDb (curve (7.0, param (master + 1) / 1000.0));
  258. const auto postDb = toDb (curve (7.0, param (master + 2) / 1000.0));
  259. if (mode == 3) // master off: pass-through (a transparent utility keeps the slot position)
  260. {
  261. slot = makeEffectSlot (EffectKind::utility);
  262. return true;
  263. }
  264. if (mode != 0) // no compression: gains only
  265. {
  266. slot = makeEffectSlot (EffectKind::utility);
  267. slot.params[0] = utilityGainValue (std::pow (10.0, (preDb + postDb) / 20.0));
  268. return true;
  269. }
  270. // master compression curve: 7th envelope (low, 2nd, mid, 2nd, high, 2nd, master, 2nd)
  271. std::vector<std::pair<double, double>> points;
  272. std::size_t pos = version >= 15 ? 0x21F : version >= 14 ? 0x21B : 0x1FE;
  273. for (int env = 0; env < 8 && pos + 12 <= d.size(); ++env)
  274. {
  275. const auto count = (std::size_t) std::max (0, i32At (d, pos + 8));
  276. if (count > 512 || pos + 12 + 24 * count + 20 > d.size())
  277. break;
  278. if (env == 6)
  279. {
  280. double x = 0.0;
  281. for (std::size_t k = 0; k < count; ++k)
  282. {
  283. double dx = 0.0, y = 0.0;
  284. std::memcpy (&dx, d.data() + pos + 12 + 24 * k, 8);
  285. std::memcpy (&y, d.data() + pos + 12 + 24 * k + 8, 8);
  286. x += dx;
  287. if (std::isfinite (x) && std::isfinite (y))
  288. points.push_back ({ std::clamp (x, 0.0, 1.0), std::clamp (y, 0.0, 1.0) });
  289. }
  290. }
  291. pos += 12 + 24 * count + 20;
  292. }
  293. if (points.size() < 2)
  294. points = { { 0.0, 0.0 }, { 0.5, 0.5 }, { 1.0, 0.5 } }; // the default curve: limiter at −0.22 dB
  295. auto curveAt = [&] (double c) {
  296. if (c <= points.front().first)
  297. return points.front().second;
  298. for (std::size_t k = 1; k < points.size(); ++k)
  299. if (c <= points[k].first)
  300. {
  301. const auto span = points[k].first - points[k - 1].first;
  302. const auto t = span > 0.0 ? (c - points[k - 1].first) / span : 1.0;
  303. return points[k - 1].second + t * (points[k].second - points[k - 1].second);
  304. }
  305. return points.back().second;
  306. };
  307. auto levelDb = [] (double c, double scale) { return toDb ((std::pow (3.0, 2.0 * c) - 1.0) / 2.0 * scale); };
  308. // ceiling, knee and ratio of the curve (input level c: 0.5 = 0 dBFS, 1 = +12 dB)
  309. double ceilingDb = -120.0, kneeDb = 0.0, kneeOut = 0.0;
  310. bool knee = false;
  311. for (int k = 1; k <= 400; ++k)
  312. {
  313. const auto c = k / 400.0;
  314. const auto in = levelDb (c, 1.0), out = levelDb (curveAt (c), 0.975);
  315. ceilingDb = std::max (ceilingDb, out);
  316. if (! knee && out < in - 0.5)
  317. {
  318. knee = true;
  319. kneeDb = in;
  320. kneeOut = out;
  321. }
  322. }
  323. const auto topIn = levelDb (1.0, 1.0), topOut = levelDb (curveAt (1.0), 0.975);
  324. const auto ratio = knee && topOut - kneeOut > 0.05 ? (topIn - kneeDb) / (topOut - kneeOut) : 100.0;
  325. if (knee && kneeDb < -0.5 && ratio < 8.0) // real compression (knee below 0 dB)
  326. {
  327. slot = makeEffectSlot (EffectKind::compressor);
  328. slot.params[0] = clamp01 ((kneeDb + 60.0) / 60.0);
  329. slot.params[1] = clamp01 ((std::clamp (ratio, 1.0, 20.0) - 1.0) / 19.0);
  330. slot.params[2] = compressorAttackValue (timeMs (param (master + 6)));
  331. slot.params[3] = compressorReleaseValue (2.0 * timeMs (param (master + 9)));
  332. slot.params[4] = clamp01 (std::max (0.0, preDb + postDb) / 24.0);
  333. return true;
  334. }
  335. slot = makeEffectSlot (EffectKind::limiter);
  336. slot.params[0] = clamp01 ((std::min (ceilingDb + postDb, 0.0) + 12.0) / 12.0);
  337. slot.params[1] = clamp01 (std::max (0.0, preDb) / 24.0);
  338. slot.params[2] = limiterReleaseValue (2.0 * timeMs (param (master + 9)));
  339. return true;
  340. }
  341. bool soundgoodizer (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  342. {
  343. if (d.size() < 12)
  344. return false;
  345. // preset A–D (full Maximus copies); compressor approximation measured at amount 0.6
  346. struct Preset { double threshold, ratio, makeup; };
  347. constexpr Preset presets[] = { { -6.0, 2.5, 5.3 }, { -10.5, 2.2, 6.6 }, { -10.8, 2.5, 7.9 }, { -14.0, 1.9, 8.5 } };
  348. const auto& p = presets[std::clamp (i32At (d, 4), 0, 3)];
  349. const auto amount = std::clamp (i32At (d, 8), 0, 1000) / 1000.0;
  350. const auto scale = amount / 0.6;
  351. slot = makeEffectSlot (EffectKind::compressor);
  352. slot.params[0] = clamp01 ((p.threshold + 60.0) / 60.0);
  353. slot.params[1] = clamp01 ((1.0 + (p.ratio - 1.0) * scale - 1.0) / 19.0);
  354. slot.params[2] = compressorAttackValue (2.0);
  355. slot.params[3] = compressorReleaseValue (85.0);
  356. slot.params[4] = clamp01 (p.makeup * scale / 24.0);
  357. return true;
  358. }
  359. bool fastDist (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  360. {
  361. if (d.size() < 20)
  362. return false;
  363. const auto pre = curve (11.0, std::clamp (i32At (d, 0), 64, 192) / 128.0);
  364. const auto t = std::clamp (i32At (d, 4), 1, 10);
  365. const auto typeB = i32At (d, 8) != 0;
  366. const auto mix = curve (11.0, std::clamp (i32At (d, 12), 0, 128) / 128.0);
  367. const auto post = curve (11.0, std::clamp (i32At (d, 16), 0, 128) / 128.0);
  368. // equivalent tanh drive for FL's curve (fit from REA_rodada8 §3.2)
  369. constexpr double driveA[] = { 0, 0, 0, 0, 2.2, 4.5, 6.8, 9.2, 12.8, 17.8, 22.8 };
  370. const auto curveDb = typeB ? std::max (0.0, 0.8 + (t - 6) * 0.85) * (t >= 6 ? 1.0 : 0.0) : driveA[t];
  371. const auto wet = mix * post;
  372. const auto dry = (post - wet) * pre * post; // post is applied squared to the dry signal (as in FL)
  373. const auto level = typeB ? 1.0 : std::pow (10.0, -0.9 * (t - 1) / 20.0);
  374. const auto total = wet * level + dry;
  375. slot.params[0] = clamp01 ((toDb (pre) + curveDb) / 36.0);
  376. slot.params[1] = 1.0f; // Fast Dist does not filter
  377. slot.params[2] = total > 1.0e-6 ? clamp01 (wet * level / total) : 1.0f;
  378. slot.params[3] = clamp01 (0.5 + toDb (std::max (total, 1.0e-6)) / 24.0);
  379. return true;
  380. }
  381. bool fruityFilter (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  382. {
  383. if (d.size() < 4 + 6 * 4)
  384. return false;
  385. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  386. const auto lp = curve (11.0, std::clamp (param (2), 0, 1024) / 1024.0);
  387. const auto bp = curve (11.0, std::clamp (param (3), 0, 1024) / 1024.0);
  388. const auto hp = curve (11.0, std::clamp (param (4), 0, 1024) / 1024.0);
  389. // built-in filter type: 0 low-pass, 1 high-pass, 2 band-pass (floor(3v))
  390. auto type = 0.0f;
  391. if (hp > lp && hp >= bp && ! (lp >= 0.5 && hp >= 0.5))
  392. type = 0.4f;
  393. else if (bp > lp && bp > hp)
  394. type = 0.7f;
  395. // Chamberlin SVF without rate correction: "open" still cuts at ~7.35 kHz (at 44.1 kHz)
  396. const auto f = std::min (1.0, (std::pow (26.0, std::clamp (param (0), 0, 1024) / 1024.0) - 1.0) * 0.04);
  397. const auto hz = std::max (20.0, 44100.0 / 3.141592653589793 * std::asin (f / 2.0));
  398. auto q = 1.0 / (1.0 - 0.8 * std::clamp (param (1), 0, 1024) / 1024.0);
  399. if (param (5) != 0)
  400. q *= 1.3; // x2: second stage in series
  401. slot.params[0] = type;
  402. slot.params[1] = fromLog (hz, 20.0, 20000.0);
  403. slot.params[2] = fromLog (q, 0.5, 12.0);
  404. slot.params[3] = 1.0f;
  405. return true;
  406. }
  407. bool flangus (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  408. {
  409. if (d.size() < 32)
  410. return false;
  411. auto param = [&] (int i) { return i32At (d, 4 * (std::size_t) i); };
  412. const auto depthMs = 24.8 * curve (11.0, std::clamp (param (1), 0, 1024) / 1024.0);
  413. const auto voiceDepth = depthMs * 44.1 + 10.0; // samples at 44.1 kHz
  414. const auto period = 75000.0 * (std::pow (101.0, 1.0 - std::clamp (param (2), 0, 1024) / 1024.0) - 1.0) + 50000.0;
  415. const auto hz = 250.0 * 44100.0 / (period * voiceDepth);
  416. const auto dryGain = curve (11.0, std::abs (std::clamp (param (6), -128, 128)) / 128.0);
  417. const auto wetGain = curve (11.0, std::abs (std::clamp (param (7), -128, 128)) / 128.0);
  418. const auto r = dryGain > 1.0e-6 ? wetGain / dryGain : 1000.0;
  419. slot.params[0] = fromLog (std::max (0.05, hz), 0.05, 5.0);
  420. slot.params[1] = clamp01 (2.0 * depthMs / 8.0);
  421. slot.params[2] = clamp01 (2.0 * r / (1.0 + r));
  422. slot.params[3] = 0.0f; // Flangus has no feedback
  423. return true;
  424. }
  425. bool softClipper (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  426. {
  427. if (d.size() < 8)
  428. return false;
  429. const auto t = std::clamp (i32At (d, 0), 1, 127);
  430. const auto postDb = toDb (curve (11.0, std::clamp (i32At (d, 4), 0, 160) / 128.0));
  431. const auto drop = t >= 100 ? 1.5 : t >= 64 ? 2.0 - (t - 64) / 36.0 * 0.5 : 3.8 - (t - 1) / 63.0 * 1.8; // level drop of the curve
  432. slot.params[0] = 0.0f;
  433. slot.params[1] = 1.0f;
  434. slot.params[2] = 1.0f;
  435. slot.params[3] = clamp01 (0.5 + (postDb - drop) / 24.0);
  436. return true;
  437. }
  438. // --- Balance, PanOMatic, Stereo Shaper (§6) -> Utility ------------------------------------------
  439. float utilityGainValue (double gain) { return clamp01 (toDb (gain) / 48.0 + 0.5); }
  440. float utilityMatrixValue (double m) { return clamp01 ((m + 2.0) / 4.0); }
  441. bool balance (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  442. {
  443. if (d.size() < 8)
  444. return false;
  445. slot.params[1] = clamp01 (std::clamp (i32At (d, 0), -128, 128) / 256.0 + 0.5);
  446. slot.params[0] = utilityGainValue (curve (11.0, i32At (d, 4) / 256.0)); // 256 = 0 dB
  447. return true;
  448. }
  449. bool panOMatic (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  450. {
  451. if (d.size() < 8)
  452. return false;
  453. slot.params[1] = clamp01 (std::clamp (i32At (d, 0), 0, 256) / 256.0); // 128 = center
  454. slot.params[0] = utilityGainValue (curve (11.0, i32At (d, 4) / 256.0));
  455. return true; // PanOMatic's LFO (enabled in few demos) is left out
  456. }
  457. bool stereoShaper (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  458. {
  459. if (d.size() < 4 + 4 * 4)
  460. return false;
  461. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i) / 12800.0; }; // 12800 = 1.0
  462. slot.params[3] = utilityMatrixValue (param (0)); // R -> L
  463. slot.params[2] = utilityMatrixValue (param (1)); // L
  464. slot.params[5] = utilityMatrixValue (param (2)); // R
  465. slot.params[4] = utilityMatrixValue (param (3)); // L -> R
  466. return true;
  467. }
  468. // --- REA_rodada14: Gross Beat (volume), Frequency Splitter, Effector ----------------------------
  469. // Gross Beat: the selected volume slot (4-beat envelope) becomes the gate's 32 steps; the
  470. // "time travel" has no equivalent. Without a readable state, the gate stays transparent.
  471. bool grossBeat (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  472. {
  473. for (int i = 0; i < 32; ++i)
  474. slot.params[(std::size_t) i] = 1.0f;
  475. if (d.size() < 64)
  476. return false;
  477. auto version = (std::uint32_t) i32At (d, 0);
  478. if (version > 0xffff)
  479. version >>= 16;
  480. if (version < 3 || version > 9)
  481. return false;
  482. const auto paramCount = 9;
  483. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  484. std::size_t pos = 4 + 4 * paramCount + (version >= 5 ? 9 : 8);
  485. const auto volumeSlot = 36 + std::clamp (param (1), 0, 35);
  486. // the 72 slots (36 time + 36 volume): name, 15 B of sync, envelope
  487. std::vector<std::pair<double, double>> chosen; // (accumulated x in beats, y)
  488. std::vector<int> curves;
  489. for (int s = 0; s < 72; ++s)
  490. {
  491. if (pos >= d.size())
  492. return false;
  493. pos += 1 + d[pos] + 15;
  494. if (pos + 12 > d.size())
  495. return false;
  496. const auto n = i32At (d, pos + 8);
  497. if (n < 0 || n > 5000 || pos + 12 + (std::size_t) n * 24 + 20 > d.size())
  498. return false;
  499. pos += 12;
  500. double x = 0.0;
  501. for (int k = 0; k < n; ++k, pos += 24)
  502. if (s == volumeSlot)
  503. {
  504. double dx = 0.0, y = 0.0;
  505. std::memcpy (&dx, d.data() + pos, 8);
  506. std::memcpy (&y, d.data() + pos + 8, 8);
  507. x += std::isfinite (dx) ? std::max (0.0, dx) : 0.0;
  508. chosen.emplace_back (x, std::isfinite (y) ? std::clamp (y, 0.0, 1.0) : 1.0);
  509. curves.push_back (d[pos + 20]);
  510. }
  511. pos += 20;
  512. }
  513. if (chosen.empty())
  514. return true;
  515. // value at the middle of each step; "hold" (2) holds the previous point, the rest is linear
  516. for (int i = 0; i < 32; ++i)
  517. {
  518. const auto t = (i + 0.5) / 32.0 * 4.0;
  519. auto value = chosen.back().second;
  520. if (t <= chosen.front().first)
  521. value = chosen.front().second;
  522. else
  523. for (std::size_t k = 1; k < chosen.size(); ++k)
  524. if (t < chosen[k].first)
  525. {
  526. const auto [x0, y0] = chosen[k - 1];
  527. const auto [x1, y1] = chosen[k];
  528. value = curves[k] == 2 || x1 <= x0 ? y0 : y0 + (y1 - y0) * (t - x0) / (x1 - x0);
  529. break;
  530. }
  531. slot.params[(std::size_t) i] = (float) value;
  532. }
  533. slot.params[32] = clamp01 (param (3) / 256.0);
  534. slot.params[33] = clamp01 (param (4) / 10000.0);
  535. slot.params[34] = clamp01 (param (5) / 10000.0);
  536. return true;
  537. }
  538. // Frequency Splitter: the main output with per-band level becomes shelves in the parametric EQ
  539. bool frequencySplitter (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  540. {
  541. if (d.size() < 15 * 4)
  542. return false;
  543. auto word = [&] (int i) { return i32At (d, 4 * (std::size_t) i); };
  544. auto hz = [] (int v) { return 30.0 * std::pow (16000.0 / 30.0, std::clamp (v, 0, 65536) / 65536.0); };
  545. auto db = [] (int v) { return std::clamp (v / 100.0, -18.0, 18.0); };
  546. const auto threeBands = word (6) != 0;
  547. const auto low = db (word (12)), mid = db (word (13)), high = db (word (14));
  548. for (int b = 0; b < peq::bands; ++b)
  549. slot.params[(std::size_t) peq::param (b, 0)] = peq::typeValue (peq::BandType::off);
  550. auto shelf = [&] (int band, peq::BandType type, double f, double gain) {
  551. slot.params[(std::size_t) peq::param (band, 0)] = peq::typeValue (type);
  552. slot.params[(std::size_t) peq::param (band, 1)] = peq::hzValue (f);
  553. slot.params[(std::size_t) peq::param (band, 2)] = peq::gainValue (gain);
  554. slot.params[(std::size_t) peq::param (band, 3)] = peq::qValue (1.0);
  555. };
  556. if (threeBands)
  557. {
  558. shelf (0, peq::BandType::lowShelf, hz (word (1)), low - mid);
  559. shelf (6, peq::BandType::highShelf, hz (word (2)), high - mid);
  560. slot.params[peq::outputParam] = peq::gainValue (mid);
  561. }
  562. else
  563. {
  564. shelf (0, peq::BandType::lowShelf, hz (word (1)), low - mid);
  565. slot.params[peq::outputParam] = peq::gainValue (mid);
  566. }
  567. return true;
  568. }
  569. // Peak Controller (§7.3): base, amount, tension and decay speed
  570. bool peakController (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  571. {
  572. if (d.size() < 40)
  573. return false;
  574. slot.params[0] = clamp01 (i32At (d, 0) / 65536.0);
  575. slot.params[1] = clamp01 ((std::clamp (i32At (d, 4), -256, 256) / 256.0 + 1.0) / 2.0);
  576. slot.params[2] = clamp01 ((std::clamp (i32At (d, 8), -128, 128) / 128.0 + 1.0) / 2.0);
  577. slot.params[3] = clamp01 ((std::clamp (i32At (d, 12), 5, 128) - 5) / 123.0);
  578. slot.params[4] = clamp01 (i32At (d, 16) / 65536.0); // LFO base
  579. return true;
  580. }
  581. // --- REA_rodada15: Pitch Shifter, Frequency Shifter, Emphasis, Low Lifter, Send, Transporter -----
  582. bool pitchShifter (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  583. {
  584. if (d.size() < 4 + 13 * 4)
  585. return false;
  586. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  587. const auto cents = std::clamp (param (0), -1200, 1200);
  588. const auto semis = (int) std::lround (cents / 100.0);
  589. slot.params[0] = clamp01 (0.5 + semis / 48.0);
  590. slot.params[1] = clamp01 (0.5 + (cents - semis * 100) / 200.0);
  591. slot.params[2] = clamp01 (std::clamp (param (3), 0, 2400) / 2400.0);
  592. const auto granular = d.size() >= 65 && i32At (d, 61) == 1;
  593. const auto grainMs = granular ? 20.0 + 130.0 * (std::clamp (param (4), 2000, 15000) - 2000) / 13000.0 : 60.0;
  594. slot.params[3] = clamp01 ((grainMs - 20.0) / 100.0);
  595. return true;
  596. }
  597. bool frequencyShifter (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  598. {
  599. if (d.size() < 4 + 10 * 4)
  600. return false;
  601. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  602. const auto s = std::clamp (param (2), -70000, 70000) / 70000.0;
  603. const auto k = param (8) == 0 ? 17.6096 : 10.9658; // range 20 kHz / 200 Hz ("Tempo" is treated as 200 Hz)
  604. const auto hz = s == 0.0 ? 0.0 : (s < 0.0 ? -1.0 : 1.0) * 0.1 * std::pow (2.0, k * std::abs (s));
  605. // our scale: 2000·x·|x| Hz
  606. const auto x = std::clamp ((hz < 0.0 ? -1.0 : 1.0) * std::sqrt (std::min (1.0, std::abs (hz) / 2000.0)), -1.0, 1.0);
  607. slot.params[0] = clamp01 (0.5 + x / 2.0);
  608. slot.params[1] = clamp01 (std::clamp (param (0), 0, 10000) / 10000.0);
  609. slot.params[2] = 0.0f;
  610. return true;
  611. }
  612. bool emphasis (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  613. {
  614. if (d.size() < 4 + 13 * 4)
  615. return false;
  616. auto x = [&] (int i) { return std::clamp (i32At (d, 4 + 4 * (std::size_t) i), 0, 10000) / 10000.0; };
  617. const auto ceilingDb = toDb (curve (11.0, x (9)));
  618. slot.params[0] = clamp01 ((std::min (ceilingDb, 0.0) + 12.0) / 12.0);
  619. slot.params[1] = clamp01 (x (6)); // input gain 24x dB = our 0..24 dB scale
  620. slot.enabled = i32At (d, 4) == 0;
  621. return true;
  622. }
  623. bool lowLifter (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  624. {
  625. if (d.size() < 4 + 15 * 4)
  626. return false;
  627. auto x = [&] (int i) { return std::clamp (i32At (d, 4 + 4 * (std::size_t) i), 0, 10000) / 10000.0; };
  628. auto hz = [] (double v) { return 20.0 + 100.0 * v * v; };
  629. const auto mix = x (1);
  630. const auto lowDb = std::clamp ((x (2) - 0.5) * 24.0, -18.0, 12.0);
  631. const auto highDb = std::clamp (100.0 * std::log10 (std::max (1e-3, x (9))), -18.0, 0.0);
  632. // the harmonics generator (Chebyshev up to 3rd order, REA_rodada17 §13) has no equivalent here; the EQ
  633. // reproduces only the low cut and the band gains
  634. const auto boost = 0.0;
  635. for (int b = 0; b < peq::bands; ++b)
  636. slot.params[(std::size_t) peq::param (b, 0)] = peq::typeValue (peq::BandType::off);
  637. if (hz (x (11)) > 21.0)
  638. {
  639. slot.params[(std::size_t) peq::param (0, 0)] = peq::typeValue (peq::BandType::lowCut);
  640. slot.params[(std::size_t) peq::param (0, 1)] = peq::hzValue (hz (x (11)));
  641. slot.params[(std::size_t) peq::param (0, 3)] = peq::qValue (0.707);
  642. }
  643. slot.params[(std::size_t) peq::param (1, 0)] = peq::typeValue (peq::BandType::lowShelf);
  644. slot.params[(std::size_t) peq::param (1, 1)] = peq::hzValue (hz (x (10)));
  645. slot.params[(std::size_t) peq::param (1, 2)] = peq::gainValue (std::clamp ((lowDb - highDb) * mix + boost, -18.0, 18.0));
  646. slot.params[(std::size_t) peq::param (1, 3)] = peq::qValue (1.0);
  647. slot.params[peq::outputParam] = peq::gainValue (highDb * mix);
  648. slot.enabled = i32At (d, 4) != 0;
  649. return true;
  650. }
  651. bool fruitySend (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  652. {
  653. if (d.size() < 12)
  654. return false;
  655. slot.params[0] = utilityGainValue (std::clamp (i32At (d, 8), 0, 256) / 256.0); // what continues on the track
  656. slot.params[1] = 0.5f;
  657. return true;
  658. }
  659. // Fruity Squeeze (REA_rodada29 §2): the exact effect; state = u32 version + 12 raw i32 controls
  660. bool squeeze (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  661. {
  662. if (d.size() < 4 + 12 * 4)
  663. return false;
  664. slot = makeEffectSlot (EffectKind::squeeze);
  665. for (int i = 0; i < 12; ++i)
  666. slot.params[(std::size_t) i] = (float) clamp01 (std::clamp (i32At (d, 4 + 4 * (std::size_t) i), 0, (int) squeezeMax[(std::size_t) i])
  667. / (double) squeezeMax[(std::size_t) i]);
  668. return true;
  669. }
  670. bool transporter (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  671. {
  672. if (d.size() < 8)
  673. return false;
  674. slot.params[0] = utilityGainValue (curve (11.0, std::clamp (i32At (d, 4), 0, 16000) / 12800.0)); // dry level
  675. return true;
  676. }
  677. // Effector: LYNX's Effector effect (core/Effector.h) receives FL's 12 raw parameters
  678. std::optional<EffectSlot> effector (const std::vector<std::uint8_t>& d, double)
  679. {
  680. // "SSLF" + size + "10XF" + 64 reserved floats + the 12 parameters
  681. if (d.size() < 12 + 76 * 4 || std::memcmp (d.data(), "SSLF", 4) != 0)
  682. return std::nullopt;
  683. float p[12];
  684. std::memcpy (p, d.data() + 12 + 64 * 4, sizeof (p));
  685. auto slot = makeEffectSlot (EffectKind::effector);
  686. for (int i = 0; i < 12; ++i)
  687. slot.params[(std::size_t) i] = std::isfinite (p[i]) ? std::clamp (p[i], 0.0f, 1.0f) : 0.0f;
  688. slot.enabled = slot.params[1] == 0.0f; // bypass = any value ≠ 0
  689. slot.params[1] = 0.0f;
  690. slot.mix = 1.0f; // each algorithm's mix is done inside the effect (Wet Mix = parameter 2)
  691. return slot;
  692. }
  693. // Fruity Vocoder (REA_rodada20 §4.1.1, REA_rodada70): the exact engine (core/FruityVocoder.h). The slot keeps FL's
  694. // saved state (its per-band levels) and the controls as parameters (Effects.cpp, namespace vocoder).
  695. std::optional<EffectSlot> fruityVocoder (const std::vector<std::uint8_t>& d, std::vector<std::string>* notes)
  696. {
  697. (void) notes;
  698. const auto st = fvocoder::parse (d);
  699. if (! st)
  700. return std::nullopt;
  701. auto slot = makeEffectSlot (EffectKind::vocoder);
  702. int index = 0;
  703. while ((4 << index) < st->bands && index < 5)
  704. ++index;
  705. slot.params[0] = (float) index / 5.0f;
  706. slot.params[1] = (float) (st->order - 1) / 2.0f;
  707. slot.params[2] = (float) st->listen / 2.0f;
  708. slot.params[3] = st->swap ? 1.0f : 0.0f;
  709. for (int c = 0; c < fvocoder::controls; ++c)
  710. {
  711. const auto lo = fvocoder::controlMin (c), hi = fvocoder::controlMax (c);
  712. const auto v = std::clamp (st->c[(std::size_t) c], lo, hi);
  713. slot.params[(std::size_t) (4 + c)] = (float) (v - lo) / (float) (hi - lo);
  714. }
  715. slot.state = d;
  716. return slot;
  717. }
  718. } // namespace
  719. std::vector<EffectSlot> soundgoodizerApproximation (int preset, int amount)
  720. {
  721. // a Soundgoodizer state (version, preset, amount) through the same conversion as the plugin's own slots, then
  722. // the master limiter of its Maximus presets (a–d.dat: −0.2 dB ceiling, 86 ms release; REA_rodada8 §2)
  723. std::vector<std::uint8_t> state (12, 0);
  724. const std::int32_t words[3] { 1, preset, amount };
  725. std::memcpy (state.data(), words, sizeof (words));
  726. std::vector<EffectSlot> chain (1);
  727. soundgoodizer (state, chain[0]);
  728. auto limiterSlot = makeEffectSlot (EffectKind::limiter);
  729. limiterSlot.params[0] = clamp01 ((-0.2 + 12.0) / 12.0);
  730. limiterSlot.params[1] = 0.0f;
  731. limiterSlot.params[2] = limiterReleaseValue (86.0);
  732. chain.push_back (limiterSlot);
  733. return chain;
  734. }
  735. std::optional<VocodexConversion> vocodexEffect (const std::vector<std::uint8_t>& d, const FlDataReader& data)
  736. {
  737. auto out = vocodexEffect (d);
  738. if (! out || ! data)
  739. return out;
  740. const auto state = vocodex::parseState (d);
  741. vocodex::InstallData files;
  742. const std::string folder = "Plugins\\Fruity\\Effects\\Vocodex\\";
  743. if (state->params[vocodex::sg] > 0)
  744. {
  745. files.sgDat = data (folder + "Data\\SG.dat");
  746. if (hlfx::makeMaximusPreset (files.sgDat) == nullptr)
  747. files.sgDat.clear();
  748. }
  749. if (state->internalCarrier() && ! state->tone.empty())
  750. {
  751. // FUN_008506a0: Synthesizer\Data\Presets\<tone>.FST (the files ship as .fst)
  752. auto fst = data (folder + "Synthesizer\\Data\\Presets\\" + state->tone + ".fst");
  753. if (fst.empty())
  754. fst = data (folder + "Synthesizer\\Data\\Presets\\" + state->tone + ".FST");
  755. if (auto st = hlfx::maximusStateFromFst (fst); st && sytrus::stateUsable (*st))
  756. files.carrierState = std::move (*st);
  757. }
  758. out->slot.state = vocodex::appendInstallData (d, files);
  759. out->notes.clear();
  760. if (state->internalCarrier() && vocodex::makeInternalCarrier (*state, files.carrierState) == nullptr)
  761. out->notes.push_back ("Vocodex: internal carrier synth not available (modulator only)");
  762. if (state->params[vocodex::sg] > 0 && files.sgDat.empty())
  763. out->notes.push_back ("Vocodex: Soundgoodizer stage approximated (SG.dat not found)");
  764. return out;
  765. }
  766. std::optional<VocodexConversion> vocodexEffect (const std::vector<std::uint8_t>& d)
  767. {
  768. // the exact engine keeps FL's raw state (REA_rodada35); ModIn/CarIn index the track's inputs list
  769. const auto state = vocodex::parseState (d);
  770. if (! state)
  771. return std::nullopt;
  772. VocodexConversion out;
  773. out.slot = makeEffectSlot (EffectKind::vocodex);
  774. out.slot.state = d;
  775. out.modIn = state->lrEncoding ? 0 : state->modIn;
  776. out.carIn = state->lrEncoding ? 0 : state->carIn;
  777. if (state->internalCarrier() && vocodex::makeInternalCarrier (*state) == nullptr)
  778. out.notes.push_back ("Vocodex: internal carrier synth not available (modulator only)");
  779. if (state->params[vocodex::sg] > 0)
  780. out.notes.push_back ("Vocodex: Soundgoodizer stage approximated (SG.dat not found)");
  781. return out;
  782. }
  783. std::optional<EffectSlot> fruityLimiterCompressor (const std::vector<std::uint8_t>& state)
  784. {
  785. EffectSlot slot;
  786. if (! limiter (state, slot, true))
  787. return std::nullopt;
  788. return slot;
  789. }
  790. std::optional<EffectSlot> maximusExact (const std::vector<std::uint8_t>& state)
  791. {
  792. maximusfx::Blob blob;
  793. blob.state = state;
  794. if (maximusfx::presetOf (blob) == nullptr)
  795. return std::nullopt;
  796. auto slot = makeEffectSlot (EffectKind::maximus);
  797. slot.state = maximusfx::pack (blob);
  798. return slot;
  799. }
  800. EffectSlot soundgoodizerApproxSlot (int preset, int amount)
  801. {
  802. preset = std::clamp (preset, 0, 3);
  803. amount = std::clamp (amount, 0, 1000);
  804. auto slot = makeEffectSlot (EffectKind::parallel);
  805. slot.branches.push_back ({ soundgoodizerApproximation (preset, amount), 1.0f });
  806. slot.state.resize (12);
  807. const std::int32_t words[3] { 1, preset, amount };
  808. std::memcpy (slot.state.data(), words, sizeof (words));
  809. return slot;
  810. }
  811. bool isSoundgoodizerApprox (const EffectSlot& slot)
  812. {
  813. return slot.kind == EffectKind::parallel && slot.state.size() == 12 && slot.branches.size() == 1
  814. && maximusfx::parseSoundgoodizer (slot.state).has_value();
  815. }
  816. std::optional<EffectSlot> soundgoodizerExact (const std::vector<std::uint8_t>& state, const std::vector<std::uint8_t>& presetFile)
  817. {
  818. // FUN_00836ac0 (Soundgoodizer_x64.dll): the preset file is loaded through the plugin's own Maximus copy, then
  819. // parameter 5 (band mix) is set to Amount (REA_rodada40 §3.1)
  820. const auto sg = maximusfx::parseSoundgoodizer (state);
  821. if (! sg)
  822. return std::nullopt;
  823. maximusfx::Blob blob;
  824. blob.build = hlfx::MaximusBuild::soundgoodizer;
  825. blob.mix = sg->amount;
  826. blob.state = presetFile;
  827. if (maximusfx::presetOf (blob) == nullptr)
  828. return std::nullopt;
  829. auto slot = makeEffectSlot (EffectKind::maximus);
  830. slot.state = maximusfx::pack (blob);
  831. return slot;
  832. }
  833. std::string soundgoodizerPresetPath (const std::vector<std::uint8_t>& state)
  834. {
  835. const auto sg = maximusfx::parseSoundgoodizer (state);
  836. if (! sg)
  837. return {};
  838. return std::string ("Plugins\\Fruity\\Effects\\Soundgoodizer\\Data\\Presets\\") + maximusfx::soundgoodizerPresetFile (sg->preset);
  839. }
  840. std::optional<EffectSlot> effectFromFl (const std::string& pluginName, const std::vector<std::uint8_t>& state, double bpm,
  841. const FlDataReader& data)
  842. {
  843. if (pluginName == "Vocodex" && data)
  844. if (auto v = vocodexEffect (state, data))
  845. return v->slot;
  846. if (pluginName == "Distructor" && data) // the amp/cabinet impulses of the installation travel with the slot (REA_rodada71)
  847. if (const auto s = distructor::parse (state))
  848. {
  849. std::map<int, std::vector<std::uint8_t>> files;
  850. for (const auto i : distructor::impulsesUsed (*s))
  851. if (auto bytes = data (distructor::impulsePath (i)); ! bytes.empty())
  852. files[i] = std::move (bytes);
  853. auto slot = makeEffectSlot (EffectKind::distructor);
  854. slot.state = distructor::packBlob (state, files);
  855. distructor::toParams (*s, slot.params.data());
  856. return slot;
  857. }
  858. if (pluginName == "Fruity Convolver" && data) // impulse response file of the installation (REA_rodada60)
  859. if (auto exact = convolverFromFl (state, data))
  860. return exact;
  861. if (pluginName == "Hardcore" && data) // the exact engine with the installation's cabinet impulses (REA_rodada71)
  862. if (auto exact = hardcore::fromFl (state, data))
  863. return exact;
  864. if (pluginName == "Soundgoodizer" && data)
  865. if (const auto path = soundgoodizerPresetPath (state); ! path.empty())
  866. if (auto exact = soundgoodizerExact (state, data (path)))
  867. return exact;
  868. return effectFromFl (pluginName, state, bpm);
  869. }
  870. std::optional<EffectSlot> fruityDynamicsExact (const std::string& pluginName, const std::vector<std::uint8_t>& state)
  871. {
  872. // FL's own Fruity Limiter / Fruity Compressor engines from the saved controls (REA_rodada48); the approximations
  873. // of REA_rodada7 remain for states that cannot be read
  874. std::string lower = pluginName;
  875. std::transform (lower.begin(), lower.end(), lower.begin(), [] (unsigned char c) { return (char) std::tolower (c); });
  876. if (lower == "fruity limiter")
  877. if (const auto s = fldyn::parseLimiter (state))
  878. {
  879. auto slot = makeEffectSlot (EffectKind::fruityLimiter);
  880. fldyn::limiterToParams (*s, slot.params.data());
  881. return slot;
  882. }
  883. if (lower == "fruity compressor")
  884. if (const auto s = fldyn::parseCompressor (state))
  885. {
  886. auto slot = makeEffectSlot (EffectKind::fruityCompressor);
  887. fldyn::compressorToParams (*s, slot.params.data());
  888. return slot;
  889. }
  890. return std::nullopt;
  891. }
  892. std::optional<EffectSlot> fruityShaperExact (const std::string& pluginName, const std::vector<std::uint8_t>& state)
  893. {
  894. // FL's own Fruity WaveShaper / Fruity Soft Clipper engines from the saved state (REA_rodada51); the distortion
  895. // approximations of REA_rodada8 remain for states that cannot be read
  896. if (pluginName == "Fruity WaveShaper")
  897. if (const auto s = flshape::parseWaveShaper (state))
  898. {
  899. auto slot = makeEffectSlot (EffectKind::fruityWaveShaper);
  900. flshape::waveShaperToParams (*s, slot.params.data());
  901. slot.state = state; // the curve
  902. return slot;
  903. }
  904. if (pluginName == "Fruity Soft Clipper")
  905. if (const auto s = flshape::parseSoftClipper (state))
  906. {
  907. auto slot = makeEffectSlot (EffectKind::fruitySoftClipper);
  908. flshape::softClipperToParams (*s, slot.params.data());
  909. return slot;
  910. }
  911. return std::nullopt;
  912. }
  913. std::optional<EffectSlot> fruityFilterExact (const std::string& pluginName, const std::vector<std::uint8_t>& state)
  914. {
  915. // FL's own Fruity Fast LP / Free Filter / Filter engines from the saved state (REA_rodada63); the generic filter
  916. // approximations remain for states that cannot be read
  917. if (pluginName == "Fruity Fast LP")
  918. if (const auto s = flfilter::parseFastLP (state))
  919. {
  920. auto slot = makeEffectSlot (EffectKind::fruityFastLP);
  921. flfilter::fastLPToParams (*s, slot.params.data());
  922. return slot;
  923. }
  924. if (pluginName == "Fruity Free Filter")
  925. if (const auto s = flfilter::parseFreeFilter (state))
  926. {
  927. auto slot = makeEffectSlot (EffectKind::fruityFreeFilter);
  928. flfilter::freeFilterToParams (*s, slot.params.data());
  929. return slot;
  930. }
  931. if (pluginName == "Fruity Filter")
  932. if (const auto s = flfilter::parseFilter (state))
  933. {
  934. auto slot = makeEffectSlot (EffectKind::fruityFilter);
  935. flfilter::filterToParams (*s, slot.params.data());
  936. return slot;
  937. }
  938. return std::nullopt;
  939. }
  940. std::optional<EffectSlot> fruityBasicExact (const std::string& pluginName, const std::vector<std::uint8_t>& state)
  941. {
  942. // FL's own Fruity PanOMatic / Balance / Blood Overdrive engines from the saved state (REA_rodada58); the utility
  943. // and distortion approximations remain for states that cannot be read
  944. if (pluginName == "Fruity PanOMatic")
  945. if (const auto s = flbasic::parsePanOMatic (state))
  946. {
  947. auto slot = makeEffectSlot (EffectKind::fruityPanOMatic);
  948. flbasic::panOMaticToParams (*s, slot.params.data());
  949. return slot;
  950. }
  951. if (pluginName == "Fruity Balance")
  952. if (const auto s = flbasic::parseBalance (state))
  953. {
  954. auto slot = makeEffectSlot (EffectKind::fruityBalance);
  955. flbasic::balanceToParams (*s, slot.params.data());
  956. return slot;
  957. }
  958. if (pluginName == "Fruity Send") // the exact send to another insert (REA_rodada105_send)
  959. if (const auto s = flbasic::parseSend (state))
  960. {
  961. auto slot = makeEffectSlot (EffectKind::fruitySend);
  962. flbasic::sendToParams (*s, slot.params.data());
  963. return slot;
  964. }
  965. if (pluginName == "Fruity Blood Overdrive")
  966. if (const auto s = flbasic::parseBloodOverdrive (state))
  967. {
  968. auto slot = makeEffectSlot (EffectKind::fruityBloodOverdrive);
  969. flbasic::bloodOverdriveToParams (*s, slot.params.data());
  970. return slot;
  971. }
  972. return std::nullopt;
  973. }
  974. std::optional<EffectSlot> fruityDelayExact (const std::string& pluginName, const std::vector<std::uint8_t>& state)
  975. {
  976. // FL's own Fruity Delay 2 / Delay 3 engines from the saved controls (REA_rodada52); the approximations of
  977. // REA_rodada7 remain for states that cannot be read (case-insensitive: FL 3 presets say "Fruity delay 2")
  978. std::string lower = pluginName;
  979. std::transform (lower.begin(), lower.end(), lower.begin(), [] (unsigned char c) { return (char) std::tolower (c); });
  980. if (lower == "fruity delay 2")
  981. if (const auto s = fldelay::parseDelay2 (state))
  982. {
  983. auto slot = makeEffectSlot (EffectKind::fruityDelay2);
  984. fldelay::delay2ToParams (*s, slot.params.data());
  985. return slot;
  986. }
  987. if (lower == "fruity delay 3")
  988. if (const auto s = fldelay::parseDelay3 (state))
  989. {
  990. auto slot = makeEffectSlot (EffectKind::fruityDelay3);
  991. fldelay::delay3ToParams (*s, slot.params.data());
  992. return slot;
  993. }
  994. return std::nullopt;
  995. }
  996. std::optional<EffectSlot> stereoFxExact (const std::string& pluginName, const std::vector<std::uint8_t>& state)
  997. {
  998. // FL's own engines from the saved state (REA_rodada54); the earlier approximations remain for unreadable states
  999. if (pluginName == "Fruity Stereo Shaper")
  1000. if (const auto s = stereofx::parseStereoShaper (state))
  1001. {
  1002. auto slot = makeEffectSlot (EffectKind::stereoShaper);
  1003. stereofx::stereoShaperToParams (*s, slot.params.data());
  1004. return slot;
  1005. }
  1006. if (pluginName == "Fruity Stereo Enhancer")
  1007. if (const auto s = stereofx::parseStereoEnhancer (state))
  1008. {
  1009. auto slot = makeEffectSlot (EffectKind::stereoEnhancer);
  1010. stereofx::stereoEnhancerToParams (*s, slot.params.data());
  1011. return slot;
  1012. }
  1013. if (pluginName == "Transient Processor")
  1014. if (const auto s = stereofx::parseTransientProcessor (state))
  1015. {
  1016. auto slot = makeEffectSlot (EffectKind::transientProcessor);
  1017. stereofx::transientProcessorToParams (*s, slot.params.data());
  1018. return slot;
  1019. }
  1020. if (pluginName == "Spreader")
  1021. if (const auto s = stereofx::parseSpreader (state))
  1022. {
  1023. auto slot = makeEffectSlot (EffectKind::spreader);
  1024. stereofx::spreaderToParams (*s, slot.params.data());
  1025. return slot;
  1026. }
  1027. return std::nullopt;
  1028. }
  1029. std::optional<EffectSlot> fruityDelayBankExact (const std::vector<std::uint8_t>& state)
  1030. {
  1031. // FL's own Delay Bank engine from the saved state (REA_rodada56); the generic delay remains for unreadable states
  1032. const auto s = fldbank::parse (state);
  1033. if (! s)
  1034. return std::nullopt;
  1035. auto slot = makeEffectSlot (EffectKind::fruityDelayBank);
  1036. slot.state = fldbank::serialise (*s);
  1037. for (int i = 0; i < fldbank::globalControls; ++i)
  1038. slot.params[(std::size_t) i] = (float) std::clamp (s->p[(std::size_t) i], 0, 128) / 128.0f;
  1039. return slot;
  1040. }
  1041. std::optional<EffectSlot> fruityMultibandExact (const std::vector<std::uint8_t>& state)
  1042. {
  1043. // FL's own Multiband Compressor engine (REA_rodada56); the single-band approximation remains for unreadable states.
  1044. // The slot keeps FL's state too, so that the exact integer controls survive the project file's rounding.
  1045. const auto s = flmb::parse (state);
  1046. if (! s)
  1047. return std::nullopt;
  1048. auto slot = makeEffectSlot (EffectKind::fruityMultiband);
  1049. flmb::toParams (*s, slot.params.data());
  1050. slot.state = state;
  1051. return slot;
  1052. }
  1053. std::optional<EffectSlot> effectFromFl (const std::string& pluginName, const std::vector<std::uint8_t>& state, double bpm)
  1054. {
  1055. // Newtone / Newtime: FL's state kept as it is; the source sample is resolved by the project import (REA_rodada73)
  1056. // Formula Controller (REA_rodada133 §3): the knobs as parameters, the formula in the slot's state (FL's own bytes)
  1057. if (pluginName == "Fruity Formula Controller")
  1058. if (const auto parsed = formula::parseState (state))
  1059. {
  1060. auto slot = makeEffectSlot (EffectKind::formulaController);
  1061. slot.params[0] = parsed->a;
  1062. slot.params[1] = parsed->b;
  1063. slot.params[2] = parsed->c;
  1064. slot.state = state;
  1065. return slot;
  1066. }
  1067. // X-Y / X-Y-Z Controller, Control Surface (REA_rodada164a): live controllers with FL's settings
  1068. if (auto xyc = xy::fromFl (pluginName, state))
  1069. return xyc;
  1070. if (pluginName == "Control Surface")
  1071. if (auto controls = surface::parseFlControlSurface (state))
  1072. {
  1073. auto slot = makeEffectSlot (EffectKind::controlSurface);
  1074. slot.patcher.surface = std::move (*controls);
  1075. surface::syncParams (slot);
  1076. slot.state = state;
  1077. return slot;
  1078. }
  1079. if (pluginName == "Newtone" || pluginName == "Newtime")
  1080. {
  1081. auto slot = makeEffectSlot (pluginName == "Newtone" ? EffectKind::newtone : EffectKind::newtime);
  1082. if (const auto parsed = newtonefx::parse (state))
  1083. {
  1084. newtonefx::toParams (*parsed, slot.params);
  1085. slot.state = state;
  1086. }
  1087. return slot;
  1088. }
  1089. if (pluginName == "Emphasizer") // REA_rodada154: the exact engine
  1090. if (const auto s = emphasizer::parse (state))
  1091. {
  1092. auto slot = makeEffectSlot (EffectKind::emphasizer);
  1093. emphasizer::toParams (*s, slot.params.data());
  1094. return slot;
  1095. }
  1096. // round 165g (REA_rodada165g): Fruity Mute 2 (exact), Big Clock, Tuner, HTML NoteBook
  1097. if (pluginName == "Fruity Mute 2")
  1098. {
  1099. auto slot = makeEffectSlot (EffectKind::fruityMute2);
  1100. flextra::mute2::fromFl (state, slot);
  1101. return slot;
  1102. }
  1103. if (pluginName == "Fruity Big Clock")
  1104. return makeEffectSlot (EffectKind::bigClock);
  1105. if (pluginName == "Tuner")
  1106. {
  1107. auto slot = makeEffectSlot (EffectKind::tuner);
  1108. flextra::tuner::fromFl (state, slot);
  1109. return slot;
  1110. }
  1111. if (pluginName == "Fruity Scratcher") // REA_rodada165g-flscratch165: FL's engine; the sample path is resolved by the import
  1112. {
  1113. auto slot = makeEffectSlot (EffectKind::scratcher);
  1114. if (const auto st = scratcher::parse (state))
  1115. {
  1116. for (int i = 0; i < scratcher::count; ++i)
  1117. slot.params[(std::size_t) i] = st->values[(std::size_t) i];
  1118. slot.params[scratcher::timecodeType] = scratcher::timecodeValue (st->timecode);
  1119. slot.params[scratcher::riaaPreamp] = st->riaa ? 1.0f : 0.0f;
  1120. }
  1121. return slot;
  1122. }
  1123. if (pluginName == "Transmitter") // REA_rodada165g-fltrans165: the 42 controls (raw) of FL's state 0xD5
  1124. {
  1125. auto slot = makeEffectSlot (EffectKind::transmitter);
  1126. if (const auto st = transmitter::parse (state))
  1127. for (int i = 0; i < transmitter::count; ++i)
  1128. slot.params[(std::size_t) i] = transmitter::paramFromRaw (i, st->raw[(std::size_t) i]);
  1129. return slot;
  1130. }
  1131. if (pluginName == "Fruity HTML NoteBook")
  1132. {
  1133. auto slot = makeEffectSlot (EffectKind::htmlNotebook);
  1134. slot.state = flextra::htmlnotebook::stateFor (flextra::htmlnotebook::address (state));
  1135. return slot;
  1136. }
  1137. if (pluginName == "Wave Candy") // REA_rodada164c: LYNX's analyser in its role, in the display mode FL's state shows
  1138. {
  1139. auto slot = makeEffectSlot (EffectKind::waveCandy);
  1140. visualfx::wavecandy::fromFl (state, slot);
  1141. return slot;
  1142. }
  1143. if (pluginName == "ZGameEditor Visualizer") // REA_rodada164c: its layers mapped to LYNX Visualizer layers
  1144. {
  1145. auto slot = makeEffectSlot (EffectKind::visualizer);
  1146. if (const auto scene = visualfx::scene::fromZge (state))
  1147. slot.state = visualfx::scene::toState (*scene);
  1148. return slot;
  1149. }
  1150. {
  1151. // REA_rodada165k: the legacy Fruity Phase Inverter, Center, Bass Boost, 7 Band EQ and Delay (v1) on FL's own engines (the
  1152. // generic approximations remain for states that cannot be read); FL 3 presets spell some names in lower case
  1153. std::string lname = pluginName;
  1154. std::transform (lname.begin(), lname.end(), lname.begin(), [] (unsigned char c) { return (char) std::tolower (c); });
  1155. auto exact = [] (EffectKind kind, const auto& s) {
  1156. auto slot = makeEffectSlot (kind);
  1157. fllegacy::toParams (s, slot.params.data());
  1158. return slot;
  1159. };
  1160. if (lname == "fruity phase inverter")
  1161. if (const auto s = fllegacy::parsePhaseInverter (state))
  1162. return exact (EffectKind::flPhaseInverter, *s);
  1163. if (lname == "fruity center")
  1164. if (const auto s = fllegacy::parseCenter (state))
  1165. return exact (EffectKind::flCenter, *s);
  1166. if (lname == "fruity bass boost")
  1167. if (const auto s = fllegacy::parseBassBoost (state))
  1168. return exact (EffectKind::flBassBoost, *s);
  1169. if (lname == "fruity 7 band eq")
  1170. if (const auto s = fllegacy::parseEq7 (state))
  1171. return exact (EffectKind::fl7BandEq, *s);
  1172. if (lname == "fruity delay")
  1173. if (const auto s = fllegacy::parseDelay1 (state))
  1174. return exact (EffectKind::flDelay1, *s);
  1175. }
  1176. if (pluginName == "Low Lifter") // REA_rodada154 §5: FL's own engine
  1177. if (const auto s = lowlifter::parse (state))
  1178. {
  1179. auto slot = makeEffectSlot (EffectKind::flLowLifter);
  1180. lowlifter::toParams (*s, slot.params.data());
  1181. return slot;
  1182. }
  1183. if (pluginName == "Frequency Shifter") // REA_rodada154 §4: FL's own engine
  1184. if (const auto s = fsfl::parse (state))
  1185. {
  1186. auto slot = makeEffectSlot (EffectKind::flFrequencyShifter);
  1187. fsfl::toParams (*s, slot.params.data());
  1188. return slot;
  1189. }
  1190. if (auto exact = fruityDelayExact (pluginName, state))
  1191. return exact;
  1192. if (auto exact = fruityDynamicsExact (pluginName, state))
  1193. return exact;
  1194. if (auto exact = fruityBasicExact (pluginName, state))
  1195. return exact;
  1196. if (auto exact = fruityFilterExact (pluginName, state))
  1197. return exact;
  1198. if (auto exact = fruityShaperExact (pluginName, state))
  1199. return exact;
  1200. if (auto exact = stereoFxExact (pluginName, state))
  1201. return exact;
  1202. if (auto exact = chorusFromFl (pluginName, state)) // REA_rodada59; the generic chorus remains for unreadable states
  1203. return exact;
  1204. if (pluginName == "Fruity Delay Bank")
  1205. if (auto exact = fruityDelayBankExact (state))
  1206. return exact;
  1207. if (pluginName == "Fruity Multiband Compressor")
  1208. if (auto exact = fruityMultibandExact (state))
  1209. return exact;
  1210. // FL's own Fast Dist / Flangus engines from the saved controls (REA_rodada55); the approximations of REA_rodada8
  1211. // remain for states that cannot be read
  1212. if (pluginName == "Fruity Fast Dist")
  1213. if (const auto s = fastdistfx::parse (state))
  1214. {
  1215. auto slot = makeEffectSlot (EffectKind::fruityFastDist);
  1216. fastdistfx::toParams (*s, slot.params.data());
  1217. return slot;
  1218. }
  1219. if (pluginName == "Fruity Flangus")
  1220. if (const auto s = flangusfx::parse (state))
  1221. {
  1222. auto slot = makeEffectSlot (EffectKind::fruityFlangus);
  1223. flangusfx::toParams (*s, slot.params.data());
  1224. return slot;
  1225. }
  1226. if (pluginName == "Fruity Flanger")
  1227. if (const auto s = flangerfx::parse (state))
  1228. {
  1229. auto slot = makeEffectSlot (EffectKind::fruityFlanger);
  1230. flangerfx::toParams (*s, slot.params.data());
  1231. return slot;
  1232. }
  1233. if (pluginName == "Fruity Phaser") // the exact engine (REA_rodada105_phaser); LYNX's generic chorus otherwise
  1234. if (const auto s = phaserfx::parse (state))
  1235. {
  1236. auto slot = makeEffectSlot (EffectKind::fruityPhaser);
  1237. phaserfx::toParams (*s, slot.params.data());
  1238. return slot;
  1239. }
  1240. if (pluginName == "Maximus") // FL's own engine from the saved state (REA_rodada36/38); the approximation otherwise
  1241. if (auto exact = maximusExact (state))
  1242. return exact;
  1243. if (pluginName == "Gross Beat") // FL's own engine from the saved slots (REA_rodada53); the volume gate otherwise
  1244. if (auto exact = grossbeat::slotFromFl (state))
  1245. return exact;
  1246. if (pluginName == "Fruity Love Philter") // FL's own engine (REA_rodada66); lovePhilterEffects' conversion otherwise
  1247. if (auto exact = philter::slotFromFl (state))
  1248. return exact;
  1249. if (pluginName == "Pitch Shifter") // the exact engines (REA_rodada61); LYNX's generic pitch shifter otherwise
  1250. if (psfl::State ps; psfl::parsePitchShifter (state.data(), state.size(), ps))
  1251. {
  1252. auto slot = makeEffectSlot (EffectKind::flPitchShifter);
  1253. psfl::toParams (ps, slot.params);
  1254. return slot;
  1255. }
  1256. if (pluginName == "Hardcore") // the exact engine without the cabinet impulses (REA_rodada71)
  1257. if (auto exact = hardcore::fromFl (state, {}))
  1258. return exact;
  1259. if (pluginName == "Distructor") // the exact modules (REA_rodada71); a generic distortion otherwise
  1260. if (const auto s = distructor::parse (state))
  1261. {
  1262. auto slot = makeEffectSlot (EffectKind::distructor);
  1263. slot.state = distructor::packBlob (state, {});
  1264. distructor::toParams (*s, slot.params.data());
  1265. return slot;
  1266. }
  1267. if (pluginName == "EQUO") // the exact engine (REA_rodada61); a flat parametric EQ otherwise
  1268. if (const auto s = equofx::parse (state))
  1269. {
  1270. auto slot = makeEffectSlot (EffectKind::equo);
  1271. slot.state = state;
  1272. equofx::toParams (*s, slot.params);
  1273. return slot;
  1274. }
  1275. if (pluginName == "Multiband Delay") // the exact engine (REA_rodada71); LYNX's generic delay otherwise
  1276. if (const auto s = mbdelay::parse (state))
  1277. {
  1278. auto slot = makeEffectSlot (EffectKind::multibandDelay);
  1279. slot.state = state;
  1280. mbdelay::toParams (*s, slot.params);
  1281. return slot;
  1282. }
  1283. if (pluginName == "Frequency Splitter") // FL's engine (REA_rodada62): its summed (main) output; shelves otherwise
  1284. if (const auto s = fsplit::parseState (state))
  1285. {
  1286. auto slot = makeEffectSlot (EffectKind::frequencySplitter);
  1287. fsplit::toParams (*s, slot.params.data());
  1288. return slot;
  1289. }
  1290. if (pluginName == "Effector")
  1291. return effector (state, bpm);
  1292. if (pluginName == "Fruity Vocoder")
  1293. return fruityVocoder (state, nullptr);
  1294. if (pluginName == "Vocodex")
  1295. if (auto v = vocodexEffect (state))
  1296. return v->slot;
  1297. if (pluginName == "Vintage Phaser") // the exact effect (REA_rodada29 §3)
  1298. return vintagePhaserFromFl (state);
  1299. if (pluginName == "Fruity Reeverb 2") // the exact effect (REA_rodada49); the Freeverb mapping below otherwise
  1300. if (auto exact = reeverb2FromFl (state))
  1301. return exact;
  1302. if (pluginName == "LuxeVerb") // the exact effect (REA_rodada60); the generic reverb mapping below otherwise
  1303. if (auto exact = luxeVerbFromFl (state))
  1304. return exact;
  1305. if (pluginName == "Fruity Convolver") // the exact effect with an embedded impulse response (REA_rodada60)
  1306. if (auto exact = convolverFromFl (state))
  1307. return exact;
  1308. if (pluginName == "Fruity Reeverb") // the first version, exact (REA_rodada49 §6)
  1309. if (auto exact = reeverb1FromFl (state))
  1310. return exact;
  1311. if (pluginName == "Pitcher")
  1312. {
  1313. // "SSLF" + u32 + "1RTP" + 33 f32 (raw parameters 0…32) + 12 B note mask + f32 (REA_rodada19 §6)
  1314. if (state.size() < 12 + 33 * 4 + 12 || std::memcmp (state.data(), "SSLF", 4) != 0 || std::memcmp (state.data() + 8, "1RTP", 4) != 0)
  1315. return std::nullopt;
  1316. float p[33];
  1317. std::memcpy (p, state.data() + 12, sizeof (p));
  1318. for (auto& v : p)
  1319. v = std::isfinite (v) ? v : 0.0f;
  1320. auto slot = makeEffectSlot (EffectKind::pitchCorrect);
  1321. slot.params[0] = std::clamp (p[0], 0.0f, 1.0f);
  1322. slot.params[1] = std::clamp (p[3], 0.0f, 1.0f);
  1323. slot.params[2] = p[4] != 0.0f ? 1.0f : 0.0f;
  1324. slot.params[3] = std::clamp (p[23], 0.0f, 1.0f);
  1325. slot.params[4] = (float) std::clamp ((int) p[32], 0, 5) / 5.0f;
  1326. const auto* mask = state.data() + 12 + 33 * 4;
  1327. for (int k = 0; k < 12; ++k)
  1328. {
  1329. slot.params[(std::size_t) (5 + k)] = mask[k] != 0 ? 1.0f : 0.0f;
  1330. slot.params[(std::size_t) (17 + k)] = p[5 + k] != 0.0f ? 1.0f : 0.0f;
  1331. }
  1332. // Formant/Gender, Pitch Reference, harmony and MIDI (REA_rodada30 §2; MIDI notes do not reach LYNX effects)
  1333. auto flag = [&p] (int i) { return p[i] != 0.0f ? 1.0f : 0.0f; };
  1334. slot.params[29] = flag (22);
  1335. slot.params[30] = std::clamp (p[2], 0.0f, 1.0f);
  1336. slot.params[31] = std::clamp (p[24], 0.0f, 1.0f);
  1337. slot.params[32] = std::clamp (p[1], 0.0f, 1.0f);
  1338. slot.params[33] = flag (27);
  1339. slot.params[34] = flag (25);
  1340. slot.params[35] = flag (26);
  1341. slot.params[36] = flag (30);
  1342. slot.params[37] = flag (31);
  1343. for (int j = 0; j < 4; ++j)
  1344. slot.params[(std::size_t) (38 + j)] = std::clamp (p[17 + j], 0.0f, 1.0f);
  1345. return slot;
  1346. }
  1347. const auto kind = effectForFlPlugin (pluginName);
  1348. if (! kind)
  1349. return std::nullopt;
  1350. auto slot = makeEffectSlot (*kind);
  1351. // case-insensitive compare (FL 3 presets: "Fruity delay 2")
  1352. std::string lower = pluginName;
  1353. std::transform (lower.begin(), lower.end(), lower.begin(), [] (unsigned char c) { return (char) std::tolower (c); });
  1354. auto is = [&] (const char* canonical) {
  1355. std::string other = canonical;
  1356. std::transform (other.begin(), other.end(), other.begin(), [] (unsigned char c) { return (char) std::tolower (c); });
  1357. return lower == other;
  1358. };
  1359. if (*kind == EffectKind::filter)
  1360. slot.params[1] = 1.0f; // open: without FL's settings, better not to muffle
  1361. if (is ("Fruity Soft Clipper"))
  1362. softClipper (state, slot);
  1363. if (is ("Fruity Parametric EQ 2"))
  1364. parametricEq2 (state, slot);
  1365. else if (is ("Fruity Parametric EQ"))
  1366. {
  1367. if (const auto exact = peqfl::parsePeq1 (state)) // REA_rodada50 §3
  1368. peqfl::toParams (*exact, slot.params);
  1369. }
  1370. else if (is ("Fruity Limiter"))
  1371. limiter (state, slot);
  1372. else if (is ("Fruity Reeverb 2"))
  1373. reeverb2Approx (state, slot, bpm);
  1374. else if (is ("Fruity Delay 2"))
  1375. delay2 (state, slot);
  1376. else if (is ("Fruity Delay 3"))
  1377. delay3 (state, slot);
  1378. else if (is ("Fruity Compressor"))
  1379. compressor (state, slot);
  1380. else if (is ("Fruity Balance"))
  1381. balance (state, slot);
  1382. else if (is ("Fruity PanOMatic"))
  1383. panOMatic (state, slot);
  1384. else if (is ("Fruity Stereo Shaper"))
  1385. stereoShaper (state, slot);
  1386. else if (is ("Maximus"))
  1387. maximus (state, slot);
  1388. else if (is ("Soundgoodizer"))
  1389. {
  1390. // the compressor approximation followed by its presets' master limiter (REA_rodada8 §2; FL's own SG DLL checked in REA_rodada35),
  1391. // as one serial branch
  1392. if (const auto sg = maximusfx::parseSoundgoodizer (state))
  1393. slot = soundgoodizerApproxSlot (sg->preset, sg->amount);
  1394. }
  1395. else if (is ("Fruity Fast Dist"))
  1396. fastDist (state, slot);
  1397. else if (is ("Fruity Filter"))
  1398. fruityFilter (state, slot);
  1399. else if (is ("Fruity Flangus"))
  1400. flangus (state, slot);
  1401. else if (is ("Gross Beat"))
  1402. grossBeat (state, slot);
  1403. else if (is ("Fruity Peak Controller"))
  1404. peakController (state, slot);
  1405. else if (is ("Pitch Shifter"))
  1406. pitchShifter (state, slot);
  1407. else if (is ("Frequency Shifter"))
  1408. frequencyShifter (state, slot);
  1409. else if (is ("Emphasis"))
  1410. emphasis (state, slot);
  1411. else if (is ("Low Lifter"))
  1412. lowLifter (state, slot);
  1413. else if (is ("Fruity Send"))
  1414. fruitySend (state, slot);
  1415. else if (is ("Transporter"))
  1416. transporter (state, slot);
  1417. else if (is ("Fruity Squeeze"))
  1418. squeeze (state, slot);
  1419. else if (is ("Frequency Splitter"))
  1420. frequencySplitter (state, slot);
  1421. else if (is ("Fruity Phase Inverter"))
  1422. {
  1423. slot.params[2] = utilityMatrixValue (-1.0); // inverts both sides
  1424. slot.params[5] = utilityMatrixValue (-1.0);
  1425. }
  1426. return slot;
  1427. }
  1428. std::optional<EffectSlot> effectFromFlPreset (const std::vector<std::uint8_t>& bytes, double bpm)
  1429. {
  1430. try
  1431. {
  1432. // the effect preset has the same format as the channel one: plugin name (0xC9) and state (0xD5)
  1433. const auto project = flp::parseProject (bytes, true);
  1434. if (project.channels.empty() || project.channels.front().pluginState.empty())
  1435. return std::nullopt;
  1436. return effectFromFl (project.channels.front().pluginName, project.channels.front().pluginState, bpm);
  1437. }
  1438. catch (const flp::Error&)
  1439. {
  1440. return std::nullopt;
  1441. }
  1442. }
  1443. std::vector<std::string> flPluginsForEffect (EffectKind kind)
  1444. {
  1445. switch (kind)
  1446. {
  1447. case EffectKind::parametricEq: return { "Fruity Parametric EQ 2" };
  1448. case EffectKind::fruityLimiter: return { "Fruity Limiter" };
  1449. case EffectKind::fruityCompressor: return { "Fruity Compressor" };
  1450. case EffectKind::fruityWaveShaper: return { "Fruity WaveShaper" };
  1451. case EffectKind::fruitySoftClipper: return { "Fruity Soft Clipper" };
  1452. case EffectKind::fruityChorus: return { "Fruity Chorus" };
  1453. case EffectKind::hyperChorus: return { "Hyper Chorus" };
  1454. case EffectKind::vintageChorus: return { "Vintage Chorus" };
  1455. case EffectKind::fruityPanOMatic: return { "Fruity PanOMatic" };
  1456. case EffectKind::fruityBalance: return { "Fruity Balance" };
  1457. case EffectKind::fruitySend: return { "Fruity Send" };
  1458. case EffectKind::fruityBloodOverdrive: return { "Fruity Blood Overdrive" };
  1459. case EffectKind::fruityFastDist: return { "Fruity Fast Dist" };
  1460. case EffectKind::fruityFlangus: return { "Fruity Flangus" };
  1461. case EffectKind::fruityFlanger: return { "Fruity Flanger" };
  1462. case EffectKind::fruityPhaser: return { "Fruity Phaser" };
  1463. case EffectKind::fruityFastLP: return { "Fruity Fast LP" };
  1464. case EffectKind::fruityFreeFilter: return { "Fruity Free Filter" };
  1465. case EffectKind::fruityFilter: return { "Fruity Filter" };
  1466. case EffectKind::newtone: return { "Newtone" };
  1467. case EffectKind::newtime: return { "Newtime" };
  1468. case EffectKind::facetEq:
  1469. case EffectKind::crestCompressor:
  1470. case EffectKind::prismMultiband:
  1471. case EffectKind::sentryGate:
  1472. case EffectKind::quellDenoiser:
  1473. case EffectKind::geminiDoubler:
  1474. case EffectKind::apexLimiter: return {};
  1475. case EffectKind::formulaController: return { "Fruity Formula Controller" }; // LYNX's own effects: no FL counterpart
  1476. case EffectKind::voiceShift: return {}; // LYNX's own effect: no FL counterpart
  1477. case EffectKind::hardwareInsert: return {}; // LYNX's own effect (round 164d): FL has none
  1478. case EffectKind::emphasizer: return { "Emphasizer" };
  1479. case EffectKind::flFrequencyShifter: return { "Frequency Shifter" };
  1480. case EffectKind::flLowLifter: return { "Low Lifter" };
  1481. case EffectKind::flPhaseInverter: return { "Fruity Phase Inverter" };
  1482. case EffectKind::flCenter: return { "Fruity Center" };
  1483. case EffectKind::flBassBoost: return { "Fruity Bass Boost" };
  1484. case EffectKind::fl7BandEq: return { "Fruity 7 Band EQ" };
  1485. case EffectKind::flDelay1: return { "Fruity Delay" };
  1486. case EffectKind::waveCandy: return { "Wave Candy" };
  1487. case EffectKind::fruityMute2: return { "Fruity Mute 2" };
  1488. case EffectKind::bigClock: return { "Fruity Big Clock" };
  1489. case EffectKind::tuner: return { "Tuner" };
  1490. case EffectKind::htmlNotebook: return { "Fruity HTML NoteBook" };
  1491. case EffectKind::scratcher: return { "Fruity Scratcher" };
  1492. case EffectKind::transmitter: return { "Transmitter" };
  1493. case EffectKind::visualizer: return { "ZGameEditor Visualizer" };
  1494. case EffectKind::xyController: return { "Fruity X-Y Controller" };
  1495. case EffectKind::xyzController: return {}; // (FL's X-Y-Z Controller: name from REA_rodada164a)
  1496. case EffectKind::controlSurface: return { "Control Surface" };
  1497. case EffectKind::limiter:
  1498. case EffectKind::compressor: return { "Fruity Limiter", "Fruity Compressor" };
  1499. case EffectKind::reverb:
  1500. case EffectKind::reeverb2: return { "Fruity Reeverb 2" };
  1501. case EffectKind::luxeVerb: return { "LuxeVerb" };
  1502. case EffectKind::maximus: return { "Maximus" }; // (a Soundgoodizer slot has its own presets A–D)
  1503. case EffectKind::convolver: return { "Fruity Convolver" };
  1504. case EffectKind::delay: return { "Fruity Delay 3", "Fruity Delay 2" };
  1505. case EffectKind::fruityDelay2: return { "Fruity Delay 2" };
  1506. case EffectKind::fruityDelay3: return { "Fruity Delay 3" };
  1507. case EffectKind::utility:
  1508. case EffectKind::stereoShaper: return { "Fruity Stereo Shaper" };
  1509. case EffectKind::stereoEnhancer: return { "Fruity Stereo Enhancer" };
  1510. case EffectKind::transientProcessor: return { "Transient Processor" };
  1511. case EffectKind::spreader: return { "Spreader" };
  1512. case EffectKind::fruityDelayBank: return { "Fruity Delay Bank" };
  1513. case EffectKind::fruityMultiband: return { "Fruity Multiband Compressor" };
  1514. case EffectKind::equo: return { "EQUO" };
  1515. case EffectKind::hardcore: return { "Hardcore" };
  1516. case EffectKind::distructor: return { "Distructor" };
  1517. case EffectKind::flPitchShifter: return { "Pitch Shifter" };
  1518. case EffectKind::multibandDelay: return { "Multiband Delay" };
  1519. case EffectKind::vocoder: return { "Fruity Vocoder" };
  1520. case EffectKind::vocodex: return { "Vocodex" };
  1521. case EffectKind::effector: return { "Effector" };
  1522. case EffectKind::frequencySplitter: return { "Frequency Splitter" };
  1523. default: return {};
  1524. }
  1525. }
  1526. std::optional<FlParamMap> effectParamFromFl (const std::string& pluginName, int k)
  1527. {
  1528. auto is = [&] (const char* n) { return pluginName == n; };
  1529. // the exact Fruity Limiter / Compressor take FL's controls as they are (value over the control's range)
  1530. if (is ("Fruity Limiter") && k >= 0 && k < fldyn::limiterControls)
  1531. return FlParamMap { k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1532. if (is ("Fruity Compressor") && k >= 0 && k < fldyn::compressorControls)
  1533. return FlParamMap { k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1534. // the exact WaveShaper (pre, mix, post) and Soft Clipper (threshold, post) take FL's controls over their ranges
  1535. if ((is ("Fruity WaveShaper") && k >= 0 && k < 3) || (is ("Fruity Soft Clipper") && k >= 0 && k < 2))
  1536. return FlParamMap { k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1537. // the exact PanOMatic, Balance and Blood Overdrive likewise (REA_rodada58)
  1538. if ((is ("Fruity PanOMatic") && k >= 0 && k < flbasic::panOMaticParams)
  1539. || (is ("Fruity Balance") && k >= 0 && k < flbasic::balanceParams)
  1540. || (is ("Fruity Blood Overdrive") && k >= 0 && k < flbasic::bloodOverdriveParams)
  1541. || (is ("Fruity Send") && k >= 0 && k < 3)) // (pan, volume, dry; the insert is not a parameter in FL)
  1542. return FlParamMap { k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1543. // Hardcore's 50 parameters: FL sends 0…1 for all of them (the box types and the cabinet as raw numbers, REA_rodada71)
  1544. if (is ("Hardcore") && k >= 0 && k < hardcore::numParams)
  1545. return FlParamMap { k, [k] (float v) { return hardcore::toSlot (k, std::clamp (v, 0.0f, 1.0f)); } };
  1546. // Distructor: module m's controls 36…38 (FL parameter 49m + 36…38) → slot parameters 3m…3m+2
  1547. if (is ("Distructor") && k >= 0 && k < 4 * distructor::moduleParams && k % distructor::moduleParams >= 36
  1548. && k % distructor::moduleParams <= 38)
  1549. return FlParamMap { 3 * (k / distructor::moduleParams) + k % distructor::moduleParams - 36,
  1550. [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1551. // EQUO's five knobs (shift, mix, morph, smoothness, volume) over their ranges; band automation is not mapped
  1552. // Multiband Delay's twelve global controls (FL parameters 384…395) over their ranges; bank cells are not mapped
  1553. if (is ("Multiband Delay") && k >= 384 && k < 396)
  1554. return FlParamMap { k - 384, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1555. if (is ("EQUO") && k >= 0 && k < 5)
  1556. return FlParamMap { k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1557. // the exact Fast LP, Free Filter and Fruity Filter likewise (REA_rodada63; the Free Filter's CENTER and the
  1558. // Filter's CENTER are buttons, not parameters)
  1559. if ((is ("Fruity Fast LP") && k >= 0 && k < flfilter::fastLPParams)
  1560. || (is ("Fruity Free Filter") && k >= 0 && k < 4)
  1561. || (is ("Fruity Filter") && k >= 0 && k < 6))
  1562. return FlParamMap { k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1563. // the exact choruses take FL's controls over their ranges
  1564. if (chorusControls (pluginName) > k && k >= 0)
  1565. return FlParamMap { k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1566. // the exact Fruity Delay 2 / 3 likewise (every control)
  1567. if (is ("Fruity Delay 2") && k >= 0 && k < fldelay::delay2Controls)
  1568. return FlParamMap { k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1569. if (is ("Fruity Delay 3") && k >= 0 && k < fldelay::delay3Controls)
  1570. return FlParamMap { k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1571. // the exact Stereo Shaper takes FL's controls over their ranges; FL's parameter 7 (effects order) is our 6
  1572. if (is ("Fruity Stereo Shaper") && ((k >= 0 && k < 6) || k == 7))
  1573. return FlParamMap { k == 7 ? 6 : k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1574. // the exact Stereo Enhancer and Transient Processor take every FL control over its range
  1575. if ((is ("Fruity Stereo Enhancer") && k >= 0 && k < 6) || (is ("Transient Processor") && k >= 0 && k < 9)
  1576. || (is ("Spreader") && k >= 0 && k < 5))
  1577. return FlParamMap { k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1578. // the exact Delay Bank: its four global controls (0…128) are the slot's parameters
  1579. if (is ("Fruity Delay Bank") && k >= 0 && k < fldbank::globalControls)
  1580. return FlParamMap { k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1581. if (is ("Fruity Multiband Compressor") && k >= 0 && k < flmb::controls) // FL's controls as they are (value over range)
  1582. // the exact Fast Dist / Flangus / Flanger take FL's controls over their ranges
  1583. if ((is ("Fruity Fast Dist") && k >= 0 && k < fastdistfx::params) || (is ("Fruity Flangus") && k >= 0 && k < flangusfx::params)
  1584. || (is ("Fruity Flanger") && k >= 0 && k < flangerfx::params))
  1585. return FlParamMap { k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1586. // the Scratcher's fourteen controls over their ranges (the platter wheel 0…30000, REA_rodada165g-flscratch165 §5);
  1587. // quantized to the control's steps like FL's
  1588. if (is ("Fruity Scratcher") && k >= 0 && k < scratcher::count)
  1589. return FlParamMap { k, [k] (float v) { return scratcher::quantize (k, std::clamp (v, 0.0f, 1.0f)); } };
  1590. // Transmitter: its 42 controls over their ranges (the switches and selectors on their steps)
  1591. if (is ("Transmitter") && k >= 0 && k < transmitter::count)
  1592. return FlParamMap { k, [k] (float v) { return transmitter::paramFromRaw (k, transmitter::rawFromParam (k, v)); } };
  1593. // the exact Fruity Phaser likewise (its nine controls; FL normalises them over their ranges)
  1594. if (is ("Fruity Phaser") && k >= 0 && k < phaserfx::params)
  1595. return FlParamMap { k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1596. if (is ("Fruity Parametric EQ 2"))
  1597. {
  1598. if (k >= 0 && k < 7) // band gain: ±18 dB
  1599. return FlParamMap { peq::param (k, 2), [] (float v) { return peq::gainValue (36.0 * v - 18.0); } };
  1600. if (k >= 7 && k < 14) // frequency: 20 Hz · 1000^v
  1601. return FlParamMap { peq::param (k - 7, 1), [] (float v) { return peq::hzValue (20.0 * std::pow (1000.0, (double) v)); } };
  1602. if (k >= 14 && k < 21) // bandwidth: the imported slot keeps FL's own width (core/ParametricEqFl.h)
  1603. return FlParamMap { peq::param (k - 14, 3), [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1604. if (k == 35)
  1605. return FlParamMap { peq::outputParam, [] (float v) { return peq::gainValue (36.0 * v - 18.0); } };
  1606. return std::nullopt;
  1607. }
  1608. if (is ("Fruity Filter"))
  1609. {
  1610. if (k == 0) // cutoff (Chamberlin SVF, as in the state import)
  1611. return FlParamMap { 1, [] (float v) {
  1612. const auto f = std::min (1.0, (std::pow (26.0, (double) v) - 1.0) * 0.04);
  1613. return fromLog (std::max (20.0, 44100.0 / 3.141592653589793 * std::asin (f / 2.0)), 20.0, 20000.0);
  1614. } };
  1615. if (k == 1)
  1616. return FlParamMap { 2, [] (float v) { return fromLog (1.0 / (1.0 - 0.8 * (double) v), 0.5, 12.0); } };
  1617. return std::nullopt;
  1618. }
  1619. if (is ("Fruity Balance") || is ("Fruity PanOMatic"))
  1620. {
  1621. if (k == 0) // pan: the full range of both matches ours
  1622. return FlParamMap { 1, [] (float v) { return v; } };
  1623. if (k == 1) // volume: 0..320 (256 = 0 dB) [HIP for the maximum]
  1624. return FlParamMap { 0, [] (float v) { return utilityGainValue (curve (11.0, 1.25 * (double) v)); } };
  1625. return std::nullopt;
  1626. }
  1627. if (is ("Fruity Fast LP") && (k == 0 || k == 1)) // cutoff / resonance: the built-in filter has the defaults, follow the knob
  1628. return FlParamMap { k + 1, [] (float v) { return v; } };
  1629. if (is ("Fruity Reeverb 2"))
  1630. {
  1631. if (k == 5) // decay 0.1..20 s -> comb feedback
  1632. return FlParamMap { 0, [] (float v) {
  1633. const auto decay = 0.1 + 19.9 * (double) v;
  1634. return clamp01 ((std::pow (10.0, -3.0 * 0.03 / decay) - 0.7) / 0.28);
  1635. } };
  1636. if (k == 10)
  1637. return FlParamMap { 4, [] (float v) { return clamp01 (curve (11.0, (double) v)); } };
  1638. if (k == 12)
  1639. return FlParamMap { 3, [] (float v) { return clamp01 (curve (11.0, (double) v) / 0.25); } };
  1640. return std::nullopt;
  1641. }
  1642. if (is ("Pitch Shifter") && k >= 0 && k < 13) // the exact effect takes FL's 13 knobs over their ranges (REA_rodada61)
  1643. return FlParamMap { k, [] (float v) { return std::clamp (v, 0.0f, 1.0f); } };
  1644. if (is ("Pitch Shifter"))
  1645. {
  1646. if (k == 0) // pitch: −12..+12 st (in cents) -> our ±24 st
  1647. return FlParamMap { 0, [] (float v) { return (float) clamp01 (0.5 + std::round (24.0 * v - 12.0) / 48.0); } };
  1648. if (k == 3)
  1649. return FlParamMap { 2, [] (float v) { return v; } };
  1650. return std::nullopt;
  1651. }
  1652. if (is ("Fruity Delay 2") && k == 1) // wet
  1653. return FlParamMap { 2, [] (float v) { return clamp01 (curve (11.0, (double) v) * 0.5); } };
  1654. if (is ("Fruity Delay 3"))
  1655. {
  1656. if (k == 14)
  1657. return FlParamMap { 1, [] (float v) { return clamp01 (v * 0.9); } };
  1658. if (k == 23)
  1659. return FlParamMap { 2, [] (float v) { return clamp01 (v * 0.5); } };
  1660. if (k == 16)
  1661. return FlParamMap { 4, [] (float v) { return clamp01 (1.0 - v); } };
  1662. }
  1663. return std::nullopt;
  1664. }
  1665. std::optional<float> controllerConstantOutput (const std::string& pluginName, const std::vector<std::uint8_t>& d, int output)
  1666. {
  1667. if (pluginName == "Fruity X-Y Controller")
  1668. {
  1669. if (d.size() < 44 || output < 0 || output > 1)
  1670. return std::nullopt;
  1671. auto param = [&] (int i) { return std::clamp (i32At (d, 4 + 4 * (std::size_t) i), -65536, 65536) / 65536.0; };
  1672. const auto axis = output * 4;
  1673. const auto level = std::clamp (param (axis), 0.0, 1.0);
  1674. // tension (REA_rodada17 §4): k = 0.1·(1001^(|t|/128) − 1); t > 0 concave, t < 0 convex
  1675. const auto t = std::clamp (i32At (d, 4 + 4 * (std::size_t) (axis + 3)), -128, 128);
  1676. auto shaped = level;
  1677. if (t != 0)
  1678. {
  1679. const auto k = 0.1 * (std::pow (1001.0, std::abs (t) / 128.0) - 1.0);
  1680. shaped = t > 0 ? 1.0 - (std::pow (k + 1.0, 1.0 - level) - 1.0) / k : (std::pow (k + 1.0, level) - 1.0) / k;
  1681. }
  1682. return (float) std::clamp (param (axis + 1) + (param (axis + 2) - param (axis + 1)) * shaped, 0.0, 1.0);
  1683. }
  1684. if (pluginName == "Control Surface")
  1685. {
  1686. if (d.size() < 16)
  1687. return std::nullopt;
  1688. std::optional<float> found;
  1689. // chunks u32 id + u64 size (id 0 = group); 0x836 = a control's value and the published output
  1690. std::function<void (std::size_t, std::size_t)> walk = [&] (std::size_t pos, std::size_t end) {
  1691. while (pos + 12 <= end && ! found)
  1692. {
  1693. std::uint32_t id = 0;
  1694. std::uint64_t size = 0;
  1695. std::memcpy (&id, d.data() + pos, 4);
  1696. std::memcpy (&size, d.data() + pos + 4, 8);
  1697. if (size > end - pos - 12)
  1698. return;
  1699. const auto body = pos + 12;
  1700. if (id == 0)
  1701. walk (body, body + (std::size_t) size);
  1702. else if (id == 0x836 && size >= 12 && i32At (d, body + 8) == output)
  1703. {
  1704. float v = 0.0f;
  1705. std::memcpy (&v, d.data() + body, 4);
  1706. if (std::isfinite (v))
  1707. found = std::clamp (v, 0.0f, 1.0f);
  1708. }
  1709. pos = body + (std::size_t) size;
  1710. }
  1711. };
  1712. walk (4, d.size());
  1713. return found;
  1714. }
  1715. return std::nullopt;
  1716. }
  1717. namespace
  1718. {
  1719. // FL knob gain curve: (11^x − 1)/10, signed
  1720. double scurve11 (double x) { return (x < 0.0 ? -1.0 : 1.0) * (std::pow (11.0, std::abs (x)) - 1.0) / 10.0; }
  1721. struct PhilterBand
  1722. {
  1723. peq::BandType type = peq::BandType::off;
  1724. double hz = 1000.0, q = 0.707, gainDb = 0.0;
  1725. int stages = 1;
  1726. bool ok = false;
  1727. std::vector<double> stageHz; // alternate modes: cutoff of each stage
  1728. };
  1729. // type, cutoff and Q of a unit with cutoff c and resonance r (0..1), at 44.1 kHz (k = 1)
  1730. PhilterBand philterBand (const LovePhilterUnit& u, double c, double r)
  1731. {
  1732. PhilterBand b;
  1733. c = std::clamp (c, 0.0, 1.0);
  1734. r = std::clamp (r, 0.0, 1.0);
  1735. const auto mode = std::clamp (u.mode, 0, 5);
  1736. if (mode == 0)
  1737. return b; // the unit does not filter
  1738. b.stages = mode == 4 ? 2 : mode == 5 ? 3 : mode;
  1739. auto lpQ = [] (double rr) { return 0.1 + 0.02495 * (std::pow (2001.0, rr) - 1.0); };
  1740. const auto lowGain = scurve11 (std::clamp (u.params[3], 0, 128) / 128.0);
  1741. const auto bandGain = scurve11 (std::clamp (u.params[4], 0, 128) / 128.0);
  1742. const auto highGain = scurve11 (std::clamp (u.params[5], 0, 128) / 128.0);
  1743. double x = 0.0;
  1744. std::function<double (double)> hzOf;
  1745. switch (u.type)
  1746. {
  1747. case 0: // Chamberlin SVF: fc = (x + 0.005)·44100/π; output = L·LP + B·BP + H·HP
  1748. {
  1749. x = (std::pow (51.0, c) - 1.0) * 0.0159;
  1750. hzOf = [] (double v) { return (v + 0.005) * 44100.0 / 3.141592653589793; };
  1751. b.hz = hzOf (x);
  1752. auto d = r <= 0.0 ? 1.0 : r >= 1.0 ? 0.2 : 1.0 - 0.9 * r;
  1753. if (u.alt)
  1754. d *= d;
  1755. b.q = 1.0 / std::max (0.05, d);
  1756. const auto l = std::abs (lowGain) > 1e-6, bp = std::abs (bandGain) > 1e-6, h = std::abs (highGain) > 1e-6;
  1757. // only the pure outputs have an equivalent: LP, BP, HP or L+H (band-reject) with the same gain
  1758. if (l && ! bp && ! h)
  1759. b.type = peq::BandType::highCut, b.gainDb = 20.0 * std::log10 (std::abs (lowGain));
  1760. else if (h && ! l && ! bp)
  1761. b.type = peq::BandType::lowCut, b.gainDb = 20.0 * std::log10 (std::abs (highGain));
  1762. else if (bp && ! l && ! h)
  1763. b.type = peq::BandType::bandPass, b.gainDb = 20.0 * std::log10 (std::abs (bandGain));
  1764. else if (l && h && ! bp && std::abs (lowGain - highGain) < 1e-6)
  1765. b.type = peq::BandType::notch, b.gainDb = 20.0 * std::log10 (std::abs (lowGain));
  1766. else
  1767. return b;
  1768. b.ok = true;
  1769. break;
  1770. }
  1771. case 1: case 2: // Lime: ω = (x + 0.007)·π/2 -> fc = (x + 0.007)·44100/4
  1772. {
  1773. x = std::min (1.6, (std::pow (76.0, c) - 1.0) * 0.01324);
  1774. hzOf = [] (double v) { return (v + 0.007) * 44100.0 / 4.0; };
  1775. b.hz = hzOf (x);
  1776. b.q = std::min (50.0, std::pow (1001.0, r));
  1777. b.type = u.type == 1 ? peq::BandType::highCut : peq::BandType::lowCut; // the L slot is "drive" here: left out
  1778. b.ok = true;
  1779. break;
  1780. }
  1781. case 3: // Vanilla LP: fc = (x + 0.003)·44100
  1782. {
  1783. x = (std::pow (1501.0, c) - 1.0) * 0.000664667;
  1784. hzOf = [] (double v) { return (v + 0.003) * 44100.0; };
  1785. b.hz = hzOf (x);
  1786. b.q = lpQ (r);
  1787. b.type = peq::BandType::highCut;
  1788. b.ok = true;
  1789. break;
  1790. }
  1791. case 6: case 8: case 9: case 10: // Vanilla 4…10 family: fc = (x + 0.0001)·44100
  1792. {
  1793. x = std::min (1.0001, (std::pow (1501.0, c) - 1.0) * 0.00039993);
  1794. if (u.type == 6)
  1795. x = std::min (x, 0.6001);
  1796. hzOf = [] (double v) { return (v + 0.0001) * 44100.0; };
  1797. b.hz = hzOf (x);
  1798. if (u.type == 6)
  1799. {
  1800. b.type = peq::BandType::lowCut;
  1801. b.q = lpQ (r);
  1802. }
  1803. else if (u.type == 9)
  1804. {
  1805. b.type = peq::BandType::peak;
  1806. b.q = 0.1 + 0.04995 * (std::pow (2001.0, 1.0 - r) - 1.0);
  1807. b.gainDb = 20.0 * std::log10 (std::max (1e-6, std::abs (lowGain))); // the peak gain is the low/gain slot
  1808. }
  1809. else
  1810. {
  1811. b.type = u.type == 8 ? peq::BandType::lowShelf : peq::BandType::highShelf;
  1812. b.q = 0.1 + 0.45 * (std::pow (3.0, 1.0 - r) - 1.0); // shelf slope S
  1813. b.gainDb = 20.0 * std::log10 (std::max (1e-6, std::abs (lowGain)));
  1814. }
  1815. b.ok = true;
  1816. break;
  1817. }
  1818. case 4: case 5: // Vanilla band-pass / band-reject: width in octaves (REA_rodada19 §7.1)
  1819. {
  1820. x = std::min (1.0001, (std::pow (1501.0, c) - 1.0) * 0.00039993);
  1821. hzOf = [] (double v) { return (v + 0.0001) * 44100.0; };
  1822. b.hz = hzOf (x);
  1823. const auto n = std::max (1, mode);
  1824. auto bw = 0.01 + 0.1199 * (std::pow (101.0, u.alt ? r * (c + 1.0) * 0.5 : r) - 1.0);
  1825. bw = u.type == 4 ? bw * n : bw / n; // per stage
  1826. const auto w = std::clamp (2.0 * 3.141592653589793 * b.hz / 44100.0, 1e-4, 2.8495);
  1827. b.q = std::clamp (1.0 / (2.0 * std::sinh (std::log (2.0) / 2.0 * bw * w / std::sin (w))), 0.1, 18.0);
  1828. b.type = u.type == 4 ? peq::BandType::bandPass : peq::BandType::notch;
  1829. b.ok = true;
  1830. if (b.stages > 1) // the width was already divided per stage: don't redo the Q root below
  1831. {
  1832. b.hz = std::clamp (b.hz, 20.0, 20000.0);
  1833. return b;
  1834. }
  1835. break;
  1836. }
  1837. case 7: // all-pass: only changes phase; alone in the unit, the magnitude is 1 (REA_rodada19 §7.2)
  1838. b.type = peq::BandType::off;
  1839. b.ok = true;
  1840. return b;
  1841. case 11: // Mango: 4 poles with feedback; the cutoff in Hz is inferred from the pole (REA_rodada19 §7.3)
  1842. {
  1843. x = std::min (3.0, (std::pow (201.0, c) - 1.0) * 0.01495);
  1844. hzOf = [] (double v) {
  1845. const auto w = std::min ((v + 0.01) * 0.5, 0.65);
  1846. return 22050.0 * w * (1.873 * w * w * w + 0.4955 * w * w - 0.649 * w + 0.9988);
  1847. };
  1848. b.hz = hzOf (x);
  1849. b.q = lpQ (r);
  1850. b.type = peq::BandType::highCut;
  1851. b.stages = std::min (4, 2 * b.stages); // 4 poles (24 dB/oct) per stage
  1852. b.ok = true;
  1853. break;
  1854. }
  1855. default:
  1856. return b;
  1857. }
  1858. if (b.stages > 1)
  1859. b.q = std::pow (b.q, 1.0 / b.stages);
  1860. // alternate (modes 4/5): each stage's frequency variable is x, x^E, x^(E²), E = 0.5 / 0.75
  1861. if ((mode == 4 || mode == 5) && hzOf)
  1862. {
  1863. const auto e = mode == 4 ? 0.5 : 0.75;
  1864. auto stageX = x;
  1865. for (int i = 0; i < b.stages; ++i)
  1866. {
  1867. b.stageHz.push_back (std::clamp (hzOf (stageX), 20.0, 20000.0));
  1868. stageX = std::pow (std::max (0.0, stageX), e);
  1869. }
  1870. }
  1871. b.hz = std::clamp (b.hz, 20.0, 20000.0);
  1872. return b;
  1873. }
  1874. EffectSlot philterSlot (const PhilterBand& band, double unitGain)
  1875. {
  1876. auto slot = makeEffectSlot (EffectKind::parametricEq);
  1877. for (int i = 0; i < peq::bands; ++i)
  1878. slot.params[(std::size_t) peq::param (i, 0)] = peq::typeValue (peq::BandType::off);
  1879. double outputDb = 20.0 * std::log10 (std::max (1e-6, unitGain));
  1880. if (band.ok)
  1881. {
  1882. const auto cut = band.type == peq::BandType::highCut || band.type == peq::BandType::lowCut;
  1883. const auto alternate = ! band.stageHz.empty();
  1884. // cuts: stages = EQ slope; the other types (and the alternate ones) repeat the band (cascade)
  1885. const auto copies = cut && ! alternate ? 1 : std::min (band.stages, peq::bands);
  1886. for (int i = 0; i < copies; ++i)
  1887. {
  1888. slot.params[(std::size_t) peq::param (i, 0)] = peq::typeValue (band.type);
  1889. slot.params[(std::size_t) peq::param (i, 1)] = peq::hzValue (alternate ? band.stageHz[(std::size_t) i] : band.hz);
  1890. slot.params[(std::size_t) peq::param (i, 2)] = peq::gainValue (std::clamp (band.gainDb, -18.0, 18.0));
  1891. slot.params[(std::size_t) peq::param (i, 3)] = peq::qValue (band.q);
  1892. slot.params[(std::size_t) (peq::slopeParam + i)] = peq::slopeValue (cut && ! alternate ? std::clamp (band.stages, 1, 4) : 1);
  1893. }
  1894. if (cut) // for cuts the L/H slot gain multiplies the output
  1895. outputDb += band.gainDb;
  1896. }
  1897. slot.params[peq::outputParam] = peq::gainValue (std::clamp (outputDb, -18.0, 18.0));
  1898. return slot;
  1899. }
  1900. } // namespace
  1901. std::optional<LovePhilterConversion> lovePhilterEffects (const std::vector<std::uint8_t>& stateBytes)
  1902. {
  1903. const auto state = parseLovePhilter (stateBytes);
  1904. if (! state)
  1905. return std::nullopt;
  1906. const auto master = scurve11 (std::clamp (state->globals[0], 0, 160) / 128.0);
  1907. std::vector<int> active;
  1908. for (int u = 0; u < 8; ++u)
  1909. if (state->units[(std::size_t) u].enabled && (state->soloUnit < 0 || state->soloUnit == u))
  1910. active.push_back (u);
  1911. LovePhilterConversion out;
  1912. // volume articulator (multiplicative, REA_rodada17 §15.3 + rodada18 §4): base × mapX(x) × mapY(y);
  1913. // disabled envelope/LFO are neutral (enabled ones are left out)
  1914. const auto modX = std::clamp (state->modX, 0, 1024) / 1024.0, modY = std::clamp (state->modY, 0, 1024) / 1024.0;
  1915. auto volumeFactor = [] (const LovePhilterUnit& unit, double x, double y) {
  1916. const auto& a = unit.articulators[1];
  1917. const auto base = std::isfinite (a.base) ? std::clamp ((double) a.base, 0.0, 4.0) : 1.0;
  1918. return std::max (0.0, base * lovePhilterMapAt (a.mapX, x) * lovePhilterMapAt (a.mapY, y));
  1919. };
  1920. auto mapsNeutral = [] (const LovePhilterMap& m) {
  1921. return std::all_of (m.points.begin(), m.points.end(), [] (const auto& pt) { return std::abs (pt.second - 1.0) < 1e-6; });
  1922. };
  1923. for (const auto u : active)
  1924. if (u + 1 < 8 && state->units[(std::size_t) u + 1].enabled && state->units[(std::size_t) u].params[11] != 0)
  1925. {
  1926. out.notes.push_back ("Love Philter: chained units (NEXT) play in parallel");
  1927. break;
  1928. }
  1929. auto unitEffects = [&] (int u) {
  1930. const auto& unit = state->units[(std::size_t) u];
  1931. const auto band = philterBand (unit, std::clamp (unit.params[1], 0, 256) / 256.0, std::clamp (unit.params[2], 0, 256) / 256.0);
  1932. if (unit.mode != 0 && ! band.ok)
  1933. out.notes.push_back ("Love Philter: filter type " + std::to_string (unit.type) + " has no equivalent");
  1934. const auto gain = scurve11 (std::clamp (unit.params[9], 0, 160) / 128.0) * master * std::abs (scurve11 (std::clamp (unit.params[10], -160, 160) / 128.0));
  1935. std::vector<EffectSlot> chain { philterSlot (band, gain) };
  1936. const auto& vol = unit.articulators[1];
  1937. if (! mapsNeutral (vol.mapX) || ! mapsNeutral (vol.mapY) || std::abs (vol.base - 1.0f) > 1e-6f)
  1938. {
  1939. auto utility = makeEffectSlot (EffectKind::utility); // volume controlled by Mod X/Y
  1940. utility.params[0] = utilityGainValue (volumeFactor (unit, modX, modY));
  1941. chain.push_back (utility);
  1942. }
  1943. // pattern envelope on volume (mode 0: gain × y·d_E), in beats and looped (REA_rodada20 §1.2–1.4)
  1944. if (const auto& env = vol.pattern; env.on)
  1945. {
  1946. double period = 0.0;
  1947. for (std::size_t k = 1; k < env.points.size(); ++k)
  1948. period += env.points[k].dt;
  1949. // points (x in beats, level, tension 0..1); curve type 5 ("pulse", REA_rodada27 §3.2) becomes steps: a square
  1950. // wave of n + 1 cycles between the two levels, n = round(0.001·(1000001^(1 − 0.6|t|) − 1)), flips at x = j/P
  1951. // (P = 2(n + 1)), starting on the previous level for t ≥ 0 and on the point's level for t < 0
  1952. struct GatePoint { double x, y, tension; };
  1953. std::vector<GatePoint> gatePoints;
  1954. const auto depth = std::clamp ((double) vol.envDepth, 0.0, 1.0);
  1955. double x = 0.0;
  1956. for (std::size_t k = 0; k < env.points.size(); ++k)
  1957. {
  1958. const auto& pt = env.points[k];
  1959. const auto level = pt.y * depth;
  1960. if (k > 0 && pt.curve == 5 && ! gatePoints.empty())
  1961. {
  1962. const auto t = (double) pt.tension;
  1963. const auto steps = 2 * ((int) std::round (0.001 * (std::pow (1000001.0, 1.0 - 0.6 * std::abs (t)) - 1.0)) + 1);
  1964. if (steps <= 22)
  1965. {
  1966. const auto from = gatePoints.back().y, eps = 1e-6 * pt.dt;
  1967. auto valueAt = [&] (int j) { return (j % 2 == 0) == (t >= 0.0) ? from : level; }; // interval j of P
  1968. if (valueAt (0) != from)
  1969. gatePoints.push_back ({ x + eps, valueAt (0), 0.5 });
  1970. for (int j = 1; j < steps; ++j)
  1971. {
  1972. const auto at = x + pt.dt * j / steps;
  1973. gatePoints.push_back ({ at - eps, valueAt (j - 1), 0.5 });
  1974. gatePoints.push_back ({ at, valueAt (j), 0.5 });
  1975. }
  1976. if (valueAt (steps - 1) != level)
  1977. gatePoints.push_back ({ x + pt.dt - eps, valueAt (steps - 1), 0.5 });
  1978. }
  1979. else
  1980. out.notes.push_back ("Love Philter: pulse curve with too many cycles treated as simple");
  1981. }
  1982. else if (k > 0 && pt.curve != 0)
  1983. out.notes.push_back ("Love Philter: envelope curve of type " + std::to_string (pt.curve) + " treated as simple");
  1984. x += k > 0 ? pt.dt : 0.0;
  1985. gatePoints.push_back ({ x, level, (pt.tension + 1.0) / 2.0 });
  1986. }
  1987. if (! env.tempo || period <= 1e-6 || gatePoints.size() > 12 || gatePoints.empty())
  1988. out.notes.push_back ("Love Philter: volume envelope outside the supported format");
  1989. else
  1990. {
  1991. auto gate = makeEffectSlot (EffectKind::rhythmGain);
  1992. gate.params[0] = (float) std::min (1.0, period / 64.0);
  1993. gate.params[1] = (float) gatePoints.size() / 12.0f;
  1994. for (std::size_t k = 0; k < gatePoints.size(); ++k)
  1995. {
  1996. gate.params[2 + 3 * k] = (float) clamp01 (gatePoints[k].x / period);
  1997. gate.params[3 + 3 * k] = (float) clamp01 (gatePoints[k].y);
  1998. gate.params[4 + 3 * k] = (float) clamp01 (gatePoints[k].tension);
  1999. }
  2000. chain.push_back (gate);
  2001. }
  2002. }
  2003. const auto pan = std::clamp (unit.params[8], -128, 128) / 128.0;
  2004. if (const auto& lfo = unit.articulators[0].lfo; lfo.on && ! lfo.frozen && lfo.tempo)
  2005. {
  2006. // pan = PAN/128 + 2·A·d_L·W(φ), period 625/(10001^(S/65536) − 1) beats (REA_rodada20 §1.5)
  2007. auto autoPan = makeEffectSlot (EffectKind::autoPan);
  2008. const auto period = 625.0 / (std::pow (10001.0, std::clamp (lfo.speed, 1, 65536) / 65536.0) - 1.0);
  2009. const auto depth = std::clamp (2.0 * lfo.amplitude * unit.articulators[0].lfoDepth, -1.0, 1.0);
  2010. autoPan.params[0] = (float) std::clamp (period / 64.0, 1e-4, 1.0);
  2011. autoPan.params[1] = (float) clamp01 (depth * 0.5 + 0.5);
  2012. autoPan.params[2] = (float) (lfo.phase / 4294967296.0);
  2013. autoPan.params[3] = (float) clamp01 (pan * 0.5 + 0.5);
  2014. autoPan.params[5] = 1.0f; // FL's pan law (REA_rodada27 §3.1)
  2015. autoPan.params[6] = (float) clamp01 ((std::clamp (lfo.tension, -128, 128) + 128) / 256.0); // §3.3
  2016. chain.push_back (autoPan);
  2017. if (lfo.skew != 0 || lfo.width != 0)
  2018. out.notes.push_back ("Love Philter: pan LFO shape approximated by a triangle");
  2019. }
  2020. else if (std::abs (pan) > 1e-3)
  2021. {
  2022. // FL's pan law (REA_rodada27 §3.1): √2·cos/sin((1 + pan)π/4) with the far side folded into the near one
  2023. float ll, rl, lr, rr;
  2024. flPanMatrix (pan, ll, rl, lr, rr);
  2025. auto utility = makeEffectSlot (EffectKind::utility);
  2026. utility.params[2] = utilityMatrixValue (ll);
  2027. utility.params[3] = utilityMatrixValue (rl);
  2028. utility.params[4] = utilityMatrixValue (lr);
  2029. utility.params[5] = utilityMatrixValue (rr);
  2030. chain.push_back (utility);
  2031. }
  2032. // cutoff and resonance envelope/LFO: live modulation of the EQ, added by modulateUnit (below)
  2033. return chain;
  2034. };
  2035. // cutoff/resonance envelope and LFO (additive, REA_rodada20 §1.4/§1.5): c = CUT/256 + (y − 0.5)·d_E (for the
  2036. // cutoff d_E = ENV/128·depth) or + A·d_L·W, and r = RES/256 + the same terms; a "control-only" slot generates
  2037. // the value and a live automation drives the EQ's frequency or Q
  2038. // the chain's EQ (slot 0) gets one control-only slot per enabled envelope/LFO, appended to `chain`;
  2039. // `addLink (source, param, curve)` records the live link from that slot to the EQ
  2040. auto modulateUnit = [&] (const LovePhilterUnit& unit, std::vector<EffectSlot>& chain,
  2041. const std::function<void (int, int, std::vector<float>)>& recordLink) {
  2042. const auto cut0 = std::clamp (unit.params[1], 0, 256) / 256.0, res0 = std::clamp (unit.params[2], 0, 256) / 256.0;
  2043. auto modulate = [&] (const auto& art, bool cutoff, double envDepth) {
  2044. if (! (art.pattern.on || art.lfo.on) || chain.empty() || chain[0].kind != EffectKind::parametricEq)
  2045. return;
  2046. auto addLink = [&] (EffectSlot control, const std::function<double (double)>& valueOf) {
  2047. std::vector<float> curve;
  2048. for (int i = 0; i <= 32; ++i)
  2049. {
  2050. const auto v = std::clamp (valueOf (i / 32.0), 0.0, 1.0);
  2051. const auto band = cutoff ? philterBand (unit, v, res0) : philterBand (unit, cut0, v);
  2052. curve.push_back (cutoff ? peq::hzValue (band.hz) : peq::qValue (band.q));
  2053. }
  2054. recordLink ((int) chain.size(), peq::param (0, cutoff ? 1 : 3), std::move (curve));
  2055. chain.push_back (std::move (control));
  2056. };
  2057. const auto start = cutoff ? cut0 : res0;
  2058. const auto& env = art.pattern;
  2059. double period = 0.0;
  2060. for (std::size_t k = 1; k < env.points.size(); ++k)
  2061. period += env.points[k].dt;
  2062. if (env.on && env.tempo && period > 1e-6 && env.points.size() <= 12)
  2063. {
  2064. auto control = makeEffectSlot (EffectKind::rhythmGain);
  2065. control.params[0] = (float) std::min (1.0, period / 64.0);
  2066. control.params[1] = (float) env.points.size() / 12.0f;
  2067. double x = 0.0;
  2068. for (std::size_t k = 0; k < env.points.size(); ++k)
  2069. {
  2070. if (k > 0)
  2071. x += env.points[k].dt;
  2072. control.params[2 + 3 * k] = (float) clamp01 (x / period);
  2073. control.params[3 + 3 * k] = (float) clamp01 (env.points[k].y);
  2074. control.params[4 + 3 * k] = (float) clamp01 ((env.points[k].tension + 1.0) / 2.0);
  2075. }
  2076. control.params[38] = 1.0f;
  2077. addLink (std::move (control), [=] (double y) { return start + (y - 0.5) * envDepth; });
  2078. }
  2079. else if (env.on)
  2080. out.notes.push_back (std::string ("Love Philter: ") + (cutoff ? "cutoff" : "resonance") + " envelope outside the supported format");
  2081. if (const auto& lfo = art.lfo; lfo.on && lfo.tempo && ! lfo.frozen)
  2082. {
  2083. auto control = makeEffectSlot (EffectKind::autoPan);
  2084. const auto period = 625.0 / (std::pow (10001.0, std::clamp (lfo.speed, 1, 65536) / 65536.0) - 1.0);
  2085. control.params[0] = (float) std::clamp (period / 64.0, 1e-4, 1.0);
  2086. control.params[2] = (float) (lfo.phase / 4294967296.0);
  2087. control.params[4] = 1.0f;
  2088. const auto amount = lfo.amplitude * art.lfoDepth;
  2089. addLink (std::move (control), [=] (double v) { return start + amount * (2.0 * v - 1.0); });
  2090. }
  2091. };
  2092. modulate (unit.articulators[2], true, std::clamp (unit.params[0], -128, 128) / 128.0 * unit.articulators[2].envDepth);
  2093. modulate (unit.articulators[3], false, unit.articulators[3].envDepth);
  2094. };
  2095. if (active.empty())
  2096. {
  2097. out.slots.push_back (philterSlot ({}, 0.0)); // no unit enabled: the Love Philter is silent
  2098. return out;
  2099. }
  2100. if (active.size() == 1)
  2101. {
  2102. const auto u = active.front();
  2103. out.slots = unitEffects (u);
  2104. const auto& unit = state->units[(std::size_t) u];
  2105. modulateUnit (unit, out.slots, [&] (int source, int param, std::vector<float> curve) {
  2106. out.links.push_back ({ source, 0, param, std::move (curve) });
  2107. });
  2108. const auto base = 8 + 44 * u;
  2109. const auto masterGain = master;
  2110. // Mod X (k = 6) / Mod Y (k = 7) drive the volume utility, if any
  2111. const auto modSlot = out.slots.size() >= 2 && out.slots[1].kind == EffectKind::utility && out.slots[1].params[1] == 0.5f ? 1 : -1;
  2112. out.paramMap = [unit, base, masterGain, modSlot, modX, modY, volumeFactor] (int k) -> std::optional<std::tuple<int, int, std::function<float (float)>>> {
  2113. if ((k == 6 || k == 7) && modSlot >= 0)
  2114. return std::make_tuple (modSlot, 0, std::function<float (float)> ([unit, k, modX, modY, volumeFactor] (float v) {
  2115. return utilityGainValue (k == 6 ? volumeFactor (unit, v, modY) : volumeFactor (unit, modX, v));
  2116. }));
  2117. if (k == base + 1) // CUT: c = value
  2118. return std::make_tuple (0, peq::param (0, 1), std::function<float (float)> ([unit] (float v) {
  2119. const auto b = philterBand (unit, v, std::clamp (unit.params[2], 0, 256) / 256.0);
  2120. return peq::hzValue (b.hz);
  2121. }));
  2122. if (k == base + 2) // RES
  2123. return std::make_tuple (0, peq::param (0, 3), std::function<float (float)> ([unit] (float v) {
  2124. const auto b = philterBand (unit, std::clamp (unit.params[1], 0, 256) / 256.0, v);
  2125. return peq::qValue (b.q);
  2126. }));
  2127. if (k == base + 9) // VOL: 0..160
  2128. return std::make_tuple (0, peq::outputParam, std::function<float (float)> ([masterGain] (float v) {
  2129. const auto g = scurve11 (160.0 * v / 128.0) * masterGain;
  2130. return peq::gainValue (std::clamp (20.0 * std::log10 (std::max (1e-6, g)), -18.0, 18.0));
  2131. }));
  2132. return std::nullopt;
  2133. };
  2134. return out;
  2135. }
  2136. auto container = makeEffectSlot (EffectKind::parallel);
  2137. for (const auto u : active)
  2138. {
  2139. auto chain = unitEffects (u);
  2140. const auto branch = (int) container.branches.size();
  2141. modulateUnit (state->units[(std::size_t) u], chain, [&] (int source, int param, std::vector<float> curve) {
  2142. container.links.push_back ({ branch, source, 0, param, std::move (curve) });
  2143. });
  2144. container.branches.push_back ({ std::move (chain), 1.0f });
  2145. }
  2146. out.slots.push_back (std::move (container));
  2147. return out;
  2148. }
  2149. PatcherBranchHead patcherBranchHead (const std::string& plugin, const std::vector<std::uint8_t>& d, int bus)
  2150. {
  2151. PatcherBranchHead head;
  2152. std::string name = plugin;
  2153. std::transform (name.begin(), name.end(), name.begin(), [] (unsigned char c) { return (char) std::tolower (c); });
  2154. if (name == "frequency splitter")
  2155. {
  2156. if (const auto s = fsplit::parseState (d))
  2157. {
  2158. // the exact engine (REA_rodada62): bus 0 = the summed output, bus k = the bands whose send destination is k
  2159. if (! fsplit::busUsed (*s, bus))
  2160. return head;
  2161. auto exact = makeEffectSlot (EffectKind::frequencySplitter);
  2162. fsplit::toParams (*s, exact.params.data(), bus);
  2163. head.valid = true;
  2164. head.slot = exact;
  2165. return head;
  2166. }
  2167. if (d.size() < 18 * 4)
  2168. return head;
  2169. auto word = [&] (int i) { return i32At (d, 4 * (std::size_t) i); };
  2170. if (bus == 0)
  2171. {
  2172. auto slot = makeEffectSlot (EffectKind::parametricEq);
  2173. if (frequencySplitter (d, slot))
  2174. {
  2175. head.valid = true;
  2176. head.slot = slot;
  2177. }
  2178. return head;
  2179. }
  2180. int band = -1;
  2181. for (int i = 0; i < 3; ++i)
  2182. if (word (15 + i) == bus)
  2183. band = i;
  2184. if (band < 0)
  2185. return head;
  2186. head.valid = true;
  2187. const auto threeBands = word (6) != 0;
  2188. if (! threeBands && band == 1)
  2189. {
  2190. head.gain = 0.0f; // with 2 bands the mid bus is muted
  2191. return head;
  2192. }
  2193. // the band becomes a Crossover with the exact structure of each slope (REA_rodada17 §1.2); only the
  2194. // 18 dB/oct one (FL's all-pass structure) is approximated by a 3rd-order Butterworth
  2195. auto slot = makeEffectSlot (EffectKind::crossover);
  2196. auto value = [] (int v) { return (float) (std::clamp (v, 0, 65536) / 65536.0); };
  2197. auto combo = [] (int c) { return (float) std::clamp (c, 0, 7) / 7.0f; };
  2198. bool approximate = false;
  2199. auto highPass = [&] (int freqWord, int comboWord) {
  2200. slot.params[0] = 1.0f;
  2201. slot.params[1] = value (word (freqWord));
  2202. slot.params[2] = combo (word (comboWord));
  2203. approximate = approximate || word (comboWord) == 2;
  2204. };
  2205. auto lowPass = [&] (int freqWord, int comboWord) {
  2206. slot.params[3] = 1.0f;
  2207. slot.params[4] = value (word (freqWord));
  2208. slot.params[5] = combo (word (comboWord));
  2209. approximate = approximate || word (comboWord) == 2;
  2210. };
  2211. if (band == 0)
  2212. lowPass (1, 3);
  2213. else if (band == 1)
  2214. {
  2215. highPass (1, 3);
  2216. lowPass (2, 4);
  2217. }
  2218. else if (threeBands)
  2219. highPass (2, 4);
  2220. else
  2221. highPass (1, 3);
  2222. head.slot = slot;
  2223. head.gain = (float) std::pow (10.0, std::clamp (word (9 + band), -6000, 1800) / 100.0 / 20.0); // send level (hundredths of a dB)
  2224. if (approximate)
  2225. head.note = "Frequency Splitter: 18 dB/oct slope approximated";
  2226. return head;
  2227. }
  2228. if (name == "fruity stereo shaper")
  2229. {
  2230. if (d.size() < 4 + 8 * 4)
  2231. return head;
  2232. if (const auto s = stereofx::parseStereoShaper (d))
  2233. {
  2234. // the exact engine (REA_rodada54): bus 0 = its output, the parameter-6 bus = input − output
  2235. if (bus != 0 && bus != s->send)
  2236. return head;
  2237. auto exact = makeEffectSlot (EffectKind::stereoShaper);
  2238. stereofx::stereoShaperToParams (*s, exact.params.data());
  2239. exact.params[7] = bus != 0 ? 1.0f : 0.0f;
  2240. head.valid = true;
  2241. head.slot = exact;
  2242. return head;
  2243. }
  2244. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  2245. auto slot = makeEffectSlot (EffectKind::utility);
  2246. if (bus == 0)
  2247. {
  2248. stereoShaper (d, slot);
  2249. head.valid = true;
  2250. head.slot = slot;
  2251. return head;
  2252. }
  2253. if (bus != param (6) || param (4) != 0 || param (5) != 0)
  2254. return head; // with delay/phase the difference is not a matrix: skip
  2255. // input − matrix output M = (I − M)
  2256. const auto rl = param (0) / 12800.0, l = param (1) / 12800.0, r = param (2) / 12800.0, lr = param (3) / 12800.0;
  2257. slot.params[2] = utilityMatrixValue (1.0 - l);
  2258. slot.params[3] = utilityMatrixValue (-rl);
  2259. slot.params[5] = utilityMatrixValue (1.0 - r);
  2260. slot.params[4] = utilityMatrixValue (-lr);
  2261. head.valid = true;
  2262. head.slot = slot;
  2263. return head;
  2264. }
  2265. return head;
  2266. }
  2267. } // namespace lynx