MixerOps.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  1. #include "core/MixerOps.h"
  2. #include "core/ClipOps.h"
  3. #include "core/FlpExport.h"
  4. #include "core/RackOps.h"
  5. #include "core/Transmitter.h"
  6. #include "core/SoundFont.h"
  7. #include "core/TransporterFl.h"
  8. #include "core/VoltageController.h"
  9. #include <algorithm>
  10. #include <cmath>
  11. #include <functional>
  12. #include <numeric>
  13. namespace lynx::mixerops
  14. {
  15. void soloSelected (Song& song, const std::vector<int>& targets, bool alt)
  16. {
  17. std::vector<int> t;
  18. for (auto x : targets)
  19. if (x > 0 && x < (int) song.mixer.size())
  20. t.push_back (x);
  21. if (t.empty())
  22. return;
  23. auto fed = [&] (int to) {
  24. for (int src = 1; src < (int) song.mixer.size(); ++src)
  25. if (src != to && song.routes (src, to))
  26. return true;
  27. return false;
  28. };
  29. // FL: "soloed" = every target soloed; "alt state" = every target soloed with its sources heard (no source: both)
  30. auto soloed = true, altState = true, anySources = false;
  31. for (auto x : t)
  32. {
  33. const auto& m = song.mixer[(std::size_t) x];
  34. const auto sources = fed (x);
  35. anySources = anySources || sources;
  36. soloed = soloed && m.solo;
  37. altState = altState && m.solo && (m.soloSources || ! sources);
  38. }
  39. bool solo;
  40. if (alt)
  41. solo = ! (soloed && altState);
  42. else
  43. solo = (altState && anySources) || ! soloed;
  44. for (std::size_t i = 1; i < song.mixer.size(); ++i)
  45. {
  46. const auto on = solo && std::find (t.begin(), t.end(), (int) i) != t.end();
  47. song.mixer[i].solo = on;
  48. if (on)
  49. song.mixer[i].soloSources = alt;
  50. }
  51. }
  52. namespace
  53. {
  54. bool isInsert (const Song& song, int t) noexcept
  55. {
  56. return t > 0 && t < (int) song.mixer.size();
  57. }
  58. // every effect slot, also inside containers and Patchers (a Fruity Send in a Patcher, a nested Patcher...)
  59. template <typename Fn>
  60. void forEachSlot (EffectSlot& slot, Fn& fn)
  61. {
  62. fn (slot);
  63. for (auto& branch : slot.branches)
  64. for (auto& child : branch.chain)
  65. forEachSlot (child, fn);
  66. for (auto& node : slot.patcher.nodes)
  67. if (! node.generator)
  68. forEachSlot (node.effect, fn);
  69. }
  70. bool isTrackTarget (AutomationTargetKind k) noexcept
  71. {
  72. return k == AutomationTargetKind::mixerVolume || k == AutomationTargetKind::mixerPan || k == AutomationTargetKind::effectParam
  73. || k == AutomationTargetKind::effectMix || k == AutomationTargetKind::mixerSend;
  74. }
  75. // A "send to insert" parameter stored as insert / (maxMixerTracks - 1) (Fruity Send's "Send to", the Transporter's
  76. // loops send; 0 = none)
  77. void remapTrackParam (float& value, const std::function<int (int)>& map)
  78. {
  79. const auto track = (int) std::lround (std::clamp ((double) value, 0.0, 1.0) * (maxMixerTracks - 1));
  80. if (track <= 0)
  81. return;
  82. const auto to = map (track);
  83. value = to > 0 ? (float) to / (float) (maxMixerTracks - 1) : 0.0f;
  84. }
  85. // The extra outputs of a generator Patcher (doc.outputTracks: an insert, or -1 = "the k-th insert after the channel's
  86. // own", FL's relative routing). A chosen insert follows its track; a relative output that would land on another insert
  87. // after the move is pinned to the one it fed (the logical track), one that still lands right stays relative.
  88. // `base` = the channel's insert before the move.
  89. void remapPatcherOutputs (ChannelDef& channel, int base, const std::function<int (int)>& map)
  90. {
  91. auto& doc = channel.patcher;
  92. int outputs = (int) doc.outputTracks.size();
  93. for (const auto& w : doc.wires)
  94. if (w.kind == PatchWire::Kind::audio && w.to == patchToHost)
  95. outputs = std::max (outputs, w.toPort);
  96. const auto newBase = std::max (0, map (base));
  97. for (int k = 1; k <= outputs; ++k)
  98. {
  99. if (k <= (int) doc.outputTracks.size() && doc.outputTracks[(std::size_t) k - 1] >= 0)
  100. {
  101. auto& t = doc.outputTracks[(std::size_t) k - 1];
  102. if (t > 0)
  103. t = std::max (0, map (t)); // (its insert deleted: the Master, as a channel's)
  104. continue;
  105. }
  106. const auto now = std::max (0, map (std::min (base + k, maxMixerTracks - 1)));
  107. if (now == std::min (newBase + k, maxMixerTracks - 1))
  108. continue;
  109. if ((int) doc.outputTracks.size() < k)
  110. doc.outputTracks.resize ((std::size_t) k, -1);
  111. doc.outputTracks[(std::size_t) k - 1] = now;
  112. }
  113. }
  114. // An effect / instrument imported from FL keeps FL's own state for the export (FlOrigin, exported while its fingerprint
  115. // matches). The insert numbers that state holds are rewritten with the remap, so a moved track still exports to the
  116. // track it was linked to and the rest of the state stays byte for byte (round 177a).
  117. void putI32 (std::vector<std::uint8_t>& d, std::size_t at, int v)
  118. {
  119. if (at + 4 <= d.size())
  120. for (int b = 0; b < 4; ++b)
  121. d[at + (std::size_t) b] = (std::uint8_t) (((std::uint32_t) v >> (8 * b)) & 0xFF);
  122. }
  123. void patchOriginTracks (EffectSlot& slot)
  124. {
  125. auto& d = slot.flOrigin.state;
  126. if (slot.kind == EffectKind::fruitySend && slot.flOrigin.plugin == "Fruity Send") // pan, vol, dry, insert (REA_rodada105_send §1)
  127. putI32 (d, 12, std::max (0, fruitySendTarget (slot)));
  128. else if (slot.kind == EffectKind::transmitter && slot.flOrigin.plugin == "Transmitter") // version, 42 controls, the two sends
  129. {
  130. putI32 (d, 4 + 4 * (std::size_t) transmitter::count, slot.sendTracks[0]);
  131. putI32 (d, 8 + 4 * (std::size_t) transmitter::count, slot.sendTracks[1]);
  132. }
  133. else if (slot.kind == EffectKind::flTransporter && slot.flOrigin.plugin == "Transporter" && d.size() >= 4)
  134. {
  135. // version, 20 / 22 controls, two flag bytes, source, stereo, send track, input track (REA_rodada167f)
  136. const auto version = (int) ((std::uint32_t) d[0] | (std::uint32_t) d[1] << 8 | (std::uint32_t) d[2] << 16 | (std::uint32_t) d[3] << 24);
  137. const std::size_t controls = version >= 8 ? 22 : 20;
  138. putI32 (d, 4 + 4 * controls + 2 + 8, std::max (0, fruitySendTarget (slot)));
  139. }
  140. }
  141. void patchOriginTracks (ChannelDef& c)
  142. {
  143. // FL's Soundfont Player: version, 13 controls, the font path (length byte, or 0xFF + i32), then the reverb and
  144. // chorus "send to" tracks (insert − 1, −1 = none), each followed by a flag byte (REA_rodada158 §1)
  145. if (c.instrument != InstrumentKind::soundfont || c.flOrigin.plugin != "Fruity Soundfont Player")
  146. return;
  147. auto& d = c.flOrigin.state;
  148. std::size_t at = 4 + 52;
  149. if (at >= d.size())
  150. return;
  151. std::size_t len = d[at++];
  152. if (len == 0xFF)
  153. {
  154. if (at + 4 > d.size())
  155. return;
  156. len = (std::size_t) ((std::uint32_t) d[at] | (std::uint32_t) d[at + 1] << 8 | (std::uint32_t) d[at + 2] << 16 | (std::uint32_t) d[at + 3] << 24);
  157. at += 4;
  158. }
  159. at += len;
  160. const auto fl = [&c] (int p) {
  161. const auto v = (int) std::lround (c.modelParams[(std::size_t) p]);
  162. return v > 0 ? v - 1 : -1;
  163. };
  164. if ((std::size_t) sf::pChorusTrack < c.modelParams.size())
  165. {
  166. putI32 (d, at, fl (sf::pReverbTrack));
  167. putI32 (d, at + 5, fl (sf::pChorusTrack));
  168. }
  169. }
  170. template <typename T>
  171. bool originMatches (const T& x)
  172. {
  173. return ! x.flOrigin.empty() && x.flOrigin.fingerprint == flpexport::fingerprint (x);
  174. }
  175. } // namespace
  176. namespace
  177. {
  178. // Every reference to an insert through `map` (old insert -> its new number, or -1 when the insert was deleted), so a
  179. // move, an insert or a delete never re-wires anything (round 177a): channels, Playlist tracks, sends, side-chains,
  180. // slot sends (Transmitter, Fruity Send, Transporter, a Patcher's "To LYNX" outputs, also inside containers and
  181. // Patchers), plugins' own "send to insert" settings (Soundfont reverb / chorus, Voltage Controller outputs),
  182. // automation, links, initial values, pattern events and controller sources. FL keeps the mixer's own routes,
  183. // channels, Playlist tracks and automation / links (FUN_012a4030, FUN_012cb6c0) but leaves the plugin-side numbers
  184. // alone, so its Fruity Send / Patcher outputs end on another track; LYNX keeps those too (REA_rodada177a §1).
  185. // A reference to a deleted insert is dropped: sends removed, side-chains and slot sends cleared, channels to the
  186. // Master, automation / links without a target (FL marks them -1 too).
  187. void remapTrackReferences (Song& song, const std::function<int (int)>& map)
  188. {
  189. for (auto& c : song.channels)
  190. {
  191. const auto exportsOrigin = originMatches (c);
  192. if (c.instrument == InstrumentKind::patcher)
  193. remapPatcherOutputs (c, c.mixerTrack, map);
  194. c.mixerTrack = std::max (0, map (c.mixerTrack));
  195. if (c.instrument == InstrumentKind::soundfont)
  196. for (const auto p : { sf::pReverbTrack, sf::pChorusTrack })
  197. if ((std::size_t) p < c.modelParams.size() && c.modelParams[(std::size_t) p] > 0.5f)
  198. c.modelParams[(std::size_t) p] = (float) map ((int) std::lround (c.modelParams[(std::size_t) p]));
  199. if (c.instrument == InstrumentKind::voltage)
  200. if (auto vs = voltage::parse (c.pluginState))
  201. {
  202. auto changed = false;
  203. for (auto& t : vs->tracks)
  204. if (t > 0)
  205. {
  206. const auto to = std::max (0, map (t));
  207. changed = changed || to != t;
  208. t = to;
  209. }
  210. if (changed)
  211. c.pluginState = voltage::write (*vs);
  212. }
  213. if (exportsOrigin && flpexport::fingerprint (c) != c.flOrigin.fingerprint)
  214. {
  215. patchOriginTracks (c);
  216. if (c.instrument == InstrumentKind::voltage) // (its state is FL's own: the export writes it)
  217. c.flOrigin.state = c.pluginState;
  218. c.flOrigin.fingerprint = flpexport::fingerprint (c);
  219. }
  220. }
  221. for (auto& vca : song.vcas) // VCA members (round 177a slice 3)
  222. {
  223. for (auto& m : vca.members)
  224. m = map (m);
  225. vca.members.erase (std::remove_if (vca.members.begin(), vca.members.end(), [] (int m) { return m <= 0; }), vca.members.end());
  226. std::sort (vca.members.begin(), vca.members.end());
  227. }
  228. for (auto& pt : song.playlistTracks) // audio / instrument tracks (round 146a)
  229. if (pt.mixerTrack > 0)
  230. {
  231. pt.mixerTrack = map (pt.mixerTrack);
  232. if (pt.mixerTrack < 0 && pt.mode == PlaylistTrackMode::audio) // its insert deleted: the link goes
  233. pt.mode = PlaylistTrackMode::unassigned;
  234. }
  235. for (auto& track : song.mixer)
  236. {
  237. for (auto& send : track.sends)
  238. send.target = map (send.target);
  239. track.sends.erase (std::remove_if (track.sends.begin(), track.sends.end(), [] (const MixerSend& s) { return s.target <= 0; }),
  240. track.sends.end());
  241. auto fix = [&map] (EffectSlot& slot) {
  242. const auto exportsOrigin = originMatches (slot);
  243. for (auto* source : { &slot.sidechain, &slot.sidechain2 })
  244. if (*source > 0)
  245. *source = std::max (-1, map (*source));
  246. for (auto& send : slot.sendTracks)
  247. if (send > 0)
  248. send = std::max (0, map (send));
  249. for (auto& t : slot.patcher.outputTracks)
  250. if (t > 0)
  251. t = std::max (0, map (t));
  252. if (slot.kind == EffectKind::fruitySend)
  253. remapTrackParam (slot.params[3], map);
  254. else if (slot.kind == EffectKind::flTransporter)
  255. remapTrackParam (slot.params[(std::size_t) transporter::sendParam], map);
  256. if (exportsOrigin)
  257. {
  258. patchOriginTracks (slot); // (unchanged numbers write the same bytes)
  259. slot.flOrigin.fingerprint = flpexport::fingerprint (slot);
  260. }
  261. };
  262. for (auto& slot : track.effects)
  263. forEachSlot (slot, fix);
  264. }
  265. song.forEachTarget ([&map] (AutomationTarget& t) {
  266. if (! isTrackTarget (t.kind))
  267. return;
  268. t.index = map (t.index);
  269. if (t.kind == AutomationTargetKind::mixerSend && t.slot > 0) // (the route's destination)
  270. t.slot = map (t.slot);
  271. if (t.index < 0 || t.slot < 0)
  272. t = AutomationTarget {}; // its insert deleted: no target
  273. });
  274. song.removeDeadMidiLinks();
  275. for (auto& automation : song.automations)
  276. if (automation.source.track > 0)
  277. automation.source.track = map (automation.source.track); // -1 = no longer live
  278. }
  279. } // namespace
  280. void swapTracks (Song& song, int a, int b)
  281. {
  282. if (a == b || ! isInsert (song, a) || ! isInsert (song, b))
  283. return;
  284. std::swap (song.mixer[(std::size_t) a], song.mixer[(std::size_t) b]);
  285. // a default name follows the position, as in FL (its unnamed tracks show "Insert <number>"): moving insert 16 to
  286. // 8 shows "Insert 8" there, not "Insert 16" (issue #47 screenshot, round 168h)
  287. for (const auto [now, was] : { std::pair { a, b }, std::pair { b, a } })
  288. if (auto& name = song.mixer[(std::size_t) now].name; name == "Insert " + std::to_string (was))
  289. name = "Insert " + std::to_string (now);
  290. remapTrackReferences (song, [a, b] (int t) { return t == a ? b : t == b ? a : t; });
  291. }
  292. bool moveTracks (Song& song, std::vector<int>& selection, int delta)
  293. {
  294. const auto count = (int) song.mixer.size();
  295. std::sort (selection.begin(), selection.end());
  296. selection.erase (std::unique (selection.begin(), selection.end()), selection.end());
  297. selection.erase (std::remove_if (selection.begin(), selection.end(), [count] (int t) { return t <= 0 || t >= count; }), selection.end());
  298. if (selection.empty() || delta == 0 || (delta < 0 && selection.front() == 1) || (delta > 0 && selection.back() == count - 1))
  299. return false;
  300. if (delta < 0)
  301. {
  302. for (auto& t : selection) // left to right: each swaps with its left neighbour
  303. {
  304. swapTracks (song, t, t - 1);
  305. --t;
  306. }
  307. }
  308. else
  309. {
  310. for (auto it = selection.rbegin(); it != selection.rend(); ++it)
  311. {
  312. swapTracks (song, *it, *it + 1);
  313. ++*it;
  314. }
  315. }
  316. return true;
  317. }
  318. namespace
  319. {
  320. // default names follow their insert's number (after an insert or a delete moved them)
  321. void renumberDefaultNames (Song& song, int from, int delta)
  322. {
  323. for (int t = std::max (1, from); t < (int) song.mixer.size(); ++t)
  324. {
  325. auto& name = song.mixer[(std::size_t) t].name;
  326. const auto old = t - delta;
  327. if (old >= 1 && name == "Insert " + std::to_string (old))
  328. name = "Insert " + std::to_string (t);
  329. }
  330. }
  331. } // namespace
  332. bool insertTrack (Song& song, int at)
  333. {
  334. return insertTracks (song, at, 1) == 1;
  335. }
  336. std::vector<bool> hiddenUnusedInserts (const Song& song, const std::set<int>& keep)
  337. {
  338. const auto tracks = (int) song.mixer.size();
  339. std::vector<bool> hidden ((std::size_t) tracks + 1, false);
  340. for (int t = 1; t < tracks; ++t)
  341. hidden[(std::size_t) t] = keep.count (t) == 0 && ! song.insertInUse (t);
  342. return hidden;
  343. }
  344. int removeUnusedTrailingInserts (Song& song, int keep)
  345. {
  346. int removed = 0;
  347. while ((int) song.mixer.size() - 1 > std::max (0, keep) && ! song.insertInUse ((int) song.mixer.size() - 1))
  348. {
  349. if (! deleteTrack (song, (int) song.mixer.size() - 1)) // clears it and drops the links to it (the count stays)
  350. break;
  351. song.mixer.pop_back();
  352. ++removed;
  353. }
  354. return removed;
  355. }
  356. bool deleteTrack (Song& song, int t)
  357. {
  358. const auto count = (int) song.mixer.size();
  359. if (t < 1 || t >= count)
  360. return false;
  361. // one shift of every reference: the deleted insert's links go, the following inserts move one place left and a
  362. // blank insert takes the last place (the count stays)
  363. remapTrackReferences (song, [t] (int x) { return x == t ? -1 : x > t ? x - 1 : x; });
  364. song.mixer.erase (song.mixer.begin() + t);
  365. song.mixer.emplace_back();
  366. renumberDefaultNames (song, t, -1);
  367. song.mixer.back().name = "Insert " + std::to_string (count - 1);
  368. return true;
  369. }
  370. bool reconnectLostSend (Song& song, int track, int index, int to)
  371. {
  372. if (! isInsert (song, track) || index < 0 || index >= (int) song.mixer[(std::size_t) track].lostSends.size() || to < 1
  373. || to >= (int) song.mixer.size() || to == track || (! song.routes (track, to) && ! song.canRoute (track, to)))
  374. return false;
  375. auto& t = song.mixer[(std::size_t) track];
  376. const auto lost = t.lostSends[(std::size_t) index];
  377. t.lostSends.erase (t.lostSends.begin() + index);
  378. if (const auto it = std::find_if (t.sends.begin(), t.sends.end(), [to] (const MixerSend& s) { return s.target == to; }); it != t.sends.end())
  379. it->level = lost.level;
  380. else
  381. t.sends.push_back ({ to, lost.level });
  382. return true;
  383. }
  384. void removeLostSends (Song& song, int track, int index)
  385. {
  386. if (! isInsert (song, track))
  387. return;
  388. auto& lost = song.mixer[(std::size_t) track].lostSends;
  389. if (index < 0)
  390. lost.clear();
  391. else if (index < (int) lost.size())
  392. lost.erase (lost.begin() + index);
  393. }
  394. std::vector<int> playlistTracksLinkedTo (const Song& song, const std::vector<int>& tracks)
  395. {
  396. const auto among = [&tracks] (int t) { return t > 0 && std::find (tracks.begin(), tracks.end(), t) != tracks.end(); };
  397. std::vector<int> result;
  398. for (int i = 0; i < (int) song.playlistTracks.size(); ++i)
  399. {
  400. const auto& pt = song.playlistTracks[(std::size_t) i];
  401. auto insert = -1;
  402. if (pt.mode == PlaylistTrackMode::audio)
  403. insert = pt.mixerTrack;
  404. else if (pt.mode == PlaylistTrackMode::instrument) // (FL: the channel's own mixer track)
  405. insert = pt.channel >= 0 && pt.channel < (int) song.channels.size() ? song.channels[(std::size_t) pt.channel].mixerTrack : pt.mixerTrack;
  406. if (among (insert))
  407. result.push_back (i);
  408. }
  409. return result;
  410. }
  411. namespace
  412. {
  413. // FL 21 (fix 19603): the tracks that sent into `t` send to `t`'s destinations instead (at their send's level)
  414. void rerouteFeeders (Song& song, int t)
  415. {
  416. const auto& gone = song.mixer[(std::size_t) t];
  417. const auto destinations = gone.sends;
  418. const auto toMaster = gone.toMaster;
  419. for (int x = 1; x < (int) song.mixer.size(); ++x)
  420. {
  421. if (x == t)
  422. continue;
  423. auto& track = song.mixer[(std::size_t) x];
  424. const auto it = std::find_if (track.sends.begin(), track.sends.end(), [t] (const MixerSend& s) { return s.target == t; });
  425. if (it == track.sends.end() || it->level <= 0.0f) // (a level-0 sidechain link carries no signal)
  426. continue;
  427. const auto level = it->level;
  428. if (toMaster)
  429. track.toMaster = true;
  430. for (const auto& d : destinations)
  431. if (d.target != x && d.target != t
  432. && std::none_of (track.sends.begin(), track.sends.end(), [&d] (const MixerSend& s) { return s.target == d.target; }))
  433. track.sends.push_back ({ d.target, level });
  434. }
  435. }
  436. } // namespace
  437. int deleteTracks (Song& song, std::vector<int> tracks, LinkedPlaylistTracks linked, bool keepLostLinks)
  438. {
  439. std::sort (tracks.begin(), tracks.end());
  440. tracks.erase (std::unique (tracks.begin(), tracks.end()), tracks.end());
  441. tracks.erase (std::remove_if (tracks.begin(), tracks.end(), [&song] (int t) { return t < 1 || t >= (int) song.mixer.size(); }), tracks.end());
  442. if (tracks.empty())
  443. return 0;
  444. const auto playlist = playlistTracksLinkedTo (song, tracks);
  445. std::vector<int> channels; // instrument tracks' channels, for "Delete tracks and channels"
  446. for (const auto i : playlist)
  447. {
  448. auto& pt = song.playlistTracks[(std::size_t) i];
  449. if (linked == LinkedPlaylistTracks::deleteToo && pt.mode == PlaylistTrackMode::instrument && pt.channel >= 0
  450. && pt.channel < (int) song.channels.size())
  451. channels.push_back (pt.channel);
  452. if (linked == LinkedPlaylistTracks::deleteToo) // FL resets the track: "Track N", unassigned
  453. {
  454. pt.name.clear();
  455. pt.colour = 0;
  456. pt.icon = 0;
  457. }
  458. pt.mode = PlaylistTrackMode::unassigned; // (FL "Unlink": the track keeps its name)
  459. pt.mixerTrack = -1;
  460. pt.channel = -1;
  461. }
  462. int deleted = 0;
  463. for (auto it = tracks.rbegin(); it != tracks.rend(); ++it) // from the right: the numbers still to delete stay valid
  464. {
  465. if (keepLostLinks) // (round 186c's option) each send into it stays, greyed, under the deleted insert's name
  466. {
  467. const auto name = song.mixer[(std::size_t) *it].name.empty() ? "Insert " + std::to_string (*it) : song.mixer[(std::size_t) *it].name;
  468. for (int x = 1; x < (int) song.mixer.size(); ++x)
  469. for (const auto& send : song.mixer[(std::size_t) x].sends)
  470. if (x != *it && send.target == *it)
  471. song.mixer[(std::size_t) x].lostSends.push_back ({ name, send.level });
  472. }
  473. rerouteFeeders (song, *it);
  474. if (deleteTrack (song, *it))
  475. ++deleted;
  476. }
  477. if (! channels.empty() && channels.size() < song.channels.size()) // (at least one channel stays, as in the Channel rack)
  478. removeChannels (song, channels);
  479. return deleted;
  480. }
  481. std::vector<int> linkChannels (Song& song, const std::vector<int>& channels, int track, bool startingHere)
  482. {
  483. std::vector<int> result;
  484. if (song.mixer.empty())
  485. song.mixer = Song::defaultMixer (0);
  486. track = std::clamp (track, 0, (int) song.mixer.size() - 1);
  487. auto next = track;
  488. for (auto channel : channels)
  489. {
  490. if (channel < 0 || channel >= (int) song.channels.size())
  491. {
  492. result.push_back (0);
  493. continue;
  494. }
  495. while (next >= (int) song.mixer.size() && (int) song.mixer.size() < maxMixerTracks)
  496. {
  497. auto& added = song.mixer.emplace_back();
  498. added.name = "Insert " + std::to_string (song.mixer.size() - 1);
  499. }
  500. const auto target = std::min (next, (int) song.mixer.size() - 1);
  501. song.channels[(std::size_t) channel].mixerTrack = target;
  502. result.push_back (target);
  503. if (startingHere && track > 0)
  504. ++next;
  505. }
  506. return result;
  507. }
  508. int ensureInsert (Song& song, int track)
  509. {
  510. track = std::clamp (track, 0, maxMixerTracks - 1);
  511. if (song.mixer.empty())
  512. song.mixer = Song::defaultMixer (0);
  513. while ((int) song.mixer.size() <= track)
  514. {
  515. const auto name = "Insert " + std::to_string (song.mixer.size());
  516. song.mixer.emplace_back().name = name;
  517. }
  518. return track;
  519. }
  520. void routeChannel (Song& song, int channel, int track)
  521. {
  522. if (channel < 0 || channel >= (int) song.channels.size())
  523. return;
  524. song.channels[(std::size_t) channel].mixerTrack = ensureInsert (song, track);
  525. }
  526. std::vector<int> channelsOn (const Song& song, int track)
  527. {
  528. std::vector<int> out;
  529. for (int c = 0; c < (int) song.channels.size(); ++c)
  530. if (song.channels[(std::size_t) c].mixerTrack == track)
  531. out.push_back (c);
  532. return out;
  533. }
  534. int routeTo (Song& song, const std::vector<int>& sources, int to, bool only)
  535. {
  536. int routed = 0;
  537. for (auto from : sources)
  538. {
  539. if (! isInsert (song, from) || from == to || (to != 0 && ! song.canRoute (from, to) && ! song.routes (from, to)))
  540. continue;
  541. if (only)
  542. {
  543. auto& track = song.mixer[(std::size_t) from];
  544. track.sends.erase (std::remove_if (track.sends.begin(), track.sends.end(), [to] (const MixerSend& s) { return s.target != to; }),
  545. track.sends.end());
  546. track.toMaster = to == 0;
  547. }
  548. song.setRoute (from, to, true);
  549. ++routed;
  550. }
  551. return routed;
  552. }
  553. int sidechainTo (Song& song, int from, int to)
  554. {
  555. if (! isInsert (song, from) || ! isInsert (song, to) || from == to)
  556. return 0;
  557. if (! song.routes (from, to))
  558. {
  559. if (! song.canRoute (from, to))
  560. return 0;
  561. song.setRoute (from, to, true, 0.0f);
  562. }
  563. int linked = 0;
  564. for (auto& fx : song.mixer[(std::size_t) to].effects)
  565. if (listensToSidechain (fx.kind) && (fx.sidechain < 0 || fx.sidechain == from))
  566. {
  567. fx.sidechain = from;
  568. ++linked;
  569. }
  570. return linked;
  571. }
  572. bool listensToSidechain (EffectKind kind) noexcept
  573. {
  574. return kind == EffectKind::compressor || kind == EffectKind::fruityLimiter || kind == EffectKind::fruityCompressor
  575. || kind == EffectKind::vocoder;
  576. }
  577. bool reportsGainReduction (EffectKind kind) noexcept
  578. {
  579. switch (kind)
  580. {
  581. case EffectKind::compressor:
  582. case EffectKind::apexLimiter:
  583. case EffectKind::crestCompressor:
  584. case EffectKind::sentryGate:
  585. case EffectKind::quellDenoiser:
  586. case EffectKind::ridgeStrip:
  587. case EffectKind::glowLeveler:
  588. case EffectKind::hushDeEsser:
  589. return true;
  590. default:
  591. return false;
  592. }
  593. }
  594. int soleSidechainSender (const Song& song, int to)
  595. {
  596. if (! isInsert (song, to))
  597. return -1;
  598. int sender = -1;
  599. for (int t = 1; t < (int) song.mixer.size(); ++t)
  600. for (const auto& send : song.mixer[(std::size_t) t].sends)
  601. if (t != to && send.target == to && send.level <= 0.0f)
  602. {
  603. if (sender >= 0 && sender != t)
  604. return -1;
  605. sender = t;
  606. }
  607. return sender;
  608. }
  609. bool linkAddedSidechain (Song& song, int to, int slot)
  610. {
  611. if (! isInsert (song, to))
  612. return false;
  613. auto& effects = song.mixer[(std::size_t) to].effects;
  614. if (slot < 0 || slot >= (int) effects.size())
  615. return false;
  616. auto& fx = effects[(std::size_t) slot];
  617. // (dynamics only: a vocoder's side input is its modulator, a choice of its own)
  618. if (fx.kind == EffectKind::vocoder || ! listensToSidechain (fx.kind) || fx.sidechain >= 0)
  619. return false;
  620. const auto from = soleSidechainSender (song, to);
  621. if (from < 0)
  622. return false;
  623. fx.sidechain = from;
  624. return true;
  625. }
  626. bool acceptsSidechain (EffectKind kind) noexcept
  627. {
  628. switch (kind)
  629. {
  630. case EffectKind::compressor: case EffectKind::fruityLimiter: case EffectKind::fruityCompressor:
  631. case EffectKind::vocoder: case EffectKind::vocodex: case EffectKind::facetEq: case EffectKind::apexLimiter:
  632. case EffectKind::hushDeEsser: case EffectKind::crestCompressor: case EffectKind::prismMultiband:
  633. case EffectKind::sentryGate: case EffectKind::luxeVerb: case EffectKind::patcher: case EffectKind::transmitter:
  634. case EffectKind::flTransporter:
  635. return true;
  636. default:
  637. return false;
  638. }
  639. }
  640. SidechainPick sidechainFrom (Song& song, int to, int slot, int from, bool second)
  641. {
  642. if ((to != 0 && ! isInsert (song, to)) || song.mixer.empty() || slot < 0 || slot >= (int) song.mixer[(std::size_t) to].effects.size())
  643. return SidechainPick::invalid;
  644. if (from < 0)
  645. {
  646. auto& fx = song.mixer[(std::size_t) to].effects[(std::size_t) slot];
  647. (second ? fx.sidechain2 : fx.sidechain) = -1;
  648. return SidechainPick::cleared;
  649. }
  650. if (! isInsert (song, from) || from == to)
  651. return SidechainPick::invalid;
  652. // (the Master is processed after every insert: it needs no route)
  653. if (to != 0 && ! song.routes (from, to))
  654. {
  655. if (! song.canRoute (from, to))
  656. return SidechainPick::cycle;
  657. song.setRoute (from, to, true, 0.0f);
  658. }
  659. auto& fx = song.mixer[(std::size_t) to].effects[(std::size_t) slot];
  660. (second ? fx.sidechain2 : fx.sidechain) = from;
  661. if (fx.kind == EffectKind::vocoder)
  662. fx.params[16] = 1.0f; // separate inputs (otherwise L = modulator, R = carrier)
  663. return SidechainPick::linked;
  664. }
  665. namespace
  666. {
  667. // FL's "only": the source stops sending anywhere (its sends and its route to the Master)
  668. void clearRoutes (Song& song, int from)
  669. {
  670. auto& track = song.mixer[(std::size_t) from];
  671. track.sends.clear();
  672. track.toMaster = false;
  673. }
  674. bool routeOne (Song& song, int from, int to, RouteKind kind)
  675. {
  676. if (! isInsert (song, from) || from == to || to < 0 || to >= (int) song.mixer.size())
  677. return false;
  678. const auto sidechain = kind == RouteKind::sidechain || kind == RouteKind::sidechainOnly;
  679. if (sidechain && to == 0)
  680. return false; // the Master has no side input
  681. if (to != 0 && ! song.routes (from, to) && ! song.canRoute (from, to))
  682. return false;
  683. if (kind == RouteKind::routeOnly || kind == RouteKind::sidechainOnly)
  684. clearRoutes (song, from);
  685. if (sidechain)
  686. sidechainTo (song, from, to);
  687. else
  688. song.setRoute (from, to, true);
  689. return true;
  690. }
  691. } // namespace
  692. int routeSelectedTo (Song& song, const std::vector<int>& selected, int to, RouteKind kind)
  693. {
  694. int routed = 0;
  695. for (auto from : selected)
  696. if (from != to && routeOne (song, from, to, kind))
  697. ++routed;
  698. return routed;
  699. }
  700. int switchSelectedRoute (Song& song, const std::vector<int>& selected, int to, bool on)
  701. {
  702. int changed = 0;
  703. for (auto from : selected)
  704. {
  705. if (! isInsert (song, from) || from == to || to < 0 || to >= (int) song.mixer.size() || song.routes (from, to) == on)
  706. continue;
  707. if (on && to != 0 && ! song.canRoute (from, to))
  708. continue; // (a cycle)
  709. song.setRoute (from, to, on);
  710. if (on)
  711. song.setRouteLevel (from, to, 1.0f);
  712. ++changed;
  713. }
  714. return changed;
  715. }
  716. int routeToSelected (Song& song, int from, const std::vector<int>& selected, RouteKind kind)
  717. {
  718. if (! isInsert (song, from))
  719. return 0;
  720. if (kind == RouteKind::routeOnly || kind == RouteKind::sidechainOnly)
  721. {
  722. // (only once, before the first route: FL clears this track's routes, then adds each selected one)
  723. clearRoutes (song, from);
  724. kind = kind == RouteKind::routeOnly ? RouteKind::route : RouteKind::sidechain;
  725. }
  726. int routed = 0;
  727. for (auto to : selected)
  728. if (to != from && routeOne (song, from, to, kind))
  729. ++routed;
  730. if (routed == 0 && song.mixer[(std::size_t) from].sends.empty())
  731. song.mixer[(std::size_t) from].toMaster = true; // nothing could be routed: never leave it without an output
  732. return routed;
  733. }
  734. bool hasDefaultName (const Song& song, int track)
  735. {
  736. if (track < 0 || track >= (int) song.mixer.size())
  737. return false;
  738. const auto& name = song.mixer[(std::size_t) track].name;
  739. return name.empty() || name == "Insert " + std::to_string (track);
  740. }
  741. int createSubmix (Song& song, const std::vector<int>& selected, int to, const std::string& name)
  742. {
  743. if (! isInsert (song, to))
  744. return 0;
  745. std::vector<int> routed;
  746. for (auto from : selected)
  747. if (from != to && routeOne (song, from, to, RouteKind::routeOnly))
  748. routed.push_back (from);
  749. if (routed.empty())
  750. return 0;
  751. const auto colour = song.mixer[(std::size_t) to].colour;
  752. for (auto t : routed)
  753. {
  754. song.mixer[(std::size_t) t].colour = colour;
  755. if (! name.empty() && hasDefaultName (song, t))
  756. song.mixer[(std::size_t) t].name = name;
  757. }
  758. if (! name.empty())
  759. song.mixer[(std::size_t) to].name = name + " master";
  760. // a group when the routed tracks are one contiguous run of one dock (and `to` joins it when it is next to it)
  761. const auto order = displayOrder (song);
  762. auto position = [&] (int t) { return (int) (std::find (order.begin(), order.end(), t) - order.begin()); };
  763. std::vector<int> places;
  764. for (auto t : routed)
  765. places.push_back (position (t));
  766. std::sort (places.begin(), places.end());
  767. const auto dock = song.mixer[(std::size_t) routed.front()].dock;
  768. const auto sameDock = std::all_of (routed.begin(), routed.end(), [&] (int t) { return song.mixer[(std::size_t) t].dock == dock; });
  769. if (sameDock && places.back() - places.front() + 1 == (int) places.size())
  770. {
  771. auto first = places.front(), last = places.back();
  772. const auto at = position (to);
  773. if (song.mixer[(std::size_t) to].dock == dock)
  774. {
  775. if (at == first - 1)
  776. first = at;
  777. else if (at == last + 1)
  778. last = at;
  779. }
  780. if (order[(std::size_t) first] > 0)
  781. song.mixer[(std::size_t) order[(std::size_t) first]].separator = true;
  782. if (last + 1 < (int) order.size() && order[(std::size_t) last + 1] > 0)
  783. song.mixer[(std::size_t) order[(std::size_t) last + 1]].separator = true;
  784. }
  785. return (int) routed.size();
  786. }
  787. int resetManualLatency (Song& song)
  788. {
  789. int count = 0;
  790. for (auto& t : song.mixer)
  791. if (t.latencyMs != 0.0f)
  792. {
  793. t.latencyMs = 0.0f;
  794. ++count;
  795. }
  796. return count;
  797. }
  798. namespace
  799. {
  800. // rows back to "packed" (-1) when they are exactly 0, 1, 2... (old projects compare and save unchanged)
  801. void tidyRows (std::vector<EffectSlot>& fx)
  802. {
  803. for (int i = 0; i < (int) fx.size(); ++i)
  804. if (fx[(std::size_t) i].row != i && fx[(std::size_t) i].row != -1)
  805. return;
  806. for (auto& e : fx)
  807. e.row = -1;
  808. }
  809. // puts the effects of `track` in the order of `rows` (one row per effect, all different) and stores the rows
  810. void applyRows (Song& song, int track, std::vector<int> rows)
  811. {
  812. auto& fx = song.mixer[(std::size_t) track].effects;
  813. const auto n = (int) fx.size();
  814. std::vector<int> order (n);
  815. for (int i = 0; i < n; ++i)
  816. order[(std::size_t) i] = i;
  817. std::stable_sort (order.begin(), order.end(), [&] (int a, int b) { return rows[(std::size_t) a] < rows[(std::size_t) b]; });
  818. std::vector<int> at (n); // at[position] = original index now there
  819. for (int i = 0; i < n; ++i)
  820. at[(std::size_t) i] = i;
  821. for (int pos = 0; pos < n; ++pos)
  822. {
  823. const auto want = order[(std::size_t) pos];
  824. const auto k = (int) (std::find (at.begin() + pos, at.end(), want) - at.begin());
  825. if (k != pos)
  826. {
  827. song.swapEffectSlots (track, pos, k); // (automations follow)
  828. std::swap (at[(std::size_t) pos], at[(std::size_t) k]);
  829. }
  830. }
  831. for (int pos = 0; pos < n; ++pos)
  832. fx[(std::size_t) pos].row = rows[(std::size_t) at[(std::size_t) pos]];
  833. tidyRows (fx);
  834. }
  835. } // namespace
  836. std::vector<int> effectRows (const std::vector<EffectSlot>& effects)
  837. {
  838. const auto n = (int) effects.size();
  839. std::vector<int> rows ((std::size_t) n);
  840. int previous = -1;
  841. for (int i = 0; i < n; ++i)
  842. {
  843. const auto wanted = effects[(std::size_t) i].row;
  844. previous = wanted > previous ? wanted : previous + 1;
  845. rows[(std::size_t) i] = previous;
  846. }
  847. for (int i = n - 1; i >= 0; --i) // (never past the last slot)
  848. rows[(std::size_t) i] = std::min (rows[(std::size_t) i], i == n - 1 ? maxEffectSlots - 1 : rows[(std::size_t) i + 1] - 1);
  849. return rows;
  850. }
  851. int effectAtRow (const std::vector<EffectSlot>& effects, int row)
  852. {
  853. const auto rows = effectRows (effects);
  854. const auto it = std::find (rows.begin(), rows.end(), row);
  855. return it == rows.end() ? -1 : (int) (it - rows.begin());
  856. }
  857. bool moveEffectToRow (Song& song, int track, int index, int row)
  858. {
  859. if (track < 0 || track >= (int) song.mixer.size() || row < 0 || row >= maxEffectSlots)
  860. return false;
  861. auto& fx = song.mixer[(std::size_t) track].effects;
  862. if (index < 0 || index >= (int) fx.size())
  863. return false;
  864. auto rows = effectRows (fx);
  865. if (rows[(std::size_t) index] == row)
  866. return false;
  867. if (const auto other = effectAtRow (fx, row); other >= 0)
  868. rows[(std::size_t) other] = rows[(std::size_t) index];
  869. rows[(std::size_t) index] = row;
  870. applyRows (song, track, rows);
  871. return true;
  872. }
  873. int moveEffectBy (Song& song, int track, int index, int delta)
  874. {
  875. if (track < 0 || track >= (int) song.mixer.size() || index < 0 || index >= (int) song.mixer[(std::size_t) track].effects.size() || delta == 0)
  876. return -1;
  877. const auto from = effectRows (song.mixer[(std::size_t) track].effects)[(std::size_t) index];
  878. const auto to = ((from + delta) % maxEffectSlots + maxEffectSlots) % maxEffectSlots; // (FL wraps around)
  879. if (! moveEffectToRow (song, track, index, to))
  880. return -1;
  881. return effectAtRow (song.mixer[(std::size_t) track].effects, to); // (the moved effect is the one there now)
  882. }
  883. int insertEffectAtRow (Song& song, int track, EffectSlot fx, int row)
  884. {
  885. if (track < 0 || track >= (int) song.mixer.size() || row < 0 || row >= maxEffectSlots)
  886. return -1;
  887. auto& effects = song.mixer[(std::size_t) track].effects;
  888. if ((int) effects.size() >= maxEffectSlots || effectAtRow (effects, row) >= 0)
  889. return -1;
  890. auto rows = effectRows (effects);
  891. effects.push_back (std::move (fx));
  892. rows.push_back (row);
  893. applyRows (song, track, rows);
  894. return effectAtRow (song.mixer[(std::size_t) track].effects, row);
  895. }
  896. void removeEffectKeepRows (Song& song, int track, int index)
  897. {
  898. if (track < 0 || track >= (int) song.mixer.size() || index < 0 || index >= (int) song.mixer[(std::size_t) track].effects.size())
  899. return;
  900. auto rows = effectRows (song.mixer[(std::size_t) track].effects);
  901. song.removeEffectSlot (track, index);
  902. rows.erase (rows.begin() + index);
  903. auto& fx = song.mixer[(std::size_t) track].effects;
  904. for (int i = 0; i < (int) fx.size(); ++i)
  905. fx[(std::size_t) i].row = rows[(std::size_t) i];
  906. tidyRows (fx);
  907. }
  908. bool moveEffectSlot (Song& song, int track, int from, int to)
  909. {
  910. if (track < 0 || track >= (int) song.mixer.size())
  911. return false;
  912. const auto count = (int) song.mixer[(std::size_t) track].effects.size();
  913. if (from < 0 || from >= count || to < 0 || to >= count || from == to)
  914. return false;
  915. const auto rows = effectRows (song.mixer[(std::size_t) track].effects);
  916. for (auto s = from; s != to; s += to > from ? 1 : -1)
  917. song.swapEffectSlots (track, s, s + (to > from ? 1 : -1));
  918. auto& fx = song.mixer[(std::size_t) track].effects;
  919. for (int i = 0; i < count; ++i) // (the rows stay where they were)
  920. fx[(std::size_t) i].row = rows[(std::size_t) i];
  921. tidyRows (fx);
  922. return true;
  923. }
  924. void setAllEffectsEnabled (Song& song, int track, bool enabled)
  925. {
  926. if (track >= 0 && track < (int) song.mixer.size())
  927. for (auto& fx : song.mixer[(std::size_t) track].effects)
  928. fx.enabled = enabled;
  929. }
  930. bool anyEffectEnabled (const Song& song, int track) noexcept
  931. {
  932. if (track < 0 || track >= (int) song.mixer.size())
  933. return false;
  934. const auto& fx = song.mixer[(std::size_t) track].effects;
  935. return std::any_of (fx.begin(), fx.end(), [] (const EffectSlot& s) { return s.enabled; });
  936. }
  937. void resetToDefault (Song& song, const std::vector<int>& tracks)
  938. {
  939. for (auto t : tracks)
  940. if (t >= 0 && t < (int) song.mixer.size())
  941. {
  942. auto& m = song.mixer[(std::size_t) t];
  943. m.volume = unityFader;
  944. m.pan = 0.0f;
  945. m.stereo = 0.0f;
  946. m.invertPhase = m.swapChannels = false;
  947. m.eq = TrackEq {};
  948. m.muted = m.solo = false;
  949. }
  950. }
  951. void resetRouting (Song& song, const std::vector<int>& tracks)
  952. {
  953. for (auto t : tracks)
  954. if (t > 0 && t < (int) song.mixer.size())
  955. {
  956. auto& m = song.mixer[(std::size_t) t];
  957. m.sends.clear();
  958. m.lostSends.clear();
  959. m.toMaster = true;
  960. m.masterLevel = 1.0f;
  961. }
  962. }
  963. int unrouteInto (Song& song, const std::vector<int>& tracks)
  964. {
  965. int removed = 0;
  966. for (int from = 1; from < (int) song.mixer.size(); ++from)
  967. {
  968. auto& m = song.mixer[(std::size_t) from];
  969. for (auto t : tracks)
  970. {
  971. if (t == from)
  972. continue;
  973. if (t == 0 && m.toMaster && ! m.sends.empty())
  974. {
  975. m.toMaster = false; // (it still goes somewhere else)
  976. ++removed;
  977. }
  978. const auto before = m.sends.size();
  979. m.sends.erase (std::remove_if (m.sends.begin(), m.sends.end(), [t] (const MixerSend& s) { return s.target == t; }), m.sends.end());
  980. removed += (int) (before - m.sends.size());
  981. }
  982. if (m.sends.empty() && ! m.toMaster)
  983. m.toMaster = true;
  984. }
  985. return removed;
  986. }
  987. std::vector<int> destinationsOf (const Song& song, int track)
  988. {
  989. std::vector<int> out;
  990. if (track <= 0 || track >= (int) song.mixer.size())
  991. return out;
  992. const auto& m = song.mixer[(std::size_t) track];
  993. if (m.toMaster)
  994. out.push_back (0);
  995. for (const auto& s : m.sends)
  996. if (std::find (out.begin(), out.end(), s.target) == out.end())
  997. out.push_back (s.target);
  998. std::sort (out.begin(), out.end());
  999. return out;
  1000. }
  1001. std::vector<int> sourcesOf (const Song& song, int track)
  1002. {
  1003. std::vector<int> out;
  1004. for (int from = 1; from < (int) song.mixer.size(); ++from)
  1005. if (from != track && song.routes (from, track))
  1006. out.push_back (from);
  1007. return out;
  1008. }
  1009. int insertTracks (Song& song, int at, int count)
  1010. {
  1011. // one shift of every reference (round 166d: hundreds of inserts at once stay instant; it used to swap the new
  1012. // insert into place one neighbour at a time)
  1013. const auto tracks = (int) song.mixer.size();
  1014. count = std::min (count, maxMixerTracks - tracks);
  1015. if (at < 1 || at > tracks || count <= 0)
  1016. return 0;
  1017. remapTrackReferences (song, [at, count] (int t) { return t >= at ? t + count : t; });
  1018. std::vector<MixerTrack> added ((std::size_t) count);
  1019. for (int i = 0; i < count; ++i)
  1020. added[(std::size_t) i].name = "Insert " + std::to_string (at + i);
  1021. song.mixer.insert (song.mixer.begin() + at, std::make_move_iterator (added.begin()), std::make_move_iterator (added.end()));
  1022. renumberDefaultNames (song, at + count, count);
  1023. return count;
  1024. }
  1025. int roomForInserts (const Song& song) noexcept
  1026. {
  1027. return std::max (0, maxMixerTracks - std::max (1, (int) song.mixer.size()));
  1028. }
  1029. std::vector<int> displayOrder (const Song& song)
  1030. {
  1031. std::vector<int> order;
  1032. for (auto dock : { MixerDock::left, MixerDock::middle, MixerDock::right })
  1033. for (int t = 0; t < (int) song.mixer.size(); ++t)
  1034. if (song.mixer[(std::size_t) t].dock == dock)
  1035. order.push_back (t);
  1036. return order;
  1037. }
  1038. std::vector<int> groupOf (const Song& song, int track)
  1039. {
  1040. std::vector<int> out;
  1041. if (track <= 0 || track >= (int) song.mixer.size())
  1042. return out;
  1043. const auto order = displayOrder (song);
  1044. const auto at = (int) (std::find (order.begin(), order.end(), track) - order.begin());
  1045. const auto dock = song.mixer[(std::size_t) track].dock;
  1046. const auto inBlock = [&] (int i) { return i >= 0 && i < (int) order.size() && order[(std::size_t) i] > 0
  1047. && song.mixer[(std::size_t) order[(std::size_t) i]].dock == dock; };
  1048. auto first = at;
  1049. while (! song.mixer[(std::size_t) order[(std::size_t) first]].separator && inBlock (first - 1))
  1050. --first;
  1051. auto last = at;
  1052. while (inBlock (last + 1) && ! song.mixer[(std::size_t) order[(std::size_t) last + 1]].separator)
  1053. ++last;
  1054. for (auto i = first; i <= last; ++i)
  1055. out.push_back (order[(std::size_t) i]);
  1056. return out;
  1057. }
  1058. bool canFold (const Song& song, int track)
  1059. {
  1060. const auto group = groupOf (song, track);
  1061. if (group.size() < 2)
  1062. return false;
  1063. if (song.mixer[(std::size_t) group.front()].separator)
  1064. return true;
  1065. const auto order = displayOrder (song);
  1066. const auto after = std::find (order.begin(), order.end(), group.back()) + 1;
  1067. return after != order.end() && *after > 0 && song.mixer[(std::size_t) *after].separator
  1068. && song.mixer[(std::size_t) *after].dock == song.mixer[(std::size_t) group.back()].dock;
  1069. }
  1070. int groupBus (const Song& song, const std::vector<int>& group)
  1071. {
  1072. for (const auto bus : group)
  1073. {
  1074. auto all = true;
  1075. for (const auto t : group)
  1076. {
  1077. if (t == bus)
  1078. continue;
  1079. const auto& m = song.mixer[(std::size_t) t];
  1080. if (m.toMaster || m.sends.size() != 1 || m.sends.front().target != bus)
  1081. {
  1082. all = false;
  1083. break;
  1084. }
  1085. }
  1086. if (all)
  1087. return bus;
  1088. }
  1089. return -1;
  1090. }
  1091. bool isFolded (const Song& song, int track)
  1092. {
  1093. const auto group = groupOf (song, track);
  1094. return ! group.empty() && song.mixer[(std::size_t) group.front()].folded && canFold (song, track);
  1095. }
  1096. std::vector<FoldShow> foldView (const Song& song)
  1097. {
  1098. std::vector<FoldShow> out (song.mixer.size(), FoldShow::strip);
  1099. for (std::size_t t = 1; t < song.mixer.size(); ++t)
  1100. {
  1101. if (! song.mixer[t].folded || out[t] != FoldShow::strip)
  1102. continue;
  1103. const auto group = groupOf (song, (int) t);
  1104. if (group.empty() || group.front() != (int) t || ! canFold (song, (int) t))
  1105. continue;
  1106. const auto bus = groupBus (song, group);
  1107. for (const auto m : group)
  1108. out[(std::size_t) m] = m == bus ? FoldShow::strip : FoldShow::hidden;
  1109. if (bus < 0)
  1110. out[(std::size_t) group.front()] = FoldShow::tab;
  1111. }
  1112. return out;
  1113. }
  1114. bool toggleFold (Song& song, int track)
  1115. {
  1116. if (! canFold (song, track))
  1117. return false;
  1118. const auto head = groupOf (song, track).front();
  1119. auto& flag = song.mixer[(std::size_t) head].folded;
  1120. flag = ! flag;
  1121. return flag;
  1122. }
  1123. int foldAll (Song& song, bool fold)
  1124. {
  1125. int changed = 0;
  1126. for (int t = 1; t < (int) song.mixer.size(); ++t)
  1127. {
  1128. const auto group = groupOf (song, t);
  1129. if (group.empty() || group.front() != t || ! canFold (song, t) || song.mixer[(std::size_t) t].folded == fold)
  1130. continue;
  1131. song.mixer[(std::size_t) t].folded = fold;
  1132. ++changed;
  1133. }
  1134. return changed;
  1135. }
  1136. std::vector<int> vcasOf (const Song& song, int track)
  1137. {
  1138. std::vector<int> out;
  1139. const auto n = (int) song.vcas.size();
  1140. for (int v = 0; v < n; ++v)
  1141. if (std::binary_search (song.vcas[(std::size_t) v].members.begin(), song.vcas[(std::size_t) v].members.end(), track))
  1142. for (int p = v, guard = 0; p >= 0 && p < n && guard <= n; p = song.vcas[(std::size_t) p].parent, ++guard) // (a cycle stops)
  1143. if (std::find (out.begin(), out.end(), p) == out.end())
  1144. out.push_back (p);
  1145. return out;
  1146. }
  1147. float vcaGain (const Song& song, int track)
  1148. {
  1149. if (song.vcas.empty())
  1150. return 1.0f;
  1151. auto g = 1.0f;
  1152. for (const auto v : vcasOf (song, track))
  1153. g *= faderGain (song.vcas[(std::size_t) v].level);
  1154. return g;
  1155. }
  1156. bool vcaMuted (const Song& song, int track)
  1157. {
  1158. if (song.vcas.empty())
  1159. return false;
  1160. const auto list = vcasOf (song, track);
  1161. return std::any_of (list.begin(), list.end(), [&song] (int v) { return song.vcas[(std::size_t) v].muted; });
  1162. }
  1163. int assignToVca (Song& song, const std::vector<int>& tracks, int vca)
  1164. {
  1165. if (vca < 0 || vca >= (int) song.vcas.size())
  1166. return 0;
  1167. auto& members = song.vcas[(std::size_t) vca].members;
  1168. int changed = 0;
  1169. for (const auto t : tracks)
  1170. if (isInsert (song, t) && ! std::binary_search (members.begin(), members.end(), t))
  1171. {
  1172. members.insert (std::lower_bound (members.begin(), members.end(), t), t);
  1173. ++changed;
  1174. }
  1175. return changed;
  1176. }
  1177. int removeFromVca (Song& song, const std::vector<int>& tracks, int vca)
  1178. {
  1179. if (vca < 0 || vca >= (int) song.vcas.size())
  1180. return 0;
  1181. auto& members = song.vcas[(std::size_t) vca].members;
  1182. const auto before = members.size();
  1183. members.erase (std::remove_if (members.begin(), members.end(),
  1184. [&tracks] (int m) { return std::find (tracks.begin(), tracks.end(), m) != tracks.end(); }),
  1185. members.end());
  1186. return (int) (before - members.size());
  1187. }
  1188. int createVca (Song& song, const std::vector<int>& tracks, const std::string& name)
  1189. {
  1190. Vca vca;
  1191. vca.name = name.empty() ? "VCA " + std::to_string (song.vcas.size() + 1) : name;
  1192. song.vcas.push_back (vca);
  1193. const auto index = (int) song.vcas.size() - 1;
  1194. assignToVca (song, tracks, index);
  1195. return index;
  1196. }
  1197. bool deleteVca (Song& song, int vca)
  1198. {
  1199. if (vca < 0 || vca >= (int) song.vcas.size())
  1200. return false;
  1201. song.vcas.erase (song.vcas.begin() + vca);
  1202. for (auto& v : song.vcas)
  1203. v.parent = v.parent == vca ? -1 : v.parent > vca ? v.parent - 1 : v.parent;
  1204. song.forEachTarget ([vca] (AutomationTarget& t) {
  1205. if (t.kind != AutomationTargetKind::vcaLevel)
  1206. return;
  1207. if (t.index == vca)
  1208. t = AutomationTarget {};
  1209. else if (t.index > vca)
  1210. --t.index;
  1211. });
  1212. song.removeDeadMidiLinks();
  1213. return true;
  1214. }
  1215. std::vector<int> vcaReach (const Song& song, int vca)
  1216. {
  1217. std::vector<int> out;
  1218. for (int t = 1; t < (int) song.mixer.size(); ++t)
  1219. {
  1220. const auto list = vcasOf (song, t);
  1221. if (std::find (list.begin(), list.end(), vca) != list.end())
  1222. out.push_back (t);
  1223. }
  1224. return out;
  1225. }
  1226. bool mergeVcaIntoMembers (Song& song, int vca)
  1227. {
  1228. if (vca < 0 || vca >= (int) song.vcas.size())
  1229. return false;
  1230. const auto gain = faderGain (song.vcas[(std::size_t) vca].level);
  1231. if (gain == 1.0f)
  1232. return false;
  1233. auto scaled = [gain] (float position) { return std::clamp (faderPosition (faderGain (position) * gain), 0.0f, 1.0f); };
  1234. for (const auto t : vcaReach (song, vca))
  1235. {
  1236. auto& m = song.mixer[(std::size_t) t];
  1237. m.volume = scaled (m.volume);
  1238. for (auto& a : song.automations) // the member's own volume automation carries the gain too
  1239. if (a.target.kind == AutomationTargetKind::mixerVolume && a.target.index == t)
  1240. for (auto& p : a.points)
  1241. p.value = scaled (p.value);
  1242. }
  1243. song.vcas[(std::size_t) vca].level = unityFader;
  1244. return true;
  1245. }
  1246. namespace
  1247. {
  1248. int linkedInsert (const Song& song, const PlaylistTrack& pt)
  1249. {
  1250. if (pt.mode == PlaylistTrackMode::audio)
  1251. return pt.mixerTrack;
  1252. if (pt.mode == PlaylistTrackMode::instrument)
  1253. return pt.channel >= 0 && pt.channel < (int) song.channels.size() ? song.channels[(std::size_t) pt.channel].mixerTrack : pt.mixerTrack;
  1254. return -1;
  1255. }
  1256. } // namespace
  1257. std::vector<int> playlistOrderInserts (const Song& song)
  1258. {
  1259. std::vector<int> out;
  1260. for (const auto& pt : song.playlistTracks)
  1261. if (const auto t = linkedInsert (song, pt); t > 0 && t < (int) song.mixer.size() && std::find (out.begin(), out.end(), t) == out.end())
  1262. out.push_back (t);
  1263. return out;
  1264. }
  1265. std::vector<PlaylistGroupInserts> playlistGroupInserts (const Song& song)
  1266. {
  1267. std::vector<PlaylistGroupInserts> groups;
  1268. std::vector<int> seen;
  1269. const auto& tracks = song.playlistTracks;
  1270. for (std::size_t i = 0; i < tracks.size(); ++i)
  1271. {
  1272. if (tracks[i].grouped)
  1273. continue; // (a member: handled with its header)
  1274. auto end = i + 1;
  1275. while (end < tracks.size() && tracks[end].grouped)
  1276. ++end;
  1277. if (end == i + 1)
  1278. continue; // an ungrouped track on its own
  1279. PlaylistGroupInserts g;
  1280. g.collapsed = tracks[i].collapsed;
  1281. for (auto k = i; k < end; ++k)
  1282. if (const auto t = linkedInsert (song, tracks[k]); t > 0 && t < (int) song.mixer.size() && std::find (seen.begin(), seen.end(), t) == seen.end())
  1283. {
  1284. seen.push_back (t);
  1285. g.inserts.push_back (t);
  1286. }
  1287. if (! g.inserts.empty())
  1288. groups.push_back (std::move (g));
  1289. }
  1290. return groups;
  1291. }
  1292. namespace
  1293. {
  1294. // the separator / fold flags followPlaylistGroups wants, per mixer track
  1295. void followedFlags (const Song& song, std::vector<bool>& separator, std::vector<bool>& folded)
  1296. {
  1297. const auto count = song.mixer.size();
  1298. separator.assign (count, false);
  1299. folded.assign (count, false);
  1300. for (std::size_t t = 0; t < count; ++t)
  1301. {
  1302. separator[t] = song.mixer[t].separator;
  1303. folded[t] = song.mixer[t].folded;
  1304. }
  1305. for (const auto t : playlistOrderInserts (song)) // the follow owns these
  1306. separator[(std::size_t) t] = folded[(std::size_t) t] = false;
  1307. const auto order = displayOrder (song);
  1308. const auto at = [&order] (int t) { return (int) (std::find (order.begin(), order.end(), t) - order.begin()); };
  1309. for (const auto& g : playlistGroupInserts (song))
  1310. {
  1311. if (g.inserts.size() < 2)
  1312. continue;
  1313. auto positions = g.inserts;
  1314. for (auto& p : positions)
  1315. p = at (p);
  1316. std::sort (positions.begin(), positions.end());
  1317. const auto dock = song.mixer[(std::size_t) order[(std::size_t) positions.front()]].dock;
  1318. auto runs = positions.back() - positions.front() + 1 == (int) positions.size();
  1319. for (const auto p : positions)
  1320. runs = runs && song.mixer[(std::size_t) order[(std::size_t) p]].dock == dock;
  1321. if (! runs)
  1322. continue; // not side by side: "Arrange inserts like the Playlist" puts them together
  1323. const auto first = order[(std::size_t) positions.front()];
  1324. separator[(std::size_t) first] = true;
  1325. folded[(std::size_t) first] = g.collapsed;
  1326. if (const auto next = positions.back() + 1; next < (int) order.size() && order[(std::size_t) next] > 0
  1327. && song.mixer[(std::size_t) order[(std::size_t) next]].dock == dock)
  1328. separator[(std::size_t) order[(std::size_t) next]] = true;
  1329. }
  1330. }
  1331. } // namespace
  1332. bool followsPlaylistGroups (const Song& song)
  1333. {
  1334. std::vector<bool> separator, folded;
  1335. followedFlags (song, separator, folded);
  1336. for (std::size_t t = 1; t < song.mixer.size(); ++t)
  1337. if (song.mixer[t].separator != separator[t] || song.mixer[t].folded != folded[t])
  1338. return false;
  1339. return true;
  1340. }
  1341. bool followPlaylistGroups (Song& song)
  1342. {
  1343. std::vector<bool> separator, folded;
  1344. followedFlags (song, separator, folded);
  1345. auto changed = false;
  1346. for (std::size_t t = 1; t < song.mixer.size(); ++t)
  1347. {
  1348. auto& m = song.mixer[t];
  1349. changed = changed || m.separator != separator[t] || m.folded != folded[t];
  1350. m.separator = separator[t];
  1351. m.folded = folded[t];
  1352. }
  1353. return changed;
  1354. }
  1355. int arrangeLikePlaylist (Song& song)
  1356. {
  1357. const auto linked = playlistOrderInserts (song);
  1358. std::vector<int> wanted = linked; // the new order of inserts 1..n: the linked ones, then the rest as they are
  1359. for (int t = 1; t < (int) song.mixer.size(); ++t)
  1360. if (std::find (linked.begin(), linked.end(), t) == linked.end())
  1361. wanted.push_back (t);
  1362. // place by place, the wanted insert swaps in (swapTracks carries every reference along)
  1363. std::vector<int> now (song.mixer.size()); // now[t] = which original insert sits at t
  1364. std::iota (now.begin(), now.end(), 0);
  1365. int moved = 0;
  1366. for (int place = 1; place <= (int) wanted.size(); ++place)
  1367. {
  1368. const auto from = (int) (std::find (now.begin() + 1, now.end(), wanted[(std::size_t) place - 1]) - now.begin());
  1369. if (from == place)
  1370. continue;
  1371. swapTracks (song, place, from);
  1372. std::swap (now[(std::size_t) place], now[(std::size_t) from]);
  1373. }
  1374. for (int t = 1; t < (int) now.size(); ++t)
  1375. moved += now[(std::size_t) t] != t ? 1 : 0;
  1376. followPlaylistGroups (song);
  1377. return moved;
  1378. }
  1379. void createGroup (Song& song, const std::vector<int>& tracks, int current)
  1380. {
  1381. const auto order = displayOrder (song);
  1382. const auto picked = [&] (int t) { return std::find (tracks.begin(), tracks.end(), t) != tracks.end(); };
  1383. for (std::size_t i = 0; i < order.size(); ++i)
  1384. {
  1385. const auto t = order[i];
  1386. if (t <= 0 || ! picked (t))
  1387. continue;
  1388. if (i == 0 || ! picked (order[i - 1]))
  1389. song.mixer[(std::size_t) t].separator = true;
  1390. if (i + 1 < order.size() && order[i + 1] > 0 && ! picked (order[i + 1]))
  1391. song.mixer[(std::size_t) order[i + 1]].separator = true;
  1392. }
  1393. if (current >= 0 && current < (int) song.mixer.size() && song.mixer[(std::size_t) current].colour != 0)
  1394. for (auto t : tracks)
  1395. if (t > 0 && t < (int) song.mixer.size() && song.mixer[(std::size_t) t].colour == 0)
  1396. song.mixer[(std::size_t) t].colour = song.mixer[(std::size_t) current].colour;
  1397. // the group's name (REA round 149 [DECOMP FLEngine FUN_012aa200 → FUN_012be6f0, the track's name at +0xc]): the
  1398. // selected tracks without a name take the current track's
  1399. if (current >= 0 && current < (int) song.mixer.size() && ! song.mixer[(std::size_t) current].name.empty())
  1400. for (auto t : tracks)
  1401. if (t > 0 && t != current && t < (int) song.mixer.size() && song.mixer[(std::size_t) t].name.empty())
  1402. song.mixer[(std::size_t) t].name = song.mixer[(std::size_t) current].name;
  1403. }
  1404. int autoColorGroup (Song& song, int track)
  1405. {
  1406. int changed = 0;
  1407. if (track <= 0 || track >= (int) song.mixer.size())
  1408. return 0;
  1409. const auto colour = song.mixer[(std::size_t) track].colour;
  1410. for (auto t : groupOf (song, track))
  1411. if (song.mixer[(std::size_t) t].colour != colour)
  1412. {
  1413. song.mixer[(std::size_t) t].colour = colour;
  1414. ++changed;
  1415. }
  1416. return changed;
  1417. }
  1418. std::uint32_t gradientColourStep (std::uint32_t from, std::uint32_t to, int k, int n) noexcept
  1419. {
  1420. if (n < 2)
  1421. return from;
  1422. const auto amount = (k << 8) / (n - 1); // FL: shl 8, idiv
  1423. std::uint32_t out = 0xff000000u;
  1424. for (int shift = 0; shift <= 16; shift += 8)
  1425. {
  1426. const auto a = (int) ((from >> shift) & 0xffu), b = (int) ((to >> shift) & 0xffu);
  1427. // FL blends with pmulhw on (b - a) * 16 and amount * 16: the high word, an arithmetic shift (floor)
  1428. const auto c = a + (((b - a) * 16 * (amount * 16)) >> 16);
  1429. out |= (std::uint32_t) std::clamp (c, 0, 255) << shift;
  1430. }
  1431. return out;
  1432. }
  1433. int gradientColour (Song& song, std::vector<int> tracks, std::uint32_t from, std::uint32_t to)
  1434. {
  1435. std::sort (tracks.begin(), tracks.end());
  1436. tracks.erase (std::unique (tracks.begin(), tracks.end()), tracks.end());
  1437. tracks.erase (std::remove_if (tracks.begin(), tracks.end(), [&] (int t) { return t < 0 || t >= (int) song.mixer.size(); }),
  1438. tracks.end());
  1439. const auto n = (int) tracks.size();
  1440. if (n < 2)
  1441. return 0;
  1442. int changed = 0;
  1443. for (int k = 0; k < n; ++k)
  1444. {
  1445. auto& colour = song.mixer[(std::size_t) tracks[(std::size_t) k]].colour;
  1446. const auto next = gradientColourStep (from, to, k, n);
  1447. if (colour != next)
  1448. {
  1449. colour = next;
  1450. ++changed;
  1451. }
  1452. }
  1453. return changed;
  1454. }
  1455. namespace
  1456. {
  1457. constexpr int audioTrackScan = 500; // FL: playlist tracks 1..500
  1458. std::vector<int> sortedInserts (const Song& song, std::vector<int> inserts)
  1459. {
  1460. std::sort (inserts.begin(), inserts.end());
  1461. inserts.erase (std::unique (inserts.begin(), inserts.end()), inserts.end());
  1462. inserts.erase (std::remove_if (inserts.begin(), inserts.end(), [&] (int t) { return t <= 0 || t >= (int) song.mixer.size(); }),
  1463. inserts.end());
  1464. return inserts;
  1465. }
  1466. // FL skips an insert that any Playlist track already points at (0x12a76e0: any mode), or whose one and only channel
  1467. // already has an instrument track (0x12a7620 with 0xffac90 / 0xffae00; audio clip channels don't count).
  1468. bool hasPlaylistTrack (const Song& song, int insert)
  1469. {
  1470. for (const auto& pt : song.playlistTracks)
  1471. if (pt.mode != PlaylistTrackMode::unassigned && pt.mixerTrack == insert)
  1472. return true;
  1473. int sole = -1, count = 0;
  1474. for (int c = 0; c < (int) song.channels.size(); ++c)
  1475. if (song.channels[(std::size_t) c].mixerTrack == insert)
  1476. {
  1477. sole = c;
  1478. ++count;
  1479. }
  1480. if (count != 1 || song.channels[(std::size_t) sole].instrument == InstrumentKind::audioClip)
  1481. return false;
  1482. return std::any_of (song.playlistTracks.begin(), song.playlistTracks.end(), [sole] (const PlaylistTrack& pt) {
  1483. return pt.mode == PlaylistTrackMode::instrument && pt.channel == sole;
  1484. });
  1485. }
  1486. int audioTrackOf (const Song& song, int insert)
  1487. {
  1488. for (int i = 0; i < (int) song.playlistTracks.size(); ++i)
  1489. {
  1490. const auto& pt = song.playlistTracks[(std::size_t) i];
  1491. if (pt.mode == PlaylistTrackMode::audio && pt.mixerTrack == insert)
  1492. return i;
  1493. }
  1494. return -1;
  1495. }
  1496. } // namespace
  1497. int assignNewAudioTracks (Song& song, std::vector<int> inserts)
  1498. {
  1499. int assigned = 0;
  1500. for (auto t : sortedInserts (song, std::move (inserts)))
  1501. {
  1502. if (hasPlaylistTrack (song, t))
  1503. continue;
  1504. for (int i = 0; i < audioTrackScan; ++i)
  1505. {
  1506. const auto untouched = i >= (int) song.playlistTracks.size()
  1507. || (song.playlistTracks[(std::size_t) i].name.empty() && song.playlistTracks[(std::size_t) i].colour == 0
  1508. && song.playlistTracks[(std::size_t) i].mode == PlaylistTrackMode::unassigned);
  1509. if (! untouched || std::any_of (song.playlist.begin(), song.playlist.end(), [i] (const Clip& c) { return c.track == i; }))
  1510. continue;
  1511. if ((int) song.playlistTracks.size() <= i)
  1512. song.playlistTracks.resize ((std::size_t) i + 1);
  1513. auto& pt = song.playlistTracks[(std::size_t) i];
  1514. const auto& insert = song.mixer[(std::size_t) t];
  1515. pt.mode = PlaylistTrackMode::audio;
  1516. pt.mixerTrack = t;
  1517. pt.name = insert.name.empty() ? "Insert " + std::to_string (t) : insert.name;
  1518. pt.colour = insert.colour;
  1519. ++assigned;
  1520. break;
  1521. }
  1522. }
  1523. return assigned;
  1524. }
  1525. int unassignAudioTracks (Song& song, std::vector<int> inserts)
  1526. {
  1527. int changed = 0;
  1528. for (auto t : sortedInserts (song, std::move (inserts)))
  1529. if (const auto i = audioTrackOf (song, t); i >= 0)
  1530. {
  1531. auto& pt = song.playlistTracks[(std::size_t) i];
  1532. pt.mode = PlaylistTrackMode::unassigned;
  1533. pt.mixerTrack = -1;
  1534. ++changed;
  1535. }
  1536. return changed;
  1537. }
  1538. void setSeparator (Song& song, const std::vector<int>& tracks, bool on)
  1539. {
  1540. for (auto t : tracks)
  1541. if (t > 0 && t < (int) song.mixer.size())
  1542. song.mixer[(std::size_t) t].separator = on;
  1543. }
  1544. void dockTracks (Song& song, const std::vector<int>& tracks, MixerDock dock)
  1545. {
  1546. for (auto t : tracks)
  1547. if (t >= 0 && t < (int) song.mixer.size())
  1548. song.mixer[(std::size_t) t].dock = dock;
  1549. }
  1550. int firstTrackAfterUsed (const Song& song) noexcept
  1551. {
  1552. int track = 0;
  1553. for (const auto& c : song.playlist)
  1554. track = std::max (track, c.track + 1);
  1555. return track;
  1556. }
  1557. std::string insertDisplayName (const Song& song, int track)
  1558. {
  1559. if (track <= 0 || track >= (int) song.mixer.size())
  1560. return "Master";
  1561. const auto& name = song.mixer[(std::size_t) track].name;
  1562. return name.empty() ? "Insert " + std::to_string (track) : name;
  1563. }
  1564. int placeRenderedFiles (Song& song, const std::vector<RenderedFile>& files)
  1565. {
  1566. int placed = 0;
  1567. auto track = firstTrackAfterUsed (song);
  1568. for (const auto& f : files)
  1569. {
  1570. if (f.path.empty() || ! (f.frames > 0.0) || ! (f.sampleRate > 0.0))
  1571. continue;
  1572. ChannelDef c;
  1573. auto stem = f.path.substr (f.path.find_last_of ("/\\") == std::string::npos ? 0 : f.path.find_last_of ("/\\") + 1);
  1574. if (const auto dot = stem.find_last_of ('.'); dot != std::string::npos && dot > 0)
  1575. stem.resize (dot);
  1576. c.name = stem;
  1577. c.instrument = InstrumentKind::audioClip;
  1578. c.samplePath = f.path;
  1579. c.mixerTrack = 0; // FL: "---"
  1580. song.channels.push_back (c);
  1581. Clip clip;
  1582. clip.kind = ClipKind::audio;
  1583. clip.channel = (int) song.channels.size() - 1;
  1584. clip.track = track++;
  1585. clip.start = 0;
  1586. clip.length = std::max<std::int64_t> (1, clipops::fileTicks (song.channels.back(), f.frames, f.sampleRate, song.bpm));
  1587. song.playlist.push_back (clip);
  1588. ++placed;
  1589. }
  1590. if (placed > 0 && song.autoCrossfades)
  1591. clipops::updateCrossfades (song);
  1592. return placed;
  1593. }
  1594. } // namespace lynx::mixerops