ソースを参照

Fruity Scratcher: DVS timecode input (Serato Mk2 A/B, Serato CD, Traktor Mk1 A/B) and RIAA preamp as FL decodes them

Ka1-S 1 ヶ月 前
親
コミット
48edd1001f

+ 1 - 0
CMakeLists.txt

@@ -95,6 +95,7 @@ add_library(lynx_core STATIC
     src/core/VisualFx.cpp
     src/core/FlExtraFx.cpp
     src/core/Scratcher.cpp
+    src/core/Dvs.cpp
     src/core/Transmitter.cpp
     src/core/VideoPlayer.cpp
     src/core/FruityDance.cpp

ファイルの差分が大きいため隠しています
+ 0 - 0
scripts/build-local.sh


+ 1 - 1
scripts/build-windows-zig.sh

@@ -172,7 +172,7 @@ APP_SOURCES+=(src/core/UpdateCheck.cpp src/ui/Updater.cpp)
 # visual plugins: Wave Candy, Visualizer, video export (round 164c)
 APP_SOURCES+=(src/core/VisualFx.cpp src/ui/VisualEditors.cpp)
 # FL plugins LYNX lacked: Mute 2, Big Clock, Tuner, HTML NoteBook (round 165g)
-APP_SOURCES+=(src/core/FlExtraFx.cpp src/core/Scratcher.cpp src/core/Transmitter.cpp src/ui/FlExtraEditors.cpp src/core/VideoPlayer.cpp src/ui/VideoPlayerView.cpp)
+APP_SOURCES+=(src/core/FlExtraFx.cpp src/core/Scratcher.cpp src/core/Dvs.cpp src/core/Transmitter.cpp src/ui/FlExtraEditors.cpp src/core/VideoPlayer.cpp src/ui/VideoPlayerView.cpp)
 APP_SOURCES+=(src/core/VisualFx.cpp src/core/FruityDance.cpp src/ui/VisualEditors.cpp src/ui/VisualGl.cpp src/ui/DanceView.cpp)
 
 echo "WavPack (decoder, C)..."

+ 262 - 0
src/core/Dvs.cpp

@@ -0,0 +1,262 @@
+#include "core/Dvs.h"
+
+#include <algorithm>
+#include <chrono>
+#include <cmath>
+#include <thread>
+
+namespace lynx::dvs
+{
+namespace
+{
+// [DECOMP engine_x64 0x1800037c0] the five formats of FL's sine-and-amplitude decoder, by the state's timecode index
+const Format seratoA { 20, 0xa5411u, 0xa786cu, 710000u, 1000, 25.7, 0x00 };
+const Format seratoB { 20, 0xd76c6u, 0x9b0f2u, 940000u, 1000, 8.5, 0x00 };
+const Format seratoCd { 20, 0x30aefu, 0x34d54u, 982000u, 1000, 0.0, 0x00 };
+const Format traktorA { 23, 0x759c41u, 0x410410u, 1500001u, 2000, 0.0, 0x70 };
+const Format traktorB { 23, 0x53e4f7u, 0x410410u, 2109993u, 2000, 0.0, 0x70 };
+
+int parity (std::uint32_t x) noexcept
+{
+    int p = 0;
+    for (; x != 0; x >>= 1)
+        p ^= (int) (x & 1u);
+    return p;
+}
+
+// The position table: (register << 32 | cycle) sorted by register
+struct Table
+{
+    std::vector<std::uint64_t> entries;
+    bool find (std::uint32_t state, std::uint32_t& cycle) const noexcept
+    {
+        const auto key = (std::uint64_t) state << 32;
+        const auto it = std::lower_bound (entries.begin(), entries.end(), key);
+        if (it == entries.end() || (*it >> 32) != state)
+            return false;
+        cycle = (std::uint32_t) *it;
+        return true;
+    }
+};
+
+constexpr int slots = 8;
+std::atomic<const Table*> tables[slots] {};
+std::atomic<bool> building[slots] {};
+
+const Table* tableOf (int state) noexcept
+{
+    return state >= 0 && state < slots ? tables[state].load (std::memory_order_acquire) : nullptr;
+}
+
+void build (int state)
+{
+    const auto* f = format (state);
+    auto* t = new Table();
+    t->entries.reserve (f->length);
+    auto s = f->seed;
+    for (std::uint32_t i = 0; i < f->length; ++i)
+    {
+        t->entries.push_back ((std::uint64_t) s << 32 | i);
+        s = nextState (s, *f);
+    }
+    std::sort (t->entries.begin(), t->entries.end());
+    tables[state].store (t, std::memory_order_release); // kept for the program's life (shared by every decoder)
+}
+} // namespace
+
+int stateOfCombo (int combo) noexcept
+{
+    // [DECOMP Fruity Scratcher 0x733fe0] Off → −1, Timecode CD (the last entry) → 0, the others keep their index
+    if (combo <= 0 || combo >= comboCount)
+        return -1;
+    return combo == comboCount - 1 ? 0 : combo;
+}
+
+int comboOfState (int state) noexcept
+{
+    if (state == 0)
+        return comboCount - 1;
+    return state >= 1 && state < comboCount - 1 ? state : 0;
+}
+
+const char* comboName (int combo) noexcept
+{
+    static const char* const names[comboCount] = { "Off",
+                                                    "Serato Mk2, Side A",
+                                                    "Serato Mk2, Side B",
+                                                    "Serato CD",
+                                                    "Traktor Skratch Mk1, Side A",
+                                                    "Traktor Skratch Mk1, Side B",
+                                                    "Traktor Skratch Mk2, Side A",
+                                                    "Traktor Skratch Mk2, Side B",
+                                                    "Timecode CD" };
+    return combo >= 0 && combo < comboCount ? names[combo] : names[0];
+}
+
+const Format* format (int state) noexcept
+{
+    // [DECOMP engine_x64 createTimecodeDecoder 0x180001080]: 1 → Serato A (id 1), 2 → Serato B (id 0), 3 → Serato
+    // CD (id 2), 4 / 5 → Traktor Mk1 A / B (ids 3 / 4); 6 / 7 = the Traktor Mk2 decoder, 0 = the CD decoder
+    switch (state)
+    {
+        case 1: return &seratoA;
+        case 2: return &seratoB;
+        case 3: return &seratoCd;
+        case 4: return &traktorA;
+        case 5: return &traktorB;
+        default: return nullptr;
+    }
+}
+
+bool supported (int state) noexcept
+{
+    return format (state) != nullptr;
+}
+
+std::uint32_t nextState (std::uint32_t state, const Format& f) noexcept
+{
+    const auto mask = (1u << f.bits) - 1u;
+    return ((state << 1) & mask) | (std::uint32_t) parity (state & f.taps);
+}
+
+void warm (int state, bool wait)
+{
+    if (format (state) == nullptr)
+        return;
+    if (tableOf (state) == nullptr && ! building[state].exchange (true))
+    {
+        if (wait)
+            build (state);
+        else
+            std::thread ([state] { build (state); }).detach();
+    }
+    if (wait)
+        while (tableOf (state) == nullptr)
+            std::this_thread::sleep_for (std::chrono::milliseconds (2));
+}
+
+// ------------------------------------------------------------------------------------------------------- decoder
+void Decoder::Crossing::feed (float x, float k) noexcept
+{
+    // [DECOMP 0x180003c10] a zero crossing with 0.004 of hysteresis around a fast-moving zero (the signal's own
+    // low frequencies: rumble and DC follow the record)
+    crossed = false;
+    if (! positive)
+    {
+        if (x > zero + 0.004f)
+            positive = crossed = true;
+    }
+    else if (x < zero - 0.004f)
+    {
+        positive = false;
+        crossed = true;
+    }
+    zero += (x - zero) * k;
+}
+
+void Decoder::configure (int state, double sampleRate) noexcept
+{
+    fmt = format (state);
+    stateIndex = state;
+    rate = sampleRate > 0.0 ? sampleRate : 44100.0;
+    dt = 1.0 / rate;
+    // [DECOMP 0x1800037c0] the zero's time constant: 1/900 s
+    zeroK = (float) (1.0 - std::exp (-1.0 / (rate / 900.0)));
+    reset();
+}
+
+void Decoder::reset() noexcept
+{
+    primary = {};
+    secondary = {};
+    dirForward = true;
+    reg = 0;
+    valid = 0;
+    refLevel = 1.0f;
+    filtX = velocity = 0.0;
+    pos = 0.0;
+    lockCount = 0;
+}
+
+void Decoder::shiftIn (bool bit) noexcept
+{
+    // [DECOMP 0x180003aa0] the bit enters the register at the end the needle moves towards; 25 bits in a row that
+    // follow the register's rule name a place on the record
+    const auto& f = *fmt;
+    const auto mask = (1u << f.bits) - 1u;
+    std::uint32_t predicted, next;
+    if (dirForward)
+    {
+        predicted = ((reg << 1) & mask) | (std::uint32_t) parity (reg & f.taps);
+        next = ((reg << 1) & mask) | (bit ? 1u : 0u);
+    }
+    else
+    {
+        predicted = (std::uint32_t) parity ((((f.taps << 1) & mask) | 1u) & reg) << (f.bits - 1) | reg >> 1;
+        next = (bit ? 1u : 0u) << (f.bits - 1) | reg >> 1;
+    }
+    reg = next;
+    if (predicted != next)
+    {
+        valid = 0;
+        return;
+    }
+    if (++valid <= 24)
+        return;
+    const auto* table = tableOf (stateIndex);
+    std::uint32_t cycle = 0;
+    if (table == nullptr || ! table->find (next, cycle))
+        return;
+    // [DECOMP 0x180003e80] the place the register names; the running position jumps there when it is 0.1 s away
+    const auto named = (double) cycle / (double) f.resolution - f.offset;
+    if (std::abs (named - pos) > 0.1)
+        pos = named;
+    ++lockCount;
+}
+
+void Decoder::process (float left, float right) noexcept
+{
+    if (fmt == nullptr)
+        return;
+    const auto& f = *fmt;
+    // [DECOMP 0x180003df0] the primary channel is the right one unless the format says left
+    const bool leftPrimary = (f.flags & 0x40) != 0;
+    primary.feed (leftPrimary ? left : right, zeroK);
+    secondary.feed (leftPrimary ? right : left, zeroK);
+
+    double step = 0.0;
+    if (primary.crossed || secondary.crossed)
+    {
+        // the order of the two channels' crossings gives the direction
+        bool fwd = primary.crossed ? primary.positive != secondary.positive : primary.positive == secondary.positive;
+        if ((f.flags & 0x10) != 0)
+            fwd = ! fwd;
+        if (fwd != dirForward)
+        {
+            dirForward = fwd;
+            valid = 0;
+        }
+        step = 0.25 / (double) f.resolution; // a quarter of a cycle per crossing
+    }
+    if (! dirForward)
+        step = -step;
+
+    // the speed tracker (alpha 1/512, beta 1/131072 per sample)
+    const auto predicted = dt * velocity + filtX;
+    const auto err = step - predicted;
+    filtX = (err * 0.001953121185 + predicted) - step;
+    velocity += err * 7.62937963e-06 / dt;
+
+    // a bit on each peak of the primary channel (the secondary's crossing): loud = 1, quiet = 0
+    const bool wantPositive = (f.flags & 0x20) == 0;
+    if (secondary.crossed && primary.positive == wantPositive)
+    {
+        const auto level = std::abs ((leftPrimary ? left : right) - primary.zero);
+        shiftIn (level > refLevel);
+        refLevel += (level - refLevel) * (1.0f / 48.0f);
+    }
+
+    // [DECOMP 0x180003f10] the running position follows the speed
+    pos += velocity / rate;
+}
+} // namespace lynx::dvs

+ 95 - 0
src/core/Dvs.h

@@ -0,0 +1,95 @@
+#pragma once
+
+// DVS (timecode vinyl / CD control) for the Fruity Scratcher, rebuilt clean-room from the behaviour decoded from FL
+// Studio 2026's Fruity Scratcher_x64.dll and its engine_x64.dll (docs/analise/REA_rodada167c.md §1). No Image-Line code.
+//
+// A control record (vinyl or CD) plays a "timecode": a sine carrier on both channels, the right one 90° from the
+// left, whose peaks are loud or quiet to spell a pseudo-random bit sequence (a linear feedback shift register). The
+// carrier's phase gives the speed and direction; once enough bits agree with the register's rule, the register's value
+// names the absolute place on the record. The decoder turns the input into a position on the record, in seconds,
+// which the Scratcher uses to play its sample (1 s of record = 1 s of sample, looping).
+//
+// FL's Timecode type list and the decoder each entry creates (the state's timecode index in brackets):
+//   Off [-1] · Serato Mk2, Side A [1] · Serato Mk2, Side B [2] · Serato CD [3] · Traktor Skratch Mk1, Side A [4] ·
+//   Traktor Skratch Mk1, Side B [5] · Traktor Skratch Mk2, Side A [6] · Traktor Skratch Mk2, Side B [7] ·
+//   Timecode CD [0].
+// LYNX decodes the five sine-and-amplitude formats (Serato Mk2 A / B, Serato CD, Traktor Mk1 A / B). Traktor Mk2 and
+// FL's "Timecode CD" use two other decoders in FL's engine that LYNX does not have yet: `supported()` says so and the
+// decoder then reports no signal.
+//
+// Real-time safe after warm(): the position tables (one per format, up to 2.1 M entries) are built off the audio
+// thread by warm() and shared; until a table is ready the decoder still follows speed and direction, it just cannot
+// find the absolute place yet. No JUCE here (tests/Dvs167Tests.inc).
+
+#include <atomic>
+#include <cstdint>
+#include <vector>
+
+namespace lynx::dvs
+{
+constexpr int comboCount = 9; // FL's Timecode type list, "Off" included
+
+// FL's list entry (0 = Off … 8 = Timecode CD) ↔ the state's timecode index (−1 = off)
+int stateOfCombo (int combo) noexcept;
+int comboOfState (int state) noexcept;
+const char* comboName (int combo) noexcept;
+
+struct Format
+{
+    int bits = 0;              // register length
+    std::uint32_t seed = 0;    // the register at the record's first cycle
+    std::uint32_t taps = 0;    // the feedback taps
+    std::uint32_t length = 0;  // cycles on the record
+    int resolution = 0;        // carrier frequency (cycles per second of record)
+    double offset = 0.0;       // seconds subtracted from the place the register names
+    std::uint8_t flags = 0;    // 0x10 reversed direction, 0x20 bits on the negative peaks, 0x40 left = primary
+};
+// The format of a state's timecode index; nullptr when LYNX has no decoder for it (off, Traktor Mk2, Timecode CD)
+const Format* format (int state) noexcept;
+bool supported (int state) noexcept;
+
+// The register's next value (forwards), as the record spells it
+std::uint32_t nextState (std::uint32_t state, const Format& f) noexcept;
+
+// Builds the format's position table in the background (any thread but the audio thread; returns at once). With
+// `wait` it blocks until the table is ready (tests, offline renders).
+void warm (int state, bool wait = false);
+
+class Decoder
+{
+public:
+    // configure: the state's timecode index and the sample rate (no allocation: callable on the audio thread)
+    void configure (int state, double sampleRate) noexcept;
+    void reset() noexcept;
+    // one stereo frame of the input
+    void process (float left, float right) noexcept;
+
+    bool active() const noexcept { return fmt != nullptr; }
+    double position() const noexcept { return pos; }    // seconds on the record
+    double pitch() const noexcept { return velocity; }  // record seconds per second (1 = normal speed, < 0 backwards)
+    bool forwards() const noexcept { return dirForward; }
+    bool locked() const noexcept { return lockCount > 0; } // the register named a place at least once
+
+private:
+    struct Crossing
+    {
+        bool positive = false, crossed = false;
+        float zero = 0.0f;
+        void feed (float x, float k) noexcept;
+    };
+    void shiftIn (bool bit) noexcept;
+
+    const Format* fmt = nullptr;
+    int stateIndex = -1;
+    double rate = 44100.0, dt = 1.0 / 44100.0;
+    float zeroK = 0.02f;
+    Crossing primary, secondary;
+    bool dirForward = true;
+    std::uint32_t reg = 0;
+    int valid = 0;
+    float refLevel = 1.0f;
+    double filtX = 0.0, velocity = 0.0; // the speed tracker (position error and speed)
+    double pos = 0.0;
+    int lockCount = 0;
+};
+} // namespace lynx::dvs

+ 2 - 1
src/core/Effects.cpp

@@ -2975,7 +2975,8 @@ const EffectInfo& effectInfo (EffectKind kind)
                                     { { "Wave position", d[0] }, { "Mute", d[1] }, { "Speed", d[2] }, { "Acceleration", d[3] },
                                       { "Pan", d[4] }, { "Volume", d[5] }, { "Hold", d[6] }, { "Sensitivity", d[7] },
                                       { "Transport", d[8] }, { "Smooth 1", d[9] }, { "Smooth 2", d[10] }, { "Smooth 3", d[11] },
-                                      { "Smooth 4", d[12] }, { "Rotations", d[13] } } };
+                                      { "Smooth 4", d[12] }, { "Rotations", d[13] }, { "Timecode type", 0.0f },
+                                      { "RIAA preamp", 0.0f } } };
             }();
             return info;
         }

