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

FPC cycle layers and velocity index, Slicex master volume, FL envelope only on native samplers

Ka1-S 1 месяц назад
Родитель
Сommit
3ea2b8cf4c
5 измененных файлов с 79 добавлено и 10 удалено
  1. 5 0
      src/core/FlSamplers.cpp
  2. 1 0
      src/core/FlSamplers.h
  3. 10 1
      src/core/FlpImport.cpp
  4. 17 9
      src/core/Instruments.cpp
  5. 46 0
      tests/tests.cpp

+ 5 - 0
src/core/FlSamplers.cpp

@@ -390,6 +390,11 @@ std::optional<SlicedSample> slicex (const std::vector<std::uint8_t>& d)
     for (auto& m : master)
         m = in.read<std::int32_t>();
     result.pitchCents = (float) std::clamp (master[5], -1200, 1200);
+    // master volume (DFM range 0…16000, 12800 = 100 %): FL's L11 law in single precision (Slicex FUN_0087efe0)
+    {
+        const auto x = (float) std::clamp (master[2], 0, 16000) * 7.8125e-05f;
+        result.gain = x == 1.0f ? 1.0f : (float) ((std::exp ((double) (x * (float) std::log (11.0))) - 1.0) * (double) 0.1f);
+    }
     in.skip (47 + 12); // options and UI state: deck A starts at 0x5F
 
     const auto referenceOnly = in.read<std::uint8_t>(); // 0 = embedded WAV follows

+ 1 - 0
src/core/FlSamplers.h

@@ -34,6 +34,7 @@ struct SlicedSample
     std::vector<std::uint8_t> embeddedWav;
     std::vector<std::int64_t> positions; // Slicer: start of each slice (frames)
     float pitchCents = 0.0f;
+    float gain = 1.0f; // Slicex master volume: (11^(v/12800) − 1)/10 (REA_rodada69 §5)
 };
 std::optional<SlicedSample> fruitySlicer (const std::vector<std::uint8_t>& state);
 std::optional<SlicedSample> slicex (const std::vector<std::uint8_t>& state);

+ 10 - 1
src/core/FlpImport.cpp

@@ -667,7 +667,7 @@ Song importFlp (const flp::Project& input, const FlpImportOptions& options)
             def.sampler.release = std::clamp (std::max (seconds (env.release), 0.002f), 0.0f, 4.0f); // envelope on: the note ends
 
             // FL's own articulator (delay, hold, tensions, tick steps; REA_rodada69 §3) from the record as stored
-            if (env.rawSize >= 68)
+            if (env.rawSize >= 68 && c.type == flp::ChannelType::sampler)
             {
                 def.sampler.flVolumeEnvelope.resize (18);
                 std::memcpy (def.sampler.flVolumeEnvelope.data(), env.raw.data(), 68);
@@ -809,6 +809,15 @@ Song importFlp (const flp::Project& input, const FlpImportOptions& options)
                     def.samplePath = path;
                     def.sampler.pitch = std::clamp (def.sampler.pitch + sliced->pitchCents / 100.0f, -24.0f, 24.0f);
 
+                    // Slicex's master volume (REA_rodada69 §5): a single full-range zone carries the gain
+                    if (sliced->gain != 1.0f && ! path.empty())
+                    {
+                        SampleZone zone;
+                        zone.path = path;
+                        zone.gain = sliced->gain;
+                        def.zones = { zone };
+                    }
+
                     if (frames > 0)
                         for (auto p : positions)
                             if (p >= 0 && p < frames)

+ 17 - 9
src/core/Instruments.cpp

@@ -1389,22 +1389,28 @@ public:
             sample = zones.front().sample;
     }
 
