Преглед изворни кода

Issue #152: LYNX Synth's and LYNX Soundfont's knobs take linked controllers and automation (round 190he)

- core/SynthParams.h: LYNX Synth's 30 knobs (3 oscillators x octave, semitone, fine, pan,
  level; envelope, filter, porta, FM, keytrack, sweep, 3-band tone) as channelParam targets
  base 0x2800 + k, 0..1 along each knob's own travel and skew. The numbers sit apart from an
  FL plugin's parameters, so an FL 3x Osc that LYNX converted keeps its old automation dead.
- Song::setTargetValue / targetValue: synth knobs, and LYNX Soundfont's knobs (no case
  before, so a MIDI link on them moved nothing; sf::automatable keeps the bank, the program
  and the send tracks out, as the editor's knobs).
- SynthInstrument::setParameter: an automation clip or a modulator drives a knob on the
  audio thread without an allocation; a Song change of that knob (hand, link) takes over,
  as the Sampler's knobs (issue #12). RackRenderer binds the synth's targets.
- Last tweaked shows the synth knobs in the page's units.
- Tests: tests/U190heLinkTests.inc (u190he_link_*).
Ka1-S пре 3 недеља
родитељ
комит
b1a3ca2d7c

+ 55 - 4
src/core/Instruments.cpp

@@ -1,6 +1,7 @@
 #include "core/FruityDance.h"
 #include "core/FruitySlicer.h"
 #include "core/Instruments.h"
+#include "core/SynthParams.h"
 #include "core/FpcInstrument.h"
 #include "core/ControlSurface.h"
 #include "core/VoltageController.h"
