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

Filtro do canal no sampler (Mod X/Y do FL) e painel sem perder fatias

- SamplerSettings ganha corte/ressonância (SVF TPT por voz, desligado
  quando aberto); salvo no projeto e editável no painel do sampler
- o import aplica o filtro do canal do FL (0xDB) também aos samplers
  (52 dos 439 samplers dos demos usam)
- o painel do sampler partia de SamplerSettings{} e apagava as fatias
  ao mexer em qualquer knob; agora parte dos ajustes atuais
Kauan 1 месяц назад
Родитель
Сommit
e0e1ab93bd
6 измененных файлов с 100 добавлено и 6 удалено
  1. 3 0
      src/core/FlpImport.cpp
  2. 40 2
      src/core/Instruments.cpp
  3. 2 0
      src/core/Song.h
  4. 7 0
      src/engine/ProjectFile.cpp
  5. 16 4
      src/ui/ChannelSettings.cpp
  6. 32 0
      tests/tests.cpp

+ 3 - 0
src/core/FlpImport.cpp

@@ -317,6 +317,9 @@ Song importFlp (const flp::Project& project, const FlpImportOptions& options)
             def.sampler.release = std::clamp (std::max (seconds (env.release), 0.002f), 0.0f, 4.0f); // envelope ligado: a nota acaba
         }
 
+        // filtro do canal (Mod X/Y + tipo do 0xDB) vale para o sampler também
+        synthFilterFromChannel (c.modX, c.modY, c.filterType, def.sampler.cutoff, def.sampler.resonance);
+
         // 3x Osc: osciladores do estado do plugin; envelope e filtro vêm do canal (REA_rodada4)
         if (def.instrument == InstrumentKind::synth && c.pluginName == "3x Osc" && synthFrom3xOsc (c.pluginState, def.synth))
         {

+ 40 - 2
src/core/Instruments.cpp

@@ -505,6 +505,7 @@ struct SamplerVoice
     float panL = 1.0f, panR = 1.0f;
     double glideRatio = 1.0;
     int glideLeft = 0;
+    float lowL = 0.0f, bandL = 0.0f, lowR = 0.0f, bandR = 0.0f; // filtro do canal
 };
 
 class SamplerInstrument final : public Instrument
@@ -534,6 +535,8 @@ public:
         stretchBeats.store (s.keepPitch ? 0.0f : std::max (0.0f, s.stretchBeats));
         regionStart.store (std::clamp (s.regionStart, 0.0f, 1.0f));
         regionEnd.store (std::clamp (s.regionEnd, 0.0f, 1.0f));
+        cutoff.store (std::clamp (s.cutoff, 20.0f, 20000.0f));
+        resonance.store (std::clamp (s.resonance, 0.0f, 1.0f));
         SliceTable table;
         table.count = (int) std::min<std::size_t> (s.slices.size(), table.starts.size());
         for (int i = 0; i < table.count; ++i)
@@ -706,6 +709,21 @@ public:
         const auto* r = sample->rightData();
         const auto last = (double) (sample->frames() - 1);
 
