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

Merge branch 'feature/177b-i97'

Ka1-S 4 недель назад
Родитель
Сommit
bcb5a20007

+ 31 - 0
docs/analise/REA_rodada177b.md

@@ -77,3 +77,34 @@ lanes (design sent to the coordinator first) → slice 4 modulators (proposal on
 
 Cost when unused: none. The plugin path copies the side input only when the slot has one (pointer set per block by
 the renderer, cleared after use); no allocation on the audio thread (the fixed-block scratch is sized at creation).
+
+---
+
+## Issue #97 — several automation clips on one target
+
+**Report** (Disar, 0.1.24): a second automation clip on a plugin's CC silences the first clip.
+
+**Cause in LYNX [CODE]:** `RackState::build` bound one automation per target, "the last automation of the same target wins", and an automation with any clip produced a value at every tick (it held its first or last point outside its clips). So the highest-numbered automation drove the target for the whole song. This applied to every target kind: channel and mixer volume and pan, effect knobs, plugin parameters, MIDI CC, tempo. Plugin and CC bindings were made per automation, so both wrote every block and the last write stayed. `AutomationDisplay` and `Song::tempoAt` repeated the same rule.
+
+**FL's rule [EXEC]:** FL 2026 command-line renders (`FL64 /R /Ewav`, no input injected) of generated projects: a 440 Hz tone on channel 0, automation channels A (ch 1) and B (ch 2) both linked to channel 0's volume, 120 BPM, 1 bar = 2 s. Generator, projects and renders: `/mnt/e/lynx-work/177b/issue97/` (`py/mk97.py`, `mk97r.py`, `mk97s.py`, `flr/fl/`). Ramps: A 0.1→0.3, B 0.5→0.7, so the level names the clip.
+
+| Case | Clips (bars) | What FL plays |
+|---|---|---|
+| ra / rb | A 4–6, B 8–10 (and swapped) | own value before the first clip; each clip; the last clip's end value holds between and after; channel order does not matter |
+| rc / rd | A 4–8, B 6–10 (and swapped) | the later start takes over at its start and plays to its end |
+| re / rf | one clip 4–10, the other 6–8 inside it, on an upper track | the inner clip, then the outer one resumes at its own position |
+| rg / rh / ri | same span, tracks swapped, file order swapped | A, the lower channel, after a short B blip at the start in rg and rh |
+| rj | the same automation twice, 4–8 and 6–10 | the later clip restarts the curve |
+
+**Rule built:** where clips of the target play, the one that started last (same start: the lowest automation); between clips, the end value of the clip that ended last; before the first clip, the control's own value. `RackState` gives the automations of a target one slot (`automationGroups`, `automationFollowers`); `AutomationDisplay` and `Song::targetValueAt` (tempo) use the same rule. Tests: `tests/Issue97Tests.inc`.
+
+**Behaviour change beyond the report:** before the first clip LYNX used to play the clip's first point. It now keeps the control's own value, as in ra and rb. REA_rodada83 already recorded this rule for pattern events.
+
+**Other target kinds [EXEC]:** the Master fader (`y_mv_ra`, `y_mv_rc`, `y_mv_re`) and a plugin parameter, Emphasizer's input gain on the Master, slot 9, id `0x8000` (`pa`–`pk`, `py/mk97p2.py`), follow the same main rule: own value before the first clip, the later start wins, a clip still playing resumes, the end value holds. Renders in `/mnt/e/lynx-work/177b/issue97/out/`, launched with `render180d/flrun.ps1` (not activated). In these renders the slot marker 0x62 comes after the slot's plugin events, so the plugin before `0x62 = 9` is slot 9. A MIDI CC target itself was not measured.
+
+**Known differences (FL's evaluator not traced):** REA was time-boxed. It reached the clip's range setup (`FUN_00fe9b90`), but `TACArticulator.Compute` (FUN_00ba1700) has only virtual-table references and the per-tick loop was not found. LYNX keeps the simple rule in these cases:
+- `rk_three` and `pk` (A 4–6, B 5–8, A 6–9): FL plays A's second clip for t = 12–14 s, then B until B ends at 16 s, then A. The rule predicts A from 12 s to 18 s. FL does the same on channel volume and on the plugin parameter, so this is not noise.
+- Overlapping flat clips (`c`, `g` in `py/mk97.py`): the earlier clip comes back about a second after the later one starts (c at about 13.1 s, g at about 13.7 s).
+- The same start: on channel volume A, the lower channel, wins whatever the track or file order (`rg`, `rh`, `ri`). On the plugin parameter, `pg` stays on B and `ph` plays B for 4 s and then A. LYNX uses the lowest automation on every target.
+
+**Checked in the GUI (Windows work build of f402eaa15, 2026-11-07):** the reporter's flow on a VST3 instrument. sforzando is not installed here, so the instrument was a CC-logging test VST3 that maps every CC to a parameter and logs what it receives (`/mnt/e/lynx-work/177b/gui/probe/`). The steps: Channel rack › right-click the instrument › Automate MIDI controller › Common controllers › CC #1 › Create automation clip, then draw a line; a second clip the same way with a different line. Both clips start at the song start. Logged while playing: the first clip's curve only (both start together: one plays). After removing the second clip, the first clip's curve plays. After undo and then removing the first clip, the second clip's curve plays (peak 0.92 at beat 4). With the second clip moved to bar 6, both curves play in turn, and the CC holds the first clip's end value between them. Log: `/mnt/e/lynx-work/177b/issue97/gui-cc-prof.log`. Not checked: sforzando itself.

+ 24 - 10
src/core/AutomationDisplay.h

@@ -137,7 +137,13 @@ private:
             if (list.empty() || automation.isLive() || automation.target.kind == AutomationTargetKind::none
                 || automation.target.kind == AutomationTargetKind::tempo)
                 continue;
-            std::sort (list.begin(), list.end(), byStart);
+            // one source per target: its clips play as one (issue #97, the renderer's rule)
+            const auto same = std::find_if (sources.begin(), sources.end(), [&] (const Source& s) { return s.target == automation.target; });
+            if (same != sources.end())
+            {
+                same->clips.insert (same->clips.end(), list.begin(), list.end());
+                continue;
+            }
             Source s;
             s.target = automation.target;
             s.automation = a;
@@ -169,24 +175,32 @@ private:
         constexpr auto none = std::numeric_limits<float>::quiet_NaN();
         if (s.automation >= 0)
         {
-            if (song.mode != PlayMode::song || s.automation >= (int) song.automations.size())
+            if (song.mode != PlayMode::song)
                 return none; // in pattern mode the Playlist does not play (the renderer keeps the last value)
-            const auto& automation = song.automations[(std::size_t) s.automation];
-            const Clip* before = nullptr;
+            const Clip* playing = nullptr;
+            const Clip* ended = nullptr;
             for (const auto i : s.clips)
             {
                 if (i >= (int) song.playlist.size())
                     return none; // (the Song changed without a new stamp)
                 const auto& c = song.playlist[(std::size_t) i];
+                if (c.automation < 0 || c.automation >= (int) song.automations.size())
+                    return none;
                 if (tick < (double) c.start)
-                    break;
+                    continue;
                 if (tick < (double) c.end())
-                    return automation.valueAt (tick - (double) c.start + (double) c.offset);
-                before = &c;
+                {
+                    if (playing == nullptr || c.start > playing->start || (c.start == playing->start && c.automation < playing->automation))
+                        playing = &c;
+                }
+                else if (ended == nullptr || c.end() > ended->end() || (c.end() == ended->end() && c.start > ended->start))
+                    ended = &c;
             }
-            if (before != nullptr)
-                return automation.valueAt ((double) (before->length + before->offset));
-            return s.clips.front() < (int) song.playlist.size() ? automation.valueAt ((double) song.playlist[(std::size_t) s.clips.front()].offset) : none;
+            if (playing != nullptr)
+                return song.automations[(std::size_t) playing->automation].valueAt (tick - (double) playing->start + (double) playing->offset);
+            if (ended != nullptr)
+                return song.automations[(std::size_t) ended->automation].valueAt ((double) (ended->length + ended->offset));
+            return none; // before the first clip: the control's own value
         }
 
         if (s.pattern < 0 || s.pattern >= (int) song.patterns.size())

+ 65 - 27
src/core/RackRenderer.cpp

@@ -1303,10 +1303,34 @@ void RackState::buildAutomationTables()
         hasAutomation = hasAutomation || used;
     };
 
-    // the last automation of the same target wins
+    // issue #97: the automations with clips on one target share a slot, the clip that plays decides (automationValueAt)
+    automationGroups.assign ((std::size_t) count, {});
+    automationFollowers.assign ((std::size_t) count, 0);
+    const auto playsClips = [&] (int a) {
+        const auto& automation = automations[(std::size_t) a];
+        return ! automationClips[(std::size_t) a].empty() && ! automation.isLive() && zoneAutomations[(std::size_t) a] == 0
+               && automation.target.kind != AutomationTargetKind::none;
+    };
+    for (int a = count - 1; a > 0; --a)
+    {
+        if (automationFollowers[(std::size_t) a] != 0 || ! playsClips (a))
+            continue;
+        auto& group = automationGroups[(std::size_t) a];
+        for (int b = 0; b < a; ++b)
+            if (playsClips (b) && automations[(std::size_t) b].target == automations[(std::size_t) a].target)
+            {
+                group.push_back (b);
+                automationFollowers[(std::size_t) b] = 1;
+            }
+        if (! group.empty())
+            group.push_back (a);
+    }
+
+    // otherwise the last automation of the same target wins (a live controller link, Performance mode clips)
     for (int a = 0; a < count; ++a)
     {
-        if (automationClips[(std::size_t) a].empty() && ! automations[(std::size_t) a].isLive() && zoneAutomations[(std::size_t) a] == 0)
+        if (automationFollowers[(std::size_t) a] != 0
+            || (automationClips[(std::size_t) a].empty() && ! automations[(std::size_t) a].isLive() && zoneAutomations[(std::size_t) a] == 0))
             continue;
 
         bind (a, automations[(std::size_t) a].target);
@@ -1697,31 +1721,45 @@ float RackRenderer::automated (int automation, float base) const noexcept
 
 namespace
 {
-// Value of an automation at a song position: inside a clip, the curve; between clips,
-// the last value of the previous clip; before the first, its initial value. Depends only on
-// position, so playing from any point (or exporting) gives the same result.
-float automationValueAt (const Song& song, const Automation& automation, const std::vector<int>& clips, double tick) noexcept
+// Value of an automation slot at a song position, as FL plays it (FL renders, issue #97, REA_rodada177b.md): where clips
+// of the target play, the one that started last (same start: the lowest automation); between clips, the end of the clip
+// that ended last, whichever automation it belongs to; before the first clip, NaN: the control's own value. Depends only
+// on position, so playing from any point (or exporting) gives the same result.
+float automationValueAt (const RackState& state, int slot, double tick) noexcept
 {
-    const Clip* before = nullptr;
-
-    for (auto i : clips) // sorted by start
-    {
-        const auto& c = song.playlist[(std::size_t) i];
-
-        if (tick < (double) c.start)
-            break;
-
-        if (tick < (double) c.end())
-            return automation.valueAt (tick - (double) c.start + (double) c.offset);
+    const auto& song = state.song;
+    const auto& group = state.automationGroups[(std::size_t) slot];
+    const Clip* playing = nullptr;
+    const Clip* ended = nullptr;
+    int playingAutomation = -1, endedAutomation = -1;
 
-        before = &c;
-    }
+    const auto scan = [&] (int a) {
+        for (const auto i : state.automationClips[(std::size_t) a]) // sorted by start
+        {
+            const auto& c = song.playlist[(std::size_t) i];
+            if (tick < (double) c.start)
+                break;
+            if (tick < (double) c.end())
+            {
+                if (playing == nullptr || c.start > playing->start)
+                    playing = &c, playingAutomation = a;
+            }
+            else if (ended == nullptr || c.end() > ended->end() || (c.end() == ended->end() && c.start > ended->start))
+                ended = &c, endedAutomation = a;
+        }
+    };
 
-    if (before != nullptr)
-        return automation.valueAt ((double) (before->length + before->offset));
+    if (group.empty())
+        scan (slot);
+    else
+        for (const auto a : group) // ascending: the lowest automation keeps a tie
+            scan (a);
 
-    const auto& first = song.playlist[(std::size_t) clips.front()];
-    return automation.valueAt ((double) first.offset);
+    if (playing != nullptr)
+        return song.automations[(std::size_t) playingAutomation].valueAt (tick - (double) playing->start + (double) playing->offset);
+    if (ended != nullptr)
+        return song.automations[(std::size_t) endedAutomation].valueAt ((double) (ended->length + ended->offset));
+    return std::numeric_limits<float>::quiet_NaN();
 }
 } // namespace
 
@@ -1830,8 +1868,8 @@ void RackRenderer::updateAutomation (const RackState& state, double startTick) n
     const auto tick = std::fmod (std::max (0.0, startTick), length);
 
     for (std::size_t a = 0; a < state.automationClips.size(); ++a)
-        if (! state.automationClips[a].empty())
-            automationValues[a] = automationValueAt (song, song.automations[a], state.automationClips[a], tick);
+        if (! state.automationClips[a].empty() && state.automationFollowers[a] == 0)
+            automationValues[a] = automationValueAt (state, (int) a, tick);
 
     // Performance mode: the launched automation clips (the last block's passes) drive their automation
     if (perfRunning)
@@ -1863,8 +1901,8 @@ float RackRenderer::compensatedAutomation (const RackState& state, int automatio
     const auto lag = state.pdc.slotInput[(std::size_t) track][(std::size_t) slot];
     if (lag <= 0)
         return current;
-    return automationValueAt (state.song, state.song.automations[(std::size_t) automation], state.automationClips[(std::size_t) automation],
-                              std::max (0.0, compensateTick - lag * compensateTicksPerSample));
+    const auto earlier = automationValueAt (state, automation, std::max (0.0, compensateTick - lag * compensateTicksPerSample));
+    return std::isnan (earlier) ? current : earlier;
 }
 
 void RackRenderer::applyPluginParams (const RackState& state, int firstSlot, bool andLive) noexcept

+ 5 - 0
src/core/RackRenderer.h

@@ -68,6 +68,11 @@ struct RackState
     static constexpr int maxAutomations = 1024;
     std::vector<std::vector<int>> automationClips; // per automation: clips in the Playlist
     std::vector<char> zoneAutomations;             // per automation: it has clips in the performance zone
+    // Several automations with clips on one target play as one (issue #97, FL's rule: docs/analise/REA_rodada177b.md):
+    // the last of them holds the slot and lists them all here (ascending; empty = it plays alone); the others are
+    // marked as followers: they bind nothing and compute nothing.
+    std::vector<std::vector<int>> automationGroups;
+    std::vector<char> automationFollowers;
     std::vector<int> channelVolumeAuto, channelPanAuto;
     std::vector<int> mixerVolumeAuto, mixerPanAuto;
     std::vector<std::vector<std::array<int, maxEffectParams>>> effectParamAuto; // [track][slot][param] (built-in)

+ 19 - 30
src/core/Song.cpp

@@ -1135,40 +1135,29 @@ bool Song::setTargetValue (const AutomationTarget& t, float v) noexcept
     return false;
 }
 
-float Song::automationValueAt (int index, double tick) const
+float Song::targetValueAt (const AutomationTarget& target, double tick) const
 {
-    if (index < 0 || index >= (int) automations.size())
-        return std::numeric_limits<float>::quiet_NaN();
-
-    std::vector<const Clip*> clips;
-
+    // the same rule as playback (RackRenderer's automationValueAt, issue #97)
+    const Clip* playing = nullptr;
+    const Clip* ended = nullptr;
     for (const auto& c : playlist)
-        if (c.kind == ClipKind::automation && c.automation == index && c.length > 0 && ! c.muted && ! isTrackMuted (c.track))
-            clips.push_back (&c);
-
-    if (clips.empty())
-        return std::numeric_limits<float>::quiet_NaN();
-
-    std::stable_sort (clips.begin(), clips.end(), [] (const Clip* a, const Clip* b) { return a->start < b->start; });
-    const auto& automation = automations[(std::size_t) index];
-    const Clip* before = nullptr;
-
-    // same rule as playback: inside a clip the curve; between clips the last value; before, the first
-    for (const auto* c : clips)
     {
-        if (tick < (double) c->start)
-            break;
-
-        if (tick < (double) c->end())
-            return automation.valueAt (tick - (double) c->start + (double) c->offset);
-
-        before = c;
+        if (c.kind != ClipKind::automation || c.automation < 0 || c.automation >= (int) automations.size() || c.length <= 0 || c.muted
+            || isTrackMuted (c.track) || ! (automations[(std::size_t) c.automation].target == target) || tick < (double) c.start)
+            continue;
+        if (tick < (double) c.end())
+        {
+            if (playing == nullptr || c.start > playing->start || (c.start == playing->start && c.automation < playing->automation))
+                playing = &c;
+        }
+        else if (ended == nullptr || c.end() > ended->end() || (c.end() == ended->end() && c.start > ended->start))
+            ended = &c;
     }
-
-    if (before != nullptr)
-        return automation.valueAt ((double) (before->length + before->offset));
-
-    return automation.valueAt ((double) clips.front()->offset);
+    if (playing != nullptr)
+        return automations[(std::size_t) playing->automation].valueAt (tick - (double) playing->start + (double) playing->offset);
+    if (ended != nullptr)
+        return automations[(std::size_t) ended->automation].valueAt ((double) (ended->length + ended->offset));
+    return std::numeric_limits<float>::quiet_NaN();
 }
 
 double Song::tempoAt (double tick) const

+ 1 - 1
src/core/Song.h

@@ -1878,7 +1878,7 @@ struct Song
     bool setTargetValue (const AutomationTarget& target, float value) noexcept;
 
     // Value of an automation at a song position (the same one playback uses); NaN without clips.
-    float automationValueAt (int automation, double tick) const;
+    float targetValueAt (const AutomationTarget& target, double tick) const; // the clips of that target, as playback plays them
     // Tempo at `tick`, taking tempo automation into account (in song mode).
     double tempoAt (double tick) const;
     // Duration in seconds of the first `ticks` of the song, integrating the tempo automation.

+ 21 - 16
src/core/TempoMap.h

@@ -32,7 +32,7 @@ inline bool hasTempoEvents (const Song& song) noexcept
     return std::any_of (song.patterns.begin(), song.patterns.end(), [] (const Pattern& p) { return tempoLane (p) != nullptr; });
 }
 
