From c68fd36a5c1afa93e0041a5396dc2f9e0ea3cce1 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 31 Oct 2026 23:04:07 +0700 Subject: [PATCH 1/4] fix(P0): keep live mixer inputs intact under in-place JUCE I/O using preallocated monitor PCM --- engine/core/PreparedMixerBlock.h | 60 +++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/engine/core/PreparedMixerBlock.h b/engine/core/PreparedMixerBlock.h index d6e1e7692..eb58541e7 100644 --- a/engine/core/PreparedMixerBlock.h +++ b/engine/core/PreparedMixerBlock.h @@ -165,12 +165,13 @@ struct RenderLevels final { class PreparedMixerBlock final { public: // The maximum is an implementation bound, NOT an OS device promise. - // Allocations are constructor-only; 20 strip lanes + optional 8 AUX + 2 master. + // Allocations are constructor-only; 20 strip lanes + 2 master + 2 + // monitor scratch + optional 8 AUX. No RT allocation or secondary DSP pass. static constexpr int hardFrameLimit = 8192; PreparedMixerBlock(const PreparedRenderGraph& plan, int maxFrames) : plan_(plan), capacity_(maxFrames), backing_(validCapacity(maxFrames) - ? static_cast(stripCount*2+2+ + ? static_cast(stripCount*2+4+ (plan.aux.active?auxBusCount*2:0))*maxFrames : 0u, 0.f) { if (!validCapacity(maxFrames)) @@ -200,9 +201,11 @@ class PreparedMixerBlock final { } masterL_=backing_.data()+static_cast(stripCount*2)*capacity_; masterR_=masterL_+capacity_; + monitorScratchL_=masterR_+capacity_; + monitorScratchR_=monitorScratchL_+capacity_; if(plan.aux.active){ for(int b=0;b(b*2)*capacity_; auxRight_[b]=auxLeft_[b]+capacity_; } @@ -281,19 +284,28 @@ class PreparedMixerBlock final { "DSP hook must be noexcept and realtime bounded"); if (observed) *observed={}; if (frames<=0 || !outputL || !outputR)return false; - std::fill_n(outputL,frames,0.f); - std::fill_n(outputR,frames,0.f); - if((monitorL==nullptr)!=(monitorR==nullptr) || - (monitorL && (!monitorGains || monitorL==monitorR || - monitorL==outputL || monitorL==outputR || - monitorR==outputL || monitorR==outputR))) + // A physical callback may expose aliased input/output PCM. Rejected + // buffer contracts still clear physical outputs; successful mixing + // must consume ALL capture sources before writing to output storage. + const bool portsValid=outputL!=outputR && + ((monitorL==nullptr)==(monitorR==nullptr)) && + (!monitorL || (monitorGains && monitorL!=monitorR && + monitorL!=outputL && monitorL!=outputR && + monitorR!=outputL && monitorR!=outputR)); + if (!portsValid || capacity_==0 || frames>capacity_ || + callbackInputCount<0) { + std::fill_n(outputL,frames,0.f); + if(outputR!=outputL)std::fill_n(outputR,frames,0.f); + if(monitorL)std::fill_n(monitorL,frames,0.f); + if(monitorR && monitorR!=monitorL)std::fill_n(monitorR,frames,0.f); return false; - if(monitorL) { - std::fill_n(monitorL,frames,0.f); - std::fill_n(monitorR,frames,0.f); } - if (capacity_==0 || frames>capacity_ || callbackInputCount<0) - return false; + float* const monitorBusL=monitorL?monitorScratchL_:nullptr; + float* const monitorBusR=monitorR?monitorScratchR_:nullptr; + if(monitorBusL) { + std::fill_n(monitorBusL,frames,0.f); + std::fill_n(monitorBusR,frames,0.f); + } for(int i=0;i0.f) { - monitorL[f]+=l[f]*monitorGain; - monitorR[f]+=r[f]*monitorGain; + monitorBusL[f]+=l[f]*monitorGain; + monitorBusR[f]+=r[f]*monitorGain; } peak=std::max(peak,std::max(std::abs(l[f]),std::abs(r[f]))); if(graph.sends.toMaster[strip]) { @@ -441,8 +453,8 @@ class PreparedMixerBlock final { l[f]*=gl; r[f]*=gr; if(monitorGain>0.f) { - monitorL[f]+=l[f]*monitorGain; - monitorR[f]+=r[f]*monitorGain; + monitorBusL[f]+=l[f]*monitorGain; + monitorBusR[f]+=r[f]*monitorGain; } peak=std::max(peak,std::max(std::abs(l[f]),std::abs(r[f]))); if(graph.sends.toMaster[strip]) { @@ -477,8 +489,8 @@ class PreparedMixerBlock final { if(f0.f) { - monitorL[f]+=vl*monitorGain; - monitorR[f]+=vr*monitorGain; + monitorBusL[f]+=vl*monitorGain; + monitorBusR[f]+=vr*monitorGain; } peak=std::max(peak,std::max(std::abs(vl),std::abs(vr))); if(bus.toMaster && !bus.mute){ @@ -498,6 +510,12 @@ class PreparedMixerBlock final { peakL=std::max(peakL,std::abs(outputL[f])); peakR=std::max(peakR,std::abs(outputR[f])); } + if(monitorBusL) { + // Physical input PCM is fully consumed and the independent MONITOR + // bus is already summed: publish once, with no extra FX traversal. + std::copy_n(monitorBusL,frames,monitorL); + std::copy_n(monitorBusR,frames,monitorR); + } if(observed){observed->masterPeakL=peakL; observed->masterPeakR=peakR;} return true; } @@ -557,5 +575,7 @@ class PreparedMixerBlock final { std::array rampPrimed_{}; float* masterL_=nullptr; float* masterR_=nullptr; + float* monitorScratchL_=nullptr; + float* monitorScratchR_=nullptr; }; } // namespace armix::mixer From 9961615a4ee11993b2256fb3afa9163301bb5151 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 31 Oct 2026 23:04:42 +0700 Subject: [PATCH 2/4] fix(P0): never clear hardware output before production native input capture PCM is consumed --- src/LiveDryRoutingLab.h | 45 ++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/LiveDryRoutingLab.h b/src/LiveDryRoutingLab.h index a363870cd..e7a9a139a 100644 --- a/src/LiveDryRoutingLab.h +++ b/src/LiveDryRoutingLab.h @@ -684,9 +684,15 @@ class LiveDryRoutingLab final { monitorPeakL_.store(0.f,std::memory_order_relaxed); monitorPeakR_.store(0.f,std::memory_order_relaxed); if(frames<=0)return true; - for(int c=0;carmix::device::LiveInputPortRegistry::limit) { topologyFault_.store(true,std::memory_order_release); - return true; + silence();return true; + } + if(topologyFault_.load(std::memory_order_acquire)) { + silence();silence();return true; } - if(topologyFault_.load(std::memory_order_acquire))return true; if(inputCount!=callbackInputCount_.load(std::memory_order_acquire)) { topologyFault_.store(true,std::memory_order_release); - return true; + silence();return true; } // A prepared graph does NOT authorize physical inputs: skip ALL // channel, AUX and MASTER vendor processing until explicit native ACK. @@ -707,7 +715,7 @@ class LiveDryRoutingLab final { if(!userPatched_.load(std::memory_order_acquire)) { fadeRemaining_=0; lastOutputL_=lastOutputR_=0.f; - return true; + silence();return true; } const auto generation=epoch(); auto lease=publisher_.acquire(); @@ -718,7 +726,7 @@ class LiveDryRoutingLab final { topologyFault_.store(true,std::memory_order_release); fadeRemaining_=0; lastOutputL_=lastOutputR_=0.f; - return true; + silence();return true; } // Callback destinations come only from the leased immutable graph. // If a driver restarts or changes topology, silence all outputs. @@ -728,7 +736,7 @@ class LiveDryRoutingLab final { outLeft>=outputCount||outRight>=outputCount|| !outputs[outLeft]||!outputs[outRight]){ topologyFault_.store(true,std::memory_order_release); - return true; + silence();return true; } float* const mainL=outputs[outLeft]; float* const mainR=outputs[outRight]; @@ -741,7 +749,7 @@ class LiveDryRoutingLab final { l==r||l==outLeft||l==outRight||r==outLeft||r==outRight|| !outputs[l]||!outputs[r]) { topologyFault_.store(true,std::memory_order_release); - return true; + silence();return true; } monitorL=outputs[l];monitorR=outputs[r]; if(actualMonitorPair)*actualMonitorPair={{l,r}}; @@ -766,12 +774,16 @@ class LiveDryRoutingLab final { fadeRemaining_=rampFrames; fadeFromL_=fadeFromR_=0.f; lastOutputL_=lastOutputR_=0.f; - return true; + silence();return true; } // Large/variable JUCE quantum is chunked using stack pointer views; // no heap allocation or new processing object inside callback. std::array shifted{}; const int max=snapshot->prepared->maxFramesPerPlugin(); + if(max<=0) { + topologyFault_.store(true,std::memory_order_release); + silence();return true; + } float peakL=0.f,peakR=0.f,monitorPeakL=0.f,monitorPeakR=0.f; std::array perChannelPeaks{}; std::array perAuxPeaks{}; @@ -787,10 +799,8 @@ class LiveDryRoutingLab final { monitorR?monitorR+offset:nullptr)) { // A failed DSP pass must never leak partially rendered // MAIN/MONITOR PCM into an armed physical output. - for(int c=0;c Date: Sat, 31 Oct 2026 23:05:35 +0700 Subject: [PATCH 3/4] test(P0): 10k-block zero-allocation production PCM with in-place physical MAIN/MONITOR aliases --- tests/prepared_mixer_block_test.cpp | 55 +++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/prepared_mixer_block_test.cpp b/tests/prepared_mixer_block_test.cpp index 80a7a31b5..79baa9d77 100644 --- a/tests/prepared_mixer_block_test.cpp +++ b/tests/prepared_mixer_block_test.cpp @@ -187,4 +187,59 @@ int main() { assert(near(right[63],old,0.0001f)); } + // P0 PRODUCTION: native 10CH kernel must safely support in-place JUCE + // capture/output backing memory without clearing a later physical input. + // Independent MONITOR summing must also defer physical writes until + // source PCM for ALL channels is consumed. No new callback/DSP graph. + { + constexpr std::array callbackPorts{0,1,2,3}; + ChannelRoutingConfig route; + for(int i=0;i<4;++i) + route.stripsAndMaster.strips[i]={ + SourceMode::mono,i,-1,1.f,0.f,false,false}; + SendRequest sends4; + for(int i=0;i<4;++i)sends4.toMaster[i]=true; + PreparedRenderGraph plan;RenderError fault; + assert(PreparedRenderGraph::compile(route,callbackPorts, + sends4,plan,fault)); + PreparedMixerBlock live(plan,32); + std::array monGains{}; + for(int i=0;i<4;++i)monGains[i]=.10f*(i+1); + std::array a{},b{},c{},d{}; + for(int i=0;i<32;++i){ + a[i]=.16f+.001f*i; b[i]=.25f-.001f*i; + c[i]=.13f+.002f*i; d[i]=.06f+.001f*i; + } + const float* sources[]={a.data(),b.data(),c.data(),d.data()}; + std::array expectedL{},expectedR{},expectedML{},expectedMR{}; + const auto noVst=[](int,float*,float*,int) noexcept {}; + assert(live.process(sources,4,expectedL.data(),expectedR.data(),32, + noVst,nullptr,nullptr,&monGains, + expectedML.data(),expectedMR.data())); + auto inPlaceL=a,inPlaceR=b,inPlaceML=c,inPlaceMR=d; + const float* shared[]={inPlaceL.data(),inPlaceR.data(), + inPlaceML.data(),inPlaceMR.data()}; + const auto beforeInPlace=allocations.load(std::memory_order_relaxed); + assert(live.process(shared,4,inPlaceL.data(),inPlaceR.data(),32, + noVst,nullptr,nullptr,&monGains, + inPlaceML.data(),inPlaceMR.data())); + assert(allocations.load(std::memory_order_relaxed)==beforeInPlace); + for(int i=0;i<32;++i){ + assert(near(inPlaceL[i],expectedL[i])); + assert(near(inPlaceR[i],expectedR[i])); + assert(near(inPlaceML[i],expectedML[i])); + assert(near(inPlaceMR[i],expectedMR[i])); + } + assert(expectedL[0]>0.f&&expectedML[0]>0.f); + // Prolonged production PCM with 0 FX does not allocate or grow + // retained buffers on the callback, including MONITOR processing. + for(int k=0;k<10000;++k) { + inPlaceL=a;inPlaceR=b;inPlaceML=c;inPlaceMR=d; + assert(live.process(shared,4,inPlaceL.data(),inPlaceR.data(), + 32,noVst,nullptr,nullptr,&monGains, + inPlaceML.data(),inPlaceMR.data())); + } + assert(allocations.load(std::memory_order_relaxed)==beforeInPlace); + } + } From 6f3e166ed3d179c0b0a94b8e0502cc39336ae33a Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 31 Oct 2026 23:06:11 +0700 Subject: [PATCH 4/4] fix(P0): deduplicate fail-silent output gate and correct live routing buffer invariant --- src/LiveDryRoutingLab.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/LiveDryRoutingLab.h b/src/LiveDryRoutingLab.h index e7a9a139a..8440da6d9 100644 --- a/src/LiveDryRoutingLab.h +++ b/src/LiveDryRoutingLab.h @@ -703,7 +703,7 @@ class LiveDryRoutingLab final { silence();return true; } if(topologyFault_.load(std::memory_order_acquire)) { - silence();silence();return true; + silence();return true; } if(inputCount!=callbackInputCount_.load(std::memory_order_acquire)) { topologyFault_.store(true,std::memory_order_release); @@ -711,7 +711,7 @@ class LiveDryRoutingLab final { } // A prepared graph does NOT authorize physical inputs: skip ALL // channel, AUX and MASTER vendor processing until explicit native ACK. - // One atomic read; output and every meter were already zeroed above. + // One atomic read; outputs are cleared by the fail-silent gate above. if(!userPatched_.load(std::memory_order_acquire)) { fadeRemaining_=0; lastOutputL_=lastOutputR_=0.f; @@ -766,7 +766,7 @@ class LiveDryRoutingLab final { fadeFromR_=lastOutputR_; } // Strict output mute wins over every topology fade. Output buffers - // were zeroed before reading the graph, so skip all muted DSP work. + // are cleared by the fail-silent gate, so skip all muted DSP work. // Prime a 256-sample fade FROM ZERO for the first unmuted block. // Never interpolate any previously audible sample while muted. if(masterMuted) {