From 0643d93d402fbe610ee64ef72ad41bc5ca1d4110 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Wed, 11 Nov 2026 03:38:48 +0700 Subject: [PATCH 1/7] fix(harmony): keep stable signed epochs for quiet/high backing vowels --- engine/builtin_fx/HarmonyPitchSynchronous.h | 51 ++++++++++++++------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/engine/builtin_fx/HarmonyPitchSynchronous.h b/engine/builtin_fx/HarmonyPitchSynchronous.h index 339126876..11b123c8d 100644 --- a/engine/builtin_fx/HarmonyPitchSynchronous.h +++ b/engine/builtin_fx/HarmonyPitchSynchronous.h @@ -65,10 +65,11 @@ class PitchSynchronousVoice final { state.initialized=true; state.nextCenter=static_cast(time)+width; if constexpr(Stereo) { - const auto peak=findPositiveEpoch(epochs,time,writeIndex, - state.nextCenter-fixedDelay,period); + const auto peak=findStableEpoch(epochs,time,writeIndex, + state.nextCenter-fixedDelay,period,state.epochPolarity); if(peak>=0) - state.nextCenter=refineEpoch(epochs,time,writeIndex,peak)+fixedDelay; + state.nextCenter=refineEpoch(epochs,time,writeIndex,peak, + state.epochPolarity)+fixedDelay; } } // Advance at most two grains per sample, even on hostile period jumps. @@ -76,12 +77,13 @@ class PitchSynchronousVoice final { for(int scheduled=0;scheduled<2 && static_cast(time)+.0001>=state.nextCenter-width;++scheduled) { const double desired=state.nextCenter-static_cast(fixedDelay); - const auto peak=findPositiveEpoch(epochs,time,writeIndex,desired,period); + const auto peak=findStableEpoch(epochs,time,writeIndex,desired,period, + state.epochPolarity); if(peak>=0) { auto& grain=state.grains[state.nextSlot]; double epoch=static_cast(peak); if constexpr(Stereo) - epoch=refineEpoch(epochs,time,writeIndex,peak); + epoch=refineEpoch(epochs,time,writeIndex,peak,state.epochPolarity); grain={state.nextCenter,epoch,width,true}; state.nextSlot=(state.nextSlot+1)%state.grains.size(); } @@ -111,13 +113,19 @@ class PitchSynchronousVoice final { std::array grains{}; double nextCenter=0.; std::size_t nextSlot=0; + int epochPolarity=0; // stable sign throughout the voiced resynthesis run bool initialized=false; }; - // Bound the peak search to <1 source cycle, never scanning beyond the - // already received microphone input. Unreliable speech uses fallback. - static std::int64_t findPositiveEpoch(const std::array& input, + // A fixed +3mV positive-peak threshold rejects quiet voiced periods and + // asymmetric vowels, leaving holes in the high backing shifter. Select + // the strongest signed source landmark once per voice and keep its sign + // throughout the phrase, so polarity never flips between adjacent grains. + // The caller's independent high-confidence pitch detector remains the + // voiced/noise gate; this floor only excludes numerical silence. + // The same <1-period scan and history bounds apply at every sample rate. + static std::int64_t findStableEpoch(const std::array& input, std::uint64_t time,std::size_t writeIndex, - double desired,double period) noexcept { + double desired,double period,int& polarity) noexcept { const auto half=static_cast(std::ceil(period*.46)); const auto center=static_cast(std::llround(desired)); const auto oldest=static_cast( @@ -126,23 +134,34 @@ class PitchSynchronousVoice final { const auto start=std::max(oldest,center-half); const auto end=std::min(newest,center+half); if(end( static_cast(time)-t); const float value=input[(writeIndex+Capacity-age)&(Capacity-1)]; - if(value>best){best=value;peak=t;} + if(value>maxPositive){maxPositive=value;positive=t;} + if(-value>maxNegative){maxNegative=-value;negative=t;} } - return peak; + constexpr float silenceFloor=1.e-5f; + if(polarity==0) { + if(std::max(maxPositive,maxNegative)=maxNegative?1:-1; + } + return polarity>0? + (maxPositive>=silenceFloor?positive:-1): + (maxNegative>=silenceFloor?negative:-1); } static double refineEpoch(const std::array& input, - std::uint64_t time,std::size_t writeIndex,std::int64_t peak) noexcept { + std::uint64_t time,std::size_t writeIndex,std::int64_t peak, + int polarity) noexcept { const auto age=time-static_cast(peak); if(age<2||age>=Capacity-3||peak==0)return static_cast(peak); const auto i=(writeIndex+Capacity-age)&(Capacity-1); - const float a=input[(i+Capacity-1)&(Capacity-1)],b=input[i]; - const float c=input[(i+1)&(Capacity-1)]; + const float sign=polarity<0?-1.f:1.f; + const float a=sign*input[(i+Capacity-1)&(Capacity-1)]; + const float b=sign*input[i]; + const float c=sign*input[(i+1)&(Capacity-1)]; const float curvature=a-2.f*b+c; if(b=-1.e-6f)return static_cast(peak); return static_cast(peak)+ From 939cf6fc029c9a5ec44c0a0efe3d89ce7c8de044 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Wed, 11 Nov 2026 03:40:08 +0700 Subject: [PATCH 2/7] fix(harmony): bridge brief pitch-confidence gaps and prevent PSOLA dropout noise --- engine/builtin_fx/HarmonyDsp.h | 38 +++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/engine/builtin_fx/HarmonyDsp.h b/engine/builtin_fx/HarmonyDsp.h index 41db290c3..648b7a298 100644 --- a/engine/builtin_fx/HarmonyDsp.h +++ b/engine/builtin_fx/HarmonyDsp.h @@ -111,6 +111,7 @@ class Engine final { leadRendering_=false;voicePlanner_.reset(); pitchSyncBlend_.fill(0.f);pitchSyncFiltered_.fill(0.f); sourcePeriodSamples_=0.f;sourceConfidence_=0.f;sampleClock_=0; + uncertainPitchHops_=0;weakSyncHops_=0;pitchSyncStable_=false; pitchSyncReferenceEnabled_=true; stageDepthReferenceEnabled_=true; roomFocusReferenceEnabled_=true; @@ -369,7 +370,9 @@ class Engine final { outL+=room.left*roomReturn;outR+=room.right*roomReturn; l[i]=std::clamp(outL*gain_,-4.f,4.f); r[i]=std::clamp(outR*gain_,-4.f,4.f); - ensemble_.advance(voiced_&&sourceConfidence_>.87f&¬eHold_>=2); + // Expression follows stable notes; confidence hysteresis is + // handled by the tracker, not by vibrato resetting on one hop. + ensemble_.advance(voiced_&¬eHold_>=2); stage_.advance(); write_=(write_+1)%capacity; ++sampleClock_; @@ -423,7 +426,8 @@ class Engine final { // overlap-add. The exact source period is measured, not inferred // from the harmony target. Uncertain/nonperiodic sounds retain the // previous validated granular shifter with a bounded 12ms crossfade. - const bool confident=pitchSyncReferenceEnabled_&&voiced_&&sourceConfidence_>.82f&& + // Avoid 150Hz switching between two synthesis algorithms. + const bool confident=pitchSyncReferenceEnabled_&&voiced_&&pitchSyncStable_&& sourcePeriodSamples_>=8.f; PitchSynchronousVoice::Output sync{}; if(confident) @@ -431,10 +435,13 @@ class Engine final { sourcePeriodSamples_,effectiveRatio, static_cast(latency_)+arrival); float& colorState=pitchSyncFiltered_[ix]; - colorState+=filterCoef*(sync.sample-colorState); + // Invalid PSOLA epochs must never inject zero into the high-voice + // output while a crossfade is still active. + if(sync.ready)colorState+=filterCoef*(sync.sample-colorState); float& blend=pitchSyncBlend_[ix]; blend+=pitchSyncAlpha_*((confident&&sync.ready?1.f:0.f)-blend); - y=f*(1.f-blend)+colorState*blend; + const float validSync=sync.ready?colorState:f; + y=f*(1.f-blend)+validSync*blend; phase_[ix]+=1.f/static_cast(grain_); if(phase_[ix]>=1.f)phase_[ix]-=1.f; return y; @@ -504,8 +511,19 @@ class Engine final { void releasePitch() noexcept { sourceConfidence_=0.f;voiced_=false;noteHold_=0; candidateNote_=-1;currentNote_=-1; + uncertainPitchHops_=0;weakSyncHops_=0;pitchSyncStable_=false; leadTune_.release();voicePlanner_.reset(); } + // Keep the last known voiced note through at most two energetic but + // ambiguous analysis windows (~13ms at 48k); actual silence immediately + // releases. This prevents periodic gating on high vowels/consonants. + void uncertainPitchEvidence() noexcept { + if(voiced_&¬eHold_>=2&&uncertainPitchHops_<2){ + ++uncertainPitchHops_; + return; + } + releasePitch(); + } void finishPitchAnalysis() noexcept { const auto& correlations=pitchCorrelations_; float best=pitchBest_; @@ -522,9 +540,10 @@ class Engine final { } } if(bestLag==0||best<=0.77f){ - releasePitch(); + uncertainPitchEvidence(); return; } + uncertainPitchHops_=0; // Three-point peak interpolation reduces integer-lag tuning steps, // especially at the high end of the supported singing range. float lag=static_cast(bestLag); @@ -578,6 +597,13 @@ class Engine final { } sourcePeriodSamples_=newPeriod; sourceConfidence_=confidence; + // Hysteresis prevents marginal correlation confidence causing + // rapid PSOLA/granular alternation. Silence disables it immediately. + if(confidence>=.86f){ + pitchSyncStable_=true;weakSyncHops_=0; + }else if(confidence<.79f){ + if(++weakSyncHops_>=3)pitchSyncStable_=false; + }else weakSyncHops_=0; // Integer intervals come from the prepared LUT. The single // fractional ratio tracks the current microphone pitch. const float baseRatio=std::exp2((note-midi)/12.f); @@ -607,6 +633,8 @@ class Engine final { float smoothingAlpha_=1.f,voicedGateAlpha_=0.002f,ratioAlpha_=0.00045f; float wet_=0.f,targetWet_=0.f,color_=0.f,targetColor_=0.f; float sourcePeriodSamples_=0.f,sourceConfidence_=0.f,pitchSyncAlpha_=0.f; + int uncertainPitchHops_=0,weakSyncHops_=0; + bool pitchSyncStable_=false; std::uint64_t sampleClock_=0; bool pitchSyncReferenceEnabled_=true,stageDepthReferenceEnabled_=true; bool roomFocusReferenceEnabled_=true; From 6542faf9b7a54cb4c614ff4655b78aac43479c3b Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Wed, 11 Nov 2026 03:40:52 +0700 Subject: [PATCH 3/7] test(harmony): cover soft soprano signed epochs and 64/257 block parity --- tests/harmony_pitch_synchronous_test.cpp | 25 +++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/tests/harmony_pitch_synchronous_test.cpp b/tests/harmony_pitch_synchronous_test.cpp index 97477cda2..e9d07403d 100644 --- a/tests/harmony_pitch_synchronous_test.cpp +++ b/tests/harmony_pitch_synchronous_test.cpp @@ -10,7 +10,8 @@ using Shifter=PitchSynchronousVoice<16384,6>; static void need(bool yes,const char* reason){ if(!yes){std::cerr< render(double rate,double f0,double ratio,int block){ +static std::vector render(double rate,double f0,double ratio,int block, + double amplitude=1.,double polarity=1.){ auto p=Shifter{}; std::array ring{}; const int n=static_cast(rate*.65); @@ -19,8 +20,8 @@ static std::vector render(double rate,double f0,double ratio,int block){ const int t=pos+j; const double phase=6.283185307179586*f0*t/rate; ring[static_cast(t)&16383]=static_cast( - .19*std::sin(phase)+.052*std::sin(2*phase+.31)+ - .029*std::sin(3*phase+.17)); + amplitude*polarity*(.19*std::sin(phase)+ + .052*std::sin(2*phase+.31)+.029*std::sin(3*phase+.17))); const auto sample=p.process(0,t,static_cast(t)&16383,ring, static_cast(rate/f0),static_cast(ratio), static_cast(rate*.032)); @@ -69,6 +70,24 @@ int main(){ need(std::abs(result-v[1]*v[2])/(v[1]*v[2])<.024, "synchronous target pitch mismatch"); } + // Previously a hard-coded +0.003V positive epoch threshold made + // genuine soft high-register vowels leave the PSOLA output at zero. + // Explicitly exercise low-amplitude *negative*-polarity waveforms at + // ASIO-shaped block boundaries. These tests measure basic continuity, + // not perceived naturalness in an actual sung phrase. + for(const double polarity:{-1.,1.}){ + const auto soft64=render(48000.,1046.502261,1.4,64,.004,polarity); + const auto soft257=render(48000.,1046.502261,1.4,257,.004,polarity); + need(soft64==soft257,"soft soprano changed with callback partitioning"); + double energy=0.;int audible=0; + for(int i=14400;i<26400;++i){ + const double x=soft64[static_cast(i)]; + energy+=x*x; + if(std::abs(x)>1.e-5)++audible; + } + need(energy>1.e-5&&audible>6000, + "soft high vowel lost epochs or became a rapid gated voice"); + } // Invalid or truncated source must return cleanly without allocations or // producing undefined samples, even after a partially scheduled phrase. Shifter bad; From a64e0f3f3b5500a4f80aaa187badeb159a0e6d00 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Wed, 11 Nov 2026 03:41:20 +0700 Subject: [PATCH 4/7] fix(harmonice-ui): expose supported two-harmony-voice Duo selector --- site/preview-v2/assets/mixer-native-harmony.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/preview-v2/assets/mixer-native-harmony.js b/site/preview-v2/assets/mixer-native-harmony.js index c603c6cbe..a985603d6 100644 --- a/site/preview-v2/assets/mixer-native-harmony.js +++ b/site/preview-v2/assets/mixer-native-harmony.js @@ -31,7 +31,7 @@ root.innerHTML=` HARMONICEVOCAL ENGINE VOICES