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

Toxic Biohazard: FL's filter envelope; Kepler: LFO1 BPM sync (REA round 33)

Toxic: exponential stages chosen per 128-sample block, attack towards 5 clipped at 1,
elapsed time in 1/44100 s; within 1.6e-8 of the DLL's envelope at 44.1/48/96 kHz.
Kepler: synced LFO1 period n/192 beats from the stored rate knob.
Kauan 1 месяц назад
Родитель
Сommit
624f8a0cd5
6 измененных файлов с 127 добавлено и 19 удалено
  1. 10 5
      docs/STATUS.md
  2. 13 4
      src/core/FlGenerators.cpp
  3. 39 3
      src/core/Instruments.cpp
  4. 4 0
      src/core/Song.h
  5. 19 7
      src/engine/ProjectFile.cpp
  6. 42 0
      tests/tests.cpp

+ 10 - 5
docs/STATUS.md

@@ -94,8 +94,10 @@ accordingly. Highlights of what is **confirmed and applied**:
 - **MiniSynth:** LFO periods, squared LFO depth, release, second voice in whole semitones + 7.5 cents.
 - **Toxic Biohazard:** FM 2·cell (matrix direction read from the code), filter modes (p300), cutoff `2^(9u + 5)`,
   key-track 0.09·KT oct/semitone, envelope ±9·EGAmt oct; FL's 4-pole ladder (table, resonance scale R(u), input
-  saturation, LP/BP/HP outputs).
-- **Kepler:** LFO1 rate, vibrato and cutoff depth (peak), delay; Chorus I / II / I+II (FL's chain).
+  saturation, LP/BP/HP outputs); FL's filter envelope (time law `max(50x⁴, 0.00101)` s, exponential stages chosen
+  per 128-sample block, within 1.6·10⁻⁸ of the DLL at 44.1/48/96 kHz, REA round 33).
+- **Kepler:** LFO1 rate, vibrato and cutoff depth (peak), delay, BPM sync (period n/192 beats, REA round 33);
+  Chorus I / II / I+II (FL's chain); chorus noise and gate are off for every state in FL's data (as LYNX).
   **BassDrum:** Peak `1 + 100x³·s⁴`.
   **Sakura:** ADSR, Tune, Mix, Hi-Cut. **BooBass:** 3-band EQ. **Drumpad:** Pitch and Mallet Amplitude.
 - **Morphine:** average spectrum of the generators on the harmonics oscillator.
@@ -132,9 +134,12 @@ accordingly. Highlights of what is **confirmed and applied**:
 - BooBass: needs the exact BooBass_Eng_x64.dll build (SHA-256 checked; otherwise the saw approximation); keys below
   36 are clamped (FL reads outside its table there).
 - Toxic: the 4-pole ladder is FL's (REA round 26) with fc kept in Hz at any rate (FL's table is built for 44.1 kHz);