+ 4 - 0
src/core/FlEffects.cpp

@@ -1241,8 +1241,12 @@ std::optional<EffectSlot> effectFromFl (const std::string& pluginName, const std
     {
         auto slot = makeEffectSlot (EffectKind::scratcher);
         if (const auto st = scratcher::parse (state))
+        {
             for (int i = 0; i < scratcher::count; ++i)
                 slot.params[(std::size_t) i] = st->values[(std::size_t) i];
+            slot.params[scratcher::timecodeType] = scratcher::timecodeValue (st->timecode);
+            slot.params[scratcher::riaaPreamp] = st->riaa ? 1.0f : 0.0f;
+        }
         return slot;
     }
     if (pluginName == "Transmitter") // REA_rodada165g-fltrans165: the 42 controls (raw) of FL's state 0xD5

+ 4 - 2
src/core/FlpImport.cpp

@@ -1,4 +1,6 @@
 #include "core/FlpImport.h"
+
+#include "core/Dvs.h"
 #include "core/FlExtraFx.h"
 #include "core/Scratcher.h"
 #include "core/Transmitter.h"
@@ -2766,8 +2768,8 @@ Song importFlp (const flp::Project& input, const FlpImportOptions& options)
                                 addMissingFile (options.report->missingFiles, wanted,
                                                 (insert.name.empty() ? "Insert " + std::to_string (insert.index) : insert.name) + ": " + slot.plugin);
                             }
