FxAudit.h 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  1. #pragma once
  2. // "Every effect control does something" (round 173d, issue #77): for one built-in effect, every control its editor
  3. // shows (core/PluginLayout.h) and every parameter it lists (effectInfo) is set to a few values with the others at
  4. // their defaults, and a short programme (drums, a sustained vocal-like pad, a full mix, a tail of silence; −18 dBFS
  5. // RMS) is rendered through the effect as the mixer runs it: one instance, the parameters arriving with every block
  6. // (the app keeps the running effect when only parameters change), controls stored in the slot's state through
  7. // acceptState or a new instance, as core/RackRenderer.cpp does.
  8. //
  9. // continuous controls 0, ½ and 1 of the range (the ends the editor reaches)
  10. // choices every option (layout radio / tabs / menu / toggle / stepper, or a parameter whose value text
  11. // takes at most `maxChoiceTexts` different values)
  12. //
  13. // A control is DEAD when no tested value changes the output by more than `movedDb` (relative RMS of the difference,
  14. // −60 dB) — nor the slot's sends, nor its control outputs (controllers). A dead control is tried again:
  15. // 1. as the editor shows it (its layout conditions met, its "idle" switch off);
  16. // 2. with side-chain inputs (a kick on the side-chain, a saw chord on the second input);
  17. // 3. in random settings of the others (sections switched on); the first single other control that wakes it up is
  18. // named as its prerequisite (dependent-ok).
  19. // BROKEN outputs: NaN / inf anywhere; and for the default and every choice option: silence, a peak over +6 dBFS, a DC
  20. // offset, a click when the option is switched while the pad plays.
  21. // Used by tools/fxaudit.cpp (the survey, scripts/fxaudit.sh) and tests/FxAudit173dTests.inc (the permanent checks).
  22. #include <cstdlib>
  23. #include <algorithm>
  24. #include <array>
  25. #include <cctype>
  26. #include <cmath>
  27. #include <complex>
  28. #include <cstdint>
  29. #include <cstdio>
  30. #include <functional>
  31. #include <map>
  32. #include <memory>
  33. #include <optional>
  34. #include <set>
  35. #include <string>
  36. #include <vector>
  37. #include "core/ConvolverFx.h"
  38. #include "core/EquoFx.h"
  39. #include "core/LovePhilter.h"
  40. #include "core/Effects.h"
  41. #include "core/FacetEq.h"
  42. #include "core/PluginLayout.h"
  43. #include "core/StateParams.h"
  44. #include "core/Vocodex.h"
  45. namespace lynx::fxaudit
  46. {
  47. constexpr double rate = 44100.0;
  48. constexpr int blockSize = 512;
  49. constexpr double movedDb = -60.0;
  50. constexpr int maxChoiceTexts = 12;
  51. constexpr int maxSteppedTexts = 64;
  52. // ---------------------------------------------------------------------------------------------------------------------
  53. // the programme
  54. struct Programme
  55. {
  56. std::vector<float> l, r; // the effect's input
  57. std::vector<float> scL, scR; // the side-chain (a kick), used only in the side-chain context
  58. std::vector<float> sc2L, sc2R; // the second side input (a saw chord: a vocoder's carrier)
  59. int segment = 0; // frames per part (drums, pad, mix), then a tail of silence
  60. int frames() const { return (int) l.size(); }
  61. int padMiddle() const { return segment + segment / 2; } // a steady pad (switch clicks are measured here)
  62. };
  63. inline Programme programme (double segmentSeconds = 0.6, double tailSeconds = 0.25)
  64. {
  65. Programme p;
  66. p.segment = (int) (segmentSeconds * rate);
  67. const int tail = (int) (tailSeconds * rate);
  68. const int n = 3 * p.segment + tail;
  69. p.l.assign ((std::size_t) n, 0.0f), p.r.assign ((std::size_t) n, 0.0f);
  70. p.scL.assign ((std::size_t) n, 0.0f), p.sc2L.assign ((std::size_t) n, 0.0f);
  71. std::uint32_t seed = 173;
  72. auto noise = [&seed] {
  73. seed = seed * 1664525u + 1013904223u;
  74. return (float) ((double) (seed >> 8) / 8388608.0 - 1.0);
  75. };
  76. constexpr double pi = 3.14159265358979323846;
  77. // drums: 16ths at 120 bpm (0.125 s): kick on 0 and 3, snare on 2 and 6, hats on every 16th
  78. std::vector<float> dl ((std::size_t) n, 0.0f), dr ((std::size_t) n, 0.0f), kick ((std::size_t) n, 0.0f);
  79. const int step = (int) (0.125 * rate);
  80. float hpState = 0.0f, prev = 0.0f;
  81. for (int at = 0; at < n - tail; at += step)
  82. {
  83. const int s16 = (at / step) % 8;
  84. for (int k = 0; k < (int) (0.25 * rate) && at + k < n; ++k)
  85. {
  86. const double t = k / rate;
  87. float v = 0.0f, side = 0.0f;
  88. if (s16 == 0 || s16 == 3)
  89. {
  90. const double ph = 2.0 * pi * (50.0 * t + 100.0 * 0.04 * (1.0 - std::exp (-t / 0.04)));
  91. const float kv = (float) (std::sin (ph) * std::exp (-t / 0.15));
  92. v += kv;
  93. kick[(std::size_t) (at + k)] += kv;
  94. }
  95. if (s16 == 2 || s16 == 6)
  96. v += (float) ((0.5 * noise() + 0.4 * std::sin (2.0 * pi * 200.0 * t)) * std::exp (-t / 0.08)), side += 0.15f * noise() * (float) std::exp (-t / 0.08);
  97. if (k < (int) (0.05 * rate))
  98. {
  99. const float w = noise();
  100. hpState = 0.6f * (hpState + w - prev); // a crude high-pass
  101. prev = w;
  102. v += 0.25f * hpState * (float) std::exp (-t / 0.02);
  103. side += 0.1f * hpState * (float) std::exp (-t / 0.02) * (s16 % 2 == 0 ? 1.0f : -1.0f);
  104. }
  105. dl[(std::size_t) (at + k)] += v + side;
  106. dr[(std::size_t) (at + k)] += v - side;
  107. }
  108. }
  109. // the pad: an "ah" vowel chord (additive saws weighted by two formants), vibrato, detuned between L and R, and an
  110. // "s" (6–10 kHz noise) in its middle; a 55 Hz bass under the mix
  111. std::vector<float> pl ((std::size_t) n, 0.0f), pr ((std::size_t) n, 0.0f), carrier ((std::size_t) n, 0.0f);
  112. const double notes[] = { 220.0, 277.18, 329.63 };
  113. auto formant = [] (double hz) {
  114. auto f = [hz] (double c, double w) { return std::exp (-0.5 * std::pow ((hz - c) / w, 2.0)); };
  115. return 0.15 + f (700.0, 180.0) + 0.6 * f (1200.0, 250.0) + 0.25 * f (2600.0, 400.0);
  116. };
  117. std::vector<double> phase (6 * 24, 0.0);
  118. float bp1 = 0.0f, bp2 = 0.0f;
  119. for (int i = 0; i < n - tail; ++i)
  120. {
  121. const double t = i / rate;
  122. const double vib = 1.0 + 0.004 * std::sin (2.0 * pi * 5.0 * t);
  123. double l = 0.0, r = 0.0;
  124. for (int v = 0; v < 3; ++v)
  125. for (int h = 1; h <= 24; ++h)
  126. {
  127. const double base = notes[v] * h * vib;
  128. if (base > 0.45 * rate)
  129. break;
  130. const double g = formant (base) / h;
  131. auto& pL = phase[(std::size_t) (v * 24 + h - 1)];
  132. auto& pR = phase[(std::size_t) (72 + v * 24 + h - 1)];
  133. pL += 2.0 * pi * base / rate;
  134. pR += 2.0 * pi * base * 1.003 / rate;
  135. l += g * std::sin (pL);
  136. r += g * std::sin (pR);
  137. }
  138. const double env = std::min (1.0, t * 40.0);
  139. pl[(std::size_t) i] = (float) (l * env), pr[(std::size_t) i] = (float) (r * env);
  140. // the carrier: a plain saw chord
  141. double c = 0.0;
  142. for (const auto f : notes)
  143. c += 2.0 * (std::fmod (t * f, 1.0) - 0.5);
  144. carrier[(std::size_t) i] = (float) c;
  145. // "s": band-passed noise for 80 ms at the middle of each pad
  146. const float w = noise();
  147. bp1 += 0.9f * (w - bp1);
  148. bp2 = 0.5f * (bp1 - bp2) + 0.5f * bp2;
  149. const double inPad = std::fmod (t, segmentSeconds) - 0.6 * segmentSeconds;
  150. if (inPad > 0.0 && inPad < 0.08)
  151. {
  152. pl[(std::size_t) i] += 0.4f * (bp1 - bp2);
  153. pr[(std::size_t) i] += 0.4f * (bp1 - bp2);
  154. }
  155. }
  156. auto rms = [] (const std::vector<float>& a, int from, int to) {
  157. double e = 0.0;
  158. for (int i = from; i < to; ++i)
  159. e += (double) a[(std::size_t) i] * a[(std::size_t) i];
  160. return std::sqrt (e / std::max (1, to - from));
  161. };
  162. const double target = std::pow (10.0, -18.0 / 20.0);
  163. const double dg = target / std::max (1e-9, rms (dl, 0, p.segment)), pg = target / std::max (1e-9, rms (pl, p.segment, 2 * p.segment));
  164. for (int i = 0; i < n; ++i)
  165. {
  166. const auto at = (std::size_t) i;
  167. const int part = std::min (3, i / std::max (1, p.segment));
  168. const double bass = part == 2 ? 0.5 * target * std::sin (2.0 * pi * 55.0 * i / rate) : 0.0;
  169. if (part == 0)
  170. p.l[at] = (float) (dg * dl[at]), p.r[at] = (float) (dg * dr[at]);
  171. else if (part == 1)
  172. p.l[at] = (float) (pg * pl[at]), p.r[at] = (float) (pg * pr[at]);
  173. else if (part == 2)
  174. p.l[at] = (float) (0.6 * (dg * dl[at] + pg * pl[at]) + bass), p.r[at] = (float) (0.6 * (dg * dr[at] + pg * pr[at]) + bass);
  175. p.scL[at] = 2.0f * kick[at]; // (a hot key: +6 dBFS kicks, over any default threshold or ceiling)
  176. p.sc2L[at] = (float) (target * 0.5 * carrier[at]);
  177. }
  178. // (the right key: the kicks a 16th later at 0.7 ×: a control that mixes or links the side-chain's channels is
  179. // heard either way, whichever channel a linked detector follows)
  180. const auto sixteenth = (std::size_t) std::lround (rate * 0.125);
  181. p.scR.assign (p.scL.size(), 0.0f);
  182. for (std::size_t i = sixteenth; i < p.scL.size(); ++i)
  183. p.scR[i] = 0.7f * p.scL[i - sixteenth];
  184. p.sc2R = p.sc2L;
  185. return p;
  186. }
  187. // ---------------------------------------------------------------------------------------------------------------------
  188. // the controls
  189. enum class Type { continuous, choice, toggle };
  190. struct Condition
  191. {
  192. int param = -1;
  193. float lo = 0.0f, hi = 1.0f;
  194. bool holds (float v) const { return v >= lo && (v < hi || hi >= 1.0f); }
  195. };
  196. struct Control
  197. {
  198. int param = -1; // plugui::paramIndex numbering (state controls after the effect's own parameters)
  199. std::string name;
  200. Type type = Type::continuous;
  201. std::vector<float> values; // 0…1 positions to render
  202. std::vector<std::string> texts; // what each value reads as
  203. bool state = false; // stored in the slot's state (core/StateParams.h)
  204. bool inLayout = false; // the editor shows it
  205. bool stepped = false; // many steps (12 of them rendered): judged like a knob (no mode checks)
  206. bool momentary = false; // a push button: the engine reacts to a flip while playing
  207. std::vector<Condition> shownWhen, idleWhen;
  208. };
  209. inline bool isNoSound (EffectKind k)
  210. {
  211. switch (k)
  212. {
  213. case EffectKind::peakController: case EffectKind::formulaController: case EffectKind::waveCandy: case EffectKind::visualizer:
  214. case EffectKind::xyController: case EffectKind::xyzController: case EffectKind::controlSurface: case EffectKind::bigClock:
  215. case EffectKind::tuner: case EffectKind::htmlNotebook:
  216. return true;
  217. default:
  218. return false;
  219. }
  220. }
  221. // effects the audit can't run alone: containers, hosted plugins, a device round trip, sample players without a sample
  222. inline const char* skipReason (EffectKind k)
  223. {
  224. switch (k)
  225. {
  226. case EffectKind::plugin: return "a hosted plugin";
  227. case EffectKind::parallel: return "a container (its children are audited on their own)";
  228. case EffectKind::patcher: return "a container (its nodes are audited on their own)";
  229. case EffectKind::hardwareInsert: return "needs an audio device (round trip through hardware)";
  230. case EffectKind::waveCandy: case EffectKind::visualizer: case EffectKind::bigClock: case EffectKind::htmlNotebook:
  231. case EffectKind::tuner:
  232. return "visual only: no sound, no control output";
  233. case EffectKind::edison: // (round 190cd)
  234. return "a recorder / editor: its controls act on its own recording and playback, the input passes through";
  235. default: return nullptr;
  236. }
  237. }
  238. inline float valueOf (const EffectSlot& s, int param)
  239. {
  240. if (plugui::stateIndex (s.kind, param) >= 0)
  241. return plugui::stateValue01 (s.kind, param, s.state);
  242. return param >= 0 && param < maxEffectParams ? s.params[(std::size_t) param] : 0.0f;
  243. }
  244. // writes a control as the editor does (PluginEditors.cpp setValue)
  245. inline void setValue (EffectSlot& s, int param, float v)
  246. {
  247. if (const auto si = plugui::stateIndex (s.kind, param); si >= 0)
  248. {
  249. if (const auto* t = stateparams::tableFor (s.kind))
  250. s.state = t->with (s.state, si, plugui::stateRaw (s.kind, param, v));
  251. return;
  252. }
  253. if (param >= 0 && param < maxEffectParams)
  254. s.params[(std::size_t) param] = v;
  255. }
  256. inline std::string textOf (const EffectSlot& s, int param, float v)
  257. {
  258. if (const auto si = plugui::stateIndex (s.kind, param); si >= 0)
  259. {
  260. const auto* t = stateparams::tableFor (s.kind);
  261. return t != nullptr && t->text ? t->text (s.state, si, plugui::stateRaw (s.kind, param, v)) : std::to_string (plugui::stateRaw (s.kind, param, v));
  262. }
  263. return formatEffectParam (s.kind, param, v, 120.0, s.params);
  264. }
  265. inline std::vector<Control> controlsOf (const EffectSlot& slot)
  266. {
  267. const auto kind = slot.kind;
  268. std::vector<Control> out;
  269. std::map<int, std::size_t> at;
  270. const auto& layout = plugui::layoutFor (kind);
  271. auto cond = [kind] (const char* p, float lo, float hi, std::vector<Condition>& to) {
  272. if (p == nullptr)
  273. return;
  274. const auto i = plugui::paramIndex (kind, p);
  275. if (i >= 0)
  276. to.push_back ({ i, lo, hi });
  277. };
  278. for (const auto& item : layout.items)
  279. {
  280. if (! plugui::isValueControl (item.type) || item.param == nullptr || (item.style & plugui::pageTabs) != 0 || item.type == plugui::Item::value)
  281. continue;
  282. const auto p = plugui::paramIndex (kind, item.param);
  283. if (p < 0)
  284. continue;
  285. Control c;
  286. c.param = p;
  287. c.name = item.param;
  288. c.inLayout = true;
  289. c.state = plugui::stateIndex (kind, p) >= 0;
  290. cond (item.whenParam, item.whenMin, item.whenMax, c.shownWhen);
  291. cond (item.when2Param, item.when2Min, item.when2Max, c.shownWhen);
  292. cond (item.when3Param, item.when3Min, item.when3Max, c.shownWhen);
  293. cond (item.idleParam, item.idleMin, item.idleMax, c.idleWhen);
  294. cond (item.idle2Param, item.idle2Min, item.idle2Max, c.idleWhen);
  295. if (item.type == plugui::Item::radio || item.type == plugui::Item::tabs || item.type == plugui::Item::menu)
  296. {
  297. c.type = Type::choice;
  298. const auto n = (int) plugui::optionLabels (item).size();
  299. for (int o = 0; o < n; ++o)
  300. c.values.push_back (plugui::optionValue (item, o));
  301. }
  302. else if (item.type == plugui::Item::toggle)
  303. c.type = Type::toggle, c.values = { 0.0f, 1.0f }, c.momentary = (item.style & plugui::momentary) != 0;
  304. else if (item.steps > 0 && item.steps <= maxChoiceTexts)
  305. {
  306. c.type = Type::choice;
  307. for (int k = 0; k <= item.steps; ++k)
  308. c.values.push_back ((float) k / (float) item.steps);
  309. }
  310. if (auto it = at.find (p); it != at.end())
  311. {
  312. // the same parameter twice (one per page / voicing): keep the richer description, merge the conditions
  313. auto& old = out[it->second];
  314. if (old.type == Type::continuous && c.type != Type::continuous)
  315. old.type = c.type, old.values = c.values;
  316. if (old.shownWhen.empty() || c.shownWhen.empty())
  317. old.shownWhen.clear();
  318. continue;
  319. }
  320. at[p] = out.size();
  321. out.push_back (std::move (c));
  322. }
  323. // the parameters the editor doesn't show (automation still reaches them)
  324. const auto count = plugui::paramCount (kind);
  325. for (int p = 0; p < count; ++p)
  326. if (at.find (p) == at.end())
  327. {
  328. Control c;
  329. c.param = p;
  330. c.name = plugui::paramName (kind, p);
  331. c.state = plugui::stateIndex (kind, p) >= 0;
  332. at[p] = out.size();
  333. out.push_back (std::move (c));
  334. }
  335. // a continuous control whose value text takes few values is a choice; the values to render
  336. for (auto& c : out)
  337. {
  338. if (c.type == Type::continuous)
  339. {
  340. std::vector<std::pair<std::string, std::vector<float>>> runs;
  341. constexpr int samples = 257;
  342. for (int k = 0; k < samples; ++k)
  343. {
  344. const float v = (float) k / (float) (samples - 1);
  345. auto t = textOf (slot, c.param, v);
  346. if (runs.empty() || runs.back().first != t)
  347. runs.push_back ({ t, {} });
  348. runs.back().second.push_back (v);
  349. if ((int) runs.size() > maxSteppedTexts)
  350. break;
  351. }
  352. // a stepped control with many steps (a slot number…): every run would cost too much, 12 spread ones
  353. const bool stepped = (int) runs.size() <= maxSteppedTexts && runs.size() > maxChoiceTexts
  354. && std::all_of (runs.begin(), runs.end(), [] (const auto& r) { return r.second.size() >= 2; });
  355. if (stepped)
  356. {
  357. std::vector<std::pair<std::string, std::vector<float>>> pick;
  358. for (int k = 0; k < maxChoiceTexts; ++k)
  359. pick.push_back (runs[(std::size_t) std::lround ((double) k * (double) (runs.size() - 1) / (maxChoiceTexts - 1))]);
  360. runs = std::move (pick);
  361. }
  362. if (((int) runs.size() <= maxChoiceTexts || stepped) && runs.size() >= 2)
  363. {
  364. c.type = runs.size() == 2 ? Type::toggle : Type::choice;
  365. c.stepped = stepped;
  366. for (const auto& [t, vs] : runs)
  367. c.values.push_back (vs.front() == 0.0f ? 0.0f : vs.back() == 1.0f ? 1.0f : vs[vs.size() / 2]);
  368. }
  369. else
  370. c.values = { 0.0f, 0.5f, 1.0f };
  371. }
  372. for (const auto v : c.values)
  373. c.texts.push_back (textOf (slot, c.param, v));
  374. }
  375. return out;
  376. }
  377. // ---------------------------------------------------------------------------------------------------------------------
  378. // rendering
  379. struct Output
  380. {
  381. std::vector<float> l, r;
  382. std::vector<float> send; // every send / extra output, interleaved
  383. std::vector<float> ctl; // control outputs 0…7 per block (every control's of a Control Surface)
  384. bool finite = true;
  385. int latency = 0;
  386. };
  387. struct RunOptions
  388. {
  389. bool sidechain = false;
  390. double startBeat = 0.0; // the song position of the first block (beats)
  391. float inputGain = 1.0f; // the programme × this (a hot input: clippers, ceilings)
  392. double bpm = 120.0; // the song tempo
  393. int moveParam = -1; // this control jumps to `moveValue` at the pad's middle (a knob turned while playing)
  394. float moveValue = 0.0f;
  395. int switchBlock = -1; // the slot `after` takes over at this block (a mode switched while playing)
  396. const EffectSlot* after = nullptr;
  397. };
  398. inline Output run (const EffectSlot& built, const EffectSlot& played, const Programme& prog, const RunOptions& o = {})
  399. {
  400. Output out;
  401. auto slot = built; // (some effects keep a pointer to their slot's parameters: it stays alive)
  402. auto fx = makeEffect (slot);
  403. if (fx == nullptr)
  404. return out;
  405. fx->prepare (rate);
  406. auto apply = [&fx, &slot] (const EffectSlot& next) {
  407. if (next.state != slot.state)
  408. {
  409. if (! fx->acceptState (next.state))
  410. {
  411. slot = next;
  412. fx = makeEffect (slot);
  413. fx->prepare (rate);
  414. return;
  415. }
  416. }
  417. slot.params = next.params;
  418. slot.state = next.state;
  419. };
  420. apply (played);
  421. out.latency = fx->latencySamples();
  422. const int n = prog.frames();
  423. out.l.resize ((std::size_t) n), out.r.resize ((std::size_t) n);
  424. std::vector<float> s0l (blockSize), s0r (blockSize), s1l (blockSize), s1r (blockSize), s2l (blockSize), s2r (blockSize);
  425. // C4 E4 G4 B4 held on every channel of the rack (an effect that follows one MIDI channel hears them on any)
  426. std::array<NoteMask, 1024> held; // (RackRenderer::maxChannels)
  427. held.fill ({ 1ull << 60, (1ull << (64 - 64)) | (1ull << (67 - 64)) | (1ull << (71 - 64)) });
  428. double tick = o.startBeat * ppq;
  429. const double ticksPerSample = o.bpm / 60.0 * ppq / rate;
  430. for (int at = 0, b = 0; at < n; at += blockSize, ++b)
  431. {
  432. if (b == o.switchBlock && o.after != nullptr)
  433. apply (*o.after);
  434. if (o.moveParam >= 0 && b == prog.padMiddle() / blockSize)
  435. {
  436. auto next = slot;
  437. setValue (next, o.moveParam, o.moveValue);
  438. apply (next);
  439. }
  440. const int len = std::min (blockSize, n - at);
  441. for (int k = 0; k < len; ++k)
  442. out.l[(std::size_t) (at + k)] = o.inputGain * prog.l[(std::size_t) (at + k)], out.r[(std::size_t) (at + k)] = o.inputGain * prog.r[(std::size_t) (at + k)];
  443. std::fill (s0l.begin(), s0l.end(), 0.0f), std::fill (s0r.begin(), s0r.end(), 0.0f);
  444. std::fill (s1l.begin(), s1l.end(), 0.0f), std::fill (s1r.begin(), s1r.end(), 0.0f);
  445. std::fill (s2l.begin(), s2l.end(), 0.0f), std::fill (s2r.begin(), s2r.end(), 0.0f);
  446. fx->setSendOutput (s0l.data(), s0r.data());
  447. fx->setExtraOutput (0, s1l.data(), s1r.data());
  448. fx->setExtraOutput (1, s2l.data(), s2r.data());
  449. fx->setSongPosition (tick);
  450. fx->setPlaying (true);
  451. fx->setHeldNotes (held.data(), (int) held.size());
  452. // round 190ee: Pitcher takes its notes from a MIDI port (a MIDI Out on that port): the same chord arrives there
  453. // (a held note's repeated note on changes nothing)
  454. if (slot.kind == EffectKind::pitchCorrect)
  455. for (const auto key : { 60, 64, 67, 71 })
  456. fx->midiNote (key, 100, 0);
  457. if (o.sidechain)
  458. {
  459. fx->setSidechain (prog.scL.data() + at, prog.scR.data() + at);
  460. fx->setSidechain2 (prog.sc2L.data() + at, prog.sc2R.data() + at);
  461. }
  462. fx->process (out.l.data() + at, out.r.data() + at, len, slot.params, o.bpm);
  463. tick += len * ticksPerSample;
  464. for (int k = 0; k < len; ++k)
  465. {
  466. out.send.push_back (s0l[(std::size_t) k] + s1l[(std::size_t) k] + 0.5f * s2l[(std::size_t) k]);
  467. out.send.push_back (s0r[(std::size_t) k] + s1r[(std::size_t) k] + 0.5f * s2r[(std::size_t) k]);
  468. }
  469. // the editor's meters and the automation sources read outputs 0…7; a Control Surface sends one per control
  470. const int outputs = slot.kind == EffectKind::controlSurface ? maxEffectParams : 8;
  471. for (int c = 0; c < outputs; ++c)
  472. out.ctl.push_back (fx->controlOutput (c));
  473. }
  474. out.latency = fx->latencySamples(); // (after the blocks: some effects learn their settings in process())
  475. for (int i = 0; i < n; ++i)
  476. if (! std::isfinite (out.l[(std::size_t) i]) || ! std::isfinite (out.r[(std::size_t) i]))
  477. {
  478. out.finite = false;
  479. break;
  480. }
  481. return out;
  482. }
  483. // ---------------------------------------------------------------------------------------------------------------------
  484. // measures
  485. inline double relDiffDb (const std::vector<float>& a, const std::vector<float>& b, double floorEnergy = 1e-12)
  486. {
  487. double e = 0.0, ref = 0.0;
  488. const auto len = std::min (a.size(), b.size());
  489. for (std::size_t i = 0; i < len; ++i)
  490. {
  491. const double d = (double) a[i] - (double) b[i];
  492. if (std::isfinite (d))
  493. e += d * d;
  494. ref += (double) b[i] * b[i];
  495. }
  496. if (e == 0.0)
  497. return -300.0;
  498. return 10.0 * std::log10 (e / std::max (ref, floorEnergy * (double) std::max<std::size_t> (1, len)));
  499. }
  500. inline double audioDiffDb (const Output& a, const Output& b)
  501. {
  502. std::vector<float> x, y;
  503. x.reserve (a.l.size() * 2), y.reserve (b.l.size() * 2);
  504. x.insert (x.end(), a.l.begin(), a.l.end()), x.insert (x.end(), a.r.begin(), a.r.end());
  505. y.insert (y.end(), b.l.begin(), b.l.end()), y.insert (y.end(), b.r.begin(), b.r.end());
  506. return relDiffDb (x, y);
  507. }
  508. inline double peakOf (const Output& o)
  509. {
  510. double p = 0.0;
  511. for (std::size_t i = 0; i < o.l.size(); ++i)
  512. p = std::max ({ p, (double) std::abs (o.l[i]), (double) std::abs (o.r[i]) });
  513. return p;
  514. }
  515. inline double dcOf (const Output& o)
  516. {
  517. double sl = 0.0, sr = 0.0;
  518. for (std::size_t i = 0; i < o.l.size(); ++i)
  519. sl += o.l[i], sr += o.r[i];
  520. const auto n = (double) std::max<std::size_t> (1, o.l.size());
  521. return std::max (std::abs (sl / n), std::abs (sr / n));
  522. }
  523. // octave-band power (mid signal, 2048-point Hann frames): bands 31.5 Hz … 16 kHz
  524. inline std::array<double, 10> octaveBands (const Output& o)
  525. {
  526. std::array<double, 10> bands {};
  527. constexpr int N = 2048;
  528. std::vector<std::complex<double>> buf (N);
  529. for (std::size_t start = 0; start + N <= o.l.size(); start += N)
  530. {
  531. for (int i = 0; i < N; ++i)
  532. {
  533. const double w = 0.5 - 0.5 * std::cos (2.0 * 3.14159265358979323846 * i / (N - 1));
  534. buf[(std::size_t) i] = w * 0.5 * ((double) o.l[start + (std::size_t) i] + o.r[start + (std::size_t) i]);
  535. }
  536. // in-place radix-2 FFT
  537. for (int i = 1, j = 0; i < N; ++i)
  538. {
  539. int bit = N >> 1;
  540. for (; j & bit; bit >>= 1)
  541. j ^= bit;
  542. j ^= bit;
  543. if (i < j)
  544. std::swap (buf[(std::size_t) i], buf[(std::size_t) j]);
  545. }
  546. for (int len = 2; len <= N; len <<= 1)
  547. {
  548. const auto w = std::polar (1.0, -2.0 * 3.14159265358979323846 / len);
  549. for (int i = 0; i < N; i += len)
  550. {
  551. std::complex<double> wk (1.0);
  552. for (int k = 0; k < len / 2; ++k)
  553. {
  554. const auto u = buf[(std::size_t) (i + k)], v = buf[(std::size_t) (i + k + len / 2)] * wk;
  555. buf[(std::size_t) (i + k)] = u + v, buf[(std::size_t) (i + k + len / 2)] = u - v;
  556. wk *= w;
  557. }
  558. }
  559. }
  560. for (int k = 1; k < N / 2; ++k)
  561. {
  562. const double hz = k * rate / N;
  563. const int band = (int) std::floor (std::log2 (hz / 22.3));
  564. if (band >= 0 && band < 10)
  565. bands[(std::size_t) band] += std::norm (buf[(std::size_t) k]);
  566. }
  567. }
  568. return bands;
  569. }
  570. // the largest change of an octave band's level (dB), over the bands within 70 dB of the loudest
  571. inline double bandDiffDb (const std::array<double, 10>& a, const std::array<double, 10>& b)
  572. {
  573. const double top = std::max (*std::max_element (a.begin(), a.end()), *std::max_element (b.begin(), b.end()));
  574. double worst = 0.0;
  575. for (std::size_t i = 0; i < a.size(); ++i)
  576. if (std::max (a[i], b[i]) > top * 1e-7)
  577. worst = std::max (worst, std::abs (10.0 * std::log10 ((a[i] + 1e-30) / (b[i] + 1e-30))));
  578. return worst;
  579. }
  580. // the change of the side signal (L − R), relative to the reference's whole signal, in dB
  581. inline double stereoDiffDb (const Output& a, const Output& b)
  582. {
  583. double e = 0.0, ref = 0.0;
  584. for (std::size_t i = 0; i < std::min (a.l.size(), b.l.size()); ++i)
  585. {
  586. const double sa = 0.5 * ((double) a.l[i] - a.r[i]), sb = 0.5 * ((double) b.l[i] - b.r[i]);
  587. e += (sa - sb) * (sa - sb);
  588. ref += 0.5 * ((double) b.l[i] * b.l[i] + (double) b.r[i] * b.r[i]);
  589. }
  590. return e == 0.0 ? -300.0 : 10.0 * std::log10 (e / std::max (ref, 1e-12));
  591. }
  592. // the biggest sample step in [from, to) of both channels
  593. inline double maxStep (const Output& o, int from, int to)
  594. {
  595. double m = 0.0;
  596. for (int i = std::max (1, from); i < std::min ((int) o.l.size(), to); ++i)
  597. m = std::max ({ m, (double) std::abs (o.l[(std::size_t) i] - o.l[(std::size_t) i - 1]),
  598. (double) std::abs (o.r[(std::size_t) i] - o.r[(std::size_t) i - 1]) });
  599. return m;
  600. }
  601. // ---------------------------------------------------------------------------------------------------------------------
  602. // the audit
  603. struct Change
  604. {
  605. float value = 0.0f;
  606. std::string text;
  607. double audio = -300.0, send = -300.0, ctl = -300.0, band = 0.0, stereo = -300.0;
  608. double peak = 0.0, dc = 0.0;
  609. bool finite = true;
  610. bool latency = false; // the reported latency (PDC) changed
  611. };
  612. struct Finding
  613. {
  614. std::string control, text, what; // what: "NaN", "silent", "clip", "dc", "click"
  615. double value = 0.0;
  616. };
  617. enum class Status { ok, dependent, dead, nonAudio };
  618. struct ControlResult
  619. {
  620. Control control;
  621. std::vector<Change> changes; // (dependent: measured in the prerequisite's context)
  622. double bestDefault = -300.0; // dependent: the best move in the editor's default context
  623. Status status = Status::ok;
  624. std::string prerequisite; // dependent: what wakes it up
  625. std::string prereqKey; // … as a key for Options::known ("" = as the editor shows it)
  626. bool onlyNewInstance = false; // dead live, moves in a new instance (a construction-time setting)
  627. double best = -300.0; // the largest change (dB) over the tested values and paths
  628. };
  629. struct KindReport
  630. {
  631. EffectKind kind {};
  632. std::string name;
  633. const char* skipped = nullptr;
  634. double noise = -300.0; // the default rendered twice (non-deterministic engines)
  635. double peak = 0.0;
  636. int listed = 0; // controls the effect has (more than `controls` when sampled)
  637. std::vector<ControlResult> controls;
  638. std::vector<Finding> broken;
  639. int dead() const { return (int) std::count_if (controls.begin(), controls.end(), [] (const ControlResult& c) { return c.status == Status::dead; }); }
  640. int dependent() const { return (int) std::count_if (controls.begin(), controls.end(), [] (const ControlResult& c) { return c.status == Status::dependent; }); }
  641. };
  642. // settings known to wake up a group of controls (an envelope's points, a band's type…): tried before the searches
  643. struct Hint
  644. {
  645. std::string note;
  646. std::vector<std::pair<int, float>> sets; // param, 0…1 value
  647. double bpm = 0.0; // > 0: also at this tempo
  648. bool sidechain = false; // also with the side-chain input
  649. int move = -1; // ≥ 0: this parameter jumps to moveTo in the middle of the pad
  650. float moveTo = 0.0f;
  651. std::function<void (EffectSlot&)> edit = nullptr; // a change no parameter makes (a send track kept in the state)
  652. };
  653. struct Options
  654. {
  655. std::vector<Hint> hints;
  656. bool clicks = true; // switch every choice while playing
  657. int contexts = 16; // random contexts for the dead controls
  658. bool quick = false; // continuous controls: only the far end from the default (not 0, ½, 1)
  659. std::vector<std::string> only; // only the controls with these names (empty = all)
  660. // the prerequisite key a previous survey found for (effect name, control name), "" = none: tried first
  661. std::function<std::string (const std::string&, const std::string&)> known;
  662. int maxControls = 120; // more than this: the state controls are sampled
  663. int searchBudget = 160; // single-control prerequisites tried per dead control
  664. // (effect, control) → true: a control known to be inert (the regression test's allowed list): not searched
  665. std::function<bool (const std::string&, const std::string&)> inert;
  666. };
  667. // the hints the audit knows per effect: settings that wake up a group of controls together (an envelope's points, a
  668. // section that needs two switches) — parameter names, 0…1 values
  669. inline std::vector<Hint> hintsFor (EffectKind kind)
  670. {
  671. std::vector<Hint> out;
  672. auto named = [kind] (std::string note, std::vector<std::pair<const char*, float>> sets) {
  673. Hint h { std::move (note), {} };
  674. for (const auto& [n, v] : sets)
  675. if (const auto i = plugui::paramIndex (kind, n); i >= 0)
  676. h.sets.push_back ({ i, v });
  677. return h;
  678. };
  679. switch (kind)
  680. {
  681. case EffectKind::rhythmGain:
  682. {
  683. Hint h { "12 points spread over a 1-beat period", { { 0, 1.0f / 64.0f }, { 1, 1.0f } } };
  684. for (int k = 0; k < 12; ++k)
  685. h.sets.push_back ({ 2 + 3 * k, (float) (k + 1) / 13.0f }), h.sets.push_back ({ 3 + 3 * k, k % 2 == 0 ? 0.2f : 1.0f }),
  686. h.sets.push_back ({ 4 + 3 * k, 0.5f });
  687. out.push_back (h);
  688. break;
  689. }
  690. case EffectKind::vintagePhaser:
  691. out.push_back (named ("the delay on and mixed in", { { "Delay", 1.0f }, { "Delay mix", 1.0f }, { "Delay fdbk", 0.5f }, { "Delay time", 0.3f } }));
  692. out.push_back (named ("the LFO synced to the tempo", { { "Tempo sync", 1.0f }, { "LFO speed", 0.5f }, { "Mix", 1.0f } }));
  693. break;
  694. case EffectKind::fruityDelayBank:
  695. {
  696. Hint on { "every bank on", {} }, shelf { "every bank on, both filters peaking", {} },
  697. low { "every bank on, both filters low-pass at a mid cutoff, feedback up, a gain pattern (time-varying: filter 1 before or after the line differs)", {} };
  698. for (int b = 1; b <= 8; ++b)
  699. {
  700. const auto n = "B" + std::to_string (b) + " ";
  701. for (auto* h : { &on, &shelf, &low })
  702. if (const auto i = plugui::paramIndex (kind, (n + "On").c_str()); i >= 0)
  703. h->sets.push_back ({ i, 1.0f });
  704. for (const char* f : { "Filter 1 type", "Filter 2 type" })
  705. if (const auto i = plugui::paramIndex (kind, (n + f).c_str()); i >= 0)
  706. shelf.sets.push_back ({ i, 6.0f / 7.0f }), low.sets.push_back ({ i, 1.0f / 7.0f });
  707. for (const char* f : { "Filter 1 gain", "Filter 2 gain" })
  708. if (const auto i = plugui::paramIndex (kind, (n + f).c_str()); i >= 0)
  709. shelf.sets.push_back ({ i, 0.8f });
  710. for (const char* f : { "Filter 1 cutoff", "Filter 2 cutoff", "Filter 1 resonance", "Filter 2 resonance" })
  711. if (const auto i = plugui::paramIndex (kind, (n + f).c_str()); i >= 0)
  712. shelf.sets.push_back ({ i, 0.5f }), low.sets.push_back ({ i, 0.5f }); // (the default cutoff sits near Nyquist)
  713. if (const auto i = plugui::paramIndex (kind, (n + "Feedback level").c_str()); i >= 0)
  714. low.sets.push_back ({ i, 0.8f });
  715. for (const char* f : { "Steps", "Shape" })
  716. if (const auto i = plugui::paramIndex (kind, (n + f).c_str()); i >= 0)
  717. low.sets.push_back ({ i, 0.3f });
  718. }
  719. out.push_back (on);
  720. out.push_back (shelf);
  721. auto slow = on;
  722. slow.note = "every bank on, at 90 BPM (a synced time equals the free one at 120 BPM)", slow.bpm = 90.0;
  723. out.push_back (slow);
  724. out.push_back (low);
  725. break;
  726. }
  727. case EffectKind::vintageChorus:
  728. out.push_back (named ("the manual mode, its LFO synced to the tempo", { { "Mode", 1.0f }, { "LFO sync", 1.0f } }));
  729. break;
  730. case EffectKind::fruityLimiter:
  731. out.push_back (named ("the compressor on (threshold low, ratio up)", { { "Comp. threshold", 0.1f }, { "Ratio", 0.8f } }));
  732. out.push_back (named ("the gate on", { { "Gate gain", 0.0f }, { "Gate threshold", 0.6f } }));
  733. break;
  734. case EffectKind::sentryGate:
  735. {
  736. out.push_back (named ("Upward style, threshold under the signal", { { "Style", 1.0f }, { "Up threshold", 0.0f } }));
  737. auto ext = named ("the external side-chain (its right channel differs)", { { "External SC", 1.0f } });
  738. ext.sidechain = true;
  739. out.push_back (ext);
  740. break;
  741. }
  742. case EffectKind::autoPan:
  743. out.push_back (named ("the Rhythm voicing triggered by the audio", { { "Voicing", 1.0f }, { "Trigger", 0.5f }, { "Trig threshold", 0.5f }, { "Amount", 1.0f } }));
  744. out.push_back (named ("the Rhythm voicing", { { "Voicing", 1.0f } }));
  745. break;
  746. case EffectKind::ember:
  747. {
  748. // the modulation: every slot on one source, full amount, on a target; six bands with their feedback up;
  749. // the Classic panel driven
  750. for (const float src : { 1.0f / 3.0f, 2.0f / 3.0f, 1.0f })
  751. for (const float target : { 0.1f, 0.3f })
  752. {
  753. Hint h { "the modulation slots on one source (LFO 1 / LFO 2 / envelope), full amount", {} };
  754. for (int m = 1; m <= 4; ++m)
  755. {
  756. const auto n = "Mod " + std::to_string (m) + " ";
  757. if (const auto i = plugui::paramIndex (kind, (n + "source").c_str()); i >= 0) h.sets.push_back ({ i, src });
  758. if (const auto i = plugui::paramIndex (kind, (n + "target").c_str()); i >= 0) h.sets.push_back ({ i, target });
  759. if (const auto i = plugui::paramIndex (kind, (n + "amount").c_str()); i >= 0) h.sets.push_back ({ i, 1.0f });
  760. }
  761. out.push_back (h);
  762. }
  763. {
  764. // the envelope source sits near +1.4 units on a −18 dBFS input, so a full amount pins Mix / a drive at
  765. // its end; a small amount on the output gain keeps it inside the range (the follower's times then act)
  766. Hint env { "the modulation slots on the envelope, a small amount on the output gain", {} };
  767. for (int m = 1; m <= 4; ++m)
  768. {
  769. const auto n = "Mod " + std::to_string (m) + " ";
  770. if (const auto i = plugui::paramIndex (kind, (n + "source").c_str()); i >= 0) env.sets.push_back ({ i, 1.0f });
  771. if (const auto i = plugui::paramIndex (kind, (n + "target").c_str()); i >= 0) env.sets.push_back ({ i, 2.0f / 26.0f });
  772. if (const auto i = plugui::paramIndex (kind, (n + "amount").c_str()); i >= 0) env.sets.push_back ({ i, 0.6f });
  773. }
  774. out.push_back (env);
  775. }
  776. Hint six { "six bands, each band's feedback up", {} };
  777. for (const char* n : { "Bands", "More bands" })
  778. if (const auto i = plugui::paramIndex (kind, n); i >= 0) six.sets.push_back ({ i, 1.0f });
  779. for (int b = 1; b <= 6; ++b)
  780. if (const auto i = plugui::paramIndex (kind, ("B" + std::to_string (b) + " feedback").c_str()); i >= 0) six.sets.push_back ({ i, 1.0f });
  781. out.push_back (six);
  782. out.push_back (named ("the Classic panel driven hard", { { "Classic drive", 0.9f } }));
  783. break;
  784. }
  785. case EffectKind::prismMultiband:
  786. {
  787. Hint la { "6 bands compressing hard, the global look-ahead at its maximum", {} },
  788. free { "6 bands, the free layout with every band's edges inside the programme", {} },
  789. sc { "6 bands, each side-chain filter free and narrow, linked 100 %", {} };
  790. for (auto* h : { &la, &free, &sc })
  791. if (const auto i = plugui::paramIndex (kind, "Bands"); i >= 0)
  792. h->sets.push_back ({ i, 1.0f });
  793. if (const auto i = plugui::paramIndex (kind, "Lookahead"); i >= 0)
  794. la.sets.push_back ({ i, 1.0f });
  795. if (const auto i = plugui::paramIndex (kind, "Band layout"); i >= 0)
  796. free.sets.push_back ({ i, 1.0f });
  797. for (int b = 1; b <= 6; ++b)
  798. {
  799. const auto n = "B" + std::to_string (b) + " ";
  800. auto set = [&] (Hint& h, const char* what, float v) {
  801. if (const auto i = plugui::paramIndex (kind, (n + what).c_str()); i >= 0)
  802. h.sets.push_back ({ i, v });
  803. };
  804. set (la, "threshold", 0.0f), set (la, "ratio", 1.0f), set (la, "range", 0.0f);
  805. set (free, "low", 0.35f), set (free, "high", 0.65f), set (free, "threshold", 0.0f);
  806. set (sc, "SC filter", 1.0f), set (sc, "SC low", 0.4f), set (sc, "SC high", 0.6f), set (sc, "link", 1.0f), set (sc, "threshold", 0.0f);
  807. }
  808. out.push_back (la);
  809. out.push_back (free);
  810. out.push_back (sc);
  811. auto ext = la;
  812. ext.note = "6 bands compressing hard, with a side-chain input", ext.sidechain = true;
  813. out.push_back (ext);
  814. break;
  815. }
  816. case EffectKind::ridgeStrip:
  817. out.push_back (named ("the Vocal strip's compander working (low thresholds)",
  818. { { "Compander", 1.0f }, { "Compander threshold", 0.1f }, { "Expander threshold", 0.6f } }));
  819. out.push_back (named ("the dynamics on: a closing gate, a hard compressor, the filters in",
  820. { { "Dynamics on", 1.0f }, { "Gate range", 1.0f }, { "Gate threshold", 0.7f }, { "Comp. threshold", 0.1f },
  821. { "Ratio", 1.0f }, { "HPF", 0.6f }, { "LPF", 0.4f } }));
  822. for (const float t : { 0.4f, 0.55f, 0.7f }) // (a soft knee acts within 6 dB of the threshold)
  823. out.push_back (named ("the compander's threshold near the signal, a high ratio",
  824. { { "Compander", 1.0f }, { "Compander threshold", t }, { "Compander ratio", 1.0f } }));
  825. break;
  826. case EffectKind::echoform:
  827. // the Rhythm taps 1…15: Rhythm on with 16 repeats in one beat (inside the programme), the free edit (the tap times count)
  828. out.push_back (named ("Rhythm on, 16 repeats, free tap times in one beat",
  829. { { "Rhythm", 1.0f }, { "Repeats", 1.0f }, { "Rhythm edit", 0.5f }, { "Length", 0.0f }, { "Decay", 0.0f } }));
  830. break;
  831. case EffectKind::facetEq:
  832. {
  833. auto h = named ("band 1 a dynamic bell (range −30 dB, a low threshold), with a side-chain input",
  834. { { "1 Shape", 2.0f / 16.0f }, { "1 Range", 0.0f }, { "1 Threshold", 0.3f } });
  835. h.sidechain = true;
  836. out.push_back (h);
  837. // every band a spectral dynamic bell (its Density / Spectral Tilt act only there), and the linear phase
  838. // with a narrow low boost (a flat EQ is a delta at any FIR length: Resolution acts only on a shaped one)
  839. for (int b = 1; b <= 8; ++b)
  840. {
  841. const auto n = std::to_string (b) + " ";
  842. Hint sp { "band " + std::to_string (b) + " a spectral dynamic bell (range −30 dB, a low threshold)", {} };
  843. for (const auto& [f, v] : { std::pair<const char*, float> { "Shape", 2.0f / 16.0f }, { "Range", 0.0f }, { "Threshold", 0.3f }, { "Spectral", 1.0f } })
  844. if (const auto i = plugui::paramIndex (kind, (n + f).c_str()); i >= 0)
  845. sp.sets.push_back ({ i, v });
  846. sp.sidechain = true;
  847. out.push_back (sp);
  848. }
  849. out.push_back (named ("the linear phase, band 1 a narrow +30 dB bell at a low frequency",
  850. { { "Phase", 1.0f }, { "1 Shape", 2.0f / 16.0f }, { "1 Gain", 1.0f }, { "1 Q", 1.0f }, { "1 Freq", 0.15f } }));
  851. break;
  852. }
  853. case EffectKind::equo:
  854. case EffectKind::multibandDelay:
  855. for (int b = 1; b < 8; ++b) // the morph on bank b + 1 (a bank's cells are heard only there)
  856. out.push_back (named ("the morph on bank " + std::to_string (b + 1), { { "Morph", (float) b / 7.0f } }));
  857. if (kind == EffectKind::equo) // round 173d3: the band sends go to an insert (FL's send box)
  858. for (int b = 0; b < 8; ++b)
  859. {
  860. auto h = named (b == 0 ? std::string ("the send to insert 2") : "the send to insert 2, the morph on bank " + std::to_string (b + 1),
  861. { { "Morph", (float) b / 7.0f } });
  862. if (b == 0)
  863. h.sets.clear();
  864. h.edit = [] (EffectSlot& e) {
  865. e.sendTracks[0] = 2;
  866. equofx::setSendInsert (e.state, 2);
  867. };
  868. out.push_back (h);
  869. }
  870. break;
  871. case EffectKind::lovePhilter:
  872. {
  873. // round 173d3: FL's default has unit 1 on and no articulator drawn, so the units, envelopes, LFOs and maps
  874. // are woken by drawing them in the state (FL: the unit page's enable boxes and the articulator editor)
  875. const auto points = [] (std::initializer_list<std::pair<double, double>> xy) {
  876. std::vector<philter::DocPoint> out;
  877. double last = 0.0;
  878. for (const auto& [x, y] : xy)
  879. {
  880. philter::DocPoint q;
  881. q.dt = x - last, q.y = y;
  882. last = x;
  883. out.push_back (q);
  884. }
  885. return out;
  886. };
  887. const auto drawn = [points] (bool shaper, bool allTargets) {
  888. return [points, shaper, allTargets] (EffectSlot& e) {
  889. auto d = philter::readDoc (e.state);
  890. if (! d)
  891. return;
  892. for (int u = 0; u < 8; ++u)
  893. {
  894. auto& un = d->u[(std::size_t) u];
  895. un.enabled = 1;
  896. if (un.mode == 0)
  897. un.mode = 1;
  898. if (shaper) // an S curve
  899. {
  900. un.wsOn = 1;
  901. un.ws.pts = points ({ { 0.0, 0.0 }, { 0.3, 0.1 }, { 0.5, 0.5 }, { 0.7, 0.9 }, { 1.0, 1.0 } });
  902. }
  903. for (int t = 0; t < 8; ++t)
  904. {
  905. if (! allTargets && t != 2)
  906. continue;
  907. auto& a = d->u[(std::size_t) u].art[(std::size_t) t];
  908. for (int p = 0; p < 10; ++p) // a different envelope per pattern
  909. {
  910. a.pat[(std::size_t) p].on = 1;
  911. a.pat[(std::size_t) p].pts = points ({ { 0.0, 0.1 }, { 0.25 + 0.1 * p, 0.95 }, { 1.0 + 0.1 * p, 0.2 + 0.06 * p } });
  912. }
  913. a.lfo.on = 1;
  914. a.lfo.pts = points ({ { 0.0, 1.0 }, { 4.0, 1.0 } });
  915. a.lfoDepth = 1.0f;
  916. a.maps[0].pts = points ({ { 0.0, 1.0 }, { 1.0, 0.0 } });
  917. a.maps[1].pts = points ({ { 0.0, 0.0 }, { 0.5, 1.0 }, { 1.0, 0.2 } });
  918. a.maps[2].pts = points ({ { 0.0, 1.0 }, { 0.3, 0.2 }, { 1.0, 0.0 } });
  919. }
  920. }
  921. e.state = philter::writeDoc (*d);
  922. };
  923. };
  924. Hint units { "every unit on (FL's enable boxes)", {} };
  925. units.edit = [] (EffectSlot& e) {
  926. if (auto d = philter::readDoc (e.state))
  927. {
  928. for (auto& un : d->u)
  929. un.enabled = 1, un.mode = un.mode == 0 ? 1 : un.mode;
  930. e.state = philter::writeDoc (*d);
  931. }
  932. };
  933. out.push_back (units);
  934. Hint cut { "every unit on, the cutoff articulators drawn: a pattern envelope per pattern, the LFO, the Mod X / Mod Y / follower maps", {} };
  935. cut.edit = drawn (false, false);
  936. out.push_back (cut);
  937. Hint all { "every unit on with an S-curve waveshaper, every articulator drawn (envelopes, LFOs, maps)", {} };
  938. all.edit = drawn (true, true);
  939. out.push_back (all);
  940. // (the envelopes' and LFOs' TEMPO: FL's free time runs at 2 units a second, its tempo time at BPM / 60, the same at
  941. // 120 BPM; the B / H articulators scale the band / high levels, so they act once those are up)
  942. auto tempo = named ("every articulator drawn, the band and high levels up, at 90 BPM", {});
  943. for (int u = 1; u <= 8; ++u)
  944. for (const auto* f : { " band", " high" })
  945. if (const auto i = plugui::paramIndex (kind, ("Unit " + std::to_string (u) + f).c_str()); i >= 0)
  946. tempo.sets.push_back ({ i, 0.6f });
  947. tempo.edit = drawn (true, true);
  948. tempo.bpm = 90.0;
  949. out.push_back (tempo);
  950. break;
  951. }
  952. case EffectKind::convolver:
  953. {
  954. // round 173d3 (gap173d-3): FL's default EQ curve is flat (its gain acts at any EQ amount), so the EQ amount
  955. // is heard only once a curve is drawn: a low shelf down, a high boost
  956. Hint h { "an EQ curve drawn (lows down, highs up)", {} };
  957. h.edit = [] (EffectSlot& e) {
  958. std::vector<convolver::EnvPoint> pts (4);
  959. pts[0].y = 0.15, pts[1].dt = 0.3, pts[1].y = 0.5, pts[2].dt = 0.4, pts[2].y = 0.5, pts[3].dt = 0.3, pts[3].y = 0.95;
  960. convolver::writeEqCurve (e.state, pts);
  961. };
  962. out.push_back (h);
  963. break;
  964. }
  965. case EffectKind::frequencySplitter:
  966. {
  967. // (the low band down: the bands no longer sum back to the input, so the crossover is heard)
  968. auto h = named ("FFT mode, the low band down, the crossover moved while it plays (the fade between the old and new filters)",
  969. { { "FFT mode", 1.0f }, { "Low level", 0.0f } });
  970. h.move = plugui::paramIndex (kind, "Low/mid freq."), h.moveTo = 0.6f;
  971. out.push_back (h);
  972. break;
  973. }
  974. case EffectKind::horizon:
  975. out.push_back (named ("the echo up and synced to the tempo", { { "Echo level", 1.0f }, { "Echo sync", 1.0f / 3.0f }, { "Echo feedback", 0.5f } }));
  976. break;
  977. case EffectKind::reeverb2:
  978. {
  979. auto h = named ("a predelay, at 90 BPM (the tempo's predelay equals the free one at 120 BPM)", { { "Predelay", 0.5f } });
  980. h.bpm = 90.0;
  981. out.push_back (h);
  982. break;
  983. }
  984. case EffectKind::distructor:
  985. out.push_back (named ("every module's output up (the final clipper's range)", { { "M1 Out", 1.0f }, { "M2 Out", 1.0f }, { "M3 Out", 1.0f }, { "M4 Out", 1.0f } }));
  986. break;
  987. case EffectKind::lynxTune:
  988. out.push_back (named ("the created vibrato on, at once", { { "Vibrato shape", 1.0f / 3.0f }, { "Vibrato depth", 1.0f }, { "Onset delay", 0.0f }, { "Vibrato rate", 0.5f } }));
  989. out.push_back (named ("the reference tone on, MIDI channel 1", { { "MIDI", 1.0f / 128.0f }, { "Reference tone", 1.0f } }));
  990. break;
  991. case EffectKind::pitchCorrect:
  992. out.push_back (named ("Harmonize from the held notes, mixed", { { "Harmonize", 1.0f }, { "MIDI", 1.0f }, { "Mix", 1.0f } }));
  993. break;
  994. case EffectKind::vocodex:
  995. {
  996. // round 173d3 (gap173d-4): a new instance's gate map ("Modulator noise level") is flat at 0, so the
  997. // Subtractive gate switch has no threshold to act on until the map is drawn
  998. Hint h { "the gate map drawn (a rising line)", {} };
  999. h.edit = [] (EffectSlot& e) { e.state = vocodex::withMap (e.state, 3, { { 0.0, 0.0 }, { 1.0, 0.8 } }); };
  1000. out.push_back (h);
  1001. break;
  1002. }
  1003. case EffectKind::pocketMultiband:
  1004. // (round 190cm) FLM's defaults compress nothing (thresholds at 0 dB, ratios 1:1): a band's attack and
  1005. // release are heard once it works
  1006. for (const char* band : { "Low", "Mid", "High" })
  1007. {
  1008. const auto t = std::string (band) + " threshold", r = std::string (band) + " ratio";
  1009. out.push_back (named (std::string ("the ") + band + " band compressing (threshold −34 dB, ratio 31:1)", { { t.c_str(), 0.3f }, { r.c_str(), 1.0f } }));
  1010. }
  1011. break;
  1012. case EffectKind::pocketTapeStop:
  1013. {
  1014. // (round 190cm) FLM's Tape Stop acts on an edge of Play while the song runs (spec-tapestop.md §3.2): the
  1015. // times, Warp and Sync shape the ramp that edge starts; held on it passes the input, held off it is silent
  1016. const auto play = plugui::paramIndex (kind, "Play");
  1017. auto stop = named ("Sync on, Play switched off while the pad plays (the stop ramp)", { { "Sync", 1.0f } });
  1018. stop.move = play, stop.moveTo = 0.0f;
  1019. auto start = named ("Play off, switched on while the pad plays (the start ramp)", { { "Play", 0.0f } });
  1020. start.move = play, start.moveTo = 1.0f;
  1021. auto startSynced = named ("Sync on and Play off, switched on while the pad plays (the start ramp)", { { "Sync", 1.0f }, { "Play", 0.0f } });
  1022. startSynced.move = play, startSynced.moveTo = 1.0f;
  1023. out.push_back (stop), out.push_back (start), out.push_back (startSynced);
  1024. break;
  1025. }
  1026. case EffectKind::timeprism: // (round 190ko) the freezer's retrigger modes need three switches together
  1027. out.push_back (named ("the freezer re-freezing every interval in beats", { { "Freezer", 1.0f }, { "Freeze", 1.0f }, { "Freeze mode", 1.0f },
  1028. { "Retrigger", 1.0f }, { "Sync units", 1.0f }, { "Delay", 0.0f } }));
  1029. out.push_back (named ("the freezer re-freezing every interval in ms", { { "Freezer", 1.0f }, { "Freeze", 1.0f }, { "Freeze mode", 1.0f },
  1030. { "Retrigger", 1.0f }, { "Sync units", 0.0f }, { "Delay", 0.0f } }));
  1031. out.push_back (named ("the freezer re-freezing every interval in ms, enveloped", { { "Freezer", 1.0f }, { "Freeze", 1.0f },
  1032. { "Freeze mode", 1.0f }, { "Retrigger", 1.0f },
  1033. { "Sync units", 0.0f }, { "Fade type", 1.0f },
  1034. { "Delay", 0.0f } }));
  1035. out.push_back (named ("the freezer at onsets, enveloped", { { "Freezer", 1.0f }, { "Freeze", 1.0f }, { "Freeze mode", 1.0f },
  1036. { "Retrigger", 0.0f }, { "Fade type", 1.0f }, { "Delay", 0.0f } }));
  1037. break;
  1038. default:
  1039. break;
  1040. }
  1041. return out;
  1042. }
  1043. inline double threshold (double noise) { return std::max (movedDb, noise + 10.0); }
  1044. inline Change measure (const Output& v, const Output& base, float value, std::string text)
  1045. {
  1046. Change c;
  1047. c.value = value, c.text = std::move (text);
  1048. c.audio = audioDiffDb (v, base);
  1049. c.send = relDiffDb (v.send, base.send, 1e-10);
  1050. c.ctl = relDiffDb (v.ctl, base.ctl, 1e-6);
  1051. c.band = bandDiffDb (octaveBands (v), octaveBands (base));
  1052. c.stereo = stereoDiffDb (v, base);
  1053. c.peak = peakOf (v), c.dc = dcOf (v), c.finite = v.finite;
  1054. c.latency = v.latency != base.latency;
  1055. return c;
  1056. }
  1057. inline double moveOf (const Change& c) { return std::max ({ c.audio, c.send, c.ctl, c.latency ? 0.0 : -300.0 }); }
  1058. inline std::string lower (std::string s)
  1059. {
  1060. std::transform (s.begin(), s.end(), s.begin(), [] (unsigned char ch) { return (char) std::tolower (ch); });
  1061. return s;
  1062. }
  1063. inline std::vector<float> valuesToTest (const Control& c, float current, bool quick)
  1064. {
  1065. std::vector<float> vs;
  1066. // (a phase knob wraps: its far end is its start, so the quick value is half a turn away)
  1067. const bool phase = lower (c.name).find ("phase") != std::string::npos;
  1068. if (c.type == Type::continuous && quick)
  1069. {
  1070. // the far end, and the near one too when the knob doesn't sit on it (a threshold's "off" end)
  1071. vs.push_back (phase ? std::fmod (current + 0.5f, 1.0f) : current > 0.5f ? 0.0f : 1.0f);
  1072. if (! phase && current > 0.02f && current < 0.98f)
  1073. vs.push_back (current > 0.5f ? 1.0f : 0.0f);
  1074. }
  1075. else
  1076. for (std::size_t i = 0; i < c.values.size(); ++i)
  1077. {
  1078. const auto v = c.values[i];
  1079. if (std::abs (v - current) <= 1e-6f)
  1080. continue;
  1081. // (a choice option that reads as the current one is the current one)
  1082. if (c.type != Type::continuous && ! c.momentary && i < c.texts.size() && i < c.values.size())
  1083. {
  1084. const auto near = std::min_element (c.values.begin(), c.values.end(), [current] (float a, float b) {
  1085. return std::abs (a - current) < std::abs (b - current);
  1086. });
  1087. if (c.texts[(std::size_t) (near - c.values.begin())] == c.texts[i] && std::abs (*near - current) < 0.5f / (float) c.values.size())
  1088. continue;
  1089. }
  1090. vs.push_back (v);
  1091. }
  1092. return vs;
  1093. }
  1094. // a render with control `c` at `played`'s value (a push button: pressed while the pad plays)
  1095. inline Output variant (const EffectSlot& ctx, const EffectSlot& played, const Control& c, const Programme& prog, RunOptions o = {})
  1096. {
  1097. // the mixer builds a new instance for this edit (RackRenderer's reuse rule): Facet EQ's resolution
  1098. if (ctx.kind == EffectKind::facetEq && c.param == facet::resolutionParam && ! c.state)
  1099. return run (played, played, prog, o);
  1100. if (! c.momentary)
  1101. return run (ctx, played, prog, o);
  1102. o.switchBlock = prog.padMiddle() / blockSize;
  1103. o.after = &played;
  1104. return run (ctx, ctx, prog, o);
  1105. }
  1106. // the best change of control `c` over its values, from context `ctx`
  1107. inline double bestMove (const EffectSlot& ctx, const Control& c, const Programme& prog, const RunOptions& o, double thr, bool quick,
  1108. std::vector<Change>* record = nullptr)
  1109. {
  1110. const auto base = run (ctx, ctx, prog, o);
  1111. double best = -300.0;
  1112. for (const auto v : valuesToTest (c, valueOf (ctx, c.param), quick))
  1113. {
  1114. auto played = ctx;
  1115. setValue (played, c.param, v);
  1116. if (played.params == ctx.params && played.state == ctx.state)
  1117. continue; // (a state control that can't be edited)
  1118. const auto out = variant (ctx, played, c, prog, o);
  1119. const auto ch = measure (out, base, v, textOf (ctx, c.param, v));
  1120. best = std::max (best, moveOf (ch));
  1121. if (record != nullptr)
  1122. record->push_back (ch);
  1123. if (record == nullptr && best > thr)
  1124. break;
  1125. }
  1126. return best;
  1127. }
  1128. // controls that set what the editor shows or how the engine runs, not the sound (meters, displays, threads)
  1129. inline bool nonAudio (const std::string& name)
  1130. {
  1131. const auto n = lower (name);
  1132. for (const char* w : { "display", "meter", "vu ", "threaded", "loudness reset", "loudness target", "view", "analyzer" })
  1133. if (n.find (w) != std::string::npos)
  1134. return true;
  1135. return n == "vu";
  1136. }
  1137. inline bool keepInContext (const std::string& name)
  1138. {
  1139. const auto n = lower (name);
  1140. for (const char* w : { "output", "out gain", "volume", "master", "mute", "bypass", "power", "dry/wet", "mix", "wet", "level" })
  1141. if (n.find (w) != std::string::npos)
  1142. return true;
  1143. return false;
  1144. }
  1145. // The number of parameters a kind had when the last survey recorded its "rand:k" contexts (tests/FxAudit173dKnown.inc).
  1146. // A random context draws one value per control in the controls' order, so a control added later — in the middle of
  1147. // the editor, or before the parameters the editor doesn't show — would hand every control after it the next one's
  1148. // draw and the recorded contexts would mean other settings (round 190bb: LYNX Tune's Breathiness made "B dir" →
  1149. // rand:3 dead). Parameters from this index on draw after all the others. Regenerate the known keys and drop the
  1150. // entry after a new survey.
  1151. inline int surveyedParamCount (EffectKind kind)
  1152. {
  1153. switch (kind)
  1154. {
  1155. case EffectKind::lynxTune: return 95; // round 190bb: + Breathiness (95)
  1156. default: return 1 << 30;
  1157. }
  1158. }
  1159. // the context a prerequisite key describes (see audit), nullopt = not usable here; `why` = its description
  1160. inline std::optional<EffectSlot> prerequisite (const std::string& key, const EffectSlot& ctx, const std::vector<Control>& controls,
  1161. const Control& c, RunOptions& ro, std::string& why)
  1162. {
  1163. if (key.rfind ("move:", 0) == 0)
  1164. {
  1165. const auto colon = key.find (':', 5);
  1166. if (colon == std::string::npos)
  1167. return std::nullopt;
  1168. ro.moveParam = std::atoi (key.c_str() + 5), ro.moveValue = (float) std::atof (key.c_str() + colon + 1);
  1169. if (ro.moveParam == c.param)
  1170. return std::nullopt;
  1171. why = std::string ("acts while ") + plugui::paramName (ctx.kind, ro.moveParam) + " moves";
  1172. return ctx;
  1173. }
  1174. if (key.rfind ("bpm:", 0) == 0)
  1175. {
  1176. ro.bpm = std::atof (key.c_str() + 4), why = "another tempo (" + key.substr (4) + " BPM)";
  1177. return ctx;
  1178. }
  1179. if (key == "hot")
  1180. {
  1181. ro.inputGain = 8.0f, why = "a hot input (+18 dB, peaks over 0 dBFS)";
  1182. return ctx;
  1183. }
  1184. if (key == "sc")
  1185. {
  1186. ro.sidechain = true, why = "a side-chain input";
  1187. return ctx;
  1188. }
  1189. if (key.rfind ("beat:", 0) == 0)
  1190. {
  1191. ro.startBeat = std::atof (key.c_str() + 5), why = "a later song position (beat " + key.substr (5) + ")";
  1192. return ctx;
  1193. }
  1194. if (key.rfind ("hint:", 0) == 0)
  1195. {
  1196. const auto hints = hintsFor (ctx.kind);
  1197. const auto h = (std::size_t) std::atoi (key.c_str() + 5);
  1198. if (h >= hints.size())
  1199. return std::nullopt;
  1200. auto hc = ctx;
  1201. for (const auto& [p, v] : hints[h].sets)
  1202. if (p != c.param)
  1203. setValue (hc, p, v);
  1204. if (hints[h].edit)
  1205. hints[h].edit (hc);
  1206. why = hints[h].note;
  1207. if (hints[h].bpm > 0.0)
  1208. ro.bpm = hints[h].bpm;
  1209. if (hints[h].sidechain)
  1210. ro.sidechain = true;
  1211. if (hints[h].move >= 0 && hints[h].move != c.param)
  1212. ro.moveParam = hints[h].move, ro.moveValue = hints[h].moveTo;
  1213. return hc;
  1214. }
  1215. if (key.rfind ("set:", 0) == 0)
  1216. {
  1217. const auto colon = key.find (':', 4);
  1218. if (colon == std::string::npos)
  1219. return std::nullopt;
  1220. const int p = std::atoi (key.c_str() + 4);
  1221. const float v = (float) std::atof (key.c_str() + colon + 1);
  1222. if (p == c.param)
  1223. return std::nullopt;
  1224. auto one = ctx;
  1225. setValue (one, p, v);
  1226. why = std::string (p < plugui::paramCount (ctx.kind) ? plugui::paramName (ctx.kind, p) : "?") + " = " + textOf (one, p, v);
  1227. return one;
  1228. }
  1229. if (key.rfind ("rand:", 0) == 0)
  1230. {
  1231. const int want = std::atoi (key.c_str() + 5);
  1232. std::uint32_t seed = 173u + (std::uint32_t) c.param * 7919u + (std::uint32_t) want * 104729u;
  1233. auto rnd = [&seed] {
  1234. seed = seed * 1664525u + 1013904223u;
  1235. return (float) ((seed >> 8) & 0xffffff) / 16777216.0f;
  1236. };
  1237. auto rc = ctx;
  1238. const auto surveyed = surveyedParamCount (ctx.kind);
  1239. for (const bool late : { false, true }) // (the parameters added since the survey draw last)
  1240. for (const auto& o : controls)
  1241. {
  1242. if (o.param == c.param || keepInContext (o.name) || (o.param >= surveyed) != late)
  1243. continue;
  1244. const auto v = o.type == Type::continuous ? rnd() : o.values[(std::size_t) std::min ((int) o.values.size() - 1, (int) (rnd() * (float) o.values.size()))];
  1245. setValue (rc, o.param, v);
  1246. }
  1247. why = "a combination of other settings (random context " + key.substr (5) + ")";
  1248. return rc;
  1249. }
  1250. return std::nullopt;
  1251. }
  1252. inline KindReport audit (const EffectSlot& slot0, const Programme& prog, const Options& opt = {})
  1253. {
  1254. KindReport rep;
  1255. rep.kind = slot0.kind;
  1256. rep.name = effectInfo (slot0.kind).name;
  1257. rep.skipped = skipReason (slot0.kind);
  1258. if (rep.skipped != nullptr)
  1259. return rep;
  1260. auto controls = controlsOf (slot0);
  1261. const auto allControls = controls; // (the prerequisite search looks at all of them)
  1262. rep.listed = (int) controls.size();
  1263. // big state tables (EQUO's 837 band cells, the delay banks' 200+ controls): every own parameter, and an even
  1264. // sample of the state controls up to opt.maxControls
  1265. if ((int) controls.size() > opt.maxControls)
  1266. {
  1267. std::vector<Control> own, states, keep;
  1268. for (auto& c : controls)
  1269. (c.state ? states : own).push_back (std::move (c));
  1270. const int room = std::max (8, opt.maxControls - (int) own.size());
  1271. keep = std::move (own);
  1272. for (int k = 0; k < room && ! states.empty(); ++k)
  1273. {
  1274. const auto at = (std::size_t) ((double) k * (double) states.size() / room);
  1275. if (at < states.size() && (keep.empty() || keep.back().param != states[at].param))
  1276. keep.push_back (states[at]);
  1277. }
  1278. controls = std::move (keep);
  1279. }
  1280. const auto base = run (slot0, slot0, prog);
  1281. rep.noise = audioDiffDb (run (slot0, slot0, prog), base);
  1282. rep.noise = std::max ({ rep.noise, relDiffDb (run (slot0, slot0, prog).ctl, base.ctl, 1e-6) });
  1283. rep.peak = peakOf (base);
  1284. const bool sound = ! isNoSound (slot0.kind);
  1285. auto check = [&rep, sound, &prog] (const Output& o, const std::string& ctl, const std::string& text, double value, bool full) {
  1286. if (! o.finite)
  1287. rep.broken.push_back ({ ctl, text, "NaN", value });
  1288. if (! full || ! o.finite)
  1289. return;
  1290. double sendPeak = 0.0;
  1291. for (const auto v : o.send)
  1292. sendPeak = std::max (sendPeak, (double) std::abs (v));
  1293. const auto p = peakOf (o);
  1294. if (sound && p < 1e-5 && sendPeak < 1e-5)
  1295. rep.broken.push_back ({ ctl, text, "silent", p });
  1296. if (p > 2.0)
  1297. rep.broken.push_back ({ ctl, text, "clip", 20.0 * std::log10 (p) });
  1298. // (the programme's own mean, a kick's asymmetric decay, is not an offset the effect made)
  1299. double inL = 0.0, inR = 0.0;
  1300. for (int i = 0; i < prog.frames(); ++i)
  1301. inL += prog.l[(std::size_t) i], inR += prog.r[(std::size_t) i];
  1302. const auto inDc = std::max (std::abs (inL), std::abs (inR)) / std::max (1, prog.frames());
  1303. if (dcOf (o) > inDc + 0.02)
  1304. rep.broken.push_back ({ ctl, text, "dc", dcOf (o) });
  1305. };
  1306. check (base, "(default)", "", 0.0, true);
  1307. const auto thr = threshold (rep.noise);
  1308. for (const auto& c : controls)
  1309. {
  1310. if (! opt.only.empty() && std::find (opt.only.begin(), opt.only.end(), c.name) == opt.only.end())
  1311. continue;
  1312. ControlResult res;
  1313. res.control = c;
  1314. // as the editor shows it: its conditions met, its idle switch off
  1315. auto ctx = slot0;
  1316. auto satisfy = [&controls, &ctx] (const Condition& k, bool inside) {
  1317. if (k.holds (valueOf (ctx, k.param)) == inside)
  1318. return;
  1319. const Control* owner = nullptr;
  1320. for (const auto& o : controls)
  1321. if (o.param == k.param)
  1322. owner = &o;
  1323. std::vector<float> cands = owner != nullptr ? owner->values : std::vector<float> {};
  1324. for (float v = 0.0f; v <= 1.0001f; v += 0.125f)
  1325. cands.push_back (std::min (v, 1.0f));
  1326. for (const auto v : cands)
  1327. if (k.holds (v) == inside)
  1328. {
  1329. setValue (ctx, k.param, v);
  1330. return;
  1331. }
  1332. };
  1333. for (const auto& k : c.shownWhen)
  1334. satisfy (k, true);
  1335. for (const auto& k : c.idleWhen)
  1336. satisfy (k, false);
  1337. const bool contextual = ctx.params != slot0.params || ctx.state != slot0.state;
  1338. const auto cbase = contextual ? run (ctx, ctx, prog) : base;
  1339. const auto current = valueOf (ctx, c.param);
  1340. for (const auto v : valuesToTest (c, current, opt.quick))
  1341. {
  1342. auto played = ctx;
  1343. setValue (played, c.param, v);
  1344. const auto text = textOf (ctx, c.param, v);
  1345. const auto out = variant (ctx, played, c, prog);
  1346. auto ch = measure (out, cbase, v, text);
  1347. res.best = std::max (res.best, moveOf (ch));
  1348. res.changes.push_back (ch);
  1349. check (out, c.name, text, v, c.type != Type::continuous && ! c.stepped);
  1350. if (opt.clicks && c.type != Type::continuous && ! c.stepped && ! c.momentary && ! c.state && out.finite)
  1351. {
  1352. // switched while the pad plays: the step at the switch against both modes' own steps there
  1353. RunOptions ro;
  1354. ro.switchBlock = prog.padMiddle() / blockSize;
  1355. ro.after = &played;
  1356. const auto sw = run (ctx, ctx, prog, ro);
  1357. const int at = ro.switchBlock * blockSize + std::max (0, out.latency);
  1358. const double jump = maxStep (sw, at - 64, at + 512);
  1359. const double own = std::max (maxStep (cbase, at - 2048, at + 2048), maxStep (out, at - 2048, at + 2048));
  1360. if (jump > 0.05 && jump > 3.0 * own)
  1361. rep.broken.push_back ({ c.name, text, "click", 20.0 * std::log10 (jump / std::max (own, 1e-9)) });
  1362. }
  1363. }
  1364. if (! c.shownWhen.empty() || ! c.idleWhen.empty())
  1365. if (contextual && res.best > thr)
  1366. res.status = Status::dependent, res.prerequisite = "as the editor shows it";
  1367. if (res.best <= thr && nonAudio (c.name))
  1368. res.status = Status::nonAudio;
  1369. else if (res.best <= thr)
  1370. {
  1371. // a new instance (the setting may be read only when the effect is built)
  1372. for (const auto v : valuesToTest (c, current, opt.quick))
  1373. {
  1374. auto played = ctx;
  1375. setValue (played, c.param, v);
  1376. if (moveOf (measure (run (played, played, prog), cbase, v, "")) > thr)
  1377. res.onlyNewInstance = true;
  1378. }
  1379. // the prerequisites to try, in order: the one a previous survey found (opt.known), side-chain inputs, a
  1380. // later song position (tempo-locked patterns longer than the programme), the kind's hints, one other
  1381. // control alone (each option of the choices and toggles, the ends of the knobs), random settings
  1382. std::vector<std::string> keys;
  1383. if (opt.known)
  1384. if (auto k = opt.known (rep.name, c.name); ! k.empty())
  1385. keys.push_back (k);
  1386. if (! opt.inert || ! opt.inert (rep.name, c.name))
  1387. {
  1388. keys.push_back ("sc");
  1389. keys.push_back ("hot");
  1390. keys.push_back ("bpm:90");
  1391. for (const int beat : { 2, 4, 8, 16, 32, 64 })
  1392. keys.push_back ("beat:" + std::to_string (beat));
  1393. const auto hints = opt.hints.empty() ? hintsFor (slot0.kind) : opt.hints;
  1394. for (std::size_t h = 0; h < hints.size(); ++h)
  1395. keys.push_back ("hint:" + std::to_string (h));
  1396. // switches and modes first, and the controls of the same group ("B3 …", "Unit 2 …", "Delay …") before
  1397. // the others
  1398. auto group = [] (const std::string& n) { return n.substr (0, n.find (' ')); };
  1399. std::vector<const Control*> order;
  1400. for (const auto& o : allControls)
  1401. if (o.param != c.param)
  1402. order.push_back (&o);
  1403. std::stable_sort (order.begin(), order.end(), [&] (const Control* a, const Control* b) {
  1404. const auto ka = (group (a->name) == group (c.name) ? 0 : 2) + (a->type == Type::continuous ? 1 : 0);
  1405. const auto kb = (group (b->name) == group (c.name) ? 0 : 2) + (b->type == Type::continuous ? 1 : 0);
  1406. return ka < kb;
  1407. });
  1408. int budget = opt.searchBudget;
  1409. for (const auto* op : order)
  1410. {
  1411. const auto& o = *op;
  1412. for (const auto v : o.type == Type::continuous ? std::vector<float> { 0.0f, 1.0f } : o.values)
  1413. if (std::abs (v - valueOf (ctx, o.param)) >= 1e-6f && budget-- > 0)
  1414. {
  1415. char b[64];
  1416. std::snprintf (b, sizeof b, "set:%d:%.9g", o.param, (double) v);
  1417. keys.push_back (b);
  1418. }
  1419. }
  1420. int moves = opt.searchBudget / 2;
  1421. for (const auto& o : controls)
  1422. if (o.param != c.param && o.type == Type::continuous && ! o.state && moves-- > 0)
  1423. {
  1424. char b[64];
  1425. std::snprintf (b, sizeof b, "move:%d:%.9g", o.param, valueOf (ctx, o.param) > 0.5f ? 0.0 : 1.0);
  1426. keys.push_back (b);
  1427. }
  1428. for (int k = 0; k < opt.contexts; ++k)
  1429. keys.push_back ("rand:" + std::to_string (k));
  1430. }
  1431. for (const auto& key : keys)
  1432. {
  1433. RunOptions ro;
  1434. std::string why;
  1435. const auto from = prerequisite (key, ctx, allControls, c, ro, why);
  1436. if (! from)
  1437. continue;
  1438. const auto moved = bestMove (*from, c, prog, ro, thr, true);
  1439. if (std::getenv ("LYNX_FXAUDIT_DEBUG") != nullptr)
  1440. std::printf (" [%s] %s: %s → %.0f dB\n", c.name.c_str(), key.c_str(), why.c_str(), moved);
  1441. if (moved > thr)
  1442. {
  1443. res.status = Status::dependent, res.prerequisite = why, res.prereqKey = key;
  1444. // report the move measured in the prerequisite's context (every value), not the default one's
  1445. res.bestDefault = res.best;
  1446. std::vector<Change> there;
  1447. res.best = bestMove (*from, c, prog, ro, thr, opt.quick, &there);
  1448. res.changes = std::move (there);
  1449. break;
  1450. }
  1451. }
  1452. if (res.status != Status::dependent)
  1453. res.status = Status::dead;
  1454. }
  1455. rep.controls.push_back (std::move (res));
  1456. }
  1457. return rep;
  1458. }
  1459. inline const char* statusText (Status s)
  1460. {
  1461. return s == Status::ok ? "ok" : s == Status::dependent ? "dependent-ok" : s == Status::nonAudio ? "non-audio" : "DEAD";
  1462. }
  1463. } // namespace lynx::fxaudit