From ad3940c81c5330fd291576ed73fe1db10d592ed0 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Tue, 10 Nov 2026 06:08:39 +0700 Subject: [PATCH 1/6] fix(harmony): bound moderate-confidence Music guide key retarget without indefinite stale key --- engine/builtin_fx/HarmonyMusicKey.h | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/engine/builtin_fx/HarmonyMusicKey.h b/engine/builtin_fx/HarmonyMusicKey.h index a633fa0f2..a860a9821 100644 --- a/engine/builtin_fx/HarmonyMusicKey.h +++ b/engine/builtin_fx/HarmonyMusicKey.h @@ -11,6 +11,15 @@ // Conservative confidence/hold; uncertain music falls back to Manual Key. // No FFT allocation, locks, async worker, trig or heap in process(). namespace armix::harmony { +// Do not indefinitely pin an obsolete key when a new candidate is stable but +// moderately confident. Strong evidence can retarget after 0.8s; weaker yet +// valid evidence needs 2.0s of consecutive agreement. A candidate that +// oscillates resets its hold in evaluate(), so this never forces ambiguity. +constexpr bool acceptMusicKeyCandidate(bool currentValid,bool sameKey, + float confidence,int stableWindows) noexcept { + if(stableWindows<8)return false; + return !currentValid||sameKey||confidence>0.10f||stableWindows>=20; +} struct MusicKeyEstimate final { int key=-1,scale=-1; // -1 means no reliable tonal evidence float confidence=0.f; @@ -145,9 +154,10 @@ class MusicKeyDetector final { uncertainFrames_=0; if(bestRoot!=candidate_||bestScale!=candidateScale_){ candidate_=bestRoot;candidateScale_=bestScale;candidateHold_=1; - }else if(candidateHold_<12)++candidateHold_; - if(candidateHold_>=8 && (!current_.valid() || - (current_.key==bestRoot&¤t_.scale==bestScale) || confidence>0.10f)) + }else if(candidateHold_<20)++candidateHold_; + if(acceptMusicKeyCandidate(current_.valid(), + current_.key==bestRoot&¤t_.scale==bestScale, + confidence,candidateHold_)) current_={bestRoot,bestScale,confidence}; } int decimation_=0,downCount_=0,period_=0,periodCount_=0,normalizeCount_=0; From 2fd3cff68d23a8a5a057fc09302aacaab1f1a1f2 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Tue, 10 Nov 2026 06:08:53 +0700 Subject: [PATCH 2/6] test(harmony): reproduce and reject indefinite moderately confident Music key staleness --- tests/harmony_music_key_test.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/harmony_music_key_test.cpp b/tests/harmony_music_key_test.cpp index 6196e9fb5..431c115af 100644 --- a/tests/harmony_music_key_test.cpp +++ b/tests/harmony_music_key_test.cpp @@ -28,6 +28,24 @@ static void sendChord(MusicKeyDetector& detector,double sampleRate, detector.process(left.data()+pos,right.data()+pos,std::min(127,size-pos)); } int main(){ + // Previously candidateHold_ stopped at 12 while a replacement key needed + // confidence > 0.10: valid 0.045..0.10 candidates could NEVER take over. + // Exercise the exact policy independently of spectral fixture sensitivity. + need(!acceptMusicKeyCandidate(true,false,.07f,7), + "weak music key changed before required hold"); + need(!acceptMusicKeyCandidate(true,false,.07f,12), + "moderate-confidence music key changed prematurely"); + need(!acceptMusicKeyCandidate(true,false,.07f,19), + "moderate-confidence music key changed before stable hold"); + need(acceptMusicKeyCandidate(true,false,.07f,20), + "stable moderately confident new music key stayed stuck forever"); + need(acceptMusicKeyCandidate(true,false,.12f,8), + "strong new key did not switch after initial stable hold"); + need(acceptMusicKeyCandidate(false,false,.06f,8), + "initial key was not acquired after stable hold"); + need(acceptMusicKeyCandidate(true,true,.06f,8), + "accepted key confidence update incorrectly blocked"); + MusicKeyDetector bad; need(!bad.prepare(8000.)&&!bad.prepare(384000.),"unsafe sample rate accepted"); for(const double rate:{48000.,96000.}){ From 163d88c049ccdb62cd2b527c29496a5541c93f8d Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Tue, 10 Nov 2026 06:31:19 +0700 Subject: [PATCH 3/6] perf(harmony): time-slice music key scoring and rotor upkeep; test PCM transitions across ASIO partitions --- engine/builtin_fx/HarmonyMusicKey.h | 79 +++++++++++++++++++++-------- tests/harmony_music_key_test.cpp | 38 ++++++++++++-- 2 files changed, 94 insertions(+), 23 deletions(-) diff --git a/engine/builtin_fx/HarmonyMusicKey.h b/engine/builtin_fx/HarmonyMusicKey.h index a860a9821..b6aeefa36 100644 --- a/engine/builtin_fx/HarmonyMusicKey.h +++ b/engine/builtin_fx/HarmonyMusicKey.h @@ -28,6 +28,12 @@ struct MusicKeyEstimate final { class MusicKeyDetector final { public: static constexpr int bins=36; // C3..B5, 3 octaves of music harmonics + // A 100ms chroma boundary must never score 24 tonal candidates in ONE + // ASIO sample. Spread two scores per decimated sample (<2.1ms total). + static constexpr int maxKeyScoresPerDecimatedSample=2; + static constexpr int maxRotorNormalizationsPerDecimatedSample=3; + int peakKeyScoresPerSample() const noexcept {return peakKeyScores_;} + int peakRotorNormalizationsPerSample() const noexcept {return peakNormalizations_;} bool prepare(double rate) noexcept { if(!std::isfinite(rate)||rate<32000.||rate>192000.)return false; decimation_=std::max(1,static_cast(std::round(rate/6000.))); @@ -49,7 +55,10 @@ class MusicKeyDetector final { stereoFold_.reset(); inC_.fill(0.f);inS_.fill(0.f); oscC_.fill(1.f);oscS_.fill(0.f);hist_.fill(0.f); - downCount_=0;periodCount_=0;normalizeCount_=0; + downCount_=0;periodCount_=0;normalizeCount_=0;normalizeIndex_=bins; + scoringActive_=false;scoringIndex_=0;scoringBest_=-2.f; + scoringSecond_=-2.f;scoringRoot_=-1;scoringScale_=-1; + scoringHist_.fill(0.f);peakKeyScores_=0;peakNormalizations_=0; framesSeen_=0;candidate_=-1;candidateScale_=-1;candidateHold_=0; silentFrames_=0;uncertainFrames_=0;windowEnergy_=0.;filtered_=0.f; current_={}; @@ -73,20 +82,31 @@ class MusicKeyDetector final { oscC_[k]=c*rotorC_[k]-s*rotorS_[k]; oscS_[k]=s*rotorC_[k]+c*rotorS_[k]; } + // Rotor housekeeping is also partitioned. Normalizing all 36 + // oscillators in the same ASIO sample caused an avoidable spike. if(++normalizeCount_>=4096){ - normalizeCount_=0; - for(int k=0;k0.5f&&std::isfinite(size)){ - oscC_[k]/=size;oscS_[k]/=size; - }else {oscC_[k]=1.f;oscS_[k]=0.f;} - } + normalizeCount_=0;normalizeIndex_=0; } + int normalized=0; + while(normalizeIndex_0.5f&&std::isfinite(size)){ + oscC_[k]/=size;oscS_[k]/=size; + }else {oscC_[k]=1.f;oscS_[k]=0.f;} + ++normalized; + } + peakNormalizations_=std::max(peakNormalizations_,normalized); if(++periodCount_>=period_){ periodCount_=0; evaluate(static_cast(std::sqrt(windowEnergy_/period_))); windowEnergy_=0.; } + // Score order matches the historical major-then-minor, root 0..11 + // loop. The scoring input is a frozen chroma snapshot, so the + // answer is independent of host callback partition boundaries. + continueScoring(); } } MusicKeyEstimate estimate() const noexcept{return current_;} @@ -110,6 +130,7 @@ class MusicKeyDetector final { return dot/std::sqrt(cc*pp+1.e-12f); } void evaluate(float rms) noexcept { + scoringActive_=false; if(!std::isfinite(rms)||rms<0.002f) { if(++silentFrames_>=10) { current_={};hist_.fill(0.f); @@ -132,18 +153,31 @@ class MusicKeyDetector final { hist_[k]=hist_[k]*0.91f+0.09f*(chroma[k]/sum); if(framesSeen_<1000)++framesSeen_; if(framesSeen_<12)return; // at least ~1.2 seconds of music - float best=-2.f,second=-2.f; - int bestRoot=-1,bestScale=-1; - for(int scale=0;scale<2;++scale) - for(int root=0;root<12;++root) { - const float score=correlation(hist_,scale?minorProfile:majorProfile,root); - if(score>best){ - second=best;best=score;bestRoot=root;bestScale=scale; - } else second=std::max(second,score); - } - // This is a KEY estimate, not chord detection. Relative minors and - // sparse/sustained notes often remain ambiguous: keep manual fallback. - const float confidence=std::clamp(best-second,0.f,1.f); + scoringHist_=hist_; // fixed-size snapshot; zero allocation + scoringIndex_=0;scoringBest_=-2.f;scoringSecond_=-2.f; + scoringRoot_=-1;scoringScale_=-1;scoringActive_=true; + } + void continueScoring() noexcept { + if(!scoringActive_)return; + int scored=0; + while(scoringIndex_<24 && scoredscoringBest_){ + scoringSecond_=scoringBest_;scoringBest_=score; + scoringRoot_=root;scoringScale_=scale; + } else scoringSecond_=std::max(scoringSecond_,score); + ++scoringIndex_;++scored; + } + peakKeyScores_=std::max(peakKeyScores_,scored); + if(scoringIndex_<24)return; + scoringActive_=false; + const float best=scoringBest_; + const int bestRoot=scoringRoot_,bestScale=scoringScale_; + // This is KEY estimation, not chord detection. A complete, frozen + // evaluation commits atomically only after all 24 candidate scores. + const float confidence=std::clamp(best-scoringSecond_,0.f,1.f); if(best<0.48f||confidence<0.045f) { candidate_=-1;candidateScale_=-1;candidateHold_=0; // A new ambiguous musical section cannot retain the previous @@ -161,6 +195,11 @@ class MusicKeyDetector final { current_={bestRoot,bestScale,confidence}; } int decimation_=0,downCount_=0,period_=0,periodCount_=0,normalizeCount_=0; + int normalizeIndex_=bins,scoringIndex_=0; + int scoringRoot_=-1,scoringScale_=-1,peakKeyScores_=0,peakNormalizations_=0; + float scoringBest_=-2.f,scoringSecond_=-2.f; + bool scoringActive_=false; + std::array scoringHist_{}; int framesSeen_=0,silentFrames_=0,uncertainFrames_=0; int candidate_=-1,candidateScale_=-1,candidateHold_=0; float sampleAlpha_=0.f,lowpassAlpha_=0.f,filtered_=0.f; diff --git a/tests/harmony_music_key_test.cpp b/tests/harmony_music_key_test.cpp index 431c115af..a7a108365 100644 --- a/tests/harmony_music_key_test.cpp +++ b/tests/harmony_music_key_test.cpp @@ -12,7 +12,7 @@ static void need(bool test,const char* reason){ if(!test){std::cerr<& pitches,double seconds){ + const std::vector& pitches,double seconds,int partition=127){ const int size=static_cast(seconds*sampleRate); std::vector left(static_cast(size)); std::vector right(static_cast(size)); @@ -24,8 +24,9 @@ static void sendChord(MusicKeyDetector& detector,double sampleRate, left[static_cast(i)]=static_cast(sample); right[static_cast(i)]=static_cast(sample); } - for(int pos=0;pos=.045f,"C-major confidence lower than acceptance"); + need(c.peakKeyScoresPerSample()>0 && + c.peakKeyScoresPerSample()<=MusicKeyDetector::maxKeyScoresPerDecimatedSample, + "music key correlation burst exceeded bounded audio sample budget"); + need(c.peakRotorNormalizationsPerSample()>0 && + c.peakRotorNormalizationsPerSample()<=MusicKeyDetector::maxRotorNormalizationsPerDecimatedSample, + "music rotor maintenance burst exceeded audio sample budget"); // Source-channel silence invalidates old guidance; Manual is safe. sendChord(c,rate,{0.},1.4); need(!c.estimate().valid(),"silent music source kept stale key"); @@ -89,6 +96,31 @@ int main(){ need(!opposed.estimate().valid(),"opposed source retained stale key"); } + // Real chroma PCM across C major -> A minor -> disconnected guide -> C + // major again, with alternate 64/127/256 ASIO-like partitions. This was + // missing from the old one-key-per-detector tests and catches stale-key + // revisions as well as callback-grain dependence. + for(int chunk:{64,127,256}){ + MusicKeyDetector live; + need(live.prepare(48000.),"transition music preparation failed"); + sendChord(live,48000.,{261.625565,329.627557,391.995436},4.5,chunk); + const auto c=live.estimate(); + need(c.valid()&&c.key==0&&c.scale==0, + "C major not acquired in live music transition sequence"); + sendChord(live,48000.,{220.,261.625565,329.627557},5.0,chunk); + const auto a=live.estimate(); + need(a.valid()&&a.key==9&&a.scale==1, + "new A minor key did not replace C major after stable evidence"); + sendChord(live,48000.,{0.},1.4,chunk); + need(!live.estimate().valid(),"stopped guide retained an obsolete key"); + sendChord(live,48000.,{261.625565,329.627557,391.995436},3.5,chunk); + const auto restarted=live.estimate(); + need(restarted.valid()&&restarted.key==0&&restarted.scale==0, + "music restarted on a different key failed to reacquire"); + need(live.peakKeyScoresPerSample()== + MusicKeyDetector::maxKeyScoresPerDecimatedSample, + "tiled tonal evaluation did not exercise its work budget"); + } // Manual Key stays unchanged when the music detector has no evidence; // selection is an audio-thread-only, valid other-ChannelId control packet. auto engine=std::make_unique(); From 1407f260867d5c96c40a748e259be46fe31448b9 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Tue, 10 Nov 2026 06:33:55 +0700 Subject: [PATCH 4/6] test(harmony): exercise native lease warm bypass at 48/96k across Wet/Space matrix, silence and rack toggles --- tests/harmony_bypass_reliability_test.cpp | 122 +++++++++++++++++++++- 1 file changed, 117 insertions(+), 5 deletions(-) diff --git a/tests/harmony_bypass_reliability_test.cpp b/tests/harmony_bypass_reliability_test.cpp index 2004e6359..5815177b6 100644 --- a/tests/harmony_bypass_reliability_test.cpp +++ b/tests/harmony_bypass_reliability_test.cpp @@ -22,16 +22,18 @@ static float singer(int n) noexcept { return static_cast(.16*std::sin(6.283185307179586*220.*t)+ .045*std::sin(6.283185307179586*440.*t)); } -static LiveChannelFxGraph::InstanceLease makeHarmony() { +static LiveChannelFxGraph::InstanceLease makeHarmony( + double rate=48000.,int quantum=64,float wet=1.f,float space=1.f) { auto p=std::make_unique(); - p->prepareToPlay(48000.,64); - assert(p->isReady() && p->getLatencySamples()==1536); + p->prepareToPlay(rate,quantum); + const int declaredLatency=2*static_cast(std::round(rate*.016)); + assert(p->isReady() && p->getLatencySamples()==declaredLatency); armix::harmony::Settings rich; - rich.voices=5;rich.wet=1.f;rich.space=1.f; + rich.voices=5;rich.wet=wet;rich.space=space; assert(p->queueAccepted(rich,9001)); return std::make_shared( std::move(p),ChannelHarmonyProcessor::catalogIdentity, - "ArSon Harmony",64); + "ArSon Harmony",quantum); } int main() { constexpr ChannelId vocal{7301}; @@ -145,6 +147,116 @@ int main() { assert(samplesCheckedWet>10000&&samplesCheckedDry>10000); assert(maxOldBypassDifference>.003f); // old skipped DSP history visibly diverges assert(!switched.rackBypassed(vocal)); + + // Cross-product regression across WET 0/50/100 and Space 0/60/100. + // Native FxInstance::render() is used at 48/96k and three ASIO periods. + // Includes a singing section, a silence/room-tail section, rack OFF/ON, + // slot OFF/ON, reentry after the singer returns, and exact dry PDC. + // Keep the original always-on/old-behavior A/B probe above, too. + struct Scenario { double rate;int quantum;float wet,space; }; + constexpr std::array cases{{ + {48000.,64,0.f,0.f},{48000.,128,0.f,.6f}, + {48000.,256,0.f,1.f},{48000.,64,.5f,0.f}, + {96000.,128,.5f,.6f},{96000.,256,.5f,1.f}, + {96000.,64,1.f,0.f},{96000.,128,1.f,.6f}, + {96000.,256,1.f,1.f} + }}; + double worstExcess=0.; + int verifiedWet=0,verifiedDry=0; + for(const auto cfg:cases) { + auto a=makeHarmony(cfg.rate,cfg.quantum,cfg.wet,cfg.space); + auto b=makeHarmony(cfg.rate,cfg.quantum,cfg.wet,cfg.space); + assert(a->latencySamples==b->latencySamples); + DynamicChannelFxLeaseBank::Assignment ref,probe; + ref.id=vocal;ref.format=ProcessingFormat::stereo; + ref.inserts.push_back(a); + probe.id=vocal;probe.format=ProcessingFormat::stereo; + probe.inserts.push_back(b); + DynamicChannelFxLeaseBank on,cycle; + const std::array ar{ref},br{probe}; + assert(DynamicChannelFxLeaseBank::prepare(3,1,ar,on)== + DynamicChannelFxLeaseBank::Fault::none); + assert(DynamicChannelFxLeaseBank::prepare(3,1,br,cycle)== + DynamicChannelFxLeaseBank::Fault::none); + const int total=static_cast(cfg.rate*.85); + const int latency=b->latencySamples; + const auto input=[&](int n) noexcept -> float { + if(n<0)return 0.f; + const double time=static_cast(n)/cfg.rate; + double amplitude=1.; + if(time>=.5&&time<.505)amplitude=1.-(time-.5)/.005; + else if(time>=.505&&time<.73)amplitude=0.; + else if(time>=.73&&time<.735)amplitude=(time-.73)/.005; + const double phase=6.283185307179586*220.*time; + return static_cast(amplitude*(.16*std::sin(phase)+ + .045*std::sin(phase*2.))); + }; + bool wasSlotOff=false,wasRackOff=false; + float lastWetL=0.f,lastWetR=0.f,lastOutL=0.f,lastOutR=0.f,lastDry=0.f; + int wetCount=0,dryCount=0; + for(int offset=0;offset(offset)/cfg.rate; + const bool slotOff=at>=.20&&at<.36; + const bool rackOff=at>=.61&&at<.73; + if(slotOff!=wasSlotOff){ + assert(cycle.setInsertBypass(3,1,vocal,0,b->instanceId,slotOff)); + wasSlotOff=slotOff; + } + if(rackOff!=wasRackOff){ + assert(cycle.setRackBypass(3,1,vocal,rackOff)); + wasRackOff=rackOff; + } + std::array rl{},rr{},pl{},pr{}; + for(int i=0;i(n)/cfg.rate; + const bool stableDry=(time>=.212&&time<.36)|| + (time>=.622&&time<.73); + const bool stableWet=(time>=.38&&time<.61)|| + (time>=.75); + const float wetSlope=std::max(std::abs(rl[i]-lastWetL), + std::abs(rr[i]-lastWetR)); + const float drySlope=std::abs(delayed-lastDry); + const float actualSlope=std::max(std::abs(pl[i]-lastOutL), + std::abs(pr[i]-lastOutR)); + if(n>latency){ + worstExcess=std::max(worstExcess,static_cast( + actualSlope-std::max(wetSlope,drySlope))); + assert(actualSlope-std::max(wetSlope,drySlope)<.045f); + } + lastWetL=rl[i];lastWetR=rr[i]; + lastOutL=pl[i];lastOutR=pr[i];lastDry=delayed; + assert(std::isfinite(pl[i])&&std::isfinite(pr[i])); + assert(std::abs(pl[i])<=4.f&&std::abs(pr[i])<=4.f); + if(stableDry){ + assert(std::abs(pl[i]-delayed)<.00003f&& + std::abs(pr[i]-delayed)<.00003f); + ++dryCount; + } + if(stableWet){ + // The exact live DSP state must match a never-bypassed + // lease, not merely have a visually smooth envelope. + assert(std::abs(pl[i]-rl[i])<.00003f&& + std::abs(pr[i]-rr[i])<.00003f); + ++wetCount; + } + } + } + assert(wetCount>0&&dryCount>0); + verifiedWet+=wetCount;verifiedDry+=dryCount; + assert(!cycle.rackBypassed(vocal)); + assert(!cycle.insertBypassed(vocal,0)); + } + std::cout<<"Harmony multi-rate wet/space bypass matrix PASS; " + <<"scenarios="<