-// The last tempo automation (the one that wins, as for every target), or −1.
+// The last tempo automation (the one that holds the slot; all of them play by the clip rule, issue #97), or −1.
 inline int tempoAutomation (const Song& song) noexcept
 {
     for (int i = (int) song.automations.size() - 1; i >= 0; --i)
@@ -124,37 +124,42 @@ inline double tempoAt (const Song& song, double tick) noexcept
         return e != nullptr ? tempoFromValue (e->value) : base;
     }
 
-    // the tempo automation: inside a clip its curve wins; after one, the value it ended with (written at its end)
+    // the tempo automations (issue #97, the rule of every target, RackRenderer's automationValueAt): inside clips the
+    // one that started last wins (same start: the lowest automation); after them, the value the clip that ended last
+    // ended with (written at its end); before the first clip, the first clip's first value (round 178a, lowest priority;
+    // unverified against FL for the tempo: the other targets have their own value there)
     Write automation;
-    const auto ta = tempoAutomation (song);
-    if (ta >= 0)
+    if (tempoAutomation (song) >= 0)
     {
-        const auto& a = song.automations[(std::size_t) ta];
         const Clip* inside = nullptr;
-        const Clip* before = nullptr;
+        const Clip* ended = nullptr;
         const Clip* first = nullptr;
         for (const auto& c : song.playlist)
         {
-            if (c.kind != ClipKind::automation || c.automation != ta || c.length <= 0 || c.muted || song.isTrackMuted (c.track))
+            if (c.kind != ClipKind::automation || c.automation < 0 || c.automation >= (int) song.automations.size() || c.length <= 0
+                || c.muted || song.isTrackMuted (c.track))
                 continue;
-            if (first == nullptr || c.start < first->start)
+            const auto& a = song.automations[(std::size_t) c.automation];
+            if (a.target.kind != AutomationTargetKind::tempo || a.isLive())
+                continue;
+            if (first == nullptr || c.start < first->start || (c.start == first->start && c.automation < first->automation))
                 first = &c;
             if (tick < (double) c.start)
                 continue;
             if (tick < (double) c.end())
             {
-                if (inside == nullptr || c.start < inside->start)
+                if (inside == nullptr || c.start > inside->start || (c.start == inside->start && c.automation < inside->automation))
                     inside = &c;
             }
-            else if (before == nullptr || c.start >= before->start)
-                before = &c;
+            else if (ended == nullptr || c.end() > ended->end() || (c.end() == ended->end() && c.start > ended->start))
+                ended = &c;
         }
-        if (inside != nullptr) // (playback's rule, Song::automationValueAt: the first clip by start that holds the position)
-            return tempoFromValue (a.valueAt (tick - (double) inside->start + (double) inside->offset));
-        if (before != nullptr)
-            automation = { (double) before->end(), a.valueAt ((double) (before->length + before->offset)) };
+        if (inside != nullptr)
+            return tempoFromValue (song.automations[(std::size_t) inside->automation].valueAt (tick - (double) inside->start + (double) inside->offset));
+        if (ended != nullptr)
+            automation = { (double) ended->end(), song.automations[(std::size_t) ended->automation].valueAt ((double) (ended->length + ended->offset)) };
         else if (first != nullptr)
-            automation.value = a.valueAt ((double) first->offset); // before the first clip: its first value (lowest priority)
+            automation.value = song.automations[(std::size_t) first->automation].valueAt ((double) first->offset);
     }
 
     Write events;

