diff --git a/engine/core/DynamicPreparedMixerBlock.h b/engine/core/DynamicPreparedMixerBlock.h index 2e4c45a93..9f02c9834 100644 --- a/engine/core/DynamicPreparedMixerBlock.h +++ b/engine/core/DynamicPreparedMixerBlock.h @@ -3,6 +3,7 @@ #include "DynamicChannelRoster.h" #include #include +#include #include #include #include @@ -62,9 +63,26 @@ class DynamicPreparedMixerBlock final { }; // Immutable topology + atomics for live control targets. Callback-only // smoothed state is preallocated per accepted graph revision. + // One atomic pair prevents a PAN/fader request crossing the callback + // between separate L and R loads. A torn stereo target can cause a + // transient image/level jump even with sample ramps. 64-bit atomics are + // required lock-free: callback must never invoke a hidden mutex. + struct StereoGainPair final { + float left=1.f,right=1.f; + }; + static constexpr std::uint64_t packGainPair(float left,float right) noexcept { + return (static_cast(std::bit_cast(right))<<32) | + std::bit_cast(left); + } + static constexpr StereoGainPair unpackGainPair(std::uint64_t both) noexcept { + return {std::bit_cast(static_cast(both)), + std::bit_cast(static_cast(both>>32))}; + } struct ChannelTarget final { - std::atomic left{1.f},right{1.f}; - float appliedL=1.f,appliedR=1.f; + static_assert(std::atomic::is_always_lock_free, + "Realtime stereo mixer requires lock-free 64-bit atomic targets"); + std::atomic both{packGainPair(1.f,1.f)}; + float appliedL=1.f,appliedR=1.f; // audio thread only }; struct SendTarget final { std::atomic gain{1.f}; @@ -93,8 +111,8 @@ class DynamicPreparedMixerBlock final { gain*(pan>0.f?1.f-pan:1.f); const float right=mono?gain*std::sqrt(.5f*(1.f+pan)): gain*(pan<0.f?1.f+pan:1.f); - channelTargets_[i].left.store(left,std::memory_order_release); - channelTargets_[i].right.store(right,std::memory_order_release); + channelTargets_[i].both.store( + packGainPair(left,right),std::memory_order_release); return true; } return false; @@ -337,8 +355,8 @@ class DynamicPreparedMixerBlock final { next.channelTargets_=std::make_unique(rows.size()); for(std::size_t i=0;i(ramp); const float stepR=(goalR-controls.appliedR)/static_cast(ramp); diff --git a/tests/mixer_routing_plan_test.cpp b/tests/mixer_routing_plan_test.cpp index 1e8c0ad72..19517fb1c 100644 --- a/tests/mixer_routing_plan_test.cpp +++ b/tests/mixer_routing_plan_test.cpp @@ -868,6 +868,22 @@ static void verifyDynamicChannelIdLiveControls() { assert(host.setChannelMix(123,1,group,0.f,0.f)); assert(host.setParallelSendGain(123,1,mic,fx,false,.5f)); assert(render()&&close(left[63],.8f*center*.5f*.5f)); + // M3E: a stereo fader/PAN update is one coherent atomic coefficient pair, + // not independent L/R stores that the callback can observe half-applied. + // With exclusive GROUP muted, the MIC FX send alone provides both paths. + assert(host.setParallelSendGain(123,1,mic,fx,false,1.f)); + assert(host.setChannelMix(123,1,mic,.8f,-1.f)); + assert(render()); + assert(close(left[63],.8f*.8f)); // hard left, all FX return + assert(close(right[63],0.f)); + assert(host.setChannelMix(123,1,mic,.8f,1.f)); + assert(render()); + assert(close(left[63],0.f)); + assert(close(right[63],.8f*.8f)); // hard right, same graph + assert(host.setChannelMix(123,1,mic,.8f,0.f)); + assert(render()); + assert(close(left[63],.8f*.8f*center)); + assert(close(right[63],.8f*.8f*center)); const auto retained=host.acceptedRevision(); assert(retained==1); // The native physical MUTE gate applies even if a live target is nonzero.