| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866 |
- #include "core/FlControlMenus.h"
- #include "core/MixerOps.h"
- #include "core/PlaylistOps.h"
- #include "core/TrackClips.h"
- #include "core/QuickRender.h"
- #include <algorithm>
- #include <cmath>
- #include <cstdio>
- #include <limits>
- namespace lynx::clipprops
- {
- namespace
- {
- bool validSource (const Song& song, ClipKind kind, int source)
- {
- switch (kind)
- {
- case ClipKind::pattern: return source >= 0 && source < (int) song.patterns.size();
- case ClipKind::audio:
- return source >= 0 && source < (int) song.channels.size() && song.channels[(std::size_t) source].instrument == InstrumentKind::audioClip;
- case ClipKind::automation: return source >= 0 && source < (int) song.automations.size();
- }
- return false;
- }
- } // namespace
- bool read (const Song& song, int clip, Props& out)
- {
- if (clip < 0 || clip >= (int) song.playlist.size())
- return false;
- const auto& c = song.playlist[(std::size_t) clip];
- Props p;
- p.kind = c.kind;
- p.track = c.track;
- p.start = c.start;
- p.length = c.length;
- p.offset = c.offset;
- p.gain = c.gain;
- p.pan = c.pan;
- p.fadeIn = c.fadeIn;
- p.fadeOut = c.fadeOut;
- p.muted = c.muted;
- if (c.kind == ClipKind::pattern && validSource (song, c.kind, c.pattern))
- {
- const auto& pat = song.patterns[(std::size_t) c.pattern];
- p.source = c.pattern;
- p.name = pat.name;
- p.colour = pat.colour;
- }
- else if (c.kind == ClipKind::audio && c.channel >= 0 && c.channel < (int) song.channels.size())
- {
- const auto& ch = song.channels[(std::size_t) c.channel];
- p.source = c.channel;
- p.name = ch.name;
- p.colour = ch.colour;
- p.pitch = ch.sampler.pitch;
- p.stretchBeats = ch.sampler.stretchBeats;
- p.keepPitch = ch.sampler.keepPitch;
- }
- else if (c.kind == ClipKind::automation && validSource (song, c.kind, c.automation))
- {
- p.source = c.automation;
- p.name = song.automations[(std::size_t) c.automation].name;
- }
- out = p;
- return true;
- }
- bool apply (Song& song, int clip, const Props& p)
- {
- if (clip < 0 || clip >= (int) song.playlist.size())
- return false;
- auto& c = song.playlist[(std::size_t) clip];
- if (p.kind == c.kind && validSource (song, c.kind, p.source))
- {
- if (c.kind == ClipKind::pattern)
- c.pattern = p.source;
- else if (c.kind == ClipKind::audio)
- c.channel = p.source;
- else
- c.automation = p.source;
- }
- c.track = std::clamp (p.track, 0, 499);
- c.start = std::max<std::int64_t> (0, p.start);
- c.length = std::max<std::int64_t> (1, p.length);
- if (c.offset != p.offset)
- c.offsetFraction = 0.0f; // an offset typed in ticks drops the imported sub-tick rest
- c.offset = std::max<std::int64_t> (0, p.offset);
- c.muted = p.muted;
- if (c.kind == ClipKind::audio)
- {
- c.gain = std::clamp (p.gain, 0.0f, 4.0f);
- c.pan = std::clamp (p.pan, -1.0f, 1.0f);
- c.fadeIn = std::clamp<std::int64_t> (p.fadeIn, 0, c.length);
- c.fadeOut = std::clamp<std::int64_t> (p.fadeOut, 0, c.length - c.fadeIn);
- }
- // the source's name and colour (and the audio channel's pitch / stretch)
- if (c.kind == ClipKind::pattern && validSource (song, c.kind, c.pattern))
- {
- auto& pat = song.patterns[(std::size_t) c.pattern];
- if (! p.name.empty())
- pat.name = p.name;
- pat.colour = p.colour;
- }
- else if (c.kind == ClipKind::audio && c.channel >= 0 && c.channel < (int) song.channels.size())
- {
- auto& ch = song.channels[(std::size_t) c.channel];
- if (! p.name.empty())
- ch.name = p.name;
- ch.colour = p.colour;
- ch.sampler.pitch = std::clamp (p.pitch, -24.0f, 24.0f);
- ch.sampler.stretchBeats = std::max (0.0f, p.stretchBeats);
- ch.sampler.keepPitch = p.keepPitch; // (round 163: also "keep length" for a pitched clip without stretch)
- }
- else if (c.kind == ClipKind::automation && validSource (song, c.kind, c.automation) && ! p.name.empty())
- song.automations[(std::size_t) c.automation].name = p.name;
- return true;
- }
- void changeMute (Song& song, const std::vector<int>& clips, MuteChange change)
- {
- for (auto i : clips)
- if (i >= 0 && i < (int) song.playlist.size())
- {
- auto& m = song.playlist[(std::size_t) i].muted;
- switch (change)
- {
- case MuteChange::muteAll: m = true; break;
- case MuteChange::unmuteAll: m = false; break;
- case MuteChange::flip: m = ! m; break;
- case MuteChange::keep: break;
- }
- }
- }
- void changeGain (Song& song, const std::vector<int>& clips, float value, bool relative)
- {
- for (auto i : clips)
- if (i >= 0 && i < (int) song.playlist.size() && song.playlist[(std::size_t) i].kind == ClipKind::audio)
- {
- auto& g = song.playlist[(std::size_t) i].gain;
- g = std::clamp (relative ? g * value : value, 0.0f, 4.0f);
- }
- }
- namespace
- {
- std::string bbt (std::int64_t ticks, Meter meter, int base)
- {
- const auto beat = meter.beat(); // (round 190fg: the meter's beats, eighths in 6/8)
- const auto bar = meter.bar();
- const auto t = std::max<std::int64_t> (0, ticks);
- char text[48];
- std::snprintf (text, sizeof (text), "%lld:%lld:%03lld", (long long) (t / bar + base), (long long) ((t % bar) / beat + base), (long long) (t % beat));
- return text;
- }
- std::int64_t parseBbt (const std::string& text, Meter meter, int base)
- {
- long long parts[3] = { 0, base, 0 };
- int count = 0;
- std::string token;
- auto flush = [&] {
- if (token.empty() || count >= 3)
- return false;
- for (auto c : token)
- if (c < '0' || c > '9')
- return false;
- parts[count++] = std::stoll (token);
- token.clear();
- return true;
- };
- for (auto c : text)
- {
- if (c == ' ')
- continue;
- if (c == ':' || c == '.')
- {
- if (! flush())
- return -1;
- }
- else
- token += c;
- }
- if (! flush())
- return -1;
- const auto beat = meter.beat();
- if (parts[0] < base || parts[1] < base || parts[1] - base >= std::max (1, meter.beats) || parts[2] >= beat)
- return -1;
- return (parts[0] - base) * meter.bar() + (parts[1] - base) * beat + parts[2];
- }
- } // namespace
- std::string formatPosition (std::int64_t ticks, Meter meter) { return bbt (ticks, meter, 1); }
- std::string formatLength (std::int64_t ticks, Meter meter) { return bbt (ticks, meter, 0); }
- std::int64_t parsePosition (const std::string& text, Meter meter) { return parseBbt (text, meter, 1); }
- std::int64_t parseLength (const std::string& text, Meter meter) { return parseBbt (text, meter, 0); }
- std::vector<std::pair<int, std::string>> sourcesFor (const Song& song, ClipKind kind)
- {
- std::vector<std::pair<int, std::string>> out;
- if (kind == ClipKind::pattern)
- for (int i = 0; i < (int) song.patterns.size(); ++i)
- {
- if (song.patterns[(std::size_t) i].name == "(tail)")
- continue;
- out.emplace_back (i, song.patterns[(std::size_t) i].name);
- }
- else if (kind == ClipKind::audio)
- {
- for (int i = 0; i < (int) song.channels.size(); ++i)
- if (song.channels[(std::size_t) i].instrument == InstrumentKind::audioClip)
- out.emplace_back (i, song.channels[(std::size_t) i].name);
- }
- else
- for (int i = 0; i < (int) song.automations.size(); ++i)
- out.emplace_back (i, song.automations[(std::size_t) i].name);
- return out;
- }
- } // namespace lynx::clipprops
- namespace lynx::arrangeops
- {
- const std::vector<Clip>& clipsOf (const Song& song, int index)
- {
- static const std::vector<Clip> none;
- if (index == song.currentArrangement)
- return song.playlist;
- if (index < 0 || index >= (int) song.arrangements.size())
- return none;
- return song.arrangements[(std::size_t) index].clips;
- }
- std::int64_t lengthOf (const Song& song, int index)
- {
- std::int64_t end = 0;
- for (const auto& c : clipsOf (song, index))
- end = std::max (end, c.end());
- return end;
- }
- std::int64_t positionTick (const Song& song, MergePosition position, std::int64_t currentTick)
- {
- switch (position)
- {
- case MergePosition::start: return 0;
- case MergePosition::current: return std::max<std::int64_t> (0, currentTick);
- case MergePosition::end:
- {
- const auto end = lengthOf (song, song.currentArrangement);
- const auto bar = song.barTicks();
- return (end + bar - 1) / bar * bar; // the next bar line
- }
- }
- return 0;
- }
- void clearRange (std::vector<Clip>& clips, std::int64_t from, std::int64_t to)
- {
- if (to <= from)
- return;
- std::vector<Clip> out;
- for (auto c : clips)
- {
- if (c.end() <= from || c.start >= to)
- {
- out.push_back (c);
- continue;
- }
- if (c.start < from) // keep the head
- {
- auto head = c;
- splitClip (head, from);
- out.push_back (head);
- }
- if (c.end() > to) // keep the end
- {
- auto copy = c;
- out.push_back (splitClip (copy, to));
- }
- }
- clips = std::move (out);
- }
- bool merge (Song& song, int source, MergeMode mode, std::int64_t at)
- {
- if (source == song.currentArrangement || source < 0 || source >= (int) song.arrangements.size())
- return false;
- const auto incoming = song.arrangements[(std::size_t) source].clips; // copy: the playlist may grow
- const auto length = lengthOf (song, source);
- at = std::max<std::int64_t> (0, at);
- if (mode == MergeMode::insert && length > 0)
- {
- // what crosses the position is split; everything after it moves later
- std::vector<Clip> moved;
- for (auto c : song.playlist)
- {
- if (c.start < at && c.end() > at)
- {
- auto rest = splitClip (c, at);
- rest.start += length;
- moved.push_back (c);
- moved.push_back (rest);
- }
- else
- {
- if (c.start >= at)
- c.start += length;
- moved.push_back (c);
- }
- }
- song.playlist = std::move (moved);
- for (auto& m : song.markers())
- if (m.tick >= at)
- m.tick += length;
- }
- else if (mode == MergeMode::replace)
- clearRange (song.playlist, at, at + length);
- for (auto c : incoming)
- {
- c.start += at;
- song.playlist.push_back (c);
- }
- std::stable_sort (song.playlist.begin(), song.playlist.end(), [] (const Clip& a, const Clip& b) {
- return a.start != b.start ? a.start < b.start : a.track < b.track;
- });
- return true;
- }
- void move (Song& song, int from, int to)
- {
- const auto n = (int) song.arrangements.size();
- if (from < 0 || from >= n || to < 0 || to >= n || from == to)
- return;
- auto item = std::move (song.arrangements[(std::size_t) from]);
- song.arrangements.erase (song.arrangements.begin() + from);
- song.arrangements.insert (song.arrangements.begin() + to, std::move (item));
- const auto follow = [from, to] (int& cur) {
- if (cur == from)
- cur = to;
- else if (from < cur && to >= cur)
- --cur;
- else if (from > cur && to <= cur)
- ++cur;
- };
- follow (song.currentArrangement);
- for (auto& t : song.playlistTracks) // round 190pe: track versions belong to their arrangement
- if (! t.versions.empty())
- follow (t.versionArrangement);
- }
- void rename (Song& song, int index, const std::string& name)
- {
- if (index >= 0 && index < (int) song.arrangements.size() && ! name.empty())
- song.arrangements[(std::size_t) index].name = name;
- }
- int duplicate (Song& song, int index)
- {
- if (index < 0 || index >= (int) song.arrangements.size())
- return -1;
- Arrangement copy = song.arrangements[(std::size_t) index];
- if (index == song.currentArrangement)
- {
- copy.clips = song.playlist;
- copy.loopStart = song.loopStart;
- copy.loopEnd = song.loopEnd;
- }
- copy.name += " (copy)";
- song.arrangements.insert (song.arrangements.begin() + index + 1, std::move (copy));
- if (song.currentArrangement > index)
- ++song.currentArrangement;
- for (auto& t : song.playlistTracks) // round 190pe: track versions belong to their arrangement (not to the copy)
- if (! t.versions.empty() && t.versionArrangement > index)
- ++t.versionArrangement;
- return index + 1;
- }
- } // namespace lynx::arrangeops
- namespace lynx::trackmodes
- {
- namespace
- {
- PlaylistTrack* trackAt (Song& song, int track)
- {
- if (track < 0 || track >= 500)
- return nullptr;
- if ((int) song.playlistTracks.size() <= track)
- song.playlistTracks.resize ((std::size_t) track + 1);
- return &song.playlistTracks[(std::size_t) track];
- }
- bool linkedElsewhere (const Song& song, int mixer, int exceptTrack)
- {
- for (int i = 0; i < (int) song.playlistTracks.size(); ++i)
- if (i != exceptTrack && song.playlistTracks[(std::size_t) i].mode != PlaylistTrackMode::unassigned
- && song.playlistTracks[(std::size_t) i].mixerTrack == mixer)
- return true;
- return false;
- }
- std::string trackName (const Song& song, int track)
- {
- if (track < (int) song.playlistTracks.size() && ! song.playlistTracks[(std::size_t) track].name.empty())
- return song.playlistTracks[(std::size_t) track].name;
- return "Track " + std::to_string (track + 1);
- }
- } // namespace
- int freeMixerTrack (Song& song)
- {
- const auto isFree = [&song] (int t) {
- const auto& m = song.mixer[(std::size_t) t];
- if (! m.effects.empty() || linkedElsewhere (song, t, -1) || song.session.reservedForOther (true, t))
- return false;
- for (const auto& c : song.channels)
- if (c.mixerTrack == t)
- return false;
- for (int from = 1; from < (int) song.mixer.size(); ++from)
- if (from != t)
- for (const auto& s : song.mixer[(std::size_t) from].sends)
- if (s.target == t)
- return false;
- return true;
- };
- for (int t = 1; t < (int) song.mixer.size(); ++t)
- if (isFree (t))
- return t;
- return addMixerTrack (song);
- }
- bool makeAudioTrack (Song& song, int track)
- {
- if (track < 0 || track >= 500)
- return false;
- auto mixer = track < (int) song.playlistTracks.size() && song.playlistTracks[(std::size_t) track].mode == PlaylistTrackMode::audio
- ? song.playlistTracks[(std::size_t) track].mixerTrack : -1;
- if (mixer <= 0)
- mixer = freeMixerTrack (song);
- if (mixer <= 0)
- return false;
- auto* t = trackAt (song, track);
- t->mode = PlaylistTrackMode::audio;
- t->mixerTrack = mixer;
- t->channel = -1;
- auto& m = song.mixer[(std::size_t) mixer];
- m.name = trackName (song, track);
- if (t->colour != 0)
- m.colour = t->colour;
- // the audio clips already on the track play through it from now on
- std::vector<int> onTrack;
- for (int i = 0; i < (int) song.playlist.size(); ++i)
- if (song.playlist[(std::size_t) i].track == track)
- onTrack.push_back (i);
- followAudioTracks (song, onTrack);
- return true;
- }
- bool makeInstrumentTrack (Song& song, int track, int channel)
- {
- if (track < 0 || track >= 500 || channel < 0 || channel >= (int) song.channels.size())
- return false;
- const auto kind = song.channels[(std::size_t) channel].instrument;
- if (kind == InstrumentKind::audioClip || kind == InstrumentKind::controller)
- return false; // FL: "Only generators can be used as instrument tracks."
- // a channel belongs to one instrument track
- for (int i = 0; i < (int) song.playlistTracks.size(); ++i)
- if (i != track && song.playlistTracks[(std::size_t) i].mode == PlaylistTrackMode::instrument
- && song.playlistTracks[(std::size_t) i].channel == channel)
- unassign (song, i);
- auto mixer = song.channels[(std::size_t) channel].mixerTrack;
- if (mixer <= 0 || mixer >= (int) song.mixer.size() || linkedElsewhere (song, mixer, track))
- mixer = freeMixerTrack (song);
- if (mixer <= 0)
- return false;
- auto* t = trackAt (song, track);
- if (t->mode != PlaylistTrackMode::instrument) // round 190ne: a new instrument track takes the layout's "Clips own their notes"
- t->ownClips = trackclips::defaultOn();
- t->mode = PlaylistTrackMode::instrument;
- t->channel = channel;
- t->mixerTrack = mixer;
- song.channels[(std::size_t) channel].mixerTrack = mixer;
- if (t->name.empty())
- t->name = song.channels[(std::size_t) channel].name;
- return true;
- }
- bool canLinkToMixerTrack (const Song& song, int track, int mixer)
- {
- return track >= 0 && track < 500 && mixer > 0 && mixer < (int) song.mixer.size() && ! linkedElsewhere (song, mixer, track)
- && ! song.session.reservedForOther (true, mixer);
- }
- bool hasOwnName (const Song& song, int mixer)
- {
- if (mixer <= 0 || mixer >= (int) song.mixer.size())
- return false;
- const auto& name = song.mixer[(std::size_t) mixer].name;
- return ! name.empty() && name != "Insert " + std::to_string (mixer);
- }
- bool canAddMixerTrack (const Song& song)
- {
- if (song.mixer.empty())
- return false;
- for (int t = (int) song.mixer.size(); t < maxMixerTracks; ++t)
- if (! song.session.reservedForOther (true, t))
- return true;
- return false;
- }
- int addMixerTrack (Song& song)
- {
- if (! canAddMixerTrack (song))
- return -1;
- // inserts reserved for another participant are added as plain inserts and skipped (Song::claimFreeInsert's rule)
- while (true)
- {
- auto& added = song.mixer.emplace_back();
- added.name = "Insert " + std::to_string (song.mixer.size() - 1);
- mixerops::giveNewTrackSlots (added); // (FL 2026.2's "Number of effect slots on new mixer tracks", round 190qf)
- if (const auto t = (int) song.mixer.size() - 1; ! song.session.reservedForOther (true, t))
- return t;
- }
- }
- bool linkToMixerTrack (Song& song, int track, int mixer)
- {
- if (! canLinkToMixerTrack (song, track, mixer))
- return false;
- auto* t = trackAt (song, track);
- if (t->mode == PlaylistTrackMode::audio && t->mixerTrack == mixer)
- return true;
- auto& m = song.mixer[(std::size_t) mixer];
- const auto insertName = hasOwnName (song, mixer) ? m.name : "Insert " + std::to_string (mixer);
- if (t->mode == PlaylistTrackMode::unassigned)
- {
- // FL (FUN_012f38f0): the insert's colour when the track has the default one; the insert's own name, else the
- // track's, else "Insert N"
- if (t->colour == 0)
- t->colour = m.colour;
- if (hasOwnName (song, mixer) || t->name.empty())
- t->name = insertName;
- }
- else if (t->mode == PlaylistTrackMode::audio && t->mixerTrack > 0 && t->mixerTrack < (int) song.mixer.size())
- {
- // FL: the previous insert is reset (name, colour; its effects and routing stay)
- auto& old = song.mixer[(std::size_t) t->mixerTrack];
- old.name = "Insert " + std::to_string (t->mixerTrack);
- old.colour = 0;
- }
- t->mode = PlaylistTrackMode::audio;
- t->mixerTrack = mixer;
- t->channel = -1;
- // FL: the track and the insert end with one name and colour
- if (t->name.empty())
- {
- t->name = insertName;
- t->colour = m.colour;
- }
- else
- {
- m.name = t->name;
- m.colour = t->colour;
- }
- std::vector<int> onTrack;
- for (int i = 0; i < (int) song.playlist.size(); ++i)
- if (song.playlist[(std::size_t) i].track == track)
- onTrack.push_back (i);
- followAudioTracks (song, onTrack);
- return true;
- }
- LinkedNames linkedNames (const Song& song)
- {
- LinkedNames n;
- for (int t = 0; t < (int) song.playlistTracks.size(); ++t)
- {
- const auto& pt = song.playlistTracks[(std::size_t) t];
- if (pt.mode != PlaylistTrackMode::audio || pt.mixerTrack <= 0 || pt.mixerTrack >= (int) song.mixer.size())
- continue;
- const auto& mt = song.mixer[(std::size_t) pt.mixerTrack];
- n.pairs.push_back ({ t, pt.mixerTrack, pt.name, pt.colour, mt.name, mt.colour });
- }
- return n;
- }
- int followLinkedNames (Song& song, const LinkedNames& before)
- {
- int changed = 0;
- for (const auto& p : before.pairs)
- {
- if (p.track >= (int) song.playlistTracks.size() || p.mixer >= (int) song.mixer.size())
- continue;
- auto& t = song.playlistTracks[(std::size_t) p.track];
- if (t.mode != PlaylistTrackMode::audio || t.mixerTrack != p.mixer)
- continue; // (unlinked or relinked by the edit: the link itself set both)
- auto& m = song.mixer[(std::size_t) p.mixer];
- const auto trackChanged = t.name != p.trackName || t.colour != p.trackColour;
- const auto insertChanged = m.name != p.insertName || m.colour != p.insertColour;
- if (trackChanged && ! insertChanged)
- {
- // FUN_012e6010: the mixer track takes the track's colour and name (an unnamed track: FL's empty mixer name)
- m.colour = t.colour;
- m.name = t.name.empty() ? "Insert " + std::to_string (p.mixer) : t.name;
- ++changed;
- }
- else if (insertChanged && ! trackChanged)
- {
- // FUN_012be6f0: the linked Playlist track takes the mixer track's name and colour
- t.colour = m.colour;
- t.name = hasOwnName (song, p.mixer) ? m.name : std::string();
- ++changed;
- }
- }
- return changed;
- }
- int assignAudioTracks (Song& song, const std::vector<int>& tracks, int mixer)
- {
- if (mixer != newMixerTrack && (mixer <= 0 || mixer >= (int) song.mixer.size()))
- return 0;
- int linked = 0;
- auto next = mixer;
- for (std::size_t i = 0; i < tracks.size(); ++i)
- {
- if (next == newMixerTrack || next >= (int) song.mixer.size())
- {
- next = addMixerTrack (song);
- if (next < 0)
- break;
- }
- // a refused track (LYNX: the pick is linked to another selected track, e.g. the menu's own) leaves the insert
- // to the next one
- if (! linkToMixerTrack (song, tracks[i], next))
- continue;
- ++linked;
- // FL: the next insert no Playlist track is linked to (LYNX: nor reserved for another participant)
- do
- ++next;
- while (next < (int) song.mixer.size() && (linkedElsewhere (song, next, -1) || song.session.reservedForOther (true, next)));
- if (next >= maxMixerTracks)
- break;
- }
- return linked;
- }
- AudioTrackMenu audioTrackMenu (const Song& song, int track)
- {
- AudioTrackMenu menu;
- menu.canAddMixerTrack = canAddMixerTrack (song);
- const auto* t = track >= 0 && track < (int) song.playlistTracks.size() ? &song.playlistTracks[(std::size_t) track] : nullptr;
- for (int m = 1; m < (int) song.mixer.size(); ++m)
- {
- auto& item = menu.inserts.emplace_back();
- item.mixer = m;
- item.name = song.mixer[(std::size_t) m].name;
- // FL ticks the insert the track is stored as linked to, whatever its mode
- item.ticked = t != nullptr && t->mode != PlaylistTrackMode::unassigned && t->mixerTrack == m;
- item.enabled = item.ticked || canLinkToMixerTrack (song, track, m);
- }
- return menu;
- }
- int instrumentTrackPattern (Song& song, int track)
- {
- if (track < 0 || track >= (int) song.playlistTracks.size())
- return -1;
- const auto& t = song.playlistTracks[(std::size_t) track];
- if (t.mode != PlaylistTrackMode::instrument || t.channel < 0 || t.channel >= (int) song.channels.size())
- return -1;
- const auto channel = t.channel;
- const auto& name = song.channels[(std::size_t) channel].name;
- for (int p = 0; p < (int) song.patterns.size(); ++p)
- {
- const auto& pattern = song.patterns[(std::size_t) p];
- if (pattern.name == name && std::all_of (pattern.notes.begin(), pattern.notes.end(), [channel] (const Note& n) { return n.channel == channel; }))
- return p;
- }
- Pattern made;
- made.name = name;
- made.colour = song.channels[(std::size_t) channel].colour;
- song.patterns.push_back (std::move (made));
- return (int) song.patterns.size() - 1;
- }
- void unassign (Song& song, int track)
- {
- if (track < 0 || track >= (int) song.playlistTracks.size())
- return;
- auto& t = song.playlistTracks[(std::size_t) track];
- t.mode = PlaylistTrackMode::unassigned;
- t.mixerTrack = -1;
- t.channel = -1;
- }
- int firstFreeTrack (const Song& song)
- {
- std::vector<bool> used (500, false);
- for (const auto& c : song.playlist)
- if (c.track >= 0 && c.track < 500)
- used[(std::size_t) c.track] = true;
- for (int t = 0; t < 500; ++t)
- if (! used[(std::size_t) t] && (t >= (int) song.playlistTracks.size() || song.playlistTracks[(std::size_t) t].mode == PlaylistTrackMode::unassigned))
- return t;
- return -1;
- }
- int addAudioTrack (Song& song)
- {
- const auto track = firstFreeTrack (song);
- if (track < 0)
- return -1;
- const auto mixerBefore = song.mixer.size();
- const auto mixer = freeMixerTrack (song);
- if (mixer <= 0 || assignAudioTracks (song, { track }, mixer) != 1)
- {
- if (song.mixer.size() > mixerBefore) // (an insert freeMixerTrack added for nothing goes again)
- song.mixer.resize (mixerBefore);
- return -1;
- }
- return track;
- }
- bool canBeInstrumentTrack (const Song& song, int channel)
- {
- if (channel < 0 || channel >= (int) song.channels.size())
- return false;
- const auto kind = song.channels[(std::size_t) channel].instrument;
- return kind != InstrumentKind::audioClip && kind != InstrumentKind::controller;
- }
- std::vector<int> instrumentTracksOf (const Song& song, int channel)
- {
- std::vector<int> tracks;
- for (int i = 0; i < (int) song.playlistTracks.size(); ++i)
- if (song.playlistTracks[(std::size_t) i].mode == PlaylistTrackMode::instrument && song.playlistTracks[(std::size_t) i].channel == channel)
- tracks.push_back (i);
- return tracks;
- }
- int assignToNewInstrumentTrack (Song& song, int channel)
- {
- if (! canBeInstrumentTrack (song, channel))
- return -1;
- const auto track = firstFreeTrack (song);
- if (track < 0)
- return -1;
- const auto before = song;
- if (! makeInstrumentTrack (song, track, channel))
- {
- song = before;
- return -1;
- }
- return track;
- }
- int unassignFromInstrumentTracks (Song& song, int channel)
- {
- const auto tracks = instrumentTracksOf (song, channel);
- for (const auto t : tracks)
- unassign (song, t);
- return (int) tracks.size();
- }
- int addInstrumentTrack (Song& song, ChannelDef generator, int track)
- {
- if (generator.instrument == InstrumentKind::audioClip || generator.instrument == InstrumentKind::controller)
- return -1;
- if (track < 0)
- track = firstFreeTrack (song);
- if (track < 0 || track >= 500 || (track < (int) song.playlistTracks.size() && song.playlistTracks[(std::size_t) track].mode == PlaylistTrackMode::instrument))
- return -1;
- const auto before = song;
- generator.mixerTrack = 0; // (makeInstrumentTrack gives it a free mixer track)
- song.channels.push_back (std::move (generator));
- const auto channel = (int) song.channels.size() - 1;
- if (! makeInstrumentTrack (song, track, channel))
- {
- song = before;
- return -1;
- }
- return channel;
- }
- std::string describe (const Song& song, int track)
- {
- if (track < 0 || track >= (int) song.playlistTracks.size())
- return {};
- const auto& t = song.playlistTracks[(std::size_t) track];
- if (t.mode == PlaylistTrackMode::instrument)
- return "(Instrument track)";
- if (t.mode == PlaylistTrackMode::audio)
- return "(Audio track, linked to mixer track " + std::to_string (t.mixerTrack) + ")";
- return {};
- }
- RouteSummary routeSummary (const Song& song, int track)
- {
- RouteSummary r;
- const auto channelCount = song.channels.size();
- std::vector<char> plays (channelCount, 0), patternSeen (song.patterns.size(), 0);
- for (const auto& c : song.playlist)
- {
- if (c.track != track)
- continue;
- if (c.kind == ClipKind::audio)
- {
- r.clips = true;
- if (c.channel >= 0 && (std::size_t) c.channel < channelCount)
- plays[(std::size_t) c.channel] = 1;
- }
- else if (c.kind == ClipKind::pattern)
- {
- r.clips = true;
- if (c.pattern < 0 || (std::size_t) c.pattern >= song.patterns.size() || patternSeen[(std::size_t) c.pattern])
- continue;
- patternSeen[(std::size_t) c.pattern] = 1;
- for (const auto& n : song.patterns[(std::size_t) c.pattern].notes)
- if (n.channel >= 0 && (std::size_t) n.channel < channelCount)
- plays[(std::size_t) n.channel] = 1;
- }
- }
- for (std::size_t ch = 0; ch < channelCount; ++ch)
- if (plays[ch])
- {
- r.channels.push_back ((int) ch);
- r.inserts.push_back (std::max (0, song.channels[ch].mixerTrack));
- }
- std::sort (r.inserts.begin(), r.inserts.end());
- r.inserts.erase (std::unique (r.inserts.begin(), r.inserts.end()), r.inserts.end());
- return r;
- }
- int armableInsert (const Song& song, int track)
- {
- if (track < 0 || track >= (int) song.playlistTracks.size())
- return -1;
- const auto& t = song.playlistTracks[(std::size_t) track];
- return t.mode == PlaylistTrackMode::audio && t.mixerTrack > 0 && t.mixerTrack < (int) song.mixer.size() ? t.mixerTrack : -1;
- }
- int meteredInsert (const Song& song, int track, const HeaderOptions& options)
- {
- if (track < 0 || track >= (int) song.playlistTracks.size())
- return -1;
- const auto& t = song.playlistTracks[(std::size_t) track];
- if (t.mode == PlaylistTrackMode::audio)
- return options.audioLevels ? armableInsert (song, track) : -1;
- if (t.mode != PlaylistTrackMode::instrument || ! options.instrumentLevels)
- return -1;
- // the channel's own route (it may have moved since the track was made); the linked mixer track without a channel
- if (t.channel >= 0 && t.channel < (int) song.channels.size())
- {
- const auto m = song.channels[(std::size_t) t.channel].mixerTrack;
- return m >= 0 && m < (int) song.mixer.size() ? m : -1;
- }
- return t.mixerTrack >= 0 && t.mixerTrack < (int) song.mixer.size() ? t.mixerTrack : -1;
- }
- namespace
- {
- int audioMixerOf (const Song& song, int track)
- {
- if (track < 0 || track >= (int) song.playlistTracks.size())
- return -1;
- const auto& t = song.playlistTracks[(std::size_t) track];
- return t.mode == PlaylistTrackMode::audio && t.mixerTrack > 0 && t.mixerTrack < (int) song.mixer.size() ? t.mixerTrack : -1;
- }
- // the mixer track a touched clip asks for its channel, or -1 when it needs nothing (not an audio clip on an audio
- // track, or its channel already plays there)
- int wantedMixer (const Song& song, const Clip& clip)
- {
- const auto mixer = audioMixerOf (song, clip.track);
- if (clip.kind != ClipKind::audio || mixer <= 0 || clip.channel < 0 || clip.channel >= (int) song.channels.size()
- || song.channels[(std::size_t) clip.channel].mixerTrack == mixer)
- return -1;
- return mixer;
- }
- // FL's test: another clip (any arrangement) of the same channel on an audio track linked to another mixer track
- bool sharedWithOtherAudioTrack (const Song& song, const Clip& clip, int mixer)
- {
- bool shared = false;
- song.forEachClipList ([&] (const std::vector<Clip>& clips) {
- for (const auto& other : clips)
- if (&other != &clip && other.kind == ClipKind::audio && other.channel == clip.channel)
- if (const auto m = audioMixerOf (song, other.track); m > 0 && m != mixer)
- shared = true;
- });
- return shared;
- }
- } // namespace
- std::vector<int> audioTrackConflicts (const Song& song, const std::vector<int>& touched)
- {
- std::vector<int> channels;
- for (const auto i : touched)
- {
- if (i < 0 || i >= (int) song.playlist.size())
- continue;
- const auto& clip = song.playlist[(std::size_t) i];
- const auto mixer = wantedMixer (song, clip);
- if (mixer > 0 && std::find (channels.begin(), channels.end(), clip.channel) == channels.end()
- && sharedWithOtherAudioTrack (song, clip, mixer))
- channels.push_back (clip.channel);
- }
- return channels;
- }
- int followAudioTracks (Song& song, const std::vector<int>& touched, bool makeUnique)
- {
- int changed = 0;
- std::vector<int> done; // (FL: the touched clips of a channel it asked about are handled together)
- for (const auto i : touched)
- {
- if (i < 0 || i >= (int) song.playlist.size() || std::find (done.begin(), done.end(), i) != done.end())
- continue;
- const auto clip = song.playlist[(std::size_t) i];
- const auto mixer = wantedMixer (song, clip);
- if (mixer <= 0)
- continue;
- if (! sharedWithOtherAudioTrack (song, song.playlist[(std::size_t) i], mixer))
- {
- song.channels[(std::size_t) clip.channel].mixerTrack = mixer; // every clip of the channel follows (FL)
- ++changed;
- continue;
- }
- // "Make clip unique?": every touched clip of this channel gets the copy (Yes) or stays as it is (No)
- auto copy = -1;
- if (makeUnique)
- {
- auto def = song.channels[(std::size_t) clip.channel];
- def.mixerTrack = mixer;
- song.channels.push_back (def);
- copy = (int) song.channels.size() - 1;
- }
- for (const auto j : touched)
- if (j >= 0 && j < (int) song.playlist.size() && song.playlist[(std::size_t) j].channel == clip.channel
- && song.playlist[(std::size_t) j].kind == ClipKind::audio && std::find (done.begin(), done.end(), j) == done.end())
- {
- done.push_back (j);
- if (copy >= 0)
- {
- song.playlist[(std::size_t) j].channel = copy;
- ++changed;
- }
- }
- }
- return changed;
- }
- HeaderDropOptions& headerDropOptions() noexcept
- {
- static HeaderDropOptions options;
- return options;
- }
- bool dropOnHeader (Song& song, int track, const std::string& path, const std::string& name, std::int64_t lengthTicks, HeaderDrop how, bool lock)
- {
- if (track < 0 || track >= 500 || how == HeaderDrop::ask || path.empty())
- return false;
- if (track < (int) song.playlistTracks.size() && song.playlistTracks[(std::size_t) track].mode != PlaylistTrackMode::unassigned)
- return false; // (FUN_00e71980: only an unassigned track takes a header drop)
- const auto before = song; // (all or nothing)
- auto fail = [&song, &before] {
- song = before;
- return false;
- };
- auto newChannel = [&song, &name, &path] (InstrumentKind kind) {
- ChannelDef c;
- c.name = quickrender::uniqueChannelName (song, name);
- c.instrument = kind;
- c.samplePath = path;
- if (kind == InstrumentKind::audioClip)
- c.sampler.declickMode = audioClipDeclickMode; // FL's AudioTrack.fst
- song.channels.push_back (c);
- return (int) song.channels.size() - 1;
- };
- auto placeAudio = [&song, track, lengthTicks] (int channel) {
- Clip clip;
- clip.kind = ClipKind::audio;
- clip.channel = channel;
- clip.track = track;
- clip.start = 0;
- clip.length = std::max<std::int64_t> (1, lengthTicks);
- song.playlist.push_back (clip);
- followAudioTracks (song, { (int) song.playlist.size() - 1 });
- };
- if (how == HeaderDrop::audioClips)
- {
- int channel = -1;
- for (int c = 0; c < (int) song.channels.size(); ++c)
- if (song.channels[(std::size_t) c].instrument == InstrumentKind::audioClip && song.channels[(std::size_t) c].samplePath == path)
- channel = c;
- if (channel < 0)
- {
- channel = newChannel (InstrumentKind::audioClip);
- song.channels[(std::size_t) channel].mixerTrack = song.claimFreeInsert();
- }
- auto* t = trackAt (song, track);
- t->name = song.channels[(std::size_t) channel].name;
- if (const auto colour = song.channels[(std::size_t) channel].colour; colour != 0)
- t->colour = colour;
- placeAudio (channel);
- }
- else if (how == HeaderDrop::audioTracks)
- {
- const auto channel = newChannel (InstrumentKind::audioClip);
- auto* t = trackAt (song, track);
- t->name = song.channels[(std::size_t) channel].name;
- if (! makeAudioTrack (song, track))
- return fail();
- placeAudio (channel); // (routed to the track's mixer track by followAudioTracks)
- }
- else
- {
- const auto channel = newChannel (InstrumentKind::sampler);
- trackAt (song, track)->name.clear(); // (named after its channel)
- if (! makeInstrumentTrack (song, track, channel))
- return fail();
- const auto pattern = instrumentTrackPattern (song, track);
- if (pattern < 0)
- return fail();
- Clip clip;
- clip.kind = ClipKind::pattern;
- clip.pattern = pattern;
- clip.track = track;
- clip.start = 0;
- clip.length = song.patterns[(std::size_t) pattern].effectiveLength (song.meter());
- song.playlist.push_back (clip);
- }
- if (lock)
- trackAt (song, track)->lockedToContent = true;
- song.mode = PlayMode::song;
- return true;
- }
- } // namespace lynx::trackmodes
- namespace lynx::audioclips
- {
- int repairChannels (Song& song)
- {
- int changed = 0;
- const auto channels = (int) song.channels.size(); // (copies added below are audio clip channels already)
- for (int index = 0; index < channels; ++index)
- {
- if (song.channels[(std::size_t) index].instrument != InstrumentKind::sampler || song.channels[(std::size_t) index].samplePath.empty())
- continue;
- auto used = false;
- song.forEachClipList ([&] (const std::vector<Clip>& clips) {
- for (const auto& c : clips)
- used = used || (c.kind == ClipKind::audio && c.channel == index);
- });
- if (! used)
- continue;
- const auto playsNotes = std::any_of (song.patterns.begin(), song.patterns.end(), [index] (const Pattern& p) {
- return std::any_of (p.notes.begin(), p.notes.end(), [index] (const Note& n) { return n.channel == index; });
- });
- ++changed;
- if (! playsNotes)
- {
- song.channels[(std::size_t) index].instrument = InstrumentKind::audioClip;
- continue;
- }
- auto copy = song.channels[(std::size_t) index];
- copy.instrument = InstrumentKind::audioClip;
- copy.instance.renew(); // (a new channel: the renderer must not take it for the Sampler it was copied from)
- song.channels.push_back (std::move (copy));
- const auto target = (int) song.channels.size() - 1;
- song.forEachClipList ([&] (std::vector<Clip>& clips) {
- for (auto& c : clips)
- if (c.kind == ClipKind::audio && c.channel == index)
- c.channel = target;
- });
- }
- return changed;
- }
- void loadSampleInto (ChannelDef& channel, const std::string& path, const std::string& name, bool autoName)
- {
- channel.samplePath = path;
- samplehistory::add (path); // FL's Sample history (round 190df): every sample a channel loads
- if (channel.instrument == InstrumentKind::granulizer || channel.instrument == InstrumentKind::wavetraveller)
- return; // FL's Granulizer keeps its engine and plays the file (round 142); so does Wave Traveller
- if (channel.instrument != InstrumentKind::audioClip)
- channel.instrument = InstrumentKind::sampler;
- // FL's General › "Auto name channels" (round 190bx): off = a channel that has a name keeps it
- if (autoName || channel.name.empty())
- channel.name = name;
- }
- bool replaceClipSample (Song& song, int clip, const std::string& path, const std::string& name, std::int64_t oldTicks,
- std::int64_t newTicks, bool autoName)
- {
- if (clip < 0 || clip >= (int) song.playlist.size() || path.empty())
- return false;
- const auto index = song.playlist[(std::size_t) clip].channel;
- if (song.playlist[(std::size_t) clip].kind != ClipKind::audio || index < 0 || index >= (int) song.channels.size())
- return false;
- auto& channel = song.channels[(std::size_t) index];
- if (channel.instrument != InstrumentKind::audioClip)
- return false;
- loadSampleInto (channel, path, name, autoName);
- channel.zones.clear();
- channel.sampler.slices.clear();
- channel.sampler.loopStart = channel.sampler.loopEnd = -1;
- channel.sampler.warp.clear();
- channel.sampler.stretchBeats = 0.0f;
- channel.sampler.stretchMul = 0;
- if (oldTicks <= 0 || newTicks <= 0)
- return true;
- song.forEachClipList ([&] (std::vector<Clip>& clips) {
- for (auto& c : clips)
- {
- if (c.kind != ClipKind::audio || c.channel != index || c.offset != 0 || std::abs (c.offsetFraction) > 1.0e-3f)
- continue;
- const auto stretch = c.stretch > 0.0 ? c.stretch : 1.0;
- const auto whole = (double) oldTicks * stretch;
- if (std::abs ((double) c.length - whole) > std::max (1.0, whole * 0.002)) // (a trimmed clip keeps its length)
- continue;
- c.length = std::max<std::int64_t> (1, (std::int64_t) std::llround ((double) newTicks * stretch));
- c.fadeIn = std::min (c.fadeIn, c.length);
- c.fadeOut = std::min (c.fadeOut, c.length - c.fadeIn);
- }
- });
- return true;
- }
- } // namespace lynx::audioclips
- namespace lynx::autoclipplace
- {
- Place forNewClip (const Song& song, bool atPlayback, std::int64_t playTick)
- {
- const auto bar = song.barTicks();
- const auto songEnd = song.playlist.empty() ? (std::int64_t) 0 : song.songLength();
- Place place;
- if (song.hasLoop())
- {
- place.start = song.loopStart;
- place.length = song.loopEnd - song.loopStart;
- }
- else
- {
- // FL (checked live, round 177b): the clip starts at the song position even past the last clip, and is one bar
- // long when nothing lies after it.
- place.start = atPlayback ? std::max ((std::int64_t) 0, playTick) : 0;
- place.length = std::max (bar, songEnd - place.start);
- }
- // FL (checked live, round 177b): the first track with no clips at all; a free gap on a used track is not taken
- auto busy = [&] (int track) {
- if (track < (int) song.playlistTracks.size() && song.playlistTracks[(std::size_t) track].mode == PlaylistTrackMode::instrument)
- return true;
- for (const auto& c : song.playlist)
- if (c.track == track)
- return true;
- return false;
- };
- while (busy (place.track))
- ++place.track;
- return place;
- }
- } // namespace lynx::autoclipplace
- namespace lynx::autolanes
- {
- int ownerTrack (const Song& song, int automation)
- {
- for (int t = 0; t < (int) song.playlistTracks.size(); ++t)
- for (const auto& l : song.playlistTracks[(std::size_t) t].lanes)
- if (l.automation == automation)
- return t;
- return -1;
- }
- namespace
- {
- std::int64_t songEnd (const Song& song)
- {
- const auto bar = song.barTicks();
- return std::max (bar * 4, song.playlist.empty() ? 0 : song.songLength());
- }
- int laneFor (const Song& song, int track, const AutomationTarget& target)
- {
- if (track < 0 || track >= (int) song.playlistTracks.size())
- return -1;
- for (const auto& l : song.playlistTracks[(std::size_t) track].lanes)
- if (l.automation >= 0 && l.automation < (int) song.automations.size() && song.automations[(std::size_t) l.automation].target == target)
- return l.automation;
- return -1;
- }
- } // namespace
- int trackFor (const Song& song, const AutomationTarget& target)
- {
- const auto& tracks = song.playlistTracks;
- for (int t = 0; t < (int) tracks.size(); ++t)
- if (laneFor (song, t, target) >= 0)
- return t;
- int channel = -1, insert = -1;
- switch (target.kind)
- {
- case AutomationTargetKind::channelVolume:
- case AutomationTargetKind::channelPan:
- case AutomationTargetKind::channelParam:
- case AutomationTargetKind::channelAdjust:
- channel = target.index;
- if (channel >= 0 && channel < (int) song.channels.size())
- insert = song.channels[(std::size_t) channel].mixerTrack;
- break;
- case AutomationTargetKind::mixerVolume:
- case AutomationTargetKind::mixerPan:
- case AutomationTargetKind::effectParam:
- case AutomationTargetKind::effectMix:
- case AutomationTargetKind::mixerSend:
- insert = target.index;
- break;
- case AutomationTargetKind::none:
- case AutomationTargetKind::tempo:
- case AutomationTargetKind::crossfader: // (round 190kc)
- break;
- }
- if (channel >= 0)
- for (int t = 0; t < (int) tracks.size(); ++t)
- if (tracks[(std::size_t) t].mode == PlaylistTrackMode::instrument && tracks[(std::size_t) t].channel == channel)
- return t;
- if (insert > 0)
- for (int t = 0; t < (int) tracks.size(); ++t)
- if (tracks[(std::size_t) t].mode != PlaylistTrackMode::unassigned && tracks[(std::size_t) t].mixerTrack == insert)
- return t;
- for (int t = 0; t < 500; ++t)
- {
- if (t < (int) tracks.size() && ! tracks[(std::size_t) t].lanes.empty())
- continue;
- if (std::none_of (song.playlist.begin(), song.playlist.end(), [t] (const Clip& c) { return c.track == t; }))
- return t;
- }
- return 0;
- }
- int addLane (Song& song, int track, const AutomationTarget& target, const std::string& name, float value)
- {
- if (track < 0 || track >= 500)
- return -1;
- if (const auto existing = laneFor (song, track, target); existing >= 0)
- return existing;
- if ((int) song.playlistTracks.size() <= track)
- song.playlistTracks.resize ((std::size_t) track + 1);
- Automation a;
- a.name = name;
- a.target = target;
- a.points = { { 0, value, 0.0f }, { songEnd (song), value, 0.0f } };
- song.automations.push_back (std::move (a));
- const auto index = (int) song.automations.size() - 1;
- song.playlistTracks[(std::size_t) track].lanes.push_back ({ index, 0 });
- song.playlistTracks[(std::size_t) track].lanesFolded = false;
- return index;
- }
- bool removeLane (Song& song, int track, int lane)
- {
- if (track < 0 || track >= (int) song.playlistTracks.size() || lane < 0 || lane >= (int) song.playlistTracks[(std::size_t) track].lanes.size())
- return false;
- song.removeAutomation (song.playlistTracks[(std::size_t) track].lanes[(std::size_t) lane].automation); // (drops the lane too)
- return true;
- }
- int clipToLane (Song& song, int clipIndex, int track)
- {
- if (clipIndex < 0 || clipIndex >= (int) song.playlist.size())
- return -1;
- const auto clip = song.playlist[(std::size_t) clipIndex];
- if (clip.kind != ClipKind::automation || clip.automation < 0 || clip.automation >= (int) song.automations.size() || clip.length <= 0)
- return -1;
- const auto source = song.automations[(std::size_t) clip.automation]; // (a copy: addLane may grow the list)
- auto lane = laneFor (song, track, source.target);
- if (lane < 0)
- lane = addLane (song, track, source.target, source.name, source.valueAt ((double) clip.offset));
- if (lane < 0)
- return -1;
- // the clip's curve in song time: its edges exact, the points inside it shifted by start − offset
- std::vector<AutomationPoint> inside;
- inside.push_back ({ clip.start, source.valueAt ((double) clip.offset), 0.0f });
- for (const auto& p : source.points)
- if (p.tick > clip.offset && p.tick < clip.offset + clip.length)
- inside.push_back ({ clip.start + p.tick - clip.offset, p.value, p.tension, p.mode });
- inside.push_back ({ clip.end(), source.valueAt ((double) (clip.offset + clip.length)), 0.0f });
- auto& points = song.automations[(std::size_t) lane].points;
- // what the lane had before and after the clip stays its own value up to the clip's edges
- const auto before = song.automations[(std::size_t) lane].valueAt ((double) clip.start - 1.0);
- const auto after = song.automations[(std::size_t) lane].valueAt ((double) clip.end() + 1.0);
- points.erase (std::remove_if (points.begin(), points.end(), [&] (const AutomationPoint& p) { return p.tick >= clip.start && p.tick <= clip.end(); }),
- points.end());
- points.insert (points.end(), inside.begin(), inside.end());
- if (clip.start > 0 && std::none_of (points.begin(), points.end(), [&] (const AutomationPoint& p) { return p.tick == clip.start - 1; }))
- points.push_back ({ clip.start - 1, before, 0.0f });
- if (std::none_of (points.begin(), points.end(), [&] (const AutomationPoint& p) { return p.tick == clip.end() + 1; }))
- points.push_back ({ clip.end() + 1, after, 0.0f });
- std::sort (points.begin(), points.end(), [] (const AutomationPoint& a, const AutomationPoint& b) { return a.tick < b.tick; });
- // the clip goes (its automation stays: other clips may use it, as FL keeps it)
- song.playlist.erase (song.playlist.begin() + clipIndex);
- return lane;
- }
- int laneToClip (Song& song, int track, int lane)
- {
- if (track < 0 || track >= (int) song.playlistTracks.size() || lane < 0 || lane >= (int) song.playlistTracks[(std::size_t) track].lanes.size())
- return -1;
- const auto automation = song.playlistTracks[(std::size_t) track].lanes[(std::size_t) lane].automation;
- auto& lanes = song.playlistTracks[(std::size_t) track].lanes;
- lanes.erase (lanes.begin() + lane);
- if (automation < 0 || automation >= (int) song.automations.size())
- return -1;
- const auto& points = song.automations[(std::size_t) automation].points;
- const auto end = std::max (songEnd (song), points.empty() ? 0 : points.back().tick);
- Clip clip;
- clip.kind = ClipKind::automation;
- clip.automation = automation;
- clip.start = 0;
- clip.length = std::max<std::int64_t> (1, end);
- clip.track = 0;
- // the first track whose [0, end) is free (instrument tracks take no automation clips)
- auto busy = [&] (int t) {
- if (t < (int) song.playlistTracks.size() && song.playlistTracks[(std::size_t) t].mode == PlaylistTrackMode::instrument)
- return true;
- for (const auto& c : song.playlist)
- if (c.track == t && c.start < clip.length && c.end() > 0)
- return true;
- return false;
- };
- while (busy (clip.track))
- ++clip.track;
- song.playlist.push_back (clip);
- return (int) song.playlist.size() - 1;
- }
- int lanesToClips (Song& song)
- {
- int n = 0;
- for (int t = 0; t < (int) song.playlistTracks.size(); ++t)
- while (! song.playlistTracks[(std::size_t) t].lanes.empty())
- if (laneToClip (song, t, 0) >= 0)
- ++n;
- return n;
- }
- } // namespace lynx::autolanes
- namespace lynx::autolink
- {
- namespace
- {
- constexpr int maxTracks = 500;
- bool linkedToAnotherTrack (const Song& song, int mixer, int track)
- {
- for (int i = 0; i < (int) song.playlistTracks.size(); ++i)
- if (i != track && linkedInsertOf (song, i) == mixer)
- return true;
- return false;
- }
- void addOnce (std::vector<int>& v, int x)
- {
- if (std::find (v.begin(), v.end(), x) == v.end())
- v.push_back (x);
- }
- bool contains (const std::vector<int>& v, int x) { return std::find (v.begin(), v.end(), x) != v.end(); }
- // The channels each track plays (audio clips' channels, the notes' channels of its pattern clips; automation clips don't
- // count), tracks 0..499, every clip list in one pass (a pattern's channels are gathered once)
- std::vector<std::vector<int>> channelsByTrack (const Song& song)
- {
- std::vector<std::vector<int>> out (maxTracks);
- std::vector<std::vector<int>> patternChannels (song.patterns.size());
- std::vector<bool> gathered (song.patterns.size(), false);
- const auto channels = (int) song.channels.size();
- song.forEachClipList ([&] (const std::vector<Clip>& clips) {
- for (const auto& c : clips)
- {
- if (c.track < 0 || c.track >= maxTracks)
- continue;
- auto& t = out[(std::size_t) c.track];
- if (c.kind == ClipKind::audio)
- {
- if (c.channel >= 0 && c.channel < channels)
- addOnce (t, c.channel);
- }
- else if (c.kind == ClipKind::pattern && c.pattern >= 0 && c.pattern < (int) song.patterns.size())
- {
- const auto p = (std::size_t) c.pattern;
- if (! gathered[p])
- {
- gathered[p] = true;
- for (const auto& n : song.patterns[p].notes)
- if (n.channel >= 0 && n.channel < channels)
- addOnce (patternChannels[p], n.channel);
- }
- for (const auto ch : patternChannels[p])
- addOnce (t, ch);
- }
- }
- });
- return out;
- }
- std::vector<int> homesFrom (const Song& song, const std::vector<std::vector<int>>& byTrack)
- {
- std::vector<int> homes (song.channels.size(), -1);
- for (int t = 0; t < maxTracks; ++t)
- for (const auto c : byTrack[(std::size_t) t])
- if (homes[(std::size_t) c] < 0)
- homes[(std::size_t) c] = t;
- return homes;
- }
- bool validInsert (const Song& song, int m) { return m > 0 && m < (int) song.mixer.size(); }
- int routeOf (const Song& song, int channel)
- {
- const auto m = song.channels[(std::size_t) channel].mixerTrack;
- return validInsert (song, m) ? m : 0;
- }
- bool receivesSends (const Song& song, int mixer)
- {
- for (int from = 1; from < (int) song.mixer.size(); ++from)
- if (from != mixer)
- for (const auto& s : song.mixer[(std::size_t) from].sends)
- if (s.target == mixer)
- return true;
- return false;
- }
- // Only track `track`'s own channels (or channels no track plays) go through `mixer`, nothing sends into it and no other
- // track is linked to it: it belongs to that track's content alone.
- bool exclusiveTo (const Song& song, int mixer, int track, const std::vector<int>& homes)
- {
- if (! validInsert (song, mixer) || receivesSends (song, mixer) || linkedToAnotherTrack (song, mixer, track)
- || song.session.reservedForOther (true, mixer))
- return false;
- for (int c = 0; c < (int) song.channels.size(); ++c)
- if (song.channels[(std::size_t) c].mixerTrack == mixer && homes[(std::size_t) c] >= 0 && homes[(std::size_t) c] != track)
- return false;
- return true;
- }
- // `track` can take insert `mixer` as it is: neutral, and no other track's channels, sends or links use it
- bool freeFor (const Song& song, int mixer, int track, const std::vector<int>& homes)
- {
- return isNeutral (song, mixer) && exclusiveTo (song, mixer, track, homes);
- }
- // a free neutral insert to move what sits at `mixer` to (after it first, then before), a new one when there is none
- int freeInsertBesides (Song& song, int mixer)
- {
- for (int m = mixer + 1; m < (int) song.mixer.size(); ++m)
- if (isNeutralFree (song, m))
- return m;
- for (int m = 1; m < mixer; ++m)
- if (isNeutralFree (song, m))
- return m;
- return trackmodes::addMixerTrack (song);
- }
- std::string insertName (const Song& song, int m)
- {
- return trackmodes::hasOwnName (song, m) ? song.mixer[(std::size_t) m].name : std::string(); // ("": its default name)
- }
- void note (Report* report, Change change)
- {
- if (report != nullptr)
- report->changes.push_back (std::move (change));
- }
- // One track: its own channels (`only`: these alone; empty: all of them) go through insert track + 1, and the track is
- // linked to it. `follow`: an edit's new content (nothing that changes the sound). True when the track got linked.
- bool pairTrack (Song& song, int track, const std::vector<int>& homes, const std::vector<int>& content, const std::vector<int>& only,
- bool follow, Report* report)
- {
- const auto n = track + 1;
- if (n >= maxMixerTracks || song.session.reservedForOther (true, n))
- return false;
- std::vector<int> own;
- for (int c = 0; c < (int) homes.size(); ++c)
- if (homes[(std::size_t) c] == track && (only.empty() || contains (only, c)))
- own.push_back (c);
- if (own.empty())
- return false;
- // 1. inserts up to N (empty inserts change nothing)
- const auto mixerBefore = song.mixer.size();
- while ((int) song.mixer.size() <= n)
- if (trackmodes::addMixerTrack (song) < 0)
- {
- song.mixer.resize (mixerBefore);
- return false;
- }
- const auto routes = [&] {
- std::vector<int> s;
- for (const auto c : own)
- addOnce (s, routeOf (song, c));
- return s;
- };
- auto from = routes();
- const auto linked = linkedInsertOf (song, track);
- // 2. what lives at insert N and is not this track's goes to a free insert (a swap: everything that refers to it
- // follows, the sound stays)
- if (! contains (from, n) && linked != n && ! freeFor (song, n, track, homes))
- {
- const auto away = freeInsertBesides (song, n);
- if (away <= 0 || away == n)
- return false;
- note (report, { Change::Kind::moveInsert, -1, -1, n, away, insertName (song, n), false });
- mixerops::swapTracks (song, n, away);
- from = routes();
- }
- // 3. the track's content all goes through one insert with settings or a name of its own (an effect chain, a bus the
- // user named) or that the track is linked to: that insert becomes insert N
- if (from.size() == 1 && from[0] != n && from[0] > 0 && exclusiveTo (song, from[0], track, homes)
- && (! isNeutral (song, from[0]) || from[0] == linked || trackmodes::hasOwnName (song, from[0])))
- {
- note (report, { Change::Kind::moveInsert, track, -1, from[0], n, insertName (song, from[0]), false });
- mixerops::swapTracks (song, from[0], n);
- from = routes();
- }
- // 4. the rest goes into insert N: an insert with settings of its own that only this track uses sends into it (its
- // sound stays); a channel on the Master, on a neutral insert or on an insert others share moves to it
- const auto intoN = isNeutral (song, n);
- std::vector<int> bussed;
- for (const auto c : own)
- {
- const auto m = routeOf (song, c);
- if (m == n || contains (bussed, m))
- continue;
- if (m > 0 && ! isNeutral (song, m) && exclusiveTo (song, m, track, homes) && song.canRoute (m, n))
- {
- note (report, { Change::Kind::routeInsert, track, -1, m, n, insertName (song, m), ! intoN });
- if (follow && ! intoN)
- continue;
- auto& source = song.mixer[(std::size_t) m];
- const auto level = source.masterLevel;
- source.toMaster = false;
- song.setRoute (m, n, true, level);
- bussed.push_back (m);
- continue;
- }
- const auto changesSound = ! intoN || (m > 0 && ! isNeutral (song, m));
- if (follow && changesSound)
- continue;
- note (report, { Change::Kind::routeChannel, track, c, m, n, song.channels[(std::size_t) c].name, changesSound });
- song.channels[(std::size_t) c].mixerTrack = n;
- }
- // (an edit's new content that could not go through insert N without changing its sound: the track stays as it is)
- if (follow && linked != n
- && std::none_of (own.begin(), own.end(), [&] (int c) { return routeOf (song, c) == n || contains (bussed, routeOf (song, c)); }))
- return false;
- // 5. the link: FL's instrument track when the track plays one generator, else FL's audio track on insert N
- if ((int) song.playlistTracks.size() <= track)
- song.playlistTracks.resize ((std::size_t) track + 1);
- for (int i = 0; i < (int) song.playlistTracks.size(); ++i) // (one track per insert)
- if (i != track && linkedInsertOf (song, i) == n)
- trackmodes::unassign (song, i);
- auto& t = song.playlistTracks[(std::size_t) track];
- const auto wasLinked = t.mode != PlaylistTrackMode::unassigned && linked == n;
- const auto single = content.size() == 1 && own.size() == 1 && content[0] == own[0] && routeOf (song, own[0]) == n
- && trackmodes::canBeInstrumentTrack (song, own[0]);
- if (single)
- {
- for (const auto other : trackmodes::instrumentTracksOf (song, own[0])) // (a channel belongs to one instrument track)
- if (other != track)
- trackmodes::unassign (song, other);
- t.mode = PlaylistTrackMode::instrument;
- t.channel = own[0];
- }
- else
- {
- t.mode = PlaylistTrackMode::audio;
- t.channel = -1;
- }
- t.mixerTrack = n;
- // 6. one name and colour: the track's own name, else the insert's, else the one instrument's or sample's
- auto& m = song.mixer[(std::size_t) n];
- if (t.name.empty() && trackmodes::hasOwnName (song, n))
- t.name = m.name;
- else if (t.name.empty() && own.size() == 1 && content.size() == 1)
- t.name = song.channels[(std::size_t) own[0]].name;
- if (! t.name.empty())
- m.name = t.name;
- if (t.colour == 0)
- t.colour = m.colour;
- else
- m.colour = t.colour;
- if (! wasLinked)
- note (report, { Change::Kind::linkTrack, track, single ? own[0] : -1, -1, n, t.name, false });
- return ! wasLinked;
- }
- } // namespace
- int linkedInsertOf (const Song& song, int track)
- {
- if (track < 0 || track >= (int) song.playlistTracks.size())
- return -1;
- const auto& t = song.playlistTracks[(std::size_t) track];
- if (t.mode == PlaylistTrackMode::audio)
- return t.mixerTrack;
- if (t.mode == PlaylistTrackMode::instrument)
- return t.channel >= 0 && t.channel < (int) song.channels.size() ? song.channels[(std::size_t) t.channel].mixerTrack : t.mixerTrack;
- return -1;
- }
- std::vector<int> otherInsertsOnTrack (const Song& song, int track, int insert)
- {
- std::vector<int> out;
- const auto add = [&] (int channel) {
- if (channel < 0 || channel >= (int) song.channels.size())
- return;
- const auto m = song.channels[(std::size_t) channel].mixerTrack;
- if (m != insert && std::find (out.begin(), out.end(), m) == out.end())
- out.push_back (m);
- };
- std::vector<char> seen (song.patterns.size(), 0);
- for (const auto& clip : song.playlist)
- {
- if (clip.track != track || clip.length <= 0)
- continue;
- if (clip.kind == ClipKind::audio)
- add (clip.channel);
- else if (clip.kind == ClipKind::pattern && clip.pattern >= 0 && clip.pattern < (int) song.patterns.size() && ! seen[(std::size_t) clip.pattern])
- {
- seen[(std::size_t) clip.pattern] = 1;
- for (const auto& n : song.patterns[(std::size_t) clip.pattern].notes)
- add (n.channel);
- }
- }
- std::sort (out.begin(), out.end());
- return out;
- }
- bool isNeutral (const Song& song, int mixer)
- {
- if (! validInsert (song, mixer))
- return false;
- const auto& m = song.mixer[(std::size_t) mixer];
- const MixerTrack plain;
- return m.effects.empty() && m.volume == plain.volume && m.pan == plain.pan && ! m.muted && ! m.solo && m.toMaster
- && m.masterLevel == plain.masterLevel && m.stereo == plain.stereo && ! m.invertPhase && ! m.swapChannels && m.sends.empty()
- && m.eq == plain.eq && m.latencyMs == plain.latencyMs && m.outputChannel == plain.outputChannel && m.inputChannel < 0;
- }
- bool isNeutralFree (const Song& song, int mixer)
- {
- if (! isNeutral (song, mixer) || song.session.reservedForOther (true, mixer) || linkedToAnotherTrack (song, mixer, -1)
- || receivesSends (song, mixer))
- return false;
- for (const auto& c : song.channels)
- if (c.mixerTrack == mixer)
- return false;
- return true;
- }
- std::vector<int> homeTracks (const Song& song)
- {
- return homesFrom (song, channelsByTrack (song));
- }
- std::vector<int> repeatedTracks (const Song& song)
- {
- const auto byTrack = channelsByTrack (song);
- const auto homes = homesFrom (song, byTrack);
- std::vector<int> out (maxTracks, -1);
- for (int t = 0; t < maxTracks; ++t)
- {
- const auto& played = byTrack[(std::size_t) t];
- if (played.empty() || std::any_of (played.begin(), played.end(), [&] (int c) { return homes[(std::size_t) c] == t; }))
- continue;
- out[(std::size_t) t] = homes[(std::size_t) played.front()];
- }
- return out;
- }
- bool Report::changesRouting() const
- {
- return std::any_of (changes.begin(), changes.end(), [] (const Change& c) { return c.kind != Change::Kind::linkTrack; });
- }
- bool Report::changesSound() const
- {
- return std::any_of (changes.begin(), changes.end(), [] (const Change& c) { return c.changesSound; });
- }
- int setOn (Song& song, bool on, Report* report)
- {
- song.linkTracksToMixer = on;
- if (! on)
- return 0;
- const auto byTrack = channelsByTrack (song);
- const auto homes = homesFrom (song, byTrack);
- int linked = 0;
- for (int t = 0; t < maxTracks; ++t)
- if (pairTrack (song, t, homes, byTrack[(std::size_t) t], {}, false, report))
- ++linked;
- if (report != nullptr)
- {
- const auto repeats = repeatedTracks (song);
- for (int t = 0; t < maxTracks; ++t)
- if (repeats[(std::size_t) t] >= 0)
- report->repeats.push_back ({ t, repeats[(std::size_t) t] });
- }
- return linked;
- }
- Report planOn (const Song& song)
- {
- auto copy = song;
- Report report;
- setOn (copy, true, &report);
- return report;
- }
- Snapshot snapshot (const Song& song)
- {
- Snapshot s;
- s.on = song.linkTracksToMixer;
- if (! s.on)
- return s;
- s.homes = homeTracks (song);
- for (int t = 0; t < (int) song.playlistTracks.size(); ++t)
- if (const auto m = linkedInsertOf (song, t); validInsert (song, m))
- {
- const auto& pt = song.playlistTracks[(std::size_t) t];
- const auto& mt = song.mixer[(std::size_t) m];
- s.pairs.push_back ({ t, m, pt.name, pt.colour, mt.name, mt.colour });
- }
- return s;
- }
- int followNewContent (Song& song, const Snapshot& before)
- {
- if (! song.linkTracksToMixer || ! before.on)
- return 0;
- // names and colours: a linked track and its insert keep one name and colour, whichever was renamed
- for (const auto& p : before.pairs)
- {
- if (linkedInsertOf (song, p.track) != p.mixer || ! validInsert (song, p.mixer))
- continue;
- auto& t = song.playlistTracks[(std::size_t) p.track];
- auto& m = song.mixer[(std::size_t) p.mixer];
- const auto trackChanged = t.name != p.trackName || t.colour != p.trackColour;
- const auto insertChanged = m.name != p.insertName || m.colour != p.insertColour;
- if (trackChanged && ! insertChanged)
- {
- m.name = t.name.empty() ? "Insert " + std::to_string (p.mixer) : t.name;
- m.colour = t.colour;
- }
- else if (insertChanged && ! trackChanged)
- {
- t.name = trackmodes::hasOwnName (song, p.mixer) ? m.name : std::string();
- t.colour = m.colour;
- }
- }
- // new content: an instrument or sample no track played before goes through its track's insert
- const auto byTrack = channelsByTrack (song);
- const auto homes = homesFrom (song, byTrack);
- // (also one whose clips all moved from a linked track to another: it follows them, from the old track's insert)
- std::vector<std::vector<int>> fresh (maxTracks);
- for (int c = 0; c < (int) homes.size(); ++c)
- {
- const auto t = homes[(std::size_t) c];
- const auto was = c < (int) before.homes.size() ? before.homes[(std::size_t) c] : -1;
- if (t >= 0 && (was < 0 || (was != t && routeOf (song, c) == was + 1 && linkedInsertOf (song, was) == was + 1)))
- fresh[(std::size_t) t].push_back (c);
- }
- int linked = 0;
- for (int t = 0; t < maxTracks; ++t)
- {
- if (fresh[(std::size_t) t].empty())
- continue;
- const auto had = std::any_of (before.homes.begin(), before.homes.end(), [t] (int h) { return h == t; });
- const auto link = linkedInsertOf (song, t);
- if (link < 0 && had)
- continue; // Track mode › Unassigned on a track that already had content: it stays so
- if (link >= 0 && link != t + 1)
- continue; // linked by hand to another insert: left as it is
- if (pairTrack (song, t, homes, byTrack[(std::size_t) t], fresh[(std::size_t) t], true, nullptr))
- ++linked;
- }
- return linked;
- }
- int matchLinkedColours (Song& song)
- {
- if (! song.linkTracksToMixer)
- return 0;
- int changed = 0;
- for (int t = 0; t < (int) song.playlistTracks.size(); ++t)
- if (const auto m = linkedInsertOf (song, t); validInsert (song, m))
- {
- auto& track = song.playlistTracks[(std::size_t) t];
- auto& insert = song.mixer[(std::size_t) m];
- if (track.colour == insert.colour || (track.colour != 0 && insert.colour != 0))
- continue;
- if (track.colour == 0)
- track.colour = insert.colour;
- else
- insert.colour = track.colour;
- ++changed;
- }
- return changed;
- }
- int pairedTakeTrack (const Song& song, int insert)
- {
- const auto track = insert - 1;
- if (! song.linkTracksToMixer || insert <= 0 || insert >= (int) song.mixer.size() || track >= maxTracks)
- return -1;
- if (track < (int) song.playlistTracks.size())
- {
- const auto& t = song.playlistTracks[(std::size_t) track];
- if (t.mode != PlaylistTrackMode::unassigned || t.grouped)
- return -1;
- }
- bool used = false;
- song.forEachClipList ([&] (const std::vector<Clip>& clips) {
- used = used || std::any_of (clips.begin(), clips.end(), [track] (const Clip& c) { return c.track == track; });
- });
- return used ? -1 : track;
- }
- } // namespace lynx::autolink
|