Просмотр исходного кода

wip 168k: Song-mode note takes keep their song positions (clip from the first note's bar, pattern grows, linear take), FL session evidence, tests

Ka1-S 1 месяц назад
Родитель
Сommit
6b3907ce81

+ 63 - 0
docs/analise/REA_rodada168k.md

@@ -0,0 +1,63 @@
+# Round 168k — Song-mode MIDI recording folds into the first bars (the YouTube review, critical)
+
+Source: docs/analise/REA_rodada168d-video.md, moments #7, #8 and #16 ("I recorded near eight bars, but it's stopping at
+two bars … it just maxes out at two bars"; "it says two bars here, but visually it's showing four bars"; the pitch
+wheel landing apart from the notes). Busy Works Beats, LYNX SHARE 0.1.21 on macOS. Nothing in the cause or the fix is
+platform-specific (core recorder + the engine's take placement).
+
+## FL evidence (one planned FL session, 2026-11-01 23:24–23:27)
+
+FL Studio 2026 driven as a black box with real keyboard / mouse input (`keybd_event` / `mouse_event`), both locks
+(`~/.cache/lynx-wingui.lock` + `~/.cache/lynx-fl.lock`), FL started once and closed by its own PID (Save changes → No).
+Notes played with FL's own typing keyboard (already on: registry `KbToMIDIBtn=1`, layout "Layout Piano"), default
+template (130 BPM, 808 Kick selected), Song mode, Record → "Everything" (= the user's `RecordingFilter2=15`, unchanged).
+No FL setting was changed: the registry export before / after differs only in FL's own run counter, cloud timestamp,
+recent-color list and the Channel rack window height, and those three were written back. Screenshots in
+`C:\Users\kauan\lynx-test\rec168k\` (crops `c_*`, zooms `z_*`); scripts `drive.ps1`, `timed.ps1` (timed key down/up).
+
+| # | setup | what was played | what FL did | shots |
+|---|---|---|---|---|
+| fl1 | Pattern 1 = one kick step, its clip on Track 1 bar 1 (a **1-bar song**); new empty Pattern 2 current; record armed; Play from 1:1 | nothing for the first 2.8 s | the song did **not** go back to bar 1: the counter read **2:08** at 2.8 s and **8:08** at 13.9 s — while recording, Song mode plays on past the song's end (linear), from the start of the take, before any note | `c_m0a.png`, `c_m1a.png` |
+| fl2 | same take | one short note just before bars 3, 4, 5, 6, 7 and 8 (D4…B4), the last one **held through Stop** (~8:3, released 0.4 s after Stop) | a **Pattern 2 clip on Track 2 from bar 2 to bar 9**: it starts at the bar of the first note (that note fell at ~2:04:15, just before bar 3) and ends at the pattern's whole bars; every note sits **where it was played** (no folding to bar 1); the held note is kept, ending at the Stop | `c_m1b.png`, `z_m1b.png` |
+| fl3 | Pattern 3 = one kick step (1 bar); its clip on Track 3 from bar 1 **stretched to bar 5** (4 bars, the clip repeats the 1-bar pattern) | notes at 2:1, 3:3 and 4:2 | the notes stay at 2:1, 3:3 and 4:2 of the clip; the Piano roll of Pattern 3 shows the kick at 1:1 and **D4 at the pattern's bar 2**: FL **grows the pattern** to the recorded position, it does **not fold** by the pattern's length; no new clip | `z_m2b.png`, `f12.png` |
+
+Earlier evidence agrees: REA_rodada165z-b fh2–fh5 (automation recorded from 2.3 on an empty Playlist: a Pattern 1 clip
+placed at Stop from bar 2, "the bar of the first move", to the pattern's whole bars; the song played on to 7.1).
+
+## Item
+
+| Reporter's words | FL evidence | LYNX before (0.1.21 … main e6e36fdd) | LYNX now |
+|---|---|---|---|
+| "I recorded near eight bars, but it's stopping at two bars … you can't record more than two bars" (#8: new empty pattern, Song mode, no clip of it under the playhead) | fl1, fl2: the take keeps its song positions, the clip goes from the first note's bar to the last recorded bar, the song plays on past its end while recording | `NoteRecorder::begin` froze the loop length at the empty pattern's 1 bar and `patternPositionForTick` folded every note outside a clip of the pattern `% loopLength` → 32 notes over 8 bars all in bar 1 (a note held over the bar line made it 2 bars: "maxes out at two bars"); the clip was placed where recording started; the song looped at its end while recording notes | Song mode: the take's **origin** = the bar of its first note (or, when that note is inside a clip of the pattern, that clip's pattern start); every note is stored at `song position − origin` (inside a clip: the clip's position); the clip is placed at the origin with the first batch and grows with the pattern to the last recorded bar; a pattern with a set length (Channel rack › Length) grows by whole bars to the notes past it; while a take records in Song mode without a loop region the song plays on past its end (`noteTakeLinear` → `liveInput.linearSong`) |
+| "it says two bars here, but visually it's showing four bars" (#7: a 2-bar pattern whose clip was stretched to 8 bars, recorded into) | fl3: FL grows the pattern, no folding | notes past the pattern's length were stored past it: never shown in the Piano roll nor played (probe "8 B") | the notes stay at their clip position and the pattern grows to them (automatic length by itself, a set length by whole bars): 8 bars recorded → 8 bars stored, shown and played |
+| held notes at Stop | fl2: kept, ending at the Stop | kept (to the last playing position) | unchanged, now covered by a test with the note past the song's end |
+| "Like it just doesn't play my notes right" (#16, pitch wheel) | FL writes the wheel with the notes (REA_rodada165z-b, pattern events) | the wheel went to an automation clip at its real song position while the notes were folded into bar 1–2: they no longer lined up | the notes are at their real song positions, the wheel's automation clip too (both linear for the whole take): they line up. The storage difference (automation clip instead of pattern events in Song mode) stays as decided in 0.1.20 (REA_rodada165z-b) |
+
+Not changed: Pattern mode (loops the pattern, positions within the loop); loop regions in Song mode keep looping —
+a take whose first note is inside the region uses the bar of the region's start as its origin, so every pass lands
+in the same bars (FL's "MIDI loop recording (song mode)" option is off here and was not measured). Round 167q's
+`patternTickInClip` fold for recording (not on main yet when this round was written) is superseded for notes by fl3:
+recording never folds.
+
+## Code
+
+- `src/core/Recorder.h/.cpp`: `takeOriginFor`, `songNotePosition`, `growForSongTake`, `commitNoteBatch` (the commit of a
+  batch of a take, shared by the engine and the tests); `NoteRecorder` keeps the take's origin (set by the first note-on
+  in Song mode) and a `linear` flag (no wrap of positions at the song's end).
+- `src/engine/AudioEngine.cpp/.h`: `updateRecording` commits through `commitNoteBatch` (the clip at the origin instead
+  of the take's start), `noteTakeLinear` for the whole Song-mode take (also used by the wheel / automation of the same
+  take), the recording-marker check does not wrap a linear take.
+
+## Tests
+
+`tests/Rec168kTests.inc` (the probe's two cases first — they fail on main: 32 notes in bar 1 / notes past a 2-bar
+pattern — then FL's measured cases fl1–fl3, the held note at Stop, a take continuing the clip it starts in, a set
+length growing, a loop region keeping one origin, the wheel landing with the notes). `bugs131_pattern_length_is_
+respected_on_the_first_recording_in_song_mode` now expects FL's placement (clip from the first note's bar).
+
+## Where I stopped
+
+- Done: FL session (fl1–fl3), code, tests written.
+- Next: build the tests (mini harness `~/.claude/jobs/f8789b49/tmp/rec168k/mini/build.sh`, then `scripts/build-local.sh`
+  with FL Data), the probe against the new code, the Windows build + GUI check of the user's flow (fresh profile), notes,
+  land.

+ 136 - 1
src/core/Recorder.cpp

@@ -170,6 +170,116 @@ bool placePatternClipAt (Song& song, int pattern, std::int64_t tick)
     return true;
 }
 
+namespace
+{
+// The clip of the current pattern playing at song position `tick` (the last one in the list, as the Playlist draws it).
+const Clip* currentPatternClipAt (const Song& song, std::int64_t tick)
+{
+    const Clip* found = nullptr;
+    for (const auto& c : song.playlist)
+        if (c.kind == ClipKind::pattern && c.pattern == song.currentPattern && tick >= c.start && tick < c.end())
+            found = &c;
+    return found;
+}
+
+// FL grows the pattern when a take is played inside a clip longer than it (round 168k [EXEC]: a 1-bar pattern's clip
+// stretched to 4 bars, notes played at bars 2.1 / 3.3 / 4.2 stayed there and the pattern became 4 bars): no fold by
+// the pattern's length while recording, even though such a clip plays its pattern over and over.
+std::int64_t clipPatternTick (const Clip& c, std::int64_t tick)
+{
+    return tick - c.start + c.offset;
+}
+} // namespace
+
+std::int64_t takeOriginFor (const Song& song, std::int64_t tick, bool* fromClip)
+{
+    tick = std::max<std::int64_t> (0, tick);
+    if (const auto* c = currentPatternClipAt (song, tick))
+    {
+        if (fromClip != nullptr)
+            *fromClip = true;
+        return c->start - c->offset;
+    }
+    if (fromClip != nullptr)
+        *fromClip = false;
+    auto origin = song.barAt (tick).tick;
+    // a loop region plays over and over: its notes keep one origin, the bar the region starts in
+    if (song.hasLoop() && tick >= song.loopStart && tick < song.loopEnd)
+        origin = std::min (origin, song.barAt (song.loopStart).tick);
+    return origin;
+}
+
+std::int64_t songNotePosition (const Song& song, std::int64_t tick, std::int64_t origin)
+{
+    tick = std::max<std::int64_t> (0, tick);
+    if (const auto* c = currentPatternClipAt (song, tick))
+        return std::max<std::int64_t> (0, clipPatternTick (*c, tick));
+    auto local = tick - std::max<std::int64_t> (0, origin);
+    if (local < 0) // played after the song (or the loop region) went back to its start: the take goes on after it
+        local += song.hasLoop() ? song.loopEnd - song.loopStart : std::max<std::int64_t> (1, song.songLength());
+    return std::max<std::int64_t> (0, local);
+}
+
+bool growForSongTake (Song& song, int pattern, std::int64_t origin, const std::vector<Note>& notes)
+{
+    if (pattern < 0 || pattern >= (int) song.patterns.size() || notes.empty() || origin < 0)
+        return false;
+
+    auto& p = song.patterns[(std::size_t) pattern];
+    bool changed = false;
+    std::int64_t lastStart = -1;
+    for (const auto& n : notes)
+        lastStart = std::max (lastStart, n.start);
+
+    // a set length grows by whole bars to the notes recorded past it (FL's pattern length follows its notes)
+    const std::int64_t bar = (std::int64_t) ppq * std::max (1, song.beatsPerBar);
+    if (p.lengthTicks > 0 && lastStart >= p.lengthTicks)
+    {
+        p.lengthTicks = (lastStart / bar + 1) * bar;
+        changed = true;
+    }
+
+    // the take's clip (the one whose pattern start is the origin) reaches the bar of the last note when no clip of the
+    // pattern plays there: from its pattern position on, the whole pattern (it stays an automatic-length clip)
+    const auto lastSong = origin + lastStart;
+    const auto covered = std::any_of (song.playlist.begin(), song.playlist.end(), [&] (const Clip& c) {
+        return c.kind == ClipKind::pattern && c.pattern == pattern && lastSong >= c.start && lastSong < c.end();
+    });
+    if (covered)
+        return changed;
+    Clip* take = nullptr;
+    for (auto& c : song.playlist)
+        if (c.kind == ClipKind::pattern && c.pattern == pattern && c.start - c.offset == origin && c.start <= lastSong
+            && (take == nullptr || c.end() > take->end()))
+            take = &c;
+    if (take == nullptr)
+        return changed;
+    const auto lastBar = song.barAt (lastSong);
+    const auto end = std::max (lastBar.tick + std::max<std::int64_t> (1, lastBar.length), origin + p.effectiveLength (song.beatsPerBar));
+    if (end > take->end())
+    {
+        take->length = end - take->start;
+        changed = true;
+    }
+    return changed;
+}
+
+void commitNoteBatch (Song& song, const NoteRecorder& recorder, std::vector<Note> notes, RecordMode mode, const TakeQuantize& q,
+                      std::vector<int>& clearedChannels, bool placeClip, bool firstBatch)
+{
+    const auto pattern = song.currentPattern;
+    if (pattern < 0 || pattern >= (int) song.patterns.size() || notes.empty())
+        return;
+    quantizeTake (notes, q);
+    commitTake (song.patterns[(std::size_t) pattern], notes, mode, 0, clearedChannels);
+
+    if (song.mode != PlayMode::song || recorder.songOrigin() < 0)
+        return;
+    if (placeClip && firstBatch && ! recorder.originFromClip())
+        placePatternClipAt (song, pattern, recorder.songOrigin());
+    growForSongTake (song, pattern, recorder.songOrigin(), notes);
+}
+
 std::int64_t countInLength (const Song& song, int bars, std::int64_t tick)
 {
     return std::max (0, bars) * std::max<std::int64_t> (1, song.barAt (std::max<std::int64_t> (0, tick)).length);
@@ -408,10 +518,21 @@ void commitEventPoints (Song& song, int patternIndex, AutomationWrite& w, std::v
 void NoteRecorder::begin (std::int64_t loopLength)
 {
     active = true;
+    linear = false;
     loop = std::max<std::int64_t> (1, loopLength);
+    origin = -1;
+    fromClip = false;
     held.clear();
 }
 
+std::int64_t NoteRecorder::songTick (const Song& song, double tick) const
+{
+    const auto position = (std::int64_t) std::floor (std::max (0.0, tick));
+    if (linear || song.hasLoop()) // (a loop region: the transport wraps it itself)
+        return position;
+    return position % std::max<std::int64_t> (1, song.songLength()); // the song plays in a loop
+}
+
 void NoteRecorder::add (const Song& song, const RecordedEvent& e)
 {
     if (! active)
@@ -421,6 +542,10 @@ void NoteRecorder::add (const Song& song, const RecordedEvent& e)
 
     if (e.on)
     {
+        // Song mode: the take's first note sets where the pattern's start lies for the notes outside its clips
+        if (song.mode == PlayMode::song && origin < 0)
+            origin = takeOriginFor (song, songTick (song, e.tick), &fromClip);
+
         // repeated note without an "off" in between: close the previous one
         if (auto it = held.find (id); it != held.end())
             finish (song, e.channel, e.key, it->second, e.tick);
@@ -448,7 +573,17 @@ void NoteRecorder::end (const Song& song, double tick)
 void NoteRecorder::finish (const Song& song, int channel, int key, const Held& h, double endTick)
 {
     Note n;
-    n.start = patternPositionForTick (song, h.tick, loop);
+    if (song.mode == PlayMode::song)
+    {
+        const auto at = songTick (song, h.tick);
+        if (origin < 0)
+            origin = takeOriginFor (song, at, &fromClip);
+        n.start = songNotePosition (song, at, origin);
+    }
+    else
+    {
+        n.start = patternPositionForTick (song, h.tick, loop);
+    }
     // Song mode loops (the loop region, or the whole song): a note held across the loop's end ends after the wrap
     auto played = endTick - h.tick;
     if (played < 0.0 && song.mode == PlayMode::song)

+ 37 - 0
src/core/Recorder.h

@@ -142,6 +142,32 @@ void commitEventPoints (Song& song, int pattern, AutomationWrite& w, std::vector
 // Removes the points between two of the same value (no change of the curve) in [from, to].
 void thinAutomation (std::vector<AutomationPoint>& points, std::int64_t from, std::int64_t to);
 
+// --- Song-mode note takes (round 168k, the YouTube review: "it just maxes out at two bars") ----------------------
+// FL (REA_rodada165z-b fh2–fh5, round 168k [EXEC]): a take recorded in Song mode goes into the current pattern at the
+// song position it was played at — nothing folds into the pattern's first bars. Where no clip of the pattern plays, the
+// pattern's start lies at the take's origin: the bar of the take's first note (its clip is placed there and grows with
+// the pattern to the last recorded bar).
+//
+// The origin of a take whose first note is played at song position `tick`: inside a clip of the current pattern, the
+// song position of that clip's pattern start (the take extends what the clip shows); elsewhere the start of that bar.
+// `fromClip` (optional) tells which one it was (a clip is placed for the take only when it was not).
+std::int64_t takeOriginFor (const Song& song, std::int64_t tick, bool* fromClip = nullptr);
+// Where a note played at song position `tick` goes in the current pattern: inside a clip of the pattern, that clip's
+// position; elsewhere `tick − origin` (0 at least).
+std::int64_t songNotePosition (const Song& song, std::int64_t tick, std::int64_t origin);
+// After a Song-mode take was committed: a pattern with a set length (Channel Rack › Length) grows by whole bars to the
+// notes recorded past its end (an automatic-length pattern grows by itself), and the clip at the take's origin reaches
+// the bar of the last of `notes` (the take's notes, pattern positions). Returns true when something changed.
+bool growForSongTake (Song& song, int pattern, std::int64_t origin, const std::vector<Note>& notes);
+
+class NoteRecorder;
+// One batch of a take's finished notes into the song, as AudioEngine::updateRecording commits it (the caller wraps it in
+// one undoable edit, which also lets the pattern's clips follow its length): quantised and committed into the current
+// pattern; in Song mode the take's clip is placed at its origin with the first batch (`placeClip`: the record option
+// "place a clip") and grows with the take (growForSongTake).
+void commitNoteBatch (Song& song, const NoteRecorder& recorder, std::vector<Note> notes, RecordMode mode, const TakeQuantize& q,
+                      std::vector<int>& clearedChannels, bool placeClip, bool firstBatch);
+
 class NoteRecorder
 {
 public:
@@ -150,6 +176,12 @@ public:
     void begin (std::int64_t loopLength);
     bool isActive() const noexcept { return active; }
 
+    // Song mode: the song plays on past its end while the take records (no wrap of the positions; round 168k).
+    void setLinear (bool on) noexcept { linear = on; }
+    // Song mode: the take's origin (see takeOriginFor), -1 until its first note; `originFromClip` = it is a clip's.
+    std::int64_t songOrigin() const noexcept { return origin; }
+    bool originFromClip() const noexcept { return fromClip; }
+
     // Delivers an event; when it's the "off" of a held note, the finished note goes to finished().
     void add (const Song& song, const RecordedEvent& e);
 
@@ -168,8 +200,13 @@ private:
 
     void finish (const Song& song, int channel, int key, const Held& held, double endTick);
 
+    std::int64_t songTick (const Song& song, double tick) const;
+
     bool active = false;
+    bool linear = false;
     std::int64_t loop = 0;
+    std::int64_t origin = -1;
+    bool fromClip = false;
     std::map<std::pair<int, int>, Held> held; // (channel, key)
     std::vector<Note> finished;
 };

+ 19 - 13
src/engine/AudioEngine.cpp

@@ -2044,7 +2044,15 @@ void AudioEngine::updateRecording()
         recorder.begin (pattern.effectiveLength (song.beatsPerBar));
         takeHasUndoStep = false;
         takeClearedChannels.clear();
-        takeStartPosition = lastPlayingTick;
+        // Song mode without a loop region: while recording, the song plays on past its end until recording stops (FL,
+        // round 168k [EXEC]: a 1-bar song recorded from bar 1 counted on to bar 8 instead of going back to bar 1), so
+        // the notes stay where they were played
+        if (song.mode == PlayMode::song && ! song.hasLoop())
+        {
+            noteTakeLinear = true;
+            recorder.setLinear (true);
+            updateLinearSong();
+        }
     };
 
     if (active)
@@ -2060,7 +2068,7 @@ void AudioEngine::updateRecording()
         if (punch.empty() || ! ev.on)
             return false;
         auto tick = ev.tick;
-        if (! song.hasLoop())
+        if (! song.hasLoop() && ! noteTakeLinear)
             tick = std::fmod (std::max (0.0, tick), (double) std::max<std::int64_t> (1, song.songLength()));
         return ! inPunchRanges (punch, tick);
     };
@@ -2092,25 +2100,23 @@ void AudioEngine::updateRecording()
 
     auto notes = recorder.takeFinished();
 
+    if (! recorder.isActive() && std::exchange (noteTakeLinear, false))
+        updateLinearSong();
+
     if (notes.empty())
         return;
 
-    // the whole take is a single undo step: overdub or replace, quantised to the record grid; in song mode a
-    // clip of the pattern is placed where recording started if none plays there
+    // the whole take is a single undo step: overdub or replace, quantised to the record grid; in song mode the notes
+    // go where they were played and a clip of the pattern is placed from the bar of the take's first note (FL; round
+    // 168k: they used to fold into the pattern's first bars)
     const auto options = recordOptions;
-    auto placeClipAt = (std::int64_t) takeStartPosition;
-    if (song.mode == PlayMode::song && ! song.hasLoop()) // (the position loops over the song)
-        placeClipAt %= std::max<std::int64_t> (1, song.songLength());
     editSong ([&] (Song& s) {
         TakeQuantize q;
         q.grid = options.quantise;
         q.starts = options.quantiseStarts;
         q.end = options.noteEnd;
         q.stepSequencer = options.toStepSequencer;
-        commitTake (s.patterns[(std::size_t) s.currentPattern], notes, options.mode, q, takeClearedChannels);
-
-        if (options.placeSongClip && s.mode == PlayMode::song && ! takeHasUndoStep)
-            placePatternClipAt (s, s.currentPattern, placeClipAt);
+        commitNoteBatch (s, recorder, std::move (notes), options.mode, q, takeClearedChannels, options.placeSongClip, ! takeHasUndoStep);
     }, ! takeHasUndoStep);
     takeHasUndoStep = true;
 }
@@ -3016,7 +3022,7 @@ void AudioEngine::updateAutomationRecording (bool active)
                     auto& w = writeFor (t.target);
                     recordedAutomationPoints += (int) t.pending.size();
                     if (songMode)
-                        commitAutomationPoints (s, w, std::move (t.pending), grid, automationLinear);
+                        commitAutomationPoints (s, w, std::move (t.pending), grid, automationLinear || noteTakeLinear);
                     else
                         commitEventPoints (s, automationPattern, w, std::move (t.pending), automationLoopLength, grid);
                     t.pending.clear();
@@ -3116,7 +3122,7 @@ void AudioEngine::updateAutomationRecording (bool active)
             updateLinearSong();
         }
     if (song.mode == PlayMode::song)
-        lastAutomationTick = songTickForRecorded (song, tick, automationLinear);
+        lastAutomationTick = songTickForRecorded (song, tick, automationLinear || noteTakeLinear);
 
     if (! ended.empty() || --automationCommitCountdown <= 0)
     {

+ 3 - 2
src/engine/AudioEngine.h

@@ -723,6 +723,8 @@ private:
     // Song mode, no loop region: once a parameter moved while recording, the song plays on past its end (no wrap to
     // the start) until recording stops, and the take's clip grows with the playhead (issue #12: it cut itself)
     bool automationLinear = false;
+    // the same for the whole of a take (FL: Song mode plays on while recording; round 168k)
+    bool noteTakeLinear = false;
     std::int64_t lastAutomationTick = -1; // the song position of the take's last update (song mode)
     std::vector<AutomationWrite> takeWrites; // per target: where this take writes (its own clip grows)
     bool takeHasUndoStep = false;
@@ -730,7 +732,6 @@ private:
     void updatePerformanceRecording();
     RecordOptions recordOptions;
     std::vector<int> takeClearedChannels; // replace mode: channels already cleared in this take
-    double takeStartPosition = 0.0;
     std::atomic<bool> waitingForInput { false };
     void startOnInput() noexcept; // MIDI or UI thread: the first note while waiting starts playback
     std::vector<int> armedInserts;
@@ -850,7 +851,7 @@ private:
     // the audio thread's view of the above while takes record (false when none is open)
     void updateLinearSong() noexcept
     {
-        const auto on = (takeCount > 0 || automationLinear) && linearRecording();
+        const auto on = (takeCount > 0 || automationLinear || noteTakeLinear) && linearRecording();
         liveInput.linearSong.store (on || bouncing);
         liveInput.noLoopRegion.store ((on && song.hasLoop()) || bouncing);
     }

+ 8 - 6
tests/Bugs131Tests.inc

@@ -46,17 +46,19 @@ TEST (bugs131_pattern_length_is_respected_on_the_first_recording_in_song_mode)
     CHECK (s.songLength() == 2 * bar);
     CHECK (lynx::playbackLoop (true, 0.0, 0.0, (double) s.songLength()).end == (double) (2 * bar));
 
-    // a note recorded on bar 2 stays on bar 2 and the clip placed for the take is 2 bars long
+    // a note recorded on bar 2 stays on bar 2 of the song and the clip placed for the take is 2 bars long; FL places
+    // the take's clip from the bar of its first note (round 168k, REA_rodada165z-b fh2–fh5), so it starts at bar 2
     lynx::NoteRecorder recorder;
     recorder.begin (s.patterns[0].effectiveLength (s.beatsPerBar));
     recorder.add (s, { (double) (bar + lynx::ppq), 0, 60, 0.8f, true });
     recorder.add (s, { (double) (bar + 2 * lynx::ppq), 0, 60, 0.0f, false });
-    const auto notes = recorder.takeFinished();
-    CHECK (notes.size() == 1 && notes[0].start == bar + lynx::ppq && notes[0].length == lynx::ppq);
+    auto notes = recorder.takeFinished();
+    CHECK (notes.size() == 1 && notes[0].start == lynx::ppq && notes[0].length == lynx::ppq);
+    CHECK (recorder.songOrigin() == bar && ! recorder.originFromClip());
     std::vector<int> cleared;
-    lynx::commitTake (s.patterns[0], notes, lynx::RecordMode::overdub, 0, cleared);
-    CHECK (lynx::placePatternClipAt (s, 0, 0));
-    CHECK (s.playlist.size() == 1 && s.playlist[0].length == 2 * bar && s.songLength() == 2 * bar);
+    lynx::commitNoteBatch (s, recorder, std::move (notes), lynx::RecordMode::overdub, {}, cleared, true, true);
+    CHECK (s.playlist.size() == 1 && s.playlist[0].start == bar && s.playlist[0].length == 2 * bar && s.songLength() == 3 * bar);
+    CHECK (s.playlist[0].start + s.patterns[0].notes[0].start == bar + lynx::ppq);
 }
 
 TEST (bugs131_pattern_clips_follow_their_pattern_length)

+ 348 - 0
tests/Rec168kTests.inc

@@ -0,0 +1,348 @@
+// Round 168k: Song-mode MIDI recording folded into the first bars (the YouTube review of 0.1.21, "it just maxes out at
+// two bars"). The take runs the way AudioEngine::updateRecording runs it: NoteRecorder events in time order, each
+// finished batch committed by commitNoteBatch inside one edit followed by followPatternLengths (AudioEngine::editSong),
+// the song played linearly from the take's first note (Song mode without a loop region).
+
+namespace rec168k
+{
+struct Take
+{
+    lynx::Song& song;
+    lynx::NoteRecorder recorder;
+    std::vector<int> cleared;
+    bool first = true;
+    bool placeClip = true;
+    lynx::TakeQuantize q {};
+
+    explicit Take (lynx::Song& s) : song (s)
+    {
+        q.grid = 0;
+        recorder.begin (song.patterns[(std::size_t) song.currentPattern].effectiveLength (song.beatsPerBar));
+        if (song.mode == lynx::PlayMode::song && ! song.hasLoop())
+            recorder.setLinear (true); // AudioEngine: noteTakeLinear for the whole take
+    }
+    void flush()
+    {
+        auto notes = recorder.takeFinished();
+        if (notes.empty())
+            return;
+        const auto before = lynx::patternLengths (song);
+        lynx::commitNoteBatch (song, recorder, std::move (notes), lynx::RecordMode::overdub, q, cleared, placeClip, first);
+        lynx::followPatternLengths (before, song);
+        first = false;
+    }
+    void event (double tick, int key, bool on)
+    {
+        recorder.add (song, { tick, 0, key, on ? 0.8f : 0.0f, on });
+        flush();
+    }
+    void note (double tick, double length, int key)
+    {
+        event (tick, key, true);
+        event (tick + length, key, false);
+    }
+    void stop (double tick)
+    {
+        recorder.end (song, tick);
+        flush();
+    }
+};
+
+// the probe's song: Song mode, Pattern 1 played as 4 clips of 2 bars (bars 1–8), Pattern 2 new and current
+lynx::Song videoSong()
+{
+    auto s = lynx::Song::makeDefault();
+    const std::int64_t bar = (std::int64_t) lynx::ppq * s.beatsPerBar;
+    s.mode = lynx::PlayMode::song;
+    s.patterns[0].notes.clear();
+    s.patterns.resize (2);
+    s.patterns[1] = lynx::Pattern {};
+    s.playlist.clear();
+    for (int i = 0; i < 4; ++i)
+    {
+        lynx::Clip c;
+        c.kind = lynx::ClipKind::pattern;
+        c.pattern = 0;
+        c.track = 0;
+        c.start = 2 * bar * i;
+        c.length = 2 * bar;
+        s.playlist.push_back (c);
+    }
+    s.patterns[0].lengthTicks = 2 * bar;
+    lynx::Note n0;
+    n0.length = lynx::ppq;
+    s.patterns[0].notes.push_back (n0);
+    s.currentPattern = 1;
+    return s;
+}
+
+std::vector<const lynx::Clip*> clipsOf (const lynx::Song& s, int pattern)
+{
+    std::vector<const lynx::Clip*> out;
+    for (const auto& c : s.playlist)
+        if (c.kind == lynx::ClipKind::pattern && c.pattern == pattern)
+            out.push_back (&c);
+    return out;
+}
+} // namespace rec168k
+
+TEST (rec168k_song_take_of_8_bars_into_an_empty_pattern_keeps_8_bars)
+{
+    // the video (probe case A): 4 notes a bar over bars 1–8, Pattern 2 has no clip → it used to store all 32 notes in
+    // bar 1 (the loop length frozen at the empty pattern's 1 bar)
+    auto s = rec168k::videoSong();
+    const std::int64_t bar = (std::int64_t) lynx::ppq * s.beatsPerBar;
+    rec168k::Take take (s);
+    for (int b = 0; b < 8; ++b)
+        for (int beat = 0; beat < 4; ++beat)
+            take.note ((double) (b * bar + beat * lynx::ppq), lynx::ppq * 0.9, 60 + b);
+    take.stop ((double) (8 * bar));
+
+    const auto& p = s.patterns[1];
+    CHECK (p.notes.size() == 32);
+    CHECK (p.effectiveLength (s.beatsPerBar) == 8 * bar);
+    for (const auto& n : p.notes)
+        CHECK (n.start / bar == n.key - 60); // every note in the bar it was played in
+    const auto clips = rec168k::clipsOf (s, 1);
+    CHECK (clips.size() == 1 && clips[0]->start == 0 && clips[0]->length == 8 * bar && clips[0]->offset == 0);
+    CHECK (clips[0]->track == 1); // the first free track
+    CHECK (s.songLength() == 8 * bar);
+}
+
+TEST (rec168k_take_clip_starts_at_the_bar_of_the_first_note_and_grows_past_the_song_end)
+{
+    // FL (REA_rodada165z-b fh2–fh5): recording started at bar 1, the first event at bar 3 → the clip starts at bar 3;
+    // the song plays on past its end (8 bars) while recording: bars 9–10 are kept where they were played
+    auto s = rec168k::videoSong();
+    const std::int64_t bar = (std::int64_t) lynx::ppq * s.beatsPerBar;
+    rec168k::Take take (s);
+    for (int b = 2; b < 10; ++b)
+        take.note ((double) (b * bar + lynx::ppq / 2), lynx::ppq, 60 + b);
+    take.stop ((double) (10 * bar + lynx::ppq));
+
+    const auto& p = s.patterns[1];
+    CHECK (p.notes.size() == 8);
+    CHECK (p.notes.front().start == lynx::ppq / 2 && p.notes.front().key == 62);
+    CHECK (p.notes.back().start == 7 * bar + lynx::ppq / 2 && p.notes.back().key == 69);
+    const auto clips = rec168k::clipsOf (s, 1);
+    CHECK (clips.size() == 1 && clips[0]->start == 2 * bar && clips[0]->length == 8 * bar);
+    CHECK (s.songLength() == 10 * bar);
+    // every note plays at the song position it was played at
+    for (const auto& n : p.notes)
+        CHECK (clips[0]->start + n.start == (std::int64_t) (n.key - 60) * bar + lynx::ppq / 2);
+}
+
+TEST (rec168k_note_held_at_stop_is_kept_to_the_stop)
+{
+    auto s = rec168k::videoSong();
+    const std::int64_t bar = (std::int64_t) lynx::ppq * s.beatsPerBar;
+    rec168k::Take take (s);
+    take.note ((double) bar, lynx::ppq, 60);
+    take.event ((double) (5 * bar + lynx::ppq), 67, true); // held when the transport stops
+    take.stop ((double) (5 * bar + 3 * lynx::ppq));
+    const auto& p = s.patterns[1];
+    CHECK (p.notes.size() == 2);
+    CHECK (p.notes.back().key == 67 && p.notes.back().start == 4 * bar + lynx::ppq && p.notes.back().length == 2 * lynx::ppq);
+    const auto clips = rec168k::clipsOf (s, 1);
+    CHECK (clips.size() == 1 && clips[0]->start == bar && clips[0]->length == 5 * bar);
+}
+
+TEST (rec168k_take_extends_the_clip_it_starts_in)
+{
+    // a clip of the pattern (automatic length, 1 bar) where the take starts: what is played past its end continues the
+    // pattern after it, and the clip follows the pattern's length
+    auto s = rec168k::videoSong();
+    const std::int64_t bar = (std::int64_t) lynx::ppq * s.beatsPerBar;
+    lynx::Clip c;
+    c.kind = lynx::ClipKind::pattern;
+    c.pattern = 1;
+    c.track = 2;
+    c.start = 4 * bar;
+    c.length = bar;
+    s.playlist.push_back (c);
+    rec168k::Take take (s);
+    for (int b = 4; b < 8; ++b)
+        take.note ((double) (b * bar), lynx::ppq, 60 + b);
+    take.stop ((double) (8 * bar));
+    const auto clips = rec168k::clipsOf (s, 1);
+    CHECK (clips.size() == 1 && clips[0]->start == 4 * bar && clips[0]->length == 4 * bar && clips[0]->track == 2);
+    CHECK (s.patterns[1].notes.size() == 4 && s.patterns[1].notes.back().start == 3 * bar);
+}
+
+TEST (rec168k_set_pattern_length_grows_to_the_notes_recorded_past_it)
+{
+    // Pattern 2 set to 1 bar (Channel Rack › Length), no clip: 3 bars recorded → 3 bars (whole bars), clip follows
+    auto s = rec168k::videoSong();
+    const std::int64_t bar = (std::int64_t) lynx::ppq * s.beatsPerBar;
+    s.patterns[1].lengthTicks = bar;
+    rec168k::Take take (s);
+    for (int b = 0; b < 3; ++b)
+        take.note ((double) (b * bar + lynx::ppq), lynx::ppq, 60 + b);
+    take.stop ((double) (3 * bar));
+    CHECK (s.patterns[1].lengthTicks == 3 * bar);
+    const auto clips = rec168k::clipsOf (s, 1);
+    CHECK (clips.size() == 1 && clips[0]->start == 0 && clips[0]->length == 3 * bar);
+}
+
+TEST (rec168k_loop_region_overdubs_one_origin)
+{
+    // Song mode with a loop region over bars 3–4: two passes land in the same 2 bars, the clip starts at the region
+    auto s = rec168k::videoSong();
+    const std::int64_t bar = (std::int64_t) lynx::ppq * s.beatsPerBar;
+    s.loopStart = 2 * bar;
+    s.loopEnd = 4 * bar;
+    rec168k::Take take (s);
+    take.note ((double) (3 * bar + lynx::ppq), lynx::ppq / 2, 60); // first pass: bar 4
+    take.note ((double) (2 * bar + lynx::ppq), lynx::ppq / 2, 62); // second pass: bar 3
+    take.stop ((double) (3 * bar));
+    const auto& p = s.patterns[1];
+    CHECK (p.notes.size() == 2);
+    CHECK (p.notes[0].key == 62 && p.notes[0].start == lynx::ppq);
+    CHECK (p.notes[1].key == 60 && p.notes[1].start == bar + lynx::ppq);
+    const auto clips = rec168k::clipsOf (s, 1);
+    CHECK (clips.size() == 1 && clips[0]->start == 2 * bar && clips[0]->length == 2 * bar);
+}
+
+TEST (rec168k_pitch_bend_lands_with_the_notes)
+{
+    // the wheel (automation clip in Song mode) and the notes of one take, both past the song's end: same song position
+    auto s = rec168k::videoSong();
+    const std::int64_t bar = (std::int64_t) lynx::ppq * s.beatsPerBar;
+    rec168k::Take take (s);
+    const auto at = (double) (9 * bar + lynx::ppq);
+    take.note ((double) (6 * bar), lynx::ppq, 60);
+    take.note (at, lynx::ppq, 64);
+    take.stop ((double) (10 * bar));
+    lynx::AutomationWrite w;
+    w.target = { lynx::AutomationTargetKind::channelParam, 0, 0, lynx::midictl::pitchBendBase };
+    w.name = "Pitch";
+    lynx::commitAutomationPoints (s, w, { { at, 0.5f }, { at + lynx::ppq / 2, 0.9f } }, 0, true);
+    const auto clips = rec168k::clipsOf (s, 1);
+    CHECK (clips.size() == 1);
+    const auto& late = s.patterns[1].notes.back();
+    CHECK (late.key == 64 && clips[0]->start + late.start == (std::int64_t) at);
+    const lynx::Clip* bend = nullptr;
+    for (const auto& c : s.playlist)
+        if (c.kind == lynx::ClipKind::automation && c.automation == w.automation)
+            bend = &c;
+    CHECK (bend != nullptr);
+    if (bend != nullptr)
+    {
+        const auto& a = s.automations[(std::size_t) bend->automation];
+        CHECK (! a.points.empty() && bend->start + a.points.front().tick - bend->offset == (std::int64_t) at);
+    }
+}
+
+TEST (rec168k_fl_measured_take_in_a_one_bar_song)
+{
+    // FL 2026 [EXEC rec168k m0a/m1a/m1b]: Pattern 1 clip on bars 1–2 (a 1-bar song), Song mode, new Pattern 2,
+    // record from bar 1; the song counted on (2:08 before any note), notes just before bars 3…8, the last one held
+    // through Stop at ~8:3 → Pattern 2 clip on Track 2 from bar 2 (the first note's bar) to bar 9, every note where it
+    // was played, the held note kept to the Stop
+    auto s = lynx::Song::makeDefault();
+    const std::int64_t bar = (std::int64_t) lynx::ppq * s.beatsPerBar;
+    s.mode = lynx::PlayMode::song;
+    s.patterns.resize (2);
+    s.patterns[1] = lynx::Pattern {};
+    lynx::Clip c;
+    c.kind = lynx::ClipKind::pattern;
+    c.pattern = 0;
+    c.length = bar;
+    s.playlist = { c };
+    s.currentPattern = 1;
+    CHECK (s.songLength() == bar);
+    rec168k::Take take (s);
+    const auto early = lynx::ppq / 8;
+    for (int b = 2; b < 7; ++b) // (tick b·bar = FL's bar b + 1)
+        take.note ((double) (b * bar - early), lynx::ppq / 2, 60 + b);
+    take.event ((double) (7 * bar - early), 67, true);
+    take.stop ((double) (7 * bar + 2 * lynx::ppq + lynx::ppq / 3));
+    const auto clips = rec168k::clipsOf (s, 1);
+    CHECK (clips.size() == 1 && clips[0]->start == bar && clips[0]->end() == 8 * bar && clips[0]->track == 1);
+    const auto& p = s.patterns[1];
+    CHECK (p.notes.size() == 6);
+    for (const auto& n : p.notes)
+        CHECK (clips[0]->start + n.start == (std::int64_t) (n.key - 60) * bar - early);
+    CHECK (p.notes.back().key == 67 && p.notes.back().length == 2 * lynx::ppq + lynx::ppq / 3 + early);
+}
+
+TEST (rec168k_take_inside_a_stretched_clip_grows_the_pattern)
+{
+    // FL 2026 [EXEC rec168k z_m2b, f12]: Pattern 3 = one kick step (1 bar), its clip stretched to bars 1–5; notes at
+    // 2.1, 3.3 and 4.2 stay there (D4 at the pattern's bar 2 in the Piano roll) — the pattern grows, nothing folds
+    auto s = lynx::Song::makeDefault();
+    const std::int64_t bar = (std::int64_t) lynx::ppq * s.beatsPerBar;
+    s.mode = lynx::PlayMode::song;
+    s.patterns[0].notes.clear();
+    lynx::Note kick;
+    kick.length = lynx::ticksPerStep;
+    s.patterns[0].notes.push_back (kick);
+    lynx::Clip c;
+    c.kind = lynx::ClipKind::pattern;
+    c.pattern = 0;
+    c.track = 2;
+    c.length = 4 * bar;
+    s.playlist = { c };
+    rec168k::Take take (s);
+    take.note ((double) bar, lynx::ppq / 2, 62);
+    take.note ((double) (2 * bar + 2 * lynx::ppq), lynx::ppq / 2, 64);
+    take.note ((double) (3 * bar + lynx::ppq), lynx::ppq / 2, 65);
+    take.stop ((double) (3 * bar + 3 * lynx::ppq));
+    const auto& p = s.patterns[0];
+    CHECK (p.notes.size() == 4);
+    CHECK (p.notes[1].start == bar && p.notes[2].start == 2 * bar + 2 * lynx::ppq && p.notes[3].start == 3 * bar + lynx::ppq);
+    CHECK (p.effectiveLength (s.beatsPerBar) == 4 * bar);
+    CHECK (s.playlist.size() == 1 && s.playlist[0].length == 4 * bar); // no clip added
+}
+
+TEST (rec168k_probe_case_b_set_length_pattern_in_an_8_bar_clip)
+{
+    // the video's other case (probe "8 B"): Pattern 2 set to 2 bars, its clip stretched to 8 bars, 8 bars recorded →
+    // the notes of bars 3–8 used to be stored past the pattern's 2 bars, never shown nor played. Now the pattern grows to
+    // 8 bars (as FL's does), so the clip shows and plays all 8 bars once
+    auto s = rec168k::videoSong();
+    const std::int64_t bar = (std::int64_t) lynx::ppq * s.beatsPerBar;
+    s.patterns[1].lengthTicks = 2 * bar;
+    lynx::Clip c;
+    c.kind = lynx::ClipKind::pattern;
+    c.pattern = 1;
+    c.track = 1;
+    c.length = 8 * bar;
+    s.playlist.push_back (c);
+    rec168k::Take take (s);
+    for (int b = 0; b < 8; ++b)
+        for (int beat = 0; beat < 4; ++beat)
+            take.note ((double) (b * bar + beat * lynx::ppq), lynx::ppq * 0.9, 60 + b);
+    take.stop ((double) (8 * bar));
+    const auto& p = s.patterns[1];
+    CHECK (p.notes.size() == 32 && p.lengthTicks == 8 * bar && p.effectiveLength (s.beatsPerBar) == 8 * bar);
+    for (const auto& n : p.notes)
+        CHECK (n.start / bar == n.key - 60);
+    const auto clips = rec168k::clipsOf (s, 1);
+    CHECK (clips.size() == 1 && clips[0]->start == 0 && clips[0]->length == 8 * bar);
+}
+
+TEST (rec168k_auto_length_pattern_stretched_clip_8_bars)
+{
+    // the video's moment 7: Pattern 1 recorded as 2 bars, its clip dragged to 8 bars, recorded again over the 8 bars
+    auto s = rec168k::videoSong();
+    const std::int64_t bar = (std::int64_t) lynx::ppq * s.beatsPerBar;
+    lynx::Note held;
+    held.start = bar + lynx::ppq;
+    held.length = lynx::ppq;
+    held.key = 50;
+    s.patterns[1].notes = { held }; // automatic length: 2 bars
+    lynx::Clip c;
+    c.kind = lynx::ClipKind::pattern;
+    c.pattern = 1;
+    c.track = 1;
+    c.length = 8 * bar;
+    s.playlist.push_back (c);
+    rec168k::Take take (s);
+    for (int b = 2; b < 8; ++b)
+        take.note ((double) (b * bar), lynx::ppq, 60 + b);
+    take.stop ((double) (8 * bar));
+    CHECK (s.patterns[1].notes.size() == 7 && s.patterns[1].effectiveLength (s.beatsPerBar) == 8 * bar);
+    CHECK (s.patterns[1].notes.back().start == 7 * bar);
+    CHECK (rec168k::clipsOf (s, 1).size() == 1);
+}

+ 1 - 0
tests/tests.cpp

@@ -16879,3 +16879,4 @@ TEST (fl_low_lifter_matches_fl_engine)
 #include "Transporter167fTests.inc"
 #include "Polish167pTests.inc"
 #include "Zoom167tTests.inc"
+#include "Rec168kTests.inc"