-  linear filter envelope.
-- Kepler: the chorus is FL's (REA round 26, within −75 dB of the DLL) without its optional noise/gate and with the
-  0.5 Hz pre-filter high-pass; "Range" ×10 (Exo only); LFO BPM sync.
+  the filter envelope moves the cutoff on LYNX's 16-sample control tick (FL: every sample, quantised to 9/65535 oct);
+  the re-trigger fade of a reused voice is left out.
+- Kepler: the chorus is FL's (REA round 26, within −75 dB of the DLL) with the 0.5 Hz pre-filter high-pass; "Range"
+  ×10 (Exo only); a synced LFO1 is locked to the song position at note start (FL runs it from the host's beat delta);
+  old (version < 4) synced states use the stored rate, as FL's engine right after loading (whether FL re-sends the
+  converted rate is not confirmed).
 - Harmless: the filter as a per-partial curve (LYNX uses a biquad; FL's law is in REA round 27 §1.3); p39 outside
   ±1200/±2400; resonance shapes.
 - Ogun: the evolving spectrum is 8 snapshots per note with linear crossfades; partials binned onto one harmonic

+ 13 - 4
src/core/FlGenerators.cpp

@@ -971,8 +971,9 @@ bool synthFromKepler (const std::vector<std::uint8_t>& state, SynthSettings& syn
     }
     // LFO1 (REA_rodada17 §6, rodada18 §7, rodada19 §3): a single, nearly sinusoidal global LFO, value ±1;
     // rate 21.739·2^(−T(x)) Hz; vibrato ±A(k10) octaves and cutoff ±A_c(k23) octaves (peaks); delay = linear
-    // ramp of D(k7) s. The ×10 "Range" only exists on the Kepler Exo panel (left off here); BPM Sync
-    // (k6) was not read. Chorus (panel 30): mode trunc(3x + 0.5) = Off / I / II / I+II, FL's chain (REA_rodada26 §3).
+    // ramp of D(k7) s. The ×10 "Range" only exists on the Kepler Exo panel (left off here). BPM Sync (k6 ≥ 0.5,
+    // REA_rodada33 §3.4): period n/192 beats, n = trunc((2^(7(1 − x)) − 1)·48.370079 + 1.1) from the rate knob as
+    // stored (what the engine uses right after loading, also for old states). Chorus (panel 30): mode trunc(3x + 0.5) = Off / I / II / I+II, FL's chain (REA_rodada26 §3).
     static constexpr double lfoRateTable[11] = { 7.4422, 5.8074, 4.3499, 3.6563, 3.1131, 2.6712, 2.0463, 1.3093, 0.8515, 0.2838, 0.0 };
     static constexpr double vibratoTable[11] = { 0, 0.00116, 0.00813, 0.01506, 0.02196, 0.02882, 0.05259, 0.07819, 0.12057, 0.17728, 0.21388 };
     static constexpr double lfoCutoffTable[11] = { 0, 0.0914, 0.38038, 0.662, 0.96846, 1.23732, 1.48249, 1.85735, 2.27554, 2.66441, 2.82 };
@@ -980,6 +981,14 @@ bool synthFromKepler (const std::vector<std::uint8_t>& state, SynthSettings& syn
     ChannelLfo lfo;
     s.chorus = std::clamp ((int) (3.0f * k[30] + 0.5f), 0, 3);
     lfo.period = (float) (1.0 / (21.739 * std::exp2 (-table (lfoRateTable, k[5]))));
+    if (k[6] >= 0.5f)
+    {
+        const auto curve = std::exp2 ((1.0f - k[5]) * 7.0f) - 1.0f; // float32 steps, as the engine
+        const auto n = (int) (curve * 48.370079f + 1.1f);
+        lfo.sync = true;
+        lfo.period = (float) std::max (1, n) / 192.0f;
+        lfo.secondsTiming = true;
+    }
     lfo.attack = (float) table (lfoDelayTable, k[7]);
     lfo.global = true;
     if (const auto oct = table (vibratoTable, k[10]); oct > 1e-4)
@@ -1279,8 +1288,8 @@ bool synthFromToxic (const std::vector<std::uint8_t>& state, SynthSettings& synt
         const auto eg = 2.0 * p[23] - 1.0;
         const auto egAmount = 9.0 * (eg < 0.0 ? -eg * eg : eg * eg);
         if (std::abs (egAmount) > 0.01)
-            // FL's exponential pole gets close to the target in ~half the time T; LYNX's envelope is linear
-            s.filterEnv = { (float) egAmount, seconds (p[19]), 0.0f, 0.5f * seconds (p[20]), p[21] * p[21], 0.5f * seconds (p[22]) };
+            // FL's own envelope law (REA_rodada33 §2): T = max(50x⁴, 0.00101) s, sustain x², exponential stages
+            s.filterEnv = { (float) egAmount, seconds (p[19]), 0.0f, seconds (p[20]), p[21] * p[21], seconds (p[22]), 1 };
     }
     gain = 1.0f;
     synth = s;

+ 39 - 3
src/core/Instruments.cpp

@@ -247,12 +247,17 @@ struct ModState
     float level = 0.0f;
     float releaseStep = 0.0f; // fixed on note release: from the current level down to 0
     int stage = 0, held = 0;
+    // Toxic shape: samples left in the current 128-sample block, elapsed time and the block's target/coefficient
+    int blockLeft = 0;
+    double elapsed = 0.0;
+    float target = 0.0f, coefficient = 0.0f;
 };
 
 struct ModCoefficients
 {
-    bool active = false;
+    bool active = false, toxic = false;
     float amount = 0.0f, attackStep = 1.0f, decayStep = 1.0f, releasePerLevel = 1.0f, sustain = 0.0f;
+    float toxicAttack = 0.0f, toxicDecay = 0.0f, toxicRelease = 0.0f; // per-sample coefficients
     int holdSamples = 0;
 
     static ModCoefficients make (const ModEnvelope& e, double sampleRate) noexcept
@@ -260,6 +265,18 @@ struct ModCoefficients
         ModCoefficients c;
         c.active = e.amount != 0.0f;
         c.amount = e.amount;
+        if (e.shape == 1)
+        {
+            // REA_rodada33 §2.1–2.2: times in seconds (floor 0.001), sustain level as is
+            auto seconds = [] (float t) { return std::max (t, 0.001f); };
+            c.toxic = true;
+            c.attackStep = seconds (e.attack); // the attack stage lasts while elapsed < A
+            c.sustain = e.sustain;
+            c.toxicAttack = (float) (8.82 / ((double) seconds (e.attack) * 39.441975 * sampleRate));
+            c.toxicDecay = (float) (8.82 / ((double) seconds (e.decay) * sampleRate));
+            c.toxicRelease = (float) (8.82 / ((double) seconds (e.release) * sampleRate));
+            return c;
+        }
         c.attackStep = (float) (1.0 / (std::max (0.0005, (double) e.attack) * sampleRate));
         c.holdSamples = (int) (std::max (0.0f, e.hold) * sampleRate);
         c.sustain = std::clamp (e.sustain, 0.0f, 1.0f);
@@ -270,6 +287,24 @@ struct ModCoefficients
 
     void advance (ModState& m) const noexcept
     {
+        if (toxic)
+        {
+            // stage chosen at each block start (released → 0, attack → 5 while elapsed < A, else decay → S + 1e-7);
+            // one-pole step every sample, clipped at 1
+            if (--m.blockLeft < 0)
+            {
+                m.blockLeft = 127;
+                if (m.stage == 4)
+                    m.target = 0.0f, m.coefficient = toxicRelease;
+                else if ((double) attackStep <= m.elapsed)
+                    m.target = sustain + 1.0e-7f, m.coefficient = toxicDecay;
+                else
+                    m.target = 5.0f, m.coefficient = toxicAttack;
+                m.elapsed += 128.0 / 44100.0;
+            }
+            m.level = std::min (1.0f, (m.target - m.level) * m.coefficient + m.level);
+            return;
+        }
         switch (m.stage)
         {
             case 0:
@@ -321,8 +356,9 @@ struct LfoCoefficients
         c.amount = l.amount;
         const auto unit = l.sync ? 60.0 / std::max (1.0, bpm) : 1.0; // beats -> seconds
         c.increment = 1.0 / (std::max (0.005, (double) l.period * unit) * sampleRate);
-        c.delaySamples = std::max (0.0, (double) l.delay * unit * sampleRate);
-        c.fadeStep = l.attack > 0.0f ? (float) (1.0 / ((double) l.attack * unit * sampleRate)) : 1.0f;
+        const auto timeUnit = l.secondsTiming ? 1.0 : unit;
+        c.delaySamples = std::max (0.0, (double) l.delay * timeUnit * sampleRate);
+        c.fadeStep = l.attack > 0.0f ? (float) (1.0 / ((double) l.attack * timeUnit * sampleRate)) : 1.0f;
         c.shape = std::clamp (l.shape, 0, 2);
         c.global = l.global;
         // period in beats (synced) or in seconds converted via BPM

+ 4 - 0
src/core/Song.h

@@ -80,6 +80,9 @@ struct ModEnvelope
     float decay = 0.3f;
     float sustain = 0.0f; // 0..1
     float release = 0.2f;
+    // 0 = LYNX's linear AHDSR; 1 = Toxic Biohazard's filter envelope (REA_rodada33 §2: exponential stages chosen per
+    // 128-sample block, attack towards 5 clipped at 1, elapsed time counted in 1/44100 s)
+    int shape = 0;
 
     bool operator== (const ModEnvelope&) const = default;
 };
@@ -95,6 +98,7 @@ struct ChannelLfo
     float delay = 0.0f;   // seconds (or beats with `sync`) before starting
     float attack = 0.0f;  // fade-in, in the same unit
     bool global = false;  // phase locked to the song position (FL's "Global" button) instead of restarting per note
+    bool secondsTiming = false; // delay and fade-in stay in seconds even with `sync` (Kepler's LFO1)
 
     bool operator== (const ChannelLfo&) const = default;
 };

+ 19 - 7
src/engine/ProjectFile.cpp

@@ -9,22 +9,28 @@ namespace lynx::ProjectFile
 {
 namespace
 {
-// cutoff/pitch envelopes: [amount, attack, hold, decay, sustain, release]; only the active ones
+// cutoff/pitch envelopes: [amount, attack, hold, decay, sustain, release(, shape)]; only the active ones
 void writeEnvelopes (juce::DynamicObject& o, const ModEnvelope& filter, const ModEnvelope& pitch)
 {
     for (auto [name, env] : { std::pair { "filterEnv", &filter }, { "pitchEnv", &pitch } })
         if (env->amount != 0.0f)
-            o.setProperty (name, juce::Array<juce::var> { env->amount, env->attack, env->hold, env->decay, env->sustain, env->release });
+        {
+            juce::Array<juce::var> values { env->amount, env->attack, env->hold, env->decay, env->sustain, env->release };
+            if (env->shape != 0)
+                values.add (env->shape);
+            o.setProperty (name, values);
+        }
 }
 
 void readEnvelopes (const juce::var& o, ModEnvelope& filter, ModEnvelope& pitch)
 {
     for (auto [name, env, limit] : { std::tuple { "filterEnv", &filter, 10.0f }, { "pitchEnv", &pitch, 48.0f } })
-        if (const auto& e = o[name]; e.isArray() && e.size() == 6)
+        if (const auto& e = o[name]; e.isArray() && (e.size() == 6 || e.size() == 7))
         {
-            auto time = [&] (int i) { return juce::jlimit (0.0f, 20.0f, (float) (double) e[i]); };
+            const auto shape = e.size() == 7 ? juce::jlimit (0, 1, (int) e[6]) : 0;
+            auto time = [&] (int i) { return juce::jlimit (0.0f, shape == 1 ? 50.0f : 20.0f, (float) (double) e[i]); };
             *env = { juce::jlimit (-limit, limit, (float) (double) e[0]), time (1), time (2), time (3),
-                     juce::jlimit (0.0f, 1.0f, (float) (double) e[4]), time (5) };
+                     juce::jlimit (0.0f, 1.0f, (float) (double) e[4]), time (5), shape };
         }
 }
 
@@ -35,7 +41,12 @@ void writeLfos (juce::DynamicObject& o, const std::array<ChannelLfo, lfoTargets>
 
     for (int i = 0; i < lfoTargets; ++i)
         if (const auto& l = lfo[(std::size_t) i]; l.amount != 0.0f)
-            list.add (juce::Array<juce::var> { i, l.amount, l.period, l.sync, l.shape, l.delay, l.attack, l.global });
+        {
+            juce::Array<juce::var> values { i, l.amount, l.period, l.sync, l.shape, l.delay, l.attack, l.global };
+            if (l.secondsTiming)
+                values.add (true);
+            list.add (values);
+        }
 
     if (! list.isEmpty())
         o.setProperty ("lfo", list);
@@ -49,7 +60,8 @@ void readLfos (const juce::var& o, std::array<ChannelLfo, lfoTargets>& lfo)
                 if (const auto i = (int) e[0]; i >= 0 && i < lfoTargets)
                     lfo[(std::size_t) i] = { juce::jlimit (-48.0f, 48.0f, (float) (double) e[1]), juce::jlimit (0.005f, 64.0f, (float) (double) e[2]),
                                              (bool) e[3], juce::jlimit (0, 2, (int) e[4]), juce::jlimit (0.0f, 64.0f, (float) (double) e[5]),
-                                             juce::jlimit (0.0f, 64.0f, (float) (double) e[6]), e.size() >= 8 && (bool) e[7] };
+                                             juce::jlimit (0.0f, 64.0f, (float) (double) e[6]), e.size() >= 8 && (bool) e[7],
+                                             e.size() >= 9 && (bool) e[8] };
 }
 
 const char* instrumentId (InstrumentKind k)

+ 42 - 0
tests/tests.cpp

@@ -1853,6 +1853,20 @@ TEST (synth_pitch_and_filter_envelopes)
         return e;
     };
     CHECK (hf (0, 2400) > 4.0 * hf (40000, 42400));
+
+    // Toxic Biohazard's envelope (REA_rodada33 §2): exponential towards 5 clipped at 1, so the cutoff is still near
+    // its base early in the attack, fully open at A = 0.2 s and back down after the exponential decay
+    lynx::SynthSettings toxic = pluck;
+    toxic.filterEnv = { 5.0f, 0.2f, 0.0f, 0.1f, 0.0f, 0.1f, 1 };
+    const auto t = render (toxic, 48000);
+    auto thf = [&] (int from, int to) {
+        double e = 0.0;
+        for (int i = from + 1; i < to; ++i)
+            e += std::pow ((double) t[(std::size_t) i] - t[(std::size_t) i - 1], 2.0);
+        return e;
+    };
+    CHECK (thf (9000, 9600) > 4.0 * thf (300, 900));
+    CHECK (thf (9000, 9600) > 4.0 * thf (40000, 40600));
 }
 
 TEST (synth_mono_reuses_the_sounding_voice)
