|
|
@@ -0,0 +1,348 @@
|
|
|
+// Round 190tk (QUEUE 0.1.33 item 92, owner decision: imported FL projects): FL's automation timing
|
|
|
+// (docs/analise/REA_rodada190tk.md). FL cuts its blocks at every tick of the project's PPQ (tick k starts at sample
|
|
|
+// rint(Σ samples per tick)) and, at a tick's start, reads each playing clip one tick on: the target holds the clip's
|
|
|
+// value at the END of the tick for the whole tick. A song with Song::flAutomationPpq > 0 (an imported .flp) plays so;
|
|
|
+// LYNX's own projects (0) keep LYNX's timing. The FL render cases (FL 2026 /R renders of DC probes) are in the app's
|
|
|
+// --selftest "round190tk" (src/Round190tkCheck.inc) too.
|
|
|
+
|
|
|
+#include <filesystem>
|
|
|
+#include <fstream>
|
|
|
+
|
|
|
+#include "core/FlpExport.h"
|
|
|
+#include "core/ThreeOsc.h"
|
|
|
+
|
|
|
+namespace at190tk
|
|
|
+{
|
|
|
+// A Master fader automated by automation 0 (slot 0 shows what it plays): a clip from `clipBeat` with `points`, a long
|
|
|
+// empty pattern clip so the song never wraps
|
|
|
+struct Rig
|
|
|
+{
|
|
|
+ lynx::Song song;
|
|
|
+ Rig (int flPpq, double bpm, std::vector<lynx::AutomationPoint> points, double clipBeat = 0.0, double clipBeats = 16.0)
|
|
|
+ {
|
|
|
+ song = lynx::Song::makeDefault();
|
|
|
+ song.mode = lynx::PlayMode::song;
|
|
|
+ song.bpm = bpm;
|
|
|
+ song.flAutomationPpq = flPpq;
|
|
|
+ song.channels.clear();
|
|
|
+ song.channels.emplace_back().name = "Tone";
|
|
|
+ song.playlist.clear();
|
|
|
+ song.automations.clear();
|
|
|
+ song.patterns.clear();
|
|
|
+ song.mixer = lynx::Song::defaultMixer (2);
|
|
|
+ lynx::Automation a;
|
|
|
+ a.name = "Ramp";
|
|
|
+ a.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
|
|
|
+ a.points = points;
|
|
|
+ song.automations.push_back (a);
|
|
|
+ lynx::Clip c;
|
|
|
+ c.kind = lynx::ClipKind::automation;
|
|
|
+ c.automation = 0;
|
|
|
+ c.track = 1;
|
|
|
+ c.start = (std::int64_t) std::llround (clipBeat * lynx::ppq);
|
|
|
+ c.length = (std::int64_t) std::llround (clipBeats * lynx::ppq);
|
|
|
+ song.playlist.push_back (c);
|
|
|
+ song.patterns.emplace_back().lengthTicks = 4 * lynx::ppq;
|
|
|
+ lynx::Clip pad;
|
|
|
+ pad.kind = lynx::ClipKind::pattern;
|
|
|
+ pad.pattern = 0;
|
|
|
+ pad.track = 3;
|
|
|
+ pad.length = 256 * lynx::ppq;
|
|
|
+ song.playlist.push_back (pad);
|
|
|
+ }
|
|
|
+ // slot 0's value at each of the first `samples` samples, played from the song's start in blocks of `block` samples:
|
|
|
+ // with blocks of more than one sample, the value of each block's last sample (the last piece's) stands for the block
|
|
|
+ std::vector<float> play (int samples, int block = 1, bool followTempo = false) const
|
|
|
+ {
|
|
|
+ auto state = lynx::RackState::build (song, nullptr, 44100.0);
|
|
|
+ lynx::RackRenderer renderer;
|
|
|
+ renderer.prepare (44100.0, std::max (64, block));
|
|
|
+ std::vector<float> l ((std::size_t) block), r ((std::size_t) block), out;
|
|
|
+ out.reserve ((std::size_t) samples);
|
|
|
+ double tick = 0.0;
|
|
|
+ for (int done = 0; done < samples; done += block)
|
|
|
+ {
|
|
|
+ const auto n = std::min (block, samples - done);
|
|
|
+ renderer.process (state.get(), true, tick, n, l.data(), r.data());
|
|
|
+ for (int i = 0; i < n; ++i)
|
|
|
+ out.push_back (i == n - 1 ? renderer.automationValueForCheck (0) : std::numeric_limits<float>::quiet_NaN());
|
|
|
+ tick += n * lynx::ticksPerSample (followTempo ? renderer.currentBpm() : song.bpm, 44100.0);
|
|
|
+ }
|
|
|
+ return out;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// FL's tick starts: rint (to even) of the running sum of samples per tick
|
|
|
+std::vector<std::int64_t> flStarts (double bpm, int flPpq, int count)
|
|
|
+{
|
|
|
+ std::vector<std::int64_t> s;
|
|
|
+ const auto spt = 44100.0 * 60.0 / (bpm * flPpq);
|
|
|
+ auto acc = 0.0;
|
|
|
+ for (int k = 0; k < count; ++k)
|
|
|
+ {
|
|
|
+ s.push_back (std::llrint (acc));
|
|
|
+ acc += spt;
|
|
|
+ }
|
|
|
+ return s;
|
|
|
+}
|
|
|
+
|
|
|
+std::vector<int> changes (const std::vector<float>& v)
|
|
|
+{
|
|
|
+ std::vector<int> c;
|
|
|
+ for (std::size_t i = 1; i < v.size(); ++i)
|
|
|
+ if (! (v[i] == v[i - 1]) && ! (std::isnan (v[i]) && std::isnan (v[i - 1])))
|
|
|
+ c.push_back ((int) i);
|
|
|
+ return c;
|
|
|
+}
|
|
|
+
|
|
|
+// every sample holds the clip's value at the end of its FL tick (FL's tick starts from flStarts); NaN before the clip
|
|
|
+bool holdsTickEnds (const std::vector<float>& v, double bpm, int flPpq, double clipBeat, double clipBeats)
|
|
|
+{
|
|
|
+ const auto T = (double) lynx::ppq / flPpq;
|
|
|
+ const auto starts = flStarts (bpm, flPpq, (int) (v.size() / (44100.0 * 60.0 / (bpm * flPpq))) + 4);
|
|
|
+ std::size_t k = 0;
|
|
|
+ const auto clipStart = clipBeat * lynx::ppq, len = clipBeats * lynx::ppq;
|
|
|
+ for (std::size_t s = 0; s < v.size(); ++s)
|
|
|
+ {
|
|
|
+ while (k + 1 < starts.size() && starts[k + 1] <= (std::int64_t) s)
|
|
|
+ ++k;
|
|
|
+ const auto selected = (double) k * T >= clipStart - 1.0e-6;
|
|
|
+ const auto expected = selected ? (float) std::clamp (((double) (k + 1) * T - clipStart) / len, 0.0, 1.0) : std::numeric_limits<float>::quiet_NaN();
|
|
|
+ if (selected ? std::abs (v[s] - expected) > 1.0e-5f : ! std::isnan (v[s]))
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+// a blocked play (each block's last sample) agrees with the sample-by-sample one
|
|
|
+bool sameAtBlockEnds (const std::vector<float>& bySample, const std::vector<float>& blocked, int block)
|
|
|
+{
|
|
|
+ for (std::size_t s = (std::size_t) block - 1; s < blocked.size(); s += (std::size_t) block)
|
|
|
+ if (! (bySample[s] == blocked[s]) && ! (std::isnan (bySample[s]) && std::isnan (blocked[s])))
|
|
|
+ return false;
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+const std::vector<lynx::AutomationPoint> ramp { { 0, 0.0f, 0.0f }, { 16 * lynx::ppq, 1.0f, 0.0f } };
|
|
|
+} // namespace at190tk
|
|
|
+
|
|
|
+// The value FL hands during a tick is the clip's at the tick's end, from the tick's first sample (96 PPQ, 120 BPM: FL's
|
|
|
+// render changed at samples 230, 459, 689, 919, 1148, 1378, 1608, 1838 — 1837.5 rounded to even — and so on)
|
|
|
+TEST (autotime190tk_value_is_the_clip_at_the_tick_end_96_ppq)
|
|
|
+{
|
|
|
+ using namespace at190tk;
|
|
|
+ const Rig rig (96, 120.0, ramp);
|
|
|
+ const auto v = rig.play (6000);
|
|
|
+ CHECK (holdsTickEnds (v, 120.0, 96, 0.0, 16.0));
|
|
|
+ const auto c = changes (v);
|
|
|
+ const std::vector<int> fl { 230, 459, 689, 919, 1148, 1378, 1608, 1838 };
|
|
|
+ CHECK (c.size() > fl.size() && std::equal (fl.begin(), fl.end(), c.begin()));
|
|
|
+ CHECK (std::abs (v[0] - 10.0f / (16.0f * lynx::ppq)) < 1.0e-6f); // (tick 0 already holds the clip one tick on)
|
|
|
+ // the same samples whatever the device block (the export grid)
|
|
|
+ CHECK (sameAtBlockEnds (v, rig.play (6000, 333), 333) && sameAtBlockEnds (v, rig.play (6000, 64), 64));
|
|
|
+}
|
|
|
+
|
|
|
+TEST (autotime190tk_value_is_the_clip_at_the_tick_end_960_ppq)
|
|
|
+{
|
|
|
+ using namespace at190tk;
|
|
|
+ const Rig rig (960, 120.0, ramp);
|
|
|
+ const auto v = rig.play (3000);
|
|
|
+ CHECK (holdsTickEnds (v, 120.0, 960, 0.0, 16.0));
|
|
|
+ const auto c = changes (v);
|
|
|
+ const std::vector<int> fl { 23, 46, 69, 92, 115, 138, 161, 184 }; // (FL's render, 960 PPQ)
|
|
|
+ CHECK (c.size() > fl.size() && std::equal (fl.begin(), fl.end(), c.begin()));
|
|
|
+}
|
|
|
+
|
|
|
+// Other tempos: FL's running sum decides the ties (97 BPM: tick 97 at 27562.5 → FL's 27563)
|
|
|
+TEST (autotime190tk_tempos_and_fl_tick_ties)
|
|
|
+{
|
|
|
+ using namespace at190tk;
|
|
|
+ {
|
|
|
+ const Rig rig (96, 97.0, ramp);
|
|
|
+ const auto v = rig.play (28000, 1);
|
|
|
+ CHECK (holdsTickEnds (v, 97.0, 96, 0.0, 16.0));
|
|
|
+ const auto c = changes (v);
|
|
|
+ const std::vector<int> fl { 284, 568, 852, 1137, 1421, 1705, 1989, 2273 };
|
|
|
+ CHECK (c.size() > fl.size() && std::equal (fl.begin(), fl.end(), c.begin()));
|
|
|
+ CHECK (std::find (c.begin(), c.end(), 27563) != c.end() && std::find (c.begin(), c.end(), 27562) == c.end());
|
|
|
+ }
|
|
|
+ {
|
|
|
+ const Rig rig (96, 173.0, ramp);
|
|
|
+ const auto v = rig.play (4000);
|
|
|
+ CHECK (holdsTickEnds (v, 173.0, 96, 0.0, 16.0) && sameAtBlockEnds (v, rig.play (4000, 7), 7));
|
|
|
+ const std::vector<int> fl { 159, 319, 478, 637, 797, 956, 1115, 1275 };
|
|
|
+ const auto c = changes (v);
|
|
|
+ CHECK (c.size() > fl.size() && std::equal (fl.begin(), fl.end(), c.begin()));
|
|
|
+ }
|
|
|
+ // a clip that starts at bar 2: nothing before (the control's own value), FL's first change at sample 88200 = tick 384
|
|
|
+ {
|
|
|
+ const Rig rig (96, 120.0, ramp, 4.0);
|
|
|
+ const auto v = rig.play (89000);
|
|
|
+ CHECK (std::isnan (v[88199]) && std::abs (v[88200] - 10.0f / (16.0f * lynx::ppq)) < 1.0e-6f);
|
|
|
+ CHECK (holdsTickEnds (v, 120.0, 96, 4.0, 16.0) && sameAtBlockEnds (v, rig.play (89000, 256), 256));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// A tempo that moves (a tempo map): the value still changes on FL's ticks of the song position (the sample nearest
|
|
|
+// each tick) and holds the clip at the tick's end
|
|
|
+TEST (autotime190tk_tempo_change)
|
|
|
+{
|
|
|
+ using namespace at190tk;
|
|
|
+ Rig rig (96, 120.0, ramp);
|
|
|
+ lynx::Automation tempo;
|
|
|
+ tempo.name = "Tempo";
|
|
|
+ tempo.target = { lynx::AutomationTargetKind::tempo, 0 };
|
|
|
+ tempo.points = { { 0, lynx::valueFromTempo (120.0), 0.0f }, { 2 * lynx::ppq, lynx::valueFromTempo (120.0), 0.0f },
|
|
|
+ { 2 * lynx::ppq, lynx::valueFromTempo (150.0), 0.0f }, { 16 * lynx::ppq, lynx::valueFromTempo (150.0), 0.0f } };
|
|
|
+ rig.song.automations.push_back (tempo);
|
|
|
+ lynx::Clip c;
|
|
|
+ c.kind = lynx::ClipKind::automation;
|
|
|
+ c.automation = 1;
|
|
|
+ c.track = 2;
|
|
|
+ c.length = 16 * lynx::ppq;
|
|
|
+ rig.song.playlist.push_back (c);
|
|
|
+ // the transport, sample by sample as the engine moves it: each sample's song position, then its FL tick
|
|
|
+ auto renderState = lynx::RackState::build (rig.song, nullptr, 44100.0);
|
|
|
+ CHECK (renderState->tempoMap);
|
|
|
+ lynx::RackRenderer renderer;
|
|
|
+ renderer.prepare (44100.0, 64);
|
|
|
+ std::vector<float> l (16), r (16);
|
|
|
+ double tick = 0.0;
|
|
|
+ auto ok = true, faster = false;
|
|
|
+ for (int sample = 0; sample < 200000 && ok; ++sample)
|
|
|
+ {
|
|
|
+ renderer.process (renderState.get(), true, tick, 1, l.data(), r.data());
|
|
|
+ const auto tps = lynx::ticksPerSample (renderer.currentBpm(), 44100.0); // (the block's tempo)
|
|
|
+ const auto k = std::floor ((tick + 0.5 * tps) / 10.0 + 1.0e-9);
|
|
|
+ const auto expected = (float) std::clamp ((k + 1.0) * 10.0 / (16.0 * lynx::ppq), 0.0, 1.0);
|
|
|
+ ok = std::abs (renderer.automationValueForCheck (0) - expected) < 1.0e-5f;
|
|
|
+ faster = faster || renderer.currentBpm() > 140.0;
|
|
|
+ tick += tps;
|
|
|
+ if (tick > 3 * lynx::ppq)
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ CHECK (ok && faster);
|
|
|
+}
|
|
|
+
|
|
|
+// Cuts only where a value may change: a held clip cuts at its start and end alone; a LYNX project has no FL ticks
|
|
|
+TEST (autotime190tk_cuts_only_where_a_value_changes)
|
|
|
+{
|
|
|
+ using namespace at190tk;
|
|
|
+ {
|
|
|
+ const Rig held (96, 120.0, { { 0, 0.3f, 0.0f }, { 16 * lynx::ppq, 0.3f, 0.0f } }, 2.0, 4.0);
|
|
|
+ const auto state = lynx::RackState::build (held.song, nullptr, 44100.0);
|
|
|
+ CHECK (state->flTick == 10.0 && state->flPpq == 96);
|
|
|
+ // clip from tick 1920 to 5760 = FL ticks 192 … 576
|
|
|
+ const std::vector<std::pair<std::int64_t, std::int64_t>> want { { 191, 193 }, { 575, 577 } };
|
|
|
+ CHECK (state->flChangeTicks == want);
|
|
|
+ const auto v = held.play (90000);
|
|
|
+ CHECK (changes (v).size() == 1 && changes (v)[0] == 44100); // (NaN → 0.3 at the clip's first tick, then nothing)
|
|
|
+ }
|
|
|
+ {
|
|
|
+ // a Hold segment: only its jump (FL tick 384) between the clip's start and end
|
|
|
+ lynx::AutomationPoint first { 0, 0.2f, 0.0f };
|
|
|
+ first.mode = lynx::flcurve::hold;
|
|
|
+ const Rig stepped (96, 120.0, { first, { 4 * lynx::ppq, 0.8f, 0.0f }, { 8 * lynx::ppq, 0.8f, 0.0f } }, 0.0, 8.0);
|
|
|
+ const auto state = lynx::RackState::build (stepped.song, nullptr, 44100.0);
|
|
|
+ const std::vector<std::pair<std::int64_t, std::int64_t>> want { { -1, 1 }, { 383, 385 }, { 767, 769 } };
|
|
|
+ CHECK (state->flChangeTicks == want);
|
|
|
+ const auto v = stepped.play (95000);
|
|
|
+ const auto c = changes (v);
|
|
|
+ // FL: tick 383 holds the clip read at tick 384 (the jump's 0.8), from sample rint(383 · 229.6875) = 87970
|
|
|
+ CHECK (c.size() == 1 && c[0] == 87970 && std::abs (v[0] - 0.2f) < 1.0e-6f && std::abs (v[87970] - 0.8f) < 1.0e-6f);
|
|
|
+ }
|
|
|
+ {
|
|
|
+ const Rig ramped (96, 120.0, ramp, 0.0, 4.0);
|
|
|
+ const auto state = lynx::RackState::build (ramped.song, nullptr, 44100.0);
|
|
|
+ CHECK (! state->flChangeTicks.empty() && state->flChangeTicks.front().first <= 0 && state->flChangeTicks.back().second >= 384);
|
|
|
+ }
|
|
|
+ {
|
|
|
+ const Rig lynxOwn (0, 120.0, ramp);
|
|
|
+ const auto state = lynx::RackState::build (lynxOwn.song, nullptr, 44100.0);
|
|
|
+ CHECK (state->flTick == 0.0 && state->flChangeTicks.empty());
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// LYNX's own projects keep LYNX's timing: the clip at each 128-sample piece's first sample
|
|
|
+TEST (autotime190tk_lynx_projects_keep_lynx_timing)
|
|
|
+{
|
|
|
+ using namespace at190tk;
|
|
|
+ const Rig rig (0, 120.0, ramp);
|
|
|
+ const auto v = rig.play (1024);
|
|
|
+ const auto c = changes (v);
|
|
|
+ CHECK (! c.empty() && c.front() == 128 && v[0] < 1.0e-6f);
|
|
|
+ for (const auto s : c)
|
|
|
+ CHECK (s % 128 == 0);
|
|
|
+}
|
|
|
+
|
|
|
+// The .flp import keeps the file's PPQ for the timing; the legacy 16 is FL's default (96)
|
|
|
+TEST (autotime190tk_flp_import_keeps_the_ppq)
|
|
|
+{
|
|
|
+ using namespace lynx;
|
|
|
+ const at190tk::Rig rig (0, 120.0, at190tk::ramp);
|
|
|
+ for (const int p : { 96, 960 })
|
|
|
+ {
|
|
|
+ flpexport::Options o;
|
|
|
+ o.ppq = p;
|
|
|
+ flpexport::Report report;
|
|
|
+ const auto bytes = flpexport::exportFlp (rig.song, o, report);
|
|
|
+ FlpImportOptions io;
|
|
|
+ io.fileExists = [] (const std::string&) { return false; };
|
|
|
+ const auto back = importFlp (flp::parseProject (bytes), io);
|
|
|
+ CHECK (back.flAutomationPpq == p);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// The engines that run FL's ticks themselves (3x Osc, DX10, BeepMap, Granulizer): a render call that starts on the
|
|
|
+// sample right after a tick it never reached runs that tick's NewTick. Before, calls cut on FL's ticks lost every tick:
|
|
|
+// a released voice never ended (Right Night feeling's "Sin Synth", 72 PPQ: still sounding after 2960 such calls; now
|
|
|
+// silent after 21, as after 29 plain calls). With FL's demo (when the FL folder is given).
|
|
|
+TEST (autotime190tk_engines_run_the_tick_a_call_starts_just_after)
|
|
|
+{
|
|
|
+ using namespace lynx;
|
|
|
+ if (realFlpDir.empty())
|
|
|
+ return;
|
|
|
+ const auto demos = std::filesystem::path (realFlpDir) / "Data" / "Demo projects";
|
|
|
+ const auto dir = std::filesystem::exists (demos) ? demos : std::filesystem::path (realFlpDir) / "Demo projects";
|
|
|
+ const auto path = dir / "Demo songs" / "Atthar Feat Mehran Abbasi & Elise Chantelle - Right Night feeling.flp";
|
|
|
+ if (! std::filesystem::exists (path))
|
|
|
+ return;
|
|
|
+ std::ifstream in (path, std::ios::binary);
|
|
|
+ const auto song = importFlp (flp::parseProject (std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), {})));
|
|
|
+ const ChannelDef* sin = nullptr;
|
|
|
+ for (const auto& c : song.channels)
|
|
|
+ if (c.name == "Sin Synth" && c.instrument == InstrumentKind::threeosc)
|
|
|
+ sin = &c;
|
|
|
+ CHECK (sin != nullptr && song.flAutomationPpq == 72);
|
|
|
+ if (sin == nullptr)
|
|
|
+ return;
|
|
|
+ // calls of 300 samples (an FL tick is 350 at 105 BPM, 72 PPQ), each starting 0.3 samples after a tick (`onTicks`), or
|
|
|
+ // plain calls of 256: how many calls after the note's end until the voice has ended (3000: never)
|
|
|
+ const auto release = [sin] (bool onTicks) {
|
|
|
+ auto inst = makeThreeOscInstrument();
|
|
|
+ inst->prepare (44100.0);
|
|
|
+ inst->configure (*sin, 105.0);
|
|
|
+ std::vector<float> l (512), r (512);
|
|
|
+ const auto tps = ticksPerSample (105.0, 44100.0);
|
|
|
+ const auto T = 960.0 / 72.0;
|
|
|
+ auto t = 0.0;
|
|
|
+ const auto call = [&] (int k) {
|
|
|
+ inst->setSongPosition (onTicks ? k * T + 0.3 * tps : t);
|
|
|
+ inst->render (l.data(), r.data(), onTicks ? 300 : 256, 1.0f, 1.0f);
|
|
|
+ t += 256 * tps;
|
|
|
+ };
|
|
|
+ call (0);
|
|
|
+ inst->noteOn (71, 0.8f);
|
|
|
+ for (int k = 1; k < 40; ++k)
|
|
|
+ call (k);
|
|
|
+ if (inst->idle())
|
|
|
+ return -1;
|
|
|
+ inst->noteOff (71);
|
|
|
+ int k = 40;
|
|
|
+ for (; k < 3040 && ! inst->idle(); ++k)
|
|
|
+ call (k);
|
|
|
+ return k - 40;
|
|
|
+ };
|
|
|
+ const auto onTicks = release (true), plain = release (false);
|
|
|
+ CHECK (onTicks > 0 && onTicks < 100 && plain > 0 && plain < 100);
|
|
|
+}
|