diff --git a/engine/builtin_fx/ChannelHarmonyProcessor.h b/engine/builtin_fx/ChannelHarmonyProcessor.h index a5eb34930..b54cea5cc 100644 --- a/engine/builtin_fx/ChannelHarmonyProcessor.h +++ b/engine/builtin_fx/ChannelHarmonyProcessor.h @@ -33,7 +33,7 @@ class ChannelHarmonyProcessor final : public juce::AudioProcessor, return b.getMainInputChannelSet()==juce::AudioChannelSet::stereo() && b.getMainOutputChannelSet()==juce::AudioChannelSet::stereo(); } - void prepareToPlay(double rate,int) override { + void prepareToPlay(double rate,int maximumExpectedSamplesPerBlock) override { ready_=dsp_.prepare(rate)&&guide_.prepare(rate)&&dsp_.setSettings(accepted_); guideSource_.store(accepted_.guideMode==1?accepted_.guideChannelId:0, std::memory_order_release); @@ -41,7 +41,16 @@ class ChannelHarmonyProcessor final : public juce::AudioProcessor, guideObservedScale_.store(-1,std::memory_order_relaxed); guideConfidence_.store(0.f,std::memory_order_relaxed); publishEffectiveKey(); - if(ready_) {sampleRate_=rate;setLatencySamples(dsp_.latencySamples());} + if(ready_) { + // FxInstance samples the AudioProcessor rate once, when its fixed + // 8 ms bypass envelope is prepared. Native built-ins are prepared + // directly; without JUCE rate metadata it otherwise sees zero + // and fades in only 64 samples at real 48/96 kHz device rates. + setRateAndBufferSizeDetails(rate, + std::max(1,maximumExpectedSamplesPerBlock)); + sampleRate_=rate; + setLatencySamples(dsp_.latencySamples()); + } } void releaseResources() override {ready_=false;} void processBlock(juce::AudioBuffer& samples,juce::MidiBuffer&) override { @@ -50,9 +59,16 @@ class ChannelHarmonyProcessor final : public juce::AudioProcessor, consumeRealtimeEdits(); // Missing/deleted/disconnected source must not leave a stale accepted // tonal estimate running indefinitely. Manual Key is the fallback. + const int guideTimeout=static_cast(sampleRate_*2.); + const bool guideWasFresh=guideAge_<=guideTimeout; guideAge_=std::min(guideAge_+samples.getNumSamples(), static_cast(sampleRate_*3.)); - if(guideAge_>static_cast(sampleRate_*2.)){ + if(guideWasFresh&&guideAge_>guideTimeout){ + // The guide source went missing. A detector that still retains + // old chroma would publish the old key on the FIRST resumed + // source block, before gathering fresh evidence. Invalidate once + // at the missing-source boundary, not at every audio callback. + guide_.reset(); dsp_.setGuideKey(-1,-1); guideObservedKey_.store(-1,std::memory_order_relaxed); guideObservedScale_.store(-1,std::memory_order_relaxed); diff --git a/tests/harmony_bypass_reliability_test.cpp b/tests/harmony_bypass_reliability_test.cpp index a36135bbc..c23806e1d 100644 --- a/tests/harmony_bypass_reliability_test.cpp +++ b/tests/harmony_bypass_reliability_test.cpp @@ -28,6 +28,9 @@ static LiveChannelFxGraph::InstanceLease makeHarmony( p->prepareToPlay(rate,quantum); const int declaredLatency=2*static_cast(std::round(rate*.016)); assert(p->isReady() && p->getLatencySamples()==declaredLatency); + // Prepared built-in host metadata drives the live FxInstance envelope. + // A zero JUCE rate shortened 8 ms to just 64 frames at 48/96 kHz. + assert(std::abs(p->getSampleRate()-rate)<1.e-6); armix::harmony::Settings rich; rich.voices=5;rich.wet=wet;rich.space=space; assert(p->queueAccepted(rich,9001)); @@ -252,6 +255,18 @@ int main() { verifiedWet+=wetCount;verifiedDry+=dryCount; assert(!cycle.rackBypassed(vocal)); assert(!b->bypassRequested.load(std::memory_order_acquire)); + // Test the actual FX lease envelope, not an isolated Fade helper. + // Every WET/Space and callback-size variant must retain 8 ms at the + // device rate rather than falling back to 64 samples. + b->bypassFade.request(true); + int fadeFrames=0; + while(b->bypassFade.transitioning()&&fadeFrames<4096){ + b->bypassFade.next(); + ++fadeFrames; + } + assert(fadeFrames<4096); + const int expectedFade=static_cast(std::round(cfg.rate*.008)); + assert(std::abs(fadeFrames-expectedFade)<=1); } std::cout<<"Harmony multi-rate wet/space bypass matrix PASS; " <<"scenarios="<(i*128+j)/48000.; + l[j]=r[j]=static_cast(.20*std::sin( + 6.28318530718*220.*t)); + } + bank(vocal,l,r,128); // singer remains live during guide loss + for(int j=0;j<128;++j) + assert(std::isfinite(l[j])&&std::isfinite(r[j])); } + assert(live->observedNote()>=0); // no lead/pitch-analysis interruption assert(live->observedMusicKey()==-1); assert(live->effectiveKeyCode()==14); // music absent: manual D minor + // Regression: resuming a missing music ChannelId must NOT reuse its + // previously acquired key after two seconds of missing PCM. The old + // detector retained a valid C-major estimate and immediately published + // it on the first resumed source block, before any new tonal evidence. + { + float resumedL[128]{},resumedR[128]{}; + for(int j=0;j<128;++j){ + const double t=static_cast(j)/48000.; + resumedL[j]=resumedR[j]=static_cast(.09*( + std::sin(6.28318530718*220.*t)+ + std::sin(6.28318530718*261.625565*t)+ + std::sin(6.28318530718*329.627557*t))); + } + const float originalL=resumedL[17],originalR=resumedR[17]; + bank(music,resumedL,resumedR,128); + assert(resumedL[17]==originalL&&resumedR[17]==originalR); + assert(live->observedMusicKey()==-1); + assert(live->effectiveKeyCode()==14); // no stale C major flash + } + // Fresh A-minor guide must eventually retune after reacquisition. + for(int block=0;block<1450;++block){ + float l[128]{},r[128]{}; + for(int j=0;j<128;++j){ + const double t=static_cast(block*128+j)/48000.; + l[j]=r[j]=static_cast(.09*( + std::sin(6.28318530718*220.*t)+ + std::sin(6.28318530718*261.625565*t)+ + std::sin(6.28318530718*329.627557*t))); + } + bank(music,l,r,128); + } + assert(live->observedMusicKey()==9&&live->observedMusicScale()==1); + assert(live->effectiveKeyCode()==21); juce::MemoryBlock saved; live->getStateInformation(saved); // Realtime processor owns fixed DSP and reflection rings; production