PlaylistOps.cpp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  1. #include "core/FlControlMenus.h"
  2. #include "core/MixerOps.h"
  3. #include "core/PlaylistOps.h"
  4. #include "core/TrackClips.h"
  5. #include "core/QuickRender.h"
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <cstdio>
  9. #include <limits>
  10. namespace lynx::clipprops
  11. {
  12. namespace
  13. {
  14. bool validSource (const Song& song, ClipKind kind, int source)
  15. {
  16. switch (kind)
  17. {
  18. case ClipKind::pattern: return source >= 0 && source < (int) song.patterns.size();
  19. case ClipKind::audio:
  20. return source >= 0 && source < (int) song.channels.size() && song.channels[(std::size_t) source].instrument == InstrumentKind::audioClip;
  21. case ClipKind::automation: return source >= 0 && source < (int) song.automations.size();
  22. }
  23. return false;
  24. }
  25. } // namespace
  26. bool read (const Song& song, int clip, Props& out)
  27. {
  28. if (clip < 0 || clip >= (int) song.playlist.size())
  29. return false;
  30. const auto& c = song.playlist[(std::size_t) clip];
  31. Props p;
  32. p.kind = c.kind;
  33. p.track = c.track;
  34. p.start = c.start;
  35. p.length = c.length;
  36. p.offset = c.offset;
  37. p.gain = c.gain;
  38. p.pan = c.pan;
  39. p.fadeIn = c.fadeIn;
  40. p.fadeOut = c.fadeOut;
  41. p.muted = c.muted;
  42. if (c.kind == ClipKind::pattern && validSource (song, c.kind, c.pattern))
  43. {
  44. const auto& pat = song.patterns[(std::size_t) c.pattern];
  45. p.source = c.pattern;
  46. p.name = pat.name;
  47. p.colour = pat.colour;
  48. }
  49. else if (c.kind == ClipKind::audio && c.channel >= 0 && c.channel < (int) song.channels.size())
  50. {
  51. const auto& ch = song.channels[(std::size_t) c.channel];
  52. p.source = c.channel;
  53. p.name = ch.name;
  54. p.colour = ch.colour;
  55. p.pitch = ch.sampler.pitch;
  56. p.stretchBeats = ch.sampler.stretchBeats;
  57. p.keepPitch = ch.sampler.keepPitch;
  58. }
  59. else if (c.kind == ClipKind::automation && validSource (song, c.kind, c.automation))
  60. {
  61. p.source = c.automation;
  62. p.name = song.automations[(std::size_t) c.automation].name;
  63. }
  64. out = p;
  65. return true;
  66. }
  67. bool apply (Song& song, int clip, const Props& p)
  68. {
  69. if (clip < 0 || clip >= (int) song.playlist.size())
  70. return false;
  71. auto& c = song.playlist[(std::size_t) clip];
  72. if (p.kind == c.kind && validSource (song, c.kind, p.source))
  73. {
  74. if (c.kind == ClipKind::pattern)
  75. c.pattern = p.source;
  76. else if (c.kind == ClipKind::audio)
  77. c.channel = p.source;
  78. else
  79. c.automation = p.source;
  80. }
  81. c.track = std::clamp (p.track, 0, 499);
  82. c.start = std::max<std::int64_t> (0, p.start);
  83. c.length = std::max<std::int64_t> (1, p.length);
  84. if (c.offset != p.offset)
  85. c.offsetFraction = 0.0f; // an offset typed in ticks drops the imported sub-tick rest
  86. c.offset = std::max<std::int64_t> (0, p.offset);
  87. c.muted = p.muted;
  88. if (c.kind == ClipKind::audio)
  89. {
  90. c.gain = std::clamp (p.gain, 0.0f, 4.0f);
  91. c.pan = std::clamp (p.pan, -1.0f, 1.0f);
  92. c.fadeIn = std::clamp<std::int64_t> (p.fadeIn, 0, c.length);
  93. c.fadeOut = std::clamp<std::int64_t> (p.fadeOut, 0, c.length - c.fadeIn);
  94. }
  95. // the source's name and colour (and the audio channel's pitch / stretch)
  96. if (c.kind == ClipKind::pattern && validSource (song, c.kind, c.pattern))
  97. {
  98. auto& pat = song.patterns[(std::size_t) c.pattern];
  99. if (! p.name.empty())
  100. pat.name = p.name;
  101. pat.colour = p.colour;
  102. }
  103. else if (c.kind == ClipKind::audio && c.channel >= 0 && c.channel < (int) song.channels.size())
  104. {
  105. auto& ch = song.channels[(std::size_t) c.channel];
  106. if (! p.name.empty())
  107. ch.name = p.name;
  108. ch.colour = p.colour;
  109. ch.sampler.pitch = std::clamp (p.pitch, -24.0f, 24.0f);
  110. ch.sampler.stretchBeats = std::max (0.0f, p.stretchBeats);
  111. ch.sampler.keepPitch = p.keepPitch; // (round 163: also "keep length" for a pitched clip without stretch)
  112. }
  113. else if (c.kind == ClipKind::automation && validSource (song, c.kind, c.automation) && ! p.name.empty())
  114. song.automations[(std::size_t) c.automation].name = p.name;
  115. return true;
  116. }
  117. void changeMute (Song& song, const std::vector<int>& clips, MuteChange change)
  118. {
  119. for (auto i : clips)
  120. if (i >= 0 && i < (int) song.playlist.size())
  121. {
  122. auto& m = song.playlist[(std::size_t) i].muted;
  123. switch (change)
  124. {
  125. case MuteChange::muteAll: m = true; break;
  126. case MuteChange::unmuteAll: m = false; break;
  127. case MuteChange::flip: m = ! m; break;
  128. case MuteChange::keep: break;
  129. }
  130. }
  131. }
  132. void changeGain (Song& song, const std::vector<int>& clips, float value, bool relative)
  133. {
  134. for (auto i : clips)
  135. if (i >= 0 && i < (int) song.playlist.size() && song.playlist[(std::size_t) i].kind == ClipKind::audio)
  136. {
  137. auto& g = song.playlist[(std::size_t) i].gain;
  138. g = std::clamp (relative ? g * value : value, 0.0f, 4.0f);
  139. }
  140. }
  141. namespace
  142. {
  143. std::string bbt (std::int64_t ticks, Meter meter, int base)
  144. {
  145. const auto beat = meter.beat(); // (round 190fg: the meter's beats, eighths in 6/8)
  146. const auto bar = meter.bar();
  147. const auto t = std::max<std::int64_t> (0, ticks);
  148. char text[48];
  149. std::snprintf (text, sizeof (text), "%lld:%lld:%03lld", (long long) (t / bar + base), (long long) ((t % bar) / beat + base), (long long) (t % beat));
  150. return text;
  151. }
  152. std::int64_t parseBbt (const std::string& text, Meter meter, int base)
  153. {
  154. long long parts[3] = { 0, base, 0 };
  155. int count = 0;
  156. std::string token;
  157. auto flush = [&] {
  158. if (token.empty() || count >= 3)
  159. return false;
  160. for (auto c : token)
  161. if (c < '0' || c > '9')
  162. return false;
  163. parts[count++] = std::stoll (token);
  164. token.clear();
  165. return true;
  166. };
  167. for (auto c : text)
  168. {
  169. if (c == ' ')
  170. continue;
  171. if (c == ':' || c == '.')
  172. {
  173. if (! flush())
  174. return -1;
  175. }
  176. else
  177. token += c;
  178. }
  179. if (! flush())
  180. return -1;
  181. const auto beat = meter.beat();
  182. if (parts[0] < base || parts[1] < base || parts[1] - base >= std::max (1, meter.beats) || parts[2] >= beat)
  183. return -1;
  184. return (parts[0] - base) * meter.bar() + (parts[1] - base) * beat + parts[2];
  185. }
  186. } // namespace
  187. std::string formatPosition (std::int64_t ticks, Meter meter) { return bbt (ticks, meter, 1); }
  188. std::string formatLength (std::int64_t ticks, Meter meter) { return bbt (ticks, meter, 0); }
  189. std::int64_t parsePosition (const std::string& text, Meter meter) { return parseBbt (text, meter, 1); }
  190. std::int64_t parseLength (const std::string& text, Meter meter) { return parseBbt (text, meter, 0); }
  191. std::vector<std::pair<int, std::string>> sourcesFor (const Song& song, ClipKind kind)
  192. {
  193. std::vector<std::pair<int, std::string>> out;
  194. if (kind == ClipKind::pattern)
  195. for (int i = 0; i < (int) song.patterns.size(); ++i)
  196. {
  197. if (song.patterns[(std::size_t) i].name == "(tail)")
  198. continue;
  199. out.emplace_back (i, song.patterns[(std::size_t) i].name);
  200. }
  201. else if (kind == ClipKind::audio)
  202. {
  203. for (int i = 0; i < (int) song.channels.size(); ++i)
  204. if (song.channels[(std::size_t) i].instrument == InstrumentKind::audioClip)
  205. out.emplace_back (i, song.channels[(std::size_t) i].name);
  206. }
  207. else
  208. for (int i = 0; i < (int) song.automations.size(); ++i)
  209. out.emplace_back (i, song.automations[(std::size_t) i].name);
  210. return out;
  211. }
  212. } // namespace lynx::clipprops
  213. namespace lynx::arrangeops
  214. {
  215. const std::vector<Clip>& clipsOf (const Song& song, int index)
  216. {
  217. static const std::vector<Clip> none;
  218. if (index == song.currentArrangement)
  219. return song.playlist;
  220. if (index < 0 || index >= (int) song.arrangements.size())
  221. return none;
  222. return song.arrangements[(std::size_t) index].clips;
  223. }
  224. std::int64_t lengthOf (const Song& song, int index)
  225. {
  226. std::int64_t end = 0;
  227. for (const auto& c : clipsOf (song, index))
  228. end = std::max (end, c.end());
  229. return end;
  230. }
  231. std::int64_t positionTick (const Song& song, MergePosition position, std::int64_t currentTick)
  232. {
  233. switch (position)
  234. {
  235. case MergePosition::start: return 0;
  236. case MergePosition::current: return std::max<std::int64_t> (0, currentTick);
  237. case MergePosition::end:
  238. {
  239. const auto end = lengthOf (song, song.currentArrangement);
  240. const auto bar = song.barTicks();
  241. return (end + bar - 1) / bar * bar; // the next bar line
  242. }
  243. }
  244. return 0;
  245. }
  246. void clearRange (std::vector<Clip>& clips, std::int64_t from, std::int64_t to)
  247. {
  248. if (to <= from)
  249. return;
  250. std::vector<Clip> out;
  251. for (auto c : clips)
  252. {
  253. if (c.end() <= from || c.start >= to)
  254. {
  255. out.push_back (c);
  256. continue;
  257. }
  258. if (c.start < from) // keep the head
  259. {
  260. auto head = c;
  261. splitClip (head, from);
  262. out.push_back (head);
  263. }
  264. if (c.end() > to) // keep the end
  265. {
  266. auto copy = c;
  267. out.push_back (splitClip (copy, to));
  268. }
  269. }
  270. clips = std::move (out);
  271. }
  272. bool merge (Song& song, int source, MergeMode mode, std::int64_t at)
  273. {
  274. if (source == song.currentArrangement || source < 0 || source >= (int) song.arrangements.size())
  275. return false;
  276. const auto incoming = song.arrangements[(std::size_t) source].clips; // copy: the playlist may grow
  277. const auto length = lengthOf (song, source);
  278. at = std::max<std::int64_t> (0, at);
  279. if (mode == MergeMode::insert && length > 0)
  280. {
  281. // what crosses the position is split; everything after it moves later
  282. std::vector<Clip> moved;
  283. for (auto c : song.playlist)
  284. {
  285. if (c.start < at && c.end() > at)
  286. {
  287. auto rest = splitClip (c, at);
  288. rest.start += length;
  289. moved.push_back (c);
  290. moved.push_back (rest);
  291. }
  292. else
  293. {
  294. if (c.start >= at)
  295. c.start += length;
  296. moved.push_back (c);
  297. }
  298. }
  299. song.playlist = std::move (moved);
  300. for (auto& m : song.markers())
  301. if (m.tick >= at)
  302. m.tick += length;
  303. }
  304. else if (mode == MergeMode::replace)
  305. clearRange (song.playlist, at, at + length);
  306. for (auto c : incoming)
  307. {
  308. c.start += at;
  309. song.playlist.push_back (c);
  310. }
  311. std::stable_sort (song.playlist.begin(), song.playlist.end(), [] (const Clip& a, const Clip& b) {
  312. return a.start != b.start ? a.start < b.start : a.track < b.track;
  313. });
  314. return true;
  315. }
  316. void move (Song& song, int from, int to)
  317. {
  318. const auto n = (int) song.arrangements.size();
  319. if (from < 0 || from >= n || to < 0 || to >= n || from == to)
  320. return;
  321. auto item = std::move (song.arrangements[(std::size_t) from]);
  322. song.arrangements.erase (song.arrangements.begin() + from);
  323. song.arrangements.insert (song.arrangements.begin() + to, std::move (item));
  324. const auto follow = [from, to] (int& cur) {
  325. if (cur == from)
  326. cur = to;
  327. else if (from < cur && to >= cur)
  328. --cur;
  329. else if (from > cur && to <= cur)
  330. ++cur;
  331. };
  332. follow (song.currentArrangement);
  333. for (auto& t : song.playlistTracks) // round 190pe: track versions belong to their arrangement
  334. if (! t.versions.empty())
  335. follow (t.versionArrangement);
  336. }
  337. void rename (Song& song, int index, const std::string& name)
  338. {
  339. if (index >= 0 && index < (int) song.arrangements.size() && ! name.empty())
  340. song.arrangements[(std::size_t) index].name = name;
  341. }
  342. int duplicate (Song& song, int index)
  343. {
  344. if (index < 0 || index >= (int) song.arrangements.size())
  345. return -1;
  346. Arrangement copy = song.arrangements[(std::size_t) index];
  347. if (index == song.currentArrangement)
  348. {
  349. copy.clips = song.playlist;
  350. copy.loopStart = song.loopStart;
  351. copy.loopEnd = song.loopEnd;
  352. }
  353. copy.name += " (copy)";
  354. song.arrangements.insert (song.arrangements.begin() + index + 1, std::move (copy));
  355. if (song.currentArrangement > index)
  356. ++song.currentArrangement;
  357. for (auto& t : song.playlistTracks) // round 190pe: track versions belong to their arrangement (not to the copy)
  358. if (! t.versions.empty() && t.versionArrangement > index)
  359. ++t.versionArrangement;
  360. return index + 1;
  361. }
  362. } // namespace lynx::arrangeops
  363. namespace lynx::trackmodes
  364. {
  365. namespace
  366. {
  367. PlaylistTrack* trackAt (Song& song, int track)
  368. {
  369. if (track < 0 || track >= 500)
  370. return nullptr;
  371. if ((int) song.playlistTracks.size() <= track)
  372. song.playlistTracks.resize ((std::size_t) track + 1);
  373. return &song.playlistTracks[(std::size_t) track];
  374. }
  375. bool linkedElsewhere (const Song& song, int mixer, int exceptTrack)
  376. {
  377. for (int i = 0; i < (int) song.playlistTracks.size(); ++i)
  378. if (i != exceptTrack && song.playlistTracks[(std::size_t) i].mode != PlaylistTrackMode::unassigned
  379. && song.playlistTracks[(std::size_t) i].mixerTrack == mixer)
  380. return true;
  381. return false;
  382. }
  383. std::string trackName (const Song& song, int track)
  384. {
  385. if (track < (int) song.playlistTracks.size() && ! song.playlistTracks[(std::size_t) track].name.empty())
  386. return song.playlistTracks[(std::size_t) track].name;
  387. return "Track " + std::to_string (track + 1);
  388. }
  389. } // namespace
  390. int freeMixerTrack (Song& song)
  391. {
  392. const auto isFree = [&song] (int t) {
  393. const auto& m = song.mixer[(std::size_t) t];
  394. if (! m.effects.empty() || linkedElsewhere (song, t, -1) || song.session.reservedForOther (true, t))
  395. return false;
  396. for (const auto& c : song.channels)
  397. if (c.mixerTrack == t)
  398. return false;
  399. for (int from = 1; from < (int) song.mixer.size(); ++from)
  400. if (from != t)
  401. for (const auto& s : song.mixer[(std::size_t) from].sends)
  402. if (s.target == t)
  403. return false;
  404. return true;
  405. };
  406. for (int t = 1; t < (int) song.mixer.size(); ++t)
  407. if (isFree (t))
  408. return t;
  409. return addMixerTrack (song);
  410. }
  411. bool makeAudioTrack (Song& song, int track)
  412. {
  413. if (track < 0 || track >= 500)
  414. return false;
  415. auto mixer = track < (int) song.playlistTracks.size() && song.playlistTracks[(std::size_t) track].mode == PlaylistTrackMode::audio
  416. ? song.playlistTracks[(std::size_t) track].mixerTrack : -1;
  417. if (mixer <= 0)
  418. mixer = freeMixerTrack (song);
  419. if (mixer <= 0)
  420. return false;
  421. auto* t = trackAt (song, track);
  422. t->mode = PlaylistTrackMode::audio;
  423. t->mixerTrack = mixer;
  424. t->channel = -1;
  425. auto& m = song.mixer[(std::size_t) mixer];
  426. m.name = trackName (song, track);
  427. if (t->colour != 0)
  428. m.colour = t->colour;
  429. // the audio clips already on the track play through it from now on
  430. std::vector<int> onTrack;
  431. for (int i = 0; i < (int) song.playlist.size(); ++i)
  432. if (song.playlist[(std::size_t) i].track == track)
  433. onTrack.push_back (i);
  434. followAudioTracks (song, onTrack);
  435. return true;
  436. }
  437. bool makeInstrumentTrack (Song& song, int track, int channel)
  438. {
  439. if (track < 0 || track >= 500 || channel < 0 || channel >= (int) song.channels.size())
  440. return false;
  441. const auto kind = song.channels[(std::size_t) channel].instrument;
  442. if (kind == InstrumentKind::audioClip || kind == InstrumentKind::controller)
  443. return false; // FL: "Only generators can be used as instrument tracks."
  444. // a channel belongs to one instrument track
  445. for (int i = 0; i < (int) song.playlistTracks.size(); ++i)
  446. if (i != track && song.playlistTracks[(std::size_t) i].mode == PlaylistTrackMode::instrument
  447. && song.playlistTracks[(std::size_t) i].channel == channel)
  448. unassign (song, i);
  449. auto mixer = song.channels[(std::size_t) channel].mixerTrack;
  450. if (mixer <= 0 || mixer >= (int) song.mixer.size() || linkedElsewhere (song, mixer, track))
  451. mixer = freeMixerTrack (song);
  452. if (mixer <= 0)
  453. return false;
  454. auto* t = trackAt (song, track);
  455. if (t->mode != PlaylistTrackMode::instrument) // round 190ne: a new instrument track takes the layout's "Clips own their notes"
  456. t->ownClips = trackclips::defaultOn();
  457. t->mode = PlaylistTrackMode::instrument;
  458. t->channel = channel;
  459. t->mixerTrack = mixer;
  460. song.channels[(std::size_t) channel].mixerTrack = mixer;
  461. if (t->name.empty())
  462. t->name = song.channels[(std::size_t) channel].name;
  463. return true;
  464. }
  465. bool canLinkToMixerTrack (const Song& song, int track, int mixer)
  466. {
  467. return track >= 0 && track < 500 && mixer > 0 && mixer < (int) song.mixer.size() && ! linkedElsewhere (song, mixer, track)
  468. && ! song.session.reservedForOther (true, mixer);
  469. }
  470. bool hasOwnName (const Song& song, int mixer)
  471. {
  472. if (mixer <= 0 || mixer >= (int) song.mixer.size())
  473. return false;
  474. const auto& name = song.mixer[(std::size_t) mixer].name;
  475. return ! name.empty() && name != "Insert " + std::to_string (mixer);
  476. }
  477. bool canAddMixerTrack (const Song& song)
  478. {
  479. if (song.mixer.empty())
  480. return false;
  481. for (int t = (int) song.mixer.size(); t < maxMixerTracks; ++t)
  482. if (! song.session.reservedForOther (true, t))
  483. return true;
  484. return false;
  485. }
  486. int addMixerTrack (Song& song)
  487. {
  488. if (! canAddMixerTrack (song))
  489. return -1;
  490. // inserts reserved for another participant are added as plain inserts and skipped (Song::claimFreeInsert's rule)
  491. while (true)
  492. {
  493. auto& added = song.mixer.emplace_back();
  494. added.name = "Insert " + std::to_string (song.mixer.size() - 1);
  495. mixerops::giveNewTrackSlots (added); // (FL 2026.2's "Number of effect slots on new mixer tracks", round 190qf)
  496. if (const auto t = (int) song.mixer.size() - 1; ! song.session.reservedForOther (true, t))
  497. return t;
  498. }
  499. }
  500. bool linkToMixerTrack (Song& song, int track, int mixer)
  501. {
  502. if (! canLinkToMixerTrack (song, track, mixer))
  503. return false;
  504. auto* t = trackAt (song, track);
  505. if (t->mode == PlaylistTrackMode::audio && t->mixerTrack == mixer)
  506. return true;
  507. auto& m = song.mixer[(std::size_t) mixer];
  508. const auto insertName = hasOwnName (song, mixer) ? m.name : "Insert " + std::to_string (mixer);
  509. if (t->mode == PlaylistTrackMode::unassigned)
  510. {
  511. // FL (FUN_012f38f0): the insert's colour when the track has the default one; the insert's own name, else the
  512. // track's, else "Insert N"
  513. if (t->colour == 0)
  514. t->colour = m.colour;
  515. if (hasOwnName (song, mixer) || t->name.empty())
  516. t->name = insertName;
  517. }
  518. else if (t->mode == PlaylistTrackMode::audio && t->mixerTrack > 0 && t->mixerTrack < (int) song.mixer.size())
  519. {
  520. // FL: the previous insert is reset (name, colour; its effects and routing stay)
  521. auto& old = song.mixer[(std::size_t) t->mixerTrack];
  522. old.name = "Insert " + std::to_string (t->mixerTrack);
  523. old.colour = 0;
  524. }
  525. t->mode = PlaylistTrackMode::audio;
  526. t->mixerTrack = mixer;
  527. t->channel = -1;
  528. // FL: the track and the insert end with one name and colour
  529. if (t->name.empty())
  530. {
  531. t->name = insertName;
  532. t->colour = m.colour;
  533. }
  534. else
  535. {
  536. m.name = t->name;
  537. m.colour = t->colour;
  538. }
  539. std::vector<int> onTrack;
  540. for (int i = 0; i < (int) song.playlist.size(); ++i)
  541. if (song.playlist[(std::size_t) i].track == track)
  542. onTrack.push_back (i);
  543. followAudioTracks (song, onTrack);
  544. return true;
  545. }
  546. LinkedNames linkedNames (const Song& song)
  547. {
  548. LinkedNames n;
  549. for (int t = 0; t < (int) song.playlistTracks.size(); ++t)
  550. {
  551. const auto& pt = song.playlistTracks[(std::size_t) t];
  552. if (pt.mode != PlaylistTrackMode::audio || pt.mixerTrack <= 0 || pt.mixerTrack >= (int) song.mixer.size())
  553. continue;
  554. const auto& mt = song.mixer[(std::size_t) pt.mixerTrack];
  555. n.pairs.push_back ({ t, pt.mixerTrack, pt.name, pt.colour, mt.name, mt.colour });
  556. }
  557. return n;
  558. }
  559. int followLinkedNames (Song& song, const LinkedNames& before)
  560. {
  561. int changed = 0;
  562. for (const auto& p : before.pairs)
  563. {
  564. if (p.track >= (int) song.playlistTracks.size() || p.mixer >= (int) song.mixer.size())
  565. continue;
  566. auto& t = song.playlistTracks[(std::size_t) p.track];
  567. if (t.mode != PlaylistTrackMode::audio || t.mixerTrack != p.mixer)
  568. continue; // (unlinked or relinked by the edit: the link itself set both)
  569. auto& m = song.mixer[(std::size_t) p.mixer];
  570. const auto trackChanged = t.name != p.trackName || t.colour != p.trackColour;
  571. const auto insertChanged = m.name != p.insertName || m.colour != p.insertColour;
  572. if (trackChanged && ! insertChanged)
  573. {
  574. // FUN_012e6010: the mixer track takes the track's colour and name (an unnamed track: FL's empty mixer name)
  575. m.colour = t.colour;
  576. m.name = t.name.empty() ? "Insert " + std::to_string (p.mixer) : t.name;
  577. ++changed;
  578. }
  579. else if (insertChanged && ! trackChanged)
  580. {
  581. // FUN_012be6f0: the linked Playlist track takes the mixer track's name and colour
  582. t.colour = m.colour;
  583. t.name = hasOwnName (song, p.mixer) ? m.name : std::string();
  584. ++changed;
  585. }
  586. }
  587. return changed;
  588. }
  589. int assignAudioTracks (Song& song, const std::vector<int>& tracks, int mixer)
  590. {
  591. if (mixer != newMixerTrack && (mixer <= 0 || mixer >= (int) song.mixer.size()))
  592. return 0;
  593. int linked = 0;
  594. auto next = mixer;
  595. for (std::size_t i = 0; i < tracks.size(); ++i)
  596. {
  597. if (next == newMixerTrack || next >= (int) song.mixer.size())
  598. {
  599. next = addMixerTrack (song);
  600. if (next < 0)
  601. break;
  602. }
  603. // a refused track (LYNX: the pick is linked to another selected track, e.g. the menu's own) leaves the insert
  604. // to the next one
  605. if (! linkToMixerTrack (song, tracks[i], next))
  606. continue;
  607. ++linked;
  608. // FL: the next insert no Playlist track is linked to (LYNX: nor reserved for another participant)
  609. do
  610. ++next;
  611. while (next < (int) song.mixer.size() && (linkedElsewhere (song, next, -1) || song.session.reservedForOther (true, next)));
  612. if (next >= maxMixerTracks)
  613. break;
  614. }
  615. return linked;
  616. }
  617. AudioTrackMenu audioTrackMenu (const Song& song, int track)
  618. {
  619. AudioTrackMenu menu;
  620. menu.canAddMixerTrack = canAddMixerTrack (song);
  621. const auto* t = track >= 0 && track < (int) song.playlistTracks.size() ? &song.playlistTracks[(std::size_t) track] : nullptr;
  622. for (int m = 1; m < (int) song.mixer.size(); ++m)
  623. {
  624. auto& item = menu.inserts.emplace_back();
  625. item.mixer = m;
  626. item.name = song.mixer[(std::size_t) m].name;
  627. // FL ticks the insert the track is stored as linked to, whatever its mode
  628. item.ticked = t != nullptr && t->mode != PlaylistTrackMode::unassigned && t->mixerTrack == m;
  629. item.enabled = item.ticked || canLinkToMixerTrack (song, track, m);
  630. }
  631. return menu;
  632. }
  633. int instrumentTrackPattern (Song& song, int track)
  634. {
  635. if (track < 0 || track >= (int) song.playlistTracks.size())
  636. return -1;
  637. const auto& t = song.playlistTracks[(std::size_t) track];
  638. if (t.mode != PlaylistTrackMode::instrument || t.channel < 0 || t.channel >= (int) song.channels.size())
  639. return -1;
  640. const auto channel = t.channel;
  641. const auto& name = song.channels[(std::size_t) channel].name;
  642. for (int p = 0; p < (int) song.patterns.size(); ++p)
  643. {
  644. const auto& pattern = song.patterns[(std::size_t) p];
  645. if (pattern.name == name && std::all_of (pattern.notes.begin(), pattern.notes.end(), [channel] (const Note& n) { return n.channel == channel; }))
  646. return p;
  647. }
  648. Pattern made;
  649. made.name = name;
  650. made.colour = song.channels[(std::size_t) channel].colour;
  651. song.patterns.push_back (std::move (made));
  652. return (int) song.patterns.size() - 1;
  653. }
  654. void unassign (Song& song, int track)
  655. {
  656. if (track < 0 || track >= (int) song.playlistTracks.size())
  657. return;
  658. auto& t = song.playlistTracks[(std::size_t) track];
  659. t.mode = PlaylistTrackMode::unassigned;
  660. t.mixerTrack = -1;
  661. t.channel = -1;
  662. }
  663. int firstFreeTrack (const Song& song)
  664. {
  665. std::vector<bool> used (500, false);
  666. for (const auto& c : song.playlist)
  667. if (c.track >= 0 && c.track < 500)
  668. used[(std::size_t) c.track] = true;
  669. for (int t = 0; t < 500; ++t)
  670. if (! used[(std::size_t) t] && (t >= (int) song.playlistTracks.size() || song.playlistTracks[(std::size_t) t].mode == PlaylistTrackMode::unassigned))
  671. return t;
  672. return -1;
  673. }
  674. int addAudioTrack (Song& song)
  675. {
  676. const auto track = firstFreeTrack (song);
  677. if (track < 0)
  678. return -1;
  679. const auto mixerBefore = song.mixer.size();
  680. const auto mixer = freeMixerTrack (song);
  681. if (mixer <= 0 || assignAudioTracks (song, { track }, mixer) != 1)
  682. {
  683. if (song.mixer.size() > mixerBefore) // (an insert freeMixerTrack added for nothing goes again)
  684. song.mixer.resize (mixerBefore);
  685. return -1;
  686. }
  687. return track;
  688. }
  689. bool canBeInstrumentTrack (const Song& song, int channel)
  690. {
  691. if (channel < 0 || channel >= (int) song.channels.size())
  692. return false;
  693. const auto kind = song.channels[(std::size_t) channel].instrument;
  694. return kind != InstrumentKind::audioClip && kind != InstrumentKind::controller;
  695. }
  696. std::vector<int> instrumentTracksOf (const Song& song, int channel)
  697. {
  698. std::vector<int> tracks;
  699. for (int i = 0; i < (int) song.playlistTracks.size(); ++i)
  700. if (song.playlistTracks[(std::size_t) i].mode == PlaylistTrackMode::instrument && song.playlistTracks[(std::size_t) i].channel == channel)
  701. tracks.push_back (i);
  702. return tracks;
  703. }
  704. int assignToNewInstrumentTrack (Song& song, int channel)
  705. {
  706. if (! canBeInstrumentTrack (song, channel))
  707. return -1;
  708. const auto track = firstFreeTrack (song);
  709. if (track < 0)
  710. return -1;
  711. const auto before = song;
  712. if (! makeInstrumentTrack (song, track, channel))
  713. {
  714. song = before;
  715. return -1;
  716. }
  717. return track;
  718. }
  719. int unassignFromInstrumentTracks (Song& song, int channel)
  720. {
  721. const auto tracks = instrumentTracksOf (song, channel);
  722. for (const auto t : tracks)
  723. unassign (song, t);
  724. return (int) tracks.size();
  725. }
  726. int addInstrumentTrack (Song& song, ChannelDef generator, int track)
  727. {
  728. if (generator.instrument == InstrumentKind::audioClip || generator.instrument == InstrumentKind::controller)
  729. return -1;
  730. if (track < 0)
  731. track = firstFreeTrack (song);
  732. if (track < 0 || track >= 500 || (track < (int) song.playlistTracks.size() && song.playlistTracks[(std::size_t) track].mode == PlaylistTrackMode::instrument))
  733. return -1;
  734. const auto before = song;
  735. generator.mixerTrack = 0; // (makeInstrumentTrack gives it a free mixer track)
  736. song.channels.push_back (std::move (generator));
  737. const auto channel = (int) song.channels.size() - 1;
  738. if (! makeInstrumentTrack (song, track, channel))
  739. {
  740. song = before;
  741. return -1;
  742. }
  743. return channel;
  744. }
  745. std::string describe (const Song& song, int track)
  746. {
  747. if (track < 0 || track >= (int) song.playlistTracks.size())
  748. return {};
  749. const auto& t = song.playlistTracks[(std::size_t) track];
  750. if (t.mode == PlaylistTrackMode::instrument)
  751. return "(Instrument track)";
  752. if (t.mode == PlaylistTrackMode::audio)
  753. return "(Audio track, linked to mixer track " + std::to_string (t.mixerTrack) + ")";
  754. return {};
  755. }
  756. RouteSummary routeSummary (const Song& song, int track)
  757. {
  758. RouteSummary r;
  759. const auto channelCount = song.channels.size();
  760. std::vector<char> plays (channelCount, 0), patternSeen (song.patterns.size(), 0);
  761. for (const auto& c : song.playlist)
  762. {
  763. if (c.track != track)
  764. continue;
  765. if (c.kind == ClipKind::audio)
  766. {
  767. r.clips = true;
  768. if (c.channel >= 0 && (std::size_t) c.channel < channelCount)
  769. plays[(std::size_t) c.channel] = 1;
  770. }
  771. else if (c.kind == ClipKind::pattern)
  772. {
  773. r.clips = true;
  774. if (c.pattern < 0 || (std::size_t) c.pattern >= song.patterns.size() || patternSeen[(std::size_t) c.pattern])
  775. continue;
  776. patternSeen[(std::size_t) c.pattern] = 1;
  777. for (const auto& n : song.patterns[(std::size_t) c.pattern].notes)
  778. if (n.channel >= 0 && (std::size_t) n.channel < channelCount)
  779. plays[(std::size_t) n.channel] = 1;
  780. }
  781. }
  782. for (std::size_t ch = 0; ch < channelCount; ++ch)
  783. if (plays[ch])
  784. {
  785. r.channels.push_back ((int) ch);
  786. r.inserts.push_back (std::max (0, song.channels[ch].mixerTrack));
  787. }
  788. std::sort (r.inserts.begin(), r.inserts.end());
  789. r.inserts.erase (std::unique (r.inserts.begin(), r.inserts.end()), r.inserts.end());
  790. return r;
  791. }
  792. int armableInsert (const Song& song, int track)
  793. {
  794. if (track < 0 || track >= (int) song.playlistTracks.size())
  795. return -1;
  796. const auto& t = song.playlistTracks[(std::size_t) track];
  797. return t.mode == PlaylistTrackMode::audio && t.mixerTrack > 0 && t.mixerTrack < (int) song.mixer.size() ? t.mixerTrack : -1;
  798. }
  799. int meteredInsert (const Song& song, int track, const HeaderOptions& options)
  800. {
  801. if (track < 0 || track >= (int) song.playlistTracks.size())
  802. return -1;
  803. const auto& t = song.playlistTracks[(std::size_t) track];
  804. if (t.mode == PlaylistTrackMode::audio)
  805. return options.audioLevels ? armableInsert (song, track) : -1;
  806. if (t.mode != PlaylistTrackMode::instrument || ! options.instrumentLevels)
  807. return -1;
  808. // the channel's own route (it may have moved since the track was made); the linked mixer track without a channel
  809. if (t.channel >= 0 && t.channel < (int) song.channels.size())
  810. {
  811. const auto m = song.channels[(std::size_t) t.channel].mixerTrack;
  812. return m >= 0 && m < (int) song.mixer.size() ? m : -1;
  813. }
  814. return t.mixerTrack >= 0 && t.mixerTrack < (int) song.mixer.size() ? t.mixerTrack : -1;
  815. }
  816. namespace
  817. {
  818. int audioMixerOf (const Song& song, int track)
  819. {
  820. if (track < 0 || track >= (int) song.playlistTracks.size())
  821. return -1;
  822. const auto& t = song.playlistTracks[(std::size_t) track];
  823. return t.mode == PlaylistTrackMode::audio && t.mixerTrack > 0 && t.mixerTrack < (int) song.mixer.size() ? t.mixerTrack : -1;
  824. }
  825. // the mixer track a touched clip asks for its channel, or -1 when it needs nothing (not an audio clip on an audio
  826. // track, or its channel already plays there)
  827. int wantedMixer (const Song& song, const Clip& clip)
  828. {
  829. const auto mixer = audioMixerOf (song, clip.track);
  830. if (clip.kind != ClipKind::audio || mixer <= 0 || clip.channel < 0 || clip.channel >= (int) song.channels.size()
  831. || song.channels[(std::size_t) clip.channel].mixerTrack == mixer)
  832. return -1;
  833. return mixer;
  834. }
  835. // FL's test: another clip (any arrangement) of the same channel on an audio track linked to another mixer track
  836. bool sharedWithOtherAudioTrack (const Song& song, const Clip& clip, int mixer)
  837. {
  838. bool shared = false;
  839. song.forEachClipList ([&] (const std::vector<Clip>& clips) {
  840. for (const auto& other : clips)
  841. if (&other != &clip && other.kind == ClipKind::audio && other.channel == clip.channel)
  842. if (const auto m = audioMixerOf (song, other.track); m > 0 && m != mixer)
  843. shared = true;
  844. });
  845. return shared;
  846. }
  847. } // namespace
  848. std::vector<int> audioTrackConflicts (const Song& song, const std::vector<int>& touched)
  849. {
  850. std::vector<int> channels;
  851. for (const auto i : touched)
  852. {
  853. if (i < 0 || i >= (int) song.playlist.size())
  854. continue;
  855. const auto& clip = song.playlist[(std::size_t) i];
  856. const auto mixer = wantedMixer (song, clip);
  857. if (mixer > 0 && std::find (channels.begin(), channels.end(), clip.channel) == channels.end()
  858. && sharedWithOtherAudioTrack (song, clip, mixer))
  859. channels.push_back (clip.channel);
  860. }
  861. return channels;
  862. }
  863. int followAudioTracks (Song& song, const std::vector<int>& touched, bool makeUnique)
  864. {
  865. int changed = 0;
  866. std::vector<int> done; // (FL: the touched clips of a channel it asked about are handled together)
  867. for (const auto i : touched)
  868. {
  869. if (i < 0 || i >= (int) song.playlist.size() || std::find (done.begin(), done.end(), i) != done.end())
  870. continue;
  871. const auto clip = song.playlist[(std::size_t) i];
  872. const auto mixer = wantedMixer (song, clip);
  873. if (mixer <= 0)
  874. continue;
  875. if (! sharedWithOtherAudioTrack (song, song.playlist[(std::size_t) i], mixer))
  876. {
  877. song.channels[(std::size_t) clip.channel].mixerTrack = mixer; // every clip of the channel follows (FL)
  878. ++changed;
  879. continue;
  880. }
  881. // "Make clip unique?": every touched clip of this channel gets the copy (Yes) or stays as it is (No)
  882. auto copy = -1;
  883. if (makeUnique)
  884. {
  885. auto def = song.channels[(std::size_t) clip.channel];
  886. def.mixerTrack = mixer;
  887. song.channels.push_back (def);
  888. copy = (int) song.channels.size() - 1;
  889. }
  890. for (const auto j : touched)
  891. if (j >= 0 && j < (int) song.playlist.size() && song.playlist[(std::size_t) j].channel == clip.channel
  892. && song.playlist[(std::size_t) j].kind == ClipKind::audio && std::find (done.begin(), done.end(), j) == done.end())
  893. {
  894. done.push_back (j);
  895. if (copy >= 0)
  896. {
  897. song.playlist[(std::size_t) j].channel = copy;
  898. ++changed;
  899. }
  900. }
  901. }
  902. return changed;
  903. }
  904. HeaderDropOptions& headerDropOptions() noexcept
  905. {
  906. static HeaderDropOptions options;
  907. return options;
  908. }
  909. bool dropOnHeader (Song& song, int track, const std::string& path, const std::string& name, std::int64_t lengthTicks, HeaderDrop how, bool lock)
  910. {
  911. if (track < 0 || track >= 500 || how == HeaderDrop::ask || path.empty())
  912. return false;
  913. if (track < (int) song.playlistTracks.size() && song.playlistTracks[(std::size_t) track].mode != PlaylistTrackMode::unassigned)
  914. return false; // (FUN_00e71980: only an unassigned track takes a header drop)
  915. const auto before = song; // (all or nothing)
  916. auto fail = [&song, &before] {
  917. song = before;
  918. return false;
  919. };
  920. auto newChannel = [&song, &name, &path] (InstrumentKind kind) {
  921. ChannelDef c;
  922. c.name = quickrender::uniqueChannelName (song, name);
  923. c.instrument = kind;
  924. c.samplePath = path;
  925. if (kind == InstrumentKind::audioClip)
  926. c.sampler.declickMode = audioClipDeclickMode; // FL's AudioTrack.fst
  927. song.channels.push_back (c);
  928. return (int) song.channels.size() - 1;
  929. };
  930. auto placeAudio = [&song, track, lengthTicks] (int channel) {
  931. Clip clip;
  932. clip.kind = ClipKind::audio;
  933. clip.channel = channel;
  934. clip.track = track;
  935. clip.start = 0;
  936. clip.length = std::max<std::int64_t> (1, lengthTicks);
  937. song.playlist.push_back (clip);
  938. followAudioTracks (song, { (int) song.playlist.size() - 1 });
  939. };
  940. if (how == HeaderDrop::audioClips)
  941. {
  942. int channel = -1;
  943. for (int c = 0; c < (int) song.channels.size(); ++c)
  944. if (song.channels[(std::size_t) c].instrument == InstrumentKind::audioClip && song.channels[(std::size_t) c].samplePath == path)
  945. channel = c;
  946. if (channel < 0)
  947. {
  948. channel = newChannel (InstrumentKind::audioClip);
  949. song.channels[(std::size_t) channel].mixerTrack = song.claimFreeInsert();
  950. }
  951. auto* t = trackAt (song, track);
  952. t->name = song.channels[(std::size_t) channel].name;
  953. if (const auto colour = song.channels[(std::size_t) channel].colour; colour != 0)
  954. t->colour = colour;
  955. placeAudio (channel);
  956. }
  957. else if (how == HeaderDrop::audioTracks)
  958. {
  959. const auto channel = newChannel (InstrumentKind::audioClip);
  960. auto* t = trackAt (song, track);
  961. t->name = song.channels[(std::size_t) channel].name;
  962. if (! makeAudioTrack (song, track))
  963. return fail();
  964. placeAudio (channel); // (routed to the track's mixer track by followAudioTracks)
  965. }
  966. else
  967. {
  968. const auto channel = newChannel (InstrumentKind::sampler);
  969. trackAt (song, track)->name.clear(); // (named after its channel)
  970. if (! makeInstrumentTrack (song, track, channel))
  971. return fail();
  972. const auto pattern = instrumentTrackPattern (song, track);
  973. if (pattern < 0)
  974. return fail();
  975. Clip clip;
  976. clip.kind = ClipKind::pattern;
  977. clip.pattern = pattern;
  978. clip.track = track;
  979. clip.start = 0;
  980. clip.length = song.patterns[(std::size_t) pattern].effectiveLength (song.meter());
  981. song.playlist.push_back (clip);
  982. }
  983. if (lock)
  984. trackAt (song, track)->lockedToContent = true;
  985. song.mode = PlayMode::song;
  986. return true;
  987. }
  988. } // namespace lynx::trackmodes
  989. namespace lynx::audioclips
  990. {
  991. int repairChannels (Song& song)
  992. {
  993. int changed = 0;
  994. const auto channels = (int) song.channels.size(); // (copies added below are audio clip channels already)
  995. for (int index = 0; index < channels; ++index)
  996. {
  997. if (song.channels[(std::size_t) index].instrument != InstrumentKind::sampler || song.channels[(std::size_t) index].samplePath.empty())
  998. continue;
  999. auto used = false;
  1000. song.forEachClipList ([&] (const std::vector<Clip>& clips) {
  1001. for (const auto& c : clips)
  1002. used = used || (c.kind == ClipKind::audio && c.channel == index);
  1003. });
  1004. if (! used)
  1005. continue;
  1006. const auto playsNotes = std::any_of (song.patterns.begin(), song.patterns.end(), [index] (const Pattern& p) {
  1007. return std::any_of (p.notes.begin(), p.notes.end(), [index] (const Note& n) { return n.channel == index; });
  1008. });
  1009. ++changed;
  1010. if (! playsNotes)
  1011. {
  1012. song.channels[(std::size_t) index].instrument = InstrumentKind::audioClip;
  1013. continue;
  1014. }
  1015. auto copy = song.channels[(std::size_t) index];
  1016. copy.instrument = InstrumentKind::audioClip;
  1017. copy.instance.renew(); // (a new channel: the renderer must not take it for the Sampler it was copied from)
  1018. song.channels.push_back (std::move (copy));
  1019. const auto target = (int) song.channels.size() - 1;
  1020. song.forEachClipList ([&] (std::vector<Clip>& clips) {
  1021. for (auto& c : clips)
  1022. if (c.kind == ClipKind::audio && c.channel == index)
  1023. c.channel = target;
  1024. });
  1025. }
  1026. return changed;
  1027. }
  1028. void loadSampleInto (ChannelDef& channel, const std::string& path, const std::string& name, bool autoName)
  1029. {
  1030. channel.samplePath = path;
  1031. samplehistory::add (path); // FL's Sample history (round 190df): every sample a channel loads
  1032. if (channel.instrument == InstrumentKind::granulizer || channel.instrument == InstrumentKind::wavetraveller)
  1033. return; // FL's Granulizer keeps its engine and plays the file (round 142); so does Wave Traveller
  1034. if (channel.instrument != InstrumentKind::audioClip)
  1035. channel.instrument = InstrumentKind::sampler;
  1036. // FL's General › "Auto name channels" (round 190bx): off = a channel that has a name keeps it
  1037. if (autoName || channel.name.empty())
  1038. channel.name = name;
  1039. }
  1040. bool replaceClipSample (Song& song, int clip, const std::string& path, const std::string& name, std::int64_t oldTicks,
  1041. std::int64_t newTicks, bool autoName)
  1042. {
  1043. if (clip < 0 || clip >= (int) song.playlist.size() || path.empty())
  1044. return false;
  1045. const auto index = song.playlist[(std::size_t) clip].channel;
  1046. if (song.playlist[(std::size_t) clip].kind != ClipKind::audio || index < 0 || index >= (int) song.channels.size())
  1047. return false;
  1048. auto& channel = song.channels[(std::size_t) index];
  1049. if (channel.instrument != InstrumentKind::audioClip)
  1050. return false;
  1051. loadSampleInto (channel, path, name, autoName);
  1052. channel.zones.clear();
  1053. channel.sampler.slices.clear();
  1054. channel.sampler.loopStart = channel.sampler.loopEnd = -1;
  1055. channel.sampler.warp.clear();
  1056. channel.sampler.stretchBeats = 0.0f;
  1057. channel.sampler.stretchMul = 0;
  1058. if (oldTicks <= 0 || newTicks <= 0)
  1059. return true;
  1060. song.forEachClipList ([&] (std::vector<Clip>& clips) {
  1061. for (auto& c : clips)
  1062. {
  1063. if (c.kind != ClipKind::audio || c.channel != index || c.offset != 0 || std::abs (c.offsetFraction) > 1.0e-3f)
  1064. continue;
  1065. const auto stretch = c.stretch > 0.0 ? c.stretch : 1.0;
  1066. const auto whole = (double) oldTicks * stretch;
  1067. if (std::abs ((double) c.length - whole) > std::max (1.0, whole * 0.002)) // (a trimmed clip keeps its length)
  1068. continue;
  1069. c.length = std::max<std::int64_t> (1, (std::int64_t) std::llround ((double) newTicks * stretch));
  1070. c.fadeIn = std::min (c.fadeIn, c.length);
  1071. c.fadeOut = std::min (c.fadeOut, c.length - c.fadeIn);
  1072. }
  1073. });
  1074. return true;
  1075. }
  1076. } // namespace lynx::audioclips
  1077. namespace lynx::autoclipplace
  1078. {
  1079. Place forNewClip (const Song& song, bool atPlayback, std::int64_t playTick)
  1080. {
  1081. const auto bar = song.barTicks();
  1082. const auto songEnd = song.playlist.empty() ? (std::int64_t) 0 : song.songLength();
  1083. Place place;
  1084. if (song.hasLoop())
  1085. {
  1086. place.start = song.loopStart;
  1087. place.length = song.loopEnd - song.loopStart;
  1088. }
  1089. else
  1090. {
  1091. // FL (checked live, round 177b): the clip starts at the song position even past the last clip, and is one bar
  1092. // long when nothing lies after it.
  1093. place.start = atPlayback ? std::max ((std::int64_t) 0, playTick) : 0;
  1094. place.length = std::max (bar, songEnd - place.start);
  1095. }
  1096. // FL (checked live, round 177b): the first track with no clips at all; a free gap on a used track is not taken
  1097. auto busy = [&] (int track) {
  1098. if (track < (int) song.playlistTracks.size() && song.playlistTracks[(std::size_t) track].mode == PlaylistTrackMode::instrument)
  1099. return true;
  1100. for (const auto& c : song.playlist)
  1101. if (c.track == track)
  1102. return true;
  1103. return false;
  1104. };
  1105. while (busy (place.track))
  1106. ++place.track;
  1107. return place;
  1108. }
  1109. } // namespace lynx::autoclipplace
  1110. namespace lynx::autolanes
  1111. {
  1112. int ownerTrack (const Song& song, int automation)
  1113. {
  1114. for (int t = 0; t < (int) song.playlistTracks.size(); ++t)
  1115. for (const auto& l : song.playlistTracks[(std::size_t) t].lanes)
  1116. if (l.automation == automation)
  1117. return t;
  1118. return -1;
  1119. }
  1120. namespace
  1121. {
  1122. std::int64_t songEnd (const Song& song)
  1123. {
  1124. const auto bar = song.barTicks();
  1125. return std::max (bar * 4, song.playlist.empty() ? 0 : song.songLength());
  1126. }
  1127. int laneFor (const Song& song, int track, const AutomationTarget& target)
  1128. {
  1129. if (track < 0 || track >= (int) song.playlistTracks.size())
  1130. return -1;
  1131. for (const auto& l : song.playlistTracks[(std::size_t) track].lanes)
  1132. if (l.automation >= 0 && l.automation < (int) song.automations.size() && song.automations[(std::size_t) l.automation].target == target)
  1133. return l.automation;
  1134. return -1;
  1135. }
  1136. } // namespace
  1137. int trackFor (const Song& song, const AutomationTarget& target)
  1138. {
  1139. const auto& tracks = song.playlistTracks;
  1140. for (int t = 0; t < (int) tracks.size(); ++t)
  1141. if (laneFor (song, t, target) >= 0)
  1142. return t;
  1143. int channel = -1, insert = -1;
  1144. switch (target.kind)
  1145. {
  1146. case AutomationTargetKind::channelVolume:
  1147. case AutomationTargetKind::channelPan:
  1148. case AutomationTargetKind::channelParam:
  1149. case AutomationTargetKind::channelAdjust:
  1150. channel = target.index;
  1151. if (channel >= 0 && channel < (int) song.channels.size())
  1152. insert = song.channels[(std::size_t) channel].mixerTrack;
  1153. break;
  1154. case AutomationTargetKind::mixerVolume:
  1155. case AutomationTargetKind::mixerPan:
  1156. case AutomationTargetKind::effectParam:
  1157. case AutomationTargetKind::effectMix:
  1158. case AutomationTargetKind::mixerSend:
  1159. insert = target.index;
  1160. break;
  1161. case AutomationTargetKind::none:
  1162. case AutomationTargetKind::tempo:
  1163. case AutomationTargetKind::crossfader: // (round 190kc)
  1164. break;
  1165. }
  1166. if (channel >= 0)
  1167. for (int t = 0; t < (int) tracks.size(); ++t)
  1168. if (tracks[(std::size_t) t].mode == PlaylistTrackMode::instrument && tracks[(std::size_t) t].channel == channel)
  1169. return t;
  1170. if (insert > 0)
  1171. for (int t = 0; t < (int) tracks.size(); ++t)
  1172. if (tracks[(std::size_t) t].mode != PlaylistTrackMode::unassigned && tracks[(std::size_t) t].mixerTrack == insert)
  1173. return t;
  1174. for (int t = 0; t < 500; ++t)
  1175. {
  1176. if (t < (int) tracks.size() && ! tracks[(std::size_t) t].lanes.empty())
  1177. continue;
  1178. if (std::none_of (song.playlist.begin(), song.playlist.end(), [t] (const Clip& c) { return c.track == t; }))
  1179. return t;
  1180. }
  1181. return 0;
  1182. }
  1183. int addLane (Song& song, int track, const AutomationTarget& target, const std::string& name, float value)
  1184. {
  1185. if (track < 0 || track >= 500)
  1186. return -1;
  1187. if (const auto existing = laneFor (song, track, target); existing >= 0)
  1188. return existing;
  1189. if ((int) song.playlistTracks.size() <= track)
  1190. song.playlistTracks.resize ((std::size_t) track + 1);
  1191. Automation a;
  1192. a.name = name;
  1193. a.target = target;
  1194. a.points = { { 0, value, 0.0f }, { songEnd (song), value, 0.0f } };
  1195. song.automations.push_back (std::move (a));
  1196. const auto index = (int) song.automations.size() - 1;
  1197. song.playlistTracks[(std::size_t) track].lanes.push_back ({ index, 0 });
  1198. song.playlistTracks[(std::size_t) track].lanesFolded = false;
  1199. return index;
  1200. }
  1201. bool removeLane (Song& song, int track, int lane)
  1202. {
  1203. if (track < 0 || track >= (int) song.playlistTracks.size() || lane < 0 || lane >= (int) song.playlistTracks[(std::size_t) track].lanes.size())
  1204. return false;
  1205. song.removeAutomation (song.playlistTracks[(std::size_t) track].lanes[(std::size_t) lane].automation); // (drops the lane too)
  1206. return true;
  1207. }
  1208. int clipToLane (Song& song, int clipIndex, int track)
  1209. {
  1210. if (clipIndex < 0 || clipIndex >= (int) song.playlist.size())
  1211. return -1;
  1212. const auto clip = song.playlist[(std::size_t) clipIndex];
  1213. if (clip.kind != ClipKind::automation || clip.automation < 0 || clip.automation >= (int) song.automations.size() || clip.length <= 0)
  1214. return -1;
  1215. const auto source = song.automations[(std::size_t) clip.automation]; // (a copy: addLane may grow the list)
  1216. auto lane = laneFor (song, track, source.target);
  1217. if (lane < 0)
  1218. lane = addLane (song, track, source.target, source.name, source.valueAt ((double) clip.offset));
  1219. if (lane < 0)
  1220. return -1;
  1221. // the clip's curve in song time: its edges exact, the points inside it shifted by start − offset
  1222. std::vector<AutomationPoint> inside;
  1223. inside.push_back ({ clip.start, source.valueAt ((double) clip.offset), 0.0f });
  1224. for (const auto& p : source.points)
  1225. if (p.tick > clip.offset && p.tick < clip.offset + clip.length)
  1226. inside.push_back ({ clip.start + p.tick - clip.offset, p.value, p.tension, p.mode });
  1227. inside.push_back ({ clip.end(), source.valueAt ((double) (clip.offset + clip.length)), 0.0f });
  1228. auto& points = song.automations[(std::size_t) lane].points;
  1229. // what the lane had before and after the clip stays its own value up to the clip's edges
  1230. const auto before = song.automations[(std::size_t) lane].valueAt ((double) clip.start - 1.0);
  1231. const auto after = song.automations[(std::size_t) lane].valueAt ((double) clip.end() + 1.0);
  1232. points.erase (std::remove_if (points.begin(), points.end(), [&] (const AutomationPoint& p) { return p.tick >= clip.start && p.tick <= clip.end(); }),
  1233. points.end());
  1234. points.insert (points.end(), inside.begin(), inside.end());
  1235. if (clip.start > 0 && std::none_of (points.begin(), points.end(), [&] (const AutomationPoint& p) { return p.tick == clip.start - 1; }))
  1236. points.push_back ({ clip.start - 1, before, 0.0f });
  1237. if (std::none_of (points.begin(), points.end(), [&] (const AutomationPoint& p) { return p.tick == clip.end() + 1; }))
  1238. points.push_back ({ clip.end() + 1, after, 0.0f });
  1239. std::sort (points.begin(), points.end(), [] (const AutomationPoint& a, const AutomationPoint& b) { return a.tick < b.tick; });
  1240. // the clip goes (its automation stays: other clips may use it, as FL keeps it)
  1241. song.playlist.erase (song.playlist.begin() + clipIndex);
  1242. return lane;
  1243. }
  1244. int laneToClip (Song& song, int track, int lane)
  1245. {
  1246. if (track < 0 || track >= (int) song.playlistTracks.size() || lane < 0 || lane >= (int) song.playlistTracks[(std::size_t) track].lanes.size())
  1247. return -1;
  1248. const auto automation = song.playlistTracks[(std::size_t) track].lanes[(std::size_t) lane].automation;
  1249. auto& lanes = song.playlistTracks[(std::size_t) track].lanes;
  1250. lanes.erase (lanes.begin() + lane);
  1251. if (automation < 0 || automation >= (int) song.automations.size())
  1252. return -1;
  1253. const auto& points = song.automations[(std::size_t) automation].points;
  1254. const auto end = std::max (songEnd (song), points.empty() ? 0 : points.back().tick);
  1255. Clip clip;
  1256. clip.kind = ClipKind::automation;
  1257. clip.automation = automation;
  1258. clip.start = 0;
  1259. clip.length = std::max<std::int64_t> (1, end);
  1260. clip.track = 0;
  1261. // the first track whose [0, end) is free (instrument tracks take no automation clips)
  1262. auto busy = [&] (int t) {
  1263. if (t < (int) song.playlistTracks.size() && song.playlistTracks[(std::size_t) t].mode == PlaylistTrackMode::instrument)
  1264. return true;
  1265. for (const auto& c : song.playlist)
  1266. if (c.track == t && c.start < clip.length && c.end() > 0)
  1267. return true;
  1268. return false;
  1269. };
  1270. while (busy (clip.track))
  1271. ++clip.track;
  1272. song.playlist.push_back (clip);
  1273. return (int) song.playlist.size() - 1;
  1274. }
  1275. int lanesToClips (Song& song)
  1276. {
  1277. int n = 0;
  1278. for (int t = 0; t < (int) song.playlistTracks.size(); ++t)
  1279. while (! song.playlistTracks[(std::size_t) t].lanes.empty())
  1280. if (laneToClip (song, t, 0) >= 0)
  1281. ++n;
  1282. return n;
  1283. }
  1284. } // namespace lynx::autolanes
  1285. namespace lynx::autolink
  1286. {
  1287. namespace
  1288. {
  1289. constexpr int maxTracks = 500;
  1290. bool linkedToAnotherTrack (const Song& song, int mixer, int track)
  1291. {
  1292. for (int i = 0; i < (int) song.playlistTracks.size(); ++i)
  1293. if (i != track && linkedInsertOf (song, i) == mixer)
  1294. return true;
  1295. return false;
  1296. }
  1297. void addOnce (std::vector<int>& v, int x)
  1298. {
  1299. if (std::find (v.begin(), v.end(), x) == v.end())
  1300. v.push_back (x);
  1301. }
  1302. bool contains (const std::vector<int>& v, int x) { return std::find (v.begin(), v.end(), x) != v.end(); }
  1303. // The channels each track plays (audio clips' channels, the notes' channels of its pattern clips; automation clips don't
  1304. // count), tracks 0..499, every clip list in one pass (a pattern's channels are gathered once)
  1305. std::vector<std::vector<int>> channelsByTrack (const Song& song)
  1306. {
  1307. std::vector<std::vector<int>> out (maxTracks);
  1308. std::vector<std::vector<int>> patternChannels (song.patterns.size());
  1309. std::vector<bool> gathered (song.patterns.size(), false);
  1310. const auto channels = (int) song.channels.size();
  1311. song.forEachClipList ([&] (const std::vector<Clip>& clips) {
  1312. for (const auto& c : clips)
  1313. {
  1314. if (c.track < 0 || c.track >= maxTracks)
  1315. continue;
  1316. auto& t = out[(std::size_t) c.track];
  1317. if (c.kind == ClipKind::audio)
  1318. {
  1319. if (c.channel >= 0 && c.channel < channels)
  1320. addOnce (t, c.channel);
  1321. }
  1322. else if (c.kind == ClipKind::pattern && c.pattern >= 0 && c.pattern < (int) song.patterns.size())
  1323. {
  1324. const auto p = (std::size_t) c.pattern;
  1325. if (! gathered[p])
  1326. {
  1327. gathered[p] = true;
  1328. for (const auto& n : song.patterns[p].notes)
  1329. if (n.channel >= 0 && n.channel < channels)
  1330. addOnce (patternChannels[p], n.channel);
  1331. }
  1332. for (const auto ch : patternChannels[p])
  1333. addOnce (t, ch);
  1334. }
  1335. }
  1336. });
  1337. return out;
  1338. }
  1339. std::vector<int> homesFrom (const Song& song, const std::vector<std::vector<int>>& byTrack)
  1340. {
  1341. std::vector<int> homes (song.channels.size(), -1);
  1342. for (int t = 0; t < maxTracks; ++t)
  1343. for (const auto c : byTrack[(std::size_t) t])
  1344. if (homes[(std::size_t) c] < 0)
  1345. homes[(std::size_t) c] = t;
  1346. return homes;
  1347. }
  1348. bool validInsert (const Song& song, int m) { return m > 0 && m < (int) song.mixer.size(); }
  1349. int routeOf (const Song& song, int channel)
  1350. {
  1351. const auto m = song.channels[(std::size_t) channel].mixerTrack;
  1352. return validInsert (song, m) ? m : 0;
  1353. }
  1354. bool receivesSends (const Song& song, int mixer)
  1355. {
  1356. for (int from = 1; from < (int) song.mixer.size(); ++from)
  1357. if (from != mixer)
  1358. for (const auto& s : song.mixer[(std::size_t) from].sends)
  1359. if (s.target == mixer)
  1360. return true;
  1361. return false;
  1362. }
  1363. // Only track `track`'s own channels (or channels no track plays) go through `mixer`, nothing sends into it and no other
  1364. // track is linked to it: it belongs to that track's content alone.
  1365. bool exclusiveTo (const Song& song, int mixer, int track, const std::vector<int>& homes)
  1366. {
  1367. if (! validInsert (song, mixer) || receivesSends (song, mixer) || linkedToAnotherTrack (song, mixer, track)
  1368. || song.session.reservedForOther (true, mixer))
  1369. return false;
  1370. for (int c = 0; c < (int) song.channels.size(); ++c)
  1371. if (song.channels[(std::size_t) c].mixerTrack == mixer && homes[(std::size_t) c] >= 0 && homes[(std::size_t) c] != track)
  1372. return false;
  1373. return true;
  1374. }
  1375. // `track` can take insert `mixer` as it is: neutral, and no other track's channels, sends or links use it
  1376. bool freeFor (const Song& song, int mixer, int track, const std::vector<int>& homes)
  1377. {
  1378. return isNeutral (song, mixer) && exclusiveTo (song, mixer, track, homes);
  1379. }
  1380. // a free neutral insert to move what sits at `mixer` to (after it first, then before), a new one when there is none
  1381. int freeInsertBesides (Song& song, int mixer)
  1382. {
  1383. for (int m = mixer + 1; m < (int) song.mixer.size(); ++m)
  1384. if (isNeutralFree (song, m))
  1385. return m;
  1386. for (int m = 1; m < mixer; ++m)
  1387. if (isNeutralFree (song, m))
  1388. return m;
  1389. return trackmodes::addMixerTrack (song);
  1390. }
  1391. std::string insertName (const Song& song, int m)
  1392. {
  1393. return trackmodes::hasOwnName (song, m) ? song.mixer[(std::size_t) m].name : std::string(); // ("": its default name)
  1394. }
  1395. void note (Report* report, Change change)
  1396. {
  1397. if (report != nullptr)
  1398. report->changes.push_back (std::move (change));
  1399. }
  1400. // One track: its own channels (`only`: these alone; empty: all of them) go through insert track + 1, and the track is
  1401. // linked to it. `follow`: an edit's new content (nothing that changes the sound). True when the track got linked.
  1402. bool pairTrack (Song& song, int track, const std::vector<int>& homes, const std::vector<int>& content, const std::vector<int>& only,
  1403. bool follow, Report* report)
  1404. {
  1405. const auto n = track + 1;
  1406. if (n >= maxMixerTracks || song.session.reservedForOther (true, n))
  1407. return false;
  1408. std::vector<int> own;
  1409. for (int c = 0; c < (int) homes.size(); ++c)
  1410. if (homes[(std::size_t) c] == track && (only.empty() || contains (only, c)))
  1411. own.push_back (c);
  1412. if (own.empty())
  1413. return false;
  1414. // 1. inserts up to N (empty inserts change nothing)
  1415. const auto mixerBefore = song.mixer.size();
  1416. while ((int) song.mixer.size() <= n)
  1417. if (trackmodes::addMixerTrack (song) < 0)
  1418. {
  1419. song.mixer.resize (mixerBefore);
  1420. return false;
  1421. }
  1422. const auto routes = [&] {
  1423. std::vector<int> s;
  1424. for (const auto c : own)
  1425. addOnce (s, routeOf (song, c));
  1426. return s;
  1427. };
  1428. auto from = routes();
  1429. const auto linked = linkedInsertOf (song, track);
  1430. // 2. what lives at insert N and is not this track's goes to a free insert (a swap: everything that refers to it
  1431. // follows, the sound stays)
  1432. if (! contains (from, n) && linked != n && ! freeFor (song, n, track, homes))
  1433. {
  1434. const auto away = freeInsertBesides (song, n);
  1435. if (away <= 0 || away == n)
  1436. return false;
  1437. note (report, { Change::Kind::moveInsert, -1, -1, n, away, insertName (song, n), false });
  1438. mixerops::swapTracks (song, n, away);
  1439. from = routes();
  1440. }
  1441. // 3. the track's content all goes through one insert with settings or a name of its own (an effect chain, a bus the
  1442. // user named) or that the track is linked to: that insert becomes insert N
  1443. if (from.size() == 1 && from[0] != n && from[0] > 0 && exclusiveTo (song, from[0], track, homes)
  1444. && (! isNeutral (song, from[0]) || from[0] == linked || trackmodes::hasOwnName (song, from[0])))
  1445. {
  1446. note (report, { Change::Kind::moveInsert, track, -1, from[0], n, insertName (song, from[0]), false });
  1447. mixerops::swapTracks (song, from[0], n);
  1448. from = routes();
  1449. }
  1450. // 4. the rest goes into insert N: an insert with settings of its own that only this track uses sends into it (its
  1451. // sound stays); a channel on the Master, on a neutral insert or on an insert others share moves to it
  1452. const auto intoN = isNeutral (song, n);
  1453. std::vector<int> bussed;
  1454. for (const auto c : own)
  1455. {
  1456. const auto m = routeOf (song, c);
  1457. if (m == n || contains (bussed, m))
  1458. continue;
  1459. if (m > 0 && ! isNeutral (song, m) && exclusiveTo (song, m, track, homes) && song.canRoute (m, n))
  1460. {
  1461. note (report, { Change::Kind::routeInsert, track, -1, m, n, insertName (song, m), ! intoN });
  1462. if (follow && ! intoN)
  1463. continue;
  1464. auto& source = song.mixer[(std::size_t) m];
  1465. const auto level = source.masterLevel;
  1466. source.toMaster = false;
  1467. song.setRoute (m, n, true, level);
  1468. bussed.push_back (m);
  1469. continue;
  1470. }
  1471. const auto changesSound = ! intoN || (m > 0 && ! isNeutral (song, m));
  1472. if (follow && changesSound)
  1473. continue;
  1474. note (report, { Change::Kind::routeChannel, track, c, m, n, song.channels[(std::size_t) c].name, changesSound });
  1475. song.channels[(std::size_t) c].mixerTrack = n;
  1476. }
  1477. // (an edit's new content that could not go through insert N without changing its sound: the track stays as it is)
  1478. if (follow && linked != n
  1479. && std::none_of (own.begin(), own.end(), [&] (int c) { return routeOf (song, c) == n || contains (bussed, routeOf (song, c)); }))
  1480. return false;
  1481. // 5. the link: FL's instrument track when the track plays one generator, else FL's audio track on insert N
  1482. if ((int) song.playlistTracks.size() <= track)
  1483. song.playlistTracks.resize ((std::size_t) track + 1);
  1484. for (int i = 0; i < (int) song.playlistTracks.size(); ++i) // (one track per insert)
  1485. if (i != track && linkedInsertOf (song, i) == n)
  1486. trackmodes::unassign (song, i);
  1487. auto& t = song.playlistTracks[(std::size_t) track];
  1488. const auto wasLinked = t.mode != PlaylistTrackMode::unassigned && linked == n;
  1489. const auto single = content.size() == 1 && own.size() == 1 && content[0] == own[0] && routeOf (song, own[0]) == n
  1490. && trackmodes::canBeInstrumentTrack (song, own[0]);
  1491. if (single)
  1492. {
  1493. for (const auto other : trackmodes::instrumentTracksOf (song, own[0])) // (a channel belongs to one instrument track)
  1494. if (other != track)
  1495. trackmodes::unassign (song, other);
  1496. t.mode = PlaylistTrackMode::instrument;
  1497. t.channel = own[0];
  1498. }
  1499. else
  1500. {
  1501. t.mode = PlaylistTrackMode::audio;
  1502. t.channel = -1;
  1503. }
  1504. t.mixerTrack = n;
  1505. // 6. one name and colour: the track's own name, else the insert's, else the one instrument's or sample's
  1506. auto& m = song.mixer[(std::size_t) n];
  1507. if (t.name.empty() && trackmodes::hasOwnName (song, n))
  1508. t.name = m.name;
  1509. else if (t.name.empty() && own.size() == 1 && content.size() == 1)
  1510. t.name = song.channels[(std::size_t) own[0]].name;
  1511. if (! t.name.empty())
  1512. m.name = t.name;
  1513. if (t.colour == 0)
  1514. t.colour = m.colour;
  1515. else
  1516. m.colour = t.colour;
  1517. if (! wasLinked)
  1518. note (report, { Change::Kind::linkTrack, track, single ? own[0] : -1, -1, n, t.name, false });
  1519. return ! wasLinked;
  1520. }
  1521. } // namespace
  1522. int linkedInsertOf (const Song& song, int track)
  1523. {
  1524. if (track < 0 || track >= (int) song.playlistTracks.size())
  1525. return -1;
  1526. const auto& t = song.playlistTracks[(std::size_t) track];
  1527. if (t.mode == PlaylistTrackMode::audio)
  1528. return t.mixerTrack;
  1529. if (t.mode == PlaylistTrackMode::instrument)
  1530. return t.channel >= 0 && t.channel < (int) song.channels.size() ? song.channels[(std::size_t) t.channel].mixerTrack : t.mixerTrack;
  1531. return -1;
  1532. }
  1533. std::vector<int> otherInsertsOnTrack (const Song& song, int track, int insert)
  1534. {
  1535. std::vector<int> out;
  1536. const auto add = [&] (int channel) {
  1537. if (channel < 0 || channel >= (int) song.channels.size())
  1538. return;
  1539. const auto m = song.channels[(std::size_t) channel].mixerTrack;
  1540. if (m != insert && std::find (out.begin(), out.end(), m) == out.end())
  1541. out.push_back (m);
  1542. };
  1543. std::vector<char> seen (song.patterns.size(), 0);
  1544. for (const auto& clip : song.playlist)
  1545. {
  1546. if (clip.track != track || clip.length <= 0)
  1547. continue;
  1548. if (clip.kind == ClipKind::audio)
  1549. add (clip.channel);
  1550. else if (clip.kind == ClipKind::pattern && clip.pattern >= 0 && clip.pattern < (int) song.patterns.size() && ! seen[(std::size_t) clip.pattern])
  1551. {
  1552. seen[(std::size_t) clip.pattern] = 1;
  1553. for (const auto& n : song.patterns[(std::size_t) clip.pattern].notes)
  1554. add (n.channel);
  1555. }
  1556. }
  1557. std::sort (out.begin(), out.end());
  1558. return out;
  1559. }
  1560. bool isNeutral (const Song& song, int mixer)
  1561. {
  1562. if (! validInsert (song, mixer))
  1563. return false;
  1564. const auto& m = song.mixer[(std::size_t) mixer];
  1565. const MixerTrack plain;
  1566. return m.effects.empty() && m.volume == plain.volume && m.pan == plain.pan && ! m.muted && ! m.solo && m.toMaster
  1567. && m.masterLevel == plain.masterLevel && m.stereo == plain.stereo && ! m.invertPhase && ! m.swapChannels && m.sends.empty()
  1568. && m.eq == plain.eq && m.latencyMs == plain.latencyMs && m.outputChannel == plain.outputChannel && m.inputChannel < 0;
  1569. }
  1570. bool isNeutralFree (const Song& song, int mixer)
  1571. {
  1572. if (! isNeutral (song, mixer) || song.session.reservedForOther (true, mixer) || linkedToAnotherTrack (song, mixer, -1)
  1573. || receivesSends (song, mixer))
  1574. return false;
  1575. for (const auto& c : song.channels)
  1576. if (c.mixerTrack == mixer)
  1577. return false;
  1578. return true;
  1579. }
  1580. std::vector<int> homeTracks (const Song& song)
  1581. {
  1582. return homesFrom (song, channelsByTrack (song));
  1583. }
  1584. std::vector<int> repeatedTracks (const Song& song)
  1585. {
  1586. const auto byTrack = channelsByTrack (song);
  1587. const auto homes = homesFrom (song, byTrack);
  1588. std::vector<int> out (maxTracks, -1);
  1589. for (int t = 0; t < maxTracks; ++t)
  1590. {
  1591. const auto& played = byTrack[(std::size_t) t];
  1592. if (played.empty() || std::any_of (played.begin(), played.end(), [&] (int c) { return homes[(std::size_t) c] == t; }))
  1593. continue;
  1594. out[(std::size_t) t] = homes[(std::size_t) played.front()];
  1595. }
  1596. return out;
  1597. }
  1598. bool Report::changesRouting() const
  1599. {
  1600. return std::any_of (changes.begin(), changes.end(), [] (const Change& c) { return c.kind != Change::Kind::linkTrack; });
  1601. }
  1602. bool Report::changesSound() const
  1603. {
  1604. return std::any_of (changes.begin(), changes.end(), [] (const Change& c) { return c.changesSound; });
  1605. }
  1606. int setOn (Song& song, bool on, Report* report)
  1607. {
  1608. song.linkTracksToMixer = on;
  1609. if (! on)
  1610. return 0;
  1611. const auto byTrack = channelsByTrack (song);
  1612. const auto homes = homesFrom (song, byTrack);
  1613. int linked = 0;
  1614. for (int t = 0; t < maxTracks; ++t)
  1615. if (pairTrack (song, t, homes, byTrack[(std::size_t) t], {}, false, report))
  1616. ++linked;
  1617. if (report != nullptr)
  1618. {
  1619. const auto repeats = repeatedTracks (song);
  1620. for (int t = 0; t < maxTracks; ++t)
  1621. if (repeats[(std::size_t) t] >= 0)
  1622. report->repeats.push_back ({ t, repeats[(std::size_t) t] });
  1623. }
  1624. return linked;
  1625. }
  1626. Report planOn (const Song& song)
  1627. {
  1628. auto copy = song;
  1629. Report report;
  1630. setOn (copy, true, &report);
  1631. return report;
  1632. }
  1633. Snapshot snapshot (const Song& song)
  1634. {
  1635. Snapshot s;
  1636. s.on = song.linkTracksToMixer;
  1637. if (! s.on)
  1638. return s;
  1639. s.homes = homeTracks (song);
  1640. for (int t = 0; t < (int) song.playlistTracks.size(); ++t)
  1641. if (const auto m = linkedInsertOf (song, t); validInsert (song, m))
  1642. {
  1643. const auto& pt = song.playlistTracks[(std::size_t) t];
  1644. const auto& mt = song.mixer[(std::size_t) m];
  1645. s.pairs.push_back ({ t, m, pt.name, pt.colour, mt.name, mt.colour });
  1646. }
  1647. return s;
  1648. }
  1649. int followNewContent (Song& song, const Snapshot& before)
  1650. {
  1651. if (! song.linkTracksToMixer || ! before.on)
  1652. return 0;
  1653. // names and colours: a linked track and its insert keep one name and colour, whichever was renamed
  1654. for (const auto& p : before.pairs)
  1655. {
  1656. if (linkedInsertOf (song, p.track) != p.mixer || ! validInsert (song, p.mixer))
  1657. continue;
  1658. auto& t = song.playlistTracks[(std::size_t) p.track];
  1659. auto& m = song.mixer[(std::size_t) p.mixer];
  1660. const auto trackChanged = t.name != p.trackName || t.colour != p.trackColour;
  1661. const auto insertChanged = m.name != p.insertName || m.colour != p.insertColour;
  1662. if (trackChanged && ! insertChanged)
  1663. {
  1664. m.name = t.name.empty() ? "Insert " + std::to_string (p.mixer) : t.name;
  1665. m.colour = t.colour;
  1666. }
  1667. else if (insertChanged && ! trackChanged)
  1668. {
  1669. t.name = trackmodes::hasOwnName (song, p.mixer) ? m.name : std::string();
  1670. t.colour = m.colour;
  1671. }
  1672. }
  1673. // new content: an instrument or sample no track played before goes through its track's insert
  1674. const auto byTrack = channelsByTrack (song);
  1675. const auto homes = homesFrom (song, byTrack);
  1676. // (also one whose clips all moved from a linked track to another: it follows them, from the old track's insert)
  1677. std::vector<std::vector<int>> fresh (maxTracks);
  1678. for (int c = 0; c < (int) homes.size(); ++c)
  1679. {
  1680. const auto t = homes[(std::size_t) c];
  1681. const auto was = c < (int) before.homes.size() ? before.homes[(std::size_t) c] : -1;
  1682. if (t >= 0 && (was < 0 || (was != t && routeOf (song, c) == was + 1 && linkedInsertOf (song, was) == was + 1)))
  1683. fresh[(std::size_t) t].push_back (c);
  1684. }
  1685. int linked = 0;
  1686. for (int t = 0; t < maxTracks; ++t)
  1687. {
  1688. if (fresh[(std::size_t) t].empty())
  1689. continue;
  1690. const auto had = std::any_of (before.homes.begin(), before.homes.end(), [t] (int h) { return h == t; });
  1691. const auto link = linkedInsertOf (song, t);
  1692. if (link < 0 && had)
  1693. continue; // Track mode › Unassigned on a track that already had content: it stays so
  1694. if (link >= 0 && link != t + 1)
  1695. continue; // linked by hand to another insert: left as it is
  1696. if (pairTrack (song, t, homes, byTrack[(std::size_t) t], fresh[(std::size_t) t], true, nullptr))
  1697. ++linked;
  1698. }
  1699. return linked;
  1700. }
  1701. int matchLinkedColours (Song& song)
  1702. {
  1703. if (! song.linkTracksToMixer)
  1704. return 0;
  1705. int changed = 0;
  1706. for (int t = 0; t < (int) song.playlistTracks.size(); ++t)
  1707. if (const auto m = linkedInsertOf (song, t); validInsert (song, m))
  1708. {
  1709. auto& track = song.playlistTracks[(std::size_t) t];
  1710. auto& insert = song.mixer[(std::size_t) m];
  1711. if (track.colour == insert.colour || (track.colour != 0 && insert.colour != 0))
  1712. continue;
  1713. if (track.colour == 0)
  1714. track.colour = insert.colour;
  1715. else
  1716. insert.colour = track.colour;
  1717. ++changed;
  1718. }
  1719. return changed;
  1720. }
  1721. int pairedTakeTrack (const Song& song, int insert)
  1722. {
  1723. const auto track = insert - 1;
  1724. if (! song.linkTracksToMixer || insert <= 0 || insert >= (int) song.mixer.size() || track >= maxTracks)
  1725. return -1;
  1726. if (track < (int) song.playlistTracks.size())
  1727. {
  1728. const auto& t = song.playlistTracks[(std::size_t) track];
  1729. if (t.mode != PlaylistTrackMode::unassigned || t.grouped)
  1730. return -1;
  1731. }
  1732. bool used = false;
  1733. song.forEachClipList ([&] (const std::vector<Clip>& clips) {
  1734. used = used || std::any_of (clips.begin(), clips.end(), [track] (const Clip& c) { return c.track == track; });
  1735. });
  1736. return used ? -1 : track;
  1737. }
  1738. } // namespace lynx::autolink