+        // filtro do canal (SVF TPT, o mesmo do synth); desligado quando aberto e sem ressonância
+        const auto fc = cutoff.load(), res = resonance.load();
+        const auto filtered = fc < 19500.0f || res > 0.0f;
+
+        if (filtered && (fc != filterCutoff || res != filterResonance))
+        {
+            filterCutoff = fc;
+            filterResonance = res;
+            const auto g = (float) std::tan (3.14159265358979 * std::min ((double) fc, sampleRate * 0.45) / sampleRate);
+            const auto k = 2.0f - 1.9f * res;
+            fa1 = 1.0f / (1.0f + g * (g + k));
+            fa2 = g * fa1;
+            fa3 = g * fa2;
+        }
+
         for (auto& v : voices)
         {
             if (! v.active)
@@ -721,8 +739,15 @@ public:
 
                 const auto index = (std::size_t) v.position;
                 const auto frac = (float) (v.position - (double) index);
-                const auto sl = l[index] + frac * (l[index + 1] - l[index]);
-                const auto sr = r[index] + frac * (r[index + 1] - r[index]);
+                auto sl = l[index] + frac * (l[index + 1] - l[index]);
+                auto sr = r[index] + frac * (r[index + 1] - r[index]);
+
+                if (filtered)
+                {
+                    sl = svf (sl, v.lowL, v.bandL);
+                    sr = svf (sr, v.lowR, v.bandR);
+                }
+
                 auto gain = v.velocity * v.fade * v.env;
 
                 if (v.fadeIn > 0.0f || v.fadeOut > 0.0f)
@@ -801,6 +826,19 @@ private:
     std::atomic<float> pitch { 0.0f }, attack { 0.0f }, decay { 0.0f }, sustain { 1.0f }, release { 0.0f }, stretchBeats { 0.0f };
     std::atomic<double> bpm { 120.0 };
     std::atomic<float> regionStart { 0.0f }, regionEnd { 1.0f };
+    std::atomic<float> cutoff { 20000.0f }, resonance { 0.0f };
+    float filterCutoff = -1.0f, filterResonance = -1.0f; // valores com que os coeficientes foram feitos
+    float fa1 = 1.0f, fa2 = 0.0f, fa3 = 0.0f;
+
+    float svf (float in, float& low, float& band) const noexcept
+    {
+        const auto v3 = in - low;
+        const auto v1 = fa1 * band + fa2 * v3;
+        const auto v2 = low + fa2 * band + fa3 * v3;
+        band = 2.0f * v1 - band;
+        low = 2.0f * v2 - low;
+        return v2;
+    }
     std::array<float, 4> nextClip {}; // fades do próximo noteOn (só a thread de áudio mexe)
 
     struct SliceTable

+ 2 - 0
src/core/Song.h

@@ -62,6 +62,8 @@ struct SamplerSettings
     float regionStart = 0.0f;  // trecho tocado do sample (frações 0..1)
     float regionEnd = 1.0f;
     std::vector<float> slices; // fatias (inícios em frações): a tecla 60 + i toca a fatia i, na altura original
+    float cutoff = 20000.0f;   // passa-baixas do canal (Hz); 20 kHz e sem ressonância = desligado
+    float resonance = 0.0f;    // 0..1
 
     bool operator== (const SamplerSettings&) const = default;
 };

+ 7 - 0
src/engine/ProjectFile.cpp

@@ -252,6 +252,7 @@ juce::String toJson (const Song& song)
             }
             sampler->setProperty ("reverse", c.sampler.reverse);
             sampler->setProperty ("cut", c.sampler.cut);
+            sampler->setProperty ("filter", juce::Array<juce::var> { c.sampler.cutoff, c.sampler.resonance });
             o->setProperty ("samplerSettings", juce::var (sampler.release()));
         }
 
@@ -547,6 +548,12 @@ std::optional<Song> fromJson (const juce::String& json, juce::String& error)
                 }
                 def.sampler.reverse = (bool) s.getProperty ("reverse", false);
                 def.sampler.cut = (bool) s.getProperty ("cut", false);
+
+                if (const auto& filter = s["filter"]; filter.isArray() && filter.size() == 2)
+                {
+                    def.sampler.cutoff = juce::jlimit (20.0f, 20000.0f, (float) (double) filter[0]);
+                    def.sampler.resonance = juce::jlimit (0.0f, 1.0f, (float) (double) filter[1]);
+                }
             }
 
             song.channels.push_back (std::move (def));

+ 16 - 4
src/ui/ChannelSettings.cpp

@@ -24,6 +24,11 @@ public:
         setup (stretch, stretchLabel, "Esticar", 0.0, 64.0, 0.25, " tempos");
         setup (regionStart, regionStartLabel, "Início", 0.0, 1.0, 0.001, "");
         setup (regionEnd, regionEndLabel, "Fim", 0.0, 1.0, 0.001, "");
+        setup (cutoff, cutoffLabel, "Corte", 20.0, 20000.0, 1.0, " Hz");
+        setup (resonance, resonanceLabel, "Ressonância", 0.0, 1.0, 0.01, "");
+        cutoff.setSkewFactorFromMidPoint (1000.0);
+        cutoff.setDoubleClickReturnValue (true, 20000.0);
+        cutoff.setTooltip (utf8 ("Passa-baixas do canal (no máximo e sem ressonância fica desligado)"));
         regionEnd.setDoubleClickReturnValue (true, 1.0);
         regionStart.setTooltip (utf8 ("De onde o sample começa a tocar (fração do arquivo)"));
         sustain.setDoubleClickReturnValue (true, 1.0);
@@ -50,7 +55,7 @@ public:
 
         refresh();
         engine.addChangeListener (this);
-        setSize (420, 420);
+        setSize (420, 536);
     }
 
     ~SettingsPanel() override { engine.removeChangeListener (this); }
@@ -87,6 +92,8 @@ public:
         area.removeFromTop (6);
         place (area.removeFromTop (110), { { &regionStart, &regionStartLabel }, { &regionEnd, &regionEndLabel } });
         area.removeFromTop (6);
+        place (area.removeFromTop (110), { { &cutoff, &cutoffLabel }, { &resonance, &resonanceLabel } });
+        area.removeFromTop (6);
         reverse.setBounds (area.removeFromLeft (150).removeFromTop (24));
         cut.setBounds (area.removeFromLeft (150).removeFromTop (24));
     }
@@ -127,6 +134,8 @@ private:
             keepPitch.setToggleState (c->sampler.keepPitch, juce::dontSendNotification);
             regionStart.setValue (c->sampler.regionStart, juce::dontSendNotification);
             regionEnd.setValue (c->sampler.regionEnd, juce::dontSendNotification);
+            cutoff.setValue (c->sampler.cutoff, juce::dontSendNotification);
+            resonance.setValue (c->sampler.resonance, juce::dontSendNotification);
             release.setValue (c->sampler.release, juce::dontSendNotification);
             reverse.setToggleState (c->sampler.reverse, juce::dontSendNotification);
             cut.setToggleState (c->sampler.cut, juce::dontSendNotification);
@@ -142,7 +151,7 @@ private:
         if (updating || current() == nullptr)
             return;
 
-        SamplerSettings s;
+        auto s = current()->sampler; // mantém o que o painel não mostra (fatias)
         s.pitch = (float) pitch.getValue();
         s.attack = (float) attack.getValue();
         s.decay = (float) decay.getValue();
@@ -154,6 +163,8 @@ private:
         s.release = (float) release.getValue();
         s.reverse = reverse.getToggleState();
         s.cut = cut.getToggleState();
+        s.cutoff = (float) cutoff.getValue();
+        s.resonance = (float) resonance.getValue();
 
         if (s == current()->sampler)
             return;
@@ -179,8 +190,9 @@ private:
     AudioEngine& engine;
     int channel;
     bool updating = false;
-    juce::Slider pitch, attack, release, decay, sustain, stretch, regionStart, regionEnd;
-    juce::Label pitchLabel, attackLabel, releaseLabel, decayLabel, sustainLabel, stretchLabel, regionStartLabel, regionEndLabel;
+    juce::Slider pitch, attack, release, decay, sustain, stretch, regionStart, regionEnd, cutoff, resonance;
+    juce::Label pitchLabel, attackLabel, releaseLabel, decayLabel, sustainLabel, stretchLabel, regionStartLabel, regionEndLabel,
+        cutoffLabel, resonanceLabel;
     juce::ToggleButton reverse, cut, keepPitch;
 };
 

+ 32 - 0
tests/tests.cpp

@@ -778,6 +778,38 @@ TEST (flp_import_3x_osc_state_and_channel_filter)
     CHECK (hz >= 19999.0f);
 }
 
+TEST (sampler_channel_filter_darkens_and_bypasses_when_open)
+{
+    // ruído branco no sampler: com corte em 300 Hz a energia cai muito; aberto passa igual
+    auto data = std::make_shared<lynx::SampleData>();
+    data->sampleRate = 48000.0;
+    data->left.resize (48000);
+    std::uint32_t seed = 7;
+    for (auto& v : data->left)
+    {
+        seed = seed * 1664525u + 1013904223u;
+        v = ((float) (seed >> 8) / 16777216.0f - 0.5f);
+    }
+    data->right = data->left;
+    auto energy = [&] (float cutoff) {
+        auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
+        inst->prepare (48000.0);
+        lynx::ChannelDef c;
+        c.sampler.cutoff = cutoff;
+        inst->configure (c, 120.0);
+        inst->noteOn (60, 1.0f);
+        std::vector<float> l (24000), r (24000);
+        inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
+        double e = 0.0;
+        for (auto v : l)
+            e += (double) v * v;
+        return e;
+    };
+    const auto open = energy (20000.0f), dark = energy (300.0f);
+    CHECK (open > 100.0);
+    CHECK (dark < open * 0.05);
+}
+
 TEST (synth_oscillator_pan_and_semitones)
 {
     auto render = [] (lynx::SynthSettings s, std::vector<float>& l, std::vector<float>& r) {