فهرست منبع

Several automation clips on one target play as in FL: the clip that plays decides (issue #97)

Ka1-S 4 هفته پیش
والد
کامیت
0fd32dfd2a
9فایلهای تغییر یافته به همراه322 افزوده شده و 78 حذف شده
  1. 28 0
      docs/analise/REA_rodada177b.md
  2. 24 10
      src/core/AutomationDisplay.h
  3. 68 32
      src/core/RackRenderer.cpp
  4. 5 0
      src/core/RackRenderer.h
  5. 23 34
      src/core/Song.cpp
  6. 1 1
      src/core/Song.h
  7. 1 1
      tests/Issue12cTests.inc
  8. 171 0
      tests/Issue97Tests.inc
  9. 1 0
      tests/tests.cpp

+ 28 - 0
docs/analise/REA_rodada177b.md

@@ -77,3 +77,31 @@ 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.
+
+**Not explained or not checked:**
+- Flat clips that overlap (c, g in `py/mk97.py`) are erratic in FL: 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). LYNX follows the ramp rule there.
+- `rk_three` (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, where the rule predicts A until 18 s. LYNX plays A. FL's evaluator was not traced with REA.
+- FL was measured on channel volume only, not on a plugin's CC or on tempo. LYNX applies the same rule to them.
+- The report's "removing the last clip does not bring the first back": in the engine the first automation takes the slot again (`issue97_one_target_shares_a_slot`). Not run in the GUI with sforzando.

+ 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())

+ 68 - 32
src/core/RackRenderer.cpp

@@ -1266,10 +1266,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);
@@ -1657,31 +1681,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
 
@@ -1790,8 +1828,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)
@@ -1823,8 +1861,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
@@ -2664,11 +2702,9 @@ void RackRenderer::process (const RackState* state,
     if (tempoOverride <= 0.0 && state->tempoAutomation >= 0 && state->song.mode == PlayMode::song && ! isSlotOverridden (state->tempoAutomation))
     {
         const auto length = (double) std::max<std::int64_t> (1, state->schedule.songLength);
-        const auto& clips = state->automationClips[(std::size_t) state->tempoAutomation];
-
-        if (! clips.empty())
-            bpm = tempoFromValue (automationValueAt (state->song, state->song.automations[(std::size_t) state->tempoAutomation], clips,
-                                                     std::fmod (std::max (0.0, startTick), length)));
+        if (! state->automationClips[(std::size_t) state->tempoAutomation].empty())
+            if (const auto v = automationValueAt (*state, state->tempoAutomation, std::fmod (std::max (0.0, startTick), length)); ! std::isnan (v))
+                bpm = tempoFromValue (v);
     }
 
     const auto tps = ticksPerSample (bpm, sampleRate);

+ 5 - 0
src/core/RackRenderer.h

@@ -66,6 +66,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)

+ 23 - 34
src/core/Song.cpp

@@ -1121,40 +1121,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
@@ -1162,10 +1151,10 @@ double Song::tempoAt (double tick) const
     if (mode != PlayMode::song)
         return bpm;
 
-    for (int i = (int) automations.size() - 1; i >= 0; --i) // the last tempo automation wins
-        if (automations[(std::size_t) i].target.kind == AutomationTargetKind::tempo)
-            if (const auto v = automationValueAt (i, tick); ! std::isnan (v))
-                return tempoFromValue (v);
+    AutomationTarget tempo;
+    tempo.kind = AutomationTargetKind::tempo;
+    if (const auto v = targetValueAt (tempo, tick); ! std::isnan (v))
+        return tempoFromValue (v);
 
     return bpm;
 }

+ 1 - 1
src/core/Song.h

@@ -1748,7 +1748,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.

+ 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);

+ 171 - 0
tests/Issue97Tests.inc

@@ -0,0 +1,171 @@
+// 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);
+    CHECK (s.tempoAt ((double) issue97::bar) == s.bpm); // before: the project's tempo
+    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

@@ -17111,3 +17111,4 @@ TEST (fl_low_lifter_matches_fl_engine)
 #include "Bridge176aTests.inc"
 #include "Mixer177aTests.inc"
 #include "Sc177bTests.inc"
+#include "Issue97Tests.inc"