-                            if (st->timecode != -1)
-                                ++options.report->partialEffects["Fruity Scratcher (timecode vinyl input not reproduced: plays from its own platter)"];
+                            if (st->timecode != -1 && ! dvs::supported (st->timecode))
+                                ++options.report->partialEffects["Fruity Scratcher (Traktor Mk2 / Timecode CD timecode not decoded: the platter stays silent)"];
                             else
                                 ++options.report->converted[slot.plugin];
                         }

+ 3 - 0
src/core/RackRenderer.cpp

@@ -23,6 +23,7 @@
 #include "core/DirectWave.h"
 #include "core/SoundFont.h"
 #include "core/NewtoneFx.h"
+#include "core/Dvs.h"
 #include "core/Scratcher.h"
 
 #include <algorithm>
@@ -244,6 +245,8 @@ std::shared_ptr<Effect> buildEffect (const EffectSlot& slot, double sampleRate,
         // Fruity Scratcher: its turntable's sample, loaded off the audio thread (REA_rodada165g-flscratch165)
         effect = scratcher::make (factories.samples && ! slot.samplePath.empty() ? factories.samples (slot.samplePath) : nullptr);
         effect->prepare (sampleRate);
+        // its DVS position table (REA_rodada167c §1), built here so the audio thread finds it ready
+        dvs::warm (scratcher::timecodeOf (slot.params[scratcher::timecodeType]), true);
     }
     else
     {

+ 126 - 6
src/core/Scratcher.cpp

@@ -108,8 +108,22 @@ std::array<float, count> defaults() noexcept
     return d;
 }
 
