Skip to content
Merged
68 changes: 52 additions & 16 deletions engine/builtin_fx/HarmonyDsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,17 @@ class Engine final {
leadRatio_=1.f;leadBlend_=0.f;leadPeriod_=0.f;
leadRendering_=false;voicePlanner_.reset();
pitchSyncBlend_.fill(0.f);pitchSyncFiltered_.fill(0.f);
pitchSyncValid_.fill(0.f);
sourcePeriodSamples_=0.f;sourceConfidence_=0.f;sampleClock_=0;
uncertainPitchHops_=0;weakSyncHops_=0;pitchSyncStable_=false;
pitchSyncReferenceEnabled_=true;
stageDepthReferenceEnabled_=true;
roomFocusReferenceEnabled_=true;
presenceReferenceEnabled_=true;
pitchSyncAlpha_=static_cast<float>(1.-std::exp(-1./(rate*.012)));
// Both renderer choice and missing-epoch recovery are time-domain
// crossfades. Never hard-switch their independent source phases.
pitchSyncAlpha_=static_cast<float>(1.-std::exp(-1./(rate*.030)));
pitchSyncValidAlpha_=static_cast<float>(1.-std::exp(-1./(rate*.005)));
const float cutoff=1800.f;
decimationAlpha_=static_cast<float>(1.-std::exp(-2.*3.141592653589793*cutoff/rate));
write_=0;downWrite_=0;downSeen_=0;downCounter_=0;
Expand All @@ -129,7 +133,9 @@ class Engine final {
smoothingAlpha_=static_cast<float>(1.-std::exp(-1./(rate*0.005))); // 5 ms control ramp
// Preserve the established 48k envelope while keeping durations
// constant at 32-192k. Exponents computed off the callback.
voicedGateAlpha_=static_cast<float>(1.-std::pow(1.-0.002,48000./rate));
// Sustain a noisy, vibrato-heavy sung vowel without 2ms square-like
// envelope edges; real silence is still detected immediately.
voicedGateAlpha_=static_cast<float>(1.-std::exp(-1./(rate*.008)));
ratioAlpha_=static_cast<float>(1.-std::pow(1.-0.00045,48000./rate));
processed_=false;settings_=Settings{};
return setSettings(settings_);
Expand Down Expand Up @@ -167,7 +173,12 @@ class Engine final {
for(std::size_t v=0;v<singers.size();++v){
const auto& singer=singers[v];
if(!singerActive(singer.role,next.voices))continue;
const float angle=(singer.pan*next.space+1.f)*pi*0.25f;
// Space maps to perceptual stereo width: a 60% setting now
// already gives recognizably separated LOW/HIGH singers.
// The sqrt curve and constant-power gains are CONTROL-only;
// Space=0 remains a single mono backing center.
const float stageWidth=std::sqrt(next.space);
const float angle=(singer.pan*stageWidth+1.f)*pi*0.25f;
const float level=amplitude*singer.presence;
prepared.mixL[v]=level*std::cos(angle);
prepared.mixR[v]=level*std::sin(angle);
Expand Down Expand Up @@ -342,8 +353,12 @@ class Engine final {
const auto placed=stage_.place(v,voice,reflection,second,
mixL_[ix],mixR_[ix],space_);
if(stageDepthReferenceEnabled_){
outL+=placed.directL*backingDirect+placed.earlyL*earlyReturn;
outR+=placed.directR*backingDirect+placed.earlyR*earlyReturn;
const float directPresence=BackingStageSpatial::
directionalPresence(v,space_);
outL+=placed.directL*backingDirect*directPresence+
placed.earlyL*earlyReturn;
outR+=placed.directR*backingDirect*directPresence+
placed.earlyR*earlyReturn;
roomSendL+=placed.roomL;roomSendR+=placed.roomR;
}else{
outL+=directL*backingDirect+
Expand Down Expand Up @@ -438,10 +453,15 @@ class Engine final {
// 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);
// A single missing source epoch must not hard-switch an already
// voiced PSOLA singer to a phase-unrelated granular sample. Recover
// gradually over 5ms, even as the overall algorithm blends over 30ms.
float& valid=pitchSyncValid_[ix];
valid+=pitchSyncValidAlpha_*((sync.ready?1.f:0.f)-valid);
float& blend=pitchSyncBlend_[ix];
blend+=pitchSyncAlpha_*((confident&&sync.ready?1.f:0.f)-blend);
const float validSync=sync.ready?colorState:f;
y=f*(1.f-blend)+validSync*blend;
blend+=pitchSyncAlpha_*((confident?1.f:0.f)-blend);
const float windowedSync=colorState*valid+f*(1.f-valid);
y=f*(1.f-blend)+windowedSync*blend;
phase_[ix]+=1.f/static_cast<float>(grain_);
if(phase_[ix]>=1.f)phase_[ix]-=1.f;
return y;
Expand Down Expand Up @@ -512,13 +532,15 @@ class Engine final {
sourceConfidence_=0.f;voiced_=false;noteHold_=0;
candidateNote_=-1;currentNote_=-1;
uncertainPitchHops_=0;weakSyncHops_=0;pitchSyncStable_=false;
sourcePeriodSamples_=0.f;
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.
// Keep the last known voiced note through at most five energetic but
// ambiguous analysis windows (~33ms at 48k); true silence immediately
// releases. Avoid ~150Hz note-gating during loud vowels and long vibrato.
// Six inconclusive windows still release, never an indefinite noise hold.
void uncertainPitchEvidence() noexcept {
if(voiced_&&noteHold_>=2&&uncertainPitchHops_<2){
if(voiced_&&noteHold_>=2&&uncertainPitchHops_<5){
++uncertainPitchHops_;
return;
}
Expand Down Expand Up @@ -592,10 +614,23 @@ class Engine final {
currentNote_=note;voiced_=true;tonalTransitionPending_=false;
if(sourcePeriodSamples_>0.f&&
std::abs(newPeriod-sourcePeriodSamples_)>.25f*sourcePeriodSamples_){
pitchSync_.reset();pitchSyncBlend_.fill(0.f);
pitchSyncFiltered_.fill(0.f);
// 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_);
}
sourcePeriodSamples_=newPeriod;
sourceConfidence_=confidence;
// Hysteresis prevents marginal correlation confidence causing
// rapid PSOLA/granular alternation. Silence disables it immediately.
Expand Down Expand Up @@ -633,13 +668,14 @@ 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;
float pitchSyncValidAlpha_=0.f;
int uncertainPitchHops_=0,weakSyncHops_=0;
bool pitchSyncStable_=false;
std::uint64_t sampleClock_=0;
bool pitchSyncReferenceEnabled_=true,stageDepthReferenceEnabled_=true;
bool roomFocusReferenceEnabled_=true;
bool presenceReferenceEnabled_=true;
std::array<float,maxVoices> pitchSyncBlend_{},pitchSyncFiltered_{};
std::array<float,maxVoices> pitchSyncBlend_{},pitchSyncFiltered_{},pitchSyncValid_{};
float space_=0.f,targetSpace_=0.f;
float gain_=1.f,targetGain_=1.f,targetCorrection_=0.f;
std::array<float,maxVoices> mixL_{},mixR_{},targetMixL_{},targetMixR_{};
Expand Down
20 changes: 10 additions & 10 deletions engine/builtin_fx/HarmonyPitchSynchronous.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ class PitchSynchronousVoice final {
if(!state.initialized) {
state.initialized=true;
state.nextCenter=static_cast<double>(time)+width;
if constexpr(Stereo) {
const auto peak=findStableEpoch(epochs,time,writeIndex,
state.nextCenter-fixedDelay,period,state.epochPolarity);
if(peak>=0)
state.nextCenter=refineEpoch(epochs,time,writeIndex,peak,
state.epochPolarity)+fixedDelay;
}
// Align mono backing *and* stereo lead to fractional epochs.
// Integer-peak rounding previously induced a 1-sample phase
// staircase on high soprano periods, heard as brittle buzz.
const auto peak=findStableEpoch(epochs,time,writeIndex,
state.nextCenter-fixedDelay,period,state.epochPolarity);
if(peak>=0)
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.
Expand All @@ -81,9 +82,8 @@ class PitchSynchronousVoice final {
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,state.epochPolarity);
const double epoch=refineEpoch(
epochs,time,writeIndex,peak,state.epochPolarity);
grain={state.nextCenter,epoch,width,true};
state.nextSlot=(state.nextSlot+1)%state.grains.size();
}
Expand Down
9 changes: 9 additions & 0 deletions engine/builtin_fx/HarmonyStageSpatial.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ class BackingStageSpatial final {
const float s=std::clamp(space,0.f,1.f);
return {1.f-.62f*s,.49f*s,1.08f*s};
}
// Hard-side backing singers lose direct presence against a dense lead
// when the global Space direct/reverb ratio recedes. Restore HIGH
// intelligibility only in its directional direct field; the shared
// diffuse room and lead signal remain unchanged. No extra compressor,
// reverb instance, latency or dynamic loudness pump.
static float directionalPresence(int singer,float space) noexcept {
const float s=std::clamp(space,0.f,1.f);
return 1.f+s*(singer==2?.32f:singer==4?.10f:0.f);
}
bool prepare(double rate) noexcept {
if(!std::isfinite(rate)||rate<32000.||rate>192000.)return false;
const auto delay=static_cast<std::size_t>(std::lround(rate*.00022));
Expand Down
12 changes: 7 additions & 5 deletions engine/builtin_fx/HarmonyVoicePlanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ struct SingerProfile {
float pan,presence;
};
inline constexpr std::array<SingerProfile,5> singers{{
{Singer::low, -5,-3,-4,-.68f,1.16f},
{Singer::mid, 3, 5, 4, .00f,1.00f},
{Singer::high, 6, 9, 7, .68f,1.12f},
{Singer::lowerSupport, -9,-6,-7,-.34f,1.00f},
{Singer::upperSupport, 10,12,12, .34f,1.00f}
// Harder lateral anchors but still center-safe at Space=0. The HIGH
// singer gets more relative presence; normalization remains 1/sqrt(N).
{Singer::low, -5,-3,-4,-.94f,1.20f},
{Singer::mid, 3, 5, 4, .00f,.94f},
{Singer::high, 6, 9, 7, .95f,1.37f},
{Singer::lowerSupport, -9,-6,-7,-.66f,1.03f},
{Singer::upperSupport, 10,12,12, .67f,1.18f}
}};
inline constexpr bool singerActive(Singer role,int voices) noexcept {
return role==Singer::low||role==Singer::high||
Expand Down
89 changes: 89 additions & 0 deletions tests/harmony_dsp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,81 @@ static void testVoicePlanner(){
planner.plan(67,0,major);
need(planner.target(2)!=high,"reset retained previous phrase voicing");
}
// 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.
static void testLoudVibratoContinuity(){
constexpr int rate=48000,frames=rate*2;
std::vector<float> inL(frames),inR(frames);
constexpr double tau=6.283185307179586;
double phase=0.;
for(int i=0;i<frames;++i){
const double t=static_cast<double>(i)/rate;
const double vibrato=70.*std::sin(tau*5.1*t);
phase+=tau*220.*std::exp2(vibrato/1200.)/rate;
const double chorus=std::clamp((t-.4)*15.,0.,1.)*
(1.-std::clamp((t-1.25)*15.,0.,1.));
const double level=.12+.16*chorus;
// Busy phrase: a 14ms noisy consonant every 210ms after warm-up.
const double noise=(i>rate/3 && i%(rate*21/100)<rate*14/1000)?
.07*static_cast<double>((i*977+17)%137-68)/68.:0.;
inL[i]=static_cast<float>(
level*(std::sin(phase)+.26*std::sin(2.*phase+.2)+
.16*std::sin(3.*phase))+noise);
inR[i]=static_cast<float>(
level*(.85*std::sin(phase+.12)+.31*std::sin(3.*phase+.4))+
noise*.85);
}
auto process=[&](int block,int voices,bool wet){
auto dsp=std::make_unique<Engine>();
need(dsp->prepare(rate)&&dsp->latencySamples()==1536,
"vibrato engine PDC changed");
Settings settings;settings.voices=voices;settings.wet=wet?.75f:0.f;
settings.space=.82f;
need(dsp->setSettings(settings),"vibrato DSP settings rejected");
auto l=inL,r=inR;
int audibleTicks=0,checkedTicks=0;
for(int pos=0;pos<frames;pos+=block){
const int count=std::min(block,frames-pos);
dsp->process(l.data()+pos,r.data()+pos,count);
if(pos>rate/3&&pos<rate*6/5){
++checkedTicks;
if(dsp->voiced())++audibleTicks;
}
}
need(audibleTicks>checkedTicks*.96,
"loud-vibrato tracking repeatedly lost voiced continuity");
return std::pair{std::move(l),std::move(r)};
};
const auto five64=process(64,5,true);
const auto five257=process(257,5,true);
need(five64==five257,
"heavy five-voice vibrato output depends on ASIO callback partition");
const auto dry=process(257,5,false);
double harmonyE=0.,outE=0.,monoE=0.;
float largestStep=0.f;
for(int i=rate/2;i<frames;++i){
const float l=five64.first[static_cast<std::size_t>(i)];
const float r=five64.second[static_cast<std::size_t>(i)];
const float dl=dry.first[static_cast<std::size_t>(i)];
const float dr=dry.second[static_cast<std::size_t>(i)];
need(std::isfinite(l)&&std::isfinite(r)&&
std::abs(l)<3.9f&&std::abs(r)<3.9f,
"loud vibrato harmonic mixed PCM nonfinite or clipped");
harmonyE+=(l-dl)*(l-dl)+(r-dr)*(r-dr);
outE+=l*l+r*r;
monoE+=(l+r)*(l+r)*.25;
largestStep=std::max(largestStep,std::abs(l-
five64.first[static_cast<std::size_t>(i-1)]));
}
need(harmonyE>1.&&outE>4.&&monoE>1.,
"high-demand ensemble lost harmony or mono energy");
need(largestStep<.65f,
"long-vibrato ensemble produced an unbounded sample discontinuity");
}

int main(){
testLoudVibratoContinuity();
testLeadTune();
testVoicePlanner();
// Each Engine owns fixed ring buffers. Allocate offline fixtures on heap:
Expand Down Expand Up @@ -738,6 +812,21 @@ int main(){
"ensemble gain changes with voice count");
need(modern.mixL[0]>modern.mixR[0]&&modern.mixR[2]>modern.mixL[2],
"LOW/HIGH changed sides when voice count changed");
// 60% SPACE must now create an audible hard-sided LOW/HIGH pair
// without making the center MID dominate the music mix.
need(modern.mixL[0]>modern.mixR[0]*3.f&&
modern.mixR[2]>modern.mixL[2]*3.f,
"perceptual HARD stereo spread not applied to LOW/HIGH");
const float low=std::hypot(modern.mixL[0],modern.mixR[0]);
const float high=std::hypot(modern.mixL[2],modern.mixR[2]);
need(high>low*1.11f,"HIGH voice still masked behind LOW");
Settings centered=settings;centered.space=0.f;
Engine::PreparedSettings mono{};
need(Engine::prepareSettings(centered,mono),
"Space0 layout invalid");
need(std::abs(mono.mixL[0]-mono.mixR[0])<1.e-6f&&
std::abs(mono.mixL[2]-mono.mixR[2])<1.e-6f,
"hard pan leaks when Space=0");
}
// P0 lead preservation: backing Wet must never change the lead amplitude.
// The *only* intentional lead replacement is Pitch Tune's explicit blend.
Expand Down
7 changes: 7 additions & 0 deletions tests/harmony_stage_spatial_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ static void focusTest(double rate){
f.returnGain(0.f,0.f,1.f)==1.f,"room restart stale sample");
}
int main(){
// Space=0 is unchanged; at wide Space, HIGH retains a small direct
// spectral/level anchor rather than drowning in the shared late room.
need(BackingStageSpatial::directionalPresence(2,0.f)==1.f&&
BackingStageSpatial::directionalPresence(0,1.f)==1.f&&
BackingStageSpatial::directionalPresence(2,1.f)>1.3f&&
BackingStageSpatial::directionalPresence(4,1.f)>1.09f,
"HIGH/support direct focus incorrect or LOW unexpectedly boosted");
for(double rate:{32000.,44100.,48000.,96000.,192000.}){
focusTest(rate);
double previous=-1.;
Expand Down
Loading