Skip to content
38 changes: 33 additions & 5 deletions engine/builtin_fx/HarmonyDsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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&&noteHold_>=2);
// Expression follows stable notes; confidence hysteresis is
// handled by the tracker, not by vibrato resetting on one hop.
ensemble_.advance(voiced_&&noteHold_>=2);
stage_.advance();
write_=(write_+1)%capacity;
++sampleClock_;
Expand Down Expand Up @@ -423,18 +426,22 @@ 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<capacity,maxVoices>::Output sync{};
if(confident)
sync=pitchSync_.process(voice,sampleClock_,write_,mono_,
sourcePeriodSamples_,effectiveRatio,
static_cast<float>(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<float>(grain_);
if(phase_[ix]>=1.f)phase_[ix]-=1.f;
return y;
Expand Down Expand Up @@ -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_&&noteHold_>=2&&uncertainPitchHops_<2){
++uncertainPitchHops_;
return;
}
releasePitch();
}
void finishPitchAnalysis() noexcept {
const auto& correlations=pitchCorrelations_;
float best=pitchBest_;
Expand All @@ -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<float>(bestLag);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
51 changes: 35 additions & 16 deletions engine/builtin_fx/HarmonyPitchSynchronous.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,25 @@ class PitchSynchronousVoice final {
state.initialized=true;
state.nextCenter=static_cast<double>(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.
// Normal supported periods place >=4 samples between target marks.
for(int scheduled=0;scheduled<2 &&
static_cast<double>(time)+.0001>=state.nextCenter-width;++scheduled) {
const double desired=state.nextCenter-static_cast<double>(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<double>(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();
}
Expand Down Expand Up @@ -111,13 +113,19 @@ class PitchSynchronousVoice final {
std::array<Grain,8> 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<float,Capacity>& 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<float,Capacity>& 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::int64_t>(std::ceil(period*.46));
const auto center=static_cast<std::int64_t>(std::llround(desired));
const auto oldest=static_cast<std::int64_t>(
Expand All @@ -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<start)return -1;
std::int64_t peak=-1;
float best=.003f;
std::int64_t positive=-1,negative=-1;
float maxPositive=0.f,maxNegative=0.f;
for(auto t=start;t<=end;++t) {
const auto age=static_cast<std::size_t>(
static_cast<std::int64_t>(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)<silenceFloor)return -1;
polarity=maxPositive>=maxNegative?1:-1;
}
return polarity>0?
(maxPositive>=silenceFloor?positive:-1):
(maxNegative>=silenceFloor?negative:-1);
}
static double refineEpoch(const std::array<float,Capacity>& 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<std::uint64_t>(peak);
if(age<2||age>=Capacity-3||peak==0)return static_cast<double>(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<a||b<c||curvature>=-1.e-6f)return static_cast<double>(peak);
return static_cast<double>(peak)+
Expand Down
9 changes: 5 additions & 4 deletions site/preview-v2/assets/mixer-fx-rack.css
Original file line number Diff line number Diff line change
Expand Up @@ -484,18 +484,18 @@
.mx-harm-signature strong{font-size:17px;font-weight:500;letter-spacing:-.35px;color:#d1ebec}
.mx-harm-signature small{font-size:8px;letter-spacing:1.1px;color:#86a8b2}
.mx-harm-label{font-size:9px;letter-spacing:.9px;color:#a3c0c8;font-weight:500}
.mx-harm-voices{display:flex;gap:4px;width:100%}
.mx-harm-voices{display:flex;gap:3px;width:100%}
.mx-harm-voices button{
appearance:none;display:flex;align-items:center;justify-content:center;
gap:3px;flex:1;min-width:0;height:30px;padding:0 3px;border-radius:5px;
gap:2px;flex:1;min-width:0;height:30px;padding:0 2px;border-radius:5px;
border:1px solid #557784;line-height:1;
background:linear-gradient(180deg,#365b67,#294953);
color:#cfe5e7;font-size:11px;font-weight:500;
box-shadow:inset 0 2px 3px #0a263647,inset 0 -1px 0 #a5cad01c,
0 1px 0 #132d38,0 2px 4px #0618223f;
transition:background .13s ease,box-shadow .13s ease,color .13s ease
}
.mx-harm-voice-glyph{display:block;flex:0 0 13px;width:13px;height:13px}
.mx-harm-voice-glyph{display:block;flex:0 0 11px;width:11px;height:11px}
.mx-harm-voices button span{display:block;font-variant-numeric:tabular-nums}
.mx-harm-voices button.active{
background:linear-gradient(180deg,#438d7d,#326c63);
Expand Down Expand Up @@ -767,7 +767,8 @@
.mx-harm-signature{min-height:71px}
.mx-harm-voices{gap:2px}
.mx-harm-voices button{height:28px;padding:0 2px;gap:2px}
.mx-harm-voice-glyph{width:11px;height:11px;flex-basis:11px}
/* Four counts remain legible in the narrow 112px left rail. */
.mx-harm-voice-glyph{display:none}
.mx-harm-source{flex-wrap:wrap;gap:5px;padding:5px 8px}
.mx-harm-source-picker{flex:1}
.mx-harm-guide-status{font-size:8px}
Expand Down
2 changes: 1 addition & 1 deletion site/preview-v2/assets/mixer-native-harmony.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ root.innerHTML=`
<strong>HARMONICE</strong><small>VOCAL ENGINE</small></div>
<span class="mx-harm-label">VOICES</span>
<nav class="mx-harm-voices" id="mxHVoices" aria-label="Harmonice voice count">
<button type="button" data-voices="3" aria-label="3 harmony voices" aria-pressed="true">${voiceGlyph}<span>3</span></button><button type="button" data-voices="4" aria-label="4 harmony voices" aria-pressed="false">${voiceGlyph}<span>4</span></button><button type="button" data-voices="5" aria-label="5 harmony voices" aria-pressed="false">${voiceGlyph}<span>5</span></button>
<button type="button" data-voices="2" aria-label="2 harmony voices plus lead — Natural Duo" aria-pressed="false">${voiceGlyph}<span>2</span></button><button type="button" data-voices="3" aria-label="3 harmony voices" aria-pressed="true">${voiceGlyph}<span>3</span></button><button type="button" data-voices="4" aria-label="4 harmony voices" aria-pressed="false">${voiceGlyph}<span>4</span></button><button type="button" data-voices="5" aria-label="5 harmony voices" aria-pressed="false">${voiceGlyph}<span>5</span></button>
</nav>
<button type="button" id="mxHCorrection" class="mx-harm-correction" role="switch"
aria-checked="false" aria-busy="true" disabled>
Expand Down
12 changes: 10 additions & 2 deletions tests/approved_native_harmony_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ def main():
assert switch.get_attribute("aria-checked")=="true"
apply_edit(page,state,{**preset_settings,"correction":False},0,0)
assert switch.get_attribute("aria-checked")=="false"
# Duo is not only a factory name: it selects the actual
# two-voice DSP state, and preserves the owner's Tune OFF.
page.locator("#mxHPreset").select_option("duo")
duo=last_action(page,"nativeHarmonySetSettings")["settings"]
assert duo["voices"]==2 and duo["correction"] is False,duo
apply_edit(page,state,duo,0,0)
assert page.locator('.mx-harm-voices [data-voices="2"]').get_attribute(
"aria-pressed")=="true"
page.locator("#mxHClose").click()
row.locator('[data-action="openPlugin"]').click()
assert switch.is_disabled()
Expand Down Expand Up @@ -307,8 +315,8 @@ def face_metrics(locator):
apply_edit(page,state,changed,7,0)
assert face_metrics(major)==face_metrics(minor)
# Singer icons are authored once as a tiny shared vector.
# All three UI choices are REAL audible native voice counts.
for count in (3,4,5):
# All four supported counts publish real native voice edits.
for count in (2,3,4,5):
button=page.locator(f'.mx-harm-voices [data-voices="{count}"]')
assert button.locator('svg.mx-harm-voice-glyph').count()==1
button.click()
Expand Down
25 changes: 22 additions & 3 deletions tests/harmony_pitch_synchronous_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ using Shifter=PitchSynchronousVoice<16384,6>;
static void need(bool yes,const char* reason){
if(!yes){std::cerr<<reason<<"\n";std::exit(2);}
}
static std::vector<float> render(double rate,double f0,double ratio,int block){
static std::vector<float> render(double rate,double f0,double ratio,int block,
double amplitude=1.,double polarity=1.){
auto p=Shifter{};
std::array<float,16384> ring{};
const int n=static_cast<int>(rate*.65);
Expand All @@ -19,8 +20,8 @@ static std::vector<float> 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<std::size_t>(t)&16383]=static_cast<float>(
.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<std::size_t>(t)&16383,ring,
static_cast<float>(rate/f0),static_cast<float>(ratio),
static_cast<float>(rate*.032));
Expand Down Expand Up @@ -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<std::size_t>(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;
Expand Down
3 changes: 3 additions & 0 deletions tests/native_harmony_assembly_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ def test_reference_inspired_wide_harmony_and_exact_shared_fx_chrome(self):
self.assertIn(".mx-harm-dial-face::before,.mx-harm-dial-face::after",self.css)
self.assertIn("conic-gradient(from 225deg",self.css)
self.assertIn("x=50+36.5*Math.cos(a)",self.ui)
self.assertIn('data-voices="2"',self.ui)
self.assertIn('2 harmony voices plus lead',self.ui)
self.assertIn('duo:{voices:2',self.ui)
self.assertIn('data-voices="5"',self.ui)
self.assertIn("const voiceGlyph=",self.ui)
self.assertIn("Number.isInteger(s.voices)&&s.voices>=2&&s.voices<=5",self.ui)
Expand Down
Loading