+int timecodeOf (float value) noexcept
+{
+    return dvs::stateOfCombo ((int) std::lround (std::clamp (value, 0.0f, 1.0f) * (float) (dvs::comboCount - 1)));
+}
+
+float timecodeValue (int state) noexcept
+{
+    return (float) dvs::comboOfState (state) / (float) (dvs::comboCount - 1);
+}
+
 std::string paramText (int param, float value)
 {
+    if (param == timecodeType)
+        return dvs::comboName (dvs::comboOfState (timecodeOf (value)));
+    if (param == riaaPreamp)
+        return value >= 0.5f ? "On" : "Off";
     char b[48] {};
     const auto raw = rawOfValue (param, value);
     switch (param)
@@ -189,11 +203,16 @@ std::optional<State> parse (const std::vector<std::uint8_t>& s)
         ++at; // the editor's size option
     if (*version >= 0xF462Au)
         if (const auto tc = u32At (s, at))
-            st.timecode = (std::int32_t) *tc;
+        {
+            st.timecode = std::clamp ((std::int32_t) *tc, -1, dvs::comboCount - 2);
+            if (at + 4 < s.size())
+                st.riaa = s[at + 4] != 0;
+        }
     return st;
 }
 
-std::vector<std::uint8_t> toFl (const std::array<float, count>& values, const std::string& samplePath)
+std::vector<std::uint8_t> toFl (const std::array<float, count>& values, const std::string& samplePath, int timecodeState,
+                                bool riaaOn)
 {
     std::vector<std::uint8_t> out;
     auto put = [&out] (const void* p, std::size_t n) {
@@ -219,8 +238,8 @@ std::vector<std::uint8_t> toFl (const std::array<float, count>& values, const st
     const auto len = (std::int32_t) samplePath.size();
     put (&len, 4);
     put (samplePath.data(), samplePath.size());
-    const std::uint8_t editor = 0, riaa = 0;
-    const std::int32_t timecode = -1;
+    const std::uint8_t editor = 0, riaa = riaaOn ? 1 : 0;
+    const std::int32_t timecode = timecodeState;
     put (&editor, 1);
     put (&timecode, 4);
     put (&riaa, 1);
@@ -345,10 +364,110 @@ void Engine::setParam (int param, float value) noexcept
     }
 }
 
+std::array<double, 4> riaaCoefficients (double sampleRate) noexcept
+{
+    const auto p1 = std::exp (-1.0 / (sampleRate * 0.00318)), p2 = std::exp (-1.0 / (sampleRate * 7.5e-5));
+    const auto z1 = std::exp (-1.0 / (sampleRate * 3.18e-4));
+    return { 20.0, -20.0 * z1, -(p1 + p2), p1 * p2 };
+}
+
+void Engine::setSampleRate (double sampleRate) noexcept
+{
+    if (sampleRate > 0.0 && sampleRate != rate)
+    {
+        rate = sampleRate;
+        setupDvs();
+    }
+}
+
+void Engine::setTimecode (int state, bool riaa) noexcept
+{
+    if (state != timecode || riaa != riaaOn)
+    {
+        timecode = state;
+        riaaOn = riaa;
+        setupDvs();
+    }
+}
+
+void Engine::setupDvs() noexcept
+{
+    // [DECOMP 0x72c920] a new decoder for the type; [DECOMP 0x72ccf0 / 0x72cdf0] the RIAA preamp: poles at
+    // 3180 µs and 75 µs, a zero at 318 µs, gain 20; the hold: the same place for 0.1 s fades out, to 1/10 in 50 ms
+    decoder.configure (timecode, rate);
+    riaa = riaaCoefficients (rate);
+    riaaState = {};
+    holdLimit = (int) rate / 10;
+    holdDecay = (float) std::pow (0.1, 1.0 / (0.05 * rate));
+    dvsLast = 0.0;
+    dvsStuck = 0;
+    dvsOutL = dvsOutR = 0.0f;
+}
+
+void Engine::processDvs (float* left, float* right, int n) noexcept
+{
+    // [DECOMP 0x72add0, timecode branch] the input (through the RIAA preamp when on) drives the decoder; the record's
+    // place, in seconds × the rate, is the sample's frame (looping). The output is the sample alone: the input, the
+    // volume, the pan and the DC blocker are not used. No place (or the same place for 0.1 s): the last frame fades.
+    if (riaaOn)
+        for (int c = 0; c < 2; ++c)
+        {
+            auto* x = c == 0 ? left : right;
+            auto& s = riaaState[(std::size_t) c];
+            for (int i = 0; i < n; ++i)
+            {
+                const auto in = (double) x[i];
+                const auto y = riaa[0] * in + riaa[1] * s[0] - riaa[2] * s[1] - riaa[3] * s[2];
+                s[0] = in;
+                s[2] = s[1];
+                s[1] = y;
+                x[i] = (float) y;
+            }
+        }
+    const float* buf = playing->frames.data();
+    const auto frames = (long long) playing->n;
+    auto fade = [this] {
+        dvsOutL = std::abs ((double) dvsOutL) <= 1e-6 ? 0.0f : dvsOutL * holdDecay;
+        dvsOutR = std::abs ((double) dvsOutR) <= 1e-6 ? 0.0f : dvsOutR * holdDecay;
+    };
+    for (int i = 0; i < n; ++i)
+    {
+        decoder.process (left[i], right[i]);
+        const auto place = decoder.active() ? decoder.position() : -1.0;
+        if (place >= 0.0)
+        {
+            dvsStuck = place == dvsLast ? dvsStuck + 1 : 0;
+            if (dvsStuck < holdLimit)
+            {
+                const auto x = place * rate;
+                const auto k = (long long) x;
+                const auto f = (float) (x - (double) k);
+                const auto a = k % frames, b = (k + 1) % frames;
+                dvsOutL = (float) ((double) buf[2 * a] * (1.0 - (double) f) + (double) (buf[2 * b] * f));
+                dvsOutR = (float) ((double) buf[2 * a + 1] * (1.0 - (double) f) + (double) (buf[2 * b + 1] * f));
+                lastPos = (float) ((double) a / (double) frames);
+            }
+            else
+                fade();
+            dvsLast = place;
+        }
+        else
+        {
+            dvsStuck = 0;
+            fade();
+            dvsLast = 0.0;
+        }
+        left[i] = dvsOutL;
+        right[i] = dvsOutR;
+    }
+}
+
 void Engine::process (float* left, float* right, int n) noexcept
 {
     // [DECOMP 0x72add0]
-    if (playing != nullptr && n > 0)
+    if (playing != nullptr && n > 0 && timecode != -1)
+        processDvs (left, right, n);
+    else if (playing != nullptr && n > 0)
     {
         const float* buf = playing->frames.data();
 
@@ -475,7 +594,7 @@ public:
             engine.setSample (prepareSample (*sample));
     }
 
-    void prepare (double) noexcept override {}
+    void prepare (double sampleRate) noexcept override { engine.setSampleRate (sampleRate); }
     void reset() noexcept override
     {
         engine.reset();
@@ -493,6 +612,7 @@ public:
                 engine.setParam (i, params[(std::size_t) i]);
             }
         first = false;
+        engine.setTimecode (timecodeOf (params[timecodeType]), params[riaaPreamp] >= 0.5f);
         engine.process (left, right, n);
         position.store (engine.position(), std::memory_order_relaxed);
     }

+ 39 - 4
src/core/Scratcher.h

@@ -28,6 +28,7 @@
 #include <string_view>
 #include <vector>
 
+#include "core/Dvs.h"
 #include "core/Song.h"
 
 namespace lynx
@@ -54,8 +55,16 @@ enum Param
     smooth3 = 11,
     smooth4 = 12,
     rotations = 13,
-    count = 14
+    count = 14, // FL's 14 controls (pattern events, automation)
+    // LYNX parameters for FL's two DVS settings (stored in FL's state, not controls): Timecode type (FL's list entry
+    // / 8: 0 = Off … 1 = Timecode CD, core/Dvs.h) and RIAA preamp (0/1)
+    timecodeType = 14,
+    riaaPreamp = 15,
+    paramCount = 16
 };
