FA1: unity-Preserve bypass kills preview onset latency; drain-slot reload keeps voices ringing through curve edits; velocity path proven end-to-end

This commit is contained in:
2026-07-27 18:25:01 -04:00
parent 2b8ab4abe4
commit d9321eaf3a
6 changed files with 275 additions and 61 deletions
+82 -35
View File
@@ -130,10 +130,12 @@ tresult PLUGIN_API ReaSamplerProcessor::initialize(FUnknown* context) {
}
tresult PLUGIN_API ReaSamplerProcessor::terminate() {
// process() is not running at terminate. Free the live instrument and drain the
// graveyard. Take the pointer out of the atomic first so nothing else races it.
// process() is not running at terminate. Free the live + draining instruments and
// drain the graveyard. Take the pointers out of the atomics first so nothing else
// races them.
std::lock_guard<std::mutex> lock(reloadMutex_);
delete live_.exchange(nullptr);
delete draining_.exchange(nullptr);
graveyard_.clear();
return SingleComponentEffect::terminate();
}
@@ -148,6 +150,13 @@ tresult PLUGIN_API ReaSamplerProcessor::setActive(TBool state) {
reloadFromBank();
} else {
std::lock_guard<std::mutex> lock(reloadMutex_);
// process is guaranteed stopped: free EVERYTHING. The live instrument too — its
// voices are frozen mid-flight, and if it survived deactivation the reactivate
// reload would displace it into the DRAIN slot, resurrecting stale sustained
// voices as ghosts. Reactivation rebuilds from scratch (reloadFromBank above),
// so nothing is lost by clearing here.
delete live_.exchange(nullptr);
delete draining_.exchange(nullptr);
graveyard_.clear();
}
return kResultOk;
@@ -436,27 +445,31 @@ std::string ReaSamplerProcessor::reloadFromBank() {
}
}
// 4. Publish. Atomically install the new instrument; the DISPLACED one goes to the
// graveyard tagged with this generation (process may still be mid-block reading
// it). A null `built` (no bank / unreadable WAV) installs silence.
// `built` is heap-owned; release() hands ownership to the atomic, and the
// exchanged pointer is re-owned by the graveyard.
// 4. Publish. Atomically install the new instrument; the DISPLACED one moves into the
// DRAIN slot (FA1, bug 3b) where process() keeps rendering its ringing voices —
// a reload never cuts a sounding note; the next note-on plays the new state. The
// instrument evicted FROM the drain slot (two reloads old) goes to the graveyard
// (process may still be mid-block reading it). A null `built` (no bank / unreadable
// WAV) installs silence while the displaced tails still ring out via the drain.
// `built` is heap-owned; release() hands ownership to the atomic; the drain-evicted
// pointer is re-owned by the graveyard.
//
// Bounded reclaim: prune graveyard entries where displacedAt <= seen, where seen
// is the last generation process() published. process() publishes inst->installedAt
// (not a re-read of reloadGeneration_), so seen == D means process holds the
// instrument installed at gen D. An entry with displacedAt == D was displaced by
// reload D, which installed that very successor — process cannot be holding the
// displaced entry. The pruning condition is therefore <= (see header for the full
// proof). Remaining entries drain at setActive(false) / terminate() when process
// is guaranteed stopped.
// Bounded reclaim: free graveyard entries whose installedAt < seen, where seen is
// the minimum installedAt process() published over the pointers it holds. Both
// slots are monotone in installedAt, so seen is monotone and any future process()
// load yields installedAt >= seen — an entry below seen is provably unreachable
// (see the header proof). Remaining entries drain at setActive(false) / terminate()
// when process is guaranteed stopped.
const std::uint64_t seen = processGeneration_.load(std::memory_order_acquire);
graveyard_.erase(
std::remove_if(graveyard_.begin(), graveyard_.end(),
[seen](const GraveyardEntry& e) { return e.displacedAt <= seen; }),
[seen](const std::unique_ptr<LoadedInstrument>& e) {
return e->installedAt < seen;
}),
graveyard_.end());
LoadedInstrument* prev = live_.exchange(built.release());
if (prev) graveyard_.push_back({gen, std::unique_ptr<LoadedInstrument>(prev)});
LoadedInstrument* evicted = draining_.exchange(prev);
if (evicted) graveyard_.push_back(std::unique_ptr<LoadedInstrument>(evicted));
return resolvedId;
}
@@ -543,24 +556,43 @@ ReaSamplerProcessor::pollBankSync(bool isFocusedTarget) {
}
tresult PLUGIN_API ReaSamplerProcessor::process(ProcessData& data) {
// REAL-TIME: no allocation, no IO, no locks. Load the live instrument once for the
// whole block (a single atomic acquire), then publish inst->installedAt so the off-
// thread graveyard pruner knows exactly which generation this block is holding.
// REAL-TIME: no allocation, no IO, no locks. Load the live AND draining instruments
// once for the whole block (two atomic acquires), then publish the MINIMUM installedAt
// over the pointers held so the off-thread graveyard pruner knows exactly which
// generations this block is holding (see the header proof).
//
// We publish installedAt — not a fresh re-read of reloadGeneration_ — to close an
// ordering race: reading reloadGeneration_ after live_ could observe a generation
// newer than the pointer we actually hold, causing the pruner to free an instrument
// ordering race: reading reloadGeneration_ after the slots could observe a generation
// newer than the pointers we actually hold, causing the pruner to free an instrument
// process is still reading. installedAt was set on the reload path before the atomic
// exchange that made the instrument visible, so it is always <= the generation of any
// instrument that could have been loaded after our acquire above.
// exchange that made the instrument visible.
//
// The DRAIN instrument (FA1, bug 3b) is the previously-live snapshot displaced by the
// last reload: its already-sounding voices keep rendering (and receive note-offs) so a
// curve/param edit or bank refresh never cuts a ringing note. It receives NO note-ons.
// A racing reload can briefly leave the same pointer in both slots (live_ was loaded
// before the swap, draining_ after); collapse that to live-only so one engine is never
// advanced twice per frame.
LoadedInstrument* inst = live_.load(std::memory_order_acquire);
const std::uint64_t heldGen = inst ? inst->installedAt : 0;
LoadedInstrument* drain = draining_.load(std::memory_order_acquire);
if (drain == inst) drain = nullptr;
std::uint64_t heldGen = 0;
if (inst && drain) {
heldGen = inst->installedAt < drain->installedAt ? inst->installedAt
: drain->installedAt;
} else if (inst) {
heldGen = inst->installedAt;
} else if (drain) {
heldGen = drain->installedAt;
}
processGeneration_.store(heldGen, std::memory_order_release);
// Marshal MIDI note-on/off from the event input into the voice engine. Tier 0 maps
// events at block granularity (no per-event sample-offset split) — audible timing is
// within one block, adequate for Tier 0; sample-accurate scheduling is a later tier.
if (inst && data.inputEvents) {
// Note-offs also route to the DRAIN engine so a note held across a reload releases
// its old-snapshot voice too (otherwise it would sustain until the next reload).
if (data.inputEvents) {
const int32 count = data.inputEvents->getEventCount();
for (int32 i = 0; i < count; ++i) {
Event e;
@@ -569,12 +601,14 @@ tresult PLUGIN_API ReaSamplerProcessor::process(ProcessData& data) {
// A note-on with velocity 0 is a note-off by MIDI convention.
const int vel = static_cast<int>(e.noteOn.velocity * 127.0f + 0.5f);
if (vel <= 0) {
inst->engine.noteOff(e.noteOn.pitch);
} else {
if (inst) inst->engine.noteOff(e.noteOn.pitch);
if (drain) drain->engine.noteOff(e.noteOn.pitch);
} else if (inst) {
inst->engine.noteOn(e.noteOn.pitch, vel);
}
} else if (e.type == Event::kNoteOffEvent) {
inst->engine.noteOff(e.noteOff.pitch);
if (inst) inst->engine.noteOff(e.noteOff.pitch);
if (drain) drain->engine.noteOff(e.noteOff.pitch);
}
}
}
@@ -598,12 +632,16 @@ tresult PLUGIN_API ReaSamplerProcessor::process(ProcessData& data) {
}
}
}
if (inst) {
if (inst || drain) {
const std::uint32_t off = previewOffRequest_.load(std::memory_order_acquire);
const std::uint16_t offSeq = static_cast<std::uint16_t>(off >> 16);
if (offSeq != 0 && offSeq != previewOffConsumed_) {
previewOffConsumed_ = offSeq;
inst->engine.noteOff(static_cast<int>(off & 0xFF));
// Route the preview note-off to BOTH engines (mirror of the host note-off): a
// preview held across a reload — e.g. a curve edit committed mid-press — must
// release the old-snapshot voice now draining, not just the (empty) live engine.
if (inst) inst->engine.noteOff(static_cast<int>(off & 0xFF));
if (drain) drain->engine.noteOff(static_cast<int>(off & 0xFF));
}
}
@@ -639,10 +677,14 @@ tresult PLUGIN_API ReaSamplerProcessor::process(ProcessData& data) {
if (ch0 && ch1) {
// Stereo: clear both, render L/R. A mono sample plays dual-mono via the engine's stereo
// path (both channels equal), so a mono capture in stereo mode is centered, not silent.
// The DRAIN engine's ringing tails ADD on top (render mixes into the cleared buffer).
for (int32 i = 0; i < frames; ++i) { ch0[i] = 0.f; ch1[i] = 0.f; }
if (inst) {
inst->engine.render(ch0, ch1, static_cast<std::size_t>(frames));
}
if (drain) {
drain->engine.render(ch0, ch1, static_cast<std::size_t>(frames));
}
// Any channels beyond the first two mirror ch0 (defensive — REAPER negotiates 1 or 2).
for (int32 ch = 2; ch < out.numChannels; ++ch) {
if (float* buf = out.channelBuffers32[ch]) {
@@ -665,6 +707,9 @@ tresult PLUGIN_API ReaSamplerProcessor::process(ProcessData& data) {
if (inst) {
inst->engine.render(ch0, static_cast<std::size_t>(frames));
}
if (drain) {
drain->engine.render(ch0, static_cast<std::size_t>(frames));
}
float peak = 0.f;
for (int32 i = 0; i < frames; ++i) {
const float a = ch0[i] < 0.f ? -ch0[i] : ch0[i];
@@ -679,10 +724,12 @@ tresult PLUGIN_API ReaSamplerProcessor::process(ProcessData& data) {
}
// Report silence only when nothing is loaded (lets the host optimize when idle).
// With an instrument loaded we clear the flag so a ringing voice is not skipped.
out.silenceFlags = inst ? 0 : ((out.numChannels >= 64)
? ~0ULL
: ((1ULL << out.numChannels) - 1));
// With an instrument loaded — or a drain snapshot still ringing out — we clear the
// flag so a ringing voice is not skipped.
out.silenceFlags = (inst || drain) ? 0
: ((out.numChannels >= 64)
? ~0ULL
: ((1ULL << out.numChannels) - 1));
return kResultOk;
}