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

FLP import: channel enabled switch, FL 11 mixer slots; renderwav reads Ogg; 3x Osc host HQ render

Ka1-S 1 месяц назад
Родитель
Сommit
eba272876f
7 измененных файлов с 98 добавлено и 6 удалено
  1. 1 0
      src/core/FlpImport.cpp
  2. 1 1
      src/core/Instruments.cpp
  3. 40 1
      src/core/ThreeOsc.cpp
  4. 3 1
      src/core/ThreeOsc.h
  5. 34 0
      src/flp/Flp.cpp
  6. 2 1
      src/flp/Flp.h
  7. 17 2
      tools/WavReader.h

+ 1 - 0
src/core/FlpImport.cpp

@@ -634,6 +634,7 @@ Song importFlp (const flp::Project& input, const FlpImportOptions& options)
             def.instrument = InstrumentKind::audioClip; // no file: stays silent, but the clip remains in the playlist
 
         def.mixerTrack = std::clamp (c.mixerTrack, 0, maxMixerTracks - 1);
+        def.muted = ! c.enabled; // switched off in FL's Channel Rack: its notes don't play (REA_rodada80 §3.1)
 
         if (c.colour != 0)
             def.colour = 0xFF000000u | c.colour;

+ 1 - 1
src/core/Instruments.cpp

@@ -2205,7 +2205,7 @@ std::unique_ptr<Instrument> makeInstrument (InstrumentKind kind, std::shared_ptr
     if (kind == InstrumentKind::toxic)
         return makeToxicInstrument();
     if (kind == InstrumentKind::threeosc)
-        return makeThreeOscInstrument();
+        return makeThreeOscInstrument (&offlineRendering);
     if (kind == InstrumentKind::autogun)
         return makeAutogunInstrument();
     if (kind == InstrumentKind::transistorbass)

+ 40 - 1
src/core/ThreeOsc.cpp

@@ -378,6 +378,7 @@ struct Osc
     std::uint32_t spo = 0;
     const float* tab = nullptr;
     bool noise = false;
+    bool aa = false; // P+0x1a1: neither noise nor the sine table — the host's HQ render oversamples it (FUN_0073a000)
     float g = 0, gL = 0, gR = 0;
 };
 
@@ -428,6 +429,7 @@ public:
             else
                 o.tab = T.t[3].data(); // (negative shapes: outside the knob's range)
             o.noise = sh == 5;
+            o.aa = ! o.noise && o.tab != T.t[0].data(); // FUN_00739060, parameter kind 2
         }
         const auto g3 = (float) p[13] * 0.0078125f;
         const auto rest = P.am ? 1.0f : 1.0f - g3;
@@ -553,6 +555,33 @@ public:
                     if (! am)
                         first = false;
                 }
+                else if (bitsOf (o.g) != 0 && hostHq && o.aa && k != amIndex())
+                {
+                    // FUN_0073a000 (FL's HQ render, Dispatcher 1 bit 1): k = max(inc >> 22, 1) copies of the
+                    // oscillator at gain g/k (float), the first at the stored phase (mode 11 when it is the first
+                    // written, else 10; its end phase is kept), copy j at that phase + j·(inc/k) (int division)
+                    const auto copies = std::max<std::uint32_t> (inc >> 22, 1u);
+                    const auto g = (ld) ((side == 0 ? o.gL : o.gR) / (float) copies);
+                    const auto step = (std::uint32_t) ((std::int32_t) inc / (std::int32_t) copies);
+                    float* y = x + side;
+                    auto start = phase;
+                    for (std::uint32_t c = 0; c < copies; ++c)
+                    {
+                        auto q = start;
+                        if (first)
+                        {
+                            for (int i = 0; i < n; ++i, q += inc)
+                                y[2 * i] = (float) ((ld) o.tab[q >> 18] * g);
+                            first = false;
+                        }
+                        else
+                            for (int i = 0; i < n; ++i, q += inc)
+                                y[2 * i] = (float) ((ld) o.tab[q >> 18] * g + (ld) y[2 * i]);
+                        if (c == 0)
+                            ph[(std::size_t) k] = q;
+                        start += step;
+                    }
+                }
                 else if (bitsOf (o.g) != 0)
                 {
                     const auto g = (ld) (side == 0 ? o.gL : o.gR);
@@ -698,6 +727,7 @@ public:
     }
 
     Params P;
+    bool hostHq = false; // FL's "HQ render" host mode (P+0x1fd): set while LYNX exports, as FL's renders do
     flvp::Settings S;
     std::array<std::uint8_t, snapshotSize> snap = flvp::defaultSnapshot();
     std::shared_ptr<const std::vector<float>> custom; // the channel's sample as a custom wave (16384 points), if any
@@ -902,6 +932,8 @@ struct Setup
 class ThreeOscInstrument final : public Instrument
 {
 public:
+    bool (*hostHq)() noexcept = nullptr; // FL's HQ-render host mode while this returns true (checked on prepare)
+
     ThreeOscInstrument()
     {
         engine.prepareBlock (blockSize);
@@ -919,6 +951,7 @@ public:
         rate = sr;
         take();
         engine.custom = current.custom, engine.hqCustomSet = current.hq;
+        engine.hostHq = hostHq != nullptr && hostHq();
         engine.init (current.P, sr, current.snap.data(), current.ppq, current.tempo);
         engine.reset();
         tickPhase = 0.0;
@@ -1062,6 +1095,7 @@ private:
             current = *s;
             engine.P = current.P;
             engine.custom = current.custom, engine.hqCustomSet = current.hq;
+        engine.hostHq = hostHq != nullptr && hostHq();
             std::memcpy (engine.snap.data(), current.snap.data(), snapshotSize);
             engine.timing (current.ppq, current.tempo); // (FL: parameters and tempo reach the plugin while voices play)
             engine.update();
@@ -1087,7 +1121,12 @@ private:
 };
 } // namespace
 
-std::unique_ptr<Instrument> makeThreeOscInstrument() { return std::make_unique<ThreeOscInstrument>(); }
+std::unique_ptr<Instrument> makeThreeOscInstrument (bool (*hostHq)() noexcept)
+{
+    auto i = std::make_unique<ThreeOscInstrument>();
+    i->hostHq = hostHq;
+    return i;
+}
 
 namespace threeosc
 {

+ 3 - 1
src/core/ThreeOsc.h

@@ -52,7 +52,9 @@ std::pair<int, int> threeOscParamRange (int index) noexcept;
 // A custom wave (shape 6) from an audio file: mono, 16384 points over the whole file. Empty if unreadable.
 std::vector<float> threeOscCustomWave (const std::string& wavPath);
 
-std::unique_ptr<Instrument> makeThreeOscInstrument();
+// hostHq: when it returns true at prepare(), the oscillators render as in FL's "HQ render" host mode (FL's
+// exports and command-line renders; REA_rodada80 §3.3)
+std::unique_ptr<Instrument> makeThreeOscInstrument (bool (*hostHq)() noexcept = nullptr);
 
 namespace threeosc
 {

+ 34 - 0
src/flp/Flp.cpp

@@ -21,6 +21,8 @@ enum : std::uint32_t
     evPluginState = 0xD5,       // plugin blob (channel generator or slot effect)
     evChannelPolyphony = 0xDD,
     evChannelType = 0x15,
+    evChannelEnabled = 0x00,    // u8: 0 = the channel is switched off in the Channel Rack
+    evPluginNew = 0xD4,         // plugin record: i32 mixer insert (or channel), i32 slot, i32 kind (2 = effect), ...
     evChannelMixerTrackOld = 0x16, // u8, old files
     evNewChannel = 0x40,
     evNewPattern = 0x41,
@@ -350,6 +352,17 @@ Project parseProject (std::span<const std::uint8_t> file, bool channelPreset)
     std::string pendingInsertName;
     std::string pendingSlotPlugin;
     std::vector<std::uint8_t> pendingSlotState;
+    int pendingSlotIndex = -1; // from 0xD4: FL <= 11 files have no 0x62, the next slot/route/insert closes the slot
+    auto closeOldSlot = [&] {
+        if (pendingSlotIndex >= 0 && ! pendingSlotPlugin.empty() && ! project.mixer.empty())
+            project.mixer.back().slots.push_back ({ pendingSlotIndex, pendingSlotPlugin, std::move (pendingSlotState) });
+        if (pendingSlotIndex >= 0)
+        {
+            pendingSlotPlugin.clear();
+            pendingSlotState.clear();
+        }
+        pendingSlotIndex = -1;
+    };
     std::uint32_t pendingInsertColour = 0;
 
     auto text = [&] (const Event& e)
@@ -545,6 +558,16 @@ Project parseProject (std::span<const std::uint8_t> file, bool channelPreset)
                 channel();
                 break;
 
+            case evChannelEnabled:
+                if (section == Section::channel)
+                    channel().enabled = e.value != 0;
+                break;
+
+            case evPluginNew:
+                if (section == Section::mixer && e.data.size() >= 8)
+                    pendingSlotIndex = readLE<std::int32_t> (e.data, 4);
+                break;
+
             case evChannelType:
                 if (section == Section::channel)
                     channel().type = e.value <= 5 ? (ChannelType) e.value : ChannelType::unknown;
@@ -575,7 +598,10 @@ Project parseProject (std::span<const std::uint8_t> file, bool channelPreset)
                 if (section == Section::channel && channel().pluginName.empty())
                     channel().pluginName = text (e);
                 else if (section == Section::mixer)
+                {
+                    closeOldSlot();
                     pendingSlotPlugin = text (e);
+                }
                 break;
 
             case evMixerSlotIndex:
@@ -583,6 +609,7 @@ Project parseProject (std::span<const std::uint8_t> file, bool channelPreset)
                     project.mixer.back().slots.push_back ({ (int) e.value, pendingSlotPlugin, std::move (pendingSlotState) });
                 pendingSlotPlugin.clear();
                 pendingSlotState.clear();
+                pendingSlotIndex = -1;
                 break;
 
             case evPluginState:
@@ -772,6 +799,8 @@ Project parseProject (std::span<const std::uint8_t> file, bool channelPreset)
             }
 
             case evMixerRoutes:
+                if (section == Section::mixer)
+                    closeOldSlot();
                 if (section == Section::mixer && ! project.mixer.empty())
                 {
                     auto& insert = project.mixer.back();
@@ -809,6 +838,8 @@ Project parseProject (std::span<const std::uint8_t> file, bool channelPreset)
 
             case evMixerInsertParams:
             {
+                if (section == Section::mixer)
+                    closeOldSlot();
                 section = Section::mixer;
                 auto& insert = project.mixer.emplace_back();
                 insert.index = (int) project.mixer.size() - 1;
@@ -827,6 +858,9 @@ Project parseProject (std::span<const std::uint8_t> file, bool channelPreset)
         }
     }
 
+    if (section == Section::mixer)
+        closeOldSlot();
+
     // the stretch time is int in files without bit 0 of 0xA9, float in newer ones
     for (auto& ch : project.channels)
     {

+ 2 - 1
src/flp/Flp.h

@@ -89,6 +89,7 @@ struct Channel
     std::string pluginName;      // internal name of the generator (e.g. "FLEX")
     std::string samplePath;
     int mixerTrack = -1;
+    bool enabled = true;         // 0x00: the channel's enabled switch in the Channel Rack (false: its notes don't play)
     std::vector<AutomationPoint> automation; // channels of type automationClip
 
     // event 0xDB (channel "levels"): pan 0..12800 (6400 = center), volume 0..12800
@@ -268,7 +269,7 @@ struct MixerInsert
 
     struct Slot
     {
-        int index = 0;      // 0..9
+        int index = 0;      // 0..9 (0x62; files without it, FL <= 11: the slot field of the plugin's 0xD4)
         std::string plugin; // internal name (0xC9), e.g. "Fruity Limiter"
         std::vector<std::uint8_t> state; // the plugin's 0xD5 (e.g. Fruity Wrapper blob)
     };

+ 17 - 2
tools/WavReader.h

@@ -1,7 +1,7 @@
 #pragma once
 
-// Minimal WAV reader (PCM 8/16/24/32 and float 32) for the tools and tests.
-// The app uses JUCE's readers, which cover many more formats.
+// Minimal WAV reader (PCM 8/16/24/32 and float 32, WavPack, and FL's Ogg Vorbis: Ogg-in-WAV and .ogg, decoded as
+// the app decodes them, core/OggVorbis.h) for the tools and tests. The app uses JUCE's readers for the rest (FLAC…).
 
 #include <cstdint>
 #include <cstring>
@@ -12,6 +12,7 @@
 #include <vector>
 
 #include "core/Instruments.h"
+#include "core/OggVorbis.h"
 #include "core/WavPack.h"
 
 namespace lynx::tools
@@ -39,6 +40,20 @@ inline std::shared_ptr<const SampleData> readWav (const std::string& path)
         return out;
     }
 
+    // FL's Ogg Vorbis samples (WAV with an Ogg "data" chunk, or plain .ogg), like the app's SampleLibrary
+    if (oggvorbis::isOggWav (d.data(), d.size()) || oggvorbis::isOggStream (d.data(), d.size()))
+    {
+        oggvorbis::Decoded audio;
+        if (! oggvorbis::decode (d.data(), d.size(), audio) || audio.sampleRate <= 0 || audio.data.empty() || audio.data[0].empty())
+            return nullptr;
+        auto out = std::make_shared<SampleData>();
+        out->sampleRate = audio.sampleRate;
+        out->left = std::move (audio.data[0]);
+        if (audio.data.size() > 1)
+            out->right = std::move (audio.data[1]);
+        return out;
+    }
+
     if (d.size() < 12 || std::memcmp (d.data(), "RIFF", 4) != 0 || std::memcmp (d.data() + 8, "WAVE", 4) != 0)
         return nullptr;