+ 1 - 1
tests/Issue12cTests.inc

@@ -41,7 +41,7 @@ TEST (issue12c_automated_value_at_the_playhead)
     const auto g0 = shown.getGeneration();
     CHECK (shown.update (song, 1, 4.0 * ppq, true));
     CHECK (shown.getGeneration() != g0);
-    CHECK (shown.valueOf (send) && std::abs (*shown.valueOf (send) - song.automationValueAt (0, 4.0 * ppq)) < 1.0e-5f);
+    CHECK (shown.valueOf (send) && std::abs (*shown.valueOf (send) - song.targetValueAt (song.automations[0].target, 4.0 * ppq)) < 1.0e-5f);
     CHECK (std::abs (*shown.valueOf (send) - 0.25f) < 0.01f);
     CHECK (! shown.valueOf (volume));
     CHECK (shown.shown (volume, 0.7f) == 0.7f);

+ 172 - 0
tests/Issue97Tests.inc

@@ -0,0 +1,172 @@
+// Issue #97: several automation clips on one target. FL's rule, from FL's own renders (FL64 /R, REA_rodada177b.md): where
+// clips play, the one that started last (same start: the lowest automation); between clips, the end of the clip that
+// ended last; before the first clip, the control's own value. Not only one automation of a target plays.
+namespace issue97
+{
+const auto bar = (std::int64_t) lynx::ppq * 4;
+
+lynx::Song song()
+{
+    auto s = lynx::Song::makeDefault();
+    s.bpm = 120.0;
+    s.mode = lynx::PlayMode::song;
+    return s;
+}
+
+// a flat automation of the Master's fader
+int addFlat (lynx::Song& s, float value)
+{
+    lynx::Automation a;
+    a.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
+    a.points = { { 0, value }, { bar * 32, value } };
+    s.automations.push_back (a);
+    return (int) s.automations.size() - 1;
+}
+
+void addClip (lynx::Song& s, int automation, int startBar, int bars, int track)
+{
+    lynx::Clip c;
+    c.kind = lynx::ClipKind::automation;
+    c.automation = automation;
+    c.track = track;
+    c.start = bar * startBar;
+    c.length = bar * bars;
+    s.playlist.push_back (c);
+}
+
+// the Master's peak over a bar
+float levelAt (const lynx::Song& s, int atBar)
+{
+    const double sr = 48000.0;
+    auto state = lynx::RackState::build (s, nullptr, sr);
+    lynx::RackRenderer r;
+    r.prepare (sr, 512);
+    std::vector<float> l (512), rr (512);
+    const auto tps = lynx::ticksPerSample (s.bpm, sr);
+    float peak = 0.0f;
+    for (double tick = (double) bar * atBar; tick < (double) bar * (atBar + 1); tick += 512.0 * tps)
+    {
+        r.process (state.get(), true, tick, 512, l.data(), rr.data());
+        for (const auto v : l)
+            peak = std::max (peak, std::abs (v));
+    }
+    return peak;
+}
+
+// the pattern in every bar, so every bar has signal
+lynx::Song withSound()
+{
+    auto s = song();
+    for (int b = 0; b < 32; ++b)
+        s.playlist.push_back ({ 0, 0, bar * b, bar, 0 });
+    return s;
+}
+} // namespace issue97
+
+TEST (issue97_one_target_shares_a_slot)
+{
+    auto s = issue97::song();
+    const auto a = issue97::addFlat (s, 0.2f), b = issue97::addFlat (s, 0.5f), c = issue97::addFlat (s, 0.8f);
+    issue97::addClip (s, a, 4, 2, 1);
+    issue97::addClip (s, b, 8, 2, 2);
+    auto state = lynx::RackState::build (s, nullptr, 48000.0);
+    CHECK (state->mixerVolumeAuto[0] == b); // (the third has no clip: it plays nothing)
+    CHECK (state->automationGroups[(std::size_t) b].size() == 2 && state->automationFollowers[(std::size_t) a] == 1);
+    CHECK (state->automationFollowers[(std::size_t) c] == 0 && state->automationGroups[(std::size_t) c].empty());
+
+    // the second automation's clip goes: the first plays alone again
+    s.playlist.pop_back();
+    state = lynx::RackState::build (s, nullptr, 48000.0);
+    CHECK (state->mixerVolumeAuto[0] == a && state->automationFollowers[(std::size_t) a] == 0 && state->automationGroups[(std::size_t) a].empty());
+}
+
+TEST (issue97_plugin_parameter_binds_once)
+{
+    // a CC / plugin parameter binding was made per automation: both wrote every block and the last one stayed
+    auto s = issue97::song();
+    s.channels[0].instrument = lynx::InstrumentKind::midiOut;
+    for (const auto value : { 0.2f, 0.5f })
+    {
+        lynx::Automation a;
+        a.target = { lynx::AutomationTargetKind::channelParam, 0, 0, lynx::midictl::ccBase + 1 };
+        a.points = { { 0, value }, { issue97::bar * 8, value } };
+        s.automations.push_back (a);
+    }
+    issue97::addClip (s, 0, 0, 2, 1);
+    issue97::addClip (s, 1, 2, 2, 2);
+    const auto state = lynx::RackState::build (s, nullptr, 48000.0);
+    CHECK (state->pluginParams.size() == 1 && state->pluginParams[0].automation == 1);
+}
+
+TEST (issue97_the_clip_that_plays_decides)
+{
+    // FL's ra / rb renders: before = own value; A, held; B, held; and the channel order does not matter
+    auto s = issue97::withSound();
+    const auto own = issue97::levelAt (s, 1);
+    const auto low = issue97::addFlat (s, 0.2f), high = issue97::addFlat (s, 0.5f);
+    issue97::addClip (s, low, 4, 2, 1);
+    issue97::addClip (s, high, 8, 2, 2);
+    const auto before = issue97::levelAt (s, 1), first = issue97::levelAt (s, 4), between = issue97::levelAt (s, 6);
+    const auto second = issue97::levelAt (s, 8), after = issue97::levelAt (s, 12);
+    CHECK (std::abs (before - own) < 1e-3f);
+    CHECK (first > 0.0f && first < second);
+    CHECK (std::abs (between - first) < 1e-3f);
+    CHECK (std::abs (after - second) < 1e-3f);
+
+    auto swapped = issue97::withSound();
+    const auto high2 = issue97::addFlat (swapped, 0.5f), low2 = issue97::addFlat (swapped, 0.2f);
+    issue97::addClip (swapped, low2, 4, 2, 1);
+    issue97::addClip (swapped, high2, 8, 2, 2);
+    CHECK (std::abs (issue97::levelAt (swapped, 4) - first) < 1e-3f);
+    CHECK (std::abs (issue97::levelAt (swapped, 8) - second) < 1e-3f);
+}
+
+TEST (issue97_overlap_the_later_start_wins)
+{
+    // FL's rc / rd / re renders
+    auto s = issue97::withSound();
+    const auto a = issue97::addFlat (s, 0.2f), b = issue97::addFlat (s, 0.5f);
+    issue97::addClip (s, a, 4, 6, 2); // A 4..10
+    issue97::addClip (s, b, 6, 2, 1); // B 6..8, inside it
+    const auto onlyA = issue97::levelAt (s, 4), inside = issue97::levelAt (s, 6), resumes = issue97::levelAt (s, 8);
+    CHECK (onlyA < inside);
+    CHECK (std::abs (resumes - onlyA) < 1e-3f);
+
+    // the same start: the lowest automation (FL's rg / rh / ri, whichever the track or the file order)
+    auto tie = issue97::withSound();
+    const auto a2 = issue97::addFlat (tie, 0.2f), b2 = issue97::addFlat (tie, 0.5f);
+    issue97::addClip (tie, b2, 4, 4, 1);
+    issue97::addClip (tie, a2, 4, 4, 2);
+    CHECK (std::abs (issue97::levelAt (tie, 5) - onlyA) < 1e-3f);
+
+    // the same automation twice: the later clip restarts its curve (FL's rj)
+    auto same = issue97::song();
+    lynx::Automation ramp;
+    ramp.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
+    ramp.points = { { 0, 0.0f }, { issue97::bar * 4, 1.0f } };
+    same.automations.push_back (ramp);
+    issue97::addClip (same, 0, 0, 4, 1);
+    issue97::addClip (same, 0, 2, 4, 2);
+    CHECK (std::abs (same.targetValueAt (ramp.target, (double) issue97::bar * 2) - 0.0f) < 1e-5f);
+    CHECK (std::isnan (same.targetValueAt ({ lynx::AutomationTargetKind::mixerPan, 0 }, 0.0)));
+}
+
+TEST (issue97_two_tempo_clips)
+{
+    auto s = issue97::song();
+    for (const auto value : { 0.2f, 0.6f })
+    {
+        lynx::Automation a;
+        a.target.kind = lynx::AutomationTargetKind::tempo;
+        a.points = { { 0, value }, { issue97::bar * 8, value } };
+        s.automations.push_back (a);
+    }
+    issue97::addClip (s, 0, 2, 2, 1);
+    issue97::addClip (s, 1, 6, 2, 2);
+    // before the first clip: the first clip's value (178a's rule for the tempo, unverified against FL)
+    CHECK (std::abs (s.tempoAt ((double) issue97::bar) - lynx::tempoFromValue (0.2f)) < 1.0e-3);
+    const auto first = s.tempoAt ((double) issue97::bar * 3), second = s.tempoAt ((double) issue97::bar * 7);
+    CHECK (first < second);
+    CHECK (s.tempoAt ((double) issue97::bar * 5) == first);
+    CHECK (s.tempoAt ((double) issue97::bar * 9) == second);
+}

+ 1 - 0
tests/tests.cpp

@@ -17123,3 +17123,4 @@ TEST (fl_low_lifter_matches_fl_engine)
 #include "Mixer177a3Tests.inc"
 #include "Mixer177a4Tests.inc"
 #include "Sc177bTests.inc"
+#include "Issue97Tests.inc"