| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739 |
- #include "core/Vfx.h"
- #include <algorithm>
- #include <atomic>
- #include <cmath>
- #include <cstring>
- #include <functional>
- #include "core/FlSynths.h"
- #include "core/Song.h"
- #include "flp/Flp.h"
- namespace lynx::vfx
- {
- namespace
- {
- constexpr std::uint32_t lynxMagic = 0x5846564c; // "LVFX"
- struct Reader
- {
- const std::uint8_t* data;
- std::size_t size, pos = 0;
- bool failed = false;
- template <typename T>
- T get() noexcept
- {
- T v {};
- if (failed || sizeof (T) > size - pos)
- {
- failed = true;
- return v;
- }
- std::memcpy (&v, data + pos, sizeof (T));
- pos += sizeof (T);
- return v;
- }
- std::int32_t i32() noexcept { return get<std::int32_t>(); }
- std::uint32_t u32() noexcept { return get<std::uint32_t>(); }
- std::size_t left() const noexcept { return failed ? 0 : size - pos; }
- };
- template <typename T>
- void put (std::vector<std::uint8_t>& out, T v)
- {
- const auto* p = reinterpret_cast<const std::uint8_t*> (&v);
- out.insert (out.end(), p, p + sizeof (T));
- }
- // Settings handed from the UI thread to the audio thread without locks or allocation on the audio side (the
- // Patcher graph's pattern): the UI publishes a new object, the audio thread swaps it in and parks the old one, the
- // UI frees it on its next call.
- template <typename T>
- class Handoff
- {
- public:
- explicit Handoff (T initial) : active (std::make_unique<T> (std::move (initial))) {}
- explicit Handoff (std::unique_ptr<T> initial) : active (std::move (initial)) {}
- ~Handoff()
- {
- delete pending.exchange (nullptr);
- delete retired.exchange (nullptr);
- }
- void publish (T value)
- {
- delete retired.exchange (nullptr);
- delete pending.exchange (new T (std::move (value)));
- }
- void publish (std::unique_ptr<T> value)
- {
- delete retired.exchange (nullptr);
- delete pending.exchange (value.release());
- }
- // audio thread: true when new settings arrived
- bool take() noexcept
- {
- auto* incoming = pending.exchange (nullptr);
- if (incoming == nullptr)
- return false;
- auto* old = active.release();
- active.reset (incoming);
- delete retired.exchange (old); // (normally null: the UI frees it on its next call)
- return true;
- }
- const T& get() const noexcept { return *active; }
- private:
- std::unique_ptr<T> active;
- std::atomic<T*> pending { nullptr };
- std::atomic<T*> retired { nullptr };
- };
- } // namespace
- int inputId (int key, int colour) noexcept { return (colour & 15) * 128 + (key & 127); }
- // ---------------------------------------------------------------------------------------------------------------
- const char* flName (Kind kind) noexcept
- {
- switch (kind)
- {
- case Kind::keyMapper: return "VFX Key Mapper";
- case Kind::colorMapper: return "VFX Color Mapper";
- case Kind::envelope: return "VFX Envelope";
- case Kind::keyboardSplitter: return "VFX Keyboard Splitter";
- case Kind::levelScaler: return "VFX Level Scaler";
- case Kind::sequencer: return "VFX Sequencer";
- case Kind::script: return "VFX Script";
- }
- return "VFX";
- }
- std::optional<Kind> kindOfFlName (std::string_view name) noexcept
- {
- auto same = [] (std::string_view a, std::string_view b) {
- return a.size() == b.size() && std::equal (a.begin(), a.end(), b.begin(), [] (char x, char y) {
- return std::tolower ((unsigned char) x) == std::tolower ((unsigned char) y);
- });
- };
- for (const auto k : allKinds)
- if (same (name, flName (k)))
- return k;
- return std::nullopt;
- }
- void EventList::sortByOffset() noexcept
- {
- // stable insertion sort in place (std::stable_sort may allocate; the events arrive nearly in order); an "off"
- // goes before an "on" on the same sample
- const auto before = [] (const Event& a, const Event& b) {
- if (a.offset != b.offset)
- return a.offset < b.offset;
- return a.type == Event::Type::off && b.type != Event::Type::off;
- };
- for (int i = 1; i < count; ++i)
- {
- const auto e = events[(std::size_t) i];
- auto j = i;
- for (; j > 0 && before (e, events[(std::size_t) j - 1]); --j)
- events[(std::size_t) j] = events[(std::size_t) j - 1];
- events[(std::size_t) j] = e;
- }
- }
- void EventList::removeFirst (int n) noexcept
- {
- n = std::clamp (n, 0, count);
- std::move (events.begin() + n, events.begin() + count, events.begin());
- count -= n;
- }
- // ---------------------------------------------------------------------------------------------------------------
- // VFX Key Mapper
- KeyMapperSettings defaultKeyMapper()
- {
- KeyMapperSettings s;
- for (int i = 0; i < 12; ++i)
- s.rows[(std::size_t) i] = { i };
- return s;
- }
- std::optional<KeyMapperSettings> parseKeyMapper (const std::vector<std::uint8_t>& fl)
- {
- Reader r { fl.data(), fl.size() };
- KeyMapperSettings s;
- s.version = r.i32();
- if (r.failed || s.version < 1 || s.version > 16)
- return std::nullopt;
- s.enabled = r.i32() != 0;
- s.baseKey = r.i32();
- s.offset = r.i32();
- s.multimode = r.i32();
- s.slide = r.i32();
- for (auto& row : s.rows)
- {
- const auto n = r.i32();
- if (r.failed || n < 0 || n > 128)
- return std::nullopt;
- row.clear();
- for (int k = 0; k < n; ++k)
- row.push_back (r.get<std::int16_t>());
- }
- if (r.failed)
- return std::nullopt;
- s.extra.assign (fl.begin() + (std::ptrdiff_t) r.pos, fl.end());
- return s;
- }
- std::vector<std::uint8_t> writeKeyMapper (const KeyMapperSettings& s)
- {
- std::vector<std::uint8_t> out;
- put<std::int32_t> (out, s.version);
- put<std::int32_t> (out, s.enabled ? 1 : 0);
- put<std::int32_t> (out, s.baseKey);
- put<std::int32_t> (out, s.offset);
- put<std::int32_t> (out, s.multimode);
- put<std::int32_t> (out, s.slide);
- for (const auto& row : s.rows)
- {
- put<std::int32_t> (out, (std::int32_t) row.size());
- for (const auto v : row)
- put<std::int16_t> (out, (std::int16_t) v);
- }
- out.insert (out.end(), s.extra.begin(), s.extra.end());
- return out;
- }
- // ---------------------------------------------------------------------------------------------------------------
- // VFX Color Mapper
- ColorMapperSettings defaultColorMapper()
- {
- ColorMapperSettings s;
- for (int i = 0; i < 16; ++i)
- s.port[(std::size_t) i] = s.colour[(std::size_t) i] = i;
- return s;
- }
- std::optional<ColorMapperSettings> parseColorMapper (const std::vector<std::uint8_t>& fl)
- {
- Reader r { fl.data(), fl.size() };
- ColorMapperSettings s;
- s.header = r.u32();
- for (auto& p : s.port)
- p = std::clamp (r.i32(), 0, 15);
- for (auto& c : s.colour)
- c = std::clamp (r.i32(), 0, 15);
- if (r.failed)
- return std::nullopt;
- return s;
- }
- std::vector<std::uint8_t> writeColorMapper (const ColorMapperSettings& s)
- {
- std::vector<std::uint8_t> out;
- put<std::uint32_t> (out, s.header);
- for (const auto p : s.port)
- put<std::int32_t> (out, p);
- for (const auto c : s.colour)
- put<std::int32_t> (out, c);
- return out;
- }
- // ---------------------------------------------------------------------------------------------------------------
- // VFX Level Scaler (REA_rodada164b §Level Scaler)
- bool LevelScalerSettings::identity (int tab) const noexcept
- {
- return at (tab, multiply) == 8192 && at (tab, offset) == 0 && at (tab, tension) == 0 && at (tab, humanize) == 0;
- }
- void resetLevelScalerTab (LevelScalerSettings& s, int tab)
- {
- // the editor's Reset: bipolar properties are centred
- s.at (tab, LevelScalerSettings::center) = tab >= LevelScalerSettings::finePitch ? 4096 : 0;
- s.at (tab, LevelScalerSettings::multiply) = 8192;
- s.at (tab, LevelScalerSettings::offset) = 0;
- s.at (tab, LevelScalerSettings::tension) = 0;
- s.at (tab, LevelScalerSettings::humanize) = 0;
- s.at (tab, LevelScalerSettings::humanizeMode) = 1;
- }
- LevelScalerSettings defaultLevelScaler()
- {
- LevelScalerSettings s;
- for (int t = 0; t < LevelScalerSettings::tabCount; ++t)
- resetLevelScalerTab (s, t);
- return s;
- }
- std::optional<LevelScalerSettings> parseLevelScaler (const std::vector<std::uint8_t>& fl)
- {
- Reader r { fl.data(), fl.size() };
- auto s = defaultLevelScaler();
- s.version = r.u32();
- if (r.failed)
- return std::nullopt;
- if (s.version >= 2)
- return defaultLevelScaler(); // (FL ignores a state it does not know)
- // chunks: id, 64-bit size, data (unknown ids skipped)
- while (r.left() >= 12)
- {
- const auto id = r.u32();
- const auto size = r.get<std::uint64_t>();
- if (size > r.left())
- return std::nullopt;
- const auto end = r.pos + (std::size_t) size;
- if (id == 0x3ea && size >= 1)
- s.selectedTab = std::clamp ((int) r.get<std::uint8_t>(), 0, 5);
- else if (id == 0x3e9 && size >= 144)
- {
- static constexpr std::array<std::pair<int, int>, 6> range { { { 0, 8192 }, { -16384, 16384 }, { -8192, 8192 }, { -8192, 8192 }, { 0, 16384 }, { 0, 2 } } };
- for (int i = 0; i < 36; ++i)
- s.p[(std::size_t) i] = std::clamp (r.i32(), range[(std::size_t) (i % 6)].first, range[(std::size_t) (i % 6)].second);
- }
- r.pos = end;
- }
- if (r.failed)
- return std::nullopt;
- return s;
- }
- std::vector<std::uint8_t> writeLevelScaler (const LevelScalerSettings& s)
- {
- std::vector<std::uint8_t> out;
- put<std::uint32_t> (out, 1);
- put<std::uint32_t> (out, 0x3ea);
- put<std::uint64_t> (out, 1);
- put<std::uint8_t> (out, (std::uint8_t) std::clamp (s.selectedTab, 0, 5));
- put<std::uint32_t> (out, 0x3e9);
- put<std::uint64_t> (out, 144);
- for (const auto v : s.p)
- put<std::int32_t> (out, v);
- return out;
- }
- double levelScale (const LevelScalerSettings& s, int tab, double x, double lo, double hi, double random) noexcept
- {
- using L = LevelScalerSettings;
- if (s.identity (tab) || ! (hi > lo))
- return x;
- const auto c = s.at (tab, L::center) / 8192.0;
- const auto scale = 1.0 / std::max (c, 1.0 - c);
- const auto tension = s.at (tab, L::tension);
- const auto g = std::pow (8193.0, std::abs (tension) / 8192.0) - 1.0;
- const auto mul = s.at (tab, L::multiply) / (8192.0 * scale);
- const auto off = s.at (tab, L::offset) / 8192.0 + c;
- const auto u = (x - lo) / (hi - lo);
- const auto d = u - c;
- const auto m = std::min (1.0, std::abs (d) * scale);
- auto shape = m;
- if (tension > 0 && g > 0.0)
- shape = 1.0 - (std::pow (g + 1.0, 1.0 - m) - 1.0) / g;
- else if (tension < 0 && g > 0.0)
- shape = (std::pow (g + 1.0, m) - 1.0) / g;
- const auto y = std::clamp ((d < 0.0 ? -shape : shape) * mul + off, 0.0, 1.0);
- auto out = std::clamp (lo + y * (hi - lo), lo, hi);
- if (const auto h = s.at (tab, L::humanize) / 16384.0; h > 0.0)
- {
- const auto mode = s.at (tab, L::humanizeMode);
- const auto a = mode == 2 ? out : out - (out - lo) * h;
- const auto b = mode == 0 ? out : out + (hi - out) * h;
- out = a + std::clamp (random, 0.0, 1.0) * (b - a);
- }
- return out;
- }
- // ---------------------------------------------------------------------------------------------------------------
- // FL's mapping curves and VFX Keyboard Splitter (REA_rodada164b §Keyboard Splitter)
- MapCurve MapCurve::flat (double level)
- {
- MapCurve m;
- m.points.push_back ({ 0.0, level });
- m.points.push_back ({ 1.0, level });
- return m;
- }
- double MapCurve::at (double x) const noexcept
- {
- if (points.empty())
- return 0.0;
- if (x <= points.front().x)
- return points.front().y;
- for (std::size_t i = 1; i < points.size(); ++i)
- if (x < points[i].x)
- {
- const auto& a = points[i - 1];
- const auto& b = points[i];
- if (b.curve == 2) // hold: the previous level up to this point
- return a.y;
- auto f = b.x > a.x ? (x - a.x) / (b.x - a.x) : 1.0;
- if (b.tension != 0.0f) // (the automation clips' bend, FL's sign)
- f = std::pow (f, std::pow (2.0, (double) -b.tension * 3.0));
- return a.y + (b.y - a.y) * f;
- }
- return points.back().y;
- }
- bool MapCurve::isFlat (double level) const noexcept
- {
- return std::all_of (points.begin(), points.end(), [level] (const Point& p) { return std::abs (p.y - level) < 1e-9; });
- }
- namespace
- {
- bool readMap (Reader& r, MapCurve& m)
- {
- r.i32(); // kind (3)
- const auto version = r.i32();
- const auto n = r.i32();
- if (r.failed || n < 0 || n > 4096 || version < 2 || version > 3)
- return false;
- m.points.clear();
- double x = 0.0;
- for (int k = 0; k < n; ++k)
- {
- MapCurve::Point p;
- const auto dx = r.get<double>();
- x += std::isfinite (dx) ? dx : 0.0;
- p.x = x;
- p.y = r.get<double>();
- if (! std::isfinite (p.y))
- p.y = 0.0;
- p.tension = r.get<float>();
- if (! std::isfinite (p.tension))
- p.tension = 0.0f;
- p.curve = r.get<std::uint8_t>();
- p.b13 = r.get<std::uint8_t>();
- p.b14 = r.get<std::uint8_t>();
- p.b15 = r.get<std::uint8_t>();
- m.points.push_back (p);
- }
- for (auto& f : m.footer)
- f = r.i32();
- return ! r.failed;
- }
- void writeMap (std::vector<std::uint8_t>& out, const MapCurve& m, int kind = 3)
- {
- put<std::int32_t> (out, kind);
- put<std::int32_t> (out, 3);
- put<std::int32_t> (out, (std::int32_t) m.points.size());
- double x = 0.0;
- for (const auto& p : m.points)
- {
- put<double> (out, p.x - x);
- x = p.x;
- put<double> (out, p.y);
- put<float> (out, p.tension);
- put<std::uint8_t> (out, p.curve);
- put<std::uint8_t> (out, p.b13);
- put<std::uint8_t> (out, p.b14);
- put<std::uint8_t> (out, p.b15); // (FL's shape flag: 2 straight, 1 / 0xff bent up / down)
- }
- for (const auto f : m.footer)
- put<std::int32_t> (out, f);
- }
- } // namespace
- KeyboardSplitterSettings defaultKeyboardSplitter()
- {
- KeyboardSplitterSettings s;
- s.zones[0].keyboard = MapCurve::flat (1.0); // zone 1: the whole keyboard
- return s;
- }
- std::optional<KeyboardSplitterSettings> parseKeyboardSplitter (const std::vector<std::uint8_t>& fl)
- {
- Reader r { fl.data(), fl.size() };
- KeyboardSplitterSettings s;
- s.header = r.u32();
- if (r.failed || s.header >= 0x100004 || (s.header & 0xfffff) == 0)
- return std::nullopt;
- if ((s.header >> 20) != 0)
- s.page = std::clamp ((int) r.get<std::uint8_t>(), 0, 1);
- for (auto& z : s.zones)
- {
- if (! readMap (r, z.keyboard) || ! readMap (r, z.velocity))
- return std::nullopt;
- z.transpose = std::clamp (r.i32(), -48, 48);
- z.absolute = r.get<std::uint8_t>() != 0;
- z.random = r.get<std::uint8_t>() != 0;
- // name: u8 length (255: a u32 follows), UTF-16 characters
- std::uint32_t length = r.get<std::uint8_t>();
- if (length == 255)
- length = r.u32();
- if (r.failed || length > 4096 || length * 2 > r.left())
- return std::nullopt;
- std::u16string name;
- for (std::uint32_t k = 0; k < length; ++k)
- name.push_back ((char16_t) r.get<std::uint16_t>());
- z.name.clear();
- for (std::size_t k = 0; k < name.size(); ++k)
- {
- std::uint32_t c = name[k];
- if (c >= 0xd800 && c < 0xdc00 && k + 1 < name.size())
- c = 0x10000 + ((c - 0xd800) << 10) + ((std::uint32_t) name[++k] - 0xdc00);
- if (c < 0x80)
- z.name.push_back ((char) c);
- else if (c < 0x800)
- z.name += { (char) (0xc0 | (c >> 6)), (char) (0x80 | (c & 0x3f)) };
- else if (c < 0x10000)
- z.name += { (char) (0xe0 | (c >> 12)), (char) (0x80 | ((c >> 6) & 0x3f)), (char) (0x80 | (c & 0x3f)) };
- else
- z.name += { (char) (0xf0 | (c >> 18)), (char) (0x80 | ((c >> 12) & 0x3f)), (char) (0x80 | ((c >> 6) & 0x3f)), (char) (0x80 | (c & 0x3f)) };
- }
- }
- if (r.failed)
- return std::nullopt;
- return s;
- }
- std::vector<std::uint8_t> writeKeyboardSplitter (const KeyboardSplitterSettings& s)
- {
- std::vector<std::uint8_t> out;
- put<std::uint32_t> (out, 0x100003);
- put<std::uint8_t> (out, (std::uint8_t) std::clamp (s.page, 0, 1));
- for (const auto& z : s.zones)
- {
- writeMap (out, z.keyboard);
- writeMap (out, z.velocity);
- put<std::int32_t> (out, z.transpose);
- put<std::uint8_t> (out, z.absolute ? 1 : 0);
- put<std::uint8_t> (out, z.random ? 1 : 0);
- std::u16string name;
- for (std::size_t k = 0; k < z.name.size();)
- {
- const auto b = (std::uint8_t) z.name[k];
- std::uint32_t c = b;
- int more = b >= 0xf0 ? 3 : b >= 0xe0 ? 2 : b >= 0xc0 ? 1 : 0;
- c = more == 3 ? b & 0x07 : more == 2 ? b & 0x0f : more == 1 ? b & 0x1f : b;
- ++k;
- for (; more > 0 && k < z.name.size(); --more, ++k)
- c = (c << 6) | ((std::uint8_t) z.name[k] & 0x3f);
- if (c >= 0x10000)
- {
- c -= 0x10000;
- name.push_back ((char16_t) (0xd800 + (c >> 10)));
- name.push_back ((char16_t) (0xdc00 + (c & 0x3ff)));
- }
- else
- name.push_back ((char16_t) c);
- }
- if (name.size() < 255)
- put<std::uint8_t> (out, (std::uint8_t) name.size());
- else
- {
- put<std::uint8_t> (out, 255);
- put<std::uint32_t> (out, (std::uint32_t) name.size());
- }
- for (const auto c : name)
- put<std::uint16_t> (out, (std::uint16_t) c);
- }
- return out;
- }
- double splitterGain (double m) noexcept
- {
- return m >= 1.0 ? 1.0 : (std::pow (11.0, m) - 1.0) / 10.0;
- }
- // ---------------------------------------------------------------------------------------------------------------
- // VFX Envelope (REA_rodada164b §Envelope)
- namespace
- {
- MapCurve curveOf (std::initializer_list<std::tuple<double, double, float>> points, std::array<std::int32_t, 5> footer)
- {
- MapCurve m;
- for (const auto& [x, y, t] : points)
- m.points.push_back ({ x, y, t });
- m.footer = footer;
- return m;
- }
- } // namespace
- EnvelopeSettings defaultEnvelope()
- {
- EnvelopeSettings s;
- for (int g = 0; g < 16; ++g)
- {
- auto& a = s.articulators[(std::size_t) g];
- for (int t = 0; t < EnvelopeSettings::targetCount; ++t)
- {
- auto& p = a.parts[(std::size_t) t];
- p.lfoAmp = curveOf ({ { 0.0, 0.5, 0.0f }, { 1.0, 0.5, 0.0f }, { 1.25, 1.0, 0.0f }, { 2.25, 0.5, 0.0f } }, { 0, 0, -1, -1, -1 });
- if (t == EnvelopeSettings::velocity)
- {
- p.base = 0.0f;
- // a volume envelope: attack, a peak, sustain at half (= the note's own level), release
- p.envelope = curveOf ({ { 0.0, 0.0, 0.0f }, { 0.75, 1.0, 0.2f }, { 2.25, 0.5, 0.2f }, { 2.5, 0.0, 0.2f } },
- { 0, g == 0 ? -1 : 0, 1, -1, 2 });
- }
- else
- {
- p.base = 0.5f;
- p.env = t == EnvelopeSettings::pitch ? 0.25f : 1.0f;
- p.envelope = curveOf ({ { 0.0, 0.5, 0.0f }, { 0.25, 0.5, 0.0f } }, { 1, -1, -1, -1, -1 }); // (on, neutral)
- }
- }
- }
- return s;
- }
- namespace
- {
- bool readPart (Reader& r, EnvelopeSettings::Part& p)
- {
- p.version = r.i32();
- p.base = r.get<float>();
- p.env = r.get<float>();
- p.lfo = r.get<float>();
- if (! readMap (r, p.envelope))
- return false;
- for (auto& k : p.adsr)
- k = r.i32();
- if (! readMap (r, p.lfoAmp))
- return false;
- p.speed = r.i32();
- p.tension = r.i32();
- p.skew = r.i32();
- p.pulseWidth = r.i32();
- p.phase = p.version >= 3 ? r.i32() : 0;
- for (auto* f : { &p.base, &p.env, &p.lfo })
- if (! std::isfinite (*f))
- *f = 0.0f;
- return ! r.failed;
- }
- void writePart (std::vector<std::uint8_t>& out, const EnvelopeSettings::Part& p)
- {
- put<std::int32_t> (out, 3);
- put<float> (out, p.base);
- put<float> (out, p.env);
- put<float> (out, p.lfo);
- writeMap (out, p.envelope, 1);
- for (const auto k : p.adsr)
- put<std::int32_t> (out, k);
- writeMap (out, p.lfoAmp, 2);
- for (const auto v : { p.speed, p.tension, p.skew, p.pulseWidth, p.phase })
- put<std::int32_t> (out, v);
- }
- void writeChunk (std::vector<std::uint8_t>& out, std::int32_t id, const std::vector<std::uint8_t>& data)
- {
- put<std::int32_t> (out, id);
- put<std::int64_t> (out, (std::int64_t) data.size());
- out.insert (out.end(), data.begin(), data.end());
- }
- } // namespace
- std::optional<EnvelopeSettings> parseEnvelope (const std::vector<std::uint8_t>& fl)
- {
- Reader r { fl.data(), fl.size() };
- auto s = defaultEnvelope();
- s.header = r.u32();
- if (r.failed || s.header >= 0x10004)
- return std::nullopt;
- std::vector<std::int32_t> params;
- int group = 0, target = 0;
- while (r.left() >= 12)
- {
- const auto id = r.i32();
- const auto size = r.get<std::int64_t>();
- if (size < 0 || (std::uint64_t) size > r.left())
- return std::nullopt;
- const auto end = r.pos + (std::size_t) size;
- Reader c { fl.data(), end, r.pos };
- switch (id)
- {
- case 1003:
- s.enabled = c.i32() != 0;
- s.shownArticulator = std::clamp (c.i32(), 0, 15);
- s.shownTarget = std::clamp (c.i32(), 0, 4);
- s.shownPart = std::clamp (c.i32(), 0, 1);
- break;
- case 1008:
- params.clear();
- while (c.left() >= 4)
- params.push_back (c.i32());
- break;
- case 1007:
- for (auto& t : s.toggles)
- t = c.left() >= 4 ? c.i32() : 0;
- break;
- case 1004:
- {
- group = std::clamp (c.i32(), 0, 15);
- auto& a = s.articulators[(std::size_t) group];
- a.colour = std::clamp (c.i32(), 0, 15);
- a.low = std::clamp (c.i32(), 0, 132);
- a.high = std::clamp (c.i32(), 0, 132);
- break;
- }
- case 1005:
- group = std::clamp (c.i32(), 0, 15);
- target = std::clamp (c.i32(), 0, 4);
- break;
- case 1006:
- if (! readPart (c, s.articulators[(std::size_t) group].parts[(std::size_t) target]))
- return std::nullopt;
- break;
- default: break;
- }
- r.pos = end;
- }
- if (r.failed)
- return std::nullopt;
- // the parameter values set the base / envelope / LFO levels (FL pushes them after loading)
- if (params.size() >= 880)
- for (int g = 0; g < 16; ++g)
- for (int t = 0; t < 5; ++t)
- {
- auto& p = s.articulators[(std::size_t) g].parts[(std::size_t) t];
- const auto at = (std::size_t) ((g * 5 + t) * 11);
- p.base = std::clamp (params[at + 8], 0, 256) / 256.0f;
- p.env = std::clamp (params[at + 9], -256, 256) / 256.0f;
- p.lfo = std::clamp (params[at + 10], -256, 256) / 256.0f;
- }
- return s;
- }
- std::vector<std::uint8_t> writeEnvelope (const EnvelopeSettings& s)
- {
- std::vector<std::uint8_t> out, chunk;
- put<std::uint32_t> (out, 0x10003);
- for (const auto v : { s.enabled ? 1 : 0, s.shownArticulator, s.shownTarget, s.shownPart })
- put<std::int32_t> (chunk, v);
- writeChunk (out, 1003, chunk);
- chunk.clear();
- for (const auto& a : s.articulators)
- for (const auto& p : a.parts)
- for (const auto v : { p.adsr[0], p.adsr[1], p.adsr[2], p.adsr[3], p.speed, p.tension, p.skew, p.pulseWidth,
- (std::int32_t) std::lround (p.base * 256.0f), (std::int32_t) std::lround (p.env * 256.0f),
- (std::int32_t) std::lround (p.lfo * 256.0f) })
- put<std::int32_t> (chunk, v);
- writeChunk (out, 1008, chunk);
- chunk.clear();
- for (const auto t : s.toggles)
- put<std::int32_t> (chunk, t);
- writeChunk (out, 1007, chunk);
- for (int g = 0; g < 16; ++g)
- {
- const auto& a = s.articulators[(std::size_t) g];
- chunk.clear();
- for (const auto v : { g, a.colour, a.low, a.high })
- put<std::int32_t> (chunk, v);
- writeChunk (out, 1004, chunk);
- for (int t = 0; t < 5; ++t)
- {
- chunk.clear();
- for (const auto v : { g, t, 0 })
- put<std::int32_t> (chunk, v);
- writeChunk (out, 1005, chunk);
- chunk.clear();
- writePart (chunk, a.parts[(std::size_t) t]);
- writeChunk (out, 1006, chunk);
- }
- }
- return out;
- }
- // ---------------------------------------------------------------------------------------------------------------
- // Presets
- std::vector<Preset> builtInPresets (Kind kind)
- {
- std::vector<Preset> out;
- if (kind == Kind::keyMapper)
- {
- // scales: each key plays the nearest scale note at or below it (as FL's scale presets do)
- struct Scale
- {
- const char* name;
- std::vector<int> notes;
- };
- const std::vector<Scale> scales {
- { "Major", { 0, 2, 4, 5, 7, 9, 11 } },
- { "Natural minor", { 0, 2, 3, 5, 7, 8, 10 } },
- { "Harmonic minor", { 0, 2, 3, 5, 7, 8, 11 } },
- { "Melodic minor", { 0, 2, 3, 5, 7, 9, 11 } },
- { "Dorian", { 0, 2, 3, 5, 7, 9, 10 } },
- { "Phrygian", { 0, 1, 3, 5, 7, 8, 10 } },
- { "Lydian", { 0, 2, 4, 6, 7, 9, 11 } },
- { "Mixolydian", { 0, 2, 4, 5, 7, 9, 10 } },
- { "Locrian", { 0, 1, 3, 5, 6, 8, 10 } },
- { "Major pentatonic", { 0, 2, 4, 7, 9 } },
- { "Minor pentatonic", { 0, 3, 5, 7, 10 } },
- { "Blues", { 0, 3, 5, 6, 7, 10 } },
- { "Whole tone", { 0, 2, 4, 6, 8, 10 } },
- { "Diminished", { 0, 2, 3, 5, 6, 8, 9, 11 } },
- };
- for (const auto& scale : scales)
- {
- auto s = defaultKeyMapper();
- for (int pc = 0; pc < 12; ++pc)
- {
- int below = 0;
- for (const auto n : scale.notes)
- if (n <= pc)
- below = n;
- s.rows[(std::size_t) pc] = { below };
- }
- out.push_back ({ "Scales", scale.name, writeKeyMapper (s) });
- }
- auto shifted = [&] (const char* name, std::vector<int> intervals) {
- auto s = defaultKeyMapper();
- for (int pc = 0; pc < 12; ++pc)
- {
- s.rows[(std::size_t) pc].clear();
- for (const auto i : intervals)
- s.rows[(std::size_t) pc].push_back (pc + i);
- }
- out.push_back ({ "Harmony", name, writeKeyMapper (s) });
- };
- shifted ("Octave up", { 0, 12 });
- shifted ("Octave down", { -12, 0 });
- shifted ("Fifth up", { 0, 7 });
- shifted ("Fifth down", { -5, 0 });
- shifted ("Power chord", { 0, 7, 12 });
- shifted ("Major triad", { 0, 4, 7 });
- shifted ("Minor triad", { 0, 3, 7 });
- auto inverted = defaultKeyMapper();
- for (int pc = 0; pc < 12; ++pc)
- inverted.rows[(std::size_t) pc] = { 11 - pc };
- out.push_back ({ "Special", "Inverted", writeKeyMapper (inverted) });
- }
- if (kind == Kind::levelScaler)
- {
- using L = LevelScalerSettings;
- auto make = [&] (const char* folder, const char* name, std::function<void (L&)> f) {
- auto s = defaultLevelScaler();
- f (s);
- out.push_back ({ folder, name, writeLevelScaler (s) });
- };
- make ("Velocity", "Humanize (subtle)", [] (L& s) { s.at (L::velocity, L::humanize) = 2400; });
- make ("Velocity", "Humanize (strong)", [] (L& s) { s.at (L::velocity, L::humanize) = 6000; });
- make ("Velocity", "Softer", [] (L& s) { s.at (L::velocity, L::multiply) = 6144; });
- make ("Velocity", "Compress", [] (L& s) {
- s.at (L::velocity, L::center) = 6400;
- s.at (L::velocity, L::multiply) = 4096;
- });
- make ("Velocity", "Expand", [] (L& s) {
- s.at (L::velocity, L::center) = 6400;
- s.at (L::velocity, L::multiply) = 12288;
- });
- make ("Velocity", "Fixed", [] (L& s) {
- s.at (L::velocity, L::multiply) = 0;
- s.at (L::velocity, L::offset) = 6400;
- });
- make ("Velocity", "Inverted", [] (L& s) {
- s.at (L::velocity, L::center) = 4096;
- s.at (L::velocity, L::multiply) = -8192;
- });
- make ("Other", "Random pan", [] (L& s) { s.at (L::pan, L::humanize) = 8192; });
- make ("Other", "Detune (random fine pitch)", [] (L& s) { s.at (L::finePitch, L::humanize) = 300; });
- make ("Other", "Short releases", [] (L& s) { s.at (L::release, L::offset) = -4096; });
- }
- return out;
- }
- std::optional<std::vector<std::uint8_t>> stateFromFlPreset (const std::vector<std::uint8_t>& bytes, Kind kind)
- {
- try
- {
- const auto project = flp::parseProject (bytes, true);
- if (project.channels.empty() || kindOfFlName (project.channels.front().pluginName) != kind)
- return std::nullopt;
- return project.channels.front().pluginState;
- }
- catch (const flp::Error&)
- {
- return std::nullopt;
- }
- }
- // ---------------------------------------------------------------------------------------------------------------
- std::vector<std::uint8_t> defaultState (Kind kind)
- {
- switch (kind)
- {
- case Kind::keyMapper: return writeKeyMapper (defaultKeyMapper());
- case Kind::colorMapper: return writeColorMapper (defaultColorMapper());
- case Kind::levelScaler: return writeLevelScaler (defaultLevelScaler());
- case Kind::keyboardSplitter: return writeKeyboardSplitter (defaultKeyboardSplitter());
- case Kind::envelope: return writeEnvelope (defaultEnvelope());
- default: break;
- }
- return {};
- }
- int outputCount (Kind kind) noexcept
- {
- return kind == Kind::colorMapper || kind == Kind::keyboardSplitter ? 16 : 1;
- }
- int outputsShown (Kind kind) noexcept
- {
- return kind == Kind::colorMapper || kind == Kind::keyboardSplitter ? 4 : 1;
- }
- std::string outputName (Kind kind, int output)
- {
- if (kind == Kind::colorMapper)
- return "Voice " + std::to_string (output + 1);
- if (kind == Kind::keyboardSplitter)
- return "Zone " + std::to_string (output + 1);
- return "Voice output";
- }
- std::optional<State> readState (const std::vector<std::uint8_t>& pluginState)
- {
- Reader r { pluginState.data(), pluginState.size() };
- if (r.u32() != lynxMagic)
- return std::nullopt;
- const auto kind = r.u32();
- const auto port = r.i32();
- r.u32(); // (reserved)
- if (r.failed || kind >= (std::uint32_t) kindCount)
- return std::nullopt;
- State s;
- s.kind = (Kind) kind;
- s.port = std::clamp (port, 0, 255);
- s.fl.assign (pluginState.begin() + (std::ptrdiff_t) r.pos, pluginState.end());
- return s;
- }
- std::vector<std::uint8_t> writeState (const State& state)
- {
- std::vector<std::uint8_t> out;
- put<std::uint32_t> (out, lynxMagic);
- put<std::uint32_t> (out, (std::uint32_t) state.kind);
- put<std::int32_t> (out, state.port);
- put<std::uint32_t> (out, 0);
- out.insert (out.end(), state.fl.begin(), state.fl.end());
- return out;
- }
- void makeChannel (ChannelDef& channel, Kind kind)
- {
- channel.instrument = InstrumentKind::vfx;
- channel.name = flName (kind);
- State s;
- s.kind = kind;
- s.fl = defaultState (kind);
- channel.pluginState = writeState (s);
- }
- std::optional<Kind> kindOf (const ChannelDef& channel)
- {
- if (channel.instrument != InstrumentKind::vfx)
- return std::nullopt;
- if (auto s = readState (channel.pluginState))
- return s->kind;
- return std::nullopt;
- }
- // ---------------------------------------------------------------------------------------------------------------
- // Processors
- namespace
- {
- // What every processor keeps: the voices it sent out for each input voice, so an input's "off" ends exactly them
- // (even after the settings changed) and releaseAll ends everything.
- struct VoiceTable
- {
- struct Entry
- {
- int input = -1; // input id
- int id = 0; // output voice id
- int output = 0;
- Voice voice;
- float slide = 0.0f; // Key Mapper's pitch slide: cents still to go (0 = none)
- };
- std::array<Entry, 512> entries {};
- int count = 0;
- int nextId = 1;
- int add (int input, int output, const Voice& v) noexcept
- {
- if (count >= (int) entries.size())
- return -1;
- auto& e = entries[(std::size_t) count++];
- e.input = input;
- e.output = output;
- e.voice = v;
- e.slide = 0.0f;
- e.id = nextId++;
- if (nextId > 1 << 30)
- nextId = 1;
- return e.id;
- }
- // sends the "off" of every voice of `input` (all with input < 0)
- void release (int input, int offset, float releaseVelocity, EventList& out) noexcept
- {
- int kept = 0;
- for (int i = 0; i < count; ++i)
- {
- auto& e = entries[(std::size_t) i];
- if (input >= 0 && e.input != input)
- {
- entries[(std::size_t) kept++] = e;
- continue;
- }
- Event off;
- off.type = Event::Type::off;
- off.offset = offset;
- off.output = e.output;
- off.id = e.id;
- off.voice = e.voice;
- off.voice.release = releaseVelocity;
- out.add (off);
- }
- count = kept;
- }
- };
- Event onEvent (int offset, int output, int id, const Voice& v) noexcept
- {
- Event e;
- e.type = Event::Type::on;
- e.offset = offset;
- e.output = output;
- e.id = id;
- e.voice = v;
- return e;
- }
- // A processor whose settings are FL's parsed state
- template <typename Settings>
- class Base : public Processor
- {
- public:
- explicit Base (Settings initial) : settings (std::move (initial)) {}
- void releaseAll (EventList& out) noexcept override { voices.release (-1, 0, 0.5f, out); }
- bool busy() const noexcept override { return voices.count > 0; }
- protected:
- const Settings& current() noexcept
- {
- settings.take();
- return settings.get();
- }
- void publish (Settings s) { settings.publish (std::move (s)); }
- VoiceTable voices;
- private:
- Handoff<Settings> settings;
- };
- // Notes pass unchanged (a plugin whose behaviour LYNX does not reproduce yet keeps the notes flowing)
- class ThroughProcessor final : public Processor
- {
- public:
- explicit ThroughProcessor (Kind k) : which (k) {}
- Kind kind() const noexcept override { return which; }
- void releaseAll (EventList& out) noexcept override { voices.release (-1, 0, 0.5f, out); }
- void process (const Event* in, int count, int, const Transport&, EventList& out) noexcept override
- {
- for (int i = 0; i < count; ++i)
- {
- const auto& e = in[i];
- if (e.type == Event::Type::on)
- {
- if (const auto id = voices.add (e.id, 0, e.voice); id >= 0)
- out.add (onEvent (e.offset, 0, id, e.voice));
- }
- else if (e.type == Event::Type::off)
- voices.release (e.id, e.offset, e.voice.release, out);
- }
- }
- bool configure (const std::vector<std::uint8_t>&) override { return true; }
- private:
- Kind which;
- VoiceTable voices;
- };
- class KeyMapper final : public Base<KeyMapperSettings>
- {
- public:
- explicit KeyMapper (KeyMapperSettings s) : Base (std::move (s)) {}
- Kind kind() const noexcept override { return Kind::keyMapper; }
- bool configure (const std::vector<std::uint8_t>& fl) override
- {
- auto s = parseKeyMapper (fl);
- if (! s)
- return false;
- publish (std::move (*s));
- return true;
- }
- void process (const Event* in, int count, int n, const Transport& transport, EventList& out) noexcept override
- {
- const auto& s = current();
- // the slides under way move on (FL: the voice's pitch covers 99 % of the way in `slide` / 192 beats)
- if (s.slide > 0)
- {
- const auto beats = (double) n * transport.bpm / (60.0 * std::max (1.0, transport.sampleRate));
- const auto keep = (float) std::pow (0.01, beats * 192.0 / (double) s.slide);
- for (int i = 0; i < voices.count; ++i)
- slideStep (voices.entries[(std::size_t) i], keep, out);
- }
- else
- for (int i = 0; i < voices.count; ++i)
- slideStep (voices.entries[(std::size_t) i], 0.0f, out);
- for (int i = 0; i < count; ++i)
- {
- const auto& e = in[i];
- if (e.type == Event::Type::off)
- {
- voices.release (e.id, e.offset, e.voice.release, out);
- continue;
- }
- if (e.type != Event::Type::on)
- continue;
- if (! s.enabled)
- {
- send (e, e.voice, 0, out);
- continue;
- }
- // FL: the row of the input's pitch class counted from the base key; its values are semitones from the start
- // of that octave (on the base key); the offset transposes what comes out
- const auto k = std::max (0, e.voice.key - s.baseKey + 12);
- const auto rowIndex = k % 12;
- const auto octaveStart = e.voice.key - rowIndex;
- const auto& row = s.rows[(std::size_t) rowIndex];
- const auto size = (int) row.size();
- if (size == 0)
- continue; // (the note is muted)
- auto play = [&] (int value, float gain) {
- auto v = e.voice;
- v.key = octaveStart + value + s.offset;
- if (v.key < 0 || v.key > 127)
- return;
- v.velocity *= gain;
- // with a pitch slide the new note starts at the played pitch and glides to its own
- const auto slide = s.slide > 0 ? (float) (e.voice.key - v.key) * 100.0f : 0.0f;
- send (e, v, slide, out);
- };
- switch (s.multimode)
- {
- case 1: // all, gain-compensated (1 / sqrt n)
- for (const auto value : row)
- play (value, 1.0f / std::sqrt ((float) size));
- break;
- case 2: // one of them at random
- play (row[(std::size_t) (nextRandom() % (std::uint32_t) size)], 1.0f);
- break;
- case 3: // the next one in the row's order (the first first)
- {
- auto& c = cycle[(std::size_t) rowIndex];
- const auto at = ((c % size) + size) % size;
- c = at + 1;
- play (row[(std::size_t) at], 1.0f);
- break;
- }
- case 4: // backwards (the last first)
- {
- auto& c = cycle[(std::size_t) rowIndex];
- c = (((c + size - 1) % size) + size) % size;
- play (row[(std::size_t) c], 1.0f);
- break;
- }
- default:
- for (const auto value : row)
- play (value, 1.0f);
- break;
- }
- }
- }
- void releaseAll (EventList& out) noexcept override
- {
- voices.release (-1, 0, 0.5f, out);
- cycle.fill (0); // (FL: a flush starts the cycles again)
- }
- private:
- void send (const Event& input, Voice v, float slide, EventList& out) noexcept
- {
- const auto cents = v.cents;
- v.cents += slide;
- if (const auto id = voices.add (input.id, 0, v); id >= 0)
- {
- out.add (onEvent (input.offset, 0, id, v));
- auto& entry = voices.entries[(std::size_t) voices.count - 1];
- entry.slide = slide;
- entry.voice.cents = cents;
- }
- }
- // one block of a voice's slide: `keep` of the way left stays (a prop event with its pitch)
- static void slideStep (VoiceTable::Entry& e, float keep, EventList& out) noexcept
- {
- if (e.slide == 0.0f)
- return;
- e.slide *= keep;
- if (std::abs (e.slide) < 0.5f)
- e.slide = 0.0f;
- Event p;
- p.type = Event::Type::prop;
- p.output = e.output;
- p.id = e.id;
- p.voice = e.voice;
- p.prop = Prop::pitch;
- p.value = e.voice.cents + e.slide;
- out.add (p);
- }
- std::uint32_t nextRandom() noexcept
- {
- seed ^= seed << 13;
- seed ^= seed >> 17;
- seed ^= seed << 5;
- return seed;
- }
- std::array<int, 12> cycle {};
- std::uint32_t seed = 0x9e3779b9u;
- };
- class ColorMapper final : public Base<ColorMapperSettings>
- {
- public:
- explicit ColorMapper (ColorMapperSettings s) : Base (std::move (s)) {}
- Kind kind() const noexcept override { return Kind::colorMapper; }
- bool configure (const std::vector<std::uint8_t>& fl) override
- {
- auto s = parseColorMapper (fl);
- if (! s)
- return false;
- publish (*s);
- return true;
- }
- void process (const Event* in, int count, int, const Transport&, EventList& out) noexcept override
- {
- const auto& s = current();
- for (int i = 0; i < count; ++i)
- {
- const auto& e = in[i];
- if (e.type == Event::Type::off)
- {
- voices.release (e.id, e.offset, e.voice.release, out);
- continue;
- }
- if (e.type != Event::Type::on)
- continue;
- const auto c = e.voice.colour & 15;
- auto v = e.voice;
- v.colour = s.colour[(std::size_t) c];
- const auto port = s.port[(std::size_t) c];
- if (const auto id = voices.add (e.id, port, v); id >= 0)
- out.add (onEvent (e.offset, port, id, v));
- }
- }
- };
- } // namespace
- namespace
- {
- // One voice in, one voice out, its velocity, fine pitch, pan, Mod X / Y and release velocity through the curves.
- // (FL re-evaluates the curves every tick from the parent's live levels; LYNX applies them when the note starts and
- // to its release velocity when it ends: the voices' levels do not change on their way here.)
- class LevelScaler final : public Base<LevelScalerSettings>
- {
- public:
- explicit LevelScaler (LevelScalerSettings s) : Base (std::move (s)) {}
- Kind kind() const noexcept override { return Kind::levelScaler; }
- bool configure (const std::vector<std::uint8_t>& fl) override
- {
- auto s = parseLevelScaler (fl);
- if (! s)
- return false;
- publish (std::move (*s));
- return true;
- }
- void process (const Event* in, int count, int, const Transport&, EventList& out) noexcept override
- {
- using L = LevelScalerSettings;
- const auto& s = current();
- for (int i = 0; i < count; ++i)
- {
- const auto& e = in[i];
- if (e.type == Event::Type::on)
- {
- auto v = e.voice;
- // (FL's order: pan, Mod X, Mod Y, fine pitch, velocity; each with its own random, fixed for the note)
- v.pan = (float) levelScale (s, L::pan, v.pan, -1.0, 1.0, random());
- v.modX = (float) (levelScale (s, L::modX, v.modX * 2.0 - 1.0, -1.0, 1.0, random()) * 0.5 + 0.5);
- v.modY = (float) (levelScale (s, L::modY, v.modY * 2.0 - 1.0, -1.0, 1.0, random()) * 0.5 + 0.5);
- v.cents = (float) (levelScale (s, L::finePitch, v.cents / 1200.0, -1.0, 1.0, random()) * 1200.0);
- v.velocity = (float) levelScale (s, L::velocity, v.velocity, 0.0, 1.0, random());
- if (const auto id = voices.add (e.id, 0, v); id >= 0)
- out.add (onEvent (e.offset, 0, id, v));
- }
- else if (e.type == Event::Type::off)
- voices.release (e.id, e.offset, (float) levelScale (s, L::release, e.voice.release, 0.0, 1.0, random()), out);
- }
- }
- private:
- double random() noexcept
- {
- seed ^= seed << 13;
- seed ^= seed >> 17;
- seed ^= seed << 5;
- return (double) (seed >> 8) / 16777216.0;
- }
- std::uint32_t seed = 0x2545f491u;
- };
- } // namespace
- namespace
- {
- // Each note on every zone whose keyboard × velocity level is above zero, on that zone's output (FL: the level
- // scales the note's volume, or replaces it with Absolute; Random zones play one of them, picked by their keyboard
- // level). LYNX works in velocities: vol = (11^(1.28 v) − 1) / 10 both ways.
- class KeyboardSplitter final : public Base<KeyboardSplitterSettings>
- {
- public:
- explicit KeyboardSplitter (KeyboardSplitterSettings s) : Base (std::move (s)) {}
- Kind kind() const noexcept override { return Kind::keyboardSplitter; }
- bool configure (const std::vector<std::uint8_t>& fl) override
- {
- auto s = parseKeyboardSplitter (fl);
- if (! s)
- return false;
- publish (std::move (*s));
- return true;
- }
- void process (const Event* in, int count, int, const Transport&, EventList& out) noexcept override
- {
- const auto& s = current();
- for (int i = 0; i < count; ++i)
- {
- const auto& e = in[i];
- if (e.type == Event::Type::off)
- {
- voices.release (e.id, e.offset, e.voice.release, out);
- continue;
- }
- if (e.type != Event::Type::on)
- continue;
- const auto x = ((double) e.voice.key + e.voice.cents / 100.0) / 132.0;
- const auto velocity = std::min (1.0, (double) e.voice.velocity);
- std::array<double, 16> w {};
- bool anyRandom = false;
- for (int z = 0; z < 16; ++z)
- {
- const auto& zone = s.zones[(std::size_t) z];
- if (! zone.active())
- continue;
- w[(std::size_t) z] = splitterGain (zone.keyboard.at (x));
- anyRandom = anyRandom || zone.random;
- }
- if (anyRandom) // one weighted pick among the random zones, which then plays at full level
- {
- double total = 0.0;
- for (int z = 0; z < 16; ++z)
- if (s.zones[(std::size_t) z].random && s.zones[(std::size_t) z].active())
- total += w[(std::size_t) z];
- const auto r = random() * total;
- int chosen = -1;
- double acc = 0.0;
- for (int z = 0; z < 16; ++z)
- if (s.zones[(std::size_t) z].random && s.zones[(std::size_t) z].active() && w[(std::size_t) z] > 0.0)
- {
- if (acc <= r)
- chosen = z;
- acc += w[(std::size_t) z];
- w[(std::size_t) z] = 0.0;
- }
- if (chosen >= 0) // (FL then falls back on zone 1; LYNX plays no random zone)
- w[(std::size_t) chosen] = 1.0;
- }
- const auto volumeIn = velocityToVolume (velocity);
- for (int z = 0; z < 16; ++z)
- {
- const auto& zone = s.zones[(std::size_t) z];
- if (w[(std::size_t) z] <= 0.0)
- continue;
- const auto level = w[(std::size_t) z] * splitterGain (zone.velocity.at (velocity));
- if (level <= 1e-5)
- continue;
- auto v = e.voice;
- v.velocity = (float) volumeToVelocity (zone.absolute ? level : volumeIn * level);
- v.key = e.voice.key + zone.transpose;
- if (v.key < 0 || v.key > 127)
- continue;
- if (const auto id = voices.add (e.id, z, v); id >= 0)
- out.add (onEvent (e.offset, z, id, v));
- }
- }
- }
- private:
- static double velocityToVolume (double v) noexcept { return (std::pow (11.0, 1.28 * v) - 1.0) / 10.0; }
- static double volumeToVelocity (double vol) noexcept { return std::clamp (std::log (10.0 * vol + 1.0) / std::log (11.0) / 1.28, 0.0, 1.0); }
- double random() noexcept
- {
- seed ^= seed << 13;
- seed ^= seed >> 17;
- seed ^= seed << 5;
- return (double) (seed >> 8) / 16777216.0;
- }
- std::uint32_t seed = 0x12bd6aau;
- };
- } // namespace
- namespace
- {
- // An FL curve as the Envelope Controller's (core/FlSynths.h): accumulated points, LYNX's tension sign on the segment
- // starting at a point, a hold as a step
- void toArticulator (const MapCurve& m, ControllerArticulator& a)
- {
- a.count = 0;
- a.sustain = -1;
- a.decayPoint = -1;
- const auto sustain = m.footer[4], decay = m.footer[2];
- for (std::size_t i = 0; i < m.points.size() && a.count < (int) a.time.size(); ++i)
- {
- const auto& p = m.points[i];
- if (i > 0)
- {
- if (p.curve == 2 && p.x > m.points[i - 1].x && a.count + 1 < (int) a.time.size())
- {
- a.time[(std::size_t) a.count] = (float) p.x;
- a.level[(std::size_t) a.count] = a.level[(std::size_t) a.count - 1];
- a.tension[(std::size_t) a.count] = 0.0f;
- ++a.count;
- }
- else if (p.curve <= 1)
- a.tension[(std::size_t) a.count - 1] = std::clamp (-p.tension, -1.0f, 1.0f);
- }
- if ((int) i == sustain)
- a.sustain = a.count;
- if ((int) i == decay)
- a.decayPoint = a.count;
- a.time[(std::size_t) a.count] = (float) p.x;
- a.level[(std::size_t) a.count] = (float) std::clamp (p.y, 0.0, 1.0);
- a.tension[(std::size_t) a.count] = 0.0f;
- ++a.count;
- }
- }
- // What the audio thread evaluates: per articulator and target the envelope, the LFO amplitude and the LFO wave
- struct EnvelopeRuntime
- {
- struct Part
- {
- bool envOn = false, lfoOn = false, envTempo = false, lfoTempo = false;
- float base = 0.5f, env = 0.0f, lfo = 0.0f, offset = 0.5f;
- ControllerArticulator envelope, amplitude, wave; // (levels 0…1 with base 0: their curves' values)
- double rate = 1.0, startPhase = 0.0; // LFO: cycles per unit, phase at the trigger
- };
- struct Articulator
- {
- int colour = 0, low = 0, high = 132;
- std::array<Part, 5> parts;
- };
- bool enabled = true;
- std::array<Articulator, 16> articulators;
- // (big: on the heap, never on a stack)
- static std::unique_ptr<EnvelopeRuntime> from (const EnvelopeSettings& s)
- {
- auto made = std::make_unique<EnvelopeRuntime>();
- auto& rt = *made;
- rt.enabled = s.enabled;
- for (int g = 0; g < 16; ++g)
- {
- const auto& a = s.articulators[(std::size_t) g];
- auto& r = rt.articulators[(std::size_t) g];
- r.colour = a.colour;
- r.low = a.low;
- r.high = a.high;
- for (int t = 0; t < 5; ++t)
- {
- const auto& p = a.parts[(std::size_t) t];
- auto& q = r.parts[(std::size_t) t];
- q.envOn = p.envOn() && ! p.envelope.points.empty();
- q.lfoOn = p.lfoOn();
- q.envTempo = (p.envelope.footer[0] & 1) != 0;
- q.lfoTempo = (p.lfoAmp.footer[0] & 1) != 0;
- q.base = p.base;
- q.env = p.env;
- q.lfo = p.lfo;
- q.offset = t == EnvelopeSettings::velocity ? 0.0f : 0.5f;
- toArticulator (p.envelope, q.envelope);
- q.envelope.adsr = p.adsr;
- q.envelope = controllerWithKnobs (q.envelope);
- for (auto* c : { &q.envelope, &q.amplitude })
- {
- c->base = 0.0f;
- c->envLevel = 1.0f;
- c->bipolar = false;
- c->envOn = true;
- }
- toArticulator (p.lfoAmp, q.amplitude);
- q.wave.lfoSpeed = p.speed;
- q.wave.lfoTension = p.tension;
- q.wave.lfoSkew = p.skew;
- q.wave.lfoPulseWidth = p.pulseWidth;
- q.wave.lfoPhase = p.phase;
- q.rate = controllerLfoRate (q.wave);
- q.startPhase = controllerLfoStartPhase (q.wave);
- }
- }
- return made;
- }
- };
- // One note in, the same note out, its volume, fine pitch, pan and Mod X / Y moved by the envelopes and LFOs of every
- // articulator whose note colour and key range it matches (FL re-applies them each tick; LYNX each block).
- class Envelope final : public Processor
- {
- public:
- explicit Envelope (const EnvelopeSettings& s) : settings (EnvelopeRuntime::from (s)) {}
- Kind kind() const noexcept override { return Kind::envelope; }
- bool configure (const std::vector<std::uint8_t>& fl) override
- {
- auto s = parseEnvelope (fl);
- if (! s)
- return false;
- settings.publish (EnvelopeRuntime::from (*s));
- return true;
- }
- bool busy() const noexcept override { return count > 0; }
- void releaseAll (EventList& out) noexcept override
- {
- for (int i = 0; i < count; ++i)
- if (! voices[(std::size_t) i].released)
- out.add (offEvent (voices[(std::size_t) i], 0));
- count = 0;
- }
- void process (const Event* in, int n, int samples, const Transport& transport, EventList& out) noexcept override
- {
- settings.take();
- const auto& rt = settings.get();
- const auto seconds = samples / std::max (1.0, transport.sampleRate);
- const auto beats = seconds * transport.bpm / 60.0;
- // the voices sounding move on (and send what changed)
- for (int i = 0; i < count; ++i)
- {
- auto& v = voices[(std::size_t) i];
- v.seconds += seconds;
- v.beats += beats;
- if (v.released)
- {
- v.releaseSeconds += seconds;
- v.releaseBeats += beats;
- }
- apply (rt, v, 0, &out);
- }
- // the finished ones go (their output was released and their envelopes have ended, or 30 s after the release)
- int kept = 0;
- for (int i = 0; i < count; ++i)
- {
- const auto& v = voices[(std::size_t) i];
- if (v.released && (v.releaseSeconds > 30.0 || finished (rt, v)))
- continue;
- voices[(std::size_t) kept++] = v;
- }
- count = kept;
- for (int i = 0; i < n; ++i)
- {
- const auto& e = in[i];
- if (e.type == Event::Type::on)
- {
- Voice v;
- v.input = e.id;
- v.id = nextId++;
- if (nextId > 1 << 30)
- nextId = 1;
- v.source = e.voice;
- v.sent = e.voice;
- for (int g = 0; g < 16; ++g)
- {
- const auto& a = rt.articulators[(std::size_t) g];
- const auto key = e.voice.key + 1;
- if (a.colour == e.voice.colour && key >= a.low && key <= a.high)
- v.matched |= (std::uint16_t) (1u << g);
- }
- Voice playing = v;
- apply (rt, playing, e.offset, nullptr);
- out.add (onEvent (e.offset, 0, playing.id, playing.sent));
- if (count < (int) voices.size())
- voices[(std::size_t) count++] = playing;
- }
- else if (e.type == Event::Type::off)
- for (int k = 0; k < count; ++k)
- {
- auto& v = voices[(std::size_t) k];
- if (v.input == e.id && ! v.released)
- {
- v.released = true;
- v.source.release = e.voice.release;
- out.add (offEvent (v, e.offset));
- }
- }
- }
- }
- private:
- struct Voice
- {
- int input = -1, id = 0;
- Voice() = default;
- vfx::Voice source, sent;
- std::uint16_t matched = 0;
- bool released = false;
- double seconds = 0.0, beats = 0.0, releaseSeconds = 0.0, releaseBeats = 0.0;
- };
- static Event offEvent (const Voice& v, int offset) noexcept
- {
- Event e;
- e.type = Event::Type::off;
- e.offset = offset;
- e.id = v.id;
- e.voice = v.sent;
- e.voice.release = v.source.release;
- return e;
- }
- // the voice's levels now (FL's per-voice apply); with `out`, prop events for what changed
- static void apply (const EnvelopeRuntime& rt, Voice& v, int offset, EventList* out) noexcept
- {
- auto now = v.source;
- if (rt.enabled && v.matched != 0)
- {
- const auto sourceVolume = (std::pow (11.0, 1.28 * (double) v.source.velocity) - 1.0) / 10.0;
- auto volume = sourceVolume;
- double cents = v.source.cents, pan = v.source.pan, modX = v.source.modX * 2.0 - 1.0, modY = v.source.modY * 2.0 - 1.0;
- for (int g = 15; g >= 0; --g) // (FL: the last articulator first)
- {
- if ((v.matched & (1u << g)) == 0)
- continue;
- for (int t = 4; t >= 0; --t)
- {
- const auto& p = rt.articulators[(std::size_t) g].parts[(std::size_t) t];
- if (! p.envOn && ! p.lfoOn)
- continue;
- const auto envTime = p.envTempo ? v.beats : v.seconds;
- const auto envRelease = ! v.released ? -1.0 : p.envTempo ? v.releaseBeats : v.releaseSeconds;
- const auto e = p.envOn ? controllerValue (p.envelope, envTime, envRelease) - p.offset : 0.0f;
- double l = 0.0;
- if (p.lfoOn)
- {
- const auto lfoTime = p.lfoTempo ? v.beats : v.seconds;
- const auto lfoRelease = ! v.released ? -1.0 : p.lfoTempo ? v.releaseBeats : v.releaseSeconds;
- const auto amp = controllerValue (p.amplitude, lfoTime, lfoRelease) - 0.5;
- l = amp * controllerLfoWave (p.wave, p.startPhase + p.rate * lfoTime);
- }
- if (t == EnvelopeSettings::velocity)
- {
- double m = p.base;
- if (p.envOn)
- m = p.env >= 0.0f ? p.base + 2.0 * p.env * e : (1.0 - p.base) + 2.0 * std::abs (p.env) * (1.0 - e);
- if (p.lfoOn)
- m *= 2.0 * p.lfo * l;
- volume = std::clamp (volume * m, 0.0, 4.0);
- continue;
- }
- auto d = (double) p.base - 0.5;
- if (p.envOn)
- d += 2.0 * p.env * e;
- if (p.lfoOn)
- d += 2.0 * p.lfo * l;
- if (t == EnvelopeSettings::pitch)
- cents += d * 4800.0;
- else if (t == EnvelopeSettings::pan)
- pan = std::clamp (pan + d, -1.0, 1.0);
- else if (t == EnvelopeSettings::modX)
- modX = std::clamp (modX + d, -2.0, 2.0);
- else
- modY = std::clamp (modY + d, -2.0, 2.0);
- }
- }
- if (volume != sourceVolume)
- now.velocity = (float) std::clamp (std::log (10.0 * volume + 1.0) / std::log (11.0) / 1.28, 0.0, 1.0);
- now.cents = (float) std::clamp (cents, -v.source.key * 100.0, 12000.0 - v.source.key * 100.0); // (FL: Init.Pitch 0…12000)
- now.pan = (float) pan;
- now.modX = (float) std::clamp (modX * 0.5 + 0.5, 0.0, 1.0);
- now.modY = (float) std::clamp (modY * 0.5 + 0.5, 0.0, 1.0);
- }
- if (out != nullptr)
- {
- auto send = [&] (Prop prop, float before, float after) {
- if (std::abs (after - before) < 1e-4f)
- return;
- Event p;
- p.type = Event::Type::prop;
- p.offset = offset;
- p.id = v.id;
- p.voice = v.sent;
- p.prop = prop;
- p.value = after;
- out->add (p);
- };
- send (Prop::velocity, v.sent.velocity, now.velocity);
- send (Prop::pitch, v.sent.cents, now.cents);
- send (Prop::pan, v.sent.pan, now.pan);
- send (Prop::modX, v.sent.modX, now.modX);
- send (Prop::modY, v.sent.modY, now.modY);
- }
- now.key = v.source.key;
- v.sent = now;
- }
- static bool finished (const EnvelopeRuntime& rt, const Voice& v) noexcept
- {
- if (! rt.enabled || v.matched == 0)
- return true;
- for (int g = 0; g < 16; ++g)
- if ((v.matched & (1u << g)) != 0)
- for (const auto& p : rt.articulators[(std::size_t) g].parts)
- {
- if (p.envOn && ! controllerVoiceFinished (p.envelope, p.envTempo ? v.beats : v.seconds, p.envTempo ? v.releaseBeats : v.releaseSeconds))
- return false;
- if (p.lfoOn && ! controllerVoiceFinished (p.amplitude, p.lfoTempo ? v.beats : v.seconds, p.lfoTempo ? v.releaseBeats : v.releaseSeconds))
- return false;
- }
- return true;
- }
- Handoff<EnvelopeRuntime> settings;
- std::array<Voice, 256> voices {};
- int count = 0, nextId = 1;
- };
- } // namespace
- std::unique_ptr<Processor> makeProcessor (Kind kind, const std::vector<std::uint8_t>& fl)
- {
- switch (kind)
- {
- case Kind::keyMapper:
- return std::make_unique<KeyMapper> (parseKeyMapper (fl).value_or (defaultKeyMapper()));
- case Kind::colorMapper:
- return std::make_unique<ColorMapper> (parseColorMapper (fl).value_or (defaultColorMapper()));
- case Kind::levelScaler:
- return std::make_unique<LevelScaler> (parseLevelScaler (fl).value_or (defaultLevelScaler()));
- case Kind::keyboardSplitter:
- return std::make_unique<KeyboardSplitter> (parseKeyboardSplitter (fl).value_or (defaultKeyboardSplitter()));
- case Kind::envelope:
- return std::make_unique<Envelope> (parseEnvelope (fl).value_or (defaultEnvelope()));
- default: break;
- }
- return std::make_unique<ThroughProcessor> (kind);
- }
- } // namespace lynx::vfx
|