diff --git a/engine/builtin_fx/HarmonyDsp.h b/engine/builtin_fx/HarmonyDsp.h index ba6722942..7b24eacc0 100644 --- a/engine/builtin_fx/HarmonyDsp.h +++ b/engine/builtin_fx/HarmonyDsp.h @@ -12,6 +12,7 @@ #include "HarmonyRoomFocus.h" #include "HarmonyStageSpatial.h" #include "HarmonyPitchSynchronous.h" +#include "HarmonyPeriodContinuity.h" #include "HarmonyLeadTune.h" #include "HarmonyVoicePlanner.h" @@ -111,6 +112,7 @@ class Engine final { leadRendering_=false;voicePlanner_.reset(); pitchSyncBlend_.fill(0.f);pitchSyncFiltered_.fill(0.f); pitchSyncValid_.fill(0.f); + periodContinuity_.reset(); sourcePeriodSamples_=0.f;sourceConfidence_=0.f;sampleClock_=0; uncertainPitchHops_=0;weakSyncHops_=0;pitchSyncStable_=false; pitchSyncReferenceEnabled_=true; @@ -532,7 +534,7 @@ class Engine final { sourceConfidence_=0.f;voiced_=false;noteHold_=0; candidateNote_=-1;currentNote_=-1; uncertainPitchHops_=0;weakSyncHops_=0;pitchSyncStable_=false; - sourcePeriodSamples_=0.f; + periodContinuity_.reset();sourcePeriodSamples_=0.f; leadTune_.release();voicePlanner_.reset(); } // Keep the last known voiced note through at most five energetic but @@ -593,13 +595,18 @@ class Engine final { if(note!=candidateNote_){candidateNote_=note;noteHold_=1;} // Saturate the hold counter: a multi-hour sustained note must not // overflow a signed int in the realtime thread. - else if(noteHold_<3)++noteHold_; - // Two consistent windows for both phrase onset and melodic transitions; + else if(noteHold_<4)++noteHold_; + // Two consistent windows for ordinary transitions; a sudden octave + // (or wider interval) needs four independent analysis windows. + // This avoids re-planning the entire backing choir for two noisy + // high-harmonic windows in an otherwise sustained singer phrase. // do not generate provisional harmony on a single noisy transient. const float confidence=std::clamp( correlations[static_cast(bestLag)],0.f,1.f); - if(noteHold_<2){ - leadTune_.release(); + const int requiredNoteHops=currentNote_>=0&& + std::abs(note-currentNote_)>=9?4:2; + if(noteHold_(rate_/hz); @@ -612,25 +619,10 @@ class Engine final { ensemble_.beginNote(); } currentNote_=note;voiced_=true;tonalTransitionPending_=false; - if(sourcePeriodSamples_>0.f&& - std::abs(newPeriod-sourcePeriodSamples_)>.25f*sourcePeriodSamples_){ - // A confirmed octave-size transition is a new periodicity. - // Re-seed grains but let the 5/30ms fallback crossfades mask - // this discontinuity instead of resetting output gains to zero. - pitchSync_.reset(); - pitchSyncStable_=false;weakSyncHops_=0; - sourcePeriodSamples_=newPeriod; - }else if(sourcePeriodSamples_<=0.f){ - sourcePeriodSamples_=newPeriod; - }else{ - // Six-millisecond F0 windows fluctuate on long singer vibrato. - // Slew the source period at note-rate, not as discrete PSOLA - // grain-window jumps. A 5% bounded hop still tracks a real - // melodic transition promptly; target intervals remain separate. - const float bounded=std::clamp(newPeriod, - sourcePeriodSamples_*.95f,sourcePeriodSamples_*1.05f); - sourcePeriodSamples_+=.30f*(bounded-sourcePeriodSamples_); - } + // Retain overlapping grains across even a real musical leap. + // A brief octave readout caused by a loud second harmonic cannot + // reset all five backing renderers and create a micro-dropout. + sourcePeriodSamples_=periodContinuity_.observe(newPeriod); sourceConfidence_=confidence; // Hysteresis prevents marginal correlation confidence causing // rapid PSOLA/granular alternation. Silence disables it immediately. @@ -687,6 +679,7 @@ class Engine final { BackingDepthRoom depthRoom_{}; BackingStageSpatial stage_{}; BackingRoomFocus roomFocus_{}; + PeriodContinuity periodContinuity_{}; PitchSynchronousVoice pitchSync_{}; PitchSynchronousVoice leadShifter_{}; LeadTune leadTune_{}; diff --git a/engine/builtin_fx/HarmonyPeriodContinuity.h b/engine/builtin_fx/HarmonyPeriodContinuity.h new file mode 100644 index 000000000..9f63d3ccc --- /dev/null +++ b/engine/builtin_fx/HarmonyPeriodContinuity.h @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#pragma once +#include +#include + +// Realtime-safe period policy for one singer's measured microphone F0. +// A confident but incorrect harmonic/octave candidate can survive two 6ms +// analysis hops during dense vowel/chorus content. Never clear every +// pitch-synchronous overlap-add grain on that transient evidence. +// Distinguishes short outliers from a sustained melodic change, then +// slews true large transitions without discontinuous renderer resets. +// No heap, locks, trigonometry or device-specific state. +namespace armix::harmony { +class PeriodContinuity final { +public: + void reset() noexcept { + period_=0.f;pending_=0.f;pendingHops_=0;transitioning_=false; + } + float current() const noexcept {return period_;} + bool transitioning() const noexcept {return transitioning_;} + + float observe(float measured) noexcept { + if(!std::isfinite(measured)||measured<8.f||measured>3200.f) + return period_; + if(period_<=0.f) { + period_=measured; + return period_; + } + const float delta=measured-period_; + const float distance=std::abs(delta); + if(transitioning_) { + // A confirmed octave/large interval is a new phrase segment: + // track it within bounded 11% analysis-hop steps while the + // existing overlap-add windows retire naturally. + const float limit=.11f*period_; + period_+=std::clamp(delta,-limit,limit); + if(std::abs(measured-period_)<=.08f*period_) + transitioning_=false; + return period_; + } + if(distance>.25f*period_) { + // Correlation often prefers a temporary 2x/0.5x harmonic + // during loud vocal vowels. Require three similar windows + // before following a true large-period change. + if(pendingHops_>0 && + std::abs(measured-pending_)<=.12f*pending_) { + if(pendingHops_<3)++pendingHops_; + }else { + pending_=measured; + pendingHops_=1; + } + if(pendingHops_>=3) { + transitioning_=true;pendingHops_=0; + const float limit=.11f*period_; + period_+=std::clamp(delta,-limit,limit); + } + return period_; + } + pendingHops_=0;pending_=0.f; + // Small F0 wobble/vibrato must not quantize grain-window lengths. + const float bounded=std::clamp(measured, + period_*.95f,period_*1.05f); + period_+=.30f*(bounded-period_); + return period_; + } +private: + float period_=0.f,pending_=0.f; + int pendingHops_=0; + bool transitioning_=false; +}; +} // namespace armix::harmony diff --git a/tests/harmony_dsp_test.cpp b/tests/harmony_dsp_test.cpp index a867540e4..a55a4eab9 100644 --- a/tests/harmony_dsp_test.cpp +++ b/tests/harmony_dsp_test.cpp @@ -137,6 +137,40 @@ static void testVoicePlanner(){ planner.plan(67,0,major); need(planner.target(2)!=high,"reset retained previous phrase voicing"); } +// Octave harmonics can become louder than the true F0 in a busy chorus. +// Two noisy analyses must not reset five active backing PSOLA windows; +// a genuine sustained octave jump must still converge in bounded time. +static void testPeriodContinuityOnLoudOctaveErrors(){ + PeriodContinuity tracker; + need(tracker.current()==0.f,"new tracker not silent"); + need(tracker.observe(std::nanf(""))==0.f, + "invalid octave period acquired"); + const float established=tracker.observe(218.f); + need(established==218.f,"initial F0 period not acquired"); + for(int i=0;i<2;++i){ + need(tracker.observe(109.f)==established, + "one/two spurious octave hops retuned singer"); + } + need(!tracker.transitioning(), + "short octave error entered period-transition state"); + need(std::abs(tracker.observe(218.5f)-established)<1.f, + "return to original vowel failed"); + float previous=tracker.current(); + for(int hop=0;hop<18;++hop){ + const float next=tracker.observe(109.f); + need(std::isfinite(next)&&next>8.f&&next<=previous*1.001f, + "accepted octave caused nonmonotone/unbounded source period"); + need(previous-next<=previous*.111f, + "source-period leap would abruptly retime PSOLA grains"); + previous=next; + } + need(std::abs(tracker.current()-109.f)<7.f, + "real sustained octave did not converge"); + tracker.reset(); + need(tracker.current()==0.f&&!tracker.transitioning(), + "new singer phrase inherited stale source period"); +} + // Stress the USER failure shape: a long 5Hz/70-cent vibrato under // chorus-level gain with short noisy consonants. Dry/Wet are checked using // the actual native Engine; fixture is NOT an ASIO or human-likeness claim. @@ -211,6 +245,7 @@ static void testLoudVibratoContinuity(){ } int main(){ + testPeriodContinuityOnLoudOctaveErrors(); testLoudVibratoContinuity(); testLeadTune(); testVoicePlanner();