@@ -969,6 +970,12 @@ private:
 class SynthInstrument final : public Instrument
 {
 public:
+    SynthInstrument()
+    {
+        for (auto& k : automatedKnobs)
+            k.store (std::numeric_limits<float>::quiet_NaN(), std::memory_order_relaxed);
+    }
+
     void prepare (double sr) noexcept override
     {
         sampleRate = sr;
@@ -983,6 +990,17 @@ public:
         bpm.store (std::max (1.0, songBpm)); // first: the synced LFO uses it when reading the settings
         channelPitch.store (channel.sampler.pitch * 100.0f); // the channel's PITCH knob (issue #14)
         updateFrames (channel.synth);
+        // an automated knob (issue #152, round 190he) keeps the automation's value until its Song value changes (the
+        // knob turned by hand, a MIDI link), as the Sampler's (issue #12)
+        for (int k = 0; k < synthparam::count; ++k)
+        {
+            const auto now = synthparam::get (channel.synth, k);
+            if (! (configuredKnobs[(std::size_t) k] == now))
+            {
+                configuredKnobs[(std::size_t) k] = now;
+                automatedKnobs[(std::size_t) k].store (std::numeric_limits<float>::quiet_NaN(), std::memory_order_relaxed);
+            }
+        }
         if (const auto& l = channel.misc.levels; l.modX != 0.0f || l.modY != 0.0f) // Levels adjustment Mod X / Y (round 190dz)
         {
             auto adjusted = channel.synth;
@@ -1017,6 +1035,32 @@ public:
 
     void setSongPosition (double ticks) noexcept override { songTicks = ticks; }
 
+    // an automation clip / a modulator on one of LYNX Synth's knobs (core/SynthParams.h, issue #152). Audio thread.
+    void setParameter (int index, float value) noexcept override
+    {
+        if (! synthparam::valid (index) || ! std::isfinite (value))
+            return;
+        auto& slot = automatedKnobs[(std::size_t) synthparam::indexOf (index)];
+        if (slot.load (std::memory_order_relaxed) == value)
+            return;
+        slot.store (value, std::memory_order_relaxed);
+        automatedGeneration.fetch_add (1, std::memory_order_release);
+    }
+
+    // the Song's settings (configure's mailbox) with the automated knobs on top; coefficients only when something changed
+    void pullSettings() noexcept
+    {
+        const auto fresh = mailbox.read (settings);
+        const auto generation = automatedGeneration.load (std::memory_order_acquire);
+        if (! fresh && generation == appliedGeneration)
+            return;
+        appliedGeneration = generation;
+        for (int k = 0; k < synthparam::count; ++k)
+            if (const auto v = automatedKnobs[(std::size_t) k].load (std::memory_order_relaxed); ! std::isnan (v))
+                synthparam::set (settings, k, v);
+        updateCoefficients();
+    }
+
     // portamento: the voice starts at `from` and glides to `to` over the porta time (channel porta,
     // inverted by the note)
     void glide (SynthVoice& v, double from, double to) noexcept
@@ -1038,8 +1082,7 @@ public:
 
     void noteOn (int key, float velocity) noexcept override
     {
-        if (mailbox.read (settings))
-            updateCoefficients();
+        pullSettings();
 
         if (settings.mono)
         {
@@ -1180,8 +1223,7 @@ public:
 
     void render (float* left, float* right, int n, float gl, float gr) noexcept override
     {
-        if (mailbox.read (settings))
-            updateCoefficients();
+        pullSettings();
         // new spectrum frames (built in configure): the old set goes back for the other thread to free
         if (retiredFrames.load() == nullptr)
             if (auto* frames = incomingFrames.exchange (nullptr))
@@ -1878,6 +1920,15 @@ private:
     double sampleRate = 44100.0;
     SettingsMailbox<SynthSettings> mailbox;
     SynthSettings settings;
+    // automated knobs (issue #152): 0…1 or NaN (not automated); the Song values configure last saw (UI thread)
+    std::array<std::atomic<float>, synthparam::count> automatedKnobs; // (NaN from the constructor)
+    std::array<float, synthparam::count> configuredKnobs { [] {
+        std::array<float, synthparam::count> a {};
+        a.fill (std::numeric_limits<float>::quiet_NaN());
+        return a;
+    }() };
+    std::atomic<std::uint32_t> automatedGeneration { 0 };
+    std::uint32_t appliedGeneration = 0; // audio thread
     float attackStep = 0.01f;
     float decayCoeff = 0.999f;
     float releaseCoeff = 0.999f;

+ 31 - 0
src/core/LastTweaked.h

@@ -13,6 +13,7 @@
 
 #include "core/Effects.h"
 #include "core/Song.h"
+#include "core/SynthParams.h"
 
 namespace lynx::lasttweak
 {
@@ -76,6 +77,36 @@ inline std::string valueText (const Song& song, const AutomationTarget& t, float
         }
         case AutomationTargetKind::channelParam:
         {
+            // LYNX Synth's knobs (issue #152, round 190he): the page's units
+            if (t.index >= 0 && t.index < (int) song.channels.size() && song.channels[(std::size_t) t.index].instrument == InstrumentKind::synth
+                && synthparam::valid (t.param))
+            {
+                const auto k = synthparam::indexOf (t.param);
+                const auto x = synthparam::toNatural (k, v);
+                if (k < synthparam::attack)
+                    switch (k % synthparam::oscFields)
+                    {
+                        case synthparam::octave: std::snprintf (text, sizeof (text), "%+d oct", (int) std::lround (x)); break;
+                        case synthparam::semitones: std::snprintf (text, sizeof (text), "%+d st", (int) std::lround (x)); break;
+                        case synthparam::detune: std::snprintf (text, sizeof (text), "%+d ct", (int) std::lround (x)); break;
+                        default: std::snprintf (text, sizeof (text), "%.2f", (double) x); break;
+                    }
+                else if (k == synthparam::attack || k == synthparam::decay || k == synthparam::release || k == synthparam::fmDecay
+                         || k == synthparam::sweepTime)
+                    std::snprintf (text, sizeof (text), "%.3f s", (double) x);
+                else if (k == synthparam::cutoff)
+                {
+                    if (x >= 1000.0f)
+                        std::snprintf (text, sizeof (text), "%.1f kHz", (double) x / 1000.0);
+                    else
+                        std::snprintf (text, sizeof (text), "%d Hz", (int) std::lround (x));
+                }
+                else if (k == synthparam::porta)
+                    std::snprintf (text, sizeof (text), "%.3f t", (double) x);
+                else
+                    std::snprintf (text, sizeof (text), "%.2f", (double) x);
+                break;
+            }
             // the Sampler's knobs (issue #12)
             if (t.index < 0 || t.index >= (int) song.channels.size() || song.channels[(std::size_t) t.index].instrument != InstrumentKind::sampler
                 || t.param < 0 || t.param >= samplerparam::count)

+ 3 - 0
src/core/RackRenderer.cpp

@@ -9,6 +9,7 @@
 #include "core/SlicexInstrument.h"
 #include "core/TempoMap.h"
 #include "core/ModelParams.h"
+#include "core/SynthParams.h"
 #include "core/FlmDwSampler.h"
 #include "core/FlmSlicer.h"
 #include "core/SamplePrecompute.h"
@@ -1918,6 +1919,8 @@ void RackState::buildAutomationTables()
                                       && t.param < samplerparam::count)
                                   // the knobs of an FL engine kept in the channel's state (round 168n)
                                   || modelparams::find (song.channels[(std::size_t) t.index].instrument, t.param) != nullptr
+                                  // LYNX Synth's knobs (issue #152, round 190he)
+                                  || (song.channels[(std::size_t) t.index].instrument == InstrumentKind::synth && synthparam::valid (t.param))
                                   // FL Studio Mobile's DW Sampler: its own 34 values (round 190de)
                                   || (flmdw::isChannel (song.channels[(std::size_t) t.index]) && t.param >= flmdw::automationBase
                                       && t.param < flmdw::automationBase + flmdw::paramCount)

+ 21 - 0
src/core/Song.cpp

@@ -8,6 +8,8 @@
 #include "core/MidiControls.h"
 #include "core/SessionIds.h"
 #include "core/FlCurve.h"
+#include "core/SynthParams.h"
+#include "core/SoundFont.h"
 
 #include <algorithm>
 #include <cstdio>
@@ -1313,6 +1315,12 @@ float Song::targetValue (const AutomationTarget& t) const noexcept
             // the Sampler's knobs (issue #12)
             if (channels[(std::size_t) t.index].instrument == InstrumentKind::sampler && t.param >= 0 && t.param < samplerparam::count)
                 return samplerparam::get (channels[(std::size_t) t.index].sampler, t.param);
+            // LYNX Soundfont's knobs (issue #152, round 190he: a linked controller moved nothing)
+            if (channels[(std::size_t) t.index].instrument == InstrumentKind::soundfont && sf::automatable (t.param))
+                return sf::normalise (t.param, sf::readParams (channels[(std::size_t) t.index].modelParams)[(std::size_t) t.param]);
+            // LYNX Synth's knobs (issue #152, round 190he; core/SynthParams.h)
+            if (channels[(std::size_t) t.index].instrument == InstrumentKind::synth && synthparam::valid (t.param))
+                return synthparam::get (channels[(std::size_t) t.index].synth, synthparam::indexOf (t.param));
             // a MIDI Out's patch and knobs (round 167i)
             if (channels[(std::size_t) t.index].instrument == InstrumentKind::midiOut && t.param >= 0 && t.param <= midiout::controlCount)
                 return midiout::paramValue (midiout::stateOf (channels[(std::size_t) t.index].pluginState), t.param);
@@ -1408,6 +1416,19 @@ bool Song::setTargetValue (const AutomationTarget& t, float v) noexcept
                 samplerparam::set (channels[(std::size_t) t.index].sampler, t.param, v);
                 return true;
             }
+            if (validChannel && channels[(std::size_t) t.index].instrument == InstrumentKind::soundfont && sf::automatable (t.param))
+            {
+                auto& c = channels[(std::size_t) t.index]; // (issue #152, round 190he)
+                auto params = sf::readParams (c.modelParams);
+                params[(std::size_t) t.param] = sf::denormalise (t.param, v);
+                c.modelParams.assign (params.begin(), params.end());
+                return true;
+            }
+            if (validChannel && channels[(std::size_t) t.index].instrument == InstrumentKind::synth && synthparam::valid (t.param))
+            {
+                synthparam::set (channels[(std::size_t) t.index].synth, synthparam::indexOf (t.param), v); // (issue #152, round 190he)
+                return true;
+            }
             if (validChannel && channels[(std::size_t) t.index].instrument == InstrumentKind::midiOut && t.param >= 0
                 && t.param <= midiout::controlCount)
             {

+ 6 - 0
src/core/SoundFont.h

@@ -166,6 +166,12 @@ const ParamInfo& paramInfo (int index) noexcept;
 // A channel's settings: modelParams in table order, missing ones at their default, each clamped to its range
 std::array<float, paramCount> readParams (const std::vector<float>& modelParams) noexcept;
 std::vector<float> defaultParams();
+// the parameters an automation clip / a linked controller drives (all but the bank, the program and the send tracks:
+// the editor's knobs with FL's parameter menu)
+constexpr bool automatable (int index) noexcept
+{
+    return index >= 0 && index < paramCount && index != pBank && index != pProgram && index != pReverbTrack && index != pChorusTrack;
+}
 // 0…1 (automation) ↔ a parameter's value
 float normalise (int index, float value) noexcept;
 float denormalise (int index, float value01) noexcept;

+ 167 - 0
src/core/SynthParams.h

@@ -0,0 +1,167 @@
+#pragma once
+
+// LYNX Synth's knobs as automation targets (issue #152, round 190he: "LYNX SHARE's own plugins can't be linked"). FL
+// makes every parameter of its plugins linkable (Link to controller, automation clips, Init song value); LYNX Synth's
+// page had plain knobs with no parameter menu. A synth channel's AutomationTarget { channelParam, channel, 0, base + k }
+// with k one of these. The numbers start at `base` so that an FL project's dead automation on a 3x Osc that LYNX
+// converted to its synth (FL plugin parameter P = channelParam P, small numbers) never turns into one of these.
+// Values 0…1 follow the knobs' own travel (the Synth page's ranges and skews), so a clip at 50 % is the knob at mid
+// travel and a MIDI controller's centre is the knob's centre.
+
+#include "core/Song.h"
+
+#include <algorithm>
+#include <cmath>
+
+namespace lynx::synthparam
+{
+constexpr int base = 0x2800;
+// the oscillators' knobs (osc o: o * oscFields + field), then the page's other knobs
+enum Field : int { octave, semitones, detune, pan, level, oscFields };
+enum : int
+{
+    attack = 3 * oscFields, decay, sustain, release, cutoff, resonance, porta, fm, fmDecay, keyTrack, sweep, sweepTime,
+    bandLow, bandMid, bandHigh, count
+};
+
+constexpr bool valid (int param) noexcept { return param >= base && param < base + count; }
+constexpr int target (int k) noexcept { return base + k; }
+constexpr int indexOf (int param) noexcept { return param - base; }
+constexpr int oscParam (int osc, Field f) noexcept { return osc * oscFields + f; }
+
+struct Range
+{
+    const char* name; // English, as the page's labels ("Osc 1" goes before an oscillator's)
+    float lo, hi, mid; // mid < 0: linear (else the knob's skew from its mid point, as juce::Slider)
+    float step;        // > 0: the knob's interval (whole octaves, semitones and cents)
+};
+
+inline const Range& rangeOf (int k) noexcept
+{
+    // the same ranges and skews as the Synth page's knobs (ui/ChannelSettings.cpp SynthPanel)
+    static constexpr Range oscRanges[oscFields] = { { "Octave", -3.0f, 3.0f, -1.0f, 1.0f },     { "Semitone", -11.0f, 11.0f, -1.0f, 1.0f },
+                                                    { "Fine", -100.0f, 100.0f, -1.0f, 1.0f },  { "Pan", -1.0f, 1.0f, -1.0f, 0.0f },
+                                                    { "Level", 0.0f, 4.0f, 1.0f, 0.0f } };
+    static constexpr Range others[count - attack] = {
+        { "Attack", 0.0f, 5.0f, 0.5f, 0.0f },       { "Decay", 0.0f, 5.0f, 0.5f, 0.0f },    { "Sustain", 0.0f, 1.0f, -1.0f, 0.0f },
+        { "Release", 0.0f, 5.0f, 0.5f, 0.0f },      { "Cutoff", 20.0f, 20000.0f, 1000.0f, 0.0f }, { "Resonance", 0.0f, 1.0f, -1.0f, 0.0f },
+        { "Porta time", 0.0f, 2.0f, 0.25f, 0.0f },  { "FM", 0.0f, 8.0f, 1.0f, 0.0f },       { "FM decay", 0.0f, 5.0f, 0.5f, 0.0f },
+        { "Keytrack", -1.0f, 2.0f, -1.0f, 0.0f },   { "Sweep", 0.0f, 100.0f, 2.0f, 0.0f },  { "Sweep time", 0.001f, 2.0f, 0.1f, 0.0f },
+        { "Low", 0.0f, 2.0f, -1.0f, 0.0f },         { "Mid", 0.0f, 2.0f, -1.0f, 0.0f },     { "High", 0.0f, 2.0f, -1.0f, 0.0f } };
+    static constexpr Range none { "", 0.0f, 1.0f, -1.0f, 0.0f };
+    if (k < 0 || k >= count)
+        return none;
+    return k < attack ? oscRanges[k % oscFields] : others[k - attack];
+}
+
+// 1-based oscillator of an oscillator knob, 0 for the others
+constexpr int oscOf (int k) noexcept { return k >= 0 && k < attack ? k / oscFields + 1 : 0; }
+
+inline double skewOf (const Range& r) noexcept
+{
+    return r.mid < 0.0f ? 1.0 : std::log (0.5) / std::log ((double) (r.mid - r.lo) / (double) (r.hi - r.lo));
+}
+
+// natural value (octaves, seconds, Hz…) -> 0…1 of the knob's travel
+inline float toValue (int k, float natural) noexcept
+{
+    const auto& r = rangeOf (k);
+    const auto p = std::clamp ((double) (natural - r.lo) / (double) (r.hi - r.lo), 0.0, 1.0);
+    return (float) std::pow (p, skewOf (r));
+}
+
+// 0…1 of the knob's travel -> natural value, on the knob's interval
+inline float toNatural (int k, float value) noexcept
+{
+    const auto& r = rangeOf (k);
+    const auto v = std::clamp ((double) value, 0.0, 1.0);
+    const auto p = v <= 0.0 ? 0.0 : std::exp (std::log (v) / skewOf (r));
+    auto x = r.lo + (r.hi - r.lo) * p;
+    if (r.step > 0.0f)
+        x = r.lo + std::round ((x - r.lo) / r.step) * r.step;
+    return (float) std::clamp (x, (double) r.lo, (double) r.hi);
+}
+
+inline float natural (const SynthSettings& s, int k) noexcept
+{
+    if (k >= 0 && k < attack)
+    {
+        const auto& o = s.osc[(std::size_t) (k / oscFields)];
+        switch (k % oscFields)
+        {
+            case octave: return (float) o.octave;
+            case semitones: return (float) o.semitones;
+            case detune: return o.detune;
+            case pan: return o.pan;
+            default: return o.level;
+        }
+    }
+    switch (k)
+    {
+        case attack: return s.attack;
+        case decay: return s.decay;
+        case sustain: return s.sustain;
+        case release: return s.release;
+        case cutoff: return s.cutoff;
+        case resonance: return s.resonance;
+        case porta: return s.portaBeats;
+        case fm: return s.fm;
+        case fmDecay: return s.fmDecay;
+        case keyTrack: return s.keyTrack;
+        case sweep: return s.sweep;
+        case sweepTime: return s.sweepTime;
+        case bandLow: return s.bandGain[0];
+        case bandMid: return s.bandGain[1];
+        case bandHigh: return s.bandGain[2];
+        default: return 0.0f;
+    }
+}
+
+// writes a natural value as the page's knob does (whole octaves / semitones; FM decay 0 = a constant index)
+inline void setNatural (SynthSettings& s, int k, float x) noexcept
+{
+    if (k < 0 || k >= count || ! std::isfinite (x))
+        return;
+    const auto& r = rangeOf (k);
+    x = std::clamp (x, r.lo, r.hi);
+    if (k < attack)
+    {
+        auto& o = s.osc[(std::size_t) (k / oscFields)];
+        switch (k % oscFields)
+        {
+            case octave: o.octave = (int) std::lround (x); break;
+            case semitones: o.semitones = (int) std::lround (x); break;
+            case detune: o.detune = x; break;
+            case pan: o.pan = x; break;
+            default: o.level = x; break;
+        }
+        return;
+    }
+    switch (k)
+    {
+        case attack: s.attack = x; break;
+        case decay: s.decay = x; break;
+        case sustain: s.sustain = x; break;
+        case release: s.release = x; break;
+        case cutoff: s.cutoff = x; break;
+        case resonance: s.resonance = x; break;
+        case porta: s.portaBeats = x; break;
+        case fm: s.fm = x; break;
+        case fmDecay:
+            s.fmDecay = x;
+            if (x <= 0.0f)
+                s.fmSustain = 1.0f;
+            break;
+        case keyTrack: s.keyTrack = x; break;
+        case sweep: s.sweep = x; break;
+        case sweepTime: s.sweepTime = x; break;
+        case bandLow: s.bandGain[0] = x; break;
+        case bandMid: s.bandGain[1] = x; break;
+        case bandHigh: s.bandGain[2] = x; break;
+        default: break;
+    }
+}
+
+inline float get (const SynthSettings& s, int k) noexcept { return toValue (k, natural (s, k)); }
+inline void set (SynthSettings& s, int k, float value) noexcept { setNatural (s, k, toNatural (k, value)); }
+} // namespace lynx::synthparam

+ 230 - 0
tests/U190heLinkTests.inc

@@ -0,0 +1,230 @@
+// Round 190he, issue #152 (Nuelka, 0.1.27, item 2): "a MIDI link on FL-engine plugins changes the sound but the knob
+// doesn't move; LYNX SHARE's own plugins can't be linked". LYNX Synth's knobs are automation targets now
+// (core/SynthParams.h: right-click = FL's parameter menu, MIDI links, automation clips); LYNX Soundfont's knobs take a
+// linked controller (Song::setTargetValue had no case for them, so a link moved nothing). The FL engines' knobs follow
+// the channel's state and their automation in ui/ChannelSettings.cpp (ModelPanel); here the state side they read.
+#include "core/SynthParams.h"
+#include "core/SoundFont.h"
+#include "core/LastTweaked.h"
+#include "core/ModelParams.h"
+#include "core/PoiZone.h"
+#include "core/Instruments.h"
+#include "core/RackRenderer.h"
+#include "core/LinkFormula.h"
+#include "core/MidiControls.h"
+
+TEST (u190he_link_synth_param_table_follows_the_knobs)
+{
+    using namespace lynx;
+    CHECK (synthparam::count == 30);
+    for (int k = 0; k < synthparam::count; ++k)
+    {
+        const auto& r = synthparam::rangeOf (k);
+        CHECK (r.name != nullptr && r.name[0] != 0 && r.hi > r.lo);
+        CHECK (synthparam::valid (synthparam::target (k)) && synthparam::indexOf (synthparam::target (k)) == k);
+        for (float v : { 0.0f, 0.25f, 0.5f, 0.9f, 1.0f })
+        {
+            const auto x = synthparam::toNatural (k, v);
+            CHECK (x >= r.lo - 1.0e-6f && x <= r.hi + 1.0e-6f);
+            if (r.step <= 0.0f) // (stepped knobs land on their steps)
+                CHECK (std::abs (synthparam::toValue (k, x) - v) < 1.0e-4f);
+        }
+        CHECK (std::abs (synthparam::toNatural (k, 0.0f) - r.lo) < 1.0e-5f && std::abs (synthparam::toNatural (k, 1.0f) - r.hi) < 1.0e-3f);
+    }
+    // the page's skews: half travel is the knob's mid point (attack 0.5 s, cutoff 1 kHz, osc level 1, porta 1/4 beat)
+    CHECK (std::abs (synthparam::toNatural (synthparam::attack, 0.5f) - 0.5f) < 1.0e-4f);
+    CHECK (std::abs (synthparam::toNatural (synthparam::cutoff, 0.5f) - 1000.0f) < 0.05f);
+    CHECK (std::abs (synthparam::toNatural (synthparam::oscParam (1, synthparam::level), 0.5f) - 1.0f) < 1.0e-4f);
+    CHECK (std::abs (synthparam::toNatural (synthparam::porta, 0.5f) - 0.25f) < 1.0e-4f);
+    // whole octaves and semitones, as the knobs step
+    CHECK (synthparam::toNatural (synthparam::oscParam (0, synthparam::octave), 0.5f) == 0.0f);
+    CHECK (synthparam::toNatural (synthparam::oscParam (0, synthparam::octave), 0.6f) == 1.0f); // (-3 + 3.6 = 0.6 -> 1)
+    CHECK (synthparam::toNatural (synthparam::oscParam (2, synthparam::semitones), 1.0f) == 11.0f);
+    CHECK (synthparam::oscOf (synthparam::oscParam (2, synthparam::pan)) == 3 && synthparam::oscOf (synthparam::cutoff) == 0);
+    // the numbers sit apart from an FL plugin's (an FL 3x Osc converted to LYNX Synth keeps its automation dead), the
+    // MIDI controls (0x1000…) and the other engines' bases
+    CHECK (! synthparam::valid (5) && ! synthparam::valid (midictl::ccBase + 1) && ! synthparam::valid (midictl::pitchBendBase));
+    CHECK (! synthparam::valid (synthparam::base - 1) && ! synthparam::valid (synthparam::base + synthparam::count));
+}
+
+TEST (u190he_link_synth_targets_read_and_write_the_song)
+{
+    using namespace lynx;
+    auto song = Song::makeDefault();
+    song.channels[0].instrument = InstrumentKind::synth;
+    const AutomationTarget cutoff { AutomationTargetKind::channelParam, 0, 0, synthparam::target (synthparam::cutoff) };
+    CHECK (song.setTargetValue (cutoff, 0.5f));
+    CHECK (std::abs (song.channels[0].synth.cutoff - 1000.0f) < 0.05f);
+    CHECK (std::abs (song.targetValue (cutoff) - 0.5f) < 1.0e-4f);
+    const AutomationTarget octave { AutomationTargetKind::channelParam, 0, 0, synthparam::target (synthparam::oscParam (1, synthparam::octave)) };
+    CHECK (song.setTargetValue (octave, 1.0f) && song.channels[0].synth.osc[1].octave == 3);
+    CHECK (std::abs (song.targetValue (octave) - 1.0f) < 1.0e-6f);
+    // FM decay 0 = a constant index, as the page writes it
+    song.channels[0].synth.fmSustain = 0.3f;
+    CHECK (song.setTargetValue ({ AutomationTargetKind::channelParam, 0, 0, synthparam::target (synthparam::fmDecay) }, 0.0f));
+    CHECK (song.channels[0].synth.fmDecay == 0.0f && song.channels[0].synth.fmSustain == 1.0f);
+    // the old numbers (an FL plugin's P on a converted 3x Osc) stay no target: the synth is untouched
+    const auto before = song.channels[0].synth;
+    CHECK (! song.setTargetValue ({ AutomationTargetKind::channelParam, 0, 0, 5 }, 0.9f));
+    CHECK (song.channels[0].synth == before);
+    // another instrument's channel: not a synth target
+    song.channels[0].instrument = InstrumentKind::sampler;
+    CHECK (! song.setTargetValue (cutoff, 0.2f) || song.channels[0].synth == before);
+    song.channels[0].instrument = InstrumentKind::synth;
+    // a modulator may drive them (LYNX's own controls)
+    CHECK (Modulator::canModulate (song, cutoff));
+    // Last tweaked: the page's units
+    CHECK (lasttweak::valueText (song, cutoff, 0.5f) == "1.0 kHz");
+    CHECK (lasttweak::valueText (song, { AutomationTargetKind::channelParam, 0, 0, synthparam::target (synthparam::attack) }, 0.5f) == "0.500 s");
+    CHECK (lasttweak::valueText (song, octave, 1.0f) == "+3 oct");
+}
+
+TEST (u190he_link_a_midi_link_moves_the_synth_and_its_sound)
+{
+    using namespace lynx;
+    // the engine's link path for LYNX's own controls (AudioEngine::applyMidiLinks -> applyTargetValues): the
+    // controller's position through the link's mode and formula, then Song::setTargetValue; the instrument takes the
+    // Song's settings (configure) as for a knob turned by hand
+    auto song = Song::makeDefault();
+    auto& c = song.channels[0];
+    c.instrument = InstrumentKind::synth;
+    c.synth.osc[0] = { Waveform::saw, 0, 0.0f, 1.0f };
+    c.synth.cutoff = 20000.0f;
+    const AutomationTarget level { AutomationTargetKind::channelParam, 0, 0, synthparam::target (synthparam::oscParam (0, synthparam::level)) };
+    static const linkformula::Formula oneToOne;
+    auto energyOf = [] (Instrument& synth) {
+        synth.allNotesOff();
+        synth.noteOn (60, 0.8f);
+        std::vector<float> l (4096), r (4096);
+        synth.render (l.data(), r.data(), (int) l.size(), 0.7f, 0.7f);
+        double e = 0.0;
+        for (std::size_t i = 1024; i < l.size(); ++i)
+            e += (double) l[i] * l[i];
+        return e;
+    };
+    auto synth = makeInstrument (InstrumentKind::synth);
+    synth->prepare (48000.0);
+    synth->configure (c, 120.0);
+    const auto open = energyOf (*synth);
+
+    // CC value 0 on a plain link: the level knob goes to 0 and the synth is silent
+    const auto v = linkformula::linkModeValue ((int) linkformula::LinkMode::standard, oneToOne, 0.0f / 127.0f, song.targetValue (level));
+    CHECK (v.has_value());
+    CHECK (song.setTargetValue (level, *v));
+    CHECK (song.channels[0].synth.osc[0].level == 0.0f);
+    CHECK (std::abs (song.targetValue (level)) < 1.0e-6f); // (what the knob shows: SynthPanel refreshes from the Song)
+    synth->configure (song.channels[0], 120.0);
+    const auto linked = energyOf (*synth);
+    CHECK (open > 1.0);
+    CHECK (linked < open * 1.0e-6);
+}
+
+TEST (u190he_link_synth_automation_plays_and_a_hand_move_takes_over)
+{
+    using namespace lynx;
+    ChannelDef c;
+    c.instrument = InstrumentKind::synth;
+    c.synth.osc[0] = { Waveform::saw, 0, 0.0f, 1.0f };
+    c.synth.cutoff = 20000.0f;
+    auto energyOf = [] (Instrument& synth) {
+        synth.allNotesOff();
+        synth.noteOn (60, 0.8f);
+        std::vector<float> l (4096), r (4096);
+        synth.render (l.data(), r.data(), (int) l.size(), 0.7f, 0.7f);
+        double e = 0.0;
+        for (std::size_t i = 1024; i < l.size(); ++i)
+            e += (double) l[i] * l[i];
+        return e;
+    };
+    auto synth = makeInstrument (InstrumentKind::synth);
+    synth->prepare (48000.0);
+    synth->configure (c, 120.0);
+    const auto open = energyOf (*synth);
+
+    const auto level = synthparam::target (synthparam::oscParam (0, synthparam::level));
+    synth->setParameter (level, 0.0f); // an automation clip at its bottom (audio thread)
+    const auto automated = energyOf (*synth);
+    synth->configure (c, 120.0); // the song re-applied unchanged (an edit elsewhere): the automation keeps its value
+    const auto kept = energyOf (*synth);
+    synth->setParameter (5, 0.0f); // (not one of the synth's numbers: ignored)
+    const auto ignored = energyOf (*synth);
+    c.synth.osc[0].level = 0.5f; // the knob turned by hand (or a linked controller)
+    synth->configure (c, 120.0);
+    const auto byHand = energyOf (*synth);
+
+    CHECK (open > 1.0);
+    CHECK (automated < open * 1.0e-6);
+    CHECK (kept < open * 1.0e-6);
+    CHECK (ignored < open * 1.0e-6);
+    CHECK (byHand > open * 0.1 && byHand < open * 0.5);
+
+    // an automation clip on a synth knob binds to the instrument (RackRenderer), as the Sampler's do
+    auto song = Song::makeDefault();
+    song.channels[0].instrument = InstrumentKind::synth;
+    auto& p = song.patterns[(std::size_t) song.currentPattern];
+    PatternEvents lane;
+    lane.target = { AutomationTargetKind::channelParam, 0, 0, synthparam::target (synthparam::cutoff) };
+    lane.events.push_back ({ 0, 0.5f });
+    p.events.push_back (lane);
+    auto state = RackState::build (song, nullptr, 48000.0);
+    auto bound = false;
+    for (const auto& b : state->pluginParams)
+        bound = bound || (b.channel == 0 && b.param == synthparam::target (synthparam::cutoff));
+    CHECK (bound);
+    // ... and an FL plugin number on a synth channel does not
+    PatternEvents dead;
+    dead.target = { AutomationTargetKind::channelParam, 0, 0, 5 };
+    dead.events.push_back ({ 0, 0.5f });
+    p.events = { dead };
+    state = RackState::build (song, nullptr, 48000.0);
+    auto deadBound = false;
+    for (const auto& b : state->pluginParams)
+        deadBound = deadBound || (b.channel == 0 && b.param == 5);
+    CHECK (! deadBound);
+}
+
+TEST (u190he_link_soundfont_knobs_take_a_linked_controller)
+{
+    using namespace lynx;
+    auto song = Song::makeDefault();
+    song.channels[0] = sf::makeChannel();
+    const AutomationTarget attack { AutomationTargetKind::channelParam, 0, 0, sf::pAttack };
+    CHECK (sf::automatable (sf::pAttack) && sf::automatable (sf::pVolume) && sf::automatable (sf::pModWheel));
+    CHECK (! sf::automatable (sf::pBank) && ! sf::automatable (sf::pProgram) && ! sf::automatable (sf::pReverbTrack)
+           && ! sf::automatable (sf::pChorusTrack) && ! sf::automatable (sf::paramCount) && ! sf::automatable (-1));
+    // before: no case in Song::setTargetValue, so AudioEngine::applyTargetValues found no plugin and nothing moved
+    CHECK (song.setTargetValue (attack, 0.5f));
+    const auto params = sf::readParams (song.channels[0].modelParams);
+    CHECK (std::abs (params[(std::size_t) sf::pAttack] - sf::denormalise (sf::pAttack, 0.5f)) < 1.0e-6f);
+    CHECK (std::abs (song.targetValue (attack) - 0.5f) < 1.0e-4f);
+    // integer parameters land on whole numbers; the bank / program stay out of reach
+    CHECK (song.setTargetValue ({ AutomationTargetKind::channelParam, 0, 0, sf::pTranspose }, 0.73f));
+    const auto t = sf::readParams (song.channels[0].modelParams)[(std::size_t) sf::pTranspose];
+    CHECK (t == std::round (t) || ! sf::paramInfo (sf::pTranspose).integer);
+    const auto bankBefore = sf::readParams (song.channels[0].modelParams)[(std::size_t) sf::pBank];
+    CHECK (! song.setTargetValue ({ AutomationTargetKind::channelParam, 0, 0, sf::pBank }, 1.0f));
+    CHECK (sf::readParams (song.channels[0].modelParams)[(std::size_t) sf::pBank] == bankBefore);
+}
+
+TEST (u190he_link_fl_engine_knob_reads_what_a_link_wrote)
+{
+    using namespace lynx;
+    // the FL engines' page (ModelPanel) shows modelparams::rawValue of the channel's state; a linked controller writes
+    // it through Song::setTargetValue -> modelparams::setValue01, and the page now reads it again on the engine's change
+    // message (it read the state only when it opened: the sound changed, the knob did not)
+    auto song = Song::makeDefault();
+    song.channels[0].instrument = InstrumentKind::poizone;
+    song.channels[0].pluginState = poizoneDefaultState();
+    const auto* p = modelparams::find (InstrumentKind::poizone, 18);
+    CHECK (p != nullptr);
+    if (p == nullptr)
+        return;
+    const auto stateBefore = song.channels[0].pluginState;
+    const AutomationTarget t { AutomationTargetKind::channelParam, 0, 0, 18 };
+    CHECK (song.setTargetValue (t, 0.3f));
+    CHECK (song.channels[0].pluginState != stateBefore); // (the page's change test: the state differs)
+    const auto raw = modelparams::rawValue (song.channels[0], 18);
+    CHECK (raw.has_value() && std::abs (*raw - p->raw (0.3f)) < 1.0e-6f);
+    // an automation's 0…1 value shown on the knob is the same raw value (ModelPanel::showAutomation: p->raw (v))
+    CHECK (std::abs (p->normalised (p->raw (0.3f)) - 0.3f) < 1.0e-5f);
+}

+ 1 - 0
tests/tests.cpp

@@ -17790,3 +17790,4 @@ TEST (fl_low_lifter_matches_fl_engine)
 #include "ApexMeter190gpTests.inc"
 #include "Toolchain190gtTests.inc"
 #include "Rollup028Tests.inc" // round 190fv: semantic interactions of the 0.1.28 rollup
+#include "U190heLinkTests.inc" // round 190he (issue #152 item 2: linkable LYNX instruments, knobs that follow links)