-    // the zone for the key/velocity; several covering the same note take turns (round-robin, like
-    // FPC's cycled layers); if none covers it, the one with the nearest root key
+    // the zone for the key/velocity; several covering the same note take turns like FPC's "cycle" layers (REA_rodada69
+    // §4: among the zones covering the note, in order, the one after the last chosen, wrapping); if none covers it, the
+    // one with the nearest root key. The velocity index is FL's round(velocity · 128), at most 127.
     const LoadedZone* zoneFor (int key, float velocity) noexcept
     {
         if (zones.empty())
             return nullptr;
 
-        const auto vel = (int) std::lround (std::clamp (velocity, 0.0f, 1.0f) * 127.0f);
+        const auto vel = std::min (127, (int) std::nearbyint (std::clamp (velocity, 0.0f, 1.0f) * 128.0f));
         const LoadedZone* nearest = nullptr;
-        const LoadedZone* matches[16] {};
-        int matchCount = 0;
+        int matches[16] {};
+        int matchCount = 0, lastAt = -1;
 
-        for (const auto& z : zones)
+        for (int i = 0; i < (int) zones.size(); ++i)
         {
+            const auto& z = zones[(std::size_t) i];
             if (key >= z.zone.lowKey && key <= z.zone.highKey && vel >= z.zone.lowVelocity && vel <= z.zone.highVelocity && matchCount < 16)
-                matches[matchCount++] = &z;
+            {
+                if (i == lastZone)
+                    lastAt = matchCount;
+                matches[matchCount++] = i;
+            }
 
             if (nearest == nullptr || std::abs (z.zone.rootKey - key) < std::abs (nearest->zone.rootKey - key))
                 nearest = &z;
@@ -1413,7 +1419,9 @@ public:
         if (matchCount == 0)
             return nearest;
 
-        return matches[(roundRobin++) % (unsigned) matchCount];
+        const auto pick = matchCount == 1 || lastAt + 1 >= matchCount ? 0 : lastAt + 1;
+        lastZone = matches[pick];
+        return &zones[(std::size_t) lastZone];
     }
 
     void prepare (double sr) noexcept override
@@ -1952,7 +1960,7 @@ public:
     void setSongPosition (double ticks) noexcept override { songTicks = ticks; }
 
 private:
-    unsigned roundRobin = 0;        // turn of the next overlapping layer
+    int lastZone = -1;              // the zone the last note chose (overlapping layers cycle from it)
     double lastRate = 0.0; // read speed of the last note (where porta starts from)
     std::atomic<int> filterType { 0 };
     float filterCutoff = -1.0f, filterResonance = -1.0f; // values the coefficients were computed with

+ 46 - 0
tests/tests.cpp

@@ -2327,6 +2327,52 @@ TEST (fl_sampler_volume_envelope_steps_per_tick)
     CHECK (l[39999] == 0.0f && sampler->idle());
 }
 
+// FPC's "cycle" layers (REA_rodada69 §4): among the layers covering the velocity, in order, the one after the last
+// chosen, wrapping; FL's velocity index is round(v·128). Slicex's master volume uses FL's L11 law (§5).
+TEST (fl_fpc_cycle_layers_and_slicex_master_volume)
+{
+    auto level = [] (float v) {
+        auto d = std::make_shared<lynx::SampleData>();
+        d->sampleRate = 48000.0;
+        d->left.assign (4800, v);
+        return d;
+    };
+    // A covers 0…63, B 32…95, C 64…127 (all keys)
+    auto inst = lynx::makeZonedSampler ({ { { "a.wav", 60, 0, 127, 0, 63 }, level (0.1f) },
+                                          { { "b.wav", 60, 0, 127, 32, 95 }, level (0.2f) },
+                                          { { "c.wav", 60, 0, 127, 64, 127 }, level (0.3f) } });
+    inst->prepare (48000.0);
+    inst->configure (lynx::ChannelDef {}, 120.0);
+    auto play = [&] (float velocity) {
+        inst->allNotesOff();
+        std::vector<float> l (2000), r (2000);
+        inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
+        std::fill (l.begin(), l.end(), 0.0f);
+        inst->noteOn (60, velocity);
+        inst->render (l.data(), r.data(), 10, 1.0f, 1.0f);
+        return l[5] / velocity;
+    };
+    auto which = [] (float x) { return (int) std::lround (x / 0.1f / std::cos (0.25f * 3.14159265f) / 1.41421356f); };
+    CHECK (which (play (64.0f / 128.0f)) == 2); // candidates B, C: first B
+    CHECK (which (play (64.0f / 128.0f)) == 3); // then C
+    CHECK (which (play (40.0f / 128.0f)) == 1); // candidates A, B: C is not among them → the first, A
+    CHECK (which (play (40.0f / 128.0f)) == 2); // then B
+    CHECK (which (play (64.0f / 128.0f)) == 3); // B was last → C
+
+    // Slicex state: version, 8 master values (volume 16000), options and UI state, deck A referencing a file
+    std::vector<std::uint8_t> state (4 + 32 + 47 + 12, 0);
+    const std::int32_t version = 0x00060003, volume = 16000;
+    std::memcpy (state.data(), &version, 4);
+    std::memcpy (state.data() + 4 + 8, &volume, 4);
+    state.push_back (1);
+    state.push_back (5);
+    for (char ch : std::string ("x.wav"))
+        state.push_back ((std::uint8_t) ch);
+    const auto sliced = lynx::slicex (state);
+    CHECK (sliced.has_value() && sliced->path == "x.wav");
+    CHECK (sliced && std::abs (sliced->gain - (float) ((std::pow (11.0, 1.25) - 1.0) / 10.0)) < 1e-5f);
+}
+
 TEST (sampler_plays_pitched_one_shot)
 {
     // 1000-frame ramp sample: the position can be measured from the amplitude