| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598 |
- #include "core/RackRenderer.h"
- #include "core/ModelParams.h"
- #include "core/SamplePrecompute.h"
- #include "core/MetronomeVoice.h"
- #include "core/ChannelLoops.h"
- #include "core/ControlSurface.h"
- #include "core/VoltageController.h"
- #include "core/FlSynths.h"
- #include "core/HardwareInsert.h"
- #include "core/Layer.h"
- #include "core/RackOps.h"
- #include "core/HostSlide.h"
- #include "core/Vfx.h"
- #include "core/FacetEq.h"
- #include "core/MidiControls.h"
- #include "core/MidiOut.h"
- #include "core/Patcher.h"
- #include "core/LoadTrace.h"
- #include "core/DynamicsSuite.h"
- #include "core/MixSuite.h"
- #include "core/VoiceTools.h"
- #include "core/WorkerPool.h"
- #include "core/Denormals.h"
- #include "core/FlKeys.h"
- #include "core/FlVoiceProcessor.h"
- #include "core/DirectWave.h"
- #include "core/SoundFont.h"
- #include "core/NewtoneFx.h"
- #include "core/Dvs.h"
- #include "core/Scratcher.h"
- #include <algorithm>
- #include <atomic>
- #include <chrono>
- #include <cstdlib>
- #include <ctime>
- #include <cmath>
- #include <cstring>
- #include <limits>
- #include <map>
- #include <mutex>
- #include <thread>
- namespace lynx
- {
- namespace
- {
- // a hosted plugin is created again when its processing options change (fixed blocks, own process)
- bool samePluginProcessing (const PluginRef& a, const PluginRef& b) noexcept
- {
- return a.fixedBuffers == b.fixedBuffers && a.separateProcess == b.separateProcess && a.maxSizeBuffers == b.maxSizeBuffers
- && a.hostMaxBuffer == b.hostMaxBuffer;
- }
- // Silent instrument: plugin channel that could not be loaded.
- class SilentInstrument final : public Instrument
- {
- public:
- void prepare (double) noexcept override {}
- void noteOn (int, float) noexcept override {}
- void noteOff (int) noexcept override {}
- void allNotesOff() noexcept override {}
- void render (float*, float*, int, float, float) noexcept override {}
- };
- // FL Keys: the data FL builds from a wavetable's files, shared by every channel that plays the same wavetable
- // (Grand Piano holds 40 MB of 16-bit samples)
- std::shared_ptr<const flkeys::Wavetable> flKeysWavetable (const ChannelDef& channel, const RackState::SampleProvider& samples)
- {
- const auto table = flkeys::FlKeysTable::decode (channel.modelParams);
- if (! table || ! samples || table->files.size() != channel.zones.size())
- return nullptr;
- std::string key;
- for (const auto& z : channel.zones)
- key += z.path + '\n';
- for (auto v : channel.modelParams)
- key += std::to_string (v) + ' ';
- static std::mutex lock;
- static std::map<std::string, std::weak_ptr<const flkeys::Wavetable>> cache;
- {
- std::lock_guard<std::mutex> guard (lock);
- if (auto found = cache[key].lock())
- return found;
- }
- std::vector<flkeys::FileSamples> files;
- for (std::size_t i = 0; i < channel.zones.size(); ++i)
- {
- const auto sample = samples (channel.zones[i].path);
- if (sample == nullptr || sample->frames() == 0)
- return nullptr;
- files.push_back (flkeys::toFileSamples (*sample, table->files[i].scale));
- }
- auto built = flkeys::buildWavetable (*table, files);
- std::lock_guard<std::mutex> guard (lock);
- cache[key] = built;
- return built;
- }
- // Effect that does nothing: missing plugin slot.
- class BypassEffect final : public Effect
- {
- public:
- void prepare (double) noexcept override {}
- void reset() noexcept override {}
- void process (float*, float*, int, const EffectParams&, double) noexcept override {}
- };
- } // namespace
- std::unique_ptr<const RackState> RackState::build (Song song,
- const RackState* previous,
- double sampleRate,
- const SampleProvider& samples)
- {
- Factories factories;
- factories.samples = samples;
- return build (std::move (song), previous, sampleRate, factories);
- }
- namespace
- {
- // A channel's instrument, built new (UI thread)
- // deferKeyShift (round 165i): the pitch shifters of a keep-pitch sampler are made later by the caller, all channels at
- // once in parallel (Thief: 29 such channels × 8 shifters took most of the 860 ms of rack_instruments, one by one)
- std::shared_ptr<Instrument> buildInstrument (const ChannelDef& channel, double sampleRate, double bpm, const RackState::Factories& factories,
- std::vector<Instrument*>* deferKeyShift = nullptr)
- {
- const auto& samples = factories.samples;
- const auto kind = channel.instrument;
- if (kind == InstrumentKind::plugin)
- {
- std::shared_ptr<Instrument> instrument;
- if (factories.pluginInstruments && ! channel.plugin.id.empty())
- instrument = factories.pluginInstruments (channel.plugin, sampleRate);
- return instrument != nullptr ? instrument : std::make_shared<SilentInstrument>();
- }
- // FL Keys: its wavetable built from the channel's files
- if (kind == InstrumentKind::flkeys)
- {
- std::shared_ptr<Instrument> instrument = flkeys::makeFlKeysInstrument (flKeysWavetable (channel, samples));
- instrument->prepare (sampleRate);
- instrument->configure (channel, bpm);
- return instrument;
- }
- // DirectWave (REA_rodada72): the zones' samples as the plugin holds them
- if (kind == InstrumentKind::directwave)
- {
- const auto formats = dw::decodeFormats (channel.modelParams);
- std::vector<std::shared_ptr<const dw::ZoneSamples>> zs;
- for (std::size_t z = 0; z < channel.zones.size(); ++z)
- {
- std::shared_ptr<const dw::ZoneSamples> s;
- if (samples && ! channel.zones[z].path.empty())
- if (const auto data = samples (channel.zones[z].path))
- s = dw::zoneSamples (*data, z < formats.size() && formats[z]);
- zs.push_back (std::move (s));
- }
- std::shared_ptr<Instrument> instrument = dw::makeDirectWaveInstrument (std::move (zs));
- instrument->prepare (sampleRate);
- instrument->configure (channel, bpm);
- return instrument;
- }
- // LYNX Soundfont (REA_rodada158): the font is loaded on a background thread and shared by the channels playing it
- if (kind == InstrumentKind::soundfont)
- {
- std::shared_ptr<Instrument> instrument = sf::makeInstrument (channel.samplePath.empty() ? nullptr : sf::FontLibrary::instance().get (channel.samplePath));
- instrument->prepare (sampleRate);
- instrument->configure (channel, bpm);
- return instrument;
- }
- // multi-sample sampler: one zone per sample (DirectWave, FPC layers...)
- if (kind == InstrumentKind::sampler && ! channel.zones.empty())
- {
- const loadtrace::Scope timing ("rack_zoned");
- std::vector<LoadedZone> zones;
- for (const auto& z : channel.zones)
- zones.push_back ({ z, samples ? samples (z.path) : nullptr });
- std::shared_ptr<Instrument> instrument = makeZonedSampler (std::move (zones));
- instrument->prepare (sampleRate);
- instrument->configure (channel, bpm);
- return instrument;
- }
- std::shared_ptr<const SampleData> sample;
- if ((kind == InstrumentKind::sampler || kind == InstrumentKind::audioClip || kind == InstrumentKind::granulizer
- || kind == InstrumentKind::wavetraveller) && samples
- && ! channel.samplePath.empty())
- sample = samples (channel.samplePath);
- // pitch-preserving stretch: the sample comes already stretched to the current tempo
- if (sample != nullptr && kind != InstrumentKind::granulizer && kind != InstrumentKind::wavetraveller && channel.sampler.keepPitch && channel.sampler.stretchBeats > 0.0f
- && factories.stretchedSamples
- && sample->frames() > 0)
- {
- const auto target = channel.sampler.stretchBeats * 60.0 / std::max (1.0, bpm);
- const auto ratio = target / ((double) sample->frames() / sample->sampleRate);
- if (auto stretched = factories.stretchedSamples (channel.samplePath, ratio, channel.sampler.stretchMethod))
- sample = std::move (stretched);
- }
- // FL's Sampler › Precomputed effects (REA_rodada167h §2): the played region, processed once
- // Audio clips too (round 167k, FL render r167k/fl/clip/p_*: an audio clip channel's Normalize, Reverse polarity,
- // Fade in / out play in FL); a clip plays the whole file, so the region stays whole there
- if (sample != nullptr && kind == InstrumentKind::sampler && precompute::needed (channel.sampler))
- {
- if (auto processed = precompute::apply (*sample, channel.sampler))
- sample = std::move (processed);
- }
- else if (sample != nullptr && kind == InstrumentKind::audioClip && channel.sampler.precompute.any())
- {
- auto whole = channel.sampler;
- whole.regionStart = 0.0f;
- whole.regionEnd = 1.0f;
- if (auto processed = precompute::apply (*sample, whole))
- sample = std::move (processed);
- }
- const loadtrace::Scope timing ("rack_instruments");
- std::shared_ptr<Instrument> instrument = makeInstrument (kind, std::move (sample));
- instrument->prepare (sampleRate);
- instrument->configure (channel, bpm);
- instrument->settleNew();
- if ((kind == InstrumentKind::sampler || kind == InstrumentKind::audioClip) && channel.sampler.keepPitch)
- {
- if (deferKeyShift != nullptr)
- deferKeyShift->push_back (instrument.get());
- else
- instrument->prepareKeyShift (sampleRate); // transpositions keep the length (rounds 162, 163)
- }
- return instrument;
- }
- // A slot's effect, built new (UI thread)
- std::shared_ptr<Effect> buildEffect (const EffectSlot& slot, double sampleRate, const RackState::Factories& factories)
- {
- const auto kind = slot.kind;
- std::shared_ptr<Effect> effect;
- if (kind == EffectKind::plugin)
- {
- if (factories.pluginEffects && ! slot.plugin.id.empty())
- effect = factories.pluginEffects (slot.plugin, sampleRate);
- if (effect == nullptr)
- effect = std::make_shared<BypassEffect>();
- }
- else if ((kind == EffectKind::newtone || kind == EffectKind::newtime))
- {
- // Newtone / Newtime: their source sample (loaded off the audio thread), REA_rodada73
- // (Newtone: its note edits rendered by LYNX's engine, REA_rodada95)
- effect = newtonefx::make (factories.samples && ! slot.samplePath.empty() ? factories.samples (slot.samplePath) : nullptr,
- slot.state);
- effect->prepare (sampleRate);
- }
- else if (kind == EffectKind::scratcher)
- {
- // Fruity Scratcher: its turntable's sample, loaded off the audio thread (REA_rodada165g-flscratch165)
- effect = scratcher::make (factories.samples && ! slot.samplePath.empty() ? factories.samples (slot.samplePath) : nullptr);
- effect->prepare (sampleRate);
- // its DVS position table (REA_rodada167c §1), built here so the audio thread finds it ready
- dvs::warm (scratcher::timecodeOf (slot.params[scratcher::timecodeType]), true);
- }
- else
- {
- const loadtrace::Scope timing ("rack_builtin_fx"); // (round 112: what the open spends on LYNX's own effects)
- effect = makeEffect (slot);
- effect->prepare (sampleRate);
- }
- return effect;
- }
- // What a Patcher builds its nodes with: the same builders as the rack's channels and slots
- patcher::Factories patcherFactories (double sampleRate, double bpm, const RackState::Factories& factories)
- {
- patcher::Factories f;
- f.effect = [sampleRate, bpm, factories] (const EffectSlot& slot) -> std::shared_ptr<Effect> {
- if (slot.kind == EffectKind::patcher)
- {
- std::shared_ptr<Effect> e = patcher::makeEffect (slot, sampleRate, patcherFactories (sampleRate, bpm, factories));
- e->prepare (sampleRate);
- return e;
- }
- return buildEffect (slot, sampleRate, factories);
- };
- f.instrument = [sampleRate, bpm, factories] (const ChannelDef& channel) -> std::shared_ptr<Instrument> {
- if (channel.instrument == InstrumentKind::patcher)
- {
- std::shared_ptr<Instrument> i = patcher::makeInstrument (channel, sampleRate, patcherFactories (sampleRate, bpm, factories));
- i->configure (channel, bpm);
- return i;
- }
- return buildInstrument (channel, sampleRate, bpm, factories);
- };
- return f;
- }
- // an audio clip channel that resamples but has a clip of its own pitch (FL 2026's keys 7/8): it needs the shifters
- bool clipShiftWanted (const Song& song, std::size_t channel)
- {
- if (channel >= song.channels.size() || song.channels[channel].instrument != InstrumentKind::audioClip
- || song.channels[channel].sampler.keepPitch)
- return false;
- for (const auto& c : song.playlist)
- if (c.kind == ClipKind::audio && c.channel == (int) channel && c.pitch != 0.0f)
- return true;
- return false;
- }
- // The keep-pitch samplers' shifters, in parallel: each instrument only touches its own pool, and each shifter's
- // set-up is deterministic (a fixed seed and a silent priming run), so the result is the same as one by one
- void prepareKeyShifts (const std::vector<Instrument*>& list, double sampleRate)
- {
- if (list.empty())
- return;
- const loadtrace::Scope timing ("rack_keyshift");
- const auto threads = (std::size_t) std::clamp ((int) std::thread::hardware_concurrency() - 1, 1, 6);
- const char* off = std::getenv ("LYNX_PARALLEL_KEYSHIFT"); // "0": one by one (A/B and the null test)
- if (list.size() < 2 || threads < 2 || (off != nullptr && off[0] == '0'))
- {
- for (auto* i : list)
- i->prepareKeyShift (sampleRate);
- return;
- }
- std::atomic<std::size_t> next { 0 };
- const auto mode = FloatMode::current(); // the helpers round and flush like this thread
- auto work = [&] {
- mode.applyHere();
- for (auto k = next.fetch_add (1); k < list.size(); k = next.fetch_add (1))
- list[k]->prepareKeyShift (sampleRate);
- };
- std::vector<std::thread> helpers;
- for (std::size_t t = 1; t < std::min (threads, list.size()); ++t)
- {
- try
- {
- helpers.emplace_back (work);
- }
- catch (...) // no thread: the others (and this one) do the rest
- {
- break;
- }
- }
- work();
- for (auto& h : helpers)
- h.join();
- }
- } // namespace
- std::unique_ptr<const RackState> RackState::build (Song song,
- const RackState* previous,
- double sampleRate,
- const Factories& factories)
- {
- const auto& samples = factories.samples;
- auto state = std::make_unique<RackState>();
- state->instruments.reserve (song.channels.size());
- std::vector<Instrument*> keyShiftLater; // new instruments only: none is visible to the audio thread yet
- for (std::size_t i = 0; i < song.channels.size(); ++i)
- {
- const auto& channel = song.channels[i];
- const auto kind = channel.instrument;
- if (previous != nullptr && i < previous->song.channels.size() && i < previous->instruments.size()
- && previous->song.channels[i].instrument == kind && kind != InstrumentKind::patcher
- && ((kind != InstrumentKind::granulizer && kind != InstrumentKind::wavetraveller)
- || previous->song.channels[i].samplePath == channel.samplePath)
- && (kind != InstrumentKind::soundfont
- || (previous->song.channels[i].samplePath == channel.samplePath && previous->sampleRate == sampleRate))
- && (kind != InstrumentKind::directwave
- || (previous->song.channels[i].zones == channel.zones && previous->song.channels[i].modelParams == channel.modelParams
- && previous->song.channels[i].pluginState == channel.pluginState && previous->sampleRate == sampleRate))
- && ((kind != InstrumentKind::sampler && kind != InstrumentKind::audioClip && kind != InstrumentKind::flkeys)
- || (previous->song.channels[i].samplePath == channel.samplePath && previous->song.channels[i].zones == channel.zones
- && previous->song.channels[i].modelParams == channel.modelParams
- && previous->song.channels[i].sampler.keepPitch == channel.sampler.keepPitch
- && clipShiftWanted (previous->song, i) == clipShiftWanted (song, i)
- // FL's precomputed effects (round 167h): the sample is processed when the channel is built
- && previous->song.channels[i].sampler.precompute == channel.sampler.precompute
- && previous->song.channels[i].sampler.useLoopPoints == channel.sampler.useLoopPoints
- && (! precompute::needed (channel.sampler)
- || (previous->song.channels[i].sampler.regionStart == channel.sampler.regionStart
- && previous->song.channels[i].sampler.regionEnd == channel.sampler.regionEnd
- && previous->song.channels[i].sampler.reverse == channel.sampler.reverse
- && precompute::needed (previous->song.channels[i].sampler)))
- && (! channel.sampler.keepPitch
- || (previous->song.channels[i].sampler.stretchBeats == channel.sampler.stretchBeats
- && previous->song.channels[i].sampler.stretchMethod == channel.sampler.stretchMethod
- && previous->song.bpm == song.bpm))))
- && (kind != InstrumentKind::plugin
- || (previous->song.channels[i].plugin.id == channel.plugin.id && previous->sampleRate == sampleRate
- && samePluginProcessing (previous->song.channels[i].plugin, channel.plugin))))
- {
- state->instruments.push_back (previous->instruments[i]);
- state->instruments.back()->configure (channel, song.bpm);
- continue;
- }
- // FL's Patcher as a generator (REA_rodada141): same structure keeps the running graph (it takes the values)
- if (kind == InstrumentKind::patcher)
- {
- const auto* old = previous != nullptr && i < previous->instruments.size() ? previous->instruments[i].get() : nullptr;
- if (old != nullptr && previous->song.channels[i].instrument == kind && previous->sampleRate == sampleRate
- && patcher::sameStructure (previous->song.channels[i].patcher, channel.patcher))
- {
- state->instruments.push_back (previous->instruments[i]);
- state->instruments.back()->configure (channel, song.bpm);
- continue;
- }
- state->instruments.push_back (patcher::makeInstrument (channel, sampleRate, patcherFactories (sampleRate, song.bpm, factories), old));
- state->instruments.back()->configure (channel, song.bpm);
- continue;
- }
- state->instruments.push_back (buildInstrument (channel, sampleRate, song.bpm, factories, &keyShiftLater));
- // FL 2026's clip pitch (round 167k): a channel with a pitched clip gets its pitch shifters even when it resamples
- if (clipShiftWanted (song, i) && state->instruments.back() != nullptr)
- {
- state->instruments.back()->setClipShift (true);
- keyShiftLater.push_back (state->instruments.back().get());
- }
- }
- // Options › Project › "Fast declick for cut groups" (REA round 167h §3)
- for (auto& instrument : state->instruments)
- if (instrument != nullptr)
- {
- instrument->setFastDeclick (song.fastDeclick);
- instrument->setPanTriangular (song.panTriangular);
- }
- prepareKeyShifts (keyShiftLater, sampleRate);
- state->effects.resize (song.mixer.size());
- for (std::size_t t = 0; t < song.mixer.size(); ++t)
- {
- const auto& slots = song.mixer[t].effects;
- for (std::size_t i = 0; i < slots.size() && i < (std::size_t) maxEffectSlots; ++i)
- {
- const auto kind = slots[i].kind;
- if (previous != nullptr && t < previous->song.mixer.size() && t < previous->effects.size()
- && i < previous->song.mixer[t].effects.size() && i < previous->effects[t].size()
- && previous->song.mixer[t].effects[i].kind == kind && kind != EffectKind::patcher
- && (kind != EffectKind::parallel || sameContainer (previous->song.mixer[t].effects[i], slots[i])) // children are built in
- && previous->song.mixer[t].effects[i].samplePath == slots[i].samplePath
- // round 144: Facet EQ's buffers are sized by its linear-phase resolution: a new one, a new instance
- && (kind != EffectKind::facetEq
- || facet::resolution (previous->song.mixer[t].effects[i].params[(std::size_t) facet::resolutionParam])
- == facet::resolution (slots[i].params[(std::size_t) facet::resolutionParam]))
- && (kind != EffectKind::plugin
- || (previous->song.mixer[t].effects[i].plugin.id == slots[i].plugin.id && previous->sampleRate == sampleRate
- && samePluginProcessing (previous->song.mixer[t].effects[i].plugin, slots[i].plugin)))
- && (previous->song.mixer[t].effects[i].state == slots[i].state
- || (previous->sampleRate == sampleRate && previous->effects[t][i] != nullptr && previous->effects[t][i]->acceptState (slots[i].state))))
- {
- state->effects[t].push_back (previous->effects[t][i]);
- continue;
- }
- // FL's Patcher (REA_rodada141): same structure keeps the running graph (it takes the values)
- if (kind == EffectKind::patcher)
- {
- const auto* old = previous != nullptr && t < previous->effects.size() && i < previous->effects[t].size()
- && i < previous->song.mixer[t].effects.size() && previous->song.mixer[t].effects[i].kind == kind
- ? previous->effects[t][i].get()
- : nullptr;
- if (old != nullptr && previous->sampleRate == sampleRate
- && patcher::sameStructure (previous->song.mixer[t].effects[i].patcher, slots[i].patcher)
- && patcher::acceptValues (previous->effects[t][i].get(), slots[i]))
- {
- state->effects[t].push_back (previous->effects[t][i]);
- continue;
- }
- std::shared_ptr<Effect> effect = patcher::makeEffect (slots[i], sampleRate, patcherFactories (sampleRate, song.bpm, factories), old);
- effect->prepare (sampleRate);
- state->effects[t].push_back (std::move (effect));
- continue;
- }
- state->effects[t].push_back (buildEffect (slots[i], sampleRate, factories));
- }
- }
- // FL's Channel Rack looping (round 165f-rack): the looping channels' repeats are written out as notes here, so
- // playback, the Playlist's pattern clips and export all play them without any cost on the audio thread
- channelloops::expandSong (song);
- state->song = std::move (song);
- const auto tailStarted = std::chrono::steady_clock::now();
- // scheduling: notes sorted by start (order among notes on the same tick is preserved)
- for (auto& p : state->song.patterns)
- std::stable_sort (p.notes.begin(), p.notes.end(), [] (const Note& a, const Note& b) { return a.start < b.start; });
- state->schedule = makeScheduleIndex (state->song);
- state->performanceZone = perf::zoneClips (state->song);
- state->sampleRate = sampleRate;
- state->buildMidiRoutes();
- state->buildAutomationTables();
- {
- const loadtrace::Scope timing ("pdc");
- state->buildPdc (previous);
- }
- state->buildMixerGraph();
- const auto& channels = state->song.channels;
- state->cutTargets.assign (channels.size(), {});
- state->arpeggiators.assign (channels.size(), nullptr);
- for (std::size_t c = 0; c < channels.size(); ++c)
- if (channels[c].arp.enabled())
- state->arpeggiators[c] = previous != nullptr && c < previous->arpeggiators.size() && previous->arpeggiators[c] != nullptr
- ? previous->arpeggiators[c]
- : std::make_shared<Arpeggiator>();
- state->echoes.assign (channels.size(), nullptr);
- for (std::size_t c = 0; c < channels.size(); ++c)
- if (channels[c].misc.echo.enabled())
- state->echoes[c] = previous != nullptr && c < previous->echoes.size() && previous->echoes[c] != nullptr
- ? previous->echoes[c]
- : std::make_shared<channelmisc::NoteEcho>();
- // (FL's Kepler keeps sounding when its cut group cuts it: FL's renders with and without "cut itself" are the same
- // and both null against the engine fed every note to its end, REA_rodada165g-flbeta165k §3)
- for (std::size_t c = 0; c < channels.size(); ++c)
- if (channels[c].cutGroup > 0)
- for (std::size_t d = 0; d < channels.size(); ++d)
- if (channels[d].cutBy == channels[c].cutGroup && channels[d].instrument != InstrumentKind::kepler)
- {
- state->cutTargets[c].push_back ((int) d);
- state->hasCutGroups = true;
- }
- state->hasLayers = std::any_of (channels.begin(), channels.end(), [] (const ChannelDef& c) {
- return c.instrument == InstrumentKind::layer && ! c.layer.children.empty();
- });
- const auto audible = state->song.soloAudible();
- state->mixerAudible.assign (audible.begin(), audible.end());
- loadtrace::add ("rack_schedule_graph", loadtrace::msSince (tailStarted));
- return state;
- }
- void RackState::buildMixerGraph()
- {
- mixerOrder = song.mixerOrder();
- const auto audible = song.soloAudible();
- mixerAudible.assign (audible.begin(), audible.end());
- const auto tracks = std::max<int> (1, (int) song.mixer.size());
- mixerInputs.assign ((std::size_t) tracks, {});
- sendTaps.clear();
- slotTap.assign ((std::size_t) tracks, {});
- std::vector<int> depth ((std::size_t) tracks, 0);
- // who listens to each track via sidechain (the source goes in an earlier layer)
- std::vector<std::vector<int>> sidechainListeners ((std::size_t) tracks);
- for (int t = 1; t < tracks; ++t)
- for (const auto& fx : song.mixer[(std::size_t) t].effects)
- for (const auto source : { fx.sidechain, fx.sidechain2 })
- if (source > 0 && source < tracks && source != t)
- sidechainListeners[(std::size_t) source].push_back (t);
- for (auto t : mixerOrder) // sources before destinations: the depth is already computed
- {
- const auto& track = song.mixer[(std::size_t) t];
- for (const auto listener : sidechainListeners[(std::size_t) t])
- depth[(std::size_t) listener] = std::max (depth[(std::size_t) listener], depth[(std::size_t) t] + 1);
- // a route whose level knob is automated (issue #12)
- auto routeAuto = [this, t] (int to) {
- for (const auto& r : routeAutos)
- if (r.from == t && r.to == to)
- return r.slot;
- return -1;
- };
- if (track.toMaster)
- mixerInputs[0].push_back ({ t, 0, track.masterLevel, -1, routeAuto (0) });
- for (std::size_t k = 0; k < track.sends.size(); ++k)
- {
- const auto& send = track.sends[k];
- const auto automation = send.target > 0 ? routeAuto (send.target) : -1;
- // (a send at 0 is left out, unless an automation brings it up)
- if (send.target > 0 && send.target < tracks && send.target != t && (send.level > 0.0f || automation >= 0))
- {
- mixerInputs[(std::size_t) send.target].push_back ({ t, (int) k + 1, send.level, -1, automation });
- depth[(std::size_t) send.target] = std::max (depth[(std::size_t) send.target], depth[(std::size_t) t] + 1);
- }
- }
- // Fruity Send slots: their own signal, delay lines after the sends' (PdcPlan::sendDelay)
- const auto taps = fruitySendsOf (song, t);
- for (std::size_t j = 0; j < taps.size(); ++j)
- {
- const auto slot = taps[j].slot, target = taps[j].target;
- if ((int) sendTaps.size() >= maxSendTaps)
- break;
- if ((std::size_t) t >= slotTap.size())
- slotTap.resize ((std::size_t) t + 1);
- // (two entries per slot: its outputs 0 and 1, the Transmitter's transient / sustain sends)
- const auto key = (std::size_t) slot * maxSlotSends + (std::size_t) taps[j].output;
- if (key >= slotTap[(std::size_t) t].size())
- slotTap[(std::size_t) t].resize (key + 1, -1);
- slotTap[(std::size_t) t][key] = (int) sendTaps.size();
- mixerInputs[(std::size_t) target].push_back ({ t, (int) (1 + track.sends.size() + j), 1.0f, (int) sendTaps.size() });
- sendTaps.push_back ({ t, slot, target });
- depth[(std::size_t) target] = std::max (depth[(std::size_t) target], depth[(std::size_t) t] + 1);
- }
- }
- mixerLayers.clear();
- for (auto t : mixerOrder)
- {
- const auto d = (std::size_t) depth[(std::size_t) t];
- if (mixerLayers.size() <= d)
- mixerLayers.resize (d + 1);
- mixerLayers[d].push_back (t);
- }
- // splitting across threads costs a few microseconds per block; only worth it with plugins or many channels
- const auto plugins = std::count_if (song.channels.begin(), song.channels.end(),
- [] (const ChannelDef& c) { return c.instrument == InstrumentKind::plugin; });
- auto pluginEffects = 0;
- for (const auto& t : song.mixer)
- pluginEffects += (int) std::count_if (t.effects.begin(), t.effects.end(), [] (const EffectSlot& e) { return e.kind == EffectKind::plugin; });
- parallel = plugins + pluginEffects >= 2 || song.channels.size() >= 24;
- // plugins whose "Allow threaded processing" is off pin their channel / insert to the audio thread
- threadedChannels.clear();
- audioThreadChannels.clear();
- for (int c = 0; c < (int) song.channels.size(); ++c)
- {
- const auto& ch = song.channels[(std::size_t) c];
- // (and a generator Patcher holding a Hardware Insert, round 165c: the device-output buses are not shared between threads)
- const auto pinned = (ch.instrument == InstrumentKind::plugin && ! ch.plugin.threaded)
- || (ch.instrument == InstrumentKind::patcher && patcher::usesHardwareIo (ch.patcher));
- (pinned ? audioThreadChannels : threadedChannels).push_back (c);
- }
- threadedLayers.assign (mixerLayers.size(), {});
- audioThreadLayers.assign (mixerLayers.size(), {});
- for (std::size_t d = 0; d < mixerLayers.size(); ++d)
- for (const auto t : mixerLayers[d])
- {
- const auto& effects = song.mixer[(std::size_t) t].effects;
- // (and the hardware inserts, round 164d: the device-output buses they write are not shared between threads)
- const auto pinned = ! song.mixer[(std::size_t) t].threaded || std::any_of (effects.begin(), effects.end(),
- [] (const EffectSlot& e) {
- return (e.kind == EffectKind::plugin && ! e.plugin.threaded) || e.kind == EffectKind::hardwareInsert
- || (e.kind == EffectKind::patcher && patcher::usesHardwareIo (e.patcher)); // (round 165c)
- });
- (pinned ? audioThreadLayers : threadedLayers)[d].push_back (t);
- }
- buildTaskGraph (depth);
- }
- void RackState::buildTaskGraph (const std::vector<int>& depth)
- {
- const auto tracks = std::max<int> (1, (int) song.mixer.size());
- graphChannels = std::min<int> ((int) song.channels.size(), RackRenderer::maxChannels);
- trackChannels.assign ((std::size_t) tracks, {});
- for (int c = 0; c < graphChannels; ++c)
- trackChannels[(std::size_t) std::clamp (song.channels[(std::size_t) c].mixerTrack, 0, tracks - 1)].push_back (c);
- graphOk = false;
- graphIndegree.clear();
- graphNextStart.clear();
- graphNext.clear();
- graphRoots.clear();
- const auto pinned = ! audioThreadChannels.empty()
- || std::any_of (audioThreadLayers.begin(), audioThreadLayers.end(), [] (const std::vector<int>& l) { return ! l.empty(); });
- if (pinned)
- return;
- const auto tasks = graphChannels + tracks - 1;
- const auto trackTask = [this] (int t) { return graphChannels + t - 1; };
- std::vector<std::vector<int>> next ((std::size_t) tasks);
- auto ok = true;
- auto edge = [&] (int from, int to) {
- // the layers run a source in an earlier layer than its destination; a cycle breaks that (the layered path
- // then reads the source before it ran): keep the layers for such a mixer
- if (depth[(std::size_t) to] <= depth[(std::size_t) from])
- ok = false;
- next[(std::size_t) trackTask (from)].push_back (trackTask (to));
- };
- for (int c = 0; c < graphChannels; ++c)
- if (const auto t = std::clamp (song.channels[(std::size_t) c].mixerTrack, 0, tracks - 1); t > 0)
- next[(std::size_t) c].push_back (trackTask (t));
- for (int t = 1; t < tracks; ++t)
- {
- for (const auto& in : mixerInputs[(std::size_t) t])
- if (in.source > 0 && in.source < tracks && in.source != t)
- edge (in.source, t);
- for (const auto& fx : song.mixer[(std::size_t) t].effects)
- for (const auto source : { fx.sidechain, fx.sidechain2 })
- if (source > 0 && source < tracks && source != t)
- edge (source, t);
- }
- if (! ok)
- return;
- graphIndegree.assign ((std::size_t) tasks, 0);
- graphNextStart.assign ((std::size_t) tasks + 1, 0);
- for (int i = 0; i < tasks; ++i)
- {
- auto& n = next[(std::size_t) i];
- std::sort (n.begin(), n.end());
- n.erase (std::unique (n.begin(), n.end()), n.end());
- graphNextStart[(std::size_t) i + 1] = graphNextStart[(std::size_t) i] + (int) n.size();
- for (const auto j : n)
- {
- graphNext.push_back (j);
- ++graphIndegree[(std::size_t) j];
- }
- }
- // the channels first (they are most of the work and every insert waits for some of them), then the inserts
- // nothing feeds
- for (int i = 0; i < tasks; ++i)
- if (graphIndegree[(std::size_t) i] == 0)
- graphRoots.push_back (i);
- graphOk = true;
- }
- std::vector<int> RackState::currentLatencies() const
- {
- std::vector<int> out;
- // a hosted plugin's latency set by hand in its wrapper settings (round 149) replaces what it reports
- auto manual = [this] (const PluginRef& ref, int reported) {
- return ref.latencyOverrideMs >= 0.0f ? (int) std::lround (ref.latencyOverrideMs * 0.001 * sampleRate) : reported;
- };
- for (std::size_t c = 0; c < instruments.size(); ++c)
- {
- const auto reported = instruments[c]->latencySamples();
- out.push_back (c < song.channels.size() && song.channels[c].instrument == InstrumentKind::plugin ? manual (song.channels[c].plugin, reported) : reported);
- }
- for (std::size_t t = 0; t < effects.size(); ++t)
- for (std::size_t s = 0; s < effects[t].size(); ++s)
- {
- const auto reported = effects[t][s]->latencySamples();
- const auto* slot = t < song.mixer.size() && s < song.mixer[t].effects.size() ? &song.mixer[t].effects[s] : nullptr;
- out.push_back (slot != nullptr && slot->kind == EffectKind::plugin ? manual (slot->plugin, reported) : reported);
- }
- return out;
- }
- void RackState::buildPdc (const RackState* previous)
- {
- latencies = currentLatencies();
- std::vector<int> channelLatency (latencies.begin(), latencies.begin() + (std::ptrdiff_t) instruments.size());
- std::vector<std::vector<int>> slotLatency (effects.size());
- auto next = latencies.begin() + (std::ptrdiff_t) instruments.size();
- for (std::size_t t = 0; t < effects.size(); ++t)
- for (std::size_t s = 0; s < effects[t].size(); ++s)
- slotLatency[t].push_back (*next++);
- pdc = computePdc (song, channelLatency, slotLatency, sampleRate);
- // reuses lines of the same length: editing the song must not click the audio
- auto line = [] (int length, const std::shared_ptr<DelayLine>* old) -> std::shared_ptr<DelayLine> {
- if (length <= 0)
- return nullptr;
- if (old != nullptr && *old != nullptr && (*old)->length() == length)
- return *old;
- return std::make_shared<DelayLine> (length);
- };
- channelDelays.resize (pdc.channelDelay.size());
- for (std::size_t c = 0; c < pdc.channelDelay.size(); ++c)
- channelDelays[c] = line (pdc.channelDelay[c],
- previous != nullptr && c < previous->channelDelays.size() ? &previous->channelDelays[c] : nullptr);
- sendDelays.resize (pdc.sendDelay.size());
- for (std::size_t t = 0; t < pdc.sendDelay.size(); ++t)
- {
- sendDelays[t].resize (pdc.sendDelay[t].size());
- for (std::size_t k = 0; k < pdc.sendDelay[t].size(); ++k)
- {
- const std::shared_ptr<DelayLine>* old = nullptr;
- if (previous != nullptr && t < previous->sendDelays.size() && k < previous->sendDelays[t].size())
- old = &previous->sendDelays[t][k];
- sendDelays[t][k] = line (pdc.sendDelay[t][k], old);
- }
- }
- // inserts with a hardware output target: aligned with the Master's output (which waits for the longest path)
- hwRoutes.clear();
- for (std::size_t t = 1; t < song.mixer.size() && t < (std::size_t) maxMixerTracks; ++t)
- {
- const auto& track = song.mixer[t];
- if (track.outputChannel < 0 || track.outputChannel >= HardwareOutput::maxChannels)
- continue;
- // FL (FUN_010ffca0): only with "Compensate for master latency" on and the track not bypassed
- const auto compensated = track.compensateMasterLatency && track.latencyBypass != LatencyBypass::always;
- const auto length = compensated && t < pdc.outLat.size() ? std::max (0, pdc.total - pdc.outLat[t]) : 0;
- const std::shared_ptr<DelayLine>* old = nullptr;
- if (previous != nullptr)
- for (const auto& r : previous->hwRoutes)
- if (r.track == (int) t)
- old = &r.delay;
- hwRoutes.push_back ({ (int) t, track.outputChannel, track.outputMode, line (length, old) });
- }
- bypassWhenIdle.assign (song.mixer.size(), 0);
- anyBypassWhenIdle = false;
- for (std::size_t t = 1; t < song.mixer.size(); ++t)
- if (song.mixer[t].latencyBypass == LatencyBypass::whenRecordingOrStopped)
- anyBypassWhenIdle = bypassWhenIdle[t] = 1;
- }
- void RackState::buildMidiRoutes()
- {
- const auto count = song.channels.size();
- midiTargets.assign (count, {});
- midiSources.assign (count, {});
- midiOutChannel.assign (count, -1);
- vfxChannels.clear();
- portEventChannels.clear();
- dashboardChannels.clear();
- mapNoteColour.assign (count, 0);
- vfxPort.assign (count, -1);
- hasMidiRoutes = false;
- midiOutTargets.assign (count, {});
- hasMidiOutputs = false;
- for (std::size_t c = 0; c < count; ++c)
- if (song.channels[c].instrument == InstrumentKind::plugin && song.channels[c].plugin.midiOutPort >= 0)
- for (std::size_t t = 0; t < count; ++t)
- if (t != c && song.channels[t].instrument == InstrumentKind::plugin
- && song.channels[t].plugin.midiInPort == song.channels[c].plugin.midiOutPort)
- {
- midiOutTargets[c].push_back ((int) t);
- hasMidiOutputs = true;
- }
- midiRoutesOut.assign (count, {});
- midiRoutesIn.assign (count, {});
- effectMidiRoutes.assign (count, {});
- hasEffectMidiRoutes = false;
- // the Patchers that take MIDI ports through their "From LYNX" pins (round 162)
- std::vector<std::vector<int>> channelPorts (count);
- for (std::size_t t = 0; t < count; ++t)
- if (song.channels[t].instrument == InstrumentKind::patcher)
- channelPorts[t] = patcher::midiInputPorts (song.channels[t].patcher);
- struct SlotPorts
- {
- int track, slot;
- std::vector<int> ports;
- };
- std::vector<SlotPorts> slotPorts;
- for (std::size_t t = 0; t < song.mixer.size(); ++t)
- for (std::size_t k = 0; k < song.mixer[t].effects.size(); ++k)
- if (song.mixer[t].effects[k].kind == EffectKind::patcher)
- if (auto ports = patcher::midiInputPorts (song.mixer[t].effects[k].patcher); ! ports.empty())
- slotPorts.push_back ({ (int) t, (int) k, std::move (ports) });
- // source c sends on `port` with MIDI channel `midiChannel`: the plugins whose input port it is, the Patchers that
- // take that port
- // (a Patcher that takes the port may pass it on to "To LYNX" MIDI ports: those are sent on too, a few levels deep)
- bool portOnly = false; // (the routes sent next carry only what goes out on the port: a Patcher's VFX outputs)
- std::function<void (std::size_t, int, int, int)> send = [&] (std::size_t c, int port, int midiChannel, int depth) {
- auto passOn = [&] (const PatcherDoc& doc) {
- if (depth < 4)
- for (const auto& r : patcher::midiOutputsOf (doc, patchMidiPortPin (port)))
- if (r.port != port)
- send (c, r.port, r.midiChannel >= 0 ? r.midiChannel : midiChannel, depth + 1);
- };
- if (midiOutChannel[c] < 0)
- midiOutChannel[c] = midiChannel;
- for (std::size_t t = 0; t < count; ++t)
- {
- if (t == c)
- continue;
- const auto plugin = song.channels[t].instrument == InstrumentKind::plugin && song.channels[t].plugin.midiInPort == port;
- const auto patch = std::find (channelPorts[t].begin(), channelPorts[t].end(), port) != channelPorts[t].end();
- if (! plugin && ! patch)
- continue;
- if (std::any_of (midiRoutesOut[c].begin(), midiRoutesOut[c].end(), [&] (const MidiRoute& r) {
- return r.other == (int) t && r.port == port && r.midiChannel == midiChannel;
- }))
- continue;
- midiRoutesOut[c].push_back ({ (int) t, port, midiChannel, portOnly });
- midiRoutesIn[t].push_back ({ (int) c, port, midiChannel, portOnly });
- if (std::find (midiSources[t].begin(), midiSources[t].end(), (int) c) == midiSources[t].end())
- midiSources[t].push_back ((int) c);
- if (plugin && ! portOnly && std::find (midiTargets[c].begin(), midiTargets[c].end(), (int) t) == midiTargets[c].end())
- midiTargets[c].push_back ((int) t); // (controls and program changes: the plugins)
- hasMidiRoutes = true;
- if (patch)
- passOn (song.channels[t].patcher);
- }
- for (const auto& s : slotPorts)
- if (std::find (s.ports.begin(), s.ports.end(), port) != s.ports.end()
- && std::none_of (effectMidiRoutes[c].begin(), effectMidiRoutes[c].end(), [&] (const EffectMidiRoute& r) {
- return r.track == s.track && r.slot == s.slot && r.port == port && r.midiChannel == midiChannel;
- }))
- {
- effectMidiRoutes[c].push_back ({ s.track, s.slot, port, midiChannel, portOnly });
- hasEffectMidiRoutes = true;
- passOn (song.mixer[(std::size_t) s.track].effects[(std::size_t) s.slot].patcher);
- }
- };
- for (std::size_t c = 0; c < count; ++c)
- {
- // a generator Patcher's "MIDI port N" outputs (REA_rodada141): its notes reach the plugins on those ports, as a
- // MIDI Out on MIDI channel 1 would send them (or on the channel of a MIDI Out node on the way, round 162)
- if (song.channels[c].instrument == InstrumentKind::patcher)
- {
- for (const auto& r : patcher::midiOutputsOf (song.channels[c].patcher, 0))
- send (c, r.port, std::max (0, r.midiChannel), 0);
- // its VFX nodes' outputs cabled to "To LYNX" MIDI ports (round 165c): what they play goes out there
- if (const auto ports = patcher::vfxHostPorts (song.channels[c].patcher); ! ports.empty())
- {
- portEventChannels.push_back ((int) c);
- portOnly = true;
- for (const auto port : ports)
- send (c, port, 0, 0);
- portOnly = false;
- }
- continue;
- }
- if (song.channels[c].instrument == InstrumentKind::vfx) // a VFX channel: output k on port + k (round 164b)
- {
- if (const auto vs = vfx::readState (song.channels[c].pluginState))
- {
- vfxChannels.push_back ((int) c);
- vfxPort[c] = vs->port;
- for (int k = 0; k < vfx::outputCount (vs->kind) && vs->port + k < patchMidiPorts; ++k)
- send (c, vs->port + k, 0, 0);
- }
- continue;
- }
- if (song.channels[c].instrument == InstrumentKind::dashboard) // its notes and controls' MIDI (round 165)
- {
- const auto settings = surface::dashboardMidi (song.channels[c].pluginState);
- midiOutChannel[c] = settings.channel;
- dashboardChannels.push_back ((int) c);
- send (c, settings.port, settings.channel, 0);
- continue;
- }
- if (song.channels[c].instrument != InstrumentKind::midiOut)
- continue;
- // its knobs' MIDI (round 167i) goes out like a Dashboard's
- const auto settings = midiout::stateOf (song.channels[c].pluginState);
- midiOutChannel[c] = settings.channel;
- mapNoteColour[c] = settings.mapNoteColour ? 1 : 0;
- dashboardChannels.push_back ((int) c);
- send (c, settings.port, settings.channel, 0);
- }
- }
- void RackState::buildAutomationTables()
- {
- const auto& automations = song.automations;
- const auto count = std::min<int> ((int) automations.size(), maxAutomations);
- automationClips.assign ((std::size_t) count, {});
- zoneAutomations.assign ((std::size_t) count, 0);
- const auto zoneEnd = song.performanceZoneEnd();
- for (int i = 0; i < (int) song.playlist.size(); ++i)
- {
- const auto& c = song.playlist[(std::size_t) i];
- if (c.kind == ClipKind::automation && c.automation >= 0 && c.automation < count && c.length > 0 && ! c.muted && ! song.isTrackMuted (c.track))
- {
- if (c.start < zoneEnd) // Performance mode: plays only when launched
- zoneAutomations[(std::size_t) c.automation] = 1;
- else
- automationClips[(std::size_t) c.automation].push_back (i);
- }
- }
- for (auto& clips : automationClips)
- std::sort (clips.begin(), clips.end(),
- [this] (int a, int b) { return song.playlist[(std::size_t) a].start < song.playlist[(std::size_t) b].start; });
- liveAutomations.clear();
- for (int a = 0; a < count; ++a)
- if (automations[(std::size_t) a].isLive())
- liveAutomations.push_back (a);
- channelVolumeAuto.assign (song.channels.size(), -1);
- channelPanAuto.assign (song.channels.size(), -1);
- mixerVolumeAuto.assign (song.mixer.size(), -1);
- mixerPanAuto.assign (song.mixer.size(), -1);
- effectParamAuto.assign (song.mixer.size(), {});
- effectMixAuto.assign (song.mixer.size(), {});
- for (std::size_t t = 0; t < song.mixer.size(); ++t)
- {
- std::array<int, maxEffectParams> none;
- none.fill (-1);
- effectParamAuto[t].assign (song.mixer[t].effects.size(), none);
- effectMixAuto[t].assign (song.mixer[t].effects.size(), -1);
- }
- pluginParams.clear();
- routeAutos.clear();
- hasAutomation = false;
- tempoAutomation = -1;
- // binds automation slot `a` (an automation, or a pattern event lane after them) to its target
- const auto bind = [this] (int a, const AutomationTarget& t) {
- const auto channelOk = t.index >= 0 && t.index < (int) song.channels.size();
- const auto trackOk = t.index >= 0 && t.index < (int) song.mixer.size();
- auto used = true;
- switch (t.kind)
- {
- case AutomationTargetKind::channelVolume: if (channelOk) channelVolumeAuto[(std::size_t) t.index] = a; else used = false; break;
- case AutomationTargetKind::channelPan: if (channelOk) channelPanAuto[(std::size_t) t.index] = a; else used = false; break;
- case AutomationTargetKind::mixerVolume: if (trackOk) mixerVolumeAuto[(std::size_t) t.index] = a; else used = false; break;
- case AutomationTargetKind::mixerPan: if (trackOk) mixerPanAuto[(std::size_t) t.index] = a; else used = false; break;
- case AutomationTargetKind::channelParam:
- // VST plugins and the exact engines that take FL's parameter automation (FL Slayer: REA_rodada79)
- if (channelOk && (song.channels[(std::size_t) t.index].instrument == InstrumentKind::plugin
- || song.channels[(std::size_t) t.index].instrument == InstrumentKind::patcher // (its published parameters)
- || song.channels[(std::size_t) t.index].instrument == InstrumentKind::slayer
- || song.channels[(std::size_t) t.index].instrument == InstrumentKind::soundfont // (LYNX Soundfont's knobs)
- || (song.channels[(std::size_t) t.index].instrument == InstrumentKind::midiOut
- && (midictl::isMidiParam (t.param) || (t.param >= 0 && t.param <= midiout::controlCount)))
- // a recorded / drawn pitch bend plays on any instrument (issue #12)
- || midictl::isPitchBend (t.param)
- // an Envelope Controller's global Mod X / Y (round 164f)
- || (song.channels[(std::size_t) t.index].instrument == InstrumentKind::controller
- && (t.param == envelopeControllerModXParam || t.param == envelopeControllerModYParam))
- // a Dashboard's outputs, the Voltage Controller's offsets / scales (round 165)
- || (song.channels[(std::size_t) t.index].instrument == InstrumentKind::dashboard && t.param >= 0
- && t.param < surface::maxOutputs)
- || (song.channels[(std::size_t) t.index].instrument == InstrumentKind::voltage && t.param >= 0
- && t.param < voltage::paramCount)
- // the Sampler's knobs (issue #12)
- || (song.channels[(std::size_t) t.index].instrument == InstrumentKind::sampler && t.param >= 0
- && t.param < samplerparam::count)
- // the knobs of an FL engine kept in the channel's state (round 168n)
- || modelparams::find (song.channels[(std::size_t) t.index].instrument, t.param) != nullptr))
- pluginParams.push_back ({ a, t.index, 0, 0, t.param });
- else
- used = false;
- break;
- case AutomationTargetKind::effectParam:
- {
- const auto slotOk = trackOk && t.slot >= 0 && t.slot < (int) song.mixer[(std::size_t) t.index].effects.size();
- if (! slotOk)
- used = false;
- else if (song.mixer[(std::size_t) t.index].effects[(std::size_t) t.slot].kind == EffectKind::plugin
- || song.mixer[(std::size_t) t.index].effects[(std::size_t) t.slot].kind == EffectKind::patcher) // (its published parameters)
- pluginParams.push_back ({ a, -1, t.index, t.slot, t.param });
- else if (song.mixer[(std::size_t) t.index].effects[(std::size_t) t.slot].isSubstitute())
- used = false; // the missing plugin's parameters: kept for when it is back, they don't drive the substitute
- else if (t.param >= 0 && t.param < maxEffectParams)
- effectParamAuto[(std::size_t) t.index][(std::size_t) t.slot][(std::size_t) t.param] = a;
- else
- used = false;
- break;
- }
- case AutomationTargetKind::effectMix:
- if (trackOk && t.slot >= 0 && t.slot < (int) song.mixer[(std::size_t) t.index].effects.size())
- effectMixAuto[(std::size_t) t.index][(std::size_t) t.slot] = a;
- else
- used = false;
- break;
- case AutomationTargetKind::tempo: tempoAutomation = a; break;
- case AutomationTargetKind::mixerSend:
- if (trackOk && song.routes (t.index, t.slot))
- {
- // the last one of the same route wins (as the loop's order makes it for the other targets)
- auto it = std::find_if (routeAutos.begin(), routeAutos.end(), [&t] (const RouteAuto& r) { return r.from == t.index && r.to == t.slot; });
- if (it == routeAutos.end())
- routeAutos.push_back ({ t.index, t.slot, a });
- else
- it->slot = a;
- }
- else
- used = false;
- break;
- case AutomationTargetKind::none: used = false; break;
- }
- hasAutomation = hasAutomation || used;
- };
- // the last automation of the same target wins
- for (int a = 0; a < count; ++a)
- {
- if (automationClips[(std::size_t) a].empty() && ! automations[(std::size_t) a].isLive() && zoneAutomations[(std::size_t) a] == 0)
- continue;
- bind (a, automations[(std::size_t) a].target);
- }
- // pattern events (event editor): slots after the automations; they win over automation clips of the same
- // target. The tempo is not driven by pattern events.
- eventLanes.clear();
- patternClips.assign (song.patterns.size(), {});
- for (int i = 0; i < (int) song.playlist.size(); ++i)
- {
- const auto& c = song.playlist[(std::size_t) i];
- if (c.kind == ClipKind::pattern && c.pattern >= 0 && c.pattern < (int) song.patterns.size() && c.length > 0 && ! c.muted
- && ! song.isTrackMuted (c.track))
- patternClips[(std::size_t) c.pattern].push_back (i);
- }
- for (auto& clips : patternClips)
- std::sort (clips.begin(), clips.end(),
- [this] (int a, int b) { return song.playlist[(std::size_t) a].start < song.playlist[(std::size_t) b].start; });
- auto slot = count;
- for (int p = 0; p < (int) song.patterns.size(); ++p)
- for (int l = 0; l < (int) song.patterns[(std::size_t) p].events.size(); ++l)
- {
- const auto& lane = song.patterns[(std::size_t) p].events[(std::size_t) l];
- if (lane.events.empty() || lane.target.kind == AutomationTargetKind::tempo || slot >= maxAutomations)
- continue;
- eventLanes.push_back ({ p, l, slot });
- bind (slot++, lane.target);
- }
- }
- void RackState::slotsOfTarget (const Song& song, const AutomationTarget& target, std::vector<int>& out)
- {
- out.clear();
- if (target.kind == AutomationTargetKind::none)
- return;
- const auto count = std::min<int> ((int) song.automations.size(), maxAutomations);
- for (int a = 0; a < count; ++a)
- if (song.automations[(std::size_t) a].target == target)
- out.push_back (a);
- auto slot = count;
- for (const auto& pattern : song.patterns)
- for (const auto& lane : pattern.events)
- {
- if (lane.events.empty() || lane.target.kind == AutomationTargetKind::tempo || slot >= maxAutomations)
- continue;
- if (lane.target == target)
- out.push_back (slot);
- ++slot;
- }
- }
- MixerMatrix mixerMatrix (const MixerTrack& track, float volume, float pan, bool triangular) noexcept
- {
- // FL keeps the fader as an int (12800 = 0 dB), the pan ±6400 and the separation ±64; LYNX keeps them /16000,
- // /6400 and /64. Float arithmetic as FL's, the circular kernel in x87 extended precision (flvp::panLaw).
- const auto x = std::clamp (volume, 0.0f, 1.0f) * 1.25f;
- const auto p = std::clamp (pan, -1.0f, 1.0f);
- const auto sep = std::clamp (track.stereo, -1.0f, 1.0f);
- MixerMatrix m;
- if (x == 1.0f && p == 0.0f && sep == 0.0f) // FL's fast path: exactly unity
- {
- m.ll = m.rr = track.invertPhase ? -1.0f : 1.0f;
- m.rl = m.lr = 0.0f;
- }
- else if (x == 0.0f)
- m = { 0.0f, 0.0f, 0.0f, 0.0f };
- else
- {
- constexpr float ln11 = 2.3978953f, sqrt2 = 1.4142135f, sin45 = 0.70710677f;
- auto g = x == 1.0f ? 1.0f : (float) ((std::exp ((double) (x * ln11)) - 1.0) * (double) 0.1f);
- if (track.invertPhase)
- g = -g;
- float gl, gr;
- if (triangular) // FUN_006a39c0: the louder side keeps the level
- {
- const auto v = g * sqrt2 * sin45;
- if (std::signbit (p))
- gl = v, gr = (p + 1.0f) * v;
- else
- gr = v, gl = (1.0f - p) * v;
- }
- else
- flvp::panLaw (p, g * sqrt2, gl, gr);
- float fl = 0.0f, fr = 0.0f; // the share of each side that folds into the other
- if (p != 0.0f)
- {
- if (p < 0.0f)
- fl = 1.0f - gr / g;
- else
- fr = 1.0f - gl / g;
- }
- if (sep != 0.0f)
- {
- // narrowing (5^s − 1)/4 (up to mono), widening linear (−1: side ×2)
- const auto s = sep < 0.0f ? sep : (float) (std::exp ((double) sep * std::log (5.0)) - 1.0) * 0.25f;
- fl = std::min (fl + s, 1.0f);
- fr = std::min (fr + s, 1.0f);
- }
- m.rl = gl * fl * 0.5f;
- m.ll = gl * (1.0f - fl * 0.5f);
- m.lr = gr * fr * 0.5f;
- m.rr = gr * (1.0f - fr * 0.5f);
- }
- if (track.swapChannels) // the outputs trade places
- m = { m.lr, m.rr, m.ll, m.rl };
- return m;
- }
- namespace
- {
- // The matrix over a block, each coefficient gliding toward its target by at most 0.001 per sample (FL's fader / pan
- // glide, REA_rodada85 §3; the folded terms glide the same way). `from` = the previous block's matrix (NaN: none).
- // The output may be the input. Returns the matrix reached.
- MixerMatrix applyMatrix (const float* inL, const float* inR, float* outL, float* outR, int n, MixerMatrix from,
- const MixerMatrix& to) noexcept
- {
- if (std::isnan (from.ll))
- from = to;
- constexpr float step = 0.001f;
- auto toward = [] (float g, float target) { return target > g ? std::min (target, g + step) : std::max (target, g - step); };
- auto m = from;
- int i = 0;
- for (; i < n && ! (m == to); ++i)
- {
- m = { toward (m.ll, to.ll), toward (m.rl, to.rl), toward (m.lr, to.lr), toward (m.rr, to.rr) };
- const auto a = inL[i], b = inR[i];
- outL[i] = m.ll * a + m.rl * b;
- outR[i] = m.rr * b + m.lr * a;
- }
- if (i < n)
- {
- if (m.rl == 0.0f && m.lr == 0.0f) // no fold: two gains (the common case)
- for (int k = i; k < n; ++k)
- {
- outL[k] = inL[k] * m.ll;
- outR[k] = inR[k] * m.rr;
- }
- else
- for (int k = i; k < n; ++k)
- {
- const auto a = inL[k], b = inR[k];
- outL[k] = m.ll * a + m.rl * b;
- outR[k] = m.rr * b + m.lr * a;
- }
- }
- return m;
- }
- } // namespace
- namespace
- {
- // Constant-power pan: at center both sides are 1 (0 dB).
- void panGains (float pan, float gain, float& left, float& right) noexcept
- {
- const auto angle = (pan + 1.0f) * 0.25f * 3.14159265f;
- left = gain * std::cos (angle) * 1.41421356f;
- right = gain * std::sin (angle) * 1.41421356f;
- }
- float peakOf (const float* data, int n) noexcept
- {
- float peak = 0.0f;
- for (int i = 0; i < n; ++i)
- {
- const auto a = std::abs (data[i]);
- peak = a > peak ? a : peak; // form the compiler vectorizes
- }
- return peak;
- }
- // No NaN / inf in the block? (x · 0 is 0 for every finite x: one vectorized pass)
- bool finiteBlock (const float* l, const float* r, int n) noexcept
- {
- float acc = 0.0f;
- for (int k = 0; k < n; ++k)
- acc += l[k] * 0.0f + r[k] * 0.0f;
- return acc == 0.0f;
- }
- std::atomic<std::uint64_t> nonFiniteBlocks { 0 };
- // Any sample above `level`? Stops at the first: with sound (the common case) it costs almost nothing.
- bool anyAbove (const float* l, const float* r, int n, float level) noexcept
- {
- // in blocks of 32: the inner loop vectorizes; silent chunks (the ones that scan everything) stay cheap
- int i = 0;
- for (; i + 32 <= n; i += 32)
- {
- float m = 0.0f;
- for (int k = i; k < i + 32; ++k)
- {
- const auto a = std::abs (l[k]), b = std::abs (r[k]);
- m = a > m ? a : m;
- m = b > m ? b : m;
- }
- if (m > level)
- return true;
- }
- for (; i < n; ++i)
- if (std::abs (l[i]) > level || std::abs (r[i]) > level)
- return true;
- return false;
- }
- } // namespace
- namespace
- {
- // A Patcher-style container bakes its children (kinds, states, parameters, routing) in when it is built, so it can be
- // reused only if all of that is unchanged. (It used to be rebuilt on every edit: its children's tails and delay lines
- // restarted, and a fresh linear-phase child reporting its latency only after the first block made the engine re-plan
- // PDC and rebuild it again, over and over — REA round 87.)
- bool sameSlotSetup (const EffectSlot& a, const EffectSlot& b);
- bool sameBranches (const EffectSlot& a, const EffectSlot& b)
- {
- if (a.branches.size() != b.branches.size() || a.links.size() != b.links.size())
- return false;
- for (std::size_t i = 0; i < a.branches.size(); ++i)
- {
- const auto& x = a.branches[i];
- const auto& y = b.branches[i];
- if (x.gain != y.gain || x.chain.size() != y.chain.size())
- return false;
- for (std::size_t k = 0; k < x.chain.size(); ++k)
- if (! sameSlotSetup (x.chain[k], y.chain[k]))
- return false;
- }
- for (std::size_t i = 0; i < a.links.size(); ++i)
- {
- const auto& x = a.links[i];
- const auto& y = b.links[i];
- if (x.branch != y.branch || x.source != y.source || x.target != y.target || x.param != y.param || x.curve != y.curve)
- return false;
- }
- return true;
- }
- bool sameSlotSetup (const EffectSlot& a, const EffectSlot& b)
- {
- return a.kind == b.kind && a.enabled == b.enabled && a.params == b.params && a.mix == b.mix && a.plugin.id == b.plugin.id
- && a.sidechain == b.sidechain && a.sidechain2 == b.sidechain2 && a.sendTracks == b.sendTracks && a.state == b.state && a.samplePath == b.samplePath
- && sameBranches (a, b);
- }
- } // namespace
- bool sameContainer (const EffectSlot& a, const EffectSlot& b) { return sameBranches (a, b); }
- namespace
- {
- std::uint64_t nowNs() noexcept
- {
- #if defined(__linux__)
- // diagnostics on a busy machine: LYNX_PROFILE_CPU=1 measures each unit in its thread's CPU time (renderwav)
- static const bool threadCpu = std::getenv ("LYNX_PROFILE_CPU") != nullptr;
- if (threadCpu)
- {
- timespec ts {};
- clock_gettime (CLOCK_THREAD_CPUTIME_ID, &ts);
- return (std::uint64_t) ts.tv_sec * 1000000000ull + (std::uint64_t) ts.tv_nsec;
- }
- #endif
- return (std::uint64_t) std::chrono::duration_cast<std::chrono::nanoseconds> (std::chrono::steady_clock::now().time_since_epoch()).count();
- }
- // Adds the time since `start` to `slot` when profiling (start = 0: profiling was off when the unit began)
- struct UnitTimer
- {
- UnitTimer (bool on, std::atomic<std::uint64_t>& s, std::atomic<std::uint64_t>* m = nullptr) noexcept
- : slot (s), longest (m), start (on ? nowNs() : 0) {}
- ~UnitTimer()
- {
- if (start == 0)
- return;
- const auto used = nowNs() - start;
- slot.fetch_add (used, std::memory_order_relaxed);
- if (longest != nullptr)
- {
- auto current = longest->load (std::memory_order_relaxed);
- while (used > current && ! longest->compare_exchange_weak (current, used, std::memory_order_relaxed))
- {
- }
- }
- }
- std::atomic<std::uint64_t>& slot;
- std::atomic<std::uint64_t>* longest;
- std::uint64_t start;
- };
- } // namespace
- void RackRenderer::resetUnitTimes() noexcept
- {
- for (auto& t : units->channelNs)
- t.store (0, std::memory_order_relaxed);
- for (auto& t : units->effectNs)
- t.store (0, std::memory_order_relaxed);
- for (auto& t : units->channelMaxNs)
- t.store (0, std::memory_order_relaxed);
- for (auto& t : units->effectMaxNs)
- t.store (0, std::memory_order_relaxed);
- units->processNs.store (0, std::memory_order_relaxed);
- units->processedSamples.store (0, std::memory_order_relaxed);
- }
- void RackRenderer::prepare (double sr, int maxBlockSize)
- {
- automationValues.fill (std::numeric_limits<float>::quiet_NaN());
- lastTrackGain.fill ({ std::numeric_limits<float>::quiet_NaN(), 0.0f, 0.0f, 0.0f });
- lastTapGain.fill ({ std::numeric_limits<float>::quiet_NaN(), 0.0f, 0.0f, 0.0f });
- channelBuffers.assign ((std::size_t) maxChannels * 2 * chunkCapacity, 0.0f);
- smartDisable.assign ((std::size_t) maxMixerTracks * maxEffectSlots, {});
- trackEqs.assign ((std::size_t) maxMixerTracks, {});
- trackScratchBuffers.assign ((std::size_t) maxMixerTracks * 2 * chunkCapacity, 0.0f);
- tapBuffers.assign ((std::size_t) RackState::maxSendTaps * 2 * chunkCapacity, 0.0f);
- sampleRate = sr;
- blockCapacity = std::max (64, maxBlockSize);
- mixBuffers.assign ((std::size_t) maxMixerTracks * 2 * (std::size_t) blockCapacity, 0.0f);
- hwScratch.assign (2 * (std::size_t) blockCapacity, 0.0f);
- layerPick.assign ((std::size_t) maxChannels * 128, (std::int16_t) -1);
- polyLayers.assign ((std::size_t) maxChannels, {});
- if (lastState != nullptr)
- {
- for (auto& instrument : lastState->instruments)
- instrument->prepare (sr);
- for (auto& track : lastState->effects)
- for (auto& effect : track)
- effect->prepare (sr);
- }
- }
- float* RackRenderer::trackBuffer (int track, int channel) noexcept
- {
- return mixBuffers.data() + ((std::size_t) track * 2 + (std::size_t) channel) * (std::size_t) blockCapacity;
- }
- float* RackRenderer::tapBuffer (int tap, int side) noexcept
- {
- return tapBuffers.data() + ((std::size_t) tap * 2 + (std::size_t) side) * chunkCapacity;
- }
- float RackRenderer::automated (int automation, float base) const noexcept
- {
- if (automation < 0)
- return base;
- const auto v = automationValues[(std::size_t) automation];
- return std::isnan (v) ? base : v;
- }
- namespace
- {
- // Value of an automation at a song position: inside a clip, the curve; between clips,
- // the last value of the previous clip; before the first, its initial value. Depends only on
- // position, so playing from any point (or exporting) gives the same result.
- float automationValueAt (const Song& song, const Automation& automation, const std::vector<int>& clips, double tick) noexcept
- {
- const Clip* before = nullptr;
- for (auto i : clips) // sorted by start
- {
- const auto& c = song.playlist[(std::size_t) i];
- if (tick < (double) c.start)
- break;
- if (tick < (double) c.end())
- return automation.valueAt (tick - (double) c.start + (double) c.offset);
- before = &c;
- }
- if (before != nullptr)
- return automation.valueAt ((double) (before->length + before->offset));
- const auto& first = song.playlist[(std::size_t) clips.front()];
- return automation.valueAt ((double) first.offset);
- }
- } // namespace
- void RackRenderer::setOverriddenSlots (const std::vector<int>& slots) noexcept
- {
- std::array<std::uint32_t, RackState::maxAutomations / 32> bits {};
- for (const auto s : slots)
- if (s >= 0 && s < RackState::maxAutomations)
- bits[(std::size_t) s / 32] |= 1u << ((unsigned) s % 32);
- for (std::size_t i = 0; i < bits.size(); ++i)
- overridden[i].store (bits[i], std::memory_order_relaxed);
- anyOverridden.store (! slots.empty(), std::memory_order_release);
- }
- void RackRenderer::clearOverriddenValues() noexcept
- {
- if (! anyOverridden.load (std::memory_order_acquire))
- return;
- for (std::size_t w = 0; w < overridden.size(); ++w)
- if (auto bits = overridden[w].load (std::memory_order_relaxed); bits != 0)
- for (unsigned b = 0; b < 32; ++b)
- if ((bits & (1u << b)) != 0)
- automationValues[w * 32 + b] = std::numeric_limits<float>::quiet_NaN(); // the control's own value
- }
- void RackRenderer::updateAutomation (const RackState& state, double startTick) noexcept
- {
- const auto& song = state.song;
- // controllers (live sources): the effect's output at the end of the previous block, in any mode
- for (const auto a : state.liveAutomations)
- {
- const auto& automation = song.automations[(std::size_t) a];
- const auto& src = automation.source;
- if (src.channel >= 0)
- {
- if (src.channel < (int) state.instruments.size() && state.instruments[(std::size_t) src.channel] != nullptr)
- automationValues[(std::size_t) a] = automation.mapLive (state.instruments[(std::size_t) src.channel]->controlOutput (src.output));
- }
- else if (src.track < (int) state.effects.size() && src.slot >= 0 && src.slot < (int) state.effects[(std::size_t) src.track].size()
- && state.effects[(std::size_t) src.track][(std::size_t) src.slot] != nullptr)
- automationValues[(std::size_t) a] = automation.mapLive (state.effects[(std::size_t) src.track][(std::size_t) src.slot]->controlOutput (src.output));
- }
- // pattern events: in pattern mode the current pattern's (looped); in song mode those of the pattern clip
- // that covers the position. Elsewhere the value holds.
- if (! state.eventLanes.empty())
- {
- if (song.mode != PlayMode::song)
- {
- const auto current = song.currentPattern;
- if (current >= 0 && current < (int) song.patterns.size())
- {
- const auto& pattern = song.patterns[(std::size_t) current];
- const auto length = (double) std::max<std::int64_t> (1, pattern.effectiveLength (song.beatsPerBar));
- const auto t = std::fmod (std::max (0.0, startTick), length);
- for (const auto& lane : state.eventLanes)
- if (lane.pattern == current)
- automationValues[(std::size_t) lane.slot] = pattern.events[(std::size_t) lane.lane].valueAt (t);
- }
- }
- else
- {
- const auto length = (double) std::max<std::int64_t> (1, state.schedule.songLength);
- const auto tick = std::fmod (std::max (0.0, startTick), length);
- // inside a clip of the pattern: its events; after one: the value it ended with; before the first:
- // no value (the target's own). Depends only on the position, like automation clips.
- for (const auto& lane : state.eventLanes)
- {
- const auto& events = song.patterns[(std::size_t) lane.pattern].events[(std::size_t) lane.lane];
- auto value = std::numeric_limits<float>::quiet_NaN();
- for (auto i : state.patternClips[(std::size_t) lane.pattern]) // sorted by start
- {
- const auto& c = song.playlist[(std::size_t) i];
- if (tick < (double) c.start)
- break;
- // a stretched clip loops its pattern (LYNX extra stretchedClipsLoop, issue #25), its events too
- const auto period = (std::size_t) lane.pattern < state.schedule.patternLength.size() ? state.schedule.patternLength[(std::size_t) lane.pattern] : 0;
- value = events.valueAt (patternTickInClip (c, tick < (double) c.end() ? tick : (double) c.end() - 1.0, period));
- }
- automationValues[(std::size_t) lane.slot] = value;
- }
- }
- }
- // in pattern mode the Playlist does not play: values stay as they were
- if (song.mode != PlayMode::song)
- {
- clearOverriddenValues();
- // only the plugin parameters driven by pattern events and by live controllers (links: FL's work in any mode,
- // round 165)
- if (! state.eventLanes.empty() || ! state.liveAutomations.empty())
- applyPluginParams (state, std::min<int> ((int) song.automations.size(), RackState::maxAutomations), true);
- return;
- }
- const auto length = (double) std::max<std::int64_t> (1, state.schedule.songLength);
- const auto tick = std::fmod (std::max (0.0, startTick), length);
- for (std::size_t a = 0; a < state.automationClips.size(); ++a)
- if (! state.automationClips[a].empty())
- automationValues[a] = automationValueAt (song, song.automations[a], state.automationClips[a], tick);
- // Performance mode: the launched automation clips (the last block's passes) drive their automation
- if (perfRunning)
- for (int i = 0; i < perfPlayer.virtualCount(); ++i)
- {
- const auto& v = perfPlayer.virtualClip (i).clip;
- if (v.kind != ClipKind::automation || v.automation < 0 || v.automation >= (int) song.automations.size()
- || v.automation >= RackState::maxAutomations)
- continue;
- const auto local = std::clamp (perfClock - (double) v.start, 0.0, (double) v.length);
- automationValues[(std::size_t) v.automation] = song.automations[(std::size_t) v.automation].valueAt (local + (double) v.offset);
- }
- clearOverriddenValues();
- applyPluginParams (state, 0);
- }
- void RackRenderer::applyPluginParams (const RackState& state, int firstSlot, bool andLive) noexcept
- {
- for (const auto& b : state.pluginParams)
- {
- if (b.automation < firstSlot
- && ! (andLive && (std::size_t) b.automation < state.song.automations.size() && state.song.automations[(std::size_t) b.automation].isLive()))
- continue;
- const auto v = automationValues[(std::size_t) b.automation];
- if (std::isnan (v))
- continue;
- if (b.channel >= 0)
- {
- const auto kind = (std::size_t) b.channel < state.song.channels.size() ? state.song.channels[(std::size_t) b.channel].instrument
- : InstrumentKind::plugin;
- if (midictl::isPitchBend (b.param) && kind != InstrumentKind::plugin && kind != InstrumentKind::midiOut
- && b.channel < (int) state.instruments.size() && b.channel < maxChannels)
- {
- // LYNX's own instruments: the bend as the controller's wheel plays it (applyLiveControl), when it changes
- if (! automatedBendReset)
- {
- automatedBend.fill (std::numeric_limits<float>::quiet_NaN());
- automatedBendReset = true;
- }
- auto& last = automatedBend[(std::size_t) b.channel];
- if (! (last == v) && state.instruments[(std::size_t) b.channel] != nullptr)
- {
- last = v;
- NoteEvent e;
- e.type = NoteEvent::Type::control;
- e.channel = b.channel;
- e.key = NoteEvent::controlPitchBend;
- e.velocity = std::clamp (v, 0.0f, 1.0f) * 2.0f - 1.0f;
- applyLiveControl (state, e);
- }
- continue;
- }
- if (b.channel < (int) state.instruments.size())
- state.instruments[(std::size_t) b.channel]->setParameter (b.param, v);
- // a MIDI Out's controls reach the plugins on its port
- if ((std::size_t) b.channel < state.midiTargets.size() && midictl::isMidiParam (b.param)) // (a Dashboard's outputs stay its own)
- for (const auto t : state.midiTargets[(std::size_t) b.channel])
- if (t < (int) state.instruments.size())
- state.instruments[(std::size_t) t]->setParameter (b.param, v);
- }
- else if (b.track < (int) state.effects.size() && b.slot < (int) state.effects[(std::size_t) b.track].size())
- state.effects[(std::size_t) b.track][(std::size_t) b.slot]->setParameter (b.param, v);
- }
- }
- void RackRenderer::deliverToEffects (const RackState& state, const NoteEvent& e) noexcept
- {
- if (e.channel < 0 || (std::size_t) e.channel >= state.effectMidiRoutes.size()
- || (e.type != NoteEvent::Type::on && e.type != NoteEvent::Type::off && e.type != NoteEvent::Type::voiceProp) || e.port == -2)
- return;
- if (e.type == NoteEvent::Type::on && state.song.channels[(std::size_t) e.channel].muted)
- return;
- for (const auto& route : state.effectMidiRoutes[(std::size_t) e.channel])
- {
- if ((std::size_t) route.track >= state.effects.size() || (std::size_t) route.slot >= state.effects[(std::size_t) route.track].size())
- continue;
- if ((e.port >= 0 && route.port != e.port) || (e.port < 0 && route.portOnly))
- continue;
- if (auto* graph = patcher::graphOf (state.effects[(std::size_t) route.track][(std::size_t) route.slot].get()))
- {
- if (e.type == NoteEvent::Type::voiceProp) // (a VFX channel's voice property, round 165c: at once)
- {
- graph->voicePropertyPort (route.port, e.key, e.prop, e.velocity, e.port >= 0 ? (int) e.colour : route.midiChannel);
- continue;
- }
- // at the note's sample in the chunk the slot renders next (round 164f; it was the chunk's start); a slot
- // that is off renders nothing and gets nothing
- const auto& slot = state.song.mixer[(std::size_t) route.track].effects[(std::size_t) route.slot];
- if (! state.song.slotActive (slot))
- continue;
- if (e.type == NoteEvent::Type::on)
- {
- graph->queueNoteOnPort (route.port, e.key, e.velocity, route.midiChannel, e.sampleOffset);
- // a note wakes a smart-disabled slot: the instruments inside play it (a silent insert is their usual place)
- if (const auto key = (std::size_t) route.track * maxEffectSlots + (std::size_t) route.slot; key < smartDisable.size())
- smartDisable[key].silentSamples = 0;
- }
- else
- graph->queueNoteOffPort (route.port, e.key, route.midiChannel, e.sampleOffset);
- }
- }
- }
- float* RackRenderer::channelBuffer (int channel, int side) noexcept
- {
- return channelBuffers.data() + ((std::size_t) channel * 2 + (std::size_t) side) * chunkCapacity;
- }
- float* RackRenderer::trackScratch (int track, int side) noexcept
- {
- return trackScratchBuffers.data() + ((std::size_t) track * 2 + (std::size_t) side) * chunkCapacity;
- }
- // A whole channel over the chunk: notes at the exact sample (only its own events), volume/pan and the
- // PDC delay. Each channel only touches its own instrument and buffer: it can run in parallel.
- channelpoly::Mode RackRenderer::polyMode (const ChannelDef& c, const Instrument& instrument) const noexcept
- {
- channelpoly::Mode m;
- // (the LYNX Synth plays its own Mono / Porta, the Sampler its own Porta: the layer leaves those to them)
- m.mono = c.instrument != InstrumentKind::synth && channelMono (c);
- m.porta = channelPortamento (c);
- m.portaBeats = channelPortamentoBeats (c);
- m.arpeggiator = c.arp.direction != 0;
- m.echo = c.misc.echo.feedback > 0.0f;
- m.slides = instrument.followsSlides();
- m.glidesIn = instrument.followsChannelPorta();
- return m;
- }
- void RackRenderer::clearPolyLayers() noexcept
- {
- for (auto& layer : polyLayers)
- layer.clear();
- }
- void RackRenderer::renderChannel (const RackState& state, int i, int n) noexcept
- {
- const auto& c = state.song.channels[(std::size_t) i];
- const auto automation = state.hasAutomation;
- const auto volume = channelGain (automation ? automated (state.channelVolumeAuto[(std::size_t) i], c.volume) : c.volume);
- const auto pan = automation ? automated (state.channelPanAuto[(std::size_t) i], (c.pan + 1.0f) * 0.5f) * 2.0f - 1.0f : c.pan;
- float gl = 0.0f, gr = 0.0f;
- panGains (pan, volume, gl, gr);
- auto* l = channelBuffer (i, 0);
- auto* r = channelBuffer (i, 1);
- if ((std::size_t) i < state.instruments.size() && state.instruments[(std::size_t) i] != nullptr)
- state.instruments[(std::size_t) i]->setSongPosition (chunkTick);
- auto& instrument = *state.instruments[(std::size_t) i];
- instrument.beginChunk (n); // (extra outputs: a generator Patcher's, REA_rodada141)
- // issue #14: the channel's pitch as it is now (a hosted plugin bends to it; native engines took it at note on)
- // (round 167a: the native generators move their sounding notes with it, as FL's do)
- instrument.setChannelPitchNow (c.sampler.pitch * 100.0f, c.misc.fineTune + (c.misc.mainPitch ? state.song.masterPitch : 0.0f));
- if (c.instrument == InstrumentKind::patcher) // its Hardware Insert nodes: this chunk's device buses (round 165c)
- instrument.setHardwareIo (liveInput, hwOut != nullptr && hwOut->active.load (std::memory_order_relaxed) ? hwOut : nullptr, liveInputOffset);
- // idle channel, no notes in this chunk and no PDC: nothing to do (neither clear nor sum into the mixer)
- const auto hasDelay = (std::size_t) i < state.channelDelays.size() && state.channelDelays[(std::size_t) i] != nullptr;
- const auto hasArp = ((std::size_t) i < state.arpeggiators.size() && state.arpeggiators[(std::size_t) i] != nullptr)
- || ((std::size_t) i < state.echoes.size() && state.echoes[(std::size_t) i] != nullptr);
- const auto* sources = state.hasMidiRoutes && (std::size_t) i < state.midiSources.size() && ! state.midiSources[(std::size_t) i].empty()
- ? &state.midiSources[(std::size_t) i] : nullptr;
- const auto routed = [sources] (int channel) {
- return sources != nullptr && std::find (sources->begin(), sources->end(), channel) != sources->end();
- };
- if (! hasDelay && ! hasArp && instrument.idle()
- && std::none_of (events.begin(), events.end(), [&] (const NoteEvent& e) { return e.channel == i || routed (e.channel); }))
- {
- channelSilent[(std::size_t) i] = 1;
- return;
- }
- channelSilent[(std::size_t) i] = 0;
- const UnitTimer timer (profiling.load (std::memory_order_relaxed), units->channelNs[(std::size_t) i], &units->channelMaxNs[(std::size_t) i]);
- std::memset (l, 0, sizeof (float) * (std::size_t) n);
- std::memset (r, 0, sizeof (float) * (std::size_t) n);
- int position = 0;
- // arpeggiator: the channel's notes become the arpeggio sequence
- const NoteEvent* list = events.begin();
- int listCount = events.size();
- if ((std::size_t) i < state.arpeggiators.size() && state.arpeggiators[(std::size_t) i] != nullptr)
- {
- auto& arp = *state.arpeggiators[(std::size_t) i];
- int inCount = 0;
- for (const auto& e : events)
- if (e.channel == i && inCount < Arpeggiator::maxEvents)
- arp.input[(std::size_t) inCount++] = e;
- int outCount = 0;
- arp.process (i, arp.input.data(), inCount, chunkStartTick, chunkTps, n, c.arp, arp.output.data(), outCount);
- // FL cuts an arpeggio note at its gate even on a sample that plays out (REA round 146b [EXEC: FL's renders of
- // its Sampler render test]): the arpeggio is one voice, so its "off" stops the channel's sound (not with the
- // echo delay, whose echoes keep ringing)
- if (! c.misc.echo.enabled())
- for (int k = 0; k < outCount; ++k)
- if (arp.output[(std::size_t) k].type == NoteEvent::Type::off)
- arp.output[(std::size_t) k].type = NoteEvent::Type::choke;
- list = arp.output.data();
- listCount = outCount;
- }
- // FL's echo delay (Misc tab): the channel's notes (after the arpeggio) and their echoes
- const float* gains = nullptr;
- if ((std::size_t) i < state.echoes.size() && state.echoes[(std::size_t) i] != nullptr)
- {
- auto& echo = *state.echoes[(std::size_t) i];
- int inCount = 0;
- for (int k = 0; k < listCount; ++k)
- if (list[k].channel == i && inCount < channelmisc::NoteEcho::maxEvents)
- echo.input[(std::size_t) inCount++] = list[k];
- int outCount = 0;
- echo.process (i, echo.input.data(), inCount, chunkStartTick, chunkTps, n, c.misc.echo, echo.output.data(), echo.outputGains.data(), outCount);
- list = echo.output.data();
- listCount = outCount;
- gains = echo.outputGains.data();
- }
- const auto tracking = c.misc.key.pan != 0.0f || c.misc.velocity.pan != 0.0f;
- const auto modTracking = c.misc.key.modX != 0.0f || c.misc.key.modY != 0.0f || c.misc.velocity.modX != 0.0f || c.misc.velocity.modY != 0.0f;
- // FL's Add to key (round 149): the root note moves the notes of instruments that are not samplers
- const auto keyShift = c.misc.addToKey && c.instrument != InstrumentKind::sampler ? 60 - c.misc.rootNote : 0;
- for (int index = 0; index < listCount; ++index)
- {
- auto shifted = list[index];
- if (keyShift != 0 && shifted.channel == i)
- shifted.key = std::clamp (shifted.key + keyShift, 0, 127);
- const auto& e = shifted;
- if (e.channel != i)
- {
- // a note of a MIDI Out channel on this plugin's MIDI input port, with that MIDI Out's MIDI channel
- if (sources == nullptr || ! routed (e.channel)
- || (e.type != NoteEvent::Type::on && e.type != NoteEvent::Type::off && e.type != NoteEvent::Type::voiceProp))
- continue;
- if (e.port == -2) // (a VFX channel's own note: its processor's output goes out instead)
- continue;
- if (e.sampleOffset > position)
- {
- instrument.render (l + position, r + position, e.sampleOffset - position, gl, gr);
- position = e.sampleOffset;
- }
- // (each route from that channel: its port and MIDI channel, round 162)
- for (const auto& route : state.midiRoutesIn[(std::size_t) i])
- {
- if (route.other != e.channel || (e.port >= 0 && route.port != e.port) || (e.port < 0 && route.portOnly))
- continue;
- // (a VFX channel's voice keeps its colour as the MIDI channel; so does a MIDI Out's note with "Map note
- // color to MIDI channel", round 167i)
- const auto midiChannel = e.port >= 0 || ((std::size_t) e.channel < state.mapNoteColour.size() && state.mapNoteColour[(std::size_t) e.channel] != 0)
- ? (int) (e.colour & 15)
- : route.midiChannel;
- if (e.type == NoteEvent::Type::voiceProp) // (round 165c: outside a Patcher too)
- instrument.voicePropertyPort (route.port, e.key, e.prop, e.velocity, midiChannel);
- else if (e.type == NoteEvent::Type::on)
- {
- if (! c.muted && ! state.song.channels[(std::size_t) e.channel].muted)
- {
- if (e.port >= 0)
- {
- instrument.setNextNoteExpression (e.pan, e.cents, e.porta);
- instrument.setNextNoteModulation (e.modX, e.modY);
- instrument.setNextNoteColour (e.colour);
- }
- instrument.noteOnPort (route.port, e.key, e.velocity, midiChannel);
- }
- }
- else
- {
- if (e.port >= 0)
- instrument.setNextNoteRelease (e.release);
- instrument.noteOffPort (route.port, e.key, midiChannel);
- }
- }
- continue;
- }
- if (e.port >= 0 || e.type == NoteEvent::Type::voiceProp) // (what this channel sends out on a port is for the others)
- continue;
- if (e.sampleOffset > position)
- {
- instrument.render (l + position, r + position, e.sampleOffset - position, gl, gr);
- position = e.sampleOffset;
- }
- auto& held = heldNotes[(std::size_t) i];
- const auto bit = std::uint64_t { 1 } << (e.key & 63);
- if (e.type == NoteEvent::Type::on)
- {
- if (e.key >= 0 && e.key < 128)
- {
- held[(std::size_t) (e.key >> 6)] |= bit;
- struckNotes[(std::size_t) i][(std::size_t) (e.key >> 6)] |= bit;
- }
- // FL's key region (round 164): the channel plays only the notes inside it (audio clips: the key is the clip)
- const auto inRegion = c.instrument == InstrumentKind::audioClip || c.inKeyRegion (list[index].key);
- if (! c.muted && inRegion)
- {
- // (an audio clip follows its fine tune, not the main pitch: LYNX never moved clips with it)
- const auto noteCents = e.cents + c.misc.fineTune
- + (c.misc.mainPitch && c.instrument != InstrumentKind::audioClip ? state.song.masterPitch : 0.0f);
- // FL's channel Mono / Porta (round 168l): a Mono note over a held one moves the sounding voice instead
- const auto mode = polyMode (c, instrument);
- const auto glideSamples = (float) (channelpoly::glideBeats (mode.portaBeats) * ppq / std::max (1.0e-9, chunkTps));
- auto& poly = polyLayers[(std::size_t) i];
- // (an echo delay's repeats are FL's generated voices: they don't enter the Mono list)
- const auto on = e.echo ? channelpoly::On {} : poly.noteOn (mode, e.key, noteCents, e.porta, e.colour);
- if (on.release >= 0)
- {
- instrument.noteOff (on.release);
- voiceOrders[(std::size_t) i].noteOff (on.release);
- }
- if (! on.start)
- {
- if (on.slide)
- instrument.slideTo (e.key, noteCents, glideSamples);
- continue;
- }
- // Max polyphony: the oldest note is released first
- if (const auto limit = voiceLimitFor (c.maxPolyphony, channelMono (c)); limit > 0)
- if (const auto victim = voiceOrders[(std::size_t) i].noteOn (e.key, limit); victim >= 0)
- instrument.noteOff (victim);
- const auto pan = tracking ? channelmisc::trackedPan (c.misc, e.key, e.velocity, e.pan) : e.pan;
- // (porta: the voice starts at the group's last note and glides to its own pitch, HostSlide::anchor)
- instrument.setNextNoteExpression (pan, noteCents + (on.glide ? on.glideFrom : 0.0f), e.porta);
- HostSlide::pendingGlide() = { on.glide ? on.glideFrom : 0.0f, on.glide ? glideSamples : 0.0f, false };
- {
- const auto [modX, modY] = modTracking ? channelmisc::trackedModulation (c.misc, e.key, e.velocity, e.modX, e.modY)
- : std::pair<float, float> { e.modX, e.modY };
- instrument.setNextNoteModulation (modX, modY);
- }
- instrument.setNextNoteColour (e.colour);
- if (e.clipPitch != 0.0f)
- instrument.setNextClipPitch (e.clipPitch);
- if (e.clipStretch != 1.0f)
- instrument.setNextClipStretch (e.clipStretch);
- if (e.fadeIn > 0.0f || e.fadeOut > 0.0f || e.fadeShape != 0)
- instrument.setNextClipFades (e.clipElapsed, e.clipRemaining, e.fadeIn, e.fadeOut, e.fadeShape);
- // an echo's volume: the instrument scales the voice, else (engines without it) the velocity
- auto velocity = e.velocity;
- const auto gain = e.gain * (gains != nullptr ? gains[index] : 1.0f);
- if (gain != 1.0f && ! instrument.setNextNoteGain (gain))
- velocity *= gain;
- instrument.noteOnAt (e.key, velocity, e.startSeconds);
- HostSlide::pendingGlide() = {};
- }
- }
- else if (e.type == NoteEvent::Type::slide)
- {
- instrument.slideTo (e.key, e.cents + c.misc.fineTune + (c.misc.mainPitch ? state.song.masterPitch : 0.0f), e.slideSamples);
- }
- else if (e.type == NoteEvent::Type::choke)
- {
- instrument.cut(); // short fade, like FL's "cut"
- held = {};
- voiceOrders[(std::size_t) i].clear();
- polyLayers[(std::size_t) i].clear();
- }
- else
- {
- instrument.setNextNoteRelease (e.release);
- // a Mono channel (round 168l): the voice ends with the last held note, else it glides back to the newest
- const auto mode = polyMode (c, instrument);
- const auto off = e.echo ? channelpoly::Off { e.key } : polyLayers[(std::size_t) i].noteOff (mode, e.key, e.colour);
- if (off.slide)
- instrument.slideTo (off.key, off.cents,
- (float) (channelpoly::glideBeats (mode.portaBeats) * ppq / std::max (1.0e-9, chunkTps)));
- if (off.release >= 0)
- {
- instrument.noteOff (off.release);
- voiceOrders[(std::size_t) i].noteOff (off.release);
- }
- if (e.key >= 0 && e.key < 128)
- held[(std::size_t) (e.key >> 6)] &= ~bit;
- }
- }
- if (position < n)
- instrument.render (l + position, r + position, n - position, gl, gr);
- // PDC: this channel arrives before the track's others; delay until aligned
- if ((std::size_t) i < state.channelDelays.size() && state.channelDelays[(std::size_t) i] != nullptr)
- state.channelDelays[(std::size_t) i]->process (l, r, n);
- }
- void RackRenderer::applyTrackEq (const MixerTrack& track, int t, bool bypassed, float* l, float* r, int n) noexcept
- {
- if ((std::size_t) t >= trackEqs.size())
- return;
- auto& eq = trackEqs[(std::size_t) t];
- if (bypassed || ! track.eq.active()) // clean mixer (round 163): no built-in processing, the samples pass untouched
- {
- if (eq.running)
- eq = {}; // off: reset the filters so they don't come back with a stale tail
- return;
- }
- if (! eq.running || ! (eq.settings == track.eq))
- {
- // new coefficients, keeping the filter state (no click when adjusting)
- const auto& s = track.eq;
- eq.bands[0].shelf (sampleRate, std::min ((double) s.hz[0], sampleRate * 0.45), s.gainDb[0], false, TrackEq::shelfSlope (s.width[0]));
- eq.bands[1].peaking (sampleRate, std::min ((double) s.hz[1], sampleRate * 0.45), TrackEq::peakQ (s.width[1]), s.gainDb[1]);
- eq.bands[2].shelf (sampleRate, std::min ((double) s.hz[2], sampleRate * 0.45), s.gainDb[2], true, TrackEq::shelfSlope (s.width[2]));
- for (std::size_t b = 0; b < 3; ++b)
- if (s.gainDb[b] == 0.0f)
- eq.bands[b].reset();
- eq.settings = s;
- eq.running = true;
- }
- for (std::size_t b = 0; b < 3; ++b)
- {
- if (eq.settings.gainDb[b] == 0.0f)
- continue; // band at 0 dB: not processed (like FL)
- eq.bands[b].processBlock (l, r, n);
- }
- }
- // effects whose controlOutput() an editor shows live
- static bool hasLiveMeters (EffectKind kind) noexcept
- {
- // round 129: the voice tools (Quell's reduction meter was never published) and the basic compressor's meters
- return mixsuite::isSuiteKind (kind) || dyn::isDynKind (kind) || kind == EffectKind::lynxTune || kind == EffectKind::facetEq
- || kind == EffectKind::apexLimiter || voicetools::isVoiceKind (kind) || kind == EffectKind::compressor
- || kind == EffectKind::echoform // round 135w: Echoform's output meter
- || kind == EffectKind::autoPan || kind == EffectKind::voiceShift // round 135: the pan position, the shifter's notes
- || kind == EffectKind::hardwareInsert // round 164d: send / return levels, the ping
- || kind == EffectKind::xyController || kind == EffectKind::xyzController; // round 164: the pad's target and outputs
- }
- void RackRenderer::setSidechain (const RackState& state, const EffectSlot& slot, Effect& effect, int track) noexcept
- {
- // the source was already processed in this pass (earlier layer); silent = no signal in the detector
- for (const auto second : { false, true })
- {
- const auto source = second ? slot.sidechain2 : slot.sidechain;
- if (source < 0 || source == track || source >= (int) state.song.mixer.size())
- continue;
- const auto active = (std::size_t) source < trackActive.size() && trackActive[(std::size_t) source] != 0;
- const auto* left = active ? trackBuffer (source, 0) : nullptr;
- const auto* right = active ? trackBuffer (source, 1) : nullptr;
- if (second)
- effect.setSidechain2 (left, right);
- else
- effect.setSidechain (left, right);
- }
- }
- void RackRenderer::runEffects (const RackState& state, int track, int n) noexcept
- {
- if (track >= (int) state.effects.size())
- return;
- const auto& slots = state.song.mixer[(std::size_t) track].effects;
- const auto& effects = state.effects[(std::size_t) track];
- auto* l = trackBuffer (track, 0);
- auto* r = trackBuffer (track, 1);
- int audible = -1; // -1 = not measured yet; an effect's output is the next one's input
- const auto timing = times->enabled.load (std::memory_order_relaxed);
- auto timed = [this, timing] (std::size_t key, auto&& run) {
- if (! timing || key >= times->effectNs.size())
- {
- run();
- return;
- }
- const auto before = ProcessTimes::now();
- run();
- times->effectNs[key].fetch_add (ProcessTimes::now() - before, std::memory_order_relaxed);
- };
- for (std::size_t i = 0; i < effects.size() && i < slots.size(); ++i)
- {
- // slot dry/wet (automatable)
- const auto wet = state.hasAutomation && i < state.effectMixAuto[(std::size_t) track].size()
- ? std::clamp (automated (state.effectMixAuto[(std::size_t) track][i], slots[i].mix), 0.0f, 1.0f)
- : slots[i].mix;
- auto clearMeters = [this, track, i] {
- const auto at = ((std::size_t) track * maxEffectSlots + i) * ProcessTimes::meterOutputs;
- if (at + ProcessTimes::meterOutputs <= times->effectMeters.size())
- for (int o = 0; o < ProcessTimes::meterOutputs; ++o)
- times->effectMeters[at + (std::size_t) o].store (0.0f, std::memory_order_relaxed);
- };
- if (! state.song.slotActive (slots[i]) || wet <= 0.0f)
- {
- if (hasLiveMeters (slots[i].kind))
- clearMeters();
- continue;
- }
- // smart disable (like FL): an effect with silent input and output is skipped (plugins
- // after 4 s; built-ins after 1 s, which is well beyond their tail)
- const auto key = (std::size_t) track * maxEffectSlots + i;
- const auto isPlugin = slots[i].kind == EffectKind::plugin;
- const auto hold = (int) (sampleRate * (isPlugin || slots[i].kind == EffectKind::patcher ? 4.0 : 1.0)); // (a Patcher may hold plugins)
- // (not Gross Beat: its buffer must keep recording the silence it may play back later, as in FL)
- // (nor Newtone / Newtime: in slave mode they play their sample whatever the input)
- // (nor a slot whose "Smart disable" is off, or a plugin that does not allow it — FL's per-plugin options, REA round 139)
- if (key < smartDisable.size() && slots[i].smartDisable && ! (isPlugin && ! slots[i].plugin.allowSmartDisable)
- && slots[i].kind != EffectKind::grossBeat && slots[i].kind != EffectKind::newtone
- && slots[i].kind != EffectKind::newtime
- && slots[i].kind != EffectKind::scratcher // (nor the Scratcher: its turntable plays whatever the input)
- && slots[i].kind != EffectKind::hardwareInsert // (nor the hardware insert: outboard gear has its own tails and noise)
- && ! (slots[i].kind == EffectKind::patcher && patcher::usesHardwareIo (slots[i].patcher)) // (nor a Patcher holding one, round 165c)
- // (nor a controller: its outputs move whatever the sound — a silent insert is their usual place; round 164)
- && slots[i].kind != EffectKind::formulaController
- && slots[i].kind != EffectKind::xyController && slots[i].kind != EffectKind::xyzController
- && slots[i].kind != EffectKind::controlSurface
- // round 164c: a visual plugin keeps recording silence (its display falls, the video export stays in time)
- && slots[i].kind != EffectKind::waveCandy && slots[i].kind != EffectKind::visualizer
- && slots[i].kind != EffectKind::tuner // (round 165g: the tuner's display follows the signal into silence)
- // (round 166f: nor Facet EQ playing its comparative target — the sidechain plays while this track is silent)
- && ! (slots[i].kind == EffectKind::facetEq && facet::listensToTarget (slots[i].params)))
- {
- auto& sd = smartDisable[key];
- if (sd.effect != effects[i].get())
- sd = { effects[i].get(), 0 };
- if (audible < 0)
- audible = anyAbove (l, r, n, silenceLevel) ? 1 : 0;
- if (audible == 1)
- sd.silentSamples = 0;
- else if (sd.silentSamples >= hold)
- {
- if (hasLiveMeters (slots[i].kind))
- clearMeters();
- continue;
- }
- else
- sd.silentSamples += n;
- }
- // the editor's spectrum analyser listens to this slot: its input
- const auto tapped = times->analyzer.key.load (std::memory_order_relaxed) == (int) key;
- if (tapped)
- {
- auto& tap = times->analyzer;
- const auto w = tap.written.load (std::memory_order_relaxed);
- for (int k = 0; k < n; ++k)
- tap.pre[(std::size_t) ((w + (std::uint32_t) k) & (ProcessTimes::AnalyzerTap::size - 1))].store (0.5f * (l[k] + r[k]), std::memory_order_relaxed);
- // round 166f: and its sidechain (Facet EQ draws its comparative target)
- const auto source = slots[i].sidechain;
- const bool side = source >= 0 && source != track && source < (int) state.song.mixer.size();
- const bool sounding = side && (std::size_t) source < trackActive.size() && trackActive[(std::size_t) source] != 0;
- const auto* sl = sounding ? trackBuffer (source, 0) : nullptr;
- const auto* sr = sounding ? trackBuffer (source, 1) : nullptr;
- for (int k = 0; k < n; ++k)
- tap.side[(std::size_t) ((w + (std::uint32_t) k) & (ProcessTimes::AnalyzerTap::size - 1))].store (sl != nullptr ? 0.5f * (sl[k] + sr[k]) : 0.0f, std::memory_order_relaxed);
- tap.sideOn.store (side, std::memory_order_relaxed);
- }
- // dry/wet: keep the dry signal to mix later
- // (not the hardware insert: its dry path is delayed by its round trip inside it, so the slot's mix scales its own
- // dry/wet instead — round 164d)
- const auto partial = wet < 0.999f && slots[i].kind != EffectKind::hardwareInsert;
- if (partial)
- {
- std::memcpy (trackScratch (track, 0), l, sizeof (float) * (std::size_t) n);
- std::memcpy (trackScratch (track, 1), r, sizeof (float) * (std::size_t) n);
- }
- const UnitTimer timer (profiling.load (std::memory_order_relaxed), units->effectNs[std::min (key, units->effectNs.size() - 1)],
- &units->effectMaxNs[std::min (key, units->effectNs.size() - 1)]);
- // the hardware insert (round 164d): this piece's device input and output buses (none during an export)
- if (slots[i].kind == EffectKind::hardwareInsert || slots[i].kind == EffectKind::patcher) // (a Patcher hands them to its nodes, round 165c)
- effects[i]->setHardwareIo (liveInput, hwOut != nullptr && hwOut->active.load (std::memory_order_relaxed) ? hwOut : nullptr, liveInputOffset);
- // Fruity Send / Patcher / Transmitter / Transporter: what it sends goes to its own buffer, pulled by the destination insert
- // (processed later)
- if (slots[i].kind == EffectKind::fruitySend || slots[i].kind == EffectKind::patcher || slots[i].kind == EffectKind::transmitter
- || slots[i].kind == EffectKind::flTransporter)
- {
- for (int o = 0; o < maxSlotSends; ++o)
- {
- const auto key = i * (std::size_t) maxSlotSends + (std::size_t) o;
- const auto tap = (std::size_t) track < state.slotTap.size() && key < state.slotTap[(std::size_t) track].size()
- ? state.slotTap[(std::size_t) track][key]
- : -1;
- if (tap >= 0 && tap < RackState::maxSendTaps)
- {
- std::memset (tapBuffer (tap, 0), 0, sizeof (float) * (std::size_t) n);
- std::memset (tapBuffer (tap, 1), 0, sizeof (float) * (std::size_t) n);
- effects[i]->setExtraOutput (o, tapBuffer (tap, 0), tapBuffer (tap, 1));
- tapActive[(std::size_t) tap] = 1;
- }
- else
- effects[i]->setExtraOutput (o, nullptr, nullptr);
- }
- }
- if (slots[i].kind == EffectKind::hardwareInsert && wet < 0.999f)
- {
- auto params = slots[i].params; // copy without allocation
- if (state.hasAutomation)
- {
- const auto& autos = state.effectParamAuto[(std::size_t) track][i];
- const auto used = std::min (params.size(), effectInfo (slots[i].kind).params.size());
- for (std::size_t p = 0; p < used; ++p)
- params[p] = automated (autos[p], params[p]);
- }
- params[(std::size_t) hwinsert::dryWet] *= wet;
- timed (key, [&] { effects[i]->process (l, r, n, params, bpm); });
- }
- else if (state.hasAutomation && ! isPlugin)
- {
- auto params = slots[i].params; // copy without allocation
- const auto& autos = state.effectParamAuto[(std::size_t) track][i];
- const auto used = std::min (params.size(), effectInfo (slots[i].kind).params.size());
- for (std::size_t p = 0; p < used; ++p)
- params[p] = automated (autos[p], params[p]);
- effects[i]->setSongPosition (chunkTick);
- effects[i]->setPlaying (chunkPlaying);
- effects[i]->setHeldNotes (heldNotes.data(), maxChannels);
- setSidechain (state, slots[i], *effects[i], track);
- timed (key, [&] { effects[i]->process (l, r, n, params, bpm); });
- }
- else
- {
- effects[i]->setSongPosition (chunkTick);
- effects[i]->setPlaying (chunkPlaying);
- effects[i]->setHeldNotes (heldNotes.data(), maxChannels);
- setSidechain (state, slots[i], *effects[i], track);
- timed (key, [&] { effects[i]->process (l, r, n, slots[i].params, bpm); });
- }
- // an effect that output NaN / inf (an unstable setting): this block of it is muted and its state reset, so the
- // broken values never reach the rest of the mix (they would stay in every filter and feedback path after it)
- if (! finiteBlock (l, r, n))
- {
- std::memset (l, 0, sizeof (float) * (std::size_t) n);
- std::memset (r, 0, sizeof (float) * (std::size_t) n);
- effects[i]->reset();
- nonFiniteBlocks.fetch_add (1, std::memory_order_relaxed);
- }
- if (partial)
- {
- const auto* dl = trackScratch (track, 0);
- const auto* dr = trackScratch (track, 1);
- for (int k = 0; k < n; ++k)
- {
- l[k] = dl[k] + (l[k] - dl[k]) * wet;
- r[k] = dr[k] + (r[k] - dr[k]) * wet;
- }
- }
- // ... and its output
- if (tapped)
- {
- auto& tap = times->analyzer;
- const auto w = tap.written.load (std::memory_order_relaxed);
- for (int k = 0; k < n; ++k)
- tap.post[(std::size_t) ((w + (std::uint32_t) k) & (ProcessTimes::AnalyzerTap::size - 1))].store (0.5f * (l[k] + r[k]), std::memory_order_relaxed);
- tap.sampleRate.store (sampleRate, std::memory_order_relaxed);
- tap.written.store (w + (std::uint32_t) n, std::memory_order_release);
- }
- // live meters for the editor (the mixing suite's gain reduction, LYNX Tune's pitch, Facet EQ's dynamic gains)
- if (hasLiveMeters (slots[i].kind) && key < (std::size_t) maxMixerTracks * maxEffectSlots)
- for (int o = 0; o < ProcessTimes::meterOutputs; ++o)
- times->effectMeters[key * ProcessTimes::meterOutputs + (std::size_t) o].store (effects[i]->controlOutput (o), std::memory_order_relaxed);
- // the effect changed the signal: measure again (it is the next slot's input)
- audible = anyAbove (l, r, n, silenceLevel) ? 1 : 0;
- if (audible == 1 && key < smartDisable.size())
- smartDisable[key].silentSamples = 0; // tail (reverb, delay): keep processing
- }
- }
- std::uint64_t RackRenderer::nonFiniteEffectBlocks() noexcept
- {
- return nonFiniteBlocks.load (std::memory_order_relaxed);
- }
- void RackRenderer::process (const RackState* state,
- bool playing,
- double startTick,
- int numSamples,
- float* left,
- float* right,
- LiveEventQueue* live,
- MeterBank* meters,
- LiveEventQueue* moreLive) noexcept
- {
- const ScopedFlushDenormals noDenormals;
- const UnitTimer timer (profiling.load (std::memory_order_relaxed), units->processNs);
- if (profiling.load (std::memory_order_relaxed))
- units->processedSamples.fetch_add ((std::uint64_t) std::max (0, numSamples), std::memory_order_relaxed);
- std::memset (left, 0, sizeof (float) * (std::size_t) numSamples);
- std::memset (right, 0, sizeof (float) * (std::size_t) numSamples);
- events.clear();
- if (state == nullptr || blockCapacity == 0)
- return;
- const auto timing = times->enabled.load (std::memory_order_relaxed);
- const auto wallStart = timing ? ProcessTimes::now() : 0;
- // A snapshot built before the device opened may have another rate. The instruments'
- // prepare() does not allocate, so it can run here.
- if (state != lastState && state->sampleRate != sampleRate)
- {
- for (auto& instrument : state->instruments)
- instrument->prepare (sampleRate);
- for (auto& track : state->effects)
- for (auto& effect : track)
- effect->prepare (sampleRate);
- }
- lastState = state; // so prepare() knows which instruments to reconfigure
- // Stopping, or jumping to another position (seek/rewind), releases held notes:
- // their "off" events will no longer happen.
- const auto jumped = playing && wasPlaying && std::abs (startTick - expectedTick) > 1.0;
- if ((wasPlaying && ! playing) || (playing && ! wasPlaying))
- {
- // the arpeggiators / echoes ran on the live clock while stopped (round 149): they restart on the song's
- liveRoutedCount = 0;
- for (auto& arp : state->arpeggiators)
- if (arp != nullptr)
- arp->reset();
- for (auto& echo : state->echoes)
- if (echo != nullptr)
- echo->reset();
- }
- if ((wasPlaying && ! playing) || jumped)
- {
- for (auto& instrument : state->instruments)
- if (jumped)
- instrument->allNotesOff();
- else
- instrument->transportStopped(); // (a hosted plugin: FL's "Send All Notes Off when playback stops")
- heldNotes.fill ({});
- clearPolyLayers();
- for (auto& arp : state->arpeggiators)
- if (arp != nullptr)
- arp->reset();
- for (auto& echo : state->echoes)
- if (echo != nullptr)
- echo->reset();
- }
- // FL resets its generators when playback starts: a MIDI Out sends its bank, patch and controls again
- // (REA_rodada167i §1.4)
- if (playing && ! wasPlaying)
- for (std::size_t c = 0; c < state->song.channels.size() && c < state->instruments.size(); ++c)
- if (state->song.channels[c].instrument == InstrumentKind::midiOut && state->instruments[c] != nullptr)
- state->instruments[c]->transportStarted();
- // started playing or jumped: audio clips in progress must start mid-way
- resumeAudio = playing && (! wasPlaying || jumped);
- // FL's "Play truncated notes on transport": at the start of playback, and after the user moves the position while
- // playing (FL's set-position FUN_011e7a10 → FUN_011e78a0 → FUN_011e7470 with its catch-up flag set, round 167k),
- // the notes already sounding there play. The loop's wrap is not a set-position and chases nothing.
- const auto userJump = jumped && userJumpSerial != seenUserJumpSerial;
- seenUserJumpSerial = userJumpSerial;
- chaseNotes = playing && (! wasPlaying || userJump) && playTruncatedOnTransport;
- // FL's "Reset plugins on transport" (FUN_011e7a10 → FUN_011c8a40 mode 2 → FUN_012b5210, every registered plugin's
- // reset, generators included): the effects' tails and buffers are cleared and the plugin generators stop when
- // playback starts or jumps (round 167k: instruments too, hosted plugins with their wrapper's reset option)
- if (resetPluginsOnTransport && playing && (! wasPlaying || jumped))
- {
- for (auto& slots : state->effects)
- for (auto& effect : slots)
- if (effect != nullptr)
- effect->transportReset();
- for (auto& instrument : state->instruments)
- if (instrument != nullptr)
- instrument->transportReset();
- }
- wasPlaying = playing;
- // tempo automation: the tempo holds for the whole block (evaluated at its start)
- bpm = tempoOverride > 0.0 ? tempoOverride : state->song.bpm;
- if (tempoOverride <= 0.0 && state->tempoAutomation >= 0 && state->song.mode == PlayMode::song && ! isSlotOverridden (state->tempoAutomation))
- {
- const auto length = (double) std::max<std::int64_t> (1, state->schedule.songLength);
- const auto& clips = state->automationClips[(std::size_t) state->tempoAutomation];
- if (! clips.empty())
- bpm = tempoFromValue (automationValueAt (state->song, state->song.automations[(std::size_t) state->tempoAutomation], clips,
- std::fmod (std::max (0.0, startTick), length)));
- }
- const auto tps = ticksPerSample (bpm, sampleRate);
- expectedTick = startTick + numSamples * tps;
- for (auto* queue : { live, moreLive })
- {
- if (queue == nullptr)
- continue;
- NoteEvent e;
- while (queue->pop (e))
- {
- if (e.type == NoteEvent::Type::choke && e.channel < 0) // panic: every channel stops sounding
- {
- for (auto& instrument : state->instruments)
- if (instrument != nullptr)
- instrument->panic();
- heldNotes.fill ({});
- for (auto& order : voiceOrders)
- order.clear();
- clearPolyLayers();
- continue;
- }
- if (e.channel < 0 || e.channel >= (int) state->instruments.size())
- continue;
- if (e.type == NoteEvent::Type::control)
- {
- applyLiveControl (*state, e);
- continue;
- }
- auto& instrument = *state->instruments[(std::size_t) e.channel];
- auto& held = heldNotes[(std::size_t) std::min (e.channel, maxChannels - 1)];
- const auto bit = std::uint64_t { 1 } << (e.key & 63);
- const auto inRange = e.key >= 0 && e.key < 128;
- // played on a MIDI Out channel: the plugins on its port get the note with its MIDI channel
- if (state->hasMidiRoutes && (std::size_t) e.channel < state->midiRoutesOut.size()
- && ((std::size_t) e.channel >= state->vfxPort.size() || state->vfxPort[(std::size_t) e.channel] < 0))
- for (const auto& route : state->midiRoutesOut[(std::size_t) e.channel])
- if (! route.portOnly && route.other < (int) state->instruments.size() && state->instruments[(std::size_t) route.other] != nullptr)
- {
- auto& target = *state->instruments[(std::size_t) route.other];
- const auto midiChannel = state->mapNoteColour[(std::size_t) e.channel] != 0 ? (int) (e.colour & 15) : route.midiChannel;
- if (e.type == NoteEvent::Type::on)
- target.noteOnPort (route.port, e.key, e.velocity, midiChannel);
- else if (e.type == NoteEvent::Type::off)
- target.noteOffPort (route.port, e.key, midiChannel);
- }
- // and the mixer-slot Patchers on its port (round 162)
- // (notes that go through the channel's note path reach them in processChunk)
- if (state->hasEffectMidiRoutes && ! routesLiveNotes (*state, e.channel))
- deliverToEffects (*state, e);
- // the channel's arpeggiator / echo delay / tracking play it (in processChunk's first chunk)
- if ((e.type == NoteEvent::Type::on || e.type == NoteEvent::Type::off) && routesLiveNotes (*state, e.channel))
- {
- if (liveRoutedCount < (int) liveRouted.size())
- {
- e.sampleOffset = 0;
- liveRouted[(std::size_t) liveRoutedCount++] = e;
- }
- continue;
- }
- auto& order = voiceOrders[(std::size_t) std::min (e.channel, maxChannels - 1)];
- if (e.type == NoteEvent::Type::on && (std::size_t) e.channel < state->song.channels.size()
- && ! state->song.channels[(std::size_t) e.channel].inKeyRegion (e.key))
- continue; // outside the channel's key region (round 164)
- const auto& def = state->song.channels;
- auto& poly = polyLayers[(std::size_t) std::min (e.channel, maxChannels - 1)];
- if (e.type == NoteEvent::Type::on)
- {
- if (inRange)
- {
- held[(std::size_t) (e.key >> 6)] |= bit;
- struckNotes[(std::size_t) std::min (e.channel, maxChannels - 1)][(std::size_t) (e.key >> 6)] |= bit;
- }
- if ((std::size_t) e.channel >= def.size())
- {
- instrument.noteOn (e.key, e.velocity);
- continue;
- }
- const auto& c = def[(std::size_t) e.channel];
- // a played note is tuned like the song's notes: Misc › Fine tune and the main pitch (round 162, issue
- // #9: the fine tune did nothing to the keys played live)
- const auto cents = c.misc.fineTune + (c.misc.mainPitch ? state->song.masterPitch : 0.0f);
- // FL's channel Mono / Porta (round 168l), as for the song's notes
- const auto mode = polyMode (c, instrument);
- const auto glideSamples = (float) (channelpoly::glideBeats (mode.portaBeats) * ppq / std::max (1.0e-9, tps));
- const auto on = poly.noteOn (mode, e.key, cents, false, e.colour);
- if (on.release >= 0)
- {
- order.noteOff (on.release);
- instrument.noteOff (on.release);
- }
- if (! on.start)
- {
- if (on.slide)
- instrument.slideTo (e.key, cents, glideSamples);
- continue;
- }
- if (const auto limit = voiceLimitFor (c.maxPolyphony, channelMono (c)); limit > 0)
- if (const auto victim = order.noteOn (e.key, limit); victim >= 0)
- instrument.noteOff (victim);
- if (const auto from = on.glide ? on.glideFrom : 0.0f; cents != 0.0f || from != 0.0f)
- instrument.setNextNoteExpression (0.0f, cents + from, false);
- HostSlide::pendingGlide() = { on.glide ? on.glideFrom : 0.0f, on.glide ? glideSamples : 0.0f, false };
- instrument.noteOn (e.key, e.velocity);
- HostSlide::pendingGlide() = {};
- }
- else
- {
- if (inRange)
- held[(std::size_t) (e.key >> 6)] &= ~bit;
- if ((std::size_t) e.channel >= def.size())
- {
- order.noteOff (e.key);
- instrument.noteOff (e.key);
- continue;
- }
- const auto mode = polyMode (def[(std::size_t) e.channel], instrument);
- const auto off = poly.noteOff (mode, e.key, e.colour);
- if (off.slide)
- instrument.slideTo (off.key, off.cents, (float) (channelpoly::glideBeats (mode.portaBeats) * ppq / std::max (1.0e-9, tps)));
- if (off.release >= 0)
- {
- order.noteOff (off.release);
- instrument.noteOff (off.release);
- }
- }
- }
- }
- // Blocks larger than the mixer buffers are done in pieces. With automation, small
- // pieces so values move smoothly.
- const auto chunk = std::min (blockCapacity, state->hasAutomation ? 128 : chunkCapacity);
- for (int done = 0; done < numSamples;)
- {
- auto n = std::min (chunk, numSamples - done);
- // pattern events take effect at their own sample, as in FL (REA_rodada85 §3): a chunk ends where one falls
- if (playing && ! state->eventLanes.empty() && tps > 0.0)
- {
- const auto ahead = ticksToNextEvent (*state, startTick + done * tps);
- if (ahead < (double) n * tps)
- n = std::max (1, (int) std::ceil (ahead / tps - 1.0e-9));
- }
- liveInputOffset = done;
- processChunk (*state, playing, startTick + done * tps, n, left + done, right + done, meters);
- resumeAudio = false;
- chaseNotes = false;
- done += n;
- }
- publishSoundingKeys ((int) state->song.channels.size());
- if (timing)
- {
- times->wallNs.fetch_add (ProcessTimes::now() - wallStart, std::memory_order_relaxed);
- times->audioNs.fetch_add ((std::uint64_t) ((double) numSamples / sampleRate * 1.0e9), std::memory_order_relaxed);
- }
- }
- void RackRenderer::publishSoundingKeys (int channels) noexcept
- {
- channels = std::clamp (channels, 0, maxChannels);
- const auto top = std::max (channels, publishedTop);
- publishedTop = 0;
- for (int i = 0; i < top; ++i)
- {
- auto& was = publishedNotes[(std::size_t) i];
- NoteMask now {};
- if (i < channels)
- {
- const auto& held = heldNotes[(std::size_t) i];
- auto& struck = struckNotes[(std::size_t) i];
- now = { held[0] | struck[0], held[1] | struck[1] };
- struck = {};
- }
- if (now[0] != 0 || now[1] != 0)
- publishedTop = i + 1;
- if (now == was)
- continue;
- was = now;
- sounding[(std::size_t) i][0].store (now[0], std::memory_order_relaxed);
- sounding[(std::size_t) i][1].store (now[1], std::memory_order_relaxed);
- }
- }
- double RackRenderer::ticksToNextEvent (const RackState& state, double tick) noexcept
- {
- const auto& song = state.song;
- auto best = std::numeric_limits<double>::infinity();
- // first event tick of `events` strictly after `t` (events sorted by tick)
- auto after = [] (const PatternEvents& events, double t) {
- auto it = std::upper_bound (events.events.begin(), events.events.end(), t,
- [] (double x, const PatternEvent& e) { return x < (double) e.tick; });
- return it == events.events.end() ? std::numeric_limits<double>::infinity() : (double) it->tick;
- };
- if (song.mode != PlayMode::song)
- {
- const auto current = song.currentPattern;
- if (current < 0 || current >= (int) song.patterns.size())
- return best;
- const auto& pattern = song.patterns[(std::size_t) current];
- const auto length = (double) std::max<std::int64_t> (1, pattern.effectiveLength (song.beatsPerBar));
- const auto t = std::fmod (std::max (0.0, tick), length);
- for (const auto& lane : state.eventLanes)
- if (lane.pattern == current)
- {
- const auto& events = pattern.events[(std::size_t) lane.lane];
- best = std::min (best, after (events, t) - t);
- if (! events.events.empty()) // the loop restarts at the first event's value
- best = std::min (best, length - t);
- }
- return best;
- }
- const auto length = (double) std::max<std::int64_t> (1, state.schedule.songLength);
- const auto t = std::fmod (std::max (0.0, tick), length);
- for (const auto& lane : state.eventLanes)
- {
- const auto& events = song.patterns[(std::size_t) lane.pattern].events[(std::size_t) lane.lane];
- for (auto i : state.patternClips[(std::size_t) lane.pattern])
- {
- const auto& c = song.playlist[(std::size_t) i];
- if ((double) c.end() <= t)
- continue;
- if ((double) c.start > t)
- {
- best = std::min (best, (double) c.start - t);
- break;
- }
- const auto period = (std::size_t) lane.pattern < state.schedule.patternLength.size() ? state.schedule.patternLength[(std::size_t) lane.pattern] : 0;
- const auto local = patternTickInClip (c, t, period);
- auto ahead = after (events, local) - local;
- if (stretchedClipsLoop() && period > 0 && c.offset + c.length > period) // the next pass restarts at the first event's value
- ahead = std::min (ahead, (double) period - local);
- best = std::min (best, std::min (t + ahead, (double) c.end()) - t);
- }
- }
- return best;
- }
- void RackRenderer::applyLiveControl (const RackState& state, const NoteEvent& e) noexcept
- {
- // a controller's pitch wheel, mod wheel (CC 1), other CCs and channel pressure played into a channel (round 162,
- // issue #9): hosted plugins (and the SoundFont player) get the MIDI message; LYNX's own instruments bend by the
- // channel's pitch range (FL's Misc › Pitch range, default ±2 semitones) and take the CCs they know
- auto* instrument = state.instruments[(std::size_t) e.channel].get();
- if (instrument == nullptr)
- return;
- std::uint8_t m[3] {};
- auto size = 3;
- const auto value = std::isfinite (e.velocity) ? e.velocity : 0.0f;
- if (e.key == NoteEvent::controlPitchBend)
- {
- const auto v = (int) std::clamp (std::lround ((double) (value + 1.0f) * 8192.0), 0L, 16383L);
- m[0] = 0xE0, m[1] = (std::uint8_t) (v & 127), m[2] = (std::uint8_t) (v >> 7);
- }
- else if (e.key == NoteEvent::controlPressure)
- {
- m[0] = 0xD0, m[1] = (std::uint8_t) std::clamp (std::lround (value * 127.0f), 0L, 127L);
- size = 2;
- }
- else if (e.key == NoteEvent::controlProgram) // (round 165c: a VFX Sequencer selects its sequence, plugins their preset)
- {
- m[0] = 0xC0, m[1] = (std::uint8_t) std::clamp (std::lround (value * 127.0f), 0L, 127L);
- size = 2;
- }
- else if (e.key >= 0 && e.key < 128)
- m[0] = 0xB0, m[1] = (std::uint8_t) e.key, m[2] = (std::uint8_t) std::clamp (std::lround (value * 127.0f), 0L, 127L);
- else
- return;
- instrument->midiMessage (m, size);
- if (e.key == NoteEvent::controlPitchBend)
- {
- const auto range = (std::size_t) e.channel < state.song.channels.size() ? state.song.channels[(std::size_t) e.channel].misc.pitchRange : 2;
- instrument->pitchBend (std::clamp (value, -1.0f, 1.0f) * (float) std::max (0, range));
- }
- else if (e.key < 128)
- instrument->controlChange (e.key, std::clamp (value, 0.0f, 1.0f));
- // a MIDI Out channel: the plugins on its port get it on its MIDI channel
- if (state.hasMidiRoutes && (std::size_t) e.channel < state.midiTargets.size())
- for (const auto t : state.midiTargets[(std::size_t) e.channel])
- if (t >= 0 && t < (int) state.instruments.size() && state.instruments[(std::size_t) t] != nullptr)
- {
- std::uint8_t routed[3] { (std::uint8_t) ((m[0] & 0xF0) | (state.midiOutChannel[(std::size_t) e.channel] & 15)), m[1], m[2] };
- state.instruments[(std::size_t) t]->midiMessage (routed, size);
- }
- }
- void RackRenderer::expandLayers (const RackState& state) noexcept
- {
- const auto& channels = state.song.channels;
- const auto count = events.size();
- auto added = false;
- for (int i = 0; i < count; ++i)
- {
- const auto e = events[i];
- if (e.channel < 0 || e.channel >= (int) channels.size() || e.channel >= maxChannels)
- continue;
- const auto& l = channels[(std::size_t) e.channel];
- if (l.instrument != InstrumentKind::layer || l.layer.children.empty())
- continue;
- const auto key = std::clamp (e.key, 0, 127);
- auto* picked = layerPick.empty() ? nullptr : &layerPick[(std::size_t) e.channel * 128 + (std::size_t) key];
- auto child = e;
- child.pan = std::clamp (e.pan + l.pan, -1.0f, 1.0f);
- child.cents = e.cents + layer::pitchCents (l);
- const auto one = l.layer.random || l.layer.sequential;
- if (e.type == NoteEvent::Type::on)
- {
- if (l.muted)
- continue;
- std::array<layer::Pick, 64> picks;
- const auto n = layer::pick (l, channels, key, layerRandom, layerTurn[(std::size_t) e.channel], picks.data(), (int) picks.size());
- if (one && picked != nullptr)
- *picked = (std::int16_t) (n > 0 ? picks[0].child : -1);
- const auto level = layer::levelGain (l);
- for (int k = 0; k < n; ++k)
- {
- child.channel = picks[(std::size_t) k].child;
- child.gain = e.gain * level * picks[(std::size_t) k].gain;
- events.add (child);
- added = true;
- }
- }
- else if (e.type == NoteEvent::Type::off && one)
- {
- if (picked != nullptr && *picked >= 0)
- {
- child.channel = *picked;
- *picked = -1;
- events.add (child);
- added = true;
- }
- }
- else if (e.type == NoteEvent::Type::off || e.type == NoteEvent::Type::slide || e.type == NoteEvent::Type::choke)
- for (const auto c : l.layer.children)
- if (c >= 0 && c < (int) channels.size() && c != e.channel && channels[(std::size_t) c].instrument != InstrumentKind::layer)
- {
- child.channel = c;
- events.add (child);
- added = true;
- }
- }
- if (added)
- events.sortByOffset();
- }
- bool RackRenderer::routesLiveNotes (const RackState& state, int channel) const noexcept
- {
- const auto c = (std::size_t) channel;
- if (channel < 0 || c >= state.song.channels.size())
- return false;
- if (state.song.channels[c].instrument == InstrumentKind::layer) // (its children play the note: round 164)
- return true;
- const auto& misc = state.song.channels[c].misc;
- return (c < state.arpeggiators.size() && state.arpeggiators[c] != nullptr) || (c < state.echoes.size() && state.echoes[c] != nullptr)
- || misc.key.pan != 0.0f || misc.velocity.pan != 0.0f
- || (misc.addToKey && misc.rootNote != 60 && state.song.channels[c].instrument != InstrumentKind::sampler)
- || (c < state.vfxPort.size() && state.vfxPort[c] >= 0); // (a VFX channel's processor takes them in the chunk)
- }
- void RackRenderer::runVfxChannels (const RackState& state, bool playing, int numSamples) noexcept
- {
- vfx::Transport transport;
- transport.sampleRate = sampleRate;
- transport.bpm = chunkTps * sampleRate * 60.0 / (double) ppq; // (the tempo at this chunk)
- transport.ticks = chunkStartTick; // (stopped: the live clock, as the arpeggiator)
- transport.playing = playing;
- for (const auto c : state.vfxChannels)
- {
- if ((std::size_t) c >= state.instruments.size() || state.instruments[(std::size_t) c] == nullptr)
- continue;
- auto* processor = state.instruments[(std::size_t) c]->noteProcessor();
- if (processor == nullptr)
- continue;
- vfxIn->clear();
- vfxOut->clear();
- const auto count = events.size();
- for (int k = 0; k < count; ++k)
- {
- auto& e = events.at (k);
- if (e.channel != c || e.port != -1)
- continue;
- if (e.type == NoteEvent::Type::on || e.type == NoteEvent::Type::off)
- {
- vfx::Event in;
- in.type = e.type == NoteEvent::Type::on ? vfx::Event::Type::on : vfx::Event::Type::off;
- in.offset = e.sampleOffset;
- in.voice.key = e.key;
- in.voice.velocity = e.velocity;
- in.voice.pan = e.pan;
- in.voice.cents = e.cents;
- in.voice.modX = e.modX;
- in.voice.modY = e.modY;
- in.voice.release = e.release;
- in.voice.colour = e.colour & 15;
- in.voice.porta = e.porta;
- in.id = vfx::inputId (e.key, in.voice.colour);
- vfxIn->add (in);
- }
- e.port = -2; // (taken by the processor)
- }
- processor->process (vfxIn->size() > 0 ? &(*vfxIn)[0] : nullptr, vfxIn->size(), numSamples, transport, *vfxOut);
- const auto port = state.vfxPort[(std::size_t) c];
- for (int k = 0; k < vfxOut->size(); ++k)
- {
- const auto& o = (*vfxOut)[k];
- if (o.type == vfx::Event::Type::program) // (a VFX Sequencer passing a program change on: the plugins on its port)
- {
- if ((std::size_t) c < state.midiTargets.size())
- for (const auto t : state.midiTargets[(std::size_t) c])
- if (t >= 0 && t < (int) state.instruments.size() && state.instruments[(std::size_t) t] != nullptr)
- {
- const std::uint8_t m[2] { 0xC0, (std::uint8_t) std::clamp ((int) o.value, 0, 127) };
- state.instruments[(std::size_t) t]->midiMessage (m, 2);
- }
- continue;
- }
- if (port + o.output >= patchMidiPorts)
- continue;
- NoteEvent e;
- e.type = o.type == vfx::Event::Type::on ? NoteEvent::Type::on : o.type == vfx::Event::Type::off ? NoteEvent::Type::off : NoteEvent::Type::voiceProp;
- e.prop = (std::uint8_t) o.prop;
- if (e.type == NoteEvent::Type::voiceProp) // (round 165c: the voice's property over time, on its port too)
- {
- e.velocity = o.value;
- e.channel = c;
- e.sampleOffset = std::clamp (o.offset, 0, std::max (0, numSamples - 1));
- e.key = o.voice.key;
- e.colour = (std::int8_t) (o.voice.colour & 15);
- e.port = (std::int16_t) (port + o.output);
- events.add (e);
- continue;
- }
- e.channel = c;
- e.sampleOffset = std::clamp (o.offset, 0, std::max (0, numSamples - 1));
- e.key = o.voice.key;
- e.velocity = o.voice.velocity;
- e.pan = o.voice.pan;
- e.cents = o.voice.cents;
- e.modX = o.voice.modX;
- e.modY = o.voice.modY;
- e.release = o.voice.release;
- e.colour = (std::int8_t) (o.voice.colour & 15);
- e.porta = o.voice.porta;
- e.port = (std::int16_t) (port + o.output);
- events.add (e);
- }
- }
- events.sortByOffset();
- }
- void RackRenderer::processChunk (const RackState& state, bool playing, double startTick, int numSamples,
- float* left, float* right, MeterBank* meters) noexcept
- {
- chunkTick = startTick;
- chunkPlaying = playing;
- updatePdcBypass (state, playing);
- const auto& song = state.song;
- const auto tracks = std::max<int> (1, (int) song.mixer.size());
- if (state.hasAutomation) // (a hair later: a chunk that starts on a pattern event's sample takes its value)
- updateAutomation (state, startTick + 1.0e-6);
- chunkTps = ticksPerSample (bpm, sampleRate);
- chunkStartTick = playing ? startTick : liveTick; // stopped: live notes' arpeggios run on their own clock
- if (! playing)
- liveTick += numSamples * chunkTps;
- // hosted plugins read the transport from here (PluginHost's play head)
- {
- auto& timeline = hostTimeline();
- timeline.tick.store (hostPositionTick (song, startTick, state.schedule.songLength), std::memory_order_relaxed);
- timeline.bpm.store (bpm, std::memory_order_relaxed);
- timeline.sampleRate.store (sampleRate, std::memory_order_relaxed);
- timeline.beatsPerBar.store (std::max (1, song.beatsPerBar), std::memory_order_relaxed);
- timeline.playing.store (playing, std::memory_order_relaxed);
- timeline.looping.store (song.hasLoop(), std::memory_order_relaxed);
- timeline.loopStart.store ((double) song.loopStart, std::memory_order_relaxed);
- timeline.loopEnd.store ((double) song.loopEnd, std::memory_order_relaxed);
- }
- for (int t = 0; t < tracks; ++t)
- {
- std::memset (trackBuffer (t, 0), 0, sizeof (float) * (std::size_t) numSamples);
- std::memset (trackBuffer (t, 1), 0, sizeof (float) * (std::size_t) numSamples);
- }
- events.clear();
- const auto songPlaying = playing && song.mode == PlayMode::song;
- if (songPlaying)
- {
- scheduleSong (song, startTick, numSamples, ticksPerSample (bpm, sampleRate), events, resumeAudio, &state.schedule,
- liveInput == nullptr || ! liveInput->linearSong.load (std::memory_order_relaxed), chaseNotes);
- }
- // Performance mode (round 165m): the launched clips, on the Player's steady clock (the transport's ticks from
- // where playback started, never wrapping with the song loop)
- if (songPlaying && (! state.performanceZone.empty() || perfRunning))
- {
- if (! perfRunning)
- perfClock = startTick;
- perfRunning = true;
- const auto tps = ticksPerSample (bpm, sampleRate);
- perfPlayer.process (song, state.performanceZone, perfLive, perfClock, startTick, numSamples * tps, true);
- schedulePerformance (song, perfPlayer, perfClock, numSamples, tps, events, &state.schedule);
- perfClock += numSamples * tps;
- }
- else if (perfRunning || (perfLive != nullptr && perfLive->anyPlaying.load (std::memory_order_relaxed)))
- {
- perfPlayer.process (song, state.performanceZone, perfLive, perfClock, startTick, 0.0, false);
- perfRunning = false;
- }
- else if (perfLive != nullptr)
- {
- perf::Command c;
- while (perfLive->commands.pop (c)) {} // nothing launches while stopped or in Pattern mode
- }
- if (! songPlaying && playing && ! song.patterns.empty())
- {
- const auto patternIndex = std::clamp (song.currentPattern, 0, (int) song.patterns.size() - 1);
- const auto& pattern = song.patterns[(std::size_t) patternIndex];
- schedulePatternLoop (pattern, pattern.effectiveLength (song.beatsPerBar), startTick, numSamples,
- ticksPerSample (bpm, sampleRate), events, song.swing, &song.channels, chaseNotes);
- }
- // FL's wrapper "Output port" (round 149): the notes a plugin sent out in the previous chunk play the plugins on
- // that port, at this chunk's start
- if (state.hasMidiOutputs)
- {
- std::array<Instrument::MidiOutNote, 128> notes;
- for (std::size_t c = 0; c < state.midiOutTargets.size() && c < state.instruments.size(); ++c)
- {
- if (state.midiOutTargets[c].empty())
- continue;
- const auto count = state.instruments[c]->takeMidiOutput (notes.data(), (int) notes.size());
- for (int k = 0; k < count; ++k)
- for (const auto t : state.midiOutTargets[c])
- {
- NoteEvent e;
- e.type = notes[(std::size_t) k].velocity > 0.0f ? NoteEvent::Type::on : NoteEvent::Type::off;
- e.channel = t;
- e.key = notes[(std::size_t) k].key;
- e.velocity = notes[(std::size_t) k].velocity;
- e.sampleOffset = 0;
- events.add (e);
- }
- }
- events.sortByOffset();
- }
- // generator Patchers' VFX nodes cabled to "To LYNX" MIDI ports (round 165c): what they played in the previous
- // chunk goes out on those ports at this chunk's start (as a VFX channel's outputs do)
- if (! state.portEventChannels.empty())
- {
- std::array<Instrument::PortEvent, 128> sent;
- auto added = false;
- for (const auto c : state.portEventChannels)
- {
- if ((std::size_t) c >= state.instruments.size() || state.instruments[(std::size_t) c] == nullptr)
- continue;
- const auto count = state.instruments[(std::size_t) c]->takePortEvents (sent.data(), (int) sent.size());
- for (int k = 0; k < count; ++k)
- {
- const auto& p = sent[(std::size_t) k];
- NoteEvent e;
- e.type = p.prop >= 0 ? NoteEvent::Type::voiceProp : p.value > 0.0f ? NoteEvent::Type::on : NoteEvent::Type::off;
- e.channel = c;
- e.key = p.key;
- e.velocity = p.value;
- e.prop = (std::uint8_t) std::max (0, p.prop);
- e.pan = p.pan;
- e.cents = p.cents;
- e.modX = p.modX;
- e.modY = p.modY;
- e.release = p.release;
- e.colour = (std::int8_t) (p.channel & 15);
- e.port = (std::int16_t) p.port;
- events.add (e);
- added = true;
- }
- }
- if (added)
- events.sortByOffset();
- }
- // Dashboards (round 165, FL's Dashboard): the MIDI their controls and patch send reaches the plugins on their port
- for (const auto c : state.dashboardChannels)
- {
- if ((std::size_t) c >= state.instruments.size() || state.instruments[(std::size_t) c] == nullptr)
- continue;
- std::array<std::uint32_t, midiout::maxResetMessages + 8> sent; // (a MIDI Out's reset: every active control at once)
- const auto count = state.instruments[(std::size_t) c]->takeMidiMessages (sent.data(), (int) sent.size());
- if (count <= 0 || (std::size_t) c >= state.midiTargets.size())
- continue;
- for (int k = 0; k < count; ++k)
- {
- const auto m = sent[(std::size_t) k];
- const std::uint8_t bytes[3] { (std::uint8_t) (m & 0xff), (std::uint8_t) ((m >> 8) & 127), (std::uint8_t) ((m >> 16) & 127) };
- const auto size = (bytes[0] & 0xE0) == 0xC0 ? 2 : 3; // (program change, channel pressure: one data byte)
- for (const auto t : state.midiTargets[(std::size_t) c])
- if (t >= 0 && t < (int) state.instruments.size() && state.instruments[(std::size_t) t] != nullptr)
- state.instruments[(std::size_t) t]->midiMessage (bytes, size);
- }
- }
- // live-played notes routed through their channel's note path (round 149)
- if (liveRoutedCount > 0)
- {
- for (int k = 0; k < liveRoutedCount; ++k)
- events.add (liveRouted[(std::size_t) k]);
- liveRoutedCount = 0;
- events.sortByOffset();
- }
- // Layer channels play their children (round 164)
- if (state.hasLayers)
- expandLayers (state);
- // VFX channels (round 164b): their processors turn their notes into what they send on their ports
- if (! state.vfxChannels.empty())
- runVfxChannels (state, playing, numSamples);
- // cut groups: each note of a channel "cuts" the channels linked to it, at the same sample
- if (state.hasCutGroups)
- {
- const auto count = events.size();
- for (int i = 0; i < count; ++i)
- {
- const auto e = events[i];
- if (e.type != NoteEvent::Type::on || e.channel < 0 || e.channel >= (int) state.cutTargets.size())
- continue;
- for (auto target : state.cutTargets[(std::size_t) e.channel])
- {
- NoteEvent choke;
- choke.type = NoteEvent::Type::choke;
- choke.channel = target;
- choke.sampleOffset = e.sampleOffset;
- events.add (choke);
- }
- }
- events.sortByOffset();
- }
- // MIDI to the mixer-slot Patchers that take a MIDI port (round 162): at its sample (round 164f)
- if (state.hasEffectMidiRoutes)
- for (int k = 0; k < events.size(); ++k)
- deliverToEffects (state, events[k]);
- // 1) channels, each with its own notes at the exact sample (in parallel when worth it)
- const auto channels = std::min<int> ({ (int) state.instruments.size(), (int) song.channels.size(), maxChannels });
- auto channel = [this, &state, numSamples] (int c) {
- if (! times->enabled.load (std::memory_order_relaxed) || c >= ProcessTimes::channels)
- {
- renderChannel (state, c, numSamples);
- return;
- }
- const auto before = ProcessTimes::now();
- renderChannel (state, c, numSamples);
- times->channelNs[(std::size_t) c].fetch_add (ProcessTimes::now() - before, std::memory_order_relaxed);
- };
- // the task graph (round 165i): channels and inserts by readiness; else the channel pass, then the inserts by layers
- if (! runTaskGraph (state, playing, channels, numSamples, meters))
- {
- if (workers != nullptr && state.parallel && state.audioThreadChannels.empty())
- workers->parallelFor (channels, channel);
- else if (workers != nullptr && state.parallel)
- {
- // "Allow threaded processing" off: those channels on this thread, the rest in parallel
- auto threaded = [&state, &channel, channels] (int i) {
- if (const auto c = state.threadedChannels[(std::size_t) i]; c < channels)
- channel (c);
- };
- workers->parallelFor ((int) state.threadedChannels.size(), threaded);
- for (const auto c : state.audioThreadChannels)
- if (c < channels)
- channel (c);
- }
- else
- for (int c = 0; c < channels; ++c)
- channel (c);
- for (int c = 0; c < channels; ++c)
- {
- if (channelSilent[(std::size_t) c])
- continue;
- const auto t = std::clamp (song.channels[(std::size_t) c].mixerTrack, 0, tracks - 1);
- auto* dl = trackBuffer (t, 0);
- auto* dr = trackBuffer (t, 1);
- const auto* sl = channelBuffer (c, 0);
- const auto* sr = channelBuffer (c, 1);
- for (int k = 0; k < numSamples; ++k)
- {
- dl[k] += sl[k];
- dr[k] += sr[k];
- }
- // extra outputs to other inserts (a generator Patcher's, REA_rodada141)
- if ((std::size_t) c < state.instruments.size() && state.instruments[(std::size_t) c] != nullptr)
- {
- const auto& instrument = *state.instruments[(std::size_t) c];
- for (int k = 0; k < instrument.auxOutputCount(); ++k)
- {
- const auto target = instrument.auxOutputTrack (k);
- const auto* al = instrument.auxOutputData (k, 0);
- const auto* ar = instrument.auxOutputData (k, 1);
- if (target < 0 || target >= tracks || al == nullptr || ar == nullptr)
- continue;
- auto* tl = trackBuffer (target, 0);
- auto* tr = trackBuffer (target, 1);
- for (int x = 0; x < numSamples; ++x)
- {
- tl[x] += al[x];
- tr[x] += ar[x];
- }
- }
- }
- }
- if (liveInput != nullptr && liveInput->numChannels > 0)
- mixLiveInput (state, playing, numSamples);
- mixMetronome (state, metronomeTarget (state), numSamples);
- // 2) inserts by layers: within a layer nothing depends on anything else; each track pulls the
- // outputs of the tracks that send to it (without writing into others' buffers)
- trackActive.fill (0);
- tapActive.fill (0);
- for (std::size_t d = 0; d < state.mixerLayers.size(); ++d)
- {
- const auto split = d < state.audioThreadLayers.size() && ! state.audioThreadLayers[d].empty();
- const auto& layer = split ? state.threadedLayers[d] : state.mixerLayers[d];
- auto track = [this, &state, &layer, numSamples, meters] (int i) { processTrack (state, layer[(std::size_t) i], numSamples, meters); };
- if (workers != nullptr && state.parallel && layer.size() > 1)
- workers->parallelFor ((int) layer.size(), track);
- else
- for (int i = 0; i < (int) layer.size(); ++i)
- track (i);
- if (split) // inserts with a plugin whose "Allow threaded processing" is off
- for (const auto t : state.audioThreadLayers[d])
- processTrack (state, t, numSamples, meters);
- }
- }
- // 3) Master: inputs, effects, fader. Without a configured mixer, it stays at 0 dB.
- pullInputs (state, 0, numSamples);
- runEffects (state, 0, numSamples);
- if (! song.mixer.empty())
- {
- applyTrackEq (song.mixer[0], 0, song.cleanMixer, trackBuffer (0, 0), trackBuffer (0, 1), numSamples);
- }
- MixerMatrix target; // without a mixer: unity
- if (! song.mixer.empty())
- {
- const auto& master = song.mixer[0];
- const auto volume = state.hasAutomation ? automated (state.mixerVolumeAuto[0], master.volume) : master.volume;
- const auto pan = state.hasAutomation ? automated (state.mixerPanAuto[0], (master.pan + 1.0f) * 0.5f) * 2.0f - 1.0f : master.pan;
- target = master.muted ? MixerMatrix { 0.0f, 0.0f, 0.0f, 0.0f } : mixerMatrix (master, volume, pan, song.panTriangular);
- }
- lastTrackGain[0] = applyMatrix (trackBuffer (0, 0), trackBuffer (0, 1), left, right, numSamples, lastTrackGain[0], target);
- if (meters != nullptr)
- {
- meters->accumulate (0, peakOf (left, numSamples), peakOf (right, numSamples));
- meters->capture.push (0, left, right, numSamples);
- meters->logTap.push (0, left, right, numSamples);
- if (chunkPlaying)
- meters->bounceTap.push (0, left, right, numSamples);
- }
- if (hwOut != nullptr && ! state.hwRoutes.empty() && hwOut->active.load (std::memory_order_relaxed))
- mixHardwareOutputs (state, numSamples);
- }
- // 4) inserts with a hardware output target: their post-fader output (the same signal the Master gets) into the
- // device-channel buses, delayed like the Master's path
- void RackRenderer::mixHardwareOutputs (const RackState& state, int n) noexcept
- {
- auto* sl = hwScratch.data();
- auto* sr = hwScratch.data() + blockCapacity;
- for (const auto& route : state.hwRoutes)
- {
- const auto t = route.track;
- if (t <= 0 || t >= maxMixerTracks)
- continue;
- const float* l = trackActive[(std::size_t) t] ? trackBuffer (t, 0) : nullptr;
- const float* r = trackActive[(std::size_t) t] ? trackBuffer (t, 1) : nullptr;
- const auto bypassed = pdcBypassNow && (std::size_t) t < state.bypassWhenIdle.size() && state.bypassWhenIdle[(std::size_t) t] != 0;
- if (route.delay != nullptr && ! bypassed)
- {
- if (l != nullptr)
- {
- std::copy (l, l + n, sl);
- std::copy (r, r + n, sr);
- }
- else
- {
- std::fill (sl, sl + n, 0.0f);
- std::fill (sr, sr + n, 0.0f);
- }
- route.delay->process (sl, sr, n);
- l = sl;
- r = sr;
- }
- if (l == nullptr)
- continue;
- const auto [first, count] = outputChannelsOf (route.channel, route.mode);
- auto* a = hwOut->bus (first, liveInputOffset, n);
- auto* b = count > 1 ? hwOut->bus (first + 1, liveInputOffset, n) : nullptr;
- addToOutput (l, r, route.mode, a, b, n);
- }
- }
- void RackRenderer::mixLiveInput (const RackState& state, bool playing, int n) noexcept
- {
- const auto recording = liveInput->recording.load (std::memory_order_relaxed);
- // the Master too (FL's slot panel has its audio input selector; REA round 143): it plays whenever its
- // monitoring is not off (it has no record arm)
- for (int t = 0; t < (int) state.song.mixer.size() && t < maxMixerTracks; ++t)
- mixLiveInputTrack (state, t, recording, playing, n);
- }
- void RackRenderer::mixLiveInputTrack (const RackState& state, int t, bool recording, bool playing, int n) noexcept
- {
- if (t >= (int) state.song.mixer.size() || t >= maxMixerTracks)
- return;
- const auto& track = state.song.mixer[(std::size_t) t];
- const auto [first, count] = inputChannelsOf (track, liveInput->numChannels);
- if (count == 0)
- return;
- const auto* a = liveInput->channel (first, liveInputOffset);
- const auto* b = count > 1 ? liveInput->channel (first + 1, liveInputOffset) : a;
- if (a == nullptr || b == nullptr)
- return;
- liveInput->meters.accumulate (t, peakOf (a, n), peakOf (b, n));
- if (t == 0 ? track.monitor == InputMonitor::off
- : ! inputMonitored (track.monitor, liveInput->armed[(std::size_t) t].load (std::memory_order_relaxed), playing, recording))
- return;
- auto* dl = trackBuffer (t, 0);
- auto* dr = trackBuffer (t, 1);
- for (int i = 0; i < n; ++i)
- {
- dl[i] += a[i];
- dr[i] += b[i];
- }
- }
- void RackRenderer::sumChannelsInto (const RackState& state, int t, int channels, int n) noexcept
- {
- if ((std::size_t) t >= state.trackChannels.size())
- return;
- auto* dl = trackBuffer (t, 0);
- auto* dr = trackBuffer (t, 1);
- for (const auto c : state.trackChannels[(std::size_t) t])
- {
- if (c >= channels || channelSilent[(std::size_t) c])
- continue;
- const auto* sl = channelBuffer (c, 0);
- const auto* sr = channelBuffer (c, 1);
- for (int k = 0; k < n; ++k)
- {
- dl[k] += sl[k];
- dr[k] += sr[k];
- }
- }
- }
- // FL's FUN_012c19f0: "When recording or not playing" bypasses while the transport is stopped or recording. Back to
- // compensated, those tracks' lines are emptied: they start from silence instead of replaying what they held.
- void RackRenderer::updatePdcBypass (const RackState& state, bool playing) noexcept
- {
- const auto recording = liveInput != nullptr && liveInput->recording.load (std::memory_order_relaxed);
- pdcBypassNow = state.anyBypassWhenIdle && (! playing || recording);
- if (pdcBypassBefore && ! pdcBypassNow)
- {
- for (std::size_t t = 0; t < state.bypassWhenIdle.size() && t < state.sendDelays.size(); ++t)
- if (state.bypassWhenIdle[t] != 0)
- {
- const auto plain = std::min (state.sendDelays[t].size(), 1 + state.song.mixer[t].sends.size()); // not the Fruity Send taps
- for (std::size_t k = 0; k < plain; ++k)
- if (state.sendDelays[t][k] != nullptr)
- state.sendDelays[t][k]->clear();
- }
- for (const auto& route : state.hwRoutes)
- if (route.delay != nullptr && (std::size_t) route.track < state.bypassWhenIdle.size() && state.bypassWhenIdle[(std::size_t) route.track] != 0)
- route.delay->clear();
- }
- pdcBypassBefore = pdcBypassNow;
- }
- bool RackRenderer::runTaskGraph (const RackState& state, bool playing, int channels, int n, MeterBank* meters) noexcept
- {
- // LYNX_TASKGRAPH=0: the layers only (A/B measurements)
- static const bool enabled = [] { const char* e = std::getenv ("LYNX_TASKGRAPH"); return e == nullptr || std::atoi (e) != 0; }();
- if (! enabled || ! state.graphOk || workers == nullptr || ! state.parallel || state.graphChannels != channels)
- return false;
- const auto tracks = std::max<int> (1, (int) state.song.mixer.size());
- const auto tasks = channels + tracks - 1;
- if (tasks <= 0 || tasks > graphCapacity || (int) state.graphIndegree.size() != tasks)
- return false;
- // extra outputs of an instrument (a generator Patcher's) go to inserts known only now: the layers then
- for (int c = 0; c < channels; ++c)
- if (state.instruments[(std::size_t) c] != nullptr && state.instruments[(std::size_t) c]->auxOutputCount() > 0)
- return false;
- trackActive.fill (0);
- tapActive.fill (0);
- const auto live = liveInput != nullptr && liveInput->numChannels > 0;
- const auto recording = live && liveInput->recording.load (std::memory_order_relaxed);
- for (int i = 0; i < tasks; ++i)
- {
- graphPending[(std::size_t) i].store (state.graphIndegree[(std::size_t) i], std::memory_order_relaxed);
- graphSlots[(std::size_t) i].store (0, std::memory_order_relaxed);
- }
- auto pushed = 0;
- for (const auto r : state.graphRoots)
- graphSlots[(std::size_t) pushed++].store (r + 1, std::memory_order_relaxed);
- graphPushed.store (pushed, std::memory_order_release); // (the pool publishes its work after this: seq_cst)
- const auto timing = times->enabled.load (std::memory_order_relaxed);
- auto task = [&] (int position) {
- int id;
- for (int spins = 0; (id = graphSlots[(std::size_t) position].load (std::memory_order_acquire)) == 0; ++spins)
- {
- WorkerPool::relax();
- if ((spins & 1023) == 1023)
- std::this_thread::yield();
- }
- --id;
- if (id < channels)
- {
- if (! timing || id >= ProcessTimes::channels)
- renderChannel (state, id, n);
- else
- {
- const auto before = ProcessTimes::now();
- renderChannel (state, id, n);
- times->channelNs[(std::size_t) id].fetch_add (ProcessTimes::now() - before, std::memory_order_relaxed);
- }
- }
- else
- {
- const auto t = id - channels + 1;
- sumChannelsInto (state, t, channels, n);
- if (live)
- mixLiveInputTrack (state, t, recording, playing, n);
- mixMetronome (state, t, n);
- processTrack (state, t, n, meters);
- }
- for (auto k = state.graphNextStart[(std::size_t) id]; k < state.graphNextStart[(std::size_t) id + 1]; ++k)
- {
- const auto j = state.graphNext[(std::size_t) k];
- if (graphPending[(std::size_t) j].fetch_sub (1, std::memory_order_acq_rel) == 1)
- graphSlots[(std::size_t) graphPushed.fetch_add (1, std::memory_order_acq_rel)].store (j + 1, std::memory_order_release);
- }
- };
- workers->parallelFor (tasks, task);
- // the Master's own channels and input (its effects run after, as before)
- sumChannelsInto (state, 0, channels, n);
- if (live)
- mixLiveInputTrack (state, 0, recording, playing, n);
- mixMetronome (state, 0, n);
- return true;
- }
- int RackRenderer::metronomeTarget (const RackState& state) const noexcept
- {
- return metronome::targetTrack (clickTrack, std::min ((int) state.song.mixer.size(), maxMixerTracks));
- }
- // the metronome's click into its track's input (round 165z-a2); `track` is the one being filled, the click goes in
- // only when it is the metronome's
- void RackRenderer::mixMetronome (const RackState& state, int track, int n) noexcept
- {
- if (clickLeft == nullptr || state.song.mixer.empty() || track != metronomeTarget (state))
- return;
- auto* dl = trackBuffer (track, 0);
- auto* dr = trackBuffer (track, 1);
- const auto* a = clickLeft + liveInputOffset;
- const auto* b = clickRight + liveInputOffset;
- for (int i = 0; i < n; ++i)
- {
- dl[i] += a[i];
- dr[i] += b[i];
- }
- }
- void RackRenderer::pullInputs (const RackState& state, int t, int n) noexcept
- {
- if ((std::size_t) t >= state.mixerInputs.size())
- return;
- auto* dl = trackBuffer (t, 0);
- auto* dr = trackBuffer (t, 1);
- for (const auto& in : state.mixerInputs[(std::size_t) t])
- {
- const auto tap = in.tap >= 0 && in.tap < RackState::maxSendTaps ? in.tap : -1;
- // an automated route level (fader curve, like the knob)
- const auto automatedLevel = in.automation >= 0 ? automationValues[(std::size_t) in.automation] : std::numeric_limits<float>::quiet_NaN();
- const auto level = std::isnan (automatedLevel) ? in.level : faderGain (automatedLevel);
- const auto active = tap >= 0 ? tapActive[(std::size_t) tap] : trackActive[(std::size_t) in.source];
- const auto& delays = state.sendDelays[(std::size_t) in.source];
- auto* delay = (std::size_t) in.delay < delays.size() ? delays[(std::size_t) in.delay].get() : nullptr;
- if (pdcBypassNow && tap < 0 && state.bypassWhenIdle[(std::size_t) in.source] != 0)
- delay = nullptr; // FL's "When recording or not playing": no compensation now
- auto* sl = tap >= 0 ? tapBuffer (tap, 0) : trackBuffer (in.source, 0);
- auto* sr = tap >= 0 ? tapBuffer (tap, 1) : trackBuffer (in.source, 1);
- if (delay != nullptr)
- {
- // PDC: this edge is shorter than the slowest one arriving here. A silent source
- // still drains the tail that was in the line.
- delay->mixInto (active ? sl : nullptr, active ? sr : nullptr, level, dl, dr, n);
- continue;
- }
- if (! active)
- continue;
- for (int i = 0; i < n; ++i)
- {
- dl[i] += sl[i] * level;
- dr[i] += sr[i] * level;
- }
- }
- }
- namespace
- {
- bool isSilentBlock (const float* l, const float* r, int n) noexcept
- {
- for (int i = 0; i < n; ++i)
- if (l[i] != 0.0f || r[i] != 0.0f)
- return false;
- return true;
- }
- } // namespace
- void RackRenderer::processTrack (const RackState& state, int t, int n, MeterBank* meters) noexcept
- {
- const auto& track = state.song.mixer[(std::size_t) t];
- pullInputs (state, t, n);
- // recording taps on this insert (round 168x, issue #41): FL's "Record" positions after the input
- const auto taps = liveInput != nullptr && t > 0 ? liveInput->tappedPoints (t) : 0u;
- auto tap = [&] (RecordSource point, const float* l, const float* r) {
- if ((taps & (1u << (unsigned) point)) != 0)
- liveInput->tap (t, point, l, r, liveInputOffset, n);
- };
- const auto silent = track.muted || ! state.mixerAudible[(std::size_t) t];
- // target matrix; the ramp from the previous block avoids clicks on mute and fader
- MixerMatrix target { 0.0f, 0.0f, 0.0f, 0.0f };
- MixerMatrix level { 0.0f, 0.0f, 0.0f, 0.0f }; // the same without the mute (the "Post level and panning" tap)
- if (! silent || (taps & (1u << (unsigned) RecordSource::postLevel)) != 0)
- {
- const auto volume = state.hasAutomation ? automated (state.mixerVolumeAuto[(std::size_t) t], track.volume) : track.volume;
- const auto pan = state.hasAutomation ? automated (state.mixerPanAuto[(std::size_t) t], (track.pan + 1.0f) * 0.5f) * 2.0f - 1.0f
- : track.pan;
- level = mixerMatrix (track, volume, pan, state.song.panTriangular);
- if (! silent)
- target = level;
- }
- auto& last = lastTrackGain[(std::size_t) t];
- const auto from = std::isnan (last.ll) ? target : last;
- constexpr MixerMatrix zero { 0.0f, 0.0f, 0.0f, 0.0f };
- auto* l = trackBuffer (t, 0);
- auto* r = trackBuffer (t, 1);
- if (silent && from == zero && taps == 0) // (a muted insert being recorded still runs its effects for the take)
- {
- if (meters != nullptr)
- meters->capture.pushSilence (t, n);
- return; // stays inactive: nobody pulls anything from it
- }
- tap (RecordSource::externalAndMixer, l, r);
- // an idle insert (round 166d, up to 500 of them): no effects, no built-in EQ and nothing came in this block. Its
- // output is silence whatever the fader does, so it stays inactive and the tracks it feeds skip it (the same
- // samples: adding zeros changes nothing; a tap stays silent too)
- if (track.effects.empty() && (state.song.cleanMixer || ! track.eq.active()) && isSilentBlock (l, r, n))
- {
- if (t < (int) trackEqs.size() && trackEqs[(std::size_t) t].running)
- trackEqs[(std::size_t) t] = {};
- last = target;
- if (meters != nullptr)
- meters->capture.pushSilence (t, n);
- return;
- }
- runEffects (state, t, n);
- tap (RecordSource::postEffects, l, r);
- applyTrackEq (track, t, state.song.cleanMixer, l, r, n);
- tap (RecordSource::postEq, l, r);
- if ((taps & (1u << (unsigned) RecordSource::postLevel)) != 0)
- if (const auto dest = liveInput->tapDestination (t, RecordSource::postLevel, liveInputOffset, n); dest.first != nullptr)
- {
- // the fader, pan and separation without the mute, with their own ramp
- auto& lastLevel = lastTapGain[(std::size_t) t];
- const auto fromLevel = std::isnan (lastLevel.ll) ? level : lastLevel;
- lastLevel = applyMatrix (l, r, dest.first, dest.second, n, fromLevel, level);
- }
- last = applyMatrix (l, r, l, r, n, from, target);
- tap (RecordSource::postTrack, l, r);
- trackActive[(std::size_t) t] = 1;
- if (meters != nullptr)
- {
- meters->accumulate (t, peakOf (l, n), peakOf (r, n));
- meters->capture.push (t, l, r, n);
- }
- }
- } // namespace lynx
|