Explorar el Código

Merge origin/main into feature/ui163

Ka1-S hace 1 mes
padre
commit
e2fe367bc2

+ 3 - 0
.github/ISSUE_TEMPLATE/config.yml

@@ -6,3 +6,6 @@ contact_links:
   - name: Latest release notes
     url: https://github.com/Ka1-S/LYNX/releases
     about: Check whether your problem was already fixed.
+  - name: Questions, ideas and feedback
+    url: https://github.com/Ka1-S/LYNX/discussions
+    about: Ask how to do something, share ideas or show what you made with LYNX.

+ 10 - 0
.github/pull_request_template.md

@@ -0,0 +1,10 @@
+## What does this change?
+<!-- One or two sentences. Link the issue it fixes: "Fixes #123". -->
+
+## How was it tested?
+<!-- e.g. scripts/build-local.sh with 0 failures, tried it on Windows/macOS/Linux, screenshots for UI changes. -->
+
+## Checklist
+- [ ] Tests pass (`scripts/build-local.sh`, 0 failures)
+- [ ] New UI texts are in English, with a pt-BR line in `resources/i18n/pt-BR.txt`
+- [ ] No code, graphics, presets or samples copied from FL Studio or third-party plugins (see [CONTRIBUTING.md](../CONTRIBUTING.md))

+ 19 - 0
CODE_OF_CONDUCT.md

@@ -0,0 +1,19 @@
+# Code of Conduct
+
+LYNX is made by and for people who love making music. We want the issues, pull requests and every other LYNX space to be friendly and welcoming for everyone, whatever their experience, background or the DAW they come from.
+
+## Expected
+- Be kind and patient, especially with people who are new to music software or to open source.
+- Give feedback on the work, not the person. Assume good intent.
+- Keep discussions on topic and constructive.
+
+## Not accepted
+- Harassment, insults, personal attacks, or discriminatory jokes and language.
+- Sharing someone's private information without permission.
+- Posting cracked software, license keys, or links to pirated plugins and sample packs.
+- Spam and self-promotion that has nothing to do with the discussion.
+
+## Enforcement
+The maintainer may edit, hide or remove comments, lock threads and block people who don't follow this code. To report a problem privately, use [GitHub's report content feature](https://docs.github.com/en/communities/maintaining-your-safety-on-github/reporting-abuse-or-spam).
+
+This code is adapted from the ideas of the [Contributor Covenant](https://www.contributor-covenant.org/), version 2.1.

+ 22 - 0
CONTRIBUTING.md

@@ -0,0 +1,22 @@
+# Contributing to LYNX
+
+Thanks for wanting to help! LYNX is an alpha, so bug reports, ideas and pull requests are all very welcome.
+
+## Reporting bugs and asking for features
+- Use the [issue templates](https://github.com/Ka1-S/LYNX/issues/new/choose): *Bug report*, *Feature request* or *Sound / plugin difference*.
+- Say which LYNX version and OS you use (Help › About). For crashes, attach the crash file LYNX offers to send, or `%APPDATA%\LYNX\autosave\crash*.txt` (Windows) / `~/.config/LYNX/autosave/` (Linux).
+- Screenshots and short videos help a lot, especially side by side with FL Studio.
+
+## Pull requests
+1. Build LYNX as described in [README › Building](README.md#building) and run the tests (`scripts/build-local.sh`). They must pass with 0 failures.
+2. Keep each pull request focused on one change, and add a test for logic changes.
+3. Code, UI texts, comments and docs are in **English**. New UI texts also need a pt-BR line in `resources/i18n/pt-BR.txt`.
+4. Follow the style of the file you're editing (C++20, JUCE 8).
+
+## Clean-room rule
+LYNX recreates the FL Studio *workflow*, not its code or artwork.
+- Don't copy code, graphics, presets, samples or names from FL Studio or any third-party plugin.
+- Third-party plugins are studied as black boxes only (host them, feed signals, measure). Never decompile, patch or bypass their licensing.
+
+## License
+By contributing you agree that your contribution is licensed under the [GNU AGPL v3](LICENSE), like the rest of LYNX.

+ 11 - 0
SECURITY.md

@@ -0,0 +1,11 @@
+# Security policy
+
+## Supported versions
+Only the latest LYNX alpha release gets fixes. Please check the problem still happens there before reporting it.
+
+## Reporting a vulnerability
+Please **don't open a public issue** for security problems (for example a crafted project, sample or preset file that makes LYNX run code, or a problem with the crash-report upload or the website).
+
+Report it privately instead: on GitHub go to **Security › Report a vulnerability** ([direct link](https://github.com/Ka1-S/LYNX/security/advisories/new)). Include the LYNX version, OS, and steps or a file to reproduce it.
+
+You'll get an answer as soon as possible. After a fix is released, the report can be made public, with credit to you if you want.

+ 48 - 0
docs/analise/REA_rodada162c.md

@@ -0,0 +1,48 @@
+# Round 162c (sampler162): issue #9 — routing past insert 16, Sampler tuning / stretch, pitch wheel, export, editor volume
+
+2026-10-26. Issue #9 (BURNER1PA, Windows 10, LYNX 0.1.17): the sampler / channel / MIDI / export items. Evidence
+markers: [CODE] read in LYNX's source; [TEST] proven by tests/Sampler162Tests.inc (build-local); [DOC] FL Studio's
+online manual; [WIN] checked in the Windows build.
+
+## 1. Summary
+
+| # | Report | Cause | Fix | Status |
+|---|---|---|---|---|
+| 1 | "Unable to assign a channel past insert 16" | [CODE] the route box, the channel window's Track knob and the setters were limited to `song.mixer.size() - 1`; a new project has Master + 16 inserts (maxMixerTracks = 128 was never reachable from there); the renderer clamps a route past the last insert to the last one | `mixerops::routeChannel` / `ensureInsert`: any insert 0…127 (FL 2026: 125 + Master), the missing inserts are added; `Song::claimFreeInsert` gives new channels insert 17, 18… instead of Master once 1…16 are in use; a project whose channel routes past its saved mixer gets the inserts on load | done [TEST] |
+| 2 | "Sampler fine tune does not change the pitch" | [CODE] (a) keys played live (MIDI / PC keyboard) never got Misc › Fine tune nor the main pitch (only the song's notes did); (b) with Stretch (Resample) the pitch knob, the fine tune and the key were all ignored (`semitones = 0` when stretched) | live notes get `setNextNoteExpression (fineTune + main pitch)`; (b) see 3 | done [TEST] |
+| 3 | "Stretch ignores the key: every note the same pitch" | [CODE] Resample stretch: rate fixed at the stretch speed; "Stretch keeping pitch": pre-stretched sample then resampled per key (pitch and length both changed) | [DOC] FL: Resample = "pitch and length linked … each note at a different speed"; Stretch modes = "pitch changes keep the original duration". Resample: the key/pitch/fine transpose the stretched speed (like tape). Keeping pitch: a transposed voice reads the stretched sample `factor` times faster (exact transposition, FL's sinc reader) and a real-time Signalsmith Stretch (`VoicePitchShifter`) stretches it back to its length — the key transposes, the length stays; portamento / slides / pitch envelope / wheel move the factor | done [TEST] |
+| 4 | "MIDI pitch bend / mod wheel don't work by default" | [CODE] `handleIncomingMidiMessage` only played notes; CCs went to MIDI-learn links only; the pitch wheel was dropped | the pitch wheel, CCs (except 120…127 and CCs that have a MIDI link) and channel pressure go to the channels the notes go to (selected / device target): hosted plugins (VST/VST3/CLAP, SoundFont player, MIDI Out ports) get the MIDI message; LYNX's Sampler and Synth bend by the channel's pitch range (FL's Misc › Pitch range, default ±2) | done [TEST] |
+| 5 | "Export fails when replacing a file of the same name" | [CODE] the exporter deleted the old file and opened a new stream on the same name; on Windows a file another program (player, preview) holds open cannot be deleted, and JUCE's FileOutputStream then opens the existing file positioned at its end (the new audio appended after the old) | write to a temporary file next to it and replace the target only when complete; a target held open is reported before the render ("… is in use by another program …"); a cancelled export leaves the old file intact. The save dialog already asks to replace (warnAboutOverwriting) | done [WIN] |
+| 6 | "Audio Editor: add a volume knob" | — | a playback volume knob next to Loop (0…125 %, double-click 100 %, remembered in the settings, live while playing); "Amp..." (Tools / quick tools) already changes the audio's level | done |
+
+## 2. Notes
+
+- Native instruments other than the Sampler and the built-in Synth ignore the pitch wheel for now (they have
+  `Instrument::pitchBend`, default no-op); hosted plugins get every message. The mod wheel reaches plugins and the
+  SoundFont player; LYNX's own engines have no CC 1 mapping (FL's natives neither, unless linked).
+- A key played at the root of a "keeping pitch" sampler plays the stretched sample untouched (bit-identical to before);
+  only transposed voices go through the shifter.
+
+## 3. Stretch keeping pitch: measurements [MEAS]
+
+Scratch benchmarks (g++ -O2, machine loaded by other builds, so times are upper bounds):
+- Signalsmith's own transposition (`setTransposeFactor`) on pure sines, 60 ms blocks: up to 33 cents off at 110 Hz
+  (8 cents at 440 Hz); 120 ms blocks: 12 cents. Not usable for a sampler's keys.
+- Resample by the factor + Signalsmith time-stretch back to length (transpose 1): ≤ 0.05 cent from 55 Hz to 1 kHz,
+  −12…+12 semitones, with 80–100 ms blocks (60 ms failed at 55 Hz). Chosen: 100 ms, overlap 4 (drums: 80 ms).
+- Priming a note (outputSeek, no latency): ~1 ms unloaded (11 ms with the offline 120/20 ms preset). A render primes
+  at most 2 notes (exports: all); the 3rd+ note of a chord starts one block later. 8 shifters per channel (~4 MB);
+  beyond 8 transposed notes at once the extra ones play resampled.
+
+## Where I stopped
+
+- Items 1–6 done; tests in tests/Sampler162Tests.inc (7 tests, 40 checks). build-local with FL Data: 1752886 checks,
+  0 failures. Windows build (build-win-sampler162): selftest OK, export over the same file twice OK, a target held open
+  reported (rc 1) with the old file kept, `--snapshot` shows the Audio Editor volume knob.
+- Not done: FL black-box comparison of the stretch modes (relied on FL's manual [DOC]); native engines other than the
+  Sampler / Synth ignore the pitch wheel; pitch bend is not recorded into patterns.
+- First landing attempt failed: the keeping-pitch tests were flaky (pass/fail between runs of the same binary,
+  always failing with ASLR off). Cause: `VoicePitchShifter::maxPrimeLength()` used the shortest prime (factor 4)
+  instead of the longest (factor 0.25), so the warm-up in `allocate()` read past its buffer (ASan, g++ harness) and
+  the shifter could start from heap garbage (NaN output); real notes also got a truncated prime. Fixed; an ASan sweep
+  of factors 0.25…4 is clean.

BIN
docs/img/social-preview.png


+ 5 - 0
resources/i18n/pt-BR.txt

@@ -4698,6 +4698,11 @@ countries: br
 "[double-click] add it as a new channel, or drag it onto the Channel Rack" = "[clique duplo] adiciona como novo canal, ou arraste para o Channel Rack"
 "[double-click] add it to the selected insert, or drag it onto a mixer insert or an effect slot" = "[clique duplo] adiciona ao insert selecionado, ou arraste para um insert do Mixer ou um slot de efeito"
 
+# --- Round 162 (issue #9): export over an existing file, Audio Editor playback volume
+"\"{0}\" is in use by another program (a media player, or a preview). Close it, or export with another name." = "\"{0}\" está em uso por outro programa (um player de mídia ou uma pré-escuta). Feche-o ou exporte com outro nome."
+"could not replace {0}" = "não foi possível substituir {0}"
+"could not write {0}" = "não foi possível gravar {0}"
+"Playback volume: {0} (the audio is not changed; Tools > Amp... changes it)" = "Volume de reprodução: {0} (o áudio não é alterado; Ferramentas > Amplitude... altera)"
 # round 162 (issue #9): Follow, quantize 50%, double Stop, rack pattern selector
 "Quick quantize (starts and lengths)" = "Quantização rápida (inícios e durações)"
 "Quantize starts" = "Quantizar inícios"

+ 216 - 17
src/core/Instruments.cpp

@@ -4,6 +4,7 @@
 #include "core/DirectWave.h"
 #include "core/FlSynths.h"
 #include "core/FlResampler.h"
+#include "core/TimeStretch.h"
 #include "core/FlVoiceProcessor.h"
 #include "core/DrumSynthLive.h"
 #include "core/Gms.h"
@@ -857,6 +858,11 @@ public:
         nextPorta = false;
     }
 
+    void pitchBend (float semitones) noexcept override
+    {
+        bendMul = std::isfinite (semitones) ? std::exp2 ((double) std::clamp (semitones, -48.0f, 48.0f) / 12.0) : 1.0;
+    }
+
     void setNextNoteExpression (float pan, float cents, bool porta) noexcept override
     {
         nextPan = pan;
@@ -1029,7 +1035,7 @@ public:
                     const auto& osc = settings.osc[2];
                     if (osc.level > 0.0f)
                     {
-                        const auto delta = std::min (0.49, v.phaseDelta * ratios[2] * v.pitchMul);
+                        const auto delta = std::min (0.49, v.phaseDelta * ratios[2] * v.pitchMul * bendMul);
                         modulator = osc.level * oscillator (osc.wave, v.phase[2], delta);
                         v.phase[2] += delta;
                         if (v.phase[2] >= 1.0)
@@ -1049,7 +1055,7 @@ public:
                     if (osc.level <= 0.0f)
                         continue;
 
-                    const auto delta = std::min (0.49, v.phaseDelta * ratios[o] * v.pitchMul);
+                    const auto delta = std::min (0.49, v.phaseDelta * ratios[o] * v.pitchMul * bendMul);
                     auto phase = v.phase[o];
                     if (fmShift != 0.0)
                     {
@@ -1350,6 +1356,7 @@ private:
     bool useBands = false;
     float toneCoeff[2] { 0.025f, 0.155f };
     float nextPan = 0.0f, nextCents = 0.0f;
+    double bendMul = 1.0; // the pitch wheel (round 162), audio thread
     bool nextPorta = false;
     Noise noise;
     std::array<SynthVoice, 16> voices {};
@@ -1493,8 +1500,11 @@ public:
         release.store (std::clamp (s.release, 0.0f, 4.0f));
         reverse.store (s.reverse);
         cut.store (s.cut);
-        // with "keep pitch" the sample arrives already stretched (RackState): play at normal speed
+        // with "keep pitch" the sample arrives already stretched (RackState): play at normal speed; the keys then
+        // transpose it through each voice's pitch shifter (round 162), so its length stays
         stretchBeats.store (s.keepPitch ? 0.0f : std::max (0.0f, s.stretchBeats));
+        keyShift.store (! clipMode && s.keepPitch && s.stretchBeats > 0.0f);
+        keyShiftMethod.store ((int) s.stretchMethod);
         regionStart.store (std::clamp (s.regionStart, 0.0f, 1.0f));
         regionEnd.store (std::clamp (s.regionEnd, 0.0f, 1.0f));
         porta.store (s.porta);
@@ -1585,7 +1595,6 @@ public:
             last = index + 1 < slices.count ? std::max (first, std::floor (total * slices.starts[(std::size_t) index + 1]) - 1.0) : total;
             sliced = true;
         }
-        const auto stretched = stretchBeats.load() > 0.0f;
         const auto base = baseRate (*data);
         auto startFrame = first + std::max (0.0, startSeconds * sampleRate * base);
         if (clipMode) // FL starts an audio clip on a whole source frame (round 110 §1: ceil or round fit, floor is 1 late)
@@ -1617,10 +1626,12 @@ public:
                     target = &v;
         }
 
-        // with stretch (FL's "resample") the note pitch does not change the speed
+        // with stretch in FL's Resample mode the note still transposes: pitch and length change together, like tape
+        // (FL's manual: "playing a chord plays each note at a different speed"; round 162, issue #9 — LYNX played every
+        // key at the sample's own pitch and ignored the pitch knob and the fine tune)
         const auto root = zone != nullptr ? zone->zone.rootKey : 60;
         const auto tune = zone != nullptr ? (double) zone->zone.tune : 0.0;
-        const auto semitones = clipMode || stretched ? 0.0 : (sliced ? 0 : key - root) + tune + (double) pitch.load() + nextCents / 100.0;
+        const auto semitones = clipMode ? 0.0 : (sliced ? 0 : key - root) + tune + (double) pitch.load() + nextCents / 100.0;
         const auto attackSeconds = clipMode ? 0.0f : attack.load();
 
         *target = {};
@@ -1649,14 +1660,14 @@ public:
         target->fadeOut = nextClip[3];
         nextClip = {};
         // FL's read speed: the rate ratio at the original pitch, else exp(cents·ln2/1200)·ratio
-        target->rate = stretched ? base : flrs::speedFor ((float) (semitones * 100.0), base);
+        target->rate = flrs::speedFor ((float) (semitones * 100.0), base);
         target->reversed = backwards;
         // the render quality is read at each note (round 160): the export engine prepares its instruments before it
         // switches to offline rendering, so a value taken in prepare() kept the playback quality in most exports
         target->kernel.setQuality (offlineRendering() ? flrs::renderQuality : quality);
 
         // portamento (channel porta, inverted by the note): starts from the previous note's pitch
-        if (! clipMode && (porta.load() != nextPorta) && lastRate != 0.0 && ! stretched)
+        if (! clipMode && (porta.load() != nextPorta) && lastRate != 0.0)
         {
             const auto steps = (int) (portaBeats.load() * 60.0 / bpm.load() * sampleRate);
             const auto from = std::abs (lastRate);
@@ -1719,10 +1730,170 @@ public:
         for (std::size_t i = 0; i < lfoTargets; ++i)
             if (lfos[i].active && ! clipMode)
                 lfos[i].start (target->lfo[i], songTicks);
+        startKeyShift (*target, semitones, base);
         nextPan = nextCents = 0.0f;
         nextPorta = false;
     }
 
+    // stretch keeping pitch (round 162): each voice's pitch shifter and its source cursor
+    static constexpr int keyShiftBlock = 128;
+    static constexpr int keyShiftVoices = 8; // shifters per channel (more transposed voices at once play resampled)
+    struct KeyShift
+    {
+        VoicePitchShifter* shifter = nullptr;       // from the pool (prepareKeyShift), while the voice is shifted
+        bool on = false;
+        bool primed = false;                        // primed in render (a few per block: it costs ~1 ms each)
+        SamplerVoice ahead;                         // reads the source primeLength() frames ahead of the voice
+        double factor = 1.0, glideRatio = 1.0;      // the note's transposition and its portamento / slide
+        int glideLeft = 0;
+        std::array<float, keyShiftBlock * 4 + 2> inL {}, inR {}; // the source read `factor` times faster
+        std::array<float, keyShiftBlock> outL {}, outR {};
+        double owed = 0.0; // input frames owed to the shifter (n / factor per block, the fraction carried)
+        int pos = 0, count = 0;
+    };
+    // Stretch keeping pitch (round 162, issue #9): a transposed note reads the stretched sample at its own speed, so it
+    // lasts as long as at the root key, and its pitch shifter transposes it (FL's Stretch modes: "pitch changes keep
+    // the original duration", the voices of a chord stay in sync). The root key plays the stretched sample untouched.
+    void startKeyShift (SamplerVoice& v, double semitones, double base) noexcept
+    {
+        const auto index = (std::size_t) (&v - voices.data());
+        auto& ks = shifts[index];
+        ks.on = false;
+        ks.shifter = nullptr;
+        if (! keyShift.load (std::memory_order_relaxed) || std::abs (semitones) < 1.0e-6)
+        {
+            if (keyShift.load (std::memory_order_relaxed))
+                lastFactor = 1.0;
+            return;
+        }
+        // a free shifter: none, or its voice ended or plays unshifted
+        for (std::size_t k = 0; k < shifterPool.size() && ks.shifter == nullptr; ++k)
+        {
+            if (shifterPool[k] == nullptr || ! shifterPool[k]->ready() || primeL.size() < (std::size_t) shifterPool[k]->maxPrimeLength())
+                continue;
+            const auto user = shifterUser[k];
+            if (user < 0 || (std::size_t) user == index || ! voices[(std::size_t) user].active || shifts[(std::size_t) user].shifter != shifterPool[k].get())
+            {
+                shifterUser[k] = (int) index;
+                ks.shifter = shifterPool[k].get();
+            }
+        }
+        if (ks.shifter == nullptr)
+            return; // every shifter busy: this voice plays resampled
+
+        ks.on = true;
+        ks.factor = std::exp2 (semitones / 12.0);
+        ks.glideLeft = 0;
+        // portamento: the transposition glides from the previous note's (its length stays)
+        if (v.glideLeft > 0 && lastFactor > 0.0)
+        {
+            ks.glideRatio = std::pow (ks.factor / lastFactor, 1.0 / v.glideLeft);
+            ks.glideLeft = v.glideLeft;
+            ks.factor = lastFactor;
+        }
+        lastFactor = std::exp2 (semitones / 12.0);
+        v.rate = base;
+        v.glideLeft = 0;
+        lastRate = base;
+
+        ks.ahead = v;
+        ks.ahead.pitchMul = 1.0;
+        ks.primed = false;
+        ks.pos = ks.count = 0;
+        ks.owed = 0.0;
+    }
+
+    // the transposition the shifter applies now: the note's (gliding), the pitch envelope / LFO and the pitch wheel
+    double shiftFactor (const SamplerVoice& v, const KeyShift& ks) const noexcept
+    {
+        return std::clamp (ks.factor * v.pitchMul * bendMul, VoicePitchShifter::minFactor, VoicePitchShifter::maxFactor);
+    }
+
+    // Primes a new shifted voice's shifter with the start of its source (so its output has no latency). A render
+    // primes at most two (about 1 ms each); a voice waiting for its turn starts one block later. Exports prime all.
+    bool primeKeyShift (const SamplerVoice& v, KeyShift& ks, int& budget) noexcept
+    {
+        if (ks.primed)
+            return true;
+        if (budget <= 0)
+            return false;
+        --budget;
+        const auto factor = shiftFactor (v, ks);
+        const auto prime = std::min (ks.shifter->primeLength (factor), (int) primeL.size());
+        for (int i = 0; i < prime; ++i)
+            readAhead (v, ks, factor, primeL[(std::size_t) i], primeR[(std::size_t) i]);
+        ks.shifter->start (primeL.data(), primeR.data(), prime);
+        ks.primed = true;
+        return true;
+    }
+
+    // the shifter's source: the stretched sample read at the voice's (untransposed) speed, ahead of the voice
+    void readAhead (const SamplerVoice& v, KeyShift& ks, double factor, float& outL, float& outR) noexcept
+    {
+        auto& a = ks.ahead;
+        const auto lastFrame = std::min ((double) (a.data->frames() - 1), a.last);
+        const auto looping = a.loopEnd > a.loopStart && (! a.loopWhileHeld || v.stage < 3);
+        if (looping && a.position >= a.loopEnd)
+        {
+            while (a.position >= a.loopEnd)
+                a.position = (a.position - a.loopEnd) + a.loopStart;
+            ++a.loops;
+        }
+        if ((double) flrs::wholeFrames (a.position) > lastFrame || a.position < a.first)
+            outL = outR = 0.0f;
+        else
+            readFrame (a, a.data->leftData(), a.data->rightData(), lastFrame, looping, outL, outR, a.rate * factor);
+        a.position += a.rate * factor;
+    }
+
+    // the next frame of a shifted voice (blocks of keyShiftBlock through its shifter): the transposition, its glide,
+    // the pitch envelope / LFO (pitchMul) and the pitch wheel change the shift, never the speed
+    void readShifted (const SamplerVoice& v, KeyShift& ks, float& outL, float& outR) noexcept
+    {
+        if (ks.pos >= ks.count)
+        {
+            if (ks.glideLeft > 0)
+            {
+                const auto k = std::min (keyShiftBlock, ks.glideLeft);
+                ks.factor *= std::pow (ks.glideRatio, (double) k);
+                ks.glideLeft -= k;
+            }
+            const auto factor = shiftFactor (v, ks);
+            ks.owed += keyShiftBlock / factor;
+            const auto frames = std::min ((int) ks.inL.size(), (int) ks.owed);
+            ks.owed -= frames;
+            for (int i = 0; i < frames; ++i)
+                readAhead (v, ks, factor, ks.inL[(std::size_t) i], ks.inR[(std::size_t) i]);
+            ks.shifter->process (ks.inL.data(), ks.inR.data(), frames, ks.outL.data(), ks.outR.data(), keyShiftBlock);
+            ks.pos = 0;
+            ks.count = keyShiftBlock;
+        }
+        outL = ks.outL[(std::size_t) ks.pos];
+        outR = ks.outR[(std::size_t) ks.pos];
+        ++ks.pos;
+    }
+
+    void prepareKeyShift (double sr) override
+    {
+        if (clipMode || ! keyShift.load())
+            return;
+        const auto method = (StretchMethod) keyShiftMethod.load();
+        for (auto& shifter : shifterPool)
+        {
+            shifter = std::make_unique<VoicePitchShifter>();
+            shifter->allocate (sr, method);
+        }
+        shifterUser.fill (-1);
+        const auto prime = (std::size_t) shifterPool[0]->maxPrimeLength();
+        primeL.assign (prime, 0.0f);
+        primeR.assign (prime, 0.0f);
+    }
+
+    void pitchBend (float semitones) noexcept override
+    {
+        bendMul = clipMode || ! std::isfinite (semitones) ? 1.0 : std::exp2 ((double) std::clamp (semitones, -48.0f, 48.0f) / 12.0);
+    }
+
     void setNextNoteExpression (float pan, float cents, bool porta) noexcept override
     {
         nextPan = pan;
@@ -1752,7 +1923,15 @@ public:
         for (auto& v : voices)
             if (v.active && ! v.stolen && v.rate != 0.0 && v.data != nullptr)
             {
-                const auto target = flrs::speedFor ((float) (semitones * 100.0), v.data->sampleRate * (1.0 / sampleRate));
+                // a shifted voice (stretch keeping pitch) glides its transposition, not its speed (round 162)
+                if (auto& ks = shifts[(std::size_t) (&v - voices.data())]; ks.on)
+                {
+                    ks.glideRatio = std::pow (std::exp2 (semitones / 12.0) / ks.factor, 1.0 / steps);
+                    ks.glideLeft = steps;
+                    continue;
+                }
+                // (with stretch in Resample mode the speed is relative to the stretched one)
+                const auto target = flrs::speedFor ((float) (semitones * 100.0), baseRate (*v.data));
                 v.glideRatio = std::pow (target / v.rate, 1.0 / steps);
                 v.glideLeft = steps;
             }
@@ -1847,9 +2026,11 @@ public:
             if (std::fabs (chanPan) < 1.0e-6f)
                 chanPan = 0.0f;
             const flvp::X87Extended precision;
+            auto primeBudget = offlineRendering() ? keyShiftVoices : 2;
             for (auto& v : voices)
                 if (v.active && v.data != nullptr && v.flVoice)
-                    renderFl (v, left, right, n, chanVol, chanPan);
+                    if (auto& ks = shifts[(std::size_t) (&v - voices.data())]; ! ks.on || primeKeyShift (v, ks, primeBudget))
+                        renderFl (v, left, right, n, chanVol, chanPan);
         }
 
         // the Gain knob scales the other voices with the channel's gains (FL voices apply it in renderFl)
@@ -1875,10 +2056,13 @@ public:
 
         const flvp::X87Extended precision; // FL's interpolators run the x87 unit at 64-bit precision
 
+        auto primeBudget = offlineRendering() ? keyShiftVoices : 2;
         for (auto& v : voices)
         {
             if (! v.active || v.data == nullptr || v.flVoice)
                 continue;
+            if (auto& ks = shifts[(std::size_t) (&v - voices.data())]; ks.on && ! primeKeyShift (v, ks, primeBudget))
+                continue; // (its shifter primes in the next block)
 
             const auto* l = v.data->leftData();
             const auto* r = v.data->rightData();
@@ -1919,7 +2103,11 @@ public:
                 }
 
                 float sl, sr;
-                readFrame (v, l, r, lastFrame, looping, sl, sr);
+                auto& ks = shifts[(std::size_t) (&v - voices.data())];
+                if (ks.on)
+                    readShifted (v, ks, sl, sr);
+                else
+                    readFrame (v, l, r, lastFrame, looping, sl, sr, v.rate * v.pitchMul * bendMul);
 
                 if (modulated)
                 {
@@ -1986,7 +2174,7 @@ public:
                 left[i] += sl * gain * gl * v.panL;
                 right[i] += sr * gain * gr * v.panR;
 
-                v.position += v.rate * v.pitchMul;
+                v.position += ks.on ? v.rate : v.rate * v.pitchMul * bendMul;
                 ++v.age;
 
                 if (v.glideLeft > 0)
@@ -2170,7 +2358,10 @@ public:
                 }
                 else
                 {
-                    readFrame (v, l, r, lastFrame, looping, sl, sr);
+                    if (auto& ks = shifts[(std::size_t) (&v - voices.data())]; ks.on)
+                        readShifted (v, ks, sl, sr);
+                    else
+                        readFrame (v, l, r, lastFrame, looping, sl, sr, v.rate * v.pitchMul * bendMul);
                     v.holdL = sl, v.holdR = sr;
                 }
                 auto gain = v.fade * v.zoneGain * outGain.load (std::memory_order_relaxed);
@@ -2196,7 +2387,7 @@ public:
                     endGain *= table[(std::size_t) v.offFade++];
                 }
                 flG[(std::size_t) m] = gain * endGain;
-                v.position += v.rate * v.pitchMul;
+                v.position += shifts[(std::size_t) (&v - voices.data())].on ? v.rate : v.rate * v.pitchMul * bendMul;
                 ++v.age;
                 if (v.glideLeft > 0)
                 {
@@ -2252,9 +2443,9 @@ public:
     // FL's sampler read (REA_rodada69 §1): the kernel's taps around the read position, frames outside the region
     // repeating its first/last frame, taps past the loop end wrapping to the loop start (and, once the voice has
     // looped, taps before the loop start wrapping to its end), a reversed voice reading the region backwards
-    void readFrame (SamplerVoice& v, const float* l, const float* r, double lastFrame, bool looping, float& outL, float& outR) noexcept
+    void readFrame (SamplerVoice& v, const float* l, const float* r, double lastFrame, bool looping, float& outL, float& outR, double speed) noexcept
     {
-        v.kernel.setSpeed (v.rate * v.pitchMul);
+        v.kernel.setSpeed (speed);
         const auto half = v.kernel.halfWidth;
         const auto whole = flrs::wholeFrames (v.position);
         const auto frac = v.position - (double) whole;
@@ -2263,7 +2454,7 @@ public:
         const auto loopStart = (std::int64_t) v.loopStart, loopEnd = (std::int64_t) v.loopEnd;
 
         // at the original rate on a whole frame FL copies the sample instead of interpolating
-        if (v.rate * v.pitchMul == 1.0 && frac == 0.0)
+        if (speed == 1.0 && frac == 0.0)
         {
             const auto q = v.reversed ? first + last - whole : whole;
             outL = l[q];
@@ -2307,6 +2498,14 @@ private:
     double sampleRate = 44100.0;
     float fadeStep = 0.01f;
     std::array<SamplerVoice, 16> voices {};
+    std::array<KeyShift, 16> shifts {};
+    std::array<std::unique_ptr<VoicePitchShifter>, keyShiftVoices> shifterPool;
+    std::array<int, keyShiftVoices> shifterUser {}; // the voice using each shifter (-1 = none)
+    std::vector<float> primeL, primeR;
+    std::atomic<bool> keyShift { false };
+    std::atomic<int> keyShiftMethod { 0 };
+    double lastFactor = 1.0; // the last shifted note's transposition (where its portamento starts)
+    double bendMul = 1.0;    // the pitch wheel (pitchBend), audio thread
     int quality = flrs::liveQuality;        // FL's interpolation index (prepare)
     std::array<float, 1024> tapL {}, tapR {}; // kernel taps gathered near the region edges and loops
     static constexpr int flBlock = 512;       // FL voices: longest segment rendered at once

+ 10 - 0
src/core/Instruments.h

@@ -36,6 +36,16 @@ public:
     virtual void noteOffPort (int /*port*/, int key, int midiChannel) noexcept { noteOffMidi (key, midiChannel); }
     // A short MIDI message (program change, bank select…) for the next block. Only hosted plugins take it.
     virtual void midiMessage (const std::uint8_t* /*bytes*/, int /*size*/) noexcept {}
+    // A MIDI controller's pitch wheel on this channel (round 162, issue #9), already scaled by the channel's pitch
+    // range (FL's Misc › Pitch range, ±2 semitones by default): every sounding and new note is bent by `semitones`
+    // until the wheel moves again. Audio thread. Hosted plugins get the raw MIDI pitch bend instead (midiMessage).
+    virtual void pitchBend (float /*semitones*/) noexcept {}
+    // A controller (CC 0…127, `value` 0..1) played live into a native instrument (the mod wheel is CC 1). Ignored by
+    // engines without MIDI controllers; hosted plugins get the raw message (midiMessage).
+    virtual void controlChange (int /*cc*/, float /*value*/) noexcept {}
+    // UI thread, before the instrument reaches the audio thread: a sampler whose sample is stretched keeping its pitch
+    // allocates its per-voice pitch shifters (keys transpose it without changing its length, like FL's Stretch modes).
+    virtual void prepareKeyShift (double /*sampleRate*/) {}
 
     // Adds (does not overwrite) n samples into left/right, with the gains already applied.
     virtual void render (float* left, float* right, int n, float gainLeft, float gainRight) noexcept = 0;

+ 1 - 1
src/core/MidiImport.cpp

@@ -210,7 +210,7 @@ bool importInto (Song& song, const ParsedMidi& midi, const Options& o, const std
             c.instrument = InstrumentKind::synth;
             c.volume = 0.6f;
         }
-        c.mixerTrack = o.mixerTracks ? song.nextFreeInsert() : 0;
+        c.mixerTrack = o.mixerTracks ? song.claimFreeInsert() : 0;
         song.channels.push_back (c);
         const auto channel = (int) song.channels.size() - 1;
         for (auto n : g.notes)

+ 20 - 0
src/core/MixerOps.cpp

@@ -200,6 +200,26 @@ std::vector<int> linkChannels (Song& song, const std::vector<int>& channels, int
     return result;
 }
 
+int ensureInsert (Song& song, int track)
+{
+    track = std::clamp (track, 0, maxMixerTracks - 1);
+    if (song.mixer.empty())
+        song.mixer = Song::defaultMixer (0);
+    while ((int) song.mixer.size() <= track)
+    {
+        const auto name = "Insert " + std::to_string (song.mixer.size());
+        song.mixer.emplace_back().name = name;
+    }
+    return track;
+}
+
+void routeChannel (Song& song, int channel, int track)
+{
+    if (channel < 0 || channel >= (int) song.channels.size())
+        return;
+    song.channels[(std::size_t) channel].mixerTrack = ensureInsert (song, track);
+}
+
 std::vector<int> channelsOn (const Song& song, int track)
 {
     std::vector<int> out;

+ 6 - 0
src/core/MixerOps.h

@@ -35,6 +35,12 @@ std::vector<int> linkChannels (Song& song, const std::vector<int>& channels, int
 // Channels whose output goes to `track`.
 std::vector<int> channelsOn (const Song& song, int track);
 
+// Makes sure insert `track` exists, adding default inserts ("Insert n") up to it — like FL, a channel can be routed to
+// any insert, not only to those the mixer shows yet (round 162, issue #9). Returns `track` clamped to 0..maxMixerTracks-1.
+int ensureInsert (Song& song, int track);
+// Routes channel `channel` to insert `track` (0 = Master), adding the inserts up to it when needed.
+void routeChannel (Song& song, int channel, int track);
+
 // Sends each source insert to `to` (0 = Master). `only`: the source stops sending anywhere else. Sources
 // that would make a cycle are skipped. Returns how many were routed.
 int routeTo (Song& song, const std::vector<int>& sources, int to, bool only);

+ 63 - 0
src/core/RackRenderer.cpp

@@ -191,6 +191,8 @@ std::shared_ptr<Instrument> buildInstrument (const ChannelDef& channel, double s
     std::shared_ptr<Instrument> instrument = makeInstrument (kind, std::move (sample));
     instrument->prepare (sampleRate);
     instrument->configure (channel, bpm);
+    if (kind == InstrumentKind::sampler && channel.sampler.keepPitch && channel.sampler.stretchBeats > 0.0f)
+        instrument->prepareKeyShift (sampleRate); // the keys transpose the stretched sample (round 162)
     return instrument;
 }
 
@@ -1872,6 +1874,12 @@ void RackRenderer::process (const RackState* state,
             if (e.channel < 0 || e.channel >= (int) state->instruments.size())
                 continue;
 
+            if (e.type == NoteEvent::Type::control)
+            {
+                applyLiveControl (*state, e);
+                continue;
+            }
+
             auto& instrument = *state->instruments[(std::size_t) e.channel];
 
             auto& held = heldNotes[(std::size_t) std::min (e.channel, maxChannels - 1)];
@@ -1910,6 +1918,15 @@ void RackRenderer::process (const RackState* state,
                     if (const auto limit = voiceLimitFor (def[(std::size_t) e.channel].maxPolyphony, def[(std::size_t) e.channel].synth.mono); limit > 0)
                         if (const auto victim = order.noteOn (e.key, limit); victim >= 0)
                             instrument.noteOff (victim);
+                // a played note is tuned like the song's notes: Misc › Fine tune and the main pitch (round 162, issue
+                // #9: the fine tune did nothing to the keys played live)
+                if ((std::size_t) e.channel < def.size())
+                {
+                    const auto& misc = def[(std::size_t) e.channel].misc;
+                    const auto cents = misc.fineTune + (misc.mainPitch ? state->song.masterPitch : 0.0f);
+                    if (cents != 0.0f)
+                        instrument.setNextNoteExpression (0.0f, cents, false);
+                }
                 instrument.noteOn (e.key, e.velocity);
                 if (inRange)
                     held[(std::size_t) (e.key >> 6)] |= bit;
@@ -2005,6 +2022,52 @@ double RackRenderer::ticksToNextEvent (const RackState& state, double tick) noex
     return best;
 }
 
+void RackRenderer::applyLiveControl (const RackState& state, const NoteEvent& e) noexcept
+{
+    // a controller's pitch wheel, mod wheel (CC 1), other CCs and channel pressure played into a channel (round 162,
+    // issue #9): hosted plugins (and the SoundFont player) get the MIDI message; LYNX's own instruments bend by the
+    // channel's pitch range (FL's Misc › Pitch range, default ±2 semitones) and take the CCs they know
+    auto* instrument = state.instruments[(std::size_t) e.channel].get();
+    if (instrument == nullptr)
+        return;
+
+    std::uint8_t m[3] {};
+    auto size = 3;
+    const auto value = std::isfinite (e.velocity) ? e.velocity : 0.0f;
+    if (e.key == NoteEvent::controlPitchBend)
+    {
+        const auto v = (int) std::clamp (std::lround ((double) (value + 1.0f) * 8192.0), 0L, 16383L);
+        m[0] = 0xE0, m[1] = (std::uint8_t) (v & 127), m[2] = (std::uint8_t) (v >> 7);
+    }
+    else if (e.key == NoteEvent::controlPressure)
+    {
+        m[0] = 0xD0, m[1] = (std::uint8_t) std::clamp (std::lround (value * 127.0f), 0L, 127L);
+        size = 2;
+    }
+    else if (e.key >= 0 && e.key < 128)
+        m[0] = 0xB0, m[1] = (std::uint8_t) e.key, m[2] = (std::uint8_t) std::clamp (std::lround (value * 127.0f), 0L, 127L);
+    else
+        return;
+
+    instrument->midiMessage (m, size);
+    if (e.key == NoteEvent::controlPitchBend)
+    {
+        const auto range = (std::size_t) e.channel < state.song.channels.size() ? state.song.channels[(std::size_t) e.channel].misc.pitchRange : 2;
+        instrument->pitchBend (std::clamp (value, -1.0f, 1.0f) * (float) std::max (0, range));
+    }
+    else if (e.key < 128)
+        instrument->controlChange (e.key, std::clamp (value, 0.0f, 1.0f));
+
+    // a MIDI Out channel: the plugins on its port get it on its MIDI channel
+    if (state.hasMidiRoutes && (std::size_t) e.channel < state.midiTargets.size())
+        for (const auto t : state.midiTargets[(std::size_t) e.channel])
+            if (t >= 0 && t < (int) state.instruments.size() && state.instruments[(std::size_t) t] != nullptr)
+            {
+                std::uint8_t routed[3] { (std::uint8_t) ((m[0] & 0xF0) | (state.midiOutChannel[(std::size_t) e.channel] & 15)), m[1], m[2] };
+                state.instruments[(std::size_t) t]->midiMessage (routed, size);
+            }
+}
+
 bool RackRenderer::routesLiveNotes (const RackState& state, int channel) const noexcept
 {
     const auto c = (std::size_t) channel;

+ 1 - 0
src/core/RackRenderer.h

@@ -347,6 +347,7 @@ private:
     int liveRoutedCount = 0;
     double liveTick = 0.0;
     bool routesLiveNotes (const RackState& state, int channel) const noexcept;
+    void applyLiveControl (const RackState& state, const NoteEvent& e) noexcept;
     WorkerPool* workers = nullptr;
     LiveInput* liveInput = nullptr;
     HardwareOutput* hwOut = nullptr;

+ 4 - 1
src/core/Scheduler.h

@@ -18,7 +18,10 @@ constexpr int audioClipKey (std::size_t playlistIndex) noexcept { return audioCl
 
 struct NoteEvent
 {
-    enum class Type : std::uint8_t { on, off, slide, choke }; // slide: glides the sounding notes to `key`; choke: cut group
+    enum class Type : std::uint8_t { on, off, slide, choke, control }; // slide: glides the sounding notes to `key`; choke: cut group
+    // control (live MIDI input only, round 162): `key` = CC number 0…127, controlPitchBend or controlPressure;
+    // `velocity` = the value, 0..1 (the pitch wheel: -1..1, 0 = centre)
+    static constexpr int controlPitchBend = 128, controlPressure = 129;
 
     int sampleOffset = 0;   // 0 .. numSamples-1 within the block
     Type type = Type::on;

+ 11 - 0
src/core/Song.cpp

@@ -157,6 +157,17 @@ int Song::nextFreeInsert() const
     return 0; // every insert is in use: Master (FL's default for a new channel)
 }
 
+int Song::claimFreeInsert()
+{
+    if (const auto t = nextFreeInsert(); t > 0)
+        return t;
+    if (mixer.empty() || (int) mixer.size() >= maxMixerTracks)
+        return 0;
+    const auto name = "Insert " + std::to_string (mixer.size());
+    mixer.emplace_back().name = name;
+    return (int) mixer.size() - 1;
+}
+
 void Song::removeChannel (int index)
 {
     if (index < 0 || index >= (int) channels.size())

+ 3 - 0
src/core/Song.h

@@ -1130,6 +1130,9 @@ struct Song
     static std::vector<MixerTrack> defaultMixer (int inserts = 16);
     // First insert nothing uses (no channel, send, effect, input, Playlist link or custom name); 0 (Master) if none.
     int nextFreeInsert() const;
+    // nextFreeInsert, but when every insert is in use a new one is added (up to maxMixerTracks), like FL where a new
+    // channel takes the next of its 125 inserts (round 162); 0 (Master) only when the mixer is full.
+    int claimFreeInsert();
 
     // End of the last clip, rounded up to a bar (minimum 1 bar; with no clip, the current pattern's length).
     std::int64_t songLength() const noexcept;

+ 48 - 0
src/core/TimeStretch.cpp

@@ -423,6 +423,54 @@ std::shared_ptr<SampleData> stretchWith (const SampleData& in, double ratio, con
 }
 } // namespace stretchdetail
 
+struct VoicePitchShifter::Impl
+{
+    signalsmith::stretch::SignalsmithStretch<float> stretch { 162L }; // fixed seed: renders repeat exactly
+};
+
+VoicePitchShifter::VoicePitchShifter() = default;
+VoicePitchShifter::~VoicePitchShifter() = default;
+
+void VoicePitchShifter::allocate (double sampleRate, StretchMethod method)
+{
+    impl = std::make_unique<Impl>();
+    // real time (measured round 162): 100 ms blocks, overlap 4 — pitch exact to 0.05 cent down to 55 Hz, about 2 % of a
+    // core per voice and ~1 ms to prime a note (60 ms blocks missed low notes by 40 cents); drums: 80 ms
+    const auto block = method == StretchMethod::transients ? 0.08 : 0.1;
+    impl->stretch.configure (2, (int) std::lround (sampleRate * block), (int) std::lround (sampleRate * block / 4.0));
+    // one silent run sizes every internal buffer, so the audio thread's first note does not allocate
+    std::vector<float> silence ((std::size_t) std::max (maxPrimeLength(), 1024), 0.0f), out (256, 0.0f);
+    start (silence.data(), silence.data(), primeLength (1.5));
+    process (silence.data(), silence.data(), 85, out.data(), out.data() + 128, 128);
+}
+
+bool VoicePitchShifter::ready() const noexcept { return impl != nullptr; }
+
+int VoicePitchShifter::primeLength (double factor) const noexcept
+{
+    if (impl == nullptr)
+        return 0;
+    factor = std::clamp (factor, minFactor, maxFactor);
+    return impl->stretch.outputSeekLength ((float) (1.0 / factor)) + 1;
+}
+
+void VoicePitchShifter::start (const float* l, const float* r, int frames) noexcept
+{
+    if (impl == nullptr)
+        return;
+    const float* in[2] { l, r };
+    impl->stretch.outputSeek (in, frames);
+}
+
+void VoicePitchShifter::process (const float* inL, const float* inR, int inFrames, float* outL, float* outR, int outFrames) noexcept
+{
+    if (impl == nullptr)
+        return;
+    const float* in[2] { inL, inR };
+    float* out[2] { outL, outR };
+    impl->stretch.process (in, inFrames, out, outFrames);
+}
+
 StretchMethod stretchMethodForFlMode (int flMode) noexcept
 {
     switch (flMode)

+ 30 - 0
src/core/TimeStretch.h

@@ -22,6 +22,36 @@ std::shared_ptr<SampleData> timeStretch (const SampleData& input, double ratio,
 // pitch-preserving stretch; callers keep resampling for it.
 StretchMethod stretchMethodForFlMode (int flMode) noexcept;
 
+// A voice's real-time pitch shifter (round 162, issue #9: a sampler stretched keeping its pitch follows the keys like
+// FL's Stretch modes — the note transposes the sample and its length stays). The voice reads its source `factor` times
+// faster (an exact transposition, as a resampler) and Signalsmith Stretch stretches that back to the source's length
+// (pitch kept): measured within 0.05 cent from 55 Hz up, where Signalsmith's own spectral transposition was off by up to
+// 30 cents with real-time block sizes. The voice primes it with primeLength (factor) input frames when the note starts,
+// so the output is aligned with the source (no latency); then each process() takes the input frames read for n output
+// frames (n / factor of them, accumulated by the caller). allocate() on the UI thread; start()/process() never allocate.
+class VoicePitchShifter
+{
+public:
+    static constexpr double minFactor = 0.25, maxFactor = 4.0; // ±2 octaves
+
+    VoicePitchShifter();
+    ~VoicePitchShifter();
+    VoicePitchShifter (const VoicePitchShifter&) = delete;
+    VoicePitchShifter& operator= (const VoicePitchShifter&) = delete;
+
+    void allocate (double sampleRate, StretchMethod method);
+    bool ready() const noexcept;
+    int primeLength (double factor) const noexcept;
+    int maxPrimeLength() const noexcept { return primeLength (minFactor); } // the longest: the lowest transposition
+    // `l`/`r` (r may equal l): primeLength (factor) frames of the transposed source from the note's start
+    void start (const float* l, const float* r, int frames) noexcept;
+    void process (const float* inL, const float* inR, int inFrames, float* outL, float* outR, int outFrames) noexcept;
+
+private:
+    struct Impl;
+    std::unique_ptr<Impl> impl;
+};
+
 namespace stretchdetail
 {
 // Exposed for the tests: attack positions (frames) found in a mono signal, strongest-first filtering already applied.

+ 59 - 4
src/engine/AudioEngine.cpp

@@ -285,6 +285,7 @@ void AudioEngine::buildInitialGraph()
     auto previewOwned = std::make_unique<PreviewPlayer>();
     preview = previewOwned.get();
     preview->setVolume (controls::previewGain (previewVolume));
+    preview->setEditorVolume (controls::previewGain (editorVolume));
     auto previewNode = graph.addNode (std::move (previewOwned));
 
     for (int channel = 0; channel < 2; ++channel)
@@ -683,10 +684,17 @@ int AudioEngine::snapInputKey (int key) const noexcept
     return scale > 0 ? notetools::snapToScale (key, inputScaleRoot.load(), scale) : key;
 }
 
-void AudioEngine::previewSample (std::shared_ptr<const SampleData> sample, bool loop, float gain)
+void AudioEngine::previewSample (std::shared_ptr<const SampleData> sample, bool loop, float gain, bool audioEditor)
 {
     if (preview != nullptr)
-        preview->play (std::move (sample), loop, gain);
+        preview->play (std::move (sample), loop, gain, audioEditor);
+}
+
+void AudioEngine::setEditorVolume (double volume) noexcept
+{
+    editorVolume = controls::clampUtilityVolume (volume);
+    if (preview != nullptr)
+        preview->setEditorVolume (controls::previewGain (editorVolume));
 }
 
 void AudioEngine::setPreviewVolume (double volume) noexcept
@@ -982,7 +990,7 @@ void AudioEngine::finishAudioTake()
             c.name = m.name;
             c.instrument = InstrumentKind::audioClip;
             c.samplePath = m.path;
-            c.mixerTrack = m.track > 0 && m.track < (int) s.mixer.size() ? m.track : s.nextFreeInsert();
+            c.mixerTrack = m.track > 0 && m.track < (int) s.mixer.size() ? m.track : s.claimFreeInsert();
             s.channels.push_back (c);
 
             for (const auto& part : parts)
@@ -1147,9 +1155,48 @@ void AudioEngine::handleIncomingMidiMessage (juce::MidiInput* source, const juce
     if (device.midiChannel != 0 && m.getChannel() != device.midiChannel)
         return;
 
+    // the pitch wheel, the mod wheel and the other controllers play into the channels the notes go to (round 162,
+    // issue #9: they did nothing unless linked): hosted plugins get the MIDI message, LYNX's instruments bend by the
+    // channel's pitch range. A CC with a MIDI link only moves its parameter.
+    auto playControl = [this, &device] (int control, float value) {
+        NoteEvent e;
+        e.type = NoteEvent::Type::control;
+        e.key = control;
+        e.velocity = value;
+        const auto extra = juce::jlimit (0, maxLiveChannels, extraLiveCount.load());
+        if (device.target >= 0)
+        {
+            e.channel = device.target;
+            midiEvents.push (e);
+            return;
+        }
+        for (int i = -1; i < extra; ++i)
+        {
+            e.channel = i < 0 ? liveChannel.load() : extraLiveChannels[(std::size_t) i].load();
+            if (e.channel >= 0)
+                midiEvents.push (e);
+        }
+    };
+
     if (m.isController())
     {
-        controllerQueue.push ({ m.getControllerNumber(), m.getChannel() - 1, m.getControllerValue() });
+        const auto cc = m.getControllerNumber();
+        controllerQueue.push ({ cc, m.getChannel() - 1, m.getControllerValue() });
+        // (120…127 are channel mode messages: All Sound / Notes Off, reset…)
+        if (cc >= 0 && cc < 120 && (linkedCcs[(std::size_t) (cc >> 6)].load (std::memory_order_relaxed) >> (cc & 63) & 1u) == 0)
+            playControl (cc, (float) m.getControllerValue() / 127.0f);
+        return;
+    }
+
+    if (m.isPitchWheel())
+    {
+        playControl (NoteEvent::controlPitchBend, juce::jlimit (-1.0f, 1.0f, (float) (m.getPitchWheelValue() - 8192) / 8192.0f));
+        return;
+    }
+
+    if (m.isChannelPressure())
+    {
+        playControl (NoteEvent::controlPressure, (float) m.getChannelPressureValue() / 127.0f);
         return;
     }
 
@@ -2341,6 +2388,14 @@ void AudioEngine::applyControllers()
     }
 
     const auto links = linksWithGlobal (song.midiLinks, getGlobalLinks());
+    {
+        std::uint64_t bits[2] {};
+        for (const auto& link : links)
+            if (link.cc >= 0 && link.cc < 128)
+                bits[link.cc >> 6] |= std::uint64_t { 1 } << (link.cc & 63);
+        for (int w = 0; w < 2; ++w)
+            linkedCcs[(std::size_t) w].store (bits[w], std::memory_order_relaxed);
+    }
     if (latest.empty() || links.empty())
         return;
 

+ 8 - 1
src/engine/AudioEngine.h

@@ -149,7 +149,10 @@ public:
     void setPreviewVolume (double volume) noexcept;
     double getPreviewVolume() const noexcept { return previewVolume; }
     // Plays a sound made in memory through the same preview output (the start-up sound). UI thread.
-    void previewSample (std::shared_ptr<const SampleData> sample, bool loop = false, float gain = 0.8f);
+    void previewSample (std::shared_ptr<const SampleData> sample, bool loop = false, float gain = 0.8f, bool audioEditor = false);
+    // The Audio Editor's playback volume (its toolbar knob, round 162): 0..1.25 like the Browser's, 1 = unity.
+    void setEditorVolume (double volume) noexcept;
+    double getEditorVolume() const noexcept { return editorVolume; }
     // The frame of the previewed sample being played, -1 when the preview is silent.
     double getPreviewPosition() const noexcept;
 
@@ -483,6 +486,7 @@ private:
     double lastLogMs = 0.0;
     class PreviewPlayer* preview = nullptr; // graph node (owned by the graph)
     double previewVolume = 1.0;
+    double editorVolume = 1.0;
     void publishSong();
     void adoptPrepared (PreparedFlp& prepared); // the plugins of a prepared import found / substituted (message thread)
 
@@ -506,6 +510,9 @@ private:
         int cc = 0, channel = 0, value = 0;
     };
     SpscQueue<ControllerEvent, 1024> controllerQueue; // MIDI thread -> UI (controllers)
+    // the CCs a MIDI link (project or global) uses: they move their parameter and are not also played into the
+    // selected channel's instrument (round 162); written by applyControllers, read by the MIDI thread
+    std::array<std::atomic<std::uint64_t>, 2> linkedCcs {};
     AutomationTarget learning;
     bool learningGlobal = false;
     bool saveGlobalLinks = false; // only the engine with an audio device (the app) writes the settings file

+ 48 - 15
src/engine/Exporter.cpp

@@ -4,6 +4,7 @@
 #include "core/Mp3Encoder.h"
 #include "engine/AudioEngine.h"
 #include "engine/Text.h"
+#include "ui/I18n.h"
 
 namespace lynx
 {
@@ -53,6 +54,33 @@ double tailSecondsFor (const Options& o)
     return o.tail == TailMode::cut ? 0.0 : o.maxTailSeconds;
 }
 
+// Round 162 (issue #9: exporting over a file of the same name failed): the file is written next to the target under a
+// temporary name and replaces it only once complete, so a cancelled or failed export leaves the old file as it was. A
+// target another program holds open (a media player, a preview) is reported before the render starts.
+bool canReplace (const juce::File& target)
+{
+    if (! target.exists())
+        return true;
+    if (target.isDirectory())
+        return false;
+    juce::FileOutputStream probe (target); // opening for writing fails while another program holds it (Windows)
+    return probe.openedOk();
+}
+
+juce::String inUseMessage (const juce::File& target)
+{
+    return tr ("\"{0}\" is in use by another program (a media player, or a preview). Close it, or export with another name.",
+               { target.getFileName() });
+}
+
+bool replaceTarget (juce::TemporaryFile& temp, const juce::File& target, juce::String& error)
+{
+    if (temp.overwriteTargetFileWithTemporary())
+        return true;
+    error = canReplace (target) ? tr ("could not replace {0}", { target.getFullPathName() }) : inUseMessage (target);
+    return false;
+}
+
 int oggQualityIndex (juce::AudioFormat& format, int kbps)
 {
     const auto options = format.getQualityOptions();
@@ -107,6 +135,13 @@ bool ExportJob::run (const std::function<bool (double)>& progress, juce::String&
     render.hqAllPlugins = options.hqAllPlugins;
     render.pluginWarmUp = options.pluginWarmUp;
 
+    for (const auto f : formatsOf (options))
+        if (const auto file = f == options.format ? output : output.withFileExtension (extension (f)); ! canReplace (file))
+        {
+            error = inUseMessage (file);
+            return false;
+        }
+
     // streaming straight into the file when nothing needs the whole render first (the classic WAV/FLAC/OGG export)
     const auto streaming = options.format != exportopts::Format::mp3 && options.channels == Channels::stereo && ! options.normalize
                            && options.tail == TailMode::leave && formatsOf (options).size() == 1;
@@ -150,12 +185,12 @@ bool ExportJob::run (const std::function<bool (double)>& progress, juce::String&
     else
         format = std::make_unique<juce::WavAudioFormat>();
 
-    output.deleteFile();
-    auto stream = output.createOutputStream();
+    juce::TemporaryFile temp (output);
+    auto stream = temp.getFile().createOutputStream();
 
     if (stream == nullptr)
     {
-        error = utf8 ("could not create ") + output.getFullPathName();
+        error = tr ("could not create {0}", { output.getFullPathName() });
         return false;
     }
 
@@ -177,12 +212,11 @@ bool ExportJob::run (const std::function<bool (double)>& progress, juce::String&
 
     if (peak < 0.0f)
     {
-        output.deleteFile();
-        error = "cancelled";
+        error = "cancelled"; // (the temporary file goes away; an older file of that name stays)
         return false;
     }
 
-    return true;
+    return replaceTarget (temp, output, error);
 }
 
 bool ExportJob::writeBuffered (std::vector<float>& left, std::vector<float>& right, juce::String& error)
@@ -243,7 +277,7 @@ bool ExportJob::writeBuffered (std::vector<float>& left, std::vector<float>& rig
 
 bool ExportJob::writeFile (const Options& o, const juce::File& file, float* left, float* right, int frames, int channels, juce::String& error)
 {
-    file.deleteFile();
+    juce::TemporaryFile temp (file);
 
     if (o.format == exportopts::Format::mp3)
     {
@@ -262,12 +296,12 @@ bool ExportJob::writeFile (const Options& o, const juce::File& file, float* left
             encoder.encode (left + done, right + done, juce::jmin (block, frames - done), bytes);
         encoder.finish (bytes);
 
-        if (! file.replaceWithData (bytes.data(), bytes.size()))
+        if (! temp.getFile().replaceWithData (bytes.data(), bytes.size()))
         {
-            error = utf8 ("could not create ") + file.getFullPathName();
+            error = tr ("could not create {0}", { file.getFullPathName() });
             return false;
         }
-        return true;
+        return replaceTarget (temp, file, error);
     }
 
     std::unique_ptr<juce::AudioFormat> format;
@@ -285,10 +319,10 @@ bool ExportJob::writeFile (const Options& o, const juce::File& file, float* left
     else
         format = std::make_unique<juce::WavAudioFormat>();
 
-    auto stream = file.createOutputStream();
+    auto stream = temp.getFile().createOutputStream();
     if (stream == nullptr)
     {
-        error = utf8 ("could not create ") + file.getFullPathName();
+        error = tr ("could not create {0}", { file.getFullPathName() });
         return false;
     }
 
@@ -308,10 +342,9 @@ bool ExportJob::writeFile (const Options& o, const juce::File& file, float* left
 
     if (! ok)
     {
-        file.deleteFile();
-        error = utf8 ("could not write ") + file.getFullPathName();
+        error = tr ("could not write {0}", { file.getFullPathName() });
         return false;
     }
-    return true;
+    return replaceTarget (temp, file, error);
 }
 } // namespace lynx

+ 1 - 1
src/engine/MidiFileIo.cpp

@@ -122,7 +122,7 @@ std::optional<Song> loadSong (const juce::File& file, juce::String& error)
         ChannelDef c;
         c.name = parsed->names[t].empty() ? "MIDI " + std::to_string (t + 1) : parsed->names[t];
         c.instrument = InstrumentKind::synth;
-        c.mixerTrack = song.nextFreeInsert();
+        c.mixerTrack = song.claimFreeInsert();
         c.volume = 0.6f;
         song.channels.push_back (c);
 

+ 5 - 2
src/engine/PreviewPlayer.cpp

@@ -10,7 +10,7 @@ PreviewPlayer::PreviewPlayer()
     kernel.setQuality (flrs::liveQuality);
 }
 
-void PreviewPlayer::play (std::shared_ptr<const SampleData> sample, bool loop, float newGain)
+void PreviewPlayer::play (std::shared_ptr<const SampleData> sample, bool loop, float newGain, bool forEditor)
 {
     std::shared_ptr<const SampleData> old;
     {
@@ -20,6 +20,7 @@ void PreviewPlayer::play (std::shared_ptr<const SampleData> sample, bool loop, f
         pendingLoop = loop;
         pendingGain = newGain;
         pendingBrowser = false;
+        pendingEditor = forEditor;
         hasPending = true;
     }
 }
@@ -34,6 +35,7 @@ void PreviewPlayer::playBrowser (std::shared_ptr<const SampleData> sample, float
         pendingLoop = false;
         pendingGain = newGain;
         pendingBrowser = true;
+        pendingEditor = false;
         hasPending = true;
     }
 }
@@ -53,6 +55,7 @@ void PreviewPlayer::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBu
             looping = pendingLoop;
             gain = pendingGain;
             browser = pendingBrowser;
+            editor = pendingEditor;
             position = 0.0;
         }
     }
@@ -69,7 +72,7 @@ void PreviewPlayer::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBu
     const auto rate = current->sampleRate / deviceRate;
     auto* outL = buffer.getWritePointer (0);
     auto* outR = buffer.getWritePointer (1);
-    const auto level = gain * (browser ? volume.load (std::memory_order_relaxed) : 1.0f);
+    const auto level = gain * (browser ? volume.load (std::memory_order_relaxed) : editor ? editorVolume.load (std::memory_order_relaxed) : 1.0f);
 
     // FL's sampler interpolation (24-point sinc, round 160) instead of the linear read it had: a 44.1 kHz file on a
     // 48 kHz device lost 1.5 dB at 10 kHz and had an image at −21 dBc (13.9 kHz) in the Browser and editor previews

+ 6 - 2
src/engine/PreviewPlayer.h

@@ -16,11 +16,14 @@ public:
     PreviewPlayer();
 
     // UI thread. nullptr to stop. `loop`: plays it over and over (the audio editor's loop playback).
-    void play (std::shared_ptr<const SampleData> sample, bool loop = false, float gain = 0.8f);
+    // `editor`: the Audio Editor's playback, whose level also follows its volume knob (setEditorVolume) while it plays.
+    void play (std::shared_ptr<const SampleData> sample, bool loop = false, float gain = 0.8f, bool editor = false);
     // A Browser preview: like play(), and the level also follows the preview volume (setVolume) while it plays.
     void playBrowser (std::shared_ptr<const SampleData> sample, float gain = 0.8f);
     // The Browser's preview volume as a gain (core/ValueMath.h previewGain). Any thread; applies at once.
     void setVolume (float gainFactor) noexcept { volume.store (gainFactor, std::memory_order_relaxed); }
+    // The Audio Editor's playback volume as a gain (round 162, issue #9). Any thread; applies at once.
+    void setEditorVolume (float gainFactor) noexcept { editorVolume.store (gainFactor, std::memory_order_relaxed); }
     // Any thread: the frame being played in the current sample, or -1 when nothing plays.
     double getPlayPosition() const noexcept { return playPosition.load (std::memory_order_relaxed); }
 
@@ -49,7 +52,8 @@ private:
     bool pendingLoop = false, looping = false;
     float pendingGain = 0.8f, gain = 0.8f;
     bool pendingBrowser = false, browser = false;
-    std::atomic<float> volume { 1.0f };
+    bool pendingEditor = false, editor = false;
+    std::atomic<float> volume { 1.0f }, editorVolume { 1.0f };
     std::atomic<double> playPosition { -1.0 };
     std::shared_ptr<const SampleData> current;  // touched only by the audio thread
     std::shared_ptr<const SampleData> retired;  // released by the UI (audio never deallocates)

+ 8 - 0
src/engine/ProjectFile.cpp

@@ -1,5 +1,7 @@
 #include "engine/ProjectFile.h"
 
+#include "core/MixerOps.h"
+
 #include <algorithm>
 
 #include "core/Effects.h"
@@ -1930,6 +1932,12 @@ std::optional<Song> fromJson (const juce::String& json, juce::String& error)
     if (song.mixer.empty()) // projects from before the mixer existed
         song.mixer = Song::defaultMixer();
 
+    // a channel routed past the saved mixer's last insert (round 162): the inserts up to it exist, so it is not mixed
+    // into the last one
+    for (const auto& c : song.channels)
+        if (c.mixerTrack >= (int) song.mixer.size())
+            mixerops::ensureInsert (song, c.mixerTrack);
+
     // 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(),

+ 32 - 4
src/ui/AudioEditor.cpp

@@ -6,7 +6,10 @@
 
 #include "core/AudioCapture.h"
 #include "core/WaveAnalysis.h"
+#include "core/ValueMath.h"
 #include "engine/Text.h"
+#include "ui/HintBar.h"
+#include "ui/ValueControl.h"
 #include "ui/I18n.h"
 #include "ui/Icons.h"
 #include "ui/Theme.h"
@@ -246,6 +249,21 @@ public:
             if (playing)
                 startPlayback();
         };
+        // the playback volume (round 162, issue #9): how loud the editor plays, without changing the audio (the
+        // Amp... tool changes the audio itself); remembered with the settings
+        volumeKnob.setRange (0.0, controls::maxUtilityVolume, 0.01);
+        volumeKnob.setDoubleClickReturnValue (true, 1.0);
+        volumeKnob.textFromValueFunction = [] (double v) { return ui::utilityVolumeText (v); };
+        volumeKnob.valueFromTextFunction = [] (const juce::String& t) { return t.retainCharacters ("0123456789.").getDoubleValue() / 100.0; };
+        volumeKnob.setMouseClickGrabsKeyboardFocus (false);
+        volumeKnob.setWantsKeyboardFocus (false);
+        volumeKnob.setValue (engine.getEditorVolume(), juce::dontSendNotification);
+        volumeKnob.onValueChange = [this] {
+            engine.setEditorVolume (volumeKnob.getValue());
+            refreshVolumeKnob();
+        };
+        addAndMakeVisible (volumeKnob);
+        refreshVolumeKnob();
         setupText (viewButton, tr ("Spectrum"), tr ("Show the waveform or the spectrogram") + " (S)");
         viewButton.setClickingTogglesState (true);
         viewButton.setColour (juce::TextButton::buttonOnColourId, theme::colour::accent);
@@ -387,7 +405,8 @@ public:
         place (recordMenuButton, 18, 6);
         place (playButton, 30);
         place (stopButton, 30, 6);
-        place (loopButton, 54, 12);
+        place (loopButton, 54, 4);
+        place (volumeKnob, 26, 12);
         place (viewButton, 76, 6);
         place (snapButton, 28);
         place (fitButton, 28);
@@ -1601,7 +1620,7 @@ private:
         data->left = audio.channels[0];
         if (audio.numChannels() > 1)
             data->right = audio.channels[1];
-        engine.previewSample (data, loop, 1.0f);
+        engine.previewSample (data, loop, 1.0f, true); // (at the volume knob's level)
         playing = true;
         playFrom = from;
         playStartedMs = juce::Time::getMillisecondCounterHiRes();
@@ -1955,7 +1974,7 @@ private:
             c.name = name;
             c.instrument = InstrumentKind::audioClip;
             c.samplePath = path;
-            c.mixerTrack = s.nextFreeInsert();
+            c.mixerTrack = s.claimFreeInsert();
             s.channels.push_back (c);
             Clip clip;
             clip.kind = ClipKind::audio;
@@ -1986,7 +2005,7 @@ private:
             c.name = name;
             c.instrument = InstrumentKind::sampler;
             c.samplePath = path;
-            c.mixerTrack = s.nextFreeInsert();
+            c.mixerTrack = s.claimFreeInsert();
             s.channels.push_back (c);
             index = (int) s.channels.size() - 1;
         });
@@ -2238,8 +2257,17 @@ private:
         playButton { "play", Icon::play }, stopButton { "stop", Icon::stop }, snapButton { "snap", Icon::magnet }, fitButton { "fit", Icon::zoom },
         saveButton { "save", Icon::save };
     juce::TextButton loopButton, viewButton, sendButton;
+    juce::Slider volumeKnob { juce::Slider::RotaryHorizontalVerticalDrag, juce::Slider::NoTextBox };
     juce::OwnedArray<juce::TextButton> quickButtons;
 
+    void refreshVolumeKnob()
+    {
+        volumeKnob.setValue (engine.getEditorVolume(), juce::dontSendNotification);
+        const auto text = tr ("Playback volume: {0} (the audio is not changed; Tools > Amp... changes it)", { ui::utilityVolumeText (engine.getEditorVolume()) });
+        volumeKnob.setTooltip (text);
+        ui::setHint (volumeKnob, text + "  " + tr ("[LMB] drag  [Wheel] step  [2xLMB] 100%  [RMB] type a value"), true);
+    }
+
 };
 
 juce::PopupMenu AudioEditor::editMenu (bool contextual)

+ 2 - 2
src/ui/BrowserComponent.cpp

@@ -2784,7 +2784,7 @@ void BrowserComponent::openFile (const juce::File& file)
             c.name = name;
             c.instrument = InstrumentKind::sampler;
             c.samplePath = path;
-            c.mixerTrack = s.nextFreeInsert();
+            c.mixerTrack = s.claimFreeInsert();
             s.channels.push_back (c);
         });
         engine.setSelectedChannel ((int) engine.getSong().channels.size() - 1);
@@ -2809,7 +2809,7 @@ void BrowserComponent::openFile (const juce::File& file)
                 c.name = name;
                 c.instrument = InstrumentKind::synth;
                 c.synth = *synth;
-                c.mixerTrack = s.nextFreeInsert();
+                c.mixerTrack = s.claimFreeInsert();
                 s.channels.push_back (c);
             });
             engine.setSelectedChannel ((int) engine.getSong().channels.size() - 1);

+ 10 - 8
src/ui/ChannelRackComponent.cpp

@@ -4,6 +4,8 @@
 #include "ui/TextCache.h"
 #include "ui/ChannelRackComponent.h"
 
+#include "core/MixerOps.h"
+
 #include "ui/AutomationUi.h"
 #include "core/NoteTools.h"
 #include "core/FlSynths.h"
@@ -660,9 +662,9 @@ ui::ValueSpec ChannelRackComponent::specFor (Area area, int row) const
     // route box: the mixer insert, as a number
     ui::ValueSpec spec;
     spec.name = name + " - " + tr ("Target mixer track");
-    spec.maximum = juce::jmax (0, (int) engine.getSong().mixer.size() - 1);
+    spec.maximum = maxMixerTracks - 1; // every insert, like FL (round 162: the inserts past the mixer's last one are added)
     spec.interval = 1.0;
-    spec.pixelsForRange = 6.0 * juce::jmax (1.0, spec.maximum);
+    spec.pixelsForRange = 3.0 * juce::jmax (1.0, spec.maximum);
     spec.toText = [] (double v) { return v < 0.5 ? tr ("Master") : tr ("Insert {0}", { juce::String (juce::roundToInt (v)) }); };
     spec.fromText = [] (const juce::String& t) -> std::optional<double> {
         if (t.trim().startsWithIgnoreCase ("m"))
@@ -692,7 +694,7 @@ void ChannelRackComponent::setValue (Area area, int row, double value)
     {
         const auto track = juce::roundToInt (value);
         if (track != engine.getSong().channels[(std::size_t) row].mixerTrack)
-            engine.editSong ([row, track] (Song& s) { s.channels[(std::size_t) row].mixerTrack = track; });
+            engine.editSong ([row, track] (Song& s) { mixerops::routeChannel (s, row, track); });
     }
 }
 
@@ -1204,7 +1206,7 @@ int ChannelRackComponent::addChannel (ChannelDef c, bool openEditor)
     if (filterGroup >= 0 && c.group < 0) // a new channel joins the group shown (FL)
         c.group = filterGroup;
     engine.editSong ([&] (Song& s) {
-        c.mixerTrack = s.nextFreeInsert();
+        c.mixerTrack = s.claimFreeInsert();
         s.channels.push_back (c);
         index = (int) s.channels.size() - 1;
     });
@@ -1442,7 +1444,7 @@ void ChannelRackComponent::mouseDown (const juce::MouseEvent& e)
                 {
                     menu.addSeparator();
                     menu.addItem (tr ("Next free insert"), [this, row] {
-                        engine.editSong ([row] (Song& s) { s.channels[(std::size_t) row].mixerTrack = s.nextFreeInsert(); });
+                        engine.editSong ([row] (Song& s) { s.channels[(std::size_t) row].mixerTrack = s.claimFreeInsert(); });
                     });
                     menu.addItem (tr ("Send the selected channels to free inserts") + "\tCtrl+L", [this] { sendSelectedToInserts(); });
 
@@ -1667,7 +1669,7 @@ void ChannelRackComponent::mouseDoubleClick (const juce::MouseEvent& e)
         if (onOpenMixerInsert)
             onOpenMixerInsert (engine.getSong().channels[(std::size_t) row].mixerTrack);
         else
-            engine.editSong ([row] (Song& s) { s.channels[(std::size_t) row].mixerTrack = s.nextFreeInsert(); });
+            engine.editSong ([row] (Song& s) { s.channels[(std::size_t) row].mixerTrack = s.claimFreeInsert(); });
     }
     else if (area == Area::select && ! e.mods.isPopupMenu())
         selectAll();
@@ -2050,7 +2052,7 @@ void ChannelRackComponent::addInstrumentItems (juce::PopupMenu& menu, int insert
         if (filterGroup >= 0 && c.group < 0)
             c.group = filterGroup;
         engine.editSong ([&] (Song& s) {
-            c.mixerTrack = s.nextFreeInsert();
+            c.mixerTrack = s.claimFreeInsert();
             index = insertAt < 0 ? (s.channels.push_back (c), (int) s.channels.size() - 1) : insertChannel (s, insertAt, c);
         });
         selectionAnchor = index;
@@ -2161,7 +2163,7 @@ void ChannelRackComponent::assignSample (int channel, const juce::File& file)
 
     engine.editSong ([channel, path, name] (Song& s) {
         const auto isNew = channel < 0 || channel >= (int) s.channels.size();
-        const auto freeInsert = s.nextFreeInsert();
+        const auto freeInsert = s.claimFreeInsert();
         auto& c = isNew ? s.channels.emplace_back() : s.channels[(std::size_t) channel];
 
         if (isNew)

+ 4 - 2
src/ui/ChannelSettings.cpp

@@ -1,6 +1,8 @@
 #include "ui/WindowOwner.h"
 #include "ui/ChannelSettings.h"
 
+#include "core/MixerOps.h"
+
 #include <cstring>
 #include <functional>
 
@@ -3023,12 +3025,12 @@ public:
                     [] (Song& s, int ch, double v) { s.channels[(std::size_t) ch].misc.pitchRange = juce::jlimit (0, 48, juce::roundToInt (v)); },
                     TR_NOOP ("Range"));
         headerKnob ([this, name] {
-                        return countSpec (name() + " - " + tr ("Target mixer track"), juce::jmax (1, (int) engine.getSong().mixer.size() - 1), 0,
+                        return countSpec (name() + " - " + tr ("Target mixer track"), maxMixerTracks - 1, 0,
                                           [] (int t) { return t == 0 ? tr ("Master") : juce::String (t); });
                     },
                     [] (const ChannelDef& d) { return (double) d.mixerTrack; },
                     [] (Song& s, int ch, double v) {
-                        s.channels[(std::size_t) ch].mixerTrack = juce::jlimit (0, juce::jmax (0, (int) s.mixer.size() - 1), juce::roundToInt (v));
+                        mixerops::routeChannel (s, ch, juce::roundToInt (v)); // any insert (round 162): missing ones are added
                     },
                     TR_NOOP ("Track"));
 

+ 191 - 165
src/ui/I18nData.h

@@ -13674,171 +13674,197 @@ inline constexpr unsigned char ptBR[] = {
 105, 110, 115, 101, 114, 116, 32, 115, 101, 108, 101, 99, 105, 111, 110, 97, 100, 111, 44, 32, 111, 117, 32, 97,
 114, 114, 97, 115, 116, 101, 32, 112, 97, 114, 97, 32, 117, 109, 32, 105, 110, 115, 101, 114, 116, 32, 100, 111,
 32, 77, 105, 120, 101, 114, 32, 111, 117, 32, 117, 109, 32, 115, 108, 111, 116, 32, 100, 101, 32, 101, 102, 101,
-105, 116, 111, 34, 10, 10, 35, 32, 114, 111, 117, 110, 100, 32, 49, 54, 50, 32, 40, 105, 115, 115, 117, 101,
-32, 35, 57, 41, 58, 32, 70, 111, 108, 108, 111, 119, 44, 32, 113, 117, 97, 110, 116, 105, 122, 101, 32, 53,
-48, 37, 44, 32, 100, 111, 117, 98, 108, 101, 32, 83, 116, 111, 112, 44, 32, 114, 97, 99, 107, 32, 112, 97,
-116, 116, 101, 114, 110, 32, 115, 101, 108, 101, 99, 116, 111, 114, 10, 34, 81, 117, 105, 99, 107, 32, 113, 117,
-97, 110, 116, 105, 122, 101, 32, 40, 115, 116, 97, 114, 116, 115, 32, 97, 110, 100, 32, 108, 101, 110, 103, 116,
-104, 115, 41, 34, 32, 61, 32, 34, 81, 117, 97, 110, 116, 105, 122, 97, 195, 167, 195, 163, 111, 32, 114, 195,
-161, 112, 105, 100, 97, 32, 40, 105, 110, 195, 173, 99, 105, 111, 115, 32, 101, 32, 100, 117, 114, 97, 195, 167,
-195, 181, 101, 115, 41, 34, 10, 34, 81, 117, 97, 110, 116, 105, 122, 101, 32, 115, 116, 97, 114, 116, 115, 34,
-32, 61, 32, 34, 81, 117, 97, 110, 116, 105, 122, 97, 114, 32, 105, 110, 195, 173, 99, 105, 111, 115, 34, 10,
-34, 81, 117, 97, 110, 116, 105, 122, 101, 32, 115, 116, 97, 114, 116, 115, 32, 53, 48, 37, 32, 40, 104, 97,
-108, 102, 119, 97, 121, 32, 116, 111, 32, 116, 104, 101, 32, 103, 114, 105, 100, 41, 34, 32, 61, 32, 34, 81,
-117, 97, 110, 116, 105, 122, 97, 114, 32, 105, 110, 195, 173, 99, 105, 111, 115, 32, 53, 48, 37, 32, 40, 109,
-101, 116, 97, 100, 101, 32, 100, 111, 32, 99, 97, 109, 105, 110, 104, 111, 32, 97, 116, 195, 169, 32, 97, 32,
-103, 114, 97, 100, 101, 41, 34, 10, 34, 70, 111, 108, 108, 111, 119, 32, 116, 104, 101, 32, 112, 108, 97, 121,
-104, 101, 97, 100, 32, 40, 97, 117, 116, 111, 32, 115, 99, 114, 111, 108, 108, 105, 110, 103, 41, 32, 111, 110,
-47, 111, 102, 102, 34, 32, 61, 32, 34, 83, 101, 103, 117, 105, 114, 32, 111, 32, 99, 117, 114, 115, 111, 114,
-32, 100, 101, 32, 114, 101, 112, 114, 111, 100, 117, 195, 167, 195, 163, 111, 32, 40, 114, 111, 108, 97, 103, 101,
-109, 32, 97, 117, 116, 111, 109, 195, 161, 116, 105, 99, 97, 41, 32, 108, 105, 103, 97, 47, 100, 101, 115, 108,
-105, 103, 97, 34, 10, 34, 81, 117, 97, 110, 116, 105, 122, 101, 32, 116, 104, 101, 32, 99, 108, 105, 112, 115,
-39, 32, 115, 116, 97, 114, 116, 115, 34, 32, 61, 32, 34, 81, 117, 97, 110, 116, 105, 122, 97, 114, 32, 111,
-115, 32, 105, 110, 195, 173, 99, 105, 111, 115, 32, 100, 111, 115, 32, 99, 108, 105, 112, 101, 115, 34, 10, 34,
-81, 117, 97, 110, 116, 105, 122, 101, 32, 116, 104, 101, 32, 99, 108, 105, 112, 115, 39, 32, 115, 116, 97, 114,
-116, 115, 32, 53, 48, 37, 32, 40, 104, 97, 108, 102, 119, 97, 121, 32, 116, 111, 32, 116, 104, 101, 32, 103,
-114, 105, 100, 41, 34, 32, 61, 32, 34, 81, 117, 97, 110, 116, 105, 122, 97, 114, 32, 111, 115, 32, 105, 110,
-195, 173, 99, 105, 111, 115, 32, 100, 111, 115, 32, 99, 108, 105, 112, 101, 115, 32, 53, 48, 37, 32, 40, 109,
-101, 116, 97, 100, 101, 32, 100, 111, 32, 99, 97, 109, 105, 110, 104, 111, 32, 97, 116, 195, 169, 32, 97, 32,
-103, 114, 97, 100, 101, 41, 34, 10, 34, 83, 116, 111, 112, 32, 97, 110, 100, 32, 114, 101, 116, 117, 114, 110,
-32, 116, 111, 32, 116, 104, 101, 32, 115, 116, 97, 114, 116, 32, 40, 100, 111, 117, 98, 108, 101, 45, 99, 108,
-105, 99, 107, 58, 32, 98, 97, 99, 107, 32, 116, 111, 32, 116, 104, 101, 32, 115, 116, 97, 114, 116, 32, 97,
-110, 100, 32, 97, 108, 108, 32, 110, 111, 116, 101, 115, 32, 111, 102, 102, 41, 34, 32, 61, 32, 34, 80, 97,
-114, 97, 114, 32, 101, 32, 118, 111, 108, 116, 97, 114, 32, 97, 111, 32, 105, 110, 195, 173, 99, 105, 111, 32,
-40, 99, 108, 105, 113, 117, 101, 32, 100, 117, 112, 108, 111, 58, 32, 118, 111, 108, 116, 97, 32, 97, 111, 32,
-105, 110, 195, 173, 99, 105, 111, 32, 101, 32, 100, 101, 115, 108, 105, 103, 97, 32, 116, 111, 100, 97, 115, 32,
-97, 115, 32, 110, 111, 116, 97, 115, 41, 34, 10, 34, 80, 97, 116, 116, 101, 114, 110, 32, 116, 104, 101, 32,
-67, 104, 97, 110, 110, 101, 108, 32, 82, 97, 99, 107, 32, 101, 100, 105, 116, 115, 58, 32, 91, 76, 77, 66,
-93, 32, 111, 110, 32, 116, 104, 101, 32, 110, 97, 109, 101, 58, 32, 116, 104, 101, 32, 108, 105, 115, 116, 32,
-111, 102, 32, 112, 97, 116, 116, 101, 114, 110, 115, 44, 32, 111, 110, 32, 116, 104, 101, 32, 97, 114, 114, 111,
-119, 115, 58, 32, 112, 114, 101, 118, 105, 111, 117, 115, 32, 47, 32, 110, 101, 120, 116, 32, 32, 91, 87, 104,
-101, 101, 108, 93, 32, 115, 116, 101, 112, 34, 32, 61, 32, 34, 80, 97, 116, 116, 101, 114, 110, 32, 113, 117,
-101, 32, 111, 32, 67, 104, 97, 110, 110, 101, 108, 32, 82, 97, 99, 107, 32, 101, 100, 105, 116, 97, 58, 32,
-91, 76, 77, 66, 93, 32, 110, 111, 32, 110, 111, 109, 101, 58, 32, 97, 32, 108, 105, 115, 116, 97, 32, 100,
-101, 32, 112, 97, 116, 116, 101, 114, 110, 115, 44, 32, 110, 97, 115, 32, 115, 101, 116, 97, 115, 58, 32, 97,
-110, 116, 101, 114, 105, 111, 114, 32, 47, 32, 112, 114, 195, 179, 120, 105, 109, 111, 32, 32, 91, 87, 104, 101,
-101, 108, 93, 32, 112, 97, 115, 115, 111, 34, 10, 34, 66, 97, 99, 107, 32, 116, 111, 32, 116, 104, 101, 32,
-115, 116, 97, 114, 116, 34, 32, 61, 32, 34, 86, 111, 108, 116, 97, 114, 32, 97, 111, 32, 105, 110, 195, 173,
-99, 105, 111, 34, 10, 34, 70, 111, 108, 108, 111, 119, 32, 116, 104, 101, 32, 112, 108, 97, 121, 104, 101, 97,
-100, 32, 40, 97, 117, 116, 111, 32, 115, 99, 114, 111, 108, 108, 105, 110, 103, 41, 34, 32, 61, 32, 34, 83,
-101, 103, 117, 105, 114, 32, 111, 32, 99, 117, 114, 115, 111, 114, 32, 100, 101, 32, 114, 101, 112, 114, 111, 100,
-117, 195, 167, 195, 163, 111, 32, 40, 114, 111, 108, 97, 103, 101, 109, 32, 97, 117, 116, 111, 109, 195, 161, 116,
-105, 99, 97, 41, 34, 10, 34, 83, 116, 111, 112, 58, 32, 91, 76, 77, 66, 93, 32, 115, 116, 111, 112, 32,
-97, 110, 100, 32, 103, 111, 32, 98, 97, 99, 107, 32, 116, 111, 32, 116, 104, 101, 32, 115, 116, 97, 114, 116,
-32, 40, 111, 114, 32, 116, 104, 101, 32, 114, 101, 109, 101, 109, 98, 101, 114, 101, 100, 32, 112, 111, 115, 105,
-116, 105, 111, 110, 59, 32, 97, 103, 97, 105, 110, 58, 32, 116, 104, 101, 32, 115, 116, 97, 114, 116, 41, 32,
-32, 91, 50, 120, 76, 77, 66, 93, 32, 98, 97, 99, 107, 32, 116, 111, 32, 116, 104, 101, 32, 115, 116, 97,
-114, 116, 32, 97, 110, 100, 32, 97, 108, 108, 32, 110, 111, 116, 101, 115, 32, 111, 102, 102, 32, 40, 112, 97,
-110, 105, 99, 41, 32, 32, 91, 82, 77, 66, 93, 32, 114, 101, 109, 101, 109, 98, 101, 114, 32, 115, 101, 101,
-107, 32, 116, 105, 109, 101, 34, 32, 61, 32, 34, 80, 97, 114, 97, 114, 58, 32, 91, 76, 77, 66, 93, 32,
-112, 97, 114, 97, 32, 101, 32, 118, 111, 108, 116, 97, 32, 97, 111, 32, 105, 110, 195, 173, 99, 105, 111, 32,
-40, 111, 117, 32, 195, 160, 32, 112, 111, 115, 105, 195, 167, 195, 163, 111, 32, 108, 101, 109, 98, 114, 97, 100,
-97, 59, 32, 100, 101, 32, 110, 111, 118, 111, 58, 32, 111, 32, 105, 110, 195, 173, 99, 105, 111, 41, 32, 32,
-91, 50, 120, 76, 77, 66, 93, 32, 118, 111, 108, 116, 97, 32, 97, 111, 32, 105, 110, 195, 173, 99, 105, 111,
-32, 101, 32, 100, 101, 115, 108, 105, 103, 97, 32, 116, 111, 100, 97, 115, 32, 97, 115, 32, 110, 111, 116, 97,
-115, 32, 40, 112, 195, 162, 110, 105, 99, 111, 41, 32, 32, 91, 82, 77, 66, 93, 32, 108, 101, 109, 98, 114,
-97, 114, 32, 97, 32, 112, 111, 115, 105, 195, 167, 195, 163, 111, 34, 10, 34, 80, 108, 97, 121, 108, 105, 115,
-116, 32, 102, 111, 108, 108, 111, 119, 115, 32, 116, 104, 101, 32, 112, 108, 97, 121, 104, 101, 97, 100, 32, 40,
-97, 117, 116, 111, 32, 115, 99, 114, 111, 108, 108, 105, 110, 103, 41, 34, 32, 61, 32, 34, 65, 32, 80, 108,
-97, 121, 108, 105, 115, 116, 32, 115, 101, 103, 117, 101, 32, 111, 32, 99, 117, 114, 115, 111, 114, 32, 100, 101,
-32, 114, 101, 112, 114, 111, 100, 117, 195, 167, 195, 163, 111, 32, 40, 114, 111, 108, 97, 103, 101, 109, 32, 97,
-117, 116, 111, 109, 195, 161, 116, 105, 99, 97, 41, 34, 10, 34, 80, 105, 97, 110, 111, 32, 82, 111, 108, 108,
-32, 102, 111, 108, 108, 111, 119, 115, 32, 116, 104, 101, 32, 112, 108, 97, 121, 104, 101, 97, 100, 32, 40, 97,
-117, 116, 111, 32, 115, 99, 114, 111, 108, 108, 105, 110, 103, 41, 34, 32, 61, 32, 34, 79, 32, 80, 105, 97,
-110, 111, 32, 82, 111, 108, 108, 32, 115, 101, 103, 117, 101, 32, 111, 32, 99, 117, 114, 115, 111, 114, 32, 100,
-101, 32, 114, 101, 112, 114, 111, 100, 117, 195, 167, 195, 163, 111, 32, 40, 114, 111, 108, 97, 103, 101, 109, 32,
-97, 117, 116, 111, 109, 195, 161, 116, 105, 99, 97, 41, 34, 10, 34, 70, 111, 108, 108, 111, 119, 58, 32, 116,
-104, 101, 32, 80, 105, 97, 110, 111, 32, 82, 111, 108, 108, 32, 115, 99, 114, 111, 108, 108, 115, 32, 97, 108,
-111, 110, 103, 32, 119, 105, 116, 104, 32, 116, 104, 101, 32, 112, 108, 97, 121, 104, 101, 97, 100, 32, 119, 104,
-105, 108, 101, 32, 116, 104, 105, 115, 32, 112, 97, 116, 116, 101, 114, 110, 32, 112, 108, 97, 121, 115, 59, 32,
-111, 102, 102, 44, 32, 121, 111, 117, 32, 99, 97, 110, 32, 108, 111, 111, 107, 32, 97, 114, 111, 117, 110, 100,
-32, 102, 114, 101, 101, 108, 121, 34, 32, 61, 32, 34, 83, 101, 103, 117, 105, 114, 58, 32, 111, 32, 80, 105,
-97, 110, 111, 32, 82, 111, 108, 108, 32, 114, 111, 108, 97, 32, 106, 117, 110, 116, 111, 32, 99, 111, 109, 32,
-111, 32, 99, 117, 114, 115, 111, 114, 32, 101, 110, 113, 117, 97, 110, 116, 111, 32, 101, 115, 116, 101, 32, 112,
-97, 116, 116, 101, 114, 110, 32, 116, 111, 99, 97, 59, 32, 100, 101, 115, 108, 105, 103, 97, 100, 111, 44, 32,
-118, 111, 99, 195, 170, 32, 110, 97, 118, 101, 103, 97, 32, 108, 105, 118, 114, 101, 109, 101, 110, 116, 101, 34,
-10, 34, 70, 111, 108, 108, 111, 119, 32, 116, 104, 101, 32, 112, 108, 97, 121, 104, 101, 97, 100, 34, 32, 61,
-32, 34, 83, 101, 103, 117, 105, 114, 32, 111, 32, 99, 117, 114, 115, 111, 114, 32, 100, 101, 32, 114, 101, 112,
-114, 111, 100, 117, 195, 167, 195, 163, 111, 34, 10, 34, 81, 117, 97, 110, 116, 105, 115, 101, 32, 115, 116, 97,
-114, 116, 115, 32, 53, 48, 37, 32, 40, 104, 97, 108, 102, 119, 97, 121, 32, 116, 111, 32, 116, 104, 101, 32,
-103, 114, 105, 100, 41, 34, 32, 61, 32, 34, 81, 117, 97, 110, 116, 105, 122, 97, 114, 32, 105, 110, 195, 173,
-99, 105, 111, 115, 32, 53, 48, 37, 32, 40, 109, 101, 116, 97, 100, 101, 32, 100, 111, 32, 99, 97, 109, 105,
-110, 104, 111, 32, 97, 116, 195, 169, 32, 97, 32, 103, 114, 97, 100, 101, 41, 34, 10, 34, 81, 117, 105, 99,
-107, 32, 113, 117, 97, 110, 116, 105, 122, 101, 32, 53, 48, 37, 32, 40, 104, 97, 108, 102, 119, 97, 121, 32,
+105, 116, 111, 34, 10, 10, 35, 32, 45, 45, 45, 32, 82, 111, 117, 110, 100, 32, 49, 54, 50, 32, 40, 105,
+115, 115, 117, 101, 32, 35, 57, 41, 58, 32, 101, 120, 112, 111, 114, 116, 32, 111, 118, 101, 114, 32, 97, 110,
+32, 101, 120, 105, 115, 116, 105, 110, 103, 32, 102, 105, 108, 101, 44, 32, 65, 117, 100, 105, 111, 32, 69, 100,
+105, 116, 111, 114, 32, 112, 108, 97, 121, 98, 97, 99, 107, 32, 118, 111, 108, 117, 109, 101, 10, 34, 92, 34,
+123, 48, 125, 92, 34, 32, 105, 115, 32, 105, 110, 32, 117, 115, 101, 32, 98, 121, 32, 97, 110, 111, 116, 104,
+101, 114, 32, 112, 114, 111, 103, 114, 97, 109, 32, 40, 97, 32, 109, 101, 100, 105, 97, 32, 112, 108, 97, 121,
+101, 114, 44, 32, 111, 114, 32, 97, 32, 112, 114, 101, 118, 105, 101, 119, 41, 46, 32, 67, 108, 111, 115, 101,
+32, 105, 116, 44, 32, 111, 114, 32, 101, 120, 112, 111, 114, 116, 32, 119, 105, 116, 104, 32, 97, 110, 111, 116,
+104, 101, 114, 32, 110, 97, 109, 101, 46, 34, 32, 61, 32, 34, 92, 34, 123, 48, 125, 92, 34, 32, 101, 115,
+116, 195, 161, 32, 101, 109, 32, 117, 115, 111, 32, 112, 111, 114, 32, 111, 117, 116, 114, 111, 32, 112, 114, 111,
+103, 114, 97, 109, 97, 32, 40, 117, 109, 32, 112, 108, 97, 121, 101, 114, 32, 100, 101, 32, 109, 195, 173, 100,
+105, 97, 32, 111, 117, 32, 117, 109, 97, 32, 112, 114, 195, 169, 45, 101, 115, 99, 117, 116, 97, 41, 46, 32,
+70, 101, 99, 104, 101, 45, 111, 32, 111, 117, 32, 101, 120, 112, 111, 114, 116, 101, 32, 99, 111, 109, 32, 111,
+117, 116, 114, 111, 32, 110, 111, 109, 101, 46, 34, 10, 34, 99, 111, 117, 108, 100, 32, 110, 111, 116, 32, 114,
+101, 112, 108, 97, 99, 101, 32, 123, 48, 125, 34, 32, 61, 32, 34, 110, 195, 163, 111, 32, 102, 111, 105, 32,
+112, 111, 115, 115, 195, 173, 118, 101, 108, 32, 115, 117, 98, 115, 116, 105, 116, 117, 105, 114, 32, 123, 48, 125,
+34, 10, 34, 99, 111, 117, 108, 100, 32, 110, 111, 116, 32, 119, 114, 105, 116, 101, 32, 123, 48, 125, 34, 32,
+61, 32, 34, 110, 195, 163, 111, 32, 102, 111, 105, 32, 112, 111, 115, 115, 195, 173, 118, 101, 108, 32, 103, 114,
+97, 118, 97, 114, 32, 123, 48, 125, 34, 10, 34, 80, 108, 97, 121, 98, 97, 99, 107, 32, 118, 111, 108, 117,
+109, 101, 58, 32, 123, 48, 125, 32, 40, 116, 104, 101, 32, 97, 117, 100, 105, 111, 32, 105, 115, 32, 110, 111,
+116, 32, 99, 104, 97, 110, 103, 101, 100, 59, 32, 84, 111, 111, 108, 115, 32, 62, 32, 65, 109, 112, 46, 46,
+46, 32, 99, 104, 97, 110, 103, 101, 115, 32, 105, 116, 41, 34, 32, 61, 32, 34, 86, 111, 108, 117, 109, 101,
+32, 100, 101, 32, 114, 101, 112, 114, 111, 100, 117, 195, 167, 195, 163, 111, 58, 32, 123, 48, 125, 32, 40, 111,
+32, 195, 161, 117, 100, 105, 111, 32, 110, 195, 163, 111, 32, 195, 169, 32, 97, 108, 116, 101, 114, 97, 100, 111,
+59, 32, 70, 101, 114, 114, 97, 109, 101, 110, 116, 97, 115, 32, 62, 32, 65, 109, 112, 108, 105, 116, 117, 100,
+101, 46, 46, 46, 32, 97, 108, 116, 101, 114, 97, 41, 34, 10, 35, 32, 114, 111, 117, 110, 100, 32, 49, 54,
+50, 32, 40, 105, 115, 115, 117, 101, 32, 35, 57, 41, 58, 32, 70, 111, 108, 108, 111, 119, 44, 32, 113, 117,
+97, 110, 116, 105, 122, 101, 32, 53, 48, 37, 44, 32, 100, 111, 117, 98, 108, 101, 32, 83, 116, 111, 112, 44,
+32, 114, 97, 99, 107, 32, 112, 97, 116, 116, 101, 114, 110, 32, 115, 101, 108, 101, 99, 116, 111, 114, 10, 34,
+81, 117, 105, 99, 107, 32, 113, 117, 97, 110, 116, 105, 122, 101, 32, 40, 115, 116, 97, 114, 116, 115, 32, 97,
+110, 100, 32, 108, 101, 110, 103, 116, 104, 115, 41, 34, 32, 61, 32, 34, 81, 117, 97, 110, 116, 105, 122, 97,
+195, 167, 195, 163, 111, 32, 114, 195, 161, 112, 105, 100, 97, 32, 40, 105, 110, 195, 173, 99, 105, 111, 115, 32,
+101, 32, 100, 117, 114, 97, 195, 167, 195, 181, 101, 115, 41, 34, 10, 34, 81, 117, 97, 110, 116, 105, 122, 101,
+32, 115, 116, 97, 114, 116, 115, 34, 32, 61, 32, 34, 81, 117, 97, 110, 116, 105, 122, 97, 114, 32, 105, 110,
+195, 173, 99, 105, 111, 115, 34, 10, 34, 81, 117, 97, 110, 116, 105, 122, 101, 32, 115, 116, 97, 114, 116, 115,
+32, 53, 48, 37, 32, 40, 104, 97, 108, 102, 119, 97, 121, 32, 116, 111, 32, 116, 104, 101, 32, 103, 114, 105,
+100, 41, 34, 32, 61, 32, 34, 81, 117, 97, 110, 116, 105, 122, 97, 114, 32, 105, 110, 195, 173, 99, 105, 111,
+115, 32, 53, 48, 37, 32, 40, 109, 101, 116, 97, 100, 101, 32, 100, 111, 32, 99, 97, 109, 105, 110, 104, 111,
+32, 97, 116, 195, 169, 32, 97, 32, 103, 114, 97, 100, 101, 41, 34, 10, 34, 70, 111, 108, 108, 111, 119, 32,
+116, 104, 101, 32, 112, 108, 97, 121, 104, 101, 97, 100, 32, 40, 97, 117, 116, 111, 32, 115, 99, 114, 111, 108,
+108, 105, 110, 103, 41, 32, 111, 110, 47, 111, 102, 102, 34, 32, 61, 32, 34, 83, 101, 103, 117, 105, 114, 32,
+111, 32, 99, 117, 114, 115, 111, 114, 32, 100, 101, 32, 114, 101, 112, 114, 111, 100, 117, 195, 167, 195, 163, 111,
+32, 40, 114, 111, 108, 97, 103, 101, 109, 32, 97, 117, 116, 111, 109, 195, 161, 116, 105, 99, 97, 41, 32, 108,
+105, 103, 97, 47, 100, 101, 115, 108, 105, 103, 97, 34, 10, 34, 81, 117, 97, 110, 116, 105, 122, 101, 32, 116,
+104, 101, 32, 99, 108, 105, 112, 115, 39, 32, 115, 116, 97, 114, 116, 115, 34, 32, 61, 32, 34, 81, 117, 97,
+110, 116, 105, 122, 97, 114, 32, 111, 115, 32, 105, 110, 195, 173, 99, 105, 111, 115, 32, 100, 111, 115, 32, 99,
+108, 105, 112, 101, 115, 34, 10, 34, 81, 117, 97, 110, 116, 105, 122, 101, 32, 116, 104, 101, 32, 99, 108, 105,
+112, 115, 39, 32, 115, 116, 97, 114, 116, 115, 32, 53, 48, 37, 32, 40, 104, 97, 108, 102, 119, 97, 121, 32,
 116, 111, 32, 116, 104, 101, 32, 103, 114, 105, 100, 41, 34, 32, 61, 32, 34, 81, 117, 97, 110, 116, 105, 122,
-97, 195, 167, 195, 163, 111, 32, 114, 195, 161, 112, 105, 100, 97, 32, 53, 48, 37, 32, 40, 109, 101, 116, 97,
-100, 101, 32, 100, 111, 32, 99, 97, 109, 105, 110, 104, 111, 32, 97, 116, 195, 169, 32, 97, 32, 103, 114, 97,
-100, 101, 41, 34, 10, 34, 70, 111, 108, 108, 111, 119, 58, 32, 116, 104, 101, 32, 80, 108, 97, 121, 108, 105,
-115, 116, 32, 115, 99, 114, 111, 108, 108, 115, 32, 97, 108, 111, 110, 103, 32, 119, 105, 116, 104, 32, 116, 104,
-101, 32, 112, 108, 97, 121, 104, 101, 97, 100, 32, 119, 104, 105, 108, 101, 32, 116, 104, 101, 32, 115, 111, 110,
-103, 32, 112, 108, 97, 121, 115, 59, 32, 111, 102, 102, 44, 32, 121, 111, 117, 32, 99, 97, 110, 32, 108, 111,
-111, 107, 32, 97, 114, 111, 117, 110, 100, 32, 102, 114, 101, 101, 108, 121, 34, 32, 61, 32, 34, 83, 101, 103,
-117, 105, 114, 58, 32, 97, 32, 80, 108, 97, 121, 108, 105, 115, 116, 32, 114, 111, 108, 97, 32, 106, 117, 110,
-116, 111, 32, 99, 111, 109, 32, 111, 32, 99, 117, 114, 115, 111, 114, 32, 101, 110, 113, 117, 97, 110, 116, 111,
-32, 97, 32, 109, 195, 186, 115, 105, 99, 97, 32, 116, 111, 99, 97, 59, 32, 100, 101, 115, 108, 105, 103, 97,
-100, 111, 44, 32, 118, 111, 99, 195, 170, 32, 110, 97, 118, 101, 103, 97, 32, 108, 105, 118, 114, 101, 109, 101,
-110, 116, 101, 34, 10, 10, 35, 32, 114, 111, 117, 110, 100, 32, 49, 54, 50, 32, 40, 112, 97, 116, 99, 104,
-101, 114, 49, 54, 50, 41, 58, 32, 80, 97, 116, 99, 104, 101, 114, 32, 112, 108, 117, 103, 105, 110, 32, 108,
-105, 115, 116, 44, 32, 73, 47, 79, 32, 110, 111, 100, 101, 32, 77, 73, 68, 73, 32, 112, 111, 114, 116, 115,
-44, 32, 77, 73, 68, 73, 32, 79, 117, 116, 32, 105, 110, 115, 105, 100, 101, 32, 97, 32, 80, 97, 116, 99,
-104, 101, 114, 10, 34, 75, 101, 121, 98, 111, 97, 114, 100, 32, 67, 111, 110, 116, 114, 111, 108, 108, 101, 114,
-34, 32, 61, 32, 34, 67, 111, 110, 116, 114, 111, 108, 97, 100, 111, 114, 32, 100, 101, 32, 116, 101, 99, 108,
-97, 100, 111, 34, 10, 34, 79, 112, 101, 110, 32, 112, 108, 117, 103, 105, 110, 32, 112, 105, 99, 107, 101, 114,
-46, 46, 46, 34, 32, 61, 32, 34, 65, 98, 114, 105, 114, 32, 115, 101, 108, 101, 116, 111, 114, 32, 100, 101,
-32, 112, 108, 117, 103, 105, 110, 115, 46, 46, 46, 34, 10, 34, 77, 111, 114, 101, 46, 46, 46, 34, 32, 61,
-32, 34, 77, 97, 105, 115, 46, 46, 46, 34, 10, 34, 65, 100, 100, 32, 112, 108, 117, 103, 105, 110, 34, 32,
-61, 32, 34, 65, 100, 105, 99, 105, 111, 110, 97, 114, 32, 112, 108, 117, 103, 105, 110, 34, 10, 34, 65, 99,
-116, 105, 118, 97, 116, 101, 32, 97, 108, 108, 34, 32, 61, 32, 34, 65, 116, 105, 118, 97, 114, 32, 116, 111,
-100, 111, 115, 34, 10, 34, 68, 101, 97, 99, 116, 105, 118, 97, 116, 101, 32, 97, 108, 108, 34, 32, 61, 32,
-34, 68, 101, 115, 97, 116, 105, 118, 97, 114, 32, 116, 111, 100, 111, 115, 34, 10, 34, 68, 101, 97, 99, 116,
-105, 118, 97, 116, 101, 32, 117, 110, 117, 115, 101, 100, 34, 32, 61, 32, 34, 68, 101, 115, 97, 116, 105, 118,
-97, 114, 32, 111, 115, 32, 110, 195, 163, 111, 32, 117, 115, 97, 100, 111, 115, 34, 10, 34, 69, 118, 101, 110,
-116, 115, 34, 32, 61, 32, 34, 69, 118, 101, 110, 116, 111, 115, 34, 10, 34, 65, 100, 100, 32, 111, 110, 101,
-34, 32, 61, 32, 34, 65, 100, 105, 99, 105, 111, 110, 97, 114, 32, 117, 109, 34, 10, 34, 68, 105, 115, 99,
-111, 110, 110, 101, 99, 116, 32, 97, 108, 108, 34, 32, 61, 32, 34, 68, 101, 115, 99, 111, 110, 101, 99, 116,
-97, 114, 32, 116, 117, 100, 111, 34, 10, 34, 83, 121, 110, 116, 104, 32, 99, 108, 97, 115, 115, 105, 99, 34,
-32, 61, 32, 34, 83, 121, 110, 116, 104, 32, 99, 108, 195, 161, 115, 115, 105, 99, 111, 34, 10, 34, 83, 121,
-110, 116, 104, 32, 115, 112, 101, 99, 105, 97, 108, 34, 32, 61, 32, 34, 83, 121, 110, 116, 104, 32, 101, 115,
-112, 101, 99, 105, 97, 108, 34, 10, 34, 68, 101, 108, 97, 121, 32, 114, 101, 118, 101, 114, 98, 34, 32, 61,
-32, 34, 68, 101, 108, 97, 121, 32, 101, 32, 114, 101, 118, 101, 114, 98, 34, 10, 34, 77, 73, 68, 73, 32,
-111, 117, 116, 34, 32, 61, 32, 34, 83, 97, 195, 173, 100, 97, 32, 77, 73, 68, 73, 34, 10, 34, 73, 110,
-115, 105, 100, 101, 32, 97, 32, 80, 97, 116, 99, 104, 101, 114, 58, 32, 99, 97, 98, 108, 101, 32, 105, 116,
-115, 32, 92, 34, 77, 73, 68, 73, 32, 111, 117, 116, 92, 34, 32, 112, 105, 110, 32, 116, 111, 32, 97, 110,
-32, 105, 110, 115, 116, 114, 117, 109, 101, 110, 116, 32, 111, 114, 32, 116, 111, 32, 97, 32, 92, 34, 77, 73,
-68, 73, 32, 112, 111, 114, 116, 92, 34, 32, 112, 105, 110, 32, 111, 102, 32, 92, 34, 84, 111, 32, 76, 89,
-78, 88, 92, 34, 59, 32, 105, 116, 115, 32, 110, 111, 116, 101, 115, 32, 103, 111, 32, 111, 117, 116, 32, 111,
-110, 32, 116, 104, 105, 115, 32, 77, 73, 68, 73, 32, 99, 104, 97, 110, 110, 101, 108, 46, 34, 32, 61, 32,
-34, 68, 101, 110, 116, 114, 111, 32, 100, 101, 32, 117, 109, 32, 80, 97, 116, 99, 104, 101, 114, 58, 32, 108,
-105, 103, 117, 101, 32, 111, 32, 112, 105, 110, 111, 32, 92, 34, 83, 97, 195, 173, 100, 97, 32, 77, 73, 68,
-73, 92, 34, 32, 97, 32, 117, 109, 32, 105, 110, 115, 116, 114, 117, 109, 101, 110, 116, 111, 32, 111, 117, 32,
-97, 32, 117, 109, 32, 112, 105, 110, 111, 32, 92, 34, 80, 111, 114, 116, 97, 32, 77, 73, 68, 73, 92, 34,
-32, 100, 101, 32, 92, 34, 80, 97, 114, 97, 32, 111, 32, 76, 89, 78, 88, 92, 34, 59, 32, 97, 115, 32,
-110, 111, 116, 97, 115, 32, 115, 97, 101, 109, 32, 110, 101, 115, 116, 101, 32, 99, 97, 110, 97, 108, 32, 77,
-73, 68, 73, 46, 34, 10, 34, 66, 114, 111, 119, 115, 101, 32, 112, 108, 117, 103, 105, 110, 32, 100, 97, 116,
-97, 98, 97, 115, 101, 34, 32, 61, 32, 34, 80, 114, 111, 99, 117, 114, 97, 114, 32, 110, 111, 32, 98, 97,
-110, 99, 111, 32, 100, 101, 32, 112, 108, 117, 103, 105, 110, 115, 34, 10, 34, 123, 48, 125, 32, 40, 76, 89,
-78, 88, 41, 34, 32, 61, 32, 34, 123, 48, 125, 32, 40, 76, 89, 78, 88, 41, 34, 10, 34, 77, 101, 110,
-117, 32, 100, 101, 110, 115, 105, 116, 121, 34, 32, 61, 32, 34, 68, 101, 110, 115, 105, 100, 97, 100, 101, 32,
-100, 111, 115, 32, 109, 101, 110, 117, 115, 34, 10, 34, 67, 111, 109, 102, 111, 114, 116, 97, 98, 108, 101, 34,
-32, 61, 32, 34, 67, 111, 110, 102, 111, 114, 116, 195, 161, 118, 101, 108, 34, 10, 34, 84, 105, 109, 101, 32,
-115, 101, 108, 101, 99, 116, 105, 111, 110, 32, 32, 91, 65, 108, 116, 93, 32, 110, 111, 32, 115, 110, 97, 112,
-34, 32, 61, 32, 34, 83, 101, 108, 101, 195, 167, 195, 163, 111, 32, 100, 101, 32, 116, 101, 109, 112, 111, 32,
-32, 91, 65, 108, 116, 93, 32, 115, 101, 109, 32, 103, 114, 97, 100, 101, 34, 10, 34, 83, 111, 110, 103, 32,
-112, 111, 115, 105, 116, 105, 111, 110, 32, 32, 91, 65, 108, 116, 93, 32, 110, 111, 32, 115, 110, 97, 112, 34,
-32, 61, 32, 34, 80, 111, 115, 105, 195, 167, 195, 163, 111, 32, 110, 97, 32, 109, 195, 186, 115, 105, 99, 97,
-32, 32, 91, 65, 108, 116, 93, 32, 115, 101, 109, 32, 103, 114, 97, 100, 101, 34, 10, 34, 80, 111, 115, 105,
-116, 105, 111, 110, 32, 32, 91, 65, 108, 116, 93, 32, 110, 111, 32, 115, 110, 97, 112, 34, 32, 61, 32, 34,
-80, 111, 115, 105, 195, 167, 195, 163, 111, 32, 32, 91, 65, 108, 116, 93, 32, 115, 101, 109, 32, 103, 114, 97,
-100, 101, 34, 10, 34, 73, 110, 115, 101, 114, 116, 32, 69, 81, 32, 40, 102, 111, 108, 100, 101, 100, 41, 58,
-32, 91, 76, 77, 66, 93, 32, 117, 110, 102, 111, 108, 100, 32, 105, 116, 34, 32, 61, 32, 34, 69, 81, 32,
-100, 111, 32, 105, 110, 115, 101, 114, 116, 32, 40, 114, 101, 99, 111, 108, 104, 105, 100, 111, 41, 58, 32, 91,
-76, 77, 66, 93, 32, 97, 98, 114, 101, 34, 10, 34, 73, 110, 115, 101, 114, 116, 32, 69, 81, 58, 32, 91,
-76, 77, 66, 93, 32, 102, 111, 108, 100, 32, 105, 116, 32, 40, 105, 116, 32, 102, 111, 108, 100, 115, 32, 98,
-121, 32, 105, 116, 115, 101, 108, 102, 32, 119, 104, 101, 110, 32, 116, 104, 101, 32, 109, 105, 120, 101, 114, 32,
-105, 115, 32, 115, 104, 111, 114, 116, 41, 34, 32, 61, 32, 34, 69, 81, 32, 100, 111, 32, 105, 110, 115, 101,
-114, 116, 58, 32, 91, 76, 77, 66, 93, 32, 114, 101, 99, 111, 108, 104, 101, 32, 40, 101, 108, 101, 32, 115,
-101, 32, 114, 101, 99, 111, 108, 104, 101, 32, 115, 111, 122, 105, 110, 104, 111, 32, 113, 117, 97, 110, 100, 111,
-32, 111, 32, 109, 105, 120, 101, 114, 32, 101, 115, 116, 195, 161, 32, 98, 97, 105, 120, 111, 41, 34, 10, 34,
-97, 99, 116, 105, 118, 101, 34, 32, 61, 32, 34, 97, 116, 105, 118, 111, 34, 10,
+97, 114, 32, 111, 115, 32, 105, 110, 195, 173, 99, 105, 111, 115, 32, 100, 111, 115, 32, 99, 108, 105, 112, 101,
+115, 32, 53, 48, 37, 32, 40, 109, 101, 116, 97, 100, 101, 32, 100, 111, 32, 99, 97, 109, 105, 110, 104, 111,
+32, 97, 116, 195, 169, 32, 97, 32, 103, 114, 97, 100, 101, 41, 34, 10, 34, 83, 116, 111, 112, 32, 97, 110,
+100, 32, 114, 101, 116, 117, 114, 110, 32, 116, 111, 32, 116, 104, 101, 32, 115, 116, 97, 114, 116, 32, 40, 100,
+111, 117, 98, 108, 101, 45, 99, 108, 105, 99, 107, 58, 32, 98, 97, 99, 107, 32, 116, 111, 32, 116, 104, 101,
+32, 115, 116, 97, 114, 116, 32, 97, 110, 100, 32, 97, 108, 108, 32, 110, 111, 116, 101, 115, 32, 111, 102, 102,
+41, 34, 32, 61, 32, 34, 80, 97, 114, 97, 114, 32, 101, 32, 118, 111, 108, 116, 97, 114, 32, 97, 111, 32,
+105, 110, 195, 173, 99, 105, 111, 32, 40, 99, 108, 105, 113, 117, 101, 32, 100, 117, 112, 108, 111, 58, 32, 118,
+111, 108, 116, 97, 32, 97, 111, 32, 105, 110, 195, 173, 99, 105, 111, 32, 101, 32, 100, 101, 115, 108, 105, 103,
+97, 32, 116, 111, 100, 97, 115, 32, 97, 115, 32, 110, 111, 116, 97, 115, 41, 34, 10, 34, 80, 97, 116, 116,
+101, 114, 110, 32, 116, 104, 101, 32, 67, 104, 97, 110, 110, 101, 108, 32, 82, 97, 99, 107, 32, 101, 100, 105,
+116, 115, 58, 32, 91, 76, 77, 66, 93, 32, 111, 110, 32, 116, 104, 101, 32, 110, 97, 109, 101, 58, 32, 116,
+104, 101, 32, 108, 105, 115, 116, 32, 111, 102, 32, 112, 97, 116, 116, 101, 114, 110, 115, 44, 32, 111, 110, 32,
+116, 104, 101, 32, 97, 114, 114, 111, 119, 115, 58, 32, 112, 114, 101, 118, 105, 111, 117, 115, 32, 47, 32, 110,
+101, 120, 116, 32, 32, 91, 87, 104, 101, 101, 108, 93, 32, 115, 116, 101, 112, 34, 32, 61, 32, 34, 80, 97,
+116, 116, 101, 114, 110, 32, 113, 117, 101, 32, 111, 32, 67, 104, 97, 110, 110, 101, 108, 32, 82, 97, 99, 107,
+32, 101, 100, 105, 116, 97, 58, 32, 91, 76, 77, 66, 93, 32, 110, 111, 32, 110, 111, 109, 101, 58, 32, 97,
+32, 108, 105, 115, 116, 97, 32, 100, 101, 32, 112, 97, 116, 116, 101, 114, 110, 115, 44, 32, 110, 97, 115, 32,
+115, 101, 116, 97, 115, 58, 32, 97, 110, 116, 101, 114, 105, 111, 114, 32, 47, 32, 112, 114, 195, 179, 120, 105,
+109, 111, 32, 32, 91, 87, 104, 101, 101, 108, 93, 32, 112, 97, 115, 115, 111, 34, 10, 34, 66, 97, 99, 107,
+32, 116, 111, 32, 116, 104, 101, 32, 115, 116, 97, 114, 116, 34, 32, 61, 32, 34, 86, 111, 108, 116, 97, 114,
+32, 97, 111, 32, 105, 110, 195, 173, 99, 105, 111, 34, 10, 34, 70, 111, 108, 108, 111, 119, 32, 116, 104, 101,
+32, 112, 108, 97, 121, 104, 101, 97, 100, 32, 40, 97, 117, 116, 111, 32, 115, 99, 114, 111, 108, 108, 105, 110,
+103, 41, 34, 32, 61, 32, 34, 83, 101, 103, 117, 105, 114, 32, 111, 32, 99, 117, 114, 115, 111, 114, 32, 100,
+101, 32, 114, 101, 112, 114, 111, 100, 117, 195, 167, 195, 163, 111, 32, 40, 114, 111, 108, 97, 103, 101, 109, 32,
+97, 117, 116, 111, 109, 195, 161, 116, 105, 99, 97, 41, 34, 10, 34, 83, 116, 111, 112, 58, 32, 91, 76, 77,
+66, 93, 32, 115, 116, 111, 112, 32, 97, 110, 100, 32, 103, 111, 32, 98, 97, 99, 107, 32, 116, 111, 32, 116,
+104, 101, 32, 115, 116, 97, 114, 116, 32, 40, 111, 114, 32, 116, 104, 101, 32, 114, 101, 109, 101, 109, 98, 101,
+114, 101, 100, 32, 112, 111, 115, 105, 116, 105, 111, 110, 59, 32, 97, 103, 97, 105, 110, 58, 32, 116, 104, 101,
+32, 115, 116, 97, 114, 116, 41, 32, 32, 91, 50, 120, 76, 77, 66, 93, 32, 98, 97, 99, 107, 32, 116, 111,
+32, 116, 104, 101, 32, 115, 116, 97, 114, 116, 32, 97, 110, 100, 32, 97, 108, 108, 32, 110, 111, 116, 101, 115,
+32, 111, 102, 102, 32, 40, 112, 97, 110, 105, 99, 41, 32, 32, 91, 82, 77, 66, 93, 32, 114, 101, 109, 101,
+109, 98, 101, 114, 32, 115, 101, 101, 107, 32, 116, 105, 109, 101, 34, 32, 61, 32, 34, 80, 97, 114, 97, 114,
+58, 32, 91, 76, 77, 66, 93, 32, 112, 97, 114, 97, 32, 101, 32, 118, 111, 108, 116, 97, 32, 97, 111, 32,
+105, 110, 195, 173, 99, 105, 111, 32, 40, 111, 117, 32, 195, 160, 32, 112, 111, 115, 105, 195, 167, 195, 163, 111,
+32, 108, 101, 109, 98, 114, 97, 100, 97, 59, 32, 100, 101, 32, 110, 111, 118, 111, 58, 32, 111, 32, 105, 110,
+195, 173, 99, 105, 111, 41, 32, 32, 91, 50, 120, 76, 77, 66, 93, 32, 118, 111, 108, 116, 97, 32, 97, 111,
+32, 105, 110, 195, 173, 99, 105, 111, 32, 101, 32, 100, 101, 115, 108, 105, 103, 97, 32, 116, 111, 100, 97, 115,
+32, 97, 115, 32, 110, 111, 116, 97, 115, 32, 40, 112, 195, 162, 110, 105, 99, 111, 41, 32, 32, 91, 82, 77,
+66, 93, 32, 108, 101, 109, 98, 114, 97, 114, 32, 97, 32, 112, 111, 115, 105, 195, 167, 195, 163, 111, 34, 10,
+34, 80, 108, 97, 121, 108, 105, 115, 116, 32, 102, 111, 108, 108, 111, 119, 115, 32, 116, 104, 101, 32, 112, 108,
+97, 121, 104, 101, 97, 100, 32, 40, 97, 117, 116, 111, 32, 115, 99, 114, 111, 108, 108, 105, 110, 103, 41, 34,
+32, 61, 32, 34, 65, 32, 80, 108, 97, 121, 108, 105, 115, 116, 32, 115, 101, 103, 117, 101, 32, 111, 32, 99,
+117, 114, 115, 111, 114, 32, 100, 101, 32, 114, 101, 112, 114, 111, 100, 117, 195, 167, 195, 163, 111, 32, 40, 114,
+111, 108, 97, 103, 101, 109, 32, 97, 117, 116, 111, 109, 195, 161, 116, 105, 99, 97, 41, 34, 10, 34, 80, 105,
+97, 110, 111, 32, 82, 111, 108, 108, 32, 102, 111, 108, 108, 111, 119, 115, 32, 116, 104, 101, 32, 112, 108, 97,
+121, 104, 101, 97, 100, 32, 40, 97, 117, 116, 111, 32, 115, 99, 114, 111, 108, 108, 105, 110, 103, 41, 34, 32,
+61, 32, 34, 79, 32, 80, 105, 97, 110, 111, 32, 82, 111, 108, 108, 32, 115, 101, 103, 117, 101, 32, 111, 32,
+99, 117, 114, 115, 111, 114, 32, 100, 101, 32, 114, 101, 112, 114, 111, 100, 117, 195, 167, 195, 163, 111, 32, 40,
+114, 111, 108, 97, 103, 101, 109, 32, 97, 117, 116, 111, 109, 195, 161, 116, 105, 99, 97, 41, 34, 10, 34, 70,
+111, 108, 108, 111, 119, 58, 32, 116, 104, 101, 32, 80, 105, 97, 110, 111, 32, 82, 111, 108, 108, 32, 115, 99,
+114, 111, 108, 108, 115, 32, 97, 108, 111, 110, 103, 32, 119, 105, 116, 104, 32, 116, 104, 101, 32, 112, 108, 97,
+121, 104, 101, 97, 100, 32, 119, 104, 105, 108, 101, 32, 116, 104, 105, 115, 32, 112, 97, 116, 116, 101, 114, 110,
+32, 112, 108, 97, 121, 115, 59, 32, 111, 102, 102, 44, 32, 121, 111, 117, 32, 99, 97, 110, 32, 108, 111, 111,
+107, 32, 97, 114, 111, 117, 110, 100, 32, 102, 114, 101, 101, 108, 121, 34, 32, 61, 32, 34, 83, 101, 103, 117,
+105, 114, 58, 32, 111, 32, 80, 105, 97, 110, 111, 32, 82, 111, 108, 108, 32, 114, 111, 108, 97, 32, 106, 117,
+110, 116, 111, 32, 99, 111, 109, 32, 111, 32, 99, 117, 114, 115, 111, 114, 32, 101, 110, 113, 117, 97, 110, 116,
+111, 32, 101, 115, 116, 101, 32, 112, 97, 116, 116, 101, 114, 110, 32, 116, 111, 99, 97, 59, 32, 100, 101, 115,
+108, 105, 103, 97, 100, 111, 44, 32, 118, 111, 99, 195, 170, 32, 110, 97, 118, 101, 103, 97, 32, 108, 105, 118,
+114, 101, 109, 101, 110, 116, 101, 34, 10, 34, 70, 111, 108, 108, 111, 119, 32, 116, 104, 101, 32, 112, 108, 97,
+121, 104, 101, 97, 100, 34, 32, 61, 32, 34, 83, 101, 103, 117, 105, 114, 32, 111, 32, 99, 117, 114, 115, 111,
+114, 32, 100, 101, 32, 114, 101, 112, 114, 111, 100, 117, 195, 167, 195, 163, 111, 34, 10, 34, 81, 117, 97, 110,
+116, 105, 115, 101, 32, 115, 116, 97, 114, 116, 115, 32, 53, 48, 37, 32, 40, 104, 97, 108, 102, 119, 97, 121,
+32, 116, 111, 32, 116, 104, 101, 32, 103, 114, 105, 100, 41, 34, 32, 61, 32, 34, 81, 117, 97, 110, 116, 105,
+122, 97, 114, 32, 105, 110, 195, 173, 99, 105, 111, 115, 32, 53, 48, 37, 32, 40, 109, 101, 116, 97, 100, 101,
+32, 100, 111, 32, 99, 97, 109, 105, 110, 104, 111, 32, 97, 116, 195, 169, 32, 97, 32, 103, 114, 97, 100, 101,
+41, 34, 10, 34, 81, 117, 105, 99, 107, 32, 113, 117, 97, 110, 116, 105, 122, 101, 32, 53, 48, 37, 32, 40,
+104, 97, 108, 102, 119, 97, 121, 32, 116, 111, 32, 116, 104, 101, 32, 103, 114, 105, 100, 41, 34, 32, 61, 32,
+34, 81, 117, 97, 110, 116, 105, 122, 97, 195, 167, 195, 163, 111, 32, 114, 195, 161, 112, 105, 100, 97, 32, 53,
+48, 37, 32, 40, 109, 101, 116, 97, 100, 101, 32, 100, 111, 32, 99, 97, 109, 105, 110, 104, 111, 32, 97, 116,
+195, 169, 32, 97, 32, 103, 114, 97, 100, 101, 41, 34, 10, 34, 70, 111, 108, 108, 111, 119, 58, 32, 116, 104,
+101, 32, 80, 108, 97, 121, 108, 105, 115, 116, 32, 115, 99, 114, 111, 108, 108, 115, 32, 97, 108, 111, 110, 103,
+32, 119, 105, 116, 104, 32, 116, 104, 101, 32, 112, 108, 97, 121, 104, 101, 97, 100, 32, 119, 104, 105, 108, 101,
+32, 116, 104, 101, 32, 115, 111, 110, 103, 32, 112, 108, 97, 121, 115, 59, 32, 111, 102, 102, 44, 32, 121, 111,
+117, 32, 99, 97, 110, 32, 108, 111, 111, 107, 32, 97, 114, 111, 117, 110, 100, 32, 102, 114, 101, 101, 108, 121,
+34, 32, 61, 32, 34, 83, 101, 103, 117, 105, 114, 58, 32, 97, 32, 80, 108, 97, 121, 108, 105, 115, 116, 32,
+114, 111, 108, 97, 32, 106, 117, 110, 116, 111, 32, 99, 111, 109, 32, 111, 32, 99, 117, 114, 115, 111, 114, 32,
+101, 110, 113, 117, 97, 110, 116, 111, 32, 97, 32, 109, 195, 186, 115, 105, 99, 97, 32, 116, 111, 99, 97, 59,
+32, 100, 101, 115, 108, 105, 103, 97, 100, 111, 44, 32, 118, 111, 99, 195, 170, 32, 110, 97, 118, 101, 103, 97,
+32, 108, 105, 118, 114, 101, 109, 101, 110, 116, 101, 34, 10, 10, 35, 32, 114, 111, 117, 110, 100, 32, 49, 54,
+50, 32, 40, 112, 97, 116, 99, 104, 101, 114, 49, 54, 50, 41, 58, 32, 80, 97, 116, 99, 104, 101, 114, 32,
+112, 108, 117, 103, 105, 110, 32, 108, 105, 115, 116, 44, 32, 73, 47, 79, 32, 110, 111, 100, 101, 32, 77, 73,
+68, 73, 32, 112, 111, 114, 116, 115, 44, 32, 77, 73, 68, 73, 32, 79, 117, 116, 32, 105, 110, 115, 105, 100,
+101, 32, 97, 32, 80, 97, 116, 99, 104, 101, 114, 10, 34, 75, 101, 121, 98, 111, 97, 114, 100, 32, 67, 111,
+110, 116, 114, 111, 108, 108, 101, 114, 34, 32, 61, 32, 34, 67, 111, 110, 116, 114, 111, 108, 97, 100, 111, 114,
+32, 100, 101, 32, 116, 101, 99, 108, 97, 100, 111, 34, 10, 34, 79, 112, 101, 110, 32, 112, 108, 117, 103, 105,
+110, 32, 112, 105, 99, 107, 101, 114, 46, 46, 46, 34, 32, 61, 32, 34, 65, 98, 114, 105, 114, 32, 115, 101,
+108, 101, 116, 111, 114, 32, 100, 101, 32, 112, 108, 117, 103, 105, 110, 115, 46, 46, 46, 34, 10, 34, 77, 111,
+114, 101, 46, 46, 46, 34, 32, 61, 32, 34, 77, 97, 105, 115, 46, 46, 46, 34, 10, 34, 65, 100, 100, 32,
+112, 108, 117, 103, 105, 110, 34, 32, 61, 32, 34, 65, 100, 105, 99, 105, 111, 110, 97, 114, 32, 112, 108, 117,
+103, 105, 110, 34, 10, 34, 65, 99, 116, 105, 118, 97, 116, 101, 32, 97, 108, 108, 34, 32, 61, 32, 34, 65,
+116, 105, 118, 97, 114, 32, 116, 111, 100, 111, 115, 34, 10, 34, 68, 101, 97, 99, 116, 105, 118, 97, 116, 101,
+32, 97, 108, 108, 34, 32, 61, 32, 34, 68, 101, 115, 97, 116, 105, 118, 97, 114, 32, 116, 111, 100, 111, 115,
+34, 10, 34, 68, 101, 97, 99, 116, 105, 118, 97, 116, 101, 32, 117, 110, 117, 115, 101, 100, 34, 32, 61, 32,
+34, 68, 101, 115, 97, 116, 105, 118, 97, 114, 32, 111, 115, 32, 110, 195, 163, 111, 32, 117, 115, 97, 100, 111,
+115, 34, 10, 34, 69, 118, 101, 110, 116, 115, 34, 32, 61, 32, 34, 69, 118, 101, 110, 116, 111, 115, 34, 10,
+34, 65, 100, 100, 32, 111, 110, 101, 34, 32, 61, 32, 34, 65, 100, 105, 99, 105, 111, 110, 97, 114, 32, 117,
+109, 34, 10, 34, 68, 105, 115, 99, 111, 110, 110, 101, 99, 116, 32, 97, 108, 108, 34, 32, 61, 32, 34, 68,
+101, 115, 99, 111, 110, 101, 99, 116, 97, 114, 32, 116, 117, 100, 111, 34, 10, 34, 83, 121, 110, 116, 104, 32,
+99, 108, 97, 115, 115, 105, 99, 34, 32, 61, 32, 34, 83, 121, 110, 116, 104, 32, 99, 108, 195, 161, 115, 115,
+105, 99, 111, 34, 10, 34, 83, 121, 110, 116, 104, 32, 115, 112, 101, 99, 105, 97, 108, 34, 32, 61, 32, 34,
+83, 121, 110, 116, 104, 32, 101, 115, 112, 101, 99, 105, 97, 108, 34, 10, 34, 68, 101, 108, 97, 121, 32, 114,
+101, 118, 101, 114, 98, 34, 32, 61, 32, 34, 68, 101, 108, 97, 121, 32, 101, 32, 114, 101, 118, 101, 114, 98,
+34, 10, 34, 77, 73, 68, 73, 32, 111, 117, 116, 34, 32, 61, 32, 34, 83, 97, 195, 173, 100, 97, 32, 77,
+73, 68, 73, 34, 10, 34, 73, 110, 115, 105, 100, 101, 32, 97, 32, 80, 97, 116, 99, 104, 101, 114, 58, 32,
+99, 97, 98, 108, 101, 32, 105, 116, 115, 32, 92, 34, 77, 73, 68, 73, 32, 111, 117, 116, 92, 34, 32, 112,
+105, 110, 32, 116, 111, 32, 97, 110, 32, 105, 110, 115, 116, 114, 117, 109, 101, 110, 116, 32, 111, 114, 32, 116,
+111, 32, 97, 32, 92, 34, 77, 73, 68, 73, 32, 112, 111, 114, 116, 92, 34, 32, 112, 105, 110, 32, 111, 102,
+32, 92, 34, 84, 111, 32, 76, 89, 78, 88, 92, 34, 59, 32, 105, 116, 115, 32, 110, 111, 116, 101, 115, 32,
+103, 111, 32, 111, 117, 116, 32, 111, 110, 32, 116, 104, 105, 115, 32, 77, 73, 68, 73, 32, 99, 104, 97, 110,
+110, 101, 108, 46, 34, 32, 61, 32, 34, 68, 101, 110, 116, 114, 111, 32, 100, 101, 32, 117, 109, 32, 80, 97,
+116, 99, 104, 101, 114, 58, 32, 108, 105, 103, 117, 101, 32, 111, 32, 112, 105, 110, 111, 32, 92, 34, 83, 97,
+195, 173, 100, 97, 32, 77, 73, 68, 73, 92, 34, 32, 97, 32, 117, 109, 32, 105, 110, 115, 116, 114, 117, 109,
+101, 110, 116, 111, 32, 111, 117, 32, 97, 32, 117, 109, 32, 112, 105, 110, 111, 32, 92, 34, 80, 111, 114, 116,
+97, 32, 77, 73, 68, 73, 92, 34, 32, 100, 101, 32, 92, 34, 80, 97, 114, 97, 32, 111, 32, 76, 89, 78,
+88, 92, 34, 59, 32, 97, 115, 32, 110, 111, 116, 97, 115, 32, 115, 97, 101, 109, 32, 110, 101, 115, 116, 101,
+32, 99, 97, 110, 97, 108, 32, 77, 73, 68, 73, 46, 34, 10, 34, 66, 114, 111, 119, 115, 101, 32, 112, 108,
+117, 103, 105, 110, 32, 100, 97, 116, 97, 98, 97, 115, 101, 34, 32, 61, 32, 34, 80, 114, 111, 99, 117, 114,
+97, 114, 32, 110, 111, 32, 98, 97, 110, 99, 111, 32, 100, 101, 32, 112, 108, 117, 103, 105, 110, 115, 34, 10,
+34, 123, 48, 125, 32, 40, 76, 89, 78, 88, 41, 34, 32, 61, 32, 34, 123, 48, 125, 32, 40, 76, 89, 78,
+88, 41, 34, 10, 34, 77, 101, 110, 117, 32, 100, 101, 110, 115, 105, 116, 121, 34, 32, 61, 32, 34, 68, 101,
+110, 115, 105, 100, 97, 100, 101, 32, 100, 111, 115, 32, 109, 101, 110, 117, 115, 34, 10, 34, 67, 111, 109, 102,
+111, 114, 116, 97, 98, 108, 101, 34, 32, 61, 32, 34, 67, 111, 110, 102, 111, 114, 116, 195, 161, 118, 101, 108,
+34, 10, 34, 84, 105, 109, 101, 32, 115, 101, 108, 101, 99, 116, 105, 111, 110, 32, 32, 91, 65, 108, 116, 93,
+32, 110, 111, 32, 115, 110, 97, 112, 34, 32, 61, 32, 34, 83, 101, 108, 101, 195, 167, 195, 163, 111, 32, 100,
+101, 32, 116, 101, 109, 112, 111, 32, 32, 91, 65, 108, 116, 93, 32, 115, 101, 109, 32, 103, 114, 97, 100, 101,
+34, 10, 34, 83, 111, 110, 103, 32, 112, 111, 115, 105, 116, 105, 111, 110, 32, 32, 91, 65, 108, 116, 93, 32,
+110, 111, 32, 115, 110, 97, 112, 34, 32, 61, 32, 34, 80, 111, 115, 105, 195, 167, 195, 163, 111, 32, 110, 97,
+32, 109, 195, 186, 115, 105, 99, 97, 32, 32, 91, 65, 108, 116, 93, 32, 115, 101, 109, 32, 103, 114, 97, 100,
+101, 34, 10, 34, 80, 111, 115, 105, 116, 105, 111, 110, 32, 32, 91, 65, 108, 116, 93, 32, 110, 111, 32, 115,
+110, 97, 112, 34, 32, 61, 32, 34, 80, 111, 115, 105, 195, 167, 195, 163, 111, 32, 32, 91, 65, 108, 116, 93,
+32, 115, 101, 109, 32, 103, 114, 97, 100, 101, 34, 10, 34, 73, 110, 115, 101, 114, 116, 32, 69, 81, 32, 40,
+102, 111, 108, 100, 101, 100, 41, 58, 32, 91, 76, 77, 66, 93, 32, 117, 110, 102, 111, 108, 100, 32, 105, 116,
+34, 32, 61, 32, 34, 69, 81, 32, 100, 111, 32, 105, 110, 115, 101, 114, 116, 32, 40, 114, 101, 99, 111, 108,
+104, 105, 100, 111, 41, 58, 32, 91, 76, 77, 66, 93, 32, 97, 98, 114, 101, 34, 10, 34, 73, 110, 115, 101,
+114, 116, 32, 69, 81, 58, 32, 91, 76, 77, 66, 93, 32, 102, 111, 108, 100, 32, 105, 116, 32, 40, 105, 116,
+32, 102, 111, 108, 100, 115, 32, 98, 121, 32, 105, 116, 115, 101, 108, 102, 32, 119, 104, 101, 110, 32, 116, 104,
+101, 32, 109, 105, 120, 101, 114, 32, 105, 115, 32, 115, 104, 111, 114, 116, 41, 34, 32, 61, 32, 34, 69, 81,
+32, 100, 111, 32, 105, 110, 115, 101, 114, 116, 58, 32, 91, 76, 77, 66, 93, 32, 114, 101, 99, 111, 108, 104,
+101, 32, 40, 101, 108, 101, 32, 115, 101, 32, 114, 101, 99, 111, 108, 104, 101, 32, 115, 111, 122, 105, 110, 104,
+111, 32, 113, 117, 97, 110, 100, 111, 32, 111, 32, 109, 105, 120, 101, 114, 32, 101, 115, 116, 195, 161, 32, 98,
+97, 105, 120, 111, 41, 34, 10, 34, 97, 99, 116, 105, 118, 101, 34, 32, 61, 32, 34, 97, 116, 105, 118, 111,
+34, 10,
 };
 } // namespace lynx::i18n::data

+ 3 - 0
src/ui/MainComponent.cpp

@@ -1317,6 +1317,8 @@ void MainComponent::loadUiSettings()
             engine.getTransport().metronomeVolume.store ((float) controls::clampUtilityVolume (line.fromFirstOccurrenceOf ("=", false, false).trim().getDoubleValue()));
         else if (key == "previewVolume")
             engine.setPreviewVolume (line.fromFirstOccurrenceOf ("=", false, false).trim().getDoubleValue());
+        else if (key == "editorVolume")
+            engine.setEditorVolume (line.fromFirstOccurrenceOf ("=", false, false).trim().getDoubleValue());
         else if (key == "rackPianoRollView")
             rack.setPianoRollView (on);
         else if (key == "undoLevels")
@@ -1383,6 +1385,7 @@ void MainComponent::saveUiSettings()
                                       + "\nundoLevels=" + juce::String (engine.getUndoLevels())
                                       + "\nmetronomeVolume=" + juce::String (engine.getTransport().metronomeVolume.load(), 2)
                                       + "\npreviewVolume=" + juce::String (engine.getPreviewVolume(), 2)
+                                      + "\neditorVolume=" + juce::String (engine.getEditorVolume(), 2)
                                       + "\nrackPianoRollView=" + juce::String (rack.isPianoRollView() ? 1 : 0)
                                       + "\naskWhatToRecord=" + juce::String (engine.getRecordOptions().askWhatToRecord ? 1 : 0)
                                       + "\nrecordNotes=" + juce::String (engine.getRecordOptions().recordNotes ? 1 : 0)

+ 1 - 1
src/ui/PlaylistComponent.cpp

@@ -4002,7 +4002,7 @@ bool addAudioClip (AudioEngine& engine, const juce::File& file, std::int64_t sta
             c.name = name;
             c.instrument = InstrumentKind::audioClip;
             c.samplePath = path;
-            c.mixerTrack = s.nextFreeInsert();
+            c.mixerTrack = s.claimFreeInsert();
             s.channels.push_back (c);
             channel = (int) s.channels.size() - 1;
         }

+ 1 - 1
src/ui/SoundFontEditor.cpp

@@ -1019,7 +1019,7 @@ int addSoundFontChannel (AudioEngine& engine, const std::string& path, int bank,
     }
     int index = 0;
     engine.editSong ([&] (Song& s) {
-        c.mixerTrack = s.nextFreeInsert();
+        c.mixerTrack = s.claimFreeInsert();
         if (insertAt < 0 || insertAt > (int) s.channels.size())
         {
             s.channels.push_back (c);

+ 284 - 0
tests/Sampler162Tests.inc

@@ -0,0 +1,284 @@
+// --- Round 162 (issue #9): channel routing past insert 16, sampler fine tune / stretch / pitch wheel ---------------
+
+namespace s162
+{
+// a mono sine sample
+std::shared_ptr<lynx::SampleData> sine (double freq, double seconds, double rate = 44100.0)
+{
+    auto s = std::make_shared<lynx::SampleData>();
+    s->sampleRate = rate;
+    const auto n = (std::size_t) (seconds * rate);
+    for (std::size_t i = 0; i < n; ++i)
+        s->left.push_back ((float) (0.5 * std::sin (2.0 * 3.141592653589793 * freq * (double) i / rate)));
+    return s;
+}
+
+// frequency of the steady part of `x` (positive-going zero crossings between 30 % and 70 % of the sounding part) and
+// the number of sounding samples (until the signal stays below -60 dB)
+struct Heard
+{
+    double freq = 0.0;
+    long length = 0;
+};
+
+Heard analyse (const std::vector<float>& x, double rate)
+{
+    Heard h;
+    long last = -1;
+    for (long i = 0; i < (long) x.size(); ++i)
+        if (std::abs (x[(std::size_t) i]) > 0.001f)
+            last = i;
+    h.length = last + 1;
+    const auto from = (long) (h.length * 0.3), to = (long) (h.length * 0.7);
+    long first = -1, end = -1;
+    int crossings = 0;
+    for (long i = from + 1; i < to; ++i)
+        if (x[(std::size_t) i - 1] <= 0.0f && x[(std::size_t) i] > 0.0f)
+        {
+            if (first < 0)
+                first = i;
+            else
+                ++crossings, end = i;
+        }
+    if (crossings > 0)
+        h.freq = crossings * rate / (double) (end - first);
+    return h;
+}
+
+// a sampler channel playing `sample` with `settings`: one live note (key, cents of fine tune), the transport
+// stopped, rendered until it ends (at most `seconds`)
+Heard playLive (const lynx::SamplerSettings& settings, std::shared_ptr<lynx::SampleData> sample, int key, float fineCents = 0.0f,
+                double seconds = 6.0, float bend = 0.0f, int range = 2)
+{
+    lynx::Song s;
+    s.mixer = lynx::Song::defaultMixer (2);
+    s.bpm = 120.0;
+    lynx::ChannelDef c;
+    c.instrument = lynx::InstrumentKind::sampler;
+    c.samplePath = "sine.wav";
+    c.sampler = settings;
+    c.misc.fineTune = fineCents;
+    c.misc.pitchRange = range;
+    c.mixerTrack = 1;
+    s.channels.push_back (c);
+    s.patterns.emplace_back();
+    lynx::RackState::Factories f;
+    f.samples = [sample] (const std::string&) { return std::shared_ptr<const lynx::SampleData> (sample); };
+    f.stretchedSamples = [sample] (const std::string&, double ratio, lynx::StretchMethod m) {
+        return std::shared_ptr<const lynx::SampleData> (lynx::timeStretch (*sample, ratio, m));
+    };
+    constexpr double rate = 44100.0;
+    auto state = lynx::RackState::build (s, nullptr, rate, f);
+    lynx::RackRenderer r;
+    r.prepare (rate, 441);
+    lynx::LiveEventQueue live;
+    if (bend != 0.0f)
+    {
+        lynx::NoteEvent b;
+        b.type = lynx::NoteEvent::Type::control;
+        b.channel = 0;
+        b.key = lynx::NoteEvent::controlPitchBend;
+        b.velocity = bend;
+        live.push (b);
+    }
+    lynx::NoteEvent e;
+    e.channel = 0;
+    e.key = key;
+    e.velocity = 0.8f;
+    live.push (e);
+    std::vector<float> out, l (441), rr (441);
+    for (int b = 0; b < (int) (seconds * 100.0); ++b)
+    {
+        r.process (state.get(), false, 0.0, 441, l.data(), rr.data(), &live, nullptr);
+        out.insert (out.end(), l.begin(), l.end());
+    }
+    return analyse (out, rate);
+}
+} // namespace s162
+
+TEST (s162_a_channel_routes_to_any_insert)
+{
+    using namespace lynx;
+    auto s = Song::makeDefault();
+    CHECK (s.mixer.size() == 17); // Master + 16, as before
+    s.channels.resize (std::max<std::size_t> (s.channels.size(), 1));
+    // issue #9: past insert 16 the route box stopped; now the inserts up to the target are added
+    mixerops::routeChannel (s, 0, 64);
+    CHECK (s.channels[0].mixerTrack == 64);
+    CHECK (s.mixer.size() == 65);
+    CHECK (s.mixer[40].name == "Insert 40" && s.mixer[64].name == "Insert 64");
+    mixerops::routeChannel (s, 0, 500); // at most FL's last insert
+    CHECK (s.channels[0].mixerTrack == maxMixerTracks - 1);
+    CHECK ((int) s.mixer.size() == maxMixerTracks);
+    mixerops::routeChannel (s, 0, 3); // routing lower never removes inserts
+    CHECK (s.channels[0].mixerTrack == 3 && (int) s.mixer.size() == maxMixerTracks);
+
+    // a new channel takes the next free insert, past the 16th when they are all in use (it used to fall to Master)
+    auto t = Song::makeDefault();
+    t.channels.clear();
+    for (int i = 0; i < 20; ++i)
+    {
+        ChannelDef c;
+        c.mixerTrack = t.claimFreeInsert();
+        t.channels.push_back (c);
+    }
+    CHECK (t.channels[15].mixerTrack == 16);
+    CHECK (t.channels[16].mixerTrack == 17 && t.channels[19].mixerTrack == 20);
+    CHECK (t.mixer.size() == 21);
+    CHECK (t.nextFreeInsert() == 0); // (the const query still says "none free")
+}
+
+TEST (s162_the_rack_mixes_a_channel_routed_past_insert_16_into_that_insert)
+{
+    using namespace lynx;
+    Song s;
+    s.mixer = Song::defaultMixer (16);
+    ChannelDef c;
+    c.instrument = InstrumentKind::synth;
+    s.channels.push_back (c);
+    mixerops::routeChannel (s, 0, 40);
+    CHECK (s.mixer.size() == 41);
+    // the insert gets the channel (not insert 16, where the renderer used to clamp it)
+    CHECK (mixerops::channelsOn (s, 40).size() == 1);
+    CHECK (mixerops::channelsOn (s, 16).empty());
+}
+
+TEST (s162_sampler_fine_tune_changes_live_notes)
+{
+    using namespace lynx;
+    const auto sample = s162::sine (440.0, 1.0);
+    SamplerSettings st;
+    const auto plain = s162::playLive (st, sample, 60);
+    CHECK (std::abs (plain.freq - 440.0) < 1.0);
+    // Misc › Fine tune +50 cents on a key played live: 440 · 2^(50/1200) = 452.9 Hz (it was ignored)
+    const auto fine = s162::playLive (st, sample, 60, 50.0f);
+    CHECK (std::abs (fine.freq - 452.9) < 1.5);
+    // the sampler's pitch knob, in cents (-0.25 st)
+    st.pitch = -0.25f;
+    CHECK (std::abs (s162::playLive (st, sample, 60).freq - 440.0 * std::exp2 (-0.25 / 12.0)) < 1.0);
+}
+
+TEST (s162_resample_stretch_follows_the_keys)
+{
+    using namespace lynx;
+    // a 1 s sample stretched (Resample) to 4 beats = 2 s at 120 BPM: an octave down at the root key
+    const auto sample = s162::sine (440.0, 1.0);
+    SamplerSettings st;
+    st.stretchBeats = 4.0f;
+    const auto root = s162::playLive (st, sample, 60);
+    CHECK (std::abs (root.freq - 220.0) < 1.0);
+    CHECK (std::abs (root.length - 88200) < 400);
+    // FL's Resample: pitch and length change together, like tape (LYNX played every key at the root's pitch)
+    const auto fifth = s162::playLive (st, sample, 67);
+    CHECK (std::abs (fifth.freq - 220.0 * std::exp2 (7.0 / 12.0)) < 1.5);
+    CHECK (std::abs (fifth.length - 88200.0 / std::exp2 (7.0 / 12.0)) < 400);
+    // fine tune and pitch knob too
+    CHECK (std::abs (s162::playLive (st, sample, 60, 100.0f).freq - 220.0 * std::exp2 (1.0 / 12.0)) < 1.0);
+}
+
+TEST (s162_stretch_keeping_pitch_transposes_and_keeps_the_length)
+{
+    using namespace lynx;
+    const auto sample = s162::sine (440.0, 1.0);
+    SamplerSettings st;
+    st.stretchBeats = 4.0f; // 2 s at 120 BPM
+    st.keepPitch = true;
+    const auto root = s162::playLive (st, sample, 60);
+    CHECK (std::abs (root.freq - 440.0) < 1.5);
+    CHECK (std::abs (root.length - 88200) < 2000);
+    // FL's Stretch modes: "pitch changes keep the original duration" — the key transposes, the length stays
+    for (const int key : { 67, 55, 72 })
+    {
+        const auto heard = s162::playLive (st, sample, key);
+        const auto expected = 440.0 * std::exp2 ((key - 60) / 12.0);
+        CHECK (std::abs (heard.freq - expected) < expected * 0.004);
+        CHECK (std::abs (heard.length - root.length) < 4000);
+    }
+    // the fine tune moves it too
+    const auto fine = s162::playLive (st, sample, 60, 100.0f);
+    CHECK (std::abs (fine.freq - 440.0 * std::exp2 (1.0 / 12.0)) < 2.5);
+}
+
+TEST (s162_pitch_wheel_bends_native_instruments_by_the_pitch_range)
+{
+    using namespace lynx;
+    const auto sample = s162::sine (440.0, 1.0);
+    SamplerSettings st;
+    // full wheel up with FL's default range (2 semitones): 440 · 2^(2/12)
+    CHECK (std::abs (s162::playLive (st, sample, 60, 0.0f, 3.0, 1.0f).freq - 440.0 * std::exp2 (2.0 / 12.0)) < 1.5);
+    // half down with a 12 semitone range: -6 st
+    CHECK (std::abs (s162::playLive (st, sample, 60, 0.0f, 3.0, -0.5f, 12).freq - 440.0 * std::exp2 (-6.0 / 12.0)) < 1.0);
+    // stretch keeping pitch: the wheel bends the shifter (the length stays)
+    st.stretchBeats = 4.0f;
+    st.keepPitch = true;
+    const auto bent = s162::playLive (st, sample, 64, 0.0f, 4.0, 1.0f);
+    CHECK (std::abs (bent.freq - 440.0 * std::exp2 (6.0 / 12.0)) < 3.0);
+    CHECK (std::abs (bent.length - 88200) < 4000);
+
+    // the built-in synth: a sine at C5 bent up 2 semitones
+    auto synth = makeInstrument (InstrumentKind::synth);
+    synth->prepare (44100.0);
+    ChannelDef c;
+    c.synth.osc[0] = { Waveform::sine, 0, 0.0f, 1.0f };
+    c.synth.osc[1].level = c.synth.osc[2].level = 0.0f;
+    synth->configure (c, 120.0);
+    synth->pitchBend (2.0f);
+    synth->noteOn (69, 0.8f);
+    std::vector<float> l (44100, 0.0f), r (44100, 0.0f);
+    synth->render (l.data(), r.data(), 44100, 1.0f, 1.0f);
+    CHECK (std::abs (s162::analyse (l, 44100.0).freq - 440.0 * std::exp2 (2.0 / 12.0)) < 2.0);
+}
+
+namespace s162
+{
+// a fake hosted plugin: records the MIDI messages it gets
+struct MidiSpy final : lynx::Instrument
+{
+    std::vector<std::vector<std::uint8_t>> got;
+    void prepare (double) noexcept override {}
+    void noteOn (int, float) noexcept override {}
+    void noteOff (int) noexcept override {}
+    void allNotesOff() noexcept override {}
+    void render (float*, float*, int, float, float) noexcept override {}
+    void midiMessage (const std::uint8_t* bytes, int size) noexcept override { got.emplace_back (bytes, bytes + size); }
+};
+} // namespace s162
+
+TEST (s162_pitch_and_mod_wheel_reach_a_hosted_plugin)
+{
+    using namespace lynx;
+    Song s;
+    s.mixer = Song::defaultMixer (2);
+    ChannelDef c;
+    c.instrument = InstrumentKind::plugin;
+    c.plugin.id = "spy";
+    s.channels.push_back (c);
+    s.patterns.emplace_back();
+    auto spy = std::make_shared<s162::MidiSpy>();
+    RackState::Factories f;
+    f.pluginInstruments = [spy] (const PluginRef&, double) { return std::shared_ptr<Instrument> (spy); };
+    auto state = RackState::build (s, nullptr, 44100.0, f);
+    RackRenderer r;
+    r.prepare (44100.0, 256);
+    LiveEventQueue live;
+    NoteEvent e;
+    e.type = NoteEvent::Type::control;
+    e.channel = 0;
+    e.key = NoteEvent::controlPitchBend;
+    e.velocity = 1.0f;
+    live.push (e);
+    e.velocity = 0.0f;
+    live.push (e);
+    e.key = 1; // mod wheel
+    e.velocity = 64.0f / 127.0f;
+    live.push (e);
+    std::vector<float> l (256), rr (256);
+    r.process (state.get(), false, 0.0, 256, l.data(), rr.data(), &live, nullptr);
+    CHECK (spy->got.size() == 3);
+    if (spy->got.size() == 3)
+    {
+        CHECK ((spy->got[0] == std::vector<std::uint8_t> { 0xE0, 127, 127 }));
+        CHECK ((spy->got[1] == std::vector<std::uint8_t> { 0xE0, 0, 64 }));
+        CHECK ((spy->got[2] == std::vector<std::uint8_t> { 0xB0, 1, 64 }));
+    }
+}

+ 10 - 1
tests/tests.cpp

@@ -6029,10 +6029,18 @@ TEST (cut_groups_stretch_and_envelope_import)
     lynx::ChannelDef c;
     c.sampler.stretchBeats = 4.0f;
     inst->configure (c, 120.0);
-    inst->noteOn (72, 1.0f); // the note pitch doesn't change the speed
+    inst->noteOn (60, 1.0f);
     std::vector<float> a (48000), b (48000);
     inst->render (a.data(), b.data(), 48000, 1.0f, 1.0f);
     CHECK (std::abs (a[47999] - 0.5f) < 0.01f); // after 1 s it has read half
+    // FL's Resample: the key still transposes, like tape (round 162, issue #9): an octave up reads twice as fast
+    inst->allNotesOff();
+    std::fill (a.begin(), a.end(), 0.0f);
+    inst->render (a.data(), b.data(), 4800, 1.0f, 1.0f);
+    std::fill (a.begin(), a.end(), 0.0f);
+    inst->noteOn (72, 1.0f);
+    inst->render (a.data(), b.data(), 24000, 1.0f, 1.0f);
+    CHECK (std::abs (a[23999] - 0.5f) < 0.01f);
 
     // FL envelope time: 32950 = 2 s; < 101 = 0
     CHECK (std::abs (flp::Channel::envelopeTime (32950) - 2.0) < 0.01 && flp::Channel::envelopeTime (50) == 0.0);
@@ -16024,6 +16032,7 @@ TEST (fl_low_lifter_matches_fl_engine)
 #include "Mp3157Tests.inc"
 #include "Quality160Tests.inc"
 #include "CrashReport159Tests.inc"
+#include "Sampler162Tests.inc"
 #include "Theme162Tests.inc"
 #include "PluginPresets162Tests.inc"
 #include "Look162Tests.inc"