@@ -5439,3 +5453,31 @@ TEST (drumsynth_live_engine)
     const auto half = std::get<0> (render (tone, { 60, 0 }, 4096, 0.5f, 0.5f));
     CHECK (std::abs (half[100] - 0.25f * full[100]) < 1e-6f && std::abs (full[100]) > 0.01f);
 }
+
+TEST (kepler_lfo_bpm_sync)
+{
+    // REA_rodada33 §3.4: with BPM Sync (k6 ≥ 0.5) the LFO1 period is n/192 beats, n = trunc((2^(7(1 − x)) − 1)·48.370079 + 1.1)
+    std::vector<std::uint8_t> state (0x21 + 34 * 4, 0);
+    auto putU32 = [&] (std::size_t o, std::uint32_t v) { std::memcpy (state.data() + o, &v, 4); };
+    putU32 (12, 9);
+    std::memcpy (state.data() + 16, "Kepler-1", 9);
+    putU32 (0x19, 4);
+    putU32 (0x1D, 34);
+    float k[34] {};
+    k[5] = 0.602085769f; // "Pads - Kepler" demo: n = 286
+    k[6] = 1.0f;
+    k[10] = 1.0f; // vibrato on
+    k[33] = 1.0f;
+    std::memcpy (state.data() + 0x21, k, sizeof k);
+    lynx::SynthSettings s;
+    float gain = 0.0f;
+    CHECK (lynx::synthFromKepler (state, s, gain));
+    CHECK (s.lfo[lynx::lfoPitch].sync && s.lfo[lynx::lfoPitch].secondsTiming);
+    CHECK (std::abs (s.lfo[lynx::lfoPitch].period - 286.0f / 192.0f) < 1e-6f);
+    k[5] = 0.0f; // slowest: 32 beats
+    std::memcpy (state.data() + 0x21, k, sizeof k);
+    CHECK (lynx::synthFromKepler (state, s, gain) && std::abs (s.lfo[lynx::lfoPitch].period - 32.0f) < 1e-6f);
+    k[6] = 0.0f; // free rate in seconds
+    std::memcpy (state.data() + 0x21, k, sizeof k);
+    CHECK (lynx::synthFromKepler (state, s, gain) && ! s.lfo[lynx::lfoPitch].sync);
+}