Ver Fonte

Audio clips on a Sampler channel play again (the silent clip on Insert 6)

An audio clip only sounds on an audio clip channel: its voice key (128 + the
clip's index) is outside every key region, so a Sampler drops it. Round 190w's
project had such a channel (written by hand): the clip showed its waveform and
played nothing, on Insert 6 or anywhere. The track link, the insert and the
channel's route were right.

The app made such channels itself: a file dropped on an audio clip channel's
Channel rack row turned the channel into a Sampler.

- audioclips::repairChannels (project load): a Sampler with a file that audio
  clips point at becomes an audio clip channel; if it also plays notes it keeps
  them and the clips move to an audio clip copy.
- audioclips::loadSampleInto (Channel rack file load): an audio clip channel
  stays one.

Tests: tests/Bugs190axInsert6Tests.inc.
Ka1-S há 3 semanas atrás
pai
commit
62320fc003

+ 54 - 0
src/core/PlaylistOps.cpp

@@ -633,6 +633,60 @@ int followAudioTracks (Song& song, const std::vector<int>& touched, bool makeUni
 
 } // namespace lynx::trackmodes
 
+namespace lynx::audioclips
+{
+int repairChannels (Song& song)
+{
+    int changed = 0;
+    const auto channels = (int) song.channels.size(); // (copies added below are audio clip channels already)
+    for (int index = 0; index < channels; ++index)
+    {
+        if (song.channels[(std::size_t) index].instrument != InstrumentKind::sampler || song.channels[(std::size_t) index].samplePath.empty())
+            continue;
+
+        auto used = false;
+        song.forEachClipList ([&] (const std::vector<Clip>& clips) {
+            for (const auto& c : clips)
+                used = used || (c.kind == ClipKind::audio && c.channel == index);
+        });
+        if (! used)
+            continue;
+
+        const auto playsNotes = std::any_of (song.patterns.begin(), song.patterns.end(), [index] (const Pattern& p) {
+            return std::any_of (p.notes.begin(), p.notes.end(), [index] (const Note& n) { return n.channel == index; });
+        });
+        ++changed;
+        if (! playsNotes)
+        {
+            song.channels[(std::size_t) index].instrument = InstrumentKind::audioClip;
+            continue;
+        }
+
+        auto copy = song.channels[(std::size_t) index];
+        copy.instrument = InstrumentKind::audioClip;
+        copy.instance.renew(); // (a new channel: the renderer must not take it for the Sampler it was copied from)
+        song.channels.push_back (std::move (copy));
+        const auto target = (int) song.channels.size() - 1;
+        song.forEachClipList ([&] (std::vector<Clip>& clips) {
+            for (auto& c : clips)
+                if (c.kind == ClipKind::audio && c.channel == index)
+                    c.channel = target;
+        });
+    }
+    return changed;
+}
+
+void loadSampleInto (ChannelDef& channel, const std::string& path, const std::string& name)
+{
+    channel.samplePath = path;
+    if (channel.instrument == InstrumentKind::granulizer || channel.instrument == InstrumentKind::wavetraveller)
+        return; // FL's Granulizer keeps its engine and plays the file (round 142); so does Wave Traveller
+    if (channel.instrument != InstrumentKind::audioClip)
+        channel.instrument = InstrumentKind::sampler;
+    channel.name = name;
+}
+} // namespace lynx::audioclips
+
 namespace lynx::autoclipplace
 {
 Place forNewClip (const Song& song, bool atPlayback, std::int64_t playTick)

+ 16 - 0
src/core/PlaylistOps.h

@@ -150,6 +150,22 @@ std::vector<int> audioTrackConflicts (const Song& song, const std::vector<int>&
 // "Consolidate track(s)" moved to core/QuickRender.h (round 168e: FL's new track above, the old one grouped and muted).
 } // namespace lynx::trackmodes
 
+// Audio clips and the channels they play (round 190ax).
+namespace lynx::audioclips
+{
+// A Playlist audio clip only sounds on an audio clip channel: its voice key (audioClipKey) is no note for a Sampler,
+// which drops it, so a clip that points at a Sampler channel showed its waveform and played nothing (a file dropped on
+// an audio clip channel's Channel rack row turned it into a Sampler; so does a project written by hand). The channels
+// such clips point at, in every clip list (arrangements, clip grid), are put right: a Sampler with a file and no notes
+// becomes the audio clip channel it is used as; one that also plays notes keeps them and its clips move to an audio
+// clip copy of it (same file, levels and mixer track). Clips on channels that hold no sample file are left as they
+// are. Returns how many channels were changed or added.
+int repairChannels (Song& song);
+// The same channel loads another file: an audio clip channel stays one (FL: the channel's type does not change with
+// its sample), a Granulizer / Wave Traveller keeps its engine, every other channel becomes a Sampler named after it.
+void loadSampleInto (ChannelDef& channel, const std::string& path, const std::string& name);
+} // namespace lynx::audioclips
+
 // FL's "Create automation clip" placement (REA round 177b: FUN_01191a50 places it, FUN_00e50b50 picks the track).
 namespace lynx::autoclipplace
 {

+ 5 - 0
src/engine/ProjectFile.cpp

@@ -5,6 +5,7 @@
 #include "core/SessionIds.h"
 
 #include "core/MixerOps.h"
+#include "core/PlaylistOps.h"
 
 #include <algorithm>
 #include <tuple>
@@ -3346,6 +3347,10 @@ std::optional<Song> fromJson (const juce::String& json, juce::String& error, juc
                 track.channel = audio ? -1 : channel;
             }
 
+    // audio clips that point at a Sampler channel (a file dropped on an audio clip channel's row made one before round
+    // 190ax; hand-written projects): the clip showed its waveform and played nothing
+    audioclips::repairChannels (song);
+
     // sends to tracks that don't exist are discarded
     for (auto& t : song.mixer)
         t.sends.erase (std::remove_if (t.sends.begin(), t.sends.end(),

+ 3 - 5
src/ui/ChannelRackComponent.cpp

@@ -19,6 +19,7 @@
 #include "core/VoltageController.h"
 
 #include "core/MixerOps.h"
+#include "core/PlaylistOps.h"
 
 #include "ui/AutomationUi.h"
 #include "core/NoteTools.h"
@@ -2735,11 +2736,8 @@ void ChannelRackComponent::assignSampleNow (int channel, const juce::File& file)
 
         if (isNew)
             c.mixerTrack = freeInsert;
-        c.samplePath = path;
-        if (! isNew && (c.instrument == InstrumentKind::granulizer || c.instrument == InstrumentKind::wavetraveller)) // FL's Granulizer keeps its engine and plays the file (round 142); so does Wave Traveller
-            return;
-        c.instrument = InstrumentKind::sampler;
-        c.name = name;
+        // (an audio clip channel stays one: as a Sampler its Playlist clips went silent, round 190ax)
+        audioclips::loadSampleInto (c, path, name);
     });
 
     if (channel < 0)

+ 175 - 0
tests/Bugs190axInsert6Tests.inc

@@ -0,0 +1,175 @@
+// Round 190ax, bug 1: an audio clip on a Playlist track linked to mixer Insert 6 made no sound on Insert 6 (round 190w's
+// project shots.lynx). The link, the insert and the channel's route were right: the clip's channel was a Sampler, not
+// an audio clip channel, and a Sampler drops the clip's voice key (audioClipKey, 128 + the clip's index: outside every
+// key region). The app made such channels itself: a file dropped on an audio clip channel's Channel rack row turned it
+// into a Sampler. core/PlaylistOps.h audioclips::repairChannels (project load) / loadSampleInto (the Channel rack).
+
+#include "core/PlaylistOps.h"
+#include <cmath>
+#include <map>
+#include <memory>
+
+namespace b190ax1
+{
+constexpr double rate = 48000.0;
+
+struct Files
+{
+    std::map<std::string, std::shared_ptr<lynx::SampleData>> map;
+    lynx::RackState::SampleProvider provider()
+    {
+        return [this] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
+            const auto it = map.find (path);
+            return it == map.end() ? nullptr : it->second;
+        };
+    }
+};
+
+// the song's peak over `seconds` (the Master's output)
+float peak (const lynx::Song& song, Files& files, double seconds = 1.0)
+{
+    auto state = lynx::RackState::build (song, nullptr, rate, files.provider());
+    lynx::RackRenderer rr;
+    rr.prepare (rate, 512);
+    const auto total = (int) (seconds * rate);
+    std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
+    double tick = 0.0;
+    const auto tps = lynx::ticksPerSample (song.bpm, rate);
+    for (int at = 0; at < total; at += 512)
+    {
+        const auto n = std::min (512, total - at);
+        rr.process (state.get(), true, tick, n, l.data() + at, r.data() + at);
+        tick += n * tps;
+    }
+    float p = 0.0f;
+    for (std::size_t i = 0; i < l.size(); ++i)
+        p = std::max ({ p, std::abs (l[i]), std::abs (r[i]) });
+    return p;
+}
+
+// 190w's project in short: channel 0 plays a pattern on Insert 1, channel 1 holds a loop routed to Insert 6 and has an
+// audio clip on Playlist track 0, an audio track linked to Insert 6
+lynx::Song song190w (Files& files, lynx::InstrumentKind loopKind)
+{
+    auto d = std::make_shared<lynx::SampleData>();
+    d->sampleRate = rate;
+    d->left.resize ((std::size_t) (2.0 * rate));
+    for (std::size_t i = 0; i < d->left.size(); ++i)
+        d->left[i] = 0.25f * (float) std::sin (6.283185307179586 * 441.0 * (double) i / rate);
+    d->right = d->left;
+    files.map["loop"] = d;
+
+    auto s = lynx::Song::makeDefault();
+    s.bpm = 130.0;
+    s.mode = lynx::PlayMode::song;
+    while (s.mixer.size() < 17)
+        s.mixer.emplace_back();
+    lynx::ChannelDef kick;
+    kick.instrument = lynx::InstrumentKind::kick;
+    kick.mixerTrack = 1;
+    lynx::ChannelDef loop;
+    loop.name = "DL_Electroloop";
+    loop.instrument = loopKind;
+    loop.samplePath = "loop";
+    loop.volume = 0.78f;
+    loop.mixerTrack = 6;
+    s.channels = { kick, loop };
+    s.patterns = { lynx::Pattern {} };
+    s.playlistTracks.resize (1);
+    s.playlistTracks[0].mode = lynx::PlaylistTrackMode::audio;
+    s.playlistTracks[0].mixerTrack = 6;
+    lynx::Clip clip;
+    clip.kind = lynx::ClipKind::audio;
+    clip.channel = 1;
+    clip.track = 0;
+    clip.length = 4 * lynx::ppq;
+    s.playlist = { clip };
+    return s;
+}
+} // namespace b190ax1
+
+TEST (b190ax_an_audio_clip_on_a_sampler_channel_was_silent_and_is_repaired)
+{
+    using namespace b190ax1;
+    Files files;
+    // the reference: the clip on an audio clip channel sounds, and it sounds on Insert 6 (only that insert rendered)
+    const auto good = song190w (files, lynx::InstrumentKind::audioClip);
+    const auto goodPeak = peak (lynx::makeBounceSong (good, 6), files);
+    CHECK (goodPeak > 0.1f);
+    CHECK (peak (lynx::makeBounceSong (good, 2), files) == 0.0f); // (nothing plays on Insert 2)
+
+    // the bug: the same clip on a Sampler channel plays nothing at all, on Insert 6 or anywhere
+    auto bad = song190w (files, lynx::InstrumentKind::sampler);
+    CHECK (peak (lynx::makeBounceSong (bad, 6), files) == 0.0f);
+    CHECK (peak (bad, files) == 0.0f);
+
+    // the repair project load runs: the channel becomes the audio clip channel it is used as, in place
+    CHECK (lynx::audioclips::repairChannels (bad) == 1);
+    CHECK (bad.channels.size() == 2 && bad.channels[1].instrument == lynx::InstrumentKind::audioClip);
+    CHECK (bad.channels[1].mixerTrack == 6 && bad.channels[1].samplePath == "loop" && bad.playlist[0].channel == 1);
+    CHECK (bad.channels == good.channels && bad.playlist == good.playlist);
+    CHECK (peak (lynx::makeBounceSong (bad, 6), files) == goodPeak);
+    CHECK (lynx::audioclips::repairChannels (bad) == 0); // nothing left to do
+}
+
+TEST (b190ax_repair_keeps_a_samplers_notes_and_other_channels)
+{
+    using namespace b190ax1;
+    Files files;
+    // the Sampler also plays notes: it stays a Sampler with its notes; the clips move to an audio clip copy
+    auto s = song190w (files, lynx::InstrumentKind::sampler);
+    lynx::Note n;
+    n.channel = 1;
+    s.patterns[0].notes.push_back (n);
+    lynx::Arrangement other;
+    other.clips = s.playlist; // a stored arrangement's clip of the same channel follows
+    s.arrangements = { lynx::Arrangement {}, other };
+    s.currentArrangement = 0;
+    CHECK (lynx::audioclips::repairChannels (s) == 1);
+    CHECK (s.channels.size() == 3 && s.channels[1].instrument == lynx::InstrumentKind::sampler);
+    CHECK (s.channels[2].instrument == lynx::InstrumentKind::audioClip && s.channels[2].samplePath == "loop" && s.channels[2].mixerTrack == 6);
+    CHECK (s.playlist[0].channel == 2 && s.arrangements[1].clips[0].channel == 2 && s.patterns[0].notes[0].channel == 1);
+    CHECK (peak (lynx::makeBounceSong (s, 6), files) > 0.1f);
+    CHECK (lynx::audioclips::repairChannels (s) == 0);
+
+    // left alone: a Sampler no clip uses, a Sampler without a file, a channel that is no Sampler, a pattern clip
+    auto t = song190w (files, lynx::InstrumentKind::sampler);
+    t.playlist[0].kind = lynx::ClipKind::pattern;
+    t.playlist[0].pattern = 0;
+    const auto before = t;
+    CHECK (lynx::audioclips::repairChannels (t) == 0 && t.channels == before.channels && t.playlist == before.playlist);
+    t = song190w (files, lynx::InstrumentKind::sampler);
+    t.channels[1].samplePath.clear();
+    CHECK (lynx::audioclips::repairChannels (t) == 0 && t.channels[1].instrument == lynx::InstrumentKind::sampler);
+    t = song190w (files, lynx::InstrumentKind::synth);
+    CHECK (lynx::audioclips::repairChannels (t) == 0 && t.channels[1].instrument == lynx::InstrumentKind::synth);
+}
+
+TEST (b190ax_a_file_loaded_into_an_audio_clip_channel_keeps_it_one)
+{
+    // the Channel rack's file drop on a row (ChannelRackComponent::assignSampleNow)
+    lynx::ChannelDef c;
+    c.name = "Old";
+    c.instrument = lynx::InstrumentKind::audioClip;
+    c.samplePath = "old.wav";
+    lynx::audioclips::loadSampleInto (c, "new.wav", "new");
+    CHECK (c.instrument == lynx::InstrumentKind::audioClip && c.samplePath == "new.wav" && c.name == "new");
+
+    // as before for the others: a drum or a synth becomes a Sampler, a Granulizer / Wave Traveller keeps its engine and name
+    for (const auto kind : { lynx::InstrumentKind::kick, lynx::InstrumentKind::synth, lynx::InstrumentKind::sampler })
+    {
+        lynx::ChannelDef d;
+        d.name = "Old";
+        d.instrument = kind;
+        lynx::audioclips::loadSampleInto (d, "new.wav", "new");
+        CHECK (d.instrument == lynx::InstrumentKind::sampler && d.samplePath == "new.wav" && d.name == "new");
+    }
+    for (const auto kind : { lynx::InstrumentKind::granulizer, lynx::InstrumentKind::wavetraveller })
+    {
+        lynx::ChannelDef d;
+        d.name = "Old";
+        d.instrument = kind;
+        lynx::audioclips::loadSampleInto (d, "new.wav", "new");
+        CHECK (d.instrument == kind && d.samplePath == "new.wav" && d.name == "Old");
+    }
+}

+ 1 - 0
tests/tests.cpp

@@ -17407,3 +17407,4 @@ TEST (fl_low_lifter_matches_fl_engine)
 #include "Slicex190hTests.inc"
 #include "Parity190vTests.inc"
 #include "Surge190xTests.inc"
+#include "Bugs190axInsert6Tests.inc"