FlEffects.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. #include "core/FlEffects.h"
  2. #include "core/FlSynths.h"
  3. #include <algorithm>
  4. #include <functional>
  5. #include <cctype>
  6. #include <cmath>
  7. #include <cstring>
  8. #include "core/Effects.h"
  9. #include "core/FlpImport.h"
  10. #include "flp/Flp.h"
  11. namespace lynx
  12. {
  13. namespace
  14. {
  15. std::int32_t i32At (const std::vector<std::uint8_t>& d, std::size_t offset)
  16. {
  17. std::int32_t v = 0;
  18. if (offset + 4 <= d.size())
  19. std::memcpy (&v, d.data() + offset, 4);
  20. return v;
  21. }
  22. // Lk(x) = (k^x − 1)/(k − 1): as curvas de ganho dos plugins Fruity
  23. double curve (double k, double x)
  24. {
  25. return (std::pow (k, std::max (0.0, x)) - 1.0) / (k - 1.0);
  26. }
  27. double toDb (double gain)
  28. {
  29. return 20.0 * std::log10 (std::max (gain, 1.0e-6));
  30. }
  31. // inverso de logRange (lo·(hi/lo)^v)
  32. float fromLog (double value, double lo, double hi)
  33. {
  34. return (float) std::clamp (std::log (std::max (value, lo) / lo) / std::log (hi / lo), 0.0, 1.0);
  35. }
  36. float clamp01 (double v)
  37. {
  38. return (float) std::clamp (v, 0.0, 1.0);
  39. }
  40. // Q de um pico/passa-banda pela largura em oitavas (cookbook)
  41. double qFromOctaves (double octaves)
  42. {
  43. return 1.0 / (2.0 * std::sinh (std::log (2.0) / 2.0 * std::max (0.01, octaves)));
  44. }
  45. // --- Fruity Parametric EQ 2 (REA_rodada7 §1) ------------------------------------------------
  46. bool parametricEq2 (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  47. {
  48. if (d.size() < 4 + 36 * 4)
  49. return false;
  50. const auto version = i32At (d, 0);
  51. const auto mask = version >= 4 && d.size() >= 0x142 ? (std::uint32_t) i32At (d, 0x13E) : 0x7Fu;
  52. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  53. for (int b = 0; b < peq::bands; ++b)
  54. {
  55. const auto gainDb = std::clamp (param (b) / 100.0, -18.0, 18.0);
  56. const auto hz = 20.0 * std::pow (1000.0, std::clamp (param (7 + b), 0, 65536) / 65536.0);
  57. const auto w = std::clamp (param (14 + b), 0, 65536) / 65536.0;
  58. const auto type = param (21 + b);
  59. const auto order = std::clamp (param (28 + b), -3, 3);
  60. const auto on = (mask & (1u << b)) != 0;
  61. peq::BandType t = peq::BandType::off;
  62. double q = 1.0;
  63. switch (on ? type : 0)
  64. {
  65. case 1: t = peq::BandType::highCut; q = 0.1 + 9.9 * std::pow (1.0 - w, 3.0); break;
  66. case 2: t = peq::BandType::bandPass; q = qFromOctaves ((0.1 + 4.9 * w) * 0.708 * std::sqrt (std::abs (order) + 1.0)); break;
  67. case 3: t = peq::BandType::lowCut; q = 0.1 + 9.9 * std::pow (1.0 - w, 3.0); break;
  68. case 4: t = peq::BandType::notch; q = qFromOctaves ((0.1 + 4.9 * w) / (0.708 * std::sqrt (std::abs (order) + 1.0))); break;
  69. case 5: t = peq::BandType::lowShelf; q = 1.0 / 3.0 + 1.7193 * (1.0 - w); break; // Q = inclinação S
  70. case 6: t = peq::BandType::peak; q = qFromOctaves (0.1 + 4.9 * w); break;
  71. case 7: t = peq::BandType::highShelf; q = 1.0 / 3.0 + 1.7193 * (1.0 - w); break;
  72. default: break;
  73. }
  74. slot.params[(std::size_t) peq::param (b, 0)] = peq::typeValue (t);
  75. slot.params[(std::size_t) peq::param (b, 1)] = peq::hzValue (hz);
  76. slot.params[(std::size_t) peq::param (b, 2)] = peq::gainValue (gainDb);
  77. slot.params[(std::size_t) peq::param (b, 3)] = peq::qValue (q);
  78. // ordem: |ordem| + 1 biquads (os cortes "Steep 8" = 4)
  79. slot.params[(std::size_t) (peq::slopeParam + b)] = peq::slopeValue (std::abs (order) + 1);
  80. }
  81. slot.params[peq::outputParam] = peq::gainValue (std::clamp (param (35) / 100.0, -18.0, 18.0));
  82. return true;
  83. }
  84. // --- Fruity Limiter (§2): limiter, ou o compressor dele quando só o compressor trabalha -------
  85. bool limiter (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  86. {
  87. if (d.size() < 4 + 18 * 4)
  88. return false;
  89. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  90. auto timeMs = [] (int v) { return 2.0 * (std::pow (501.0, std::clamp (v, 0, 10000) / 10000.0) - 1.0); };
  91. const auto gainDb = toDb (curve (7.0, param (0) / 1000.0));
  92. const auto ceilingDb = toDb (curve (3.0, param (2) / 1000.0));
  93. const auto compThresholdDb = toDb (curve (7.0, param (8) / 1000.0));
  94. const auto ratioRaw = param (9);
  95. const auto ratio = ratioRaw > 0 ? 1.0 + 0.38 * (std::pow (51.0, ratioRaw / 1000.0) - 1.0) : 1.0;
  96. // teto acima de 0 dB praticamente desliga o limitador; aí o que soa é o compressor
  97. if (ceilingDb > -0.1 && ratio > 1.05 && compThresholdDb < -0.5)
  98. {
  99. slot = makeEffectSlot (EffectKind::compressor);
  100. slot.params[0] = clamp01 ((compThresholdDb + 60.0) / 60.0);
  101. slot.params[1] = clamp01 ((std::min (ratio, 20.0) - 1.0) / 19.0);
  102. slot.params[2] = fromLog (std::max (0.1, timeMs (param (11))), 0.1, 100.0);
  103. slot.params[3] = fromLog (2.0 * timeMs (param (12)), 10.0, 1000.0);
  104. slot.params[4] = clamp01 (std::max (0.0, gainDb) / 24.0);
  105. return true;
  106. }
  107. slot = makeEffectSlot (EffectKind::limiter);
  108. slot.params[0] = clamp01 ((std::min (ceilingDb, 0.0) + 12.0) / 12.0);
  109. slot.params[1] = clamp01 (std::max (0.0, gainDb) / 24.0);
  110. slot.params[2] = fromLog (2.0 * timeMs (param (5)), 10.0, 1000.0);
  111. return true;
  112. }
  113. // --- Fruity Reeverb 2 (§3) --------------------------------------------------------------------
  114. bool reeverb2 (const std::vector<std::uint8_t>& d, EffectSlot& slot, double bpm)
  115. {
  116. const auto version = i32At (d, 0);
  117. const auto count = version == 10001 ? 15 : version == 10000 ? 13 : 0;
  118. if (count == 0 || d.size() < 4 + (std::size_t) count * 4)
  119. return false;
  120. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  121. const auto decay = std::clamp (param (5) / 10.0, 0.1, 20.0);
  122. const auto dampingKhz = param (6) >= 221 ? 22.0 : std::clamp (param (6) / 10.0, 0.5, 22.0);
  123. const auto separation = std::clamp (param (9) / 64.0, -1.0, 1.0);
  124. const auto dry = curve (11.0, param (10) / 128.0);
  125. const auto wet = curve (11.0, param (12) / 256.0);
  126. // o Freeverb do LYNX: realimentação dos combs para o decaimento pedido (combs de ~30 ms)
  127. const auto feedback = std::pow (10.0, -3.0 * 0.03 / decay);
  128. slot.params[0] = clamp01 ((feedback - 0.7) / 0.28);
  129. slot.params[1] = clamp01 (1.0 - std::log (dampingKhz / 0.5) / std::log (22.0 / 0.5)); // abafa mais com o corte mais baixo
  130. slot.params[2] = clamp01 (1.0 + std::min (0.0, separation)); // separação negativa estreita
  131. slot.params[3] = clamp01 (wet / 0.25);
  132. slot.params[4] = clamp01 (dry);
  133. // corte de graves (< 20 Hz = desligado), de agudos (>= 22,1 kHz = desligado) e predelay
  134. const auto lowCutHz = param (0);
  135. slot.params[5] = lowCutHz < 20 ? 0.0f : std::max (0.002f, fromLog (lowCutHz, 20.0, 3000.0));
  136. slot.params[6] = param (1) >= 221 ? 1.0f : std::min (0.998f, fromLog (param (1) * 100.0, 500.0, 22000.0));
  137. const auto tempoPredelay = d.size() > 4 + (std::size_t) count * 4 && d[4 + (std::size_t) count * 4] != 0;
  138. const auto predelayMs = tempoPredelay ? param (2) / 48.0 * 60000.0 / std::max (1.0, bpm) : param (2) * 1000.0 / 384.0;
  139. slot.params[7] = clamp01 (predelayMs / 1000.0);
  140. return true;
  141. }
  142. // --- Fruity Delay 2 e Delay 3 (§4) ---------------------------------------------------------------
  143. float delayTimeValue (double beats)
  144. {
  145. // a divisão do delay interno mais próxima (0,25 … 4 tempos, 8 divisões)
  146. constexpr double divisions[] = { 0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 3.0, 4.0 };
  147. int best = 0;
  148. for (int i = 1; i < 8; ++i)
  149. if (std::abs (std::log (divisions[i] / beats)) < std::abs (std::log (divisions[best] / beats)))
  150. best = i;
  151. return ((float) best + 0.5f) / 8.0f;
  152. }
  153. bool delay2 (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  154. {
  155. if (d.size() < 32)
  156. return false;
  157. slot.params[0] = delayTimeValue (std::clamp (i32At (d, 0x10), 1, 768) / 48.0);
  158. slot.params[1] = clamp01 (i32At (d, 0x0C) / 127.0 * 0.9);
  159. slot.params[2] = clamp01 (curve (11.0, i32At (d, 0x04) / 128.0) * 0.5);
  160. slot.params[3] = i32At (d, 0x18) == 2 ? 1.0f : 0.0f;
  161. slot.params[4] = clamp01 (1.0 - i32At (d, 0x1C) / 128.0);
  162. return true;
  163. }
  164. bool delay3 (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  165. {
  166. if (d.size() < 4 + 26 * 4)
  167. return false;
  168. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  169. const auto synced = param (2) != 0;
  170. // sincronizado: ticks de 1/96 de tempo; sem sincronia a unidade não é conhecida: fica 1/4
  171. slot.params[0] = delayTimeValue (synced ? std::clamp (param (4), 1, 768) / 96.0 : 1.0);
  172. slot.params[1] = clamp01 (param (14) / 6000.0 * 0.9);
  173. slot.params[2] = clamp01 (param (23) / 6000.0 * 0.5);
  174. slot.params[3] = param (1) == 2 ? 1.0f : 0.0f;
  175. slot.params[4] = clamp01 (1.0 - param (16) / 6000.0);
  176. return true;
  177. }
  178. // --- Fruity Compressor (§5) ----------------------------------------------------------------------
  179. bool compressor (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  180. {
  181. if (d.size() < 4 + 6 * 4)
  182. return false;
  183. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  184. slot.params[0] = clamp01 ((std::clamp (param (0) / 10.0, -60.0, 0.0) + 60.0) / 60.0);
  185. slot.params[1] = clamp01 ((std::clamp (param (1) / 10.0, 1.0, 20.0) - 1.0) / 19.0);
  186. slot.params[4] = clamp01 (std::clamp (param (2) / 10.0, 0.0, 24.0) / 24.0);
  187. slot.params[2] = fromLog (std::max (0.1, param (3) / 10.0), 0.1, 100.0);
  188. slot.params[3] = fromLog (std::max (10.0, (double) param (4)), 10.0, 1000.0);
  189. return true;
  190. }
  191. // --- REA_rodada8: Maximus, Soundgoodizer, Fast Dist, Fruity Filter, Flangus, Soft Clipper -------
  192. double timeMs (int v) // T(x) = 2·(501^x − 1) ms
  193. {
  194. return 2.0 * (std::pow (501.0, std::clamp (v, 0, 10000) / 10000.0) - 1.0);
  195. }
  196. float compressorAttackValue (double ms) { return fromLog (std::max (0.1, ms), 0.1, 100.0); }
  197. float compressorReleaseValue (double ms) { return fromLog (std::max (10.0, ms), 10.0, 1000.0); }
  198. float limiterReleaseValue (double ms) { return fromLog (std::max (10.0, ms), 10.0, 1000.0); }
  199. float utilityGainValue (double gain);
  200. bool maximus (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  201. {
  202. if (d.size() < 4 + 59 * 4)
  203. return false;
  204. const auto header = (std::uint32_t) i32At (d, 0);
  205. const auto version = header > 0xFFFFu ? (int) (header >> 16) : (int) header;
  206. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  207. // a seção master (banda 3) representa o efeito; as bandas são multibanda (o LYNX não tem)
  208. constexpr int master = 7 + 13 * 3;
  209. const auto mode = param (master);
  210. const auto preDb = toDb (curve (7.0, param (master + 1) / 1000.0));
  211. const auto postDb = toDb (curve (7.0, param (master + 2) / 1000.0));
  212. if (mode == 3) // master desligado: passa direto (utilitário transparente mantém a posição do slot)
  213. {
  214. slot = makeEffectSlot (EffectKind::utility);
  215. return true;
  216. }
  217. if (mode != 0) // sem compressão: só os ganhos
  218. {
  219. slot = makeEffectSlot (EffectKind::utility);
  220. slot.params[0] = utilityGainValue (std::pow (10.0, (preDb + postDb) / 20.0));
  221. return true;
  222. }
  223. // curva de compressão do master: 7º envelope (low, 2º, mid, 2º, high, 2º, master, 2º)
  224. std::vector<std::pair<double, double>> points;
  225. std::size_t pos = version >= 15 ? 0x21F : version >= 14 ? 0x21B : 0x1FE;
  226. for (int env = 0; env < 8 && pos + 12 <= d.size(); ++env)
  227. {
  228. const auto count = (std::size_t) std::max (0, i32At (d, pos + 8));
  229. if (count > 512 || pos + 12 + 24 * count + 20 > d.size())
  230. break;
  231. if (env == 6)
  232. {
  233. double x = 0.0;
  234. for (std::size_t k = 0; k < count; ++k)
  235. {
  236. double dx = 0.0, y = 0.0;
  237. std::memcpy (&dx, d.data() + pos + 12 + 24 * k, 8);
  238. std::memcpy (&y, d.data() + pos + 12 + 24 * k + 8, 8);
  239. x += dx;
  240. if (std::isfinite (x) && std::isfinite (y))
  241. points.push_back ({ std::clamp (x, 0.0, 1.0), std::clamp (y, 0.0, 1.0) });
  242. }
  243. }
  244. pos += 12 + 24 * count + 20;
  245. }
  246. if (points.size() < 2)
  247. points = { { 0.0, 0.0 }, { 0.5, 0.5 }, { 1.0, 0.5 } }; // a curva padrão: limitador em −0,22 dB
  248. auto curveAt = [&] (double c) {
  249. if (c <= points.front().first)
  250. return points.front().second;
  251. for (std::size_t k = 1; k < points.size(); ++k)
  252. if (c <= points[k].first)
  253. {
  254. const auto span = points[k].first - points[k - 1].first;
  255. const auto t = span > 0.0 ? (c - points[k - 1].first) / span : 1.0;
  256. return points[k - 1].second + t * (points[k].second - points[k - 1].second);
  257. }
  258. return points.back().second;
  259. };
  260. auto levelDb = [] (double c, double scale) { return toDb ((std::pow (3.0, 2.0 * c) - 1.0) / 2.0 * scale); };
  261. // teto, joelho e ratio da curva (nível de entrada c: 0,5 = 0 dBFS, 1 = +12 dB)
  262. double ceilingDb = -120.0, kneeDb = 0.0, kneeOut = 0.0;
  263. bool knee = false;
  264. for (int k = 1; k <= 400; ++k)
  265. {
  266. const auto c = k / 400.0;
  267. const auto in = levelDb (c, 1.0), out = levelDb (curveAt (c), 0.975);
  268. ceilingDb = std::max (ceilingDb, out);
  269. if (! knee && out < in - 0.5)
  270. {
  271. knee = true;
  272. kneeDb = in;
  273. kneeOut = out;
  274. }
  275. }
  276. const auto topIn = levelDb (1.0, 1.0), topOut = levelDb (curveAt (1.0), 0.975);
  277. const auto ratio = knee && topOut - kneeOut > 0.05 ? (topIn - kneeDb) / (topOut - kneeOut) : 100.0;
  278. if (knee && kneeDb < -0.5 && ratio < 8.0) // compressão de verdade (joelho abaixo de 0 dB)
  279. {
  280. slot = makeEffectSlot (EffectKind::compressor);
  281. slot.params[0] = clamp01 ((kneeDb + 60.0) / 60.0);
  282. slot.params[1] = clamp01 ((std::clamp (ratio, 1.0, 20.0) - 1.0) / 19.0);
  283. slot.params[2] = compressorAttackValue (timeMs (param (master + 6)));
  284. slot.params[3] = compressorReleaseValue (2.0 * timeMs (param (master + 9)));
  285. slot.params[4] = clamp01 (std::max (0.0, preDb + postDb) / 24.0);
  286. return true;
  287. }
  288. slot = makeEffectSlot (EffectKind::limiter);
  289. slot.params[0] = clamp01 ((std::min (ceilingDb + postDb, 0.0) + 12.0) / 12.0);
  290. slot.params[1] = clamp01 (std::max (0.0, preDb) / 24.0);
  291. slot.params[2] = limiterReleaseValue (2.0 * timeMs (param (master + 9)));
  292. return true;
  293. }
  294. bool soundgoodizer (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  295. {
  296. if (d.size() < 12)
  297. return false;
  298. // preset A–D (cópias completas do Maximus); aproximação de compressor medida com amount 0,6
  299. struct Preset { double threshold, ratio, makeup; };
  300. 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 } };
  301. const auto& p = presets[std::clamp (i32At (d, 4), 0, 3)];
  302. const auto amount = std::clamp (i32At (d, 8), 0, 1000) / 1000.0;
  303. const auto scale = amount / 0.6;
  304. slot = makeEffectSlot (EffectKind::compressor);
  305. slot.params[0] = clamp01 ((p.threshold + 60.0) / 60.0);
  306. slot.params[1] = clamp01 ((1.0 + (p.ratio - 1.0) * scale - 1.0) / 19.0);
  307. slot.params[2] = compressorAttackValue (2.0);
  308. slot.params[3] = compressorReleaseValue (85.0);
  309. slot.params[4] = clamp01 (p.makeup * scale / 24.0);
  310. return true;
  311. }
  312. bool fastDist (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  313. {
  314. if (d.size() < 20)
  315. return false;
  316. const auto pre = curve (11.0, std::clamp (i32At (d, 0), 64, 192) / 128.0);
  317. const auto t = std::clamp (i32At (d, 4), 1, 10);
  318. const auto typeB = i32At (d, 8) != 0;
  319. const auto mix = curve (11.0, std::clamp (i32At (d, 12), 0, 128) / 128.0);
  320. const auto post = curve (11.0, std::clamp (i32At (d, 16), 0, 128) / 128.0);
  321. // drive equivalente do tanh para a curva do FL (ajuste da REA_rodada8 §3.2)
  322. constexpr double driveA[] = { 0, 0, 0, 0, 2.2, 4.5, 6.8, 9.2, 12.8, 17.8, 22.8 };
  323. const auto curveDb = typeB ? std::max (0.0, 0.8 + (t - 6) * 0.85) * (t >= 6 ? 1.0 : 0.0) : driveA[t];
  324. const auto wet = mix * post;
  325. const auto dry = (post - wet) * pre * post; // o post entra ao quadrado no seco (assim no FL)
  326. const auto level = typeB ? 1.0 : std::pow (10.0, -0.9 * (t - 1) / 20.0);
  327. const auto total = wet * level + dry;
  328. slot.params[0] = clamp01 ((toDb (pre) + curveDb) / 36.0);
  329. slot.params[1] = 1.0f; // o Fast Dist não filtra
  330. slot.params[2] = total > 1.0e-6 ? clamp01 (wet * level / total) : 1.0f;
  331. slot.params[3] = clamp01 (0.5 + toDb (std::max (total, 1.0e-6)) / 24.0);
  332. return true;
  333. }
  334. bool fruityFilter (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  335. {
  336. if (d.size() < 4 + 6 * 4)
  337. return false;
  338. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  339. const auto lp = curve (11.0, std::clamp (param (2), 0, 1024) / 1024.0);
  340. const auto bp = curve (11.0, std::clamp (param (3), 0, 1024) / 1024.0);
  341. const auto hp = curve (11.0, std::clamp (param (4), 0, 1024) / 1024.0);
  342. // tipo do filtro interno: 0 passa-baixas, 1 passa-altas, 2 passa-banda (floor(3v))
  343. auto type = 0.0f;
  344. if (hp > lp && hp >= bp && ! (lp >= 0.5 && hp >= 0.5))
  345. type = 0.4f;
  346. else if (bp > lp && bp > hp)
  347. type = 0.7f;
  348. // SVF de Chamberlin sem correção de taxa: "aberto" ainda corta em ~7,35 kHz (a 44,1 kHz)
  349. const auto f = std::min (1.0, (std::pow (26.0, std::clamp (param (0), 0, 1024) / 1024.0) - 1.0) * 0.04);
  350. const auto hz = std::max (20.0, 44100.0 / 3.141592653589793 * std::asin (f / 2.0));
  351. auto q = 1.0 / (1.0 - 0.8 * std::clamp (param (1), 0, 1024) / 1024.0);
  352. if (param (5) != 0)
  353. q *= 1.3; // x2: segundo estágio em série
  354. slot.params[0] = type;
  355. slot.params[1] = fromLog (hz, 20.0, 20000.0);
  356. slot.params[2] = fromLog (q, 0.5, 12.0);
  357. slot.params[3] = 1.0f;
  358. return true;
  359. }
  360. bool flangus (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  361. {
  362. if (d.size() < 32)
  363. return false;
  364. auto param = [&] (int i) { return i32At (d, 4 * (std::size_t) i); };
  365. const auto depthMs = 24.8 * curve (11.0, std::clamp (param (1), 0, 1024) / 1024.0);
  366. const auto voiceDepth = depthMs * 44.1 + 10.0; // amostras a 44,1 kHz
  367. const auto period = 75000.0 * (std::pow (101.0, 1.0 - std::clamp (param (2), 0, 1024) / 1024.0) - 1.0) + 50000.0;
  368. const auto hz = 250.0 * 44100.0 / (period * voiceDepth);
  369. const auto dryGain = curve (11.0, std::abs (std::clamp (param (6), -128, 128)) / 128.0);
  370. const auto wetGain = curve (11.0, std::abs (std::clamp (param (7), -128, 128)) / 128.0);
  371. const auto r = dryGain > 1.0e-6 ? wetGain / dryGain : 1000.0;
  372. slot.params[0] = fromLog (std::max (0.05, hz), 0.05, 5.0);
  373. slot.params[1] = clamp01 (2.0 * depthMs / 8.0);
  374. slot.params[2] = clamp01 (2.0 * r / (1.0 + r));
  375. slot.params[3] = 0.0f; // o Flangus não realimenta
  376. return true;
  377. }
  378. bool softClipper (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  379. {
  380. if (d.size() < 8)
  381. return false;
  382. const auto t = std::clamp (i32At (d, 0), 1, 127);
  383. const auto postDb = toDb (curve (11.0, std::clamp (i32At (d, 4), 0, 160) / 128.0));
  384. 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; // queda de nível da curva
  385. slot.params[0] = 0.0f;
  386. slot.params[1] = 1.0f;
  387. slot.params[2] = 1.0f;
  388. slot.params[3] = clamp01 (0.5 + (postDb - drop) / 24.0);
  389. return true;
  390. }
  391. // --- Balance, PanOMatic, Stereo Shaper (§6) -> Utilitário ---------------------------------------
  392. float utilityGainValue (double gain) { return clamp01 (toDb (gain) / 48.0 + 0.5); }
  393. float utilityMatrixValue (double m) { return clamp01 ((m + 2.0) / 4.0); }
  394. bool balance (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  395. {
  396. if (d.size() < 8)
  397. return false;
  398. slot.params[1] = clamp01 (std::clamp (i32At (d, 0), -128, 128) / 256.0 + 0.5);
  399. slot.params[0] = utilityGainValue (curve (11.0, i32At (d, 4) / 256.0)); // 256 = 0 dB
  400. return true;
  401. }
  402. bool panOMatic (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  403. {
  404. if (d.size() < 8)
  405. return false;
  406. slot.params[1] = clamp01 (std::clamp (i32At (d, 0), 0, 256) / 256.0); // 128 = centro
  407. slot.params[0] = utilityGainValue (curve (11.0, i32At (d, 4) / 256.0));
  408. return true; // o LFO do PanOMatic (ligado em poucos demos) fica de fora
  409. }
  410. bool stereoShaper (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  411. {
  412. if (d.size() < 4 + 4 * 4)
  413. return false;
  414. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i) / 12800.0; }; // 12800 = 1,0
  415. slot.params[3] = utilityMatrixValue (param (0)); // R -> L
  416. slot.params[2] = utilityMatrixValue (param (1)); // L
  417. slot.params[5] = utilityMatrixValue (param (2)); // R
  418. slot.params[4] = utilityMatrixValue (param (3)); // L -> R
  419. return true;
  420. }
  421. // --- REA_rodada14: Gross Beat (volume), Frequency Splitter, Effector ----------------------------
  422. // Gross Beat: o slot de volume escolhido (envelope de 4 tempos) vira os 32 passos do gate; o
  423. // "time travel" não tem equivalente. Sem estado legível, o gate fica transparente.
  424. bool grossBeat (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  425. {
  426. for (int i = 0; i < 32; ++i)
  427. slot.params[(std::size_t) i] = 1.0f;
  428. if (d.size() < 64)
  429. return false;
  430. auto version = (std::uint32_t) i32At (d, 0);
  431. if (version > 0xffff)
  432. version >>= 16;
  433. if (version < 3 || version > 9)
  434. return false;
  435. const auto paramCount = 9;
  436. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  437. std::size_t pos = 4 + 4 * paramCount + (version >= 5 ? 9 : 8);
  438. const auto volumeSlot = 36 + std::clamp (param (1), 0, 35);
  439. // os 72 slots (36 de tempo + 36 de volume): nome, 15 B de sincronismo, envelope
  440. std::vector<std::pair<double, double>> chosen; // (x acumulado em tempos, y)
  441. std::vector<int> curves;
  442. for (int s = 0; s < 72; ++s)
  443. {
  444. if (pos >= d.size())
  445. return false;
  446. pos += 1 + d[pos] + 15;
  447. if (pos + 12 > d.size())
  448. return false;
  449. const auto n = i32At (d, pos + 8);
  450. if (n < 0 || n > 5000 || pos + 12 + (std::size_t) n * 24 + 20 > d.size())
  451. return false;
  452. pos += 12;
  453. double x = 0.0;
  454. for (int k = 0; k < n; ++k, pos += 24)
  455. if (s == volumeSlot)
  456. {
  457. double dx = 0.0, y = 0.0;
  458. std::memcpy (&dx, d.data() + pos, 8);
  459. std::memcpy (&y, d.data() + pos + 8, 8);
  460. x += std::isfinite (dx) ? std::max (0.0, dx) : 0.0;
  461. chosen.emplace_back (x, std::isfinite (y) ? std::clamp (y, 0.0, 1.0) : 1.0);
  462. curves.push_back (d[pos + 20]);
  463. }
  464. pos += 20;
  465. }
  466. if (chosen.empty())
  467. return true;
  468. // valor no meio de cada passo; "hold" (2) segura o ponto anterior, o resto vira reta
  469. for (int i = 0; i < 32; ++i)
  470. {
  471. const auto t = (i + 0.5) / 32.0 * 4.0;
  472. auto value = chosen.back().second;
  473. if (t <= chosen.front().first)
  474. value = chosen.front().second;
  475. else
  476. for (std::size_t k = 1; k < chosen.size(); ++k)
  477. if (t < chosen[k].first)
  478. {
  479. const auto [x0, y0] = chosen[k - 1];
  480. const auto [x1, y1] = chosen[k];
  481. value = curves[k] == 2 || x1 <= x0 ? y0 : y0 + (y1 - y0) * (t - x0) / (x1 - x0);
  482. break;
  483. }
  484. slot.params[(std::size_t) i] = (float) value;
  485. }
  486. slot.params[32] = clamp01 (param (3) / 256.0);
  487. slot.params[33] = clamp01 (param (4) / 10000.0);
  488. slot.params[34] = clamp01 (param (5) / 10000.0);
  489. return true;
  490. }
  491. // Frequency Splitter: a saída principal com nível por banda vira prateleiras no EQ paramétrico
  492. bool frequencySplitter (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  493. {
  494. if (d.size() < 15 * 4)
  495. return false;
  496. auto word = [&] (int i) { return i32At (d, 4 * (std::size_t) i); };
  497. auto hz = [] (int v) { return 30.0 * std::pow (16000.0 / 30.0, std::clamp (v, 0, 65536) / 65536.0); };
  498. auto db = [] (int v) { return std::clamp (v / 100.0, -18.0, 18.0); };
  499. const auto threeBands = word (6) != 0;
  500. const auto low = db (word (12)), mid = db (word (13)), high = db (word (14));
  501. for (int b = 0; b < peq::bands; ++b)
  502. slot.params[(std::size_t) peq::param (b, 0)] = peq::typeValue (peq::BandType::off);
  503. auto shelf = [&] (int band, peq::BandType type, double f, double gain) {
  504. slot.params[(std::size_t) peq::param (band, 0)] = peq::typeValue (type);
  505. slot.params[(std::size_t) peq::param (band, 1)] = peq::hzValue (f);
  506. slot.params[(std::size_t) peq::param (band, 2)] = peq::gainValue (gain);
  507. slot.params[(std::size_t) peq::param (band, 3)] = peq::qValue (1.0);
  508. };
  509. if (threeBands)
  510. {
  511. shelf (0, peq::BandType::lowShelf, hz (word (1)), low - mid);
  512. shelf (6, peq::BandType::highShelf, hz (word (2)), high - mid);
  513. slot.params[peq::outputParam] = peq::gainValue (mid);
  514. }
  515. else
  516. {
  517. shelf (0, peq::BandType::lowShelf, hz (word (1)), low - mid);
  518. slot.params[peq::outputParam] = peq::gainValue (mid);
  519. }
  520. return true;
  521. }
  522. // Peak Controller (§7.3): base, quantidade, tensão e velocidade de queda
  523. bool peakController (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  524. {
  525. if (d.size() < 40)
  526. return false;
  527. slot.params[0] = clamp01 (i32At (d, 0) / 65536.0);
  528. slot.params[1] = clamp01 ((std::clamp (i32At (d, 4), -256, 256) / 256.0 + 1.0) / 2.0);
  529. slot.params[2] = clamp01 ((std::clamp (i32At (d, 8), -128, 128) / 128.0 + 1.0) / 2.0);
  530. slot.params[3] = clamp01 ((std::clamp (i32At (d, 12), 5, 128) - 5) / 123.0);
  531. slot.params[4] = clamp01 (i32At (d, 16) / 65536.0); // base do LFO
  532. return true;
  533. }
  534. // --- REA_rodada15: Pitch Shifter, Frequency Shifter, Emphasis, Low Lifter, Send, Transporter -----
  535. bool pitchShifter (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  536. {
  537. if (d.size() < 4 + 13 * 4)
  538. return false;
  539. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  540. const auto cents = std::clamp (param (0), -1200, 1200);
  541. const auto semis = (int) std::lround (cents / 100.0);
  542. slot.params[0] = clamp01 (0.5 + semis / 48.0);
  543. slot.params[1] = clamp01 (0.5 + (cents - semis * 100) / 200.0);
  544. slot.params[2] = clamp01 (std::clamp (param (3), 0, 2400) / 2400.0);
  545. const auto granular = d.size() >= 65 && i32At (d, 61) == 1;
  546. const auto grainMs = granular ? 20.0 + 130.0 * (std::clamp (param (4), 2000, 15000) - 2000) / 13000.0 : 60.0;
  547. slot.params[3] = clamp01 ((grainMs - 20.0) / 100.0);
  548. return true;
  549. }
  550. bool frequencyShifter (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  551. {
  552. if (d.size() < 4 + 10 * 4)
  553. return false;
  554. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  555. const auto s = std::clamp (param (2), -70000, 70000) / 70000.0;
  556. const auto k = param (8) == 0 ? 17.6096 : 10.9658; // faixa 20 kHz / 200 Hz (a "Tempo" fica como 200 Hz)
  557. 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));
  558. // a nossa escala: 2000·x·|x| Hz
  559. 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);
  560. slot.params[0] = clamp01 (0.5 + x / 2.0);
  561. slot.params[1] = clamp01 (std::clamp (param (0), 0, 10000) / 10000.0);
  562. slot.params[2] = 0.0f;
  563. return true;
  564. }
  565. bool emphasis (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  566. {
  567. if (d.size() < 4 + 13 * 4)
  568. return false;
  569. auto x = [&] (int i) { return std::clamp (i32At (d, 4 + 4 * (std::size_t) i), 0, 10000) / 10000.0; };
  570. const auto ceilingDb = toDb (curve (11.0, x (9)));
  571. slot.params[0] = clamp01 ((std::min (ceilingDb, 0.0) + 12.0) / 12.0);
  572. slot.params[1] = clamp01 (x (6)); // ganho de entrada 24x dB = a nossa escala de 0..24 dB
  573. slot.enabled = i32At (d, 4) == 0;
  574. return true;
  575. }
  576. bool lowLifter (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  577. {
  578. if (d.size() < 4 + 15 * 4)
  579. return false;
  580. auto x = [&] (int i) { return std::clamp (i32At (d, 4 + 4 * (std::size_t) i), 0, 10000) / 10000.0; };
  581. auto hz = [] (double v) { return 20.0 + 100.0 * v * v; };
  582. const auto mix = x (1);
  583. const auto lowDb = std::clamp ((x (2) - 0.5) * 24.0, -18.0, 12.0);
  584. const auto highDb = std::clamp (100.0 * std::log10 (std::max (1e-3, x (9))), -18.0, 0.0);
  585. // o gerador de harmônicos (Chebyshev até a 3ª ordem, REA_rodada17 §13) não tem equivalente aqui; o EQ
  586. // reproduz só o corte de graves e os ganhos das bandas
  587. const auto boost = 0.0;
  588. for (int b = 0; b < peq::bands; ++b)
  589. slot.params[(std::size_t) peq::param (b, 0)] = peq::typeValue (peq::BandType::off);
  590. if (hz (x (11)) > 21.0)
  591. {
  592. slot.params[(std::size_t) peq::param (0, 0)] = peq::typeValue (peq::BandType::lowCut);
  593. slot.params[(std::size_t) peq::param (0, 1)] = peq::hzValue (hz (x (11)));
  594. slot.params[(std::size_t) peq::param (0, 3)] = peq::qValue (0.707);
  595. }
  596. slot.params[(std::size_t) peq::param (1, 0)] = peq::typeValue (peq::BandType::lowShelf);
  597. slot.params[(std::size_t) peq::param (1, 1)] = peq::hzValue (hz (x (10)));
  598. slot.params[(std::size_t) peq::param (1, 2)] = peq::gainValue (std::clamp ((lowDb - highDb) * mix + boost, -18.0, 18.0));
  599. slot.params[(std::size_t) peq::param (1, 3)] = peq::qValue (1.0);
  600. slot.params[peq::outputParam] = peq::gainValue (highDb * mix);
  601. slot.enabled = i32At (d, 4) != 0;
  602. return true;
  603. }
  604. bool fruitySend (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  605. {
  606. if (d.size() < 12)
  607. return false;
  608. slot.params[0] = utilityGainValue (std::clamp (i32At (d, 8), 0, 256) / 256.0); // o que segue na trilha
  609. slot.params[1] = 0.5f;
  610. return true;
  611. }
  612. // Squeeze: quantização (poucos bits) + filtro; aproximado por distorção leve, com o mix dele
  613. bool squeeze (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  614. {
  615. if (d.size() < 4 + 12 * 4)
  616. return false;
  617. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  618. const auto levels = std::pow (32768.0, 1.0 - std::clamp (param (2), 1, 32768) / 32768.0);
  619. slot.params[0] = clamp01 (0.6 * (1.0 - std::log2 (std::max (2.0, levels)) / 15.0)); // menos níveis, mais "sujeira"
  620. slot.params[1] = param (3) != 0 ? clamp01 (std::clamp (param (6), 0, 65536) / 65536.0) : 0.8f;
  621. slot.params[2] = 1.0f;
  622. slot.params[3] = 0.5f;
  623. slot.mix = clamp01 (std::clamp (param (1), 0, 128) / 128.0);
  624. return true;
  625. }
  626. bool transporter (const std::vector<std::uint8_t>& d, EffectSlot& slot)
  627. {
  628. if (d.size() < 8)
  629. return false;
  630. slot.params[0] = utilityGainValue (curve (11.0, std::clamp (i32At (d, 4), 0, 16000) / 12800.0)); // dry level
  631. return true;
  632. }
  633. // Effector: o efeito Effector do LYNX (core/Effector.h) recebe os 12 parâmetros crus do FL
  634. std::optional<EffectSlot> effector (const std::vector<std::uint8_t>& d, double)
  635. {
  636. // "SSLF" + tamanho + "10XF" + 64 floats reservados + os 12 parâmetros
  637. if (d.size() < 12 + 76 * 4 || std::memcmp (d.data(), "SSLF", 4) != 0)
  638. return std::nullopt;
  639. float p[12];
  640. std::memcpy (p, d.data() + 12 + 64 * 4, sizeof (p));
  641. auto slot = makeEffectSlot (EffectKind::effector);
  642. for (int i = 0; i < 12; ++i)
  643. slot.params[(std::size_t) i] = std::isfinite (p[i]) ? std::clamp (p[i], 0.0f, 1.0f) : 0.0f;
  644. slot.enabled = slot.params[1] == 0.0f; // bypass = qualquer valor ≠ 0
  645. slot.params[1] = 0.0f;
  646. slot.mix = 1.0f; // a mistura de cada algoritmo é feita dentro do efeito (Wet Mix = parâmetro 2)
  647. return slot;
  648. }
  649. } // namespace
  650. std::optional<EffectSlot> effectFromFl (const std::string& pluginName, const std::vector<std::uint8_t>& state, double bpm)
  651. {
  652. if (pluginName == "Effector")
  653. return effector (state, bpm);
  654. const auto kind = effectForFlPlugin (pluginName);
  655. if (! kind)
  656. return std::nullopt;
  657. auto slot = makeEffectSlot (*kind);
  658. // compara sem caixa (presets do FL 3: "Fruity delay 2")
  659. std::string lower = pluginName;
  660. std::transform (lower.begin(), lower.end(), lower.begin(), [] (unsigned char c) { return (char) std::tolower (c); });
  661. auto is = [&] (const char* canonical) {
  662. std::string other = canonical;
  663. std::transform (other.begin(), other.end(), other.begin(), [] (unsigned char c) { return (char) std::tolower (c); });
  664. return lower == other;
  665. };
  666. if (*kind == EffectKind::filter)
  667. slot.params[1] = 1.0f; // aberto: sem os ajustes do FL, melhor não abafar
  668. if (is ("Fruity Soft Clipper"))
  669. softClipper (state, slot);
  670. if (is ("Fruity Parametric EQ 2"))
  671. parametricEq2 (state, slot);
  672. else if (is ("Fruity Limiter"))
  673. limiter (state, slot);
  674. else if (is ("Fruity Reeverb 2"))
  675. reeverb2 (state, slot, bpm);
  676. else if (is ("Fruity Delay 2"))
  677. delay2 (state, slot);
  678. else if (is ("Fruity Delay 3"))
  679. delay3 (state, slot);
  680. else if (is ("Fruity Compressor"))
  681. compressor (state, slot);
  682. else if (is ("Fruity Balance"))
  683. balance (state, slot);
  684. else if (is ("Fruity PanOMatic"))
  685. panOMatic (state, slot);
  686. else if (is ("Fruity Stereo Shaper"))
  687. stereoShaper (state, slot);
  688. else if (is ("Maximus"))
  689. maximus (state, slot);
  690. else if (is ("Soundgoodizer"))
  691. soundgoodizer (state, slot);
  692. else if (is ("Fruity Fast Dist"))
  693. fastDist (state, slot);
  694. else if (is ("Fruity Filter"))
  695. fruityFilter (state, slot);
  696. else if (is ("Fruity Flangus"))
  697. flangus (state, slot);
  698. else if (is ("Gross Beat"))
  699. grossBeat (state, slot);
  700. else if (is ("Fruity Peak Controller"))
  701. peakController (state, slot);
  702. else if (is ("Pitch Shifter"))
  703. pitchShifter (state, slot);
  704. else if (is ("Frequency Shifter"))
  705. frequencyShifter (state, slot);
  706. else if (is ("Emphasis"))
  707. emphasis (state, slot);
  708. else if (is ("Low Lifter"))
  709. lowLifter (state, slot);
  710. else if (is ("Fruity Send"))
  711. fruitySend (state, slot);
  712. else if (is ("Transporter"))
  713. transporter (state, slot);
  714. else if (is ("Fruity Squeeze"))
  715. squeeze (state, slot);
  716. else if (is ("Frequency Splitter"))
  717. frequencySplitter (state, slot);
  718. else if (is ("Fruity Phase Inverter"))
  719. {
  720. slot.params[2] = utilityMatrixValue (-1.0); // inverte os dois lados
  721. slot.params[5] = utilityMatrixValue (-1.0);
  722. }
  723. return slot;
  724. }
  725. std::optional<EffectSlot> effectFromFlPreset (const std::vector<std::uint8_t>& bytes, double bpm)
  726. {
  727. try
  728. {
  729. // o preset de efeito tem o mesmo formato do de canal: nome do plugin (0xC9) e estado (0xD5)
  730. const auto project = flp::parseProject (bytes, true);
  731. if (project.channels.empty() || project.channels.front().pluginState.empty())
  732. return std::nullopt;
  733. return effectFromFl (project.channels.front().pluginName, project.channels.front().pluginState, bpm);
  734. }
  735. catch (const flp::Error&)
  736. {
  737. return std::nullopt;
  738. }
  739. }
  740. std::vector<std::string> flPluginsForEffect (EffectKind kind)
  741. {
  742. switch (kind)
  743. {
  744. case EffectKind::parametricEq: return { "Fruity Parametric EQ 2" };
  745. case EffectKind::limiter:
  746. case EffectKind::compressor: return { "Fruity Limiter", "Fruity Compressor" };
  747. case EffectKind::reverb: return { "Fruity Reeverb 2" };
  748. case EffectKind::delay: return { "Fruity Delay 3", "Fruity Delay 2" };
  749. case EffectKind::utility: return { "Fruity Stereo Shaper" };
  750. default: return {};
  751. }
  752. }
  753. std::optional<FlParamMap> effectParamFromFl (const std::string& pluginName, int k)
  754. {
  755. auto is = [&] (const char* n) { return pluginName == n; };
  756. if (is ("Fruity Parametric EQ 2"))
  757. {
  758. if (k >= 0 && k < 7) // ganho da banda: ±18 dB
  759. return FlParamMap { peq::param (k, 2), [] (float v) { return peq::gainValue (36.0 * v - 18.0); } };
  760. if (k >= 7 && k < 14) // frequência: 20 Hz · 1000^v
  761. return FlParamMap { peq::param (k - 7, 1), [] (float v) { return peq::hzValue (20.0 * std::pow (1000.0, (double) v)); } };
  762. if (k == 35)
  763. return FlParamMap { peq::outputParam, [] (float v) { return peq::gainValue (36.0 * v - 18.0); } };
  764. return std::nullopt;
  765. }
  766. if (is ("Fruity Filter"))
  767. {
  768. if (k == 0) // corte (SVF de Chamberlin, como no import do estado)
  769. return FlParamMap { 1, [] (float v) {
  770. const auto f = std::min (1.0, (std::pow (26.0, (double) v) - 1.0) * 0.04);
  771. return fromLog (std::max (20.0, 44100.0 / 3.141592653589793 * std::asin (f / 2.0)), 20.0, 20000.0);
  772. } };
  773. if (k == 1)
  774. return FlParamMap { 2, [] (float v) { return fromLog (1.0 / (1.0 - 0.8 * (double) v), 0.5, 12.0); } };
  775. return std::nullopt;
  776. }
  777. if (is ("Fruity Balance") || is ("Fruity PanOMatic"))
  778. {
  779. if (k == 0) // pan: a faixa inteira dos dois casa com a nossa
  780. return FlParamMap { 1, [] (float v) { return v; } };
  781. if (k == 1) // volume: 0..320 (256 = 0 dB) [HIP no máximo]
  782. return FlParamMap { 0, [] (float v) { return utilityGainValue (curve (11.0, 1.25 * (double) v)); } };
  783. return std::nullopt;
  784. }
  785. if (is ("Fruity Fast LP") && (k == 0 || k == 1)) // corte / ressonância: o filtro interno tem os padrões, segue o knob
  786. return FlParamMap { k + 1, [] (float v) { return v; } };
  787. if (is ("Fruity Reeverb 2"))
  788. {
  789. if (k == 5) // decay 0,1..20 s -> realimentação dos combs
  790. return FlParamMap { 0, [] (float v) {
  791. const auto decay = 0.1 + 19.9 * (double) v;
  792. return clamp01 ((std::pow (10.0, -3.0 * 0.03 / decay) - 0.7) / 0.28);
  793. } };
  794. if (k == 10)
  795. return FlParamMap { 4, [] (float v) { return clamp01 (curve (11.0, (double) v)); } };
  796. if (k == 12)
  797. return FlParamMap { 3, [] (float v) { return clamp01 (curve (11.0, (double) v) / 0.25); } };
  798. return std::nullopt;
  799. }
  800. if (is ("Pitch Shifter"))
  801. {
  802. if (k == 0) // afinação: −12..+12 st (em centésimos) -> os nossos ±24 st
  803. return FlParamMap { 0, [] (float v) { return (float) clamp01 (0.5 + std::round (24.0 * v - 12.0) / 48.0); } };
  804. if (k == 3)
  805. return FlParamMap { 2, [] (float v) { return v; } };
  806. return std::nullopt;
  807. }
  808. if (is ("Fruity Delay 2") && k == 1) // wet
  809. return FlParamMap { 2, [] (float v) { return clamp01 (curve (11.0, (double) v) * 0.5); } };
  810. if (is ("Fruity Delay 3"))
  811. {
  812. if (k == 14)
  813. return FlParamMap { 1, [] (float v) { return clamp01 (v * 0.9); } };
  814. if (k == 23)
  815. return FlParamMap { 2, [] (float v) { return clamp01 (v * 0.5); } };
  816. if (k == 16)
  817. return FlParamMap { 4, [] (float v) { return clamp01 (1.0 - v); } };
  818. }
  819. return std::nullopt;
  820. }
  821. std::optional<float> controllerConstantOutput (const std::string& pluginName, const std::vector<std::uint8_t>& d, int output)
  822. {
  823. if (pluginName == "Fruity X-Y Controller")
  824. {
  825. if (d.size() < 44 || output < 0 || output > 1)
  826. return std::nullopt;
  827. auto param = [&] (int i) { return std::clamp (i32At (d, 4 + 4 * (std::size_t) i), -65536, 65536) / 65536.0; };
  828. const auto axis = output * 4;
  829. const auto level = std::clamp (param (axis), 0.0, 1.0);
  830. // tensão (REA_rodada17 §4): k = 0,1·(1001^(|t|/128) − 1); t > 0 côncava, t < 0 convexa
  831. const auto t = std::clamp (i32At (d, 4 + 4 * (std::size_t) (axis + 3)), -128, 128);
  832. auto shaped = level;
  833. if (t != 0)
  834. {
  835. const auto k = 0.1 * (std::pow (1001.0, std::abs (t) / 128.0) - 1.0);
  836. 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;
  837. }
  838. return (float) std::clamp (param (axis + 1) + (param (axis + 2) - param (axis + 1)) * shaped, 0.0, 1.0);
  839. }
  840. if (pluginName == "Control Surface")
  841. {
  842. if (d.size() < 16)
  843. return std::nullopt;
  844. std::optional<float> found;
  845. // chunks u32 id + u64 tamanho (id 0 = grupo); 0x836 = valor de um controle e a saída publicada
  846. std::function<void (std::size_t, std::size_t)> walk = [&] (std::size_t pos, std::size_t end) {
  847. while (pos + 12 <= end && ! found)
  848. {
  849. std::uint32_t id = 0;
  850. std::uint64_t size = 0;
  851. std::memcpy (&id, d.data() + pos, 4);
  852. std::memcpy (&size, d.data() + pos + 4, 8);
  853. if (size > end - pos - 12)
  854. return;
  855. const auto body = pos + 12;
  856. if (id == 0)
  857. walk (body, body + (std::size_t) size);
  858. else if (id == 0x836 && size >= 12 && i32At (d, body + 8) == output)
  859. {
  860. float v = 0.0f;
  861. std::memcpy (&v, d.data() + body, 4);
  862. if (std::isfinite (v))
  863. found = std::clamp (v, 0.0f, 1.0f);
  864. }
  865. pos = body + (std::size_t) size;
  866. }
  867. };
  868. walk (4, d.size());
  869. return found;
  870. }
  871. return std::nullopt;
  872. }
  873. namespace
  874. {
  875. // curva de ganho dos knobs do FL: (11^x − 1)/10, com sinal
  876. double scurve11 (double x) { return (x < 0.0 ? -1.0 : 1.0) * (std::pow (11.0, std::abs (x)) - 1.0) / 10.0; }
  877. struct PhilterBand
  878. {
  879. peq::BandType type = peq::BandType::off;
  880. double hz = 1000.0, q = 0.707, gainDb = 0.0;
  881. int stages = 1;
  882. bool ok = false;
  883. std::vector<double> stageHz; // modos alternate: corte de cada estágio
  884. };
  885. // tipo, corte e Q de uma unidade com corte c e ressonância r (0..1), a 44,1 kHz (k = 1)
  886. PhilterBand philterBand (const LovePhilterUnit& u, double c, double r)
  887. {
  888. PhilterBand b;
  889. c = std::clamp (c, 0.0, 1.0);
  890. r = std::clamp (r, 0.0, 1.0);
  891. const auto mode = std::clamp (u.mode, 0, 5);
  892. if (mode == 0)
  893. return b; // a unidade não filtra
  894. b.stages = mode == 4 ? 2 : mode == 5 ? 3 : mode;
  895. auto lpQ = [] (double rr) { return 0.1 + 0.02495 * (std::pow (2001.0, rr) - 1.0); };
  896. const auto lowGain = scurve11 (std::clamp (u.params[3], 0, 128) / 128.0);
  897. const auto bandGain = scurve11 (std::clamp (u.params[4], 0, 128) / 128.0);
  898. const auto highGain = scurve11 (std::clamp (u.params[5], 0, 128) / 128.0);
  899. double x = 0.0;
  900. std::function<double (double)> hzOf;
  901. switch (u.type)
  902. {
  903. case 0: // SVF de Chamberlin: fc = (x + 0,005)·44100/π; saída = L·LP + B·BP + H·HP
  904. {
  905. x = (std::pow (51.0, c) - 1.0) * 0.0159;
  906. hzOf = [] (double v) { return (v + 0.005) * 44100.0 / 3.141592653589793; };
  907. b.hz = hzOf (x);
  908. auto d = r <= 0.0 ? 1.0 : r >= 1.0 ? 0.2 : 1.0 - 0.9 * r;
  909. if (u.alt)
  910. d *= d;
  911. b.q = 1.0 / std::max (0.05, d);
  912. const auto l = std::abs (lowGain) > 1e-6, bp = std::abs (bandGain) > 1e-6, h = std::abs (highGain) > 1e-6;
  913. // só as saídas puras têm equivalente: LP, BP, HP ou L+H (rejeita-faixa) com o mesmo ganho
  914. if (l && ! bp && ! h)
  915. b.type = peq::BandType::highCut, b.gainDb = 20.0 * std::log10 (std::abs (lowGain));
  916. else if (h && ! l && ! bp)
  917. b.type = peq::BandType::lowCut, b.gainDb = 20.0 * std::log10 (std::abs (highGain));
  918. else if (bp && ! l && ! h)
  919. b.type = peq::BandType::bandPass, b.gainDb = 20.0 * std::log10 (std::abs (bandGain));
  920. else if (l && h && ! bp && std::abs (lowGain - highGain) < 1e-6)
  921. b.type = peq::BandType::notch, b.gainDb = 20.0 * std::log10 (std::abs (lowGain));
  922. else
  923. return b;
  924. b.ok = true;
  925. break;
  926. }
  927. case 1: case 2: // Lime: ω = (x + 0,007)·π/2 -> fc = (x + 0,007)·44100/4
  928. {
  929. x = std::min (1.6, (std::pow (76.0, c) - 1.0) * 0.01324);
  930. hzOf = [] (double v) { return (v + 0.007) * 44100.0 / 4.0; };
  931. b.hz = hzOf (x);
  932. b.q = std::min (50.0, std::pow (1001.0, r));
  933. b.type = u.type == 1 ? peq::BandType::highCut : peq::BandType::lowCut; // o slot L é "drive" aqui: fica de fora
  934. b.ok = true;
  935. break;
  936. }
  937. case 3: // Vanilla LP: fc = (x + 0,003)·44100
  938. {
  939. x = (std::pow (1501.0, c) - 1.0) * 0.000664667;
  940. hzOf = [] (double v) { return (v + 0.003) * 44100.0; };
  941. b.hz = hzOf (x);
  942. b.q = lpQ (r);
  943. b.type = peq::BandType::highCut;
  944. b.ok = true;
  945. break;
  946. }
  947. case 6: case 8: case 9: case 10: // família Vanilla 4…10: fc = (x + 0,0001)·44100
  948. {
  949. x = std::min (1.0001, (std::pow (1501.0, c) - 1.0) * 0.00039993);
  950. if (u.type == 6)
  951. x = std::min (x, 0.6001);
  952. hzOf = [] (double v) { return (v + 0.0001) * 44100.0; };
  953. b.hz = hzOf (x);
  954. if (u.type == 6)
  955. {
  956. b.type = peq::BandType::lowCut;
  957. b.q = lpQ (r);
  958. }
  959. else if (u.type == 9)
  960. {
  961. b.type = peq::BandType::peak;
  962. b.q = 0.1 + 0.04995 * (std::pow (2001.0, 1.0 - r) - 1.0);
  963. b.gainDb = 20.0 * std::log10 (std::max (1e-6, std::abs (lowGain))); // o ganho do pico é o slot low/gain
  964. }
  965. else
  966. {
  967. b.type = u.type == 8 ? peq::BandType::lowShelf : peq::BandType::highShelf;
  968. b.q = 0.1 + 0.45 * (std::pow (3.0, 1.0 - r) - 1.0); // inclinação S
  969. b.gainDb = 20.0 * std::log10 (std::max (1e-6, std::abs (lowGain)));
  970. }
  971. b.ok = true;
  972. break;
  973. }
  974. case 4: case 5: // Vanilla passa-faixa / rejeita-faixa: largura em oitavas (REA_rodada19 §7.1)
  975. {
  976. x = std::min (1.0001, (std::pow (1501.0, c) - 1.0) * 0.00039993);
  977. hzOf = [] (double v) { return (v + 0.0001) * 44100.0; };
  978. b.hz = hzOf (x);
  979. const auto n = std::max (1, mode);
  980. auto bw = 0.01 + 0.1199 * (std::pow (101.0, u.alt ? r * (c + 1.0) * 0.5 : r) - 1.0);
  981. bw = u.type == 4 ? bw * n : bw / n; // por estágio
  982. const auto w = std::clamp (2.0 * 3.141592653589793 * b.hz / 44100.0, 1e-4, 2.8495);
  983. 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);
  984. b.type = u.type == 4 ? peq::BandType::bandPass : peq::BandType::notch;
  985. b.ok = true;
  986. if (b.stages > 1) // a largura já foi dividida por estágio: não refazer a raiz do Q abaixo
  987. {
  988. b.hz = std::clamp (b.hz, 20.0, 20000.0);
  989. return b;
  990. }
  991. break;
  992. }
  993. case 7: // passa-tudo: só muda a fase; sozinho na unidade, o módulo é 1 (REA_rodada19 §7.2)
  994. b.type = peq::BandType::off;
  995. b.ok = true;
  996. return b;
  997. case 11: // Mango: 4 polos com realimentação; o corte em Hz é inferido do polo (REA_rodada19 §7.3)
  998. {
  999. x = std::min (3.0, (std::pow (201.0, c) - 1.0) * 0.01495);
  1000. hzOf = [] (double v) {
  1001. const auto w = std::min ((v + 0.01) * 0.5, 0.65);
  1002. return 22050.0 * w * (1.873 * w * w * w + 0.4955 * w * w - 0.649 * w + 0.9988);
  1003. };
  1004. b.hz = hzOf (x);
  1005. b.q = lpQ (r);
  1006. b.type = peq::BandType::highCut;
  1007. b.stages = std::min (4, 2 * b.stages); // 4 polos (24 dB/oit) por estágio
  1008. b.ok = true;
  1009. break;
  1010. }
  1011. default:
  1012. return b;
  1013. }
  1014. if (b.stages > 1)
  1015. b.q = std::pow (b.q, 1.0 / b.stages);
  1016. // alternate (modos 4/5): a variável de frequência de cada estágio é x, x^E, x^(E²), E = 0,5 / 0,75
  1017. if ((mode == 4 || mode == 5) && hzOf)
  1018. {
  1019. const auto e = mode == 4 ? 0.5 : 0.75;
  1020. auto stageX = x;
  1021. for (int i = 0; i < b.stages; ++i)
  1022. {
  1023. b.stageHz.push_back (std::clamp (hzOf (stageX), 20.0, 20000.0));
  1024. stageX = std::pow (std::max (0.0, stageX), e);
  1025. }
  1026. }
  1027. b.hz = std::clamp (b.hz, 20.0, 20000.0);
  1028. return b;
  1029. }
  1030. EffectSlot philterSlot (const PhilterBand& band, double unitGain)
  1031. {
  1032. auto slot = makeEffectSlot (EffectKind::parametricEq);
  1033. for (int i = 0; i < peq::bands; ++i)
  1034. slot.params[(std::size_t) peq::param (i, 0)] = peq::typeValue (peq::BandType::off);
  1035. double outputDb = 20.0 * std::log10 (std::max (1e-6, unitGain));
  1036. if (band.ok)
  1037. {
  1038. const auto cut = band.type == peq::BandType::highCut || band.type == peq::BandType::lowCut;
  1039. const auto alternate = ! band.stageHz.empty();
  1040. // cortes: estágios = inclinação do EQ; os outros tipos (e os alternate) repetem a banda (cascata)
  1041. const auto copies = cut && ! alternate ? 1 : std::min (band.stages, peq::bands);
  1042. for (int i = 0; i < copies; ++i)
  1043. {
  1044. slot.params[(std::size_t) peq::param (i, 0)] = peq::typeValue (band.type);
  1045. slot.params[(std::size_t) peq::param (i, 1)] = peq::hzValue (alternate ? band.stageHz[(std::size_t) i] : band.hz);
  1046. slot.params[(std::size_t) peq::param (i, 2)] = peq::gainValue (std::clamp (band.gainDb, -18.0, 18.0));
  1047. slot.params[(std::size_t) peq::param (i, 3)] = peq::qValue (band.q);
  1048. slot.params[(std::size_t) (peq::slopeParam + i)] = peq::slopeValue (cut && ! alternate ? std::clamp (band.stages, 1, 4) : 1);
  1049. }
  1050. if (cut) // nos cortes o ganho do slot L/H multiplica a saída
  1051. outputDb += band.gainDb;
  1052. }
  1053. slot.params[peq::outputParam] = peq::gainValue (std::clamp (outputDb, -18.0, 18.0));
  1054. return slot;
  1055. }
  1056. } // namespace
  1057. std::optional<LovePhilterConversion> lovePhilterEffects (const std::vector<std::uint8_t>& stateBytes)
  1058. {
  1059. const auto state = parseLovePhilter (stateBytes);
  1060. if (! state)
  1061. return std::nullopt;
  1062. const auto master = scurve11 (std::clamp (state->globals[0], 0, 160) / 128.0);
  1063. std::vector<int> active;
  1064. for (int u = 0; u < 8; ++u)
  1065. if (state->units[(std::size_t) u].enabled && (state->soloUnit < 0 || state->soloUnit == u))
  1066. active.push_back (u);
  1067. LovePhilterConversion out;
  1068. // articulador de volume (multiplicativo, REA_rodada17 §15.3 + rodada18 §4): base × mapaX(x) × mapaY(y);
  1069. // envelope/LFO desligados são neutros (os ligados ficam de fora)
  1070. const auto modX = std::clamp (state->modX, 0, 1024) / 1024.0, modY = std::clamp (state->modY, 0, 1024) / 1024.0;
  1071. auto volumeFactor = [] (const LovePhilterUnit& unit, double x, double y) {
  1072. const auto& a = unit.articulators[1];
  1073. const auto base = std::isfinite (a.base) ? std::clamp ((double) a.base, 0.0, 4.0) : 1.0;
  1074. return std::max (0.0, base * lovePhilterMapAt (a.mapX, x) * lovePhilterMapAt (a.mapY, y));
  1075. };
  1076. auto mapsNeutral = [] (const LovePhilterMap& m) {
  1077. return std::all_of (m.points.begin(), m.points.end(), [] (const auto& pt) { return std::abs (pt.second - 1.0) < 1e-6; });
  1078. };
  1079. for (const auto u : active)
  1080. if (u + 1 < 8 && state->units[(std::size_t) u + 1].enabled && state->units[(std::size_t) u].params[11] != 0)
  1081. {
  1082. out.notes.push_back ("Love Philter: unidades encadeadas (NEXT) tocam em paralelo");
  1083. break;
  1084. }
  1085. auto unitEffects = [&] (int u) {
  1086. const auto& unit = state->units[(std::size_t) u];
  1087. const auto band = philterBand (unit, std::clamp (unit.params[1], 0, 256) / 256.0, std::clamp (unit.params[2], 0, 256) / 256.0);
  1088. if (unit.mode != 0 && ! band.ok)
  1089. out.notes.push_back ("Love Philter: filtro tipo " + std::to_string (unit.type) + " sem equivalente");
  1090. 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));
  1091. std::vector<EffectSlot> chain { philterSlot (band, gain) };
  1092. const auto& vol = unit.articulators[1];
  1093. if (! mapsNeutral (vol.mapX) || ! mapsNeutral (vol.mapY) || std::abs (vol.base - 1.0f) > 1e-6f)
  1094. {
  1095. auto utility = makeEffectSlot (EffectKind::utility); // volume controlado por Mod X/Y
  1096. utility.params[0] = utilityGainValue (volumeFactor (unit, modX, modY));
  1097. chain.push_back (utility);
  1098. }
  1099. if (const auto pan = std::clamp (unit.params[8], -128, 128) / 128.0; std::abs (pan) > 1e-3)
  1100. {
  1101. auto utility = makeEffectSlot (EffectKind::utility);
  1102. utility.params[1] = (float) clamp01 (pan * 0.5 + 0.5);
  1103. chain.push_back (utility);
  1104. }
  1105. return chain;
  1106. };
  1107. if (active.empty())
  1108. {
  1109. out.slots.push_back (philterSlot ({}, 0.0)); // nenhuma unidade ligada: o Love Philter fica mudo
  1110. return out;
  1111. }
  1112. if (active.size() == 1)
  1113. {
  1114. const auto u = active.front();
  1115. out.slots = unitEffects (u);
  1116. const auto& unit = state->units[(std::size_t) u];
  1117. const auto base = 8 + 44 * u;
  1118. const auto masterGain = master;
  1119. // Mod X (k = 6) / Mod Y (k = 7) movem o utilitário de volume, se houver
  1120. const auto modSlot = out.slots.size() >= 2 && out.slots[1].kind == EffectKind::utility && out.slots[1].params[1] == 0.5f ? 1 : -1;
  1121. out.paramMap = [unit, base, masterGain, modSlot, modX, modY, volumeFactor] (int k) -> std::optional<std::tuple<int, int, std::function<float (float)>>> {
  1122. if ((k == 6 || k == 7) && modSlot >= 0)
  1123. return std::make_tuple (modSlot, 0, std::function<float (float)> ([unit, k, modX, modY, volumeFactor] (float v) {
  1124. return utilityGainValue (k == 6 ? volumeFactor (unit, v, modY) : volumeFactor (unit, modX, v));
  1125. }));
  1126. if (k == base + 1) // CUT: c = valor
  1127. return std::make_tuple (0, peq::param (0, 1), std::function<float (float)> ([unit] (float v) {
  1128. const auto b = philterBand (unit, v, std::clamp (unit.params[2], 0, 256) / 256.0);
  1129. return peq::hzValue (b.hz);
  1130. }));
  1131. if (k == base + 2) // RES
  1132. return std::make_tuple (0, peq::param (0, 3), std::function<float (float)> ([unit] (float v) {
  1133. const auto b = philterBand (unit, std::clamp (unit.params[1], 0, 256) / 256.0, v);
  1134. return peq::qValue (b.q);
  1135. }));
  1136. if (k == base + 9) // VOL: 0..160
  1137. return std::make_tuple (0, peq::outputParam, std::function<float (float)> ([masterGain] (float v) {
  1138. const auto g = scurve11 (160.0 * v / 128.0) * masterGain;
  1139. return peq::gainValue (std::clamp (20.0 * std::log10 (std::max (1e-6, g)), -18.0, 18.0));
  1140. }));
  1141. return std::nullopt;
  1142. };
  1143. return out;
  1144. }
  1145. auto container = makeEffectSlot (EffectKind::parallel);
  1146. for (const auto u : active)
  1147. container.branches.push_back ({ unitEffects (u), 1.0f });
  1148. out.slots.push_back (std::move (container));
  1149. return out;
  1150. }
  1151. PatcherBranchHead patcherBranchHead (const std::string& plugin, const std::vector<std::uint8_t>& d, int bus)
  1152. {
  1153. PatcherBranchHead head;
  1154. std::string name = plugin;
  1155. std::transform (name.begin(), name.end(), name.begin(), [] (unsigned char c) { return (char) std::tolower (c); });
  1156. if (name == "frequency splitter")
  1157. {
  1158. if (d.size() < 18 * 4)
  1159. return head;
  1160. auto word = [&] (int i) { return i32At (d, 4 * (std::size_t) i); };
  1161. if (bus == 0)
  1162. {
  1163. auto slot = makeEffectSlot (EffectKind::parametricEq);
  1164. if (frequencySplitter (d, slot))
  1165. {
  1166. head.valid = true;
  1167. head.slot = slot;
  1168. }
  1169. return head;
  1170. }
  1171. int band = -1;
  1172. for (int i = 0; i < 3; ++i)
  1173. if (word (15 + i) == bus)
  1174. band = i;
  1175. if (band < 0)
  1176. return head;
  1177. head.valid = true;
  1178. const auto threeBands = word (6) != 0;
  1179. if (! threeBands && band == 1)
  1180. {
  1181. head.gain = 0.0f; // em 2 bandas o barramento da média é mudo
  1182. return head;
  1183. }
  1184. // inclinação: combo 0…7 = 6/12/18/24/36/48/72/96 dB/oit; o EQ do LYNX tem 12/24/36/48 (aproxima 6, 18, 72, 96)
  1185. auto stages = [&] (int combo, bool& exact) {
  1186. static constexpr int dbPerOctave[8] = { 6, 12, 18, 24, 36, 48, 72, 96 };
  1187. const auto db = dbPerOctave[std::clamp (combo, 0, 7)];
  1188. exact = db % 12 == 0 && db <= 48;
  1189. return std::clamp ((db + 6) / 12, 1, 4);
  1190. };
  1191. auto hz = [] (int v) { return 30.0 * std::pow (16000.0 / 30.0, std::clamp (v, 0, 65536) / 65536.0); };
  1192. auto slot = makeEffectSlot (EffectKind::parametricEq);
  1193. for (int b = 0; b < peq::bands; ++b)
  1194. slot.params[(std::size_t) peq::param (b, 0)] = peq::typeValue (peq::BandType::off);
  1195. int used = 0;
  1196. bool allExact = true;
  1197. auto cut = [&] (peq::BandType type, double f, int combo) {
  1198. bool exact = true;
  1199. slot.params[(std::size_t) peq::param (used, 0)] = peq::typeValue (type);
  1200. slot.params[(std::size_t) peq::param (used, 1)] = peq::hzValue (f);
  1201. slot.params[(std::size_t) peq::param (used, 3)] = peq::qValue (0.707);
  1202. slot.params[(std::size_t) (peq::slopeParam + used)] = peq::slopeValue (stages (combo, exact));
  1203. allExact = allExact && exact;
  1204. ++used;
  1205. };
  1206. const auto f1 = hz (word (1)), f2 = hz (word (2));
  1207. if (band == 0)
  1208. cut (peq::BandType::highCut, f1, word (3));
  1209. else if (band == 1)
  1210. {
  1211. cut (peq::BandType::lowCut, f1, word (3));
  1212. cut (peq::BandType::highCut, f2, word (4));
  1213. }
  1214. else if (threeBands)
  1215. cut (peq::BandType::lowCut, f2, word (4));
  1216. else
  1217. cut (peq::BandType::lowCut, f1, word (3));
  1218. slot.params[peq::outputParam] = peq::gainValue (0.0);
  1219. head.slot = slot;
  1220. head.gain = (float) std::pow (10.0, std::clamp (word (9 + band), -6000, 1800) / 100.0 / 20.0); // nível de envio (centésimos de dB)
  1221. if (! allExact)
  1222. head.note = "Frequency Splitter: inclinação aproximada pelo EQ";
  1223. return head;
  1224. }
  1225. if (name == "fruity stereo shaper")
  1226. {
  1227. if (d.size() < 4 + 8 * 4)
  1228. return head;
  1229. auto param = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  1230. auto slot = makeEffectSlot (EffectKind::utility);
  1231. if (bus == 0)
  1232. {
  1233. stereoShaper (d, slot);
  1234. head.valid = true;
  1235. head.slot = slot;
  1236. return head;
  1237. }
  1238. if (bus != param (6) || param (4) != 0 || param (5) != 0)
  1239. return head; // com delay/fase a diferença não é uma matriz: fora
  1240. // entrada − saída da matriz M = (I − M)
  1241. const auto rl = param (0) / 12800.0, l = param (1) / 12800.0, r = param (2) / 12800.0, lr = param (3) / 12800.0;
  1242. slot.params[2] = utilityMatrixValue (1.0 - l);
  1243. slot.params[3] = utilityMatrixValue (-rl);
  1244. slot.params[5] = utilityMatrixValue (1.0 - r);
  1245. slot.params[4] = utilityMatrixValue (-lr);
  1246. head.valid = true;
  1247. head.slot = slot;
  1248. return head;
  1249. }
  1250. return head;
  1251. }
  1252. } // namespace lynx