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

DAWproject export: audio clips keep their level, their channel's resampling pitch and FL's clip stretch (in 0.1.31.x)

LYNX -> DAWproject -> LYNX made Thief's audio clips +3.3 LUFS louder (risers +7.8 dB, the lead vocal +3.9 dB) and
741 of its 1,351 clips (drums at +6 st) played at the file's own pitch and speed; other apps got the same. DAWproject's
clips have no gain, and the export left out, without a note, the clip gain and the sample channel's own volume, wrote
a resampling channel's pitch nowhere, and dropped FL 2026's clip stretch of a stretched or warped sample.

Now:
- an audio track's clips' level (clip gain x channel volume, unity at FL's 10000 / 12800 as the import writes it back)
  goes into the track's Volume when every clip on the row has the same one; rows whose clips differ are listed in the
  notes ("Audio clips at different levels ...");
- the pitch of a channel that resamples (core/ClipOps.h clipSpeed: pitch knob + fine tune) is written as the speed it
  is: a "repitch" warp, which the import reads back as the same speed; a pitch shift that keeps the length (keep pitch,
  FL 2026's clip pitch) stays in the notes;
- the clip stretch scales the warp of a stretched or warped sample instead of being dropped.

Thief per Playlist row, LYNX's render of the source vs LYNX's render after the round trip: the gain rows (risers, vocal,
crash) go from +3.9 / +6.8 / +7.8 dB to the source's level; the rest of the difference is what DAWproject has no place
for (clip pan, reverse, envelopes, effect mix and channel automation; all in the notes). Test:
iox190rm_dawproject_audio_clip_level_and_pitch; dawproject190jf_what_does_not_map now checks the clip-pan note (a clip's
gain alone is folded, not listed).
Ka1-S 1 неделя назад
Родитель
Сommit
397b7771b5
3 измененных файлов с 138 добавлено и 9 удалено
  1. 40 8
      src/core/DawProject.cpp
  2. 2 1
      tests/DawProject190jfTests.inc
  3. 96 0
      tests/Iox190rmTests.inc

+ 40 - 8
src/core/DawProject.cpp

@@ -19,6 +19,7 @@
 #include "core/AlsDevices.h"
 #include "core/AlsDevices.h"
 #include "core/AlsImport.h"
 #include "core/AlsImport.h"
 #include "core/ClipLauncher.h"
 #include "core/ClipLauncher.h"
+#include "core/ClipOps.h"
 #include "core/ApexLimiter.h"
 #include "core/ApexLimiter.h"
 #include "core/DynamicsSuite.h"
 #include "core/DynamicsSuite.h"
 #include "core/Effects.h"
 #include "core/Effects.h"
@@ -2627,6 +2628,26 @@ struct Exp
             pan = m->pan;
             pan = m->pan;
             muted = m->muted;
             muted = m->muted;
         }
         }
+        // (round 190rm) an audio track: its clips' own level, folded in when every clip on the row has the same one — the
+        // clip's gain (DAWproject's clips have none) times its channel's volume (FL's sample channel level, unity at FL's
+        // 10000 / 12800, as the import writes it back). Before, both were left out (Thief: its risers +7.8 dB, the vocal
+        // +3.9 dB, 176 drum clips +1.9 dB after a round trip)
+        if (c == nullptr && t.kind == TNode::Kind::audio && t.row >= 0)
+        {
+            std::optional<double> level;
+            auto mixed = false;
+            for (const auto& clip : song.playlist)
+                if (clip.kind == ClipKind::audio && clip.track == t.row && clip.channel >= 0 && clip.channel < (int) song.channels.size())
+                {
+                    const auto v = (double) clip.gain * channelGain (song.channels[(std::size_t) clip.channel].volume) / channelGain (audioClipUnityVolume);
+                    mixed = mixed || (level && std::abs (*level - v) > 1.0e-6 * std::max (1.0, v));
+                    level = v;
+                }
+            if (level && ! mixed)
+                gain *= *level;
+            else if (mixed)
+                note ("Audio clips at different levels (clip gain, channel volume) on one track: their levels (not written)");
+        }
         if (c != nullptr)
         if (c != nullptr)
         {
         {
             gain *= channelGain (c->volume);
             gain *= channelGain (c->volume);
@@ -3124,7 +3145,12 @@ DawProjectExportResult exportDawProject (const Song& input, const DawProjectExpo
         Node audio ("Audio");
         Node audio ("Audio");
         audio.set ("id", x.newId()).set ("channels", std::to_string (channels)).set ("sampleRate", std::to_string (rate)).set ("duration", numText (seconds));
         audio.set ("id", x.newId()).set ("channels", std::to_string (channels)).set ("sampleRate", std::to_string (rate)).set ("duration", numText (seconds));
         const auto& s = ch.sampler;
         const auto& s = ch.sampler;
-        const auto warped = s.warp.size() >= 2 || (s.stretchBeats > 0.0f && seconds > 0.0) || (c->stretch != 1.0 && seconds > 0.0);
+        // (round 190rm) the channel's pitch on a channel that resamples (FL's sample channels: Thief's drums are +6 st in
+        // 741 of its 1,351 clips) reads the file faster or slower: written as the warp it is (DAWproject "repitch"),
+        // where before the clip played at the file's own pitch and speed. With "keep pitch" it is a pitch shift that
+        // keeps the length, which DAWproject has no place for (in the notes).
+        const auto speed = clipops::clipSpeed (ch);
+        const auto warped = s.warp.size() >= 2 || (s.stretchBeats > 0.0f && seconds > 0.0) || ((c->stretch != 1.0 || speed != 1.0) && seconds > 0.0);
         audio.set ("algorithm", warped && s.keepPitch ? "stretch" : "repitch");
         audio.set ("algorithm", warped && s.keepPitch ? "stretch" : "repitch");
         auto& file = audio.add ("File");
         auto& file = audio.add ("File");
         if (const auto e = x.embeddedPath.find (samplePath); e != x.embeddedPath.end())
         if (const auto e = x.embeddedPath.find (samplePath); e != x.embeddedPath.end())
@@ -3149,7 +3175,8 @@ DawProjectExportResult exportDawProject (const Song& input, const DawProjectExpo
             clip.set ("name", ch.name);
             clip.set ("name", ch.name);
         if (c->muted || rowMuted (c->track))
         if (c->muted || rowMuted (c->track))
             clip.set ("enable", "false");
             clip.set ("enable", "false");
-        clipExtras = clipExtras || c->gain != 1.0f || c->pan != 0.0f || c->pitch != 0.0f || c->reversed || c->envelope >= 0 || s.pitch != 0.0f || s.reverse;
+        clipExtras = clipExtras || c->pan != 0.0f || c->pitch != 0.0f || c->reversed || c->envelope >= 0
+                     || (s.keepPitch && s.pitch != 0.0f) || (! warped && speed != 1.0) || s.reverse;
         if (warped)
         if (warped)
         {
         {
             clip.set ("contentTimeUnit", "beats").set ("playStart", numText (offsetBeats));
             clip.set ("contentTimeUnit", "beats").set ("playStart", numText (offsetBeats));
@@ -3158,15 +3185,20 @@ DawProjectExportResult exportDawProject (const Song& input, const DawProjectExpo
             w.set ("id", x.newId()).set ("timeUnit", "beats").set ("contentTimeUnit", "seconds");
             w.set ("id", x.newId()).set ("timeUnit", "beats").set ("contentTimeUnit", "seconds");
             w.kids.push_back (std::move (audio));
             w.kids.push_back (std::move (audio));
             std::vector<WarpPoint> marks = s.warp;
             std::vector<WarpPoint> marks = s.warp;
+            // (round 190rm: FL 2026's clip stretch and the channel's resampling pitch scale the beats every marker lands
+            // on, as core/ClipOps.h clipSpeed plays them; before, the clip stretch of a warped or stretched sample was
+            // left out, and so was the pitch)
+            const auto scale = std::clamp (c->stretch, 0.01, 100.0) / speed;
             if (marks.size() < 2)
             if (marks.size() < 2)
             {
             {
-                const auto beats = s.stretchBeats > 0.0f ? (double) s.stretchBeats : seconds * song.bpm / 60.0 * c->stretch;
+                const auto beats = (s.stretchBeats > 0.0f ? (double) s.stretchBeats : seconds * song.bpm / 60.0) * scale;
                 marks = { { 0.0, 0.0 }, { seconds, beats } };
                 marks = { { 0.0, 0.0 }, { seconds, beats } };
-                if (s.stretchBeats <= 0.0f)
+                if (s.stretchBeats <= 0.0f && c->stretch != 1.0)
                     x.note ("Per-clip stretch written as warp markers");
                     x.note ("Per-clip stretch written as warp markers");
             }
             }
-            else if (c->stretch != 1.0)
-                x.note ("Per-clip stretch on warped audio (not written)");
+            else
+                for (auto& m : marks)
+                    m.beats *= scale;
             for (const auto& m : marks)
             for (const auto& m : marks)
                 w.add ("Warp").set ("time", numText (m.beats)).set ("contentTime", numText (m.seconds));
                 w.add ("Warp").set ("time", numText (m.beats)).set ("contentTime", numText (m.seconds));
         }
         }
@@ -3176,12 +3208,12 @@ DawProjectExportResult exportDawProject (const Song& input, const DawProjectExpo
             clip.set ("contentTimeUnit", "seconds").set ("playStart", numText (offsetBeats * 60.0 / song.bpm));
             clip.set ("contentTimeUnit", "seconds").set ("playStart", numText (offsetBeats * 60.0 / song.bpm));
             clip.set ("fadeTimeUnit", "beats").set ("fadeInTime", numText ((double) c->fadeIn / ppq)).set ("fadeOutTime", numText ((double) c->fadeOut / ppq));
             clip.set ("fadeTimeUnit", "beats").set ("fadeInTime", numText ((double) c->fadeIn / ppq)).set ("fadeOutTime", numText ((double) c->fadeOut / ppq));
             clip.kids.push_back (std::move (audio));
             clip.kids.push_back (std::move (audio));
-            if (s.stretchBeats > 0.0f || c->stretch != 1.0)
+            if (s.stretchBeats > 0.0f || c->stretch != 1.0 || speed != 1.0)
                 x.note ("Stretched audio whose file length was not known (written unstretched)");
                 x.note ("Stretched audio whose file length was not known (written unstretched)");
         }
         }
     }
     }
     if (clipExtras)
     if (clipExtras)
-        x.note ("Audio clip gain, pan, pitch, reverse and clip envelopes (not written)");
+        x.note ("Audio clip pan, pitch shifts that keep the length, reverse and clip envelopes (not written)");
 
 
     // automation: each target's points in song time, from automation clips and lanes
     // automation: each target's points in song time, from automation clips and lanes
     struct Pt
     struct Pt

+ 2 - 1
tests/DawProject190jfTests.inc

@@ -932,6 +932,7 @@ TEST (dawproject190jf_what_does_not_map)
     s.patterns[0].notes[0].pan = 0.5f;
     s.patterns[0].notes[0].pan = 0.5f;
     s.patterns[0].notes[1].muted = true;
     s.patterns[0].notes[1].muted = true;
     s.playlist[3].gain = 0.5f;
     s.playlist[3].gain = 0.5f;
+    s.playlist[3].pan = 0.5f; // (round 190rm: a clip's gain alone is folded into its track's level; its pan is not written)
     s.loopStart = 0;
     s.loopStart = 0;
     s.loopEnd = 3840;
     s.loopEnd = 3840;
     s.vcas.push_back ({});
     s.vcas.push_back ({});
@@ -947,7 +948,7 @@ TEST (dawproject190jf_what_does_not_map)
     s.playlistTracks[0].lanes.push_back ({ (int) s.automations.size() - 1, 0 });
     s.playlistTracks[0].lanes.push_back ({ (int) s.automations.size() - 1, 0 });
     const auto out = exportDawProject (s); // (no readFile: the audio is referenced where it is)
     const auto out = exportDawProject (s); // (no readFile: the audio is referenced where it is)
     CHECK (! out.bytes.empty() && out.projectXml.find ("external=\"true\"") != std::string::npos);
     CHECK (! out.bytes.empty() && out.projectXml.find ("external=\"true\"") != std::string::npos);
-    for (const auto* part : { "Note pan", "Muted notes", "Audio clip gain", "song's loop", "VCA faders", "clip grid", "other than the current",
+    for (const auto* part : { "Note pan", "Muted notes", "Audio clip pan", "song's loop", "VCA faders", "clip grid", "other than the current",
                               "Insert-to-Master", "arpeggiators", "Curved automation", "has no parameter for", "length was not known" })
                               "Insert-to-Master", "arpeggiators", "Curved automation", "has no parameter for", "length was not known" })
     {
     {
         const auto found = hasNote (out.notes, part);
         const auto found = hasNote (out.notes, part);

+ 96 - 0
tests/Iox190rmTests.inc

@@ -72,3 +72,99 @@ TEST (iox190rm_dawproject_audio_format_never_zero)
     CHECK (plain.projectXml.find ("path=\"audio/riser.wv\"") != std::string::npos);
     CHECK (plain.projectXml.find ("path=\"audio/riser.wv\"") != std::string::npos);
 }
 }
 
 
+TEST (iox190rm_dawproject_audio_clip_level_and_pitch)
+{
+    // Thief → DAWproject → LYNX was +3.3 dB louder on its audio clips: the clip gain (DAWproject has none) and the sample
+    // channel's volume were left out, and a resampling channel's pitch (+6 st on 741 of 1,351 clips) played the file at
+    // its own speed and pitch. Now a level every clip of a row shares goes into its track's Volume, and the pitch is the
+    // speed it is (a "repitch" warp); the round trip plays the same.
+    using namespace lynx;
+    Song s;
+    s.channels.clear();
+    s.patterns.clear();
+    s.playlist.clear();
+    s.playlistTracks.clear();
+    s.mixer = Song::defaultMixer (3);
+    s.mode = PlayMode::song;
+    s.bpm = 120.0;
+    ChannelDef a;
+    a.name = "Riser";
+    a.instrument = InstrumentKind::audioClip;
+    a.samplePath = "/samples/riser.wav";
+    a.mixerTrack = 1;
+    a.volume = 0.6f;
+    a.sampler.pitch = 12.0f; // (resamples: twice as fast)
+    s.channels.push_back (a);
+    a.name = "Loop";
+    a.samplePath = "/samples/loop.wav";
+    a.mixerTrack = 2;
+    a.volume = audioClipUnityVolume;
+    a.sampler.pitch = 0.0f;
+    s.channels.push_back (a);
+    for (const auto* name : { "Riser", "Loop" })
+    {
+        PlaylistTrack t;
+        t.name = name;
+        s.playlistTracks.push_back (t);
+    }
+    Clip c;
+    c.kind = ClipKind::audio;
+    c.channel = 0;
+    c.track = 0;
+    c.length = 1920;
+    c.gain = 0.5f;
+    s.playlist.push_back (c);
+    c.start = 3840; // (a second clip of the row, the same level)
+    s.playlist.push_back (c);
+    c.channel = 1;
+    c.track = 1;
+    c.start = 0;
+    c.gain = 0.25f;
+    s.playlist.push_back (c);
+    c.start = 1920;
+    c.gain = 1.0f; // (a row whose clips differ: said in the notes)
+    s.playlist.push_back (c);
+    DawProjectExportOptions o;
+    o.sampleFormat = [] (const std::string&, double& seconds, int& rate, int& channels) {
+        seconds = 4.0, rate = 44100, channels = 2;
+        return true;
+    };
+    const auto r = exportDawProject (s, o);
+    CHECK (! r.bytes.empty());
+    // the Riser row: 0.5 × channelGain (0.6) / channelGain (unity) on its track's Volume
+    const auto want = 0.5 * channelGain (0.6f) / channelGain (audioClipUnityVolume) * faderGain (s.mixer[1].volume);
+    const auto riser = r.projectXml.find ("name=\"Riser\"");
+    CHECK (riser != std::string::npos);
+    const auto vol = r.projectXml.find ("<Volume", riser);
+    CHECK (vol != std::string::npos);
+    const auto at = r.projectXml.find ("value=\"", vol);
+    const auto value = std::atof (r.projectXml.c_str() + at + 7);
+    CHECK (std::abs (value - want) < 1.0e-4);
+    // the pitch: 4 s of file are 8 beats at 120 BPM, twice as fast 4 beats ("repitch"); the Loop plays at its own speed
+    CHECK (r.projectXml.find ("algorithm=\"repitch\"") != std::string::npos);
+    CHECK (r.projectXml.find ("<Warp time=\"4\" contentTime=\"4\"/>") != std::string::npos);
+    CHECK (r.projectXml.find ("<Warp time=\"8\" contentTime=\"4\"/>") == std::string::npos);
+    bool levelsNote = false;
+    for (const auto& n : r.notes)
+        levelsNote = levelsNote || n.find ("different levels") != std::string::npos;
+    CHECK (levelsNote);
+    // and back: the Riser channel plays its 4 s in 4 beats (twice as fast), its insert at the folded level
+    DawProjectImportOptions io;
+    io.common.fileExists = [] (const std::string&) { return true; };
+    const auto back = importDawProject (r.bytes, io);
+    CHECK (back.has_value());
+    if (back)
+    {
+        bool found = false;
+        for (const auto& ch : back->channels)
+            if (ch.samplePath == "/samples/riser.wav")
+            {
+                found = true;
+                CHECK (std::abs (ch.sampler.stretchBeats - 4.0f) < 1.0e-4f && ! ch.sampler.keepPitch);
+                CHECK (ch.mixerTrack > 0 && ch.mixerTrack < (int) back->mixer.size());
+                if (ch.mixerTrack > 0 && ch.mixerTrack < (int) back->mixer.size())
+                    CHECK (std::abs (faderGain (back->mixer[(std::size_t) ch.mixerTrack].volume) - want) < 2.0e-3);
+            }
+        CHECK (found);
+    }
+}