+// the state's timecode index (−1 = off) for the Timecode type parameter, and back
+int timecodeOf (float value) noexcept;
+float timecodeValue (int state) noexcept;
 
 // FL's control range of each parameter (the raw integer of its knob / switch / selector)
 struct Range
@@ -88,11 +97,13 @@ struct State
     std::uint32_t version = 0;
     std::array<float, count> values {}; // LYNX's parameters, quantized like FL's controls after a load
     std::string samplePath;             // as stored (macros not expanded)
-    int timecode = -1;                  // FL's DVS (timecode vinyl) input: not reproduced
+    int timecode = -1;                  // FL's DVS type: the state's timecode index (−1 = off, core/Dvs.h)
+    bool riaa = false;                  // FL's RIAA preamp switch
 };
 std::optional<State> parse (const std::vector<std::uint8_t>& state);
-// FL's state for these parameters and path (version 0xF462A, timecode off)
-std::vector<std::uint8_t> toFl (const std::array<float, count>& values, const std::string& samplePath);
+// FL's state for these parameters and path (version 0xF462A) with the DVS settings
+std::vector<std::uint8_t> toFl (const std::array<float, count>& values, const std::string& samplePath, int timecode = -1,
+                                bool riaa = false);
 
 // The sample as FL's host hands it to the plugin [DECOMP 0x72be20]: 16-bit stereo at 44.1 kHz, as floats (k / 32768),
 // plus one extra frame for the interpolation (FL fills it with frame 0's right channel on both sides). Mono is
@@ -106,6 +117,10 @@ struct Turntable
 std::shared_ptr<const Turntable> prepareSample (const SampleData& sample);
 std::shared_ptr<const Turntable> prepareSample (const float* left, const float* right, std::size_t frames, double sampleRate);
 
+// FL's RIAA preamp [DECOMP 0x72cdf0]: b0, b1, a1, a2 of y = b0 x + b1 x₋₁ − a1 y₋₁ − a2 y₋₂ (poles 3180 µs / 75 µs, zero
+// 318 µs, gain 20)
+std::array<double, 4> riaaCoefficients (double sampleRate) noexcept;
+
 // The engine, as FL's plugin renders [DECOMP 0x72add0]. Parameter changes take effect as in FL: Wave position, Volume
 // and Pan are read from the previous block's values (FL copies them at the end of each block), the others at once.
 class Engine
@@ -118,6 +133,13 @@ public:
     void setParam (int param, float value) noexcept; // LYNX's value; quantized here
     void reset() noexcept;                           // FL's reset: platter at 0, the defaults' gains
     void process (float* left, float* right, int n) noexcept;
+    // the project's rate (the RIAA preamp, the DVS decoder and the record → sample mapping use it)
+    void setSampleRate (double sampleRate) noexcept;
+    // DVS [REA_rodada167c §1]: the state's timecode index (−1 = off, core/Dvs.h) and the RIAA preamp. With a type set
+    // the input drives the platter: the output is the sample at the record's place (the motor, Hold and the output
+    // stage are not used, as in FL). Call dvs::warm (state) off the audio thread first.
+    void setTimecode (int state, bool riaa) noexcept;
+    int timecodeState() const noexcept { return timecode; }
 
     // the platter, for the editor: its position in turns (0…1, wrapped) and whether a sample plays
     float position() const noexcept { return lastPos; }
@@ -142,6 +164,19 @@ private:
     // [0x194…0x1a8]: 1/(n−1), n−1, 2/(n−1) of the playing sample
     float invSpan = 0.0f, motorScale = 0.0f;
     int span = 0;
+
+    // DVS [0x200 on, 0x204 type, 0x220 / 0x230 the RIAA filter, 0x298 fade, 0x29c hold limit, 0x2a0 / 0x2a4 hold]
+    void setupDvs() noexcept;
+    void processDvs (float* left, float* right, int n) noexcept;
+    double rate = 44100.0;
+    int timecode = -1;
+    bool riaaOn = false;
+    dvs::Decoder decoder;
+    std::array<double, 4> riaa {};                    // b0, b1, a1, a2
+    std::array<std::array<double, 3>, 2> riaaState {}; // x1, y1, y2 per side
+    int holdLimit = 4410, dvsStuck = 0;
+    float holdDecay = 1.0f, dvsOutL = 0.0f, dvsOutR = 0.0f;
+    double dvsLast = 0.0;
 };
 
 // Whether a file dropped on the editor (an OS file drop or a drag from LYNX's Browser) is loaded onto the platter:

+ 43 - 2
src/ui/FlExtraEditors.cpp

@@ -8,6 +8,7 @@
 
 #include "core/Effects.h"
 #include "core/FlExtraFx.h"
+#include "core/Dvs.h"
 #include "core/Scratcher.h"
 #include "core/Transmitter.h"
 #include "core/VisualFx.h"
@@ -888,8 +889,31 @@ public:
           hold (t, scratcher::hold, tr ("Off"), tr ("On"), tr ("Hold")), mute (t, scratcher::mute, tr ("Off"), tr ("On"), tr ("Mute"))
     {
         for (auto* c : std::initializer_list<juce::Component*> { &transportSel, &pan, &volume, &speed, &accel, &sens, &smooth1, &smooth2,
-                                                                 &smooth3, &smooth4, &hold, &mute, &load })
+                                                                 &smooth3, &smooth4, &hold, &mute, &load, &timecodeBox, &riaaBox })
             addAndMakeVisible (*c);
+        // DVS (REA_rodada167c §1): FL's Timecode type list and RIAA preamp switch
+        for (int i = 0; i < dvs::comboCount; ++i)
+        {
+            const auto state = dvs::stateOfCombo (i);
+            auto name = i == 0 ? tr ("Off") : juce::String (dvs::comboName (i));
+            if (i == dvs::comboCount - 1)
+                name = tr ("Timecode CD");
+            if (state != -1 && ! dvs::supported (state))
+                name += " " + tr ("(not decoded yet)");
+            timecodeBox.addItem (name, i + 1);
+        }
+        ui::setHint (timecodeBox, tr ("Timecode type: a timecode vinyl or CD on this insert's input moves the platter"));
+        timecodeBox.onChange = [this] {
+            const auto combo = juce::jmax (0, timecodeBox.getSelectedItemIndex());
+            dvs::warm (dvs::stateOfCombo (combo));
+            target.edit ([v = scratcher::timecodeValue (dvs::stateOfCombo (combo))] (EffectSlot& s) { s.params[scratcher::timecodeType] = v; });
+        };
+        riaaBox.setButtonText (tr ("RIAA preamp"));
+        ui::setHint (riaaBox, tr ("RIAA preamp: for a turntable plugged straight into a line input (phono level)"));
+        riaaBox.onClick = [this] {
+            target.edit ([on = riaaBox.getToggleState()] (EffectSlot& s) { s.params[scratcher::riaaPreamp] = on ? 1.0f : 0.0f; });
+        };
+        syncDvs();
         load.setButtonText (tr ("Load sample..."));
         load.onClick = [this] { chooseSample(); };
         ui::setHint (load, tr ("The sample on the platter (or drop a file here)"));
@@ -914,7 +938,10 @@ public:
         area.removeFromTop (4);
         waveArea = area.removeFromTop (54);
         area.removeFromTop (8);
-        transportSel.setBounds (area.removeFromBottom (30).withSizeKeepingCentre (300, 30));
+        auto bottom = area.removeFromBottom (30);
+        timecodeBox.setBounds (bottom.removeFromLeft (juce::jmin (210, bottom.getWidth() / 3)).reduced (0, 3));
+        riaaBox.setBounds (bottom.removeFromRight (juce::jmin (130, bottom.getWidth() / 3)));
+        transportSel.setBounds (bottom.withSizeKeepingCentre (juce::jmin (300, bottom.getWidth()), 30));
         area.removeFromBottom (4);
 
         auto left = area.removeFromLeft (84);
@@ -1134,10 +1161,22 @@ private:
         const auto* e = target.current();
         if (e != nullptr && e->samplePath != shownPath)
             refreshSample();
+        syncDvs();
         repaint (platter.getSmallestIntegerContainer().expanded (4));
         repaint (waveArea);
     }
 
+    void syncDvs()
+    {
+        const auto* e = target.current();
+        const auto state = e != nullptr ? scratcher::timecodeOf (e->params[scratcher::timecodeType]) : -1;
+        if (timecodeBox.getSelectedItemIndex() != dvs::comboOfState (state))
+            timecodeBox.setSelectedItemIndex (dvs::comboOfState (state), juce::dontSendNotification);
+        const auto riaa = e != nullptr && e->params[scratcher::riaaPreamp] >= 0.5f;
+        if (riaaBox.getToggleState() != riaa)
+            riaaBox.setToggleState (riaa, juce::dontSendNotification);
+    }
+
     void refreshSample()
     {
         const auto* e = target.current();
@@ -1207,6 +1246,8 @@ private:
     Knob pan, volume, speed, accel, sens, smooth1, smooth2, smooth3, smooth4;
     Switch hold, mute;
     juce::TextButton load;
+    juce::ComboBox timecodeBox;
+    juce::ToggleButton riaaBox;
     juce::Rectangle<int> nameArea, waveArea, smoothArea;
     juce::Rectangle<float> platter;
     std::vector<std::pair<float, float>> peaks;

+ 182 - 0
tests/Dvs167Tests.inc

@@ -0,0 +1,182 @@
+// --- Fruity Scratcher DVS (core/Dvs.h, core/Scratcher.h) - round 167c (REA_rodada167c.md §1)
+
+#include <complex>
+
+#include "DvsSignal.h"
+#include "core/Dvs.h"
+#include "core/Scratcher.h"
+
+TEST (dvs167_types_and_state)
+{
+    using namespace lynx;
+    // FL's list: Off → −1, Timecode CD (last) → 0, the others keep their index [DECOMP 0x733fe0]
+    CHECK (dvs::stateOfCombo (0) == -1 && dvs::stateOfCombo (1) == 1 && dvs::stateOfCombo (7) == 7 && dvs::stateOfCombo (8) == 0);
+    for (int c = 0; c < dvs::comboCount; ++c)
+        CHECK (dvs::comboOfState (dvs::stateOfCombo (c)) == c);
+    CHECK (std::string (dvs::comboName (3)) == "Serato CD" && std::string (dvs::comboName (8)) == "Timecode CD");
+    CHECK (dvs::supported (1) && dvs::supported (5) && ! dvs::supported (0) && ! dvs::supported (6) && ! dvs::supported (-1));
+    // the formats [DECOMP engine_x64 0x1800037c0]
+    CHECK (dvs::format (1)->length == 710000u && dvs::format (1)->offset == 25.7 && dvs::format (4)->resolution == 2000);
+    CHECK (dvs::format (5)->bits == 23 && dvs::format (5)->flags == 0x70 && dvs::format (3)->taps == 0x34d54u);
+
+    // the Timecode type parameter ↔ the state's index
+    for (int st = -1; st <= 7; ++st)
+        CHECK (scratcher::timecodeOf (scratcher::timecodeValue (st)) == st);
+    CHECK (scratcher::paramText (scratcher::timecodeType, scratcher::timecodeValue (4)) == "Traktor Skratch Mk1, Side A");
+    CHECK (scratcher::paramText (scratcher::riaaPreamp, 1.0f) == "On");
+
+    // FL's state keeps the type and the RIAA byte (version 0xF462A)
+    const auto d = scratcher::defaults();
+    const auto blob = scratcher::toFl (d, "x.wav", 2, true);
+    const auto st = scratcher::parse (blob);
+    CHECK (st && st->timecode == 2 && st->riaa && st->samplePath == "x.wav");
+    const auto off = scratcher::parse (scratcher::toFl (d, "", -1, false));
+    CHECK (off && off->timecode == -1 && ! off->riaa);
+    // the import's slot carries them
+    const auto fx = effectFromFl ("Fruity Scratcher", blob, 140.0);
+    CHECK (fx && scratcher::timecodeOf (fx->params[scratcher::timecodeType]) == 2 && fx->params[scratcher::riaaPreamp] == 1.0f);
+}
+
+TEST (dvs167_register_and_table)
+{
+    using namespace lynx;
+    // the register's sequence: maximal length over the record (no repeat within it), Serato A
+    const auto& f = *dvs::format (1);
+    auto s = f.seed;
+    std::set<std::uint32_t> seen;
+    for (int i = 0; i < 20000; ++i)
+    {
+        seen.insert (s);
+        s = dvs::nextState (s, f);
+    }
+    CHECK (seen.size() == 20000u);
+    // the position table: a record decoded from its own start names the right place
+    dvs::warm (1, true);
+    dvs::warm (4, true);
+    dvs::Decoder dec;
+    dec.configure (1, 44100.0);
+    const auto sig = dvstest::make (f, 0.0, [] (double) { return 1.0; }, 44100, 44100.0);
+    for (int i = 0; i < 44100; ++i)
+        dec.process (sig.left[(std::size_t) i], sig.right[(std::size_t) i]);
+    CHECK (dec.locked() && std::abs (dec.position() - sig.place.back()) < 0.005);
+}
+
+TEST (dvs167_decoder_follows_the_record)
+{
+    using namespace lynx;
+    for (const int st : { 1, 2, 3, 4, 5 })
+    {
+        dvs::warm (st, true);
+        const auto& f = *dvs::format (st);
+        for (const double speed : { 1.0, 0.5, 1.5, -1.0 })
+        {
+            const double rate = st == 2 ? 48000.0 : 44100.0;
+            const auto sig = dvstest::make (f, 60.0, [speed] (double) { return speed; }, (int) rate * 2, rate);
+            dvs::Decoder dec;
+            dec.configure (st, rate);
+            for (std::size_t i = 0; i < sig.left.size(); ++i)
+                dec.process (sig.left[i], sig.right[i]);
+            CHECK (std::abs (dec.pitch() - speed) < 0.005);
+            CHECK (dec.forwards() == (speed > 0.0));
+            // forwards the register names the place; backwards it lags by its own length (FL's reading); Serato CD's
+            // taps miss the register's top bit, so FL's backwards rule never agrees: speed only (position from 0)
+            if (speed > 0.0)
+                CHECK (dec.locked() && std::abs (dec.position() - sig.place.back()) < 0.005);
+            else if (st != 3)
+                CHECK (dec.locked() && std::abs (dec.position() - sig.place.back()) < (f.bits + 1.0) / f.resolution);
+            else
+                CHECK (! dec.locked());
+        }
+    }
+    // a scratch: forwards, a stop, backwards, forwards again; the decoder keeps up (within 30 ms) once locked
+    const auto& f = *dvs::format (1);
+    auto scratch = [] (double t) { return t < 1.0 ? 1.0 : t < 1.3 ? 1.0 - (t - 1.0) / 0.15 : t < 1.6 ? -1.0 : 1.0; };
+    const auto sig = dvstest::make (f, 120.0, scratch, 44100 * 3, 44100.0);
+    dvs::Decoder dec;
+    dec.configure (1, 44100.0);
+    double worst = 0.0;
+    for (std::size_t i = 0; i < sig.left.size(); ++i)
+    {
+        dec.process (sig.left[i], sig.right[i]);
+        if (i > 22050)
+            worst = std::max (worst, std::abs (dec.position() - sig.place[i]));
+    }
+    CHECK (worst < 0.03);
+    // the unsupported types decode nothing
+    dvs::Decoder none;
+    none.configure (6, 44100.0);
+    CHECK (! none.active());
+}
+
+TEST (dvs167_scratcher_plays_the_record)
+{
+    using namespace lynx;
+    // a sample whose frame k holds k / n (a ramp), so the output names the frame played
+    const int n = 44100;
+    std::vector<float> ramp ((std::size_t) n);
+    for (int i = 0; i < n; ++i)
+        ramp[(std::size_t) i] = (float) std::round ((double) i / n * 32767.0) / 32768.0f;
+    auto table = scratcher::prepareSample (ramp.data(), ramp.data(), (std::size_t) n, 44100.0);
+    CHECK (table != nullptr);
+    scratcher::Engine eng;
+    eng.setSample (table);
+    eng.setSampleRate (44100.0);
+    dvs::warm (1, true);
+    eng.setTimecode (1, false);
+    std::vector<float> zl (256, 0.0f), zr (256, 0.0f);
+    eng.process (zl.data(), zr.data(), 256); // (the sample takes over at the end of the first block)
+
+    // 2 s of record from 10.25 s: the output is the sample at the record's place (looping every second)
+    const auto sig = dvstest::make (*dvs::format (1), 10.25, [] (double) { return 1.0; }, 44100 * 2, 44100.0);
+    std::vector<float> l (sig.left), r (sig.right);
+    for (std::size_t at = 0; at < l.size(); at += 256)
+    {
+        const auto m = (int) std::min<std::size_t> (256, l.size() - at);
+        eng.process (l.data() + at, r.data() + at, m);
+    }
+    double worst = 0.0;
+    for (std::size_t i = 44100; i < l.size(); i += 97)
+    {
+        const auto want = std::fmod (sig.place[i], 1.0);
+        auto diff = std::abs ((double) l[i] - want);
+        diff = std::min (diff, 1.0 - diff); // (the loop's seam)
+        worst = std::max (worst, diff);
+        CHECK (l[i] == r[i]); // the sample only: the timecode input is not passed
+    }
+    CHECK (worst < 0.003);
+    CHECK (std::abs (eng.position() - std::fmod (sig.place.back(), 1.0)) < 0.003);
+
+    // the needle lifts (silence): the place stops, the last frame holds 0.1 s then fades to nothing in ~0.3 s
+    std::vector<float> sl (44100, 0.0f), sr (44100, 0.0f);
+    for (int at = 0; at < 44100; at += 256)
+        eng.process (sl.data() + at, sr.data() + at, std::min (256, 44100 - at));
+    CHECK (std::abs (sl[100]) > 0.0f && sl[30000] == 0.0f);
+}
+
+TEST (dvs167_riaa_preamp)
+{
+    using namespace lynx;
+    // FL's RIAA preamp [DECOMP 0x72cdf0]: y = 20 x − 20 z₁ x₋₁ + (p₁ + p₂) y₋₁ − p₁ p₂ y₋₂ (an impulse shows it)
+    const double rate = 44100.0;
+    const auto p1 = std::exp (-1.0 / (rate * 0.00318)), p2 = std::exp (-1.0 / (rate * 7.5e-5)), z1 = std::exp (-1.0 / (rate * 3.18e-4));
+    scratcher::Engine eng;
+    std::vector<float> ramp (1000, 0.0f);
+    eng.setSample (scratcher::prepareSample (ramp.data(), ramp.data(), ramp.size(), 44100.0));
+    eng.setSampleRate (rate);
+    eng.setTimecode (6, true); // (a type LYNX does not decode: the preamp still runs, the platter stays silent)
+    std::vector<float> l (64, 0.0f), r (64, 0.0f);
+    eng.process (l.data(), r.data(), 64);
+    std::vector<float> x (64, 0.0f), y (64, 0.0f);
+    x[0] = 0.001f;
+    eng.process (x.data(), y.data(), 64);
+    CHECK (x[0] == 0.0f); // the output is the (silent) platter, never the input
+    // the filter: RIAA playback, about +19.3 dB at 20 Hz and −19.6 dB at 20 kHz around 0 dB at 1 kHz
+    const auto c = scratcher::riaaCoefficients (rate);
+    CHECK (c[0] == 20.0 && std::abs (c[1] + 20.0 * z1) < 1e-12 && std::abs (c[2] + p1 + p2) < 1e-12 && std::abs (c[3] - p1 * p2) < 1e-12);
+    auto gainDb = [&c, rate] (double hz) {
+        const std::complex<double> z = std::polar (1.0, -2.0 * 3.141592653589793 * hz / rate);
+        return 20.0 * std::log10 (std::abs ((c[0] + c[1] * z) / (1.0 + c[2] * z + c[3] * z * z)));
+    };
+    const auto ref = gainDb (1000.0);
+    CHECK (std::abs (gainDb (20.0) - ref - 19.3) < 0.5 && gainDb (10000.0) - ref < -12.0);
+}

+ 69 - 0
tests/DvsSignal.h

@@ -0,0 +1,69 @@
+#pragma once
+
+// A generated control record for the DVS tests (core/Dvs.h): what a timecode vinyl / CD plays, written from the
+// format's description (REA_rodada167c §1.2) — a sine carrier on both channels a quarter cycle apart, each cycle's
+// peak loud (bit 1) or quiet (bit 0) to spell the register's bit sequence.
+
+#include <cmath>
+#include <cstdint>
+#include <functional>
+#include <vector>
+
+#include "core/Dvs.h"
+
+namespace dvstest
+{
+struct Signal
+{
+    std::vector<float> left, right;
+    std::vector<double> place; // the record's place under the needle at each frame (seconds, as the decoder names it)
+};
+
+// `speed(t)`: the record's speed at time t (1 = normal, negative = backwards), from `start` seconds on the record
+inline Signal make (const lynx::dvs::Format& f, double start, const std::function<double (double)>& speed, int frames,
+                    double rate, float amp = 0.5f, float quiet = 0.7f)
+{
+    // the cycles the signal can reach, and their bits (the register's newest bit at each cycle)
+    double c = (start + f.offset) * f.resolution;
+    double lo = c, hi = c;
+    {
+        double x = c;
+        for (int i = 0; i < frames; ++i)
+        {
+            x += speed (i / rate) * f.resolution / rate;
+            lo = std::min (lo, x);
+            hi = std::max (hi, x);
+        }
+    }
+    const auto first = (std::int64_t) std::max (0.0, std::floor (lo) - 2.0);
+    const auto last = (std::int64_t) std::ceil (hi) + 2;
+    std::vector<std::uint8_t> bits ((std::size_t) (last - first + 1));
+    auto s = f.seed;
+    for (std::int64_t i = 0; i <= last; ++i)
+    {
+        if (i >= first)
+            bits[(std::size_t) (i - first)] = (std::uint8_t) (s & 1u);
+        s = lynx::dvs::nextState (s, f);
+    }
+
+    Signal out;
+    out.left.resize ((std::size_t) frames);
+    out.right.resize ((std::size_t) frames);
+    out.place.resize ((std::size_t) frames);
+    const bool flip = (f.flags & 0x10) != 0, leftPrimary = (f.flags & 0x40) != 0;
+    for (int i = 0; i < frames; ++i)
+    {
+        const auto cycle = (std::int64_t) std::floor (c);
+        const auto phase = 2.0 * 3.141592653589793 * (c - (double) cycle);
+        const auto bit = cycle >= first && cycle <= last ? bits[(std::size_t) (cycle - first)] : 0;
+        const auto a = (double) amp * (bit != 0 ? 1.0 : (double) quiet);
+        const auto primary = (float) (a * std::sin (phase));
+        const auto secondary = (float) (a * (flip ? std::cos (phase) : -std::cos (phase)));
+        out.left[(std::size_t) i] = leftPrimary ? primary : secondary;
+        out.right[(std::size_t) i] = leftPrimary ? secondary : primary;
+        out.place[(std::size_t) i] = c / f.resolution - f.offset;
+        c += speed (i / rate) * f.resolution / rate;
+    }
+    return out;
+}
+} // namespace dvstest

+ 1 - 0
tests/tests.cpp

@@ -16539,3 +16539,4 @@ TEST (fl_low_lifter_matches_fl_engine)
 #include "Issue14bugsTests.inc"
 #include "Tune166cTests.inc"
 #include "Update166bTests.inc"
+#include "Dvs167Tests.inc"

この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません