Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions engine/core/PreparedMixerBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::size_t>(stripCount*2+2+
? static_cast<std::size_t>(stripCount*2+4+
(plan.aux.active?auxBusCount*2:0))*maxFrames
: 0u, 0.f) {
if (!validCapacity(maxFrames))
Expand Down Expand Up @@ -200,9 +201,11 @@ class PreparedMixerBlock final {
}
masterL_=backing_.data()+static_cast<std::size_t>(stripCount*2)*capacity_;
masterR_=masterL_+capacity_;
monitorScratchL_=masterR_+capacity_;
monitorScratchR_=monitorScratchL_+capacity_;
if(plan.aux.active){
for(int b=0;b<auxBusCount;++b){
auxLeft_[b]=masterR_+capacity_+
auxLeft_[b]=monitorScratchR_+capacity_+
static_cast<std::size_t>(b*2)*capacity_;
auxRight_[b]=auxLeft_[b]+capacity_;
}
Expand Down Expand Up @@ -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;i<stripCount;++i) {
std::fill_n(left_[i],frames,0.f);
std::fill_n(right_[i],frames,0.f);
Expand Down Expand Up @@ -428,8 +440,8 @@ class PreparedMixerBlock final {
if(f<ramp){lv+=dL;rv+=dR;}
l[f]*=lv;r[f]*=rv;
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]) {
Expand All @@ -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]) {
Expand Down Expand Up @@ -477,8 +489,8 @@ class PreparedMixerBlock final {
if(f<ramp)current+=step;
const float vl=l[f]*current,vr=r[f]*current;
if(!bus.mute && monitorGain>0.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){
Expand All @@ -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;
}
Expand Down Expand Up @@ -557,5 +575,7 @@ class PreparedMixerBlock final {
std::array<bool,stripCount> rampPrimed_{};
float* masterL_=nullptr;
float* masterR_=nullptr;
float* monitorScratchL_=nullptr;
float* monitorScratchR_=nullptr;
};
} // namespace armix::mixer
49 changes: 33 additions & 16 deletions src/LiveDryRoutingLab.h
Original file line number Diff line number Diff line change
Expand Up @@ -684,30 +684,38 @@ 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;c<outputCount;++c)
if(outputs && outputs[c])
juce::FloatVectorOperations::clear(outputs[c],frames);
// Defer physical output writes until all capture pointers have been
// read into the preallocated native graph. Clearing outputs here can
// erase aliased JUCE/driver input PCM before the CH strips consume it.
// Every failed/unpatched/MUTED route still uses this fail-silent gate.
const auto silence=[&]() noexcept {
if(!outputs)return;
for(int c=0;c<outputCount;++c)
if(outputs[c])juce::FloatVectorOperations::clear(outputs[c],frames);
};
// Do not require callback ports 0/1 to be writable when MAIN is
// actually routed to another verified pair. Validate the selected
// destinations against the leased graph below.
if(!outputs||outputCount<2 ||
outputCount!=callbackOutputCount_.load(std::memory_order_acquire) ||
inputCount<0 || inputCount>armix::device::LiveInputPortRegistry::limit) {
topologyFault_.store(true,std::memory_order_release);
return true;
silence();return true;
}
if(topologyFault_.load(std::memory_order_acquire)) {
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.
// 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;
return true;
silence();return true;
}
const auto generation=epoch();
auto lease=publisher_.acquire();
Expand All @@ -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.
Expand All @@ -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];
Expand All @@ -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}};
Expand All @@ -758,20 +766,24 @@ 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) {
masterMonoPolicy_.primeWhileMuted(masterMono);
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<const float*,armix::device::LiveInputPortRegistry::limit> 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<float,count> perChannelPeaks{};
std::array<float,auxCount> perAuxPeaks{};
Expand All @@ -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<outputCount;++c)
if(outputs[c])juce::FloatVectorOperations::clear(outputs[c],frames);
topologyFault_.store(true,std::memory_order_release);
return true;
silence();return true;
}
for(int ch=0;ch<count;++ch)
perChannelPeaks[ch]=std::max(perChannelPeaks[ch],level.channelPeak[ch]);
Expand Down Expand Up @@ -829,6 +839,13 @@ class LiveDryRoutingLab final {
}
}
}
// All input PCM has been staged/processed by the native graph.
// Only now silence unselected physical ports; do not clobber an
// aliased MAIN/MONITOR destination reported twice by a driver.
for(int c=0;c<outputCount;++c)
if(outputs[c] && outputs[c]!=mainL && outputs[c]!=mainR &&
outputs[c]!=monitorL && outputs[c]!=monitorR)
juce::FloatVectorOperations::clear(outputs[c],frames);
monitorPeakL_.store(monitorPeakL,std::memory_order_relaxed);
monitorPeakR_.store(monitorPeakR,std::memory_order_relaxed);
peakL_.store(peakL,std::memory_order_relaxed);
Expand Down
55 changes: 55 additions & 0 deletions tests/prepared_mixer_block_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int,4> 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<float,stripCount+auxBusCount> monGains{};
for(int i=0;i<4;++i)monGains[i]=.10f*(i+1);
std::array<float,32> 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<float,32> 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);
}

}
Loading