Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions engine/builtin_fx/ChannelHarmonyProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,27 @@ 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);
guideAge_=0;guideObservedKey_.store(-1,std::memory_order_relaxed);
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_) {
// Native built-ins are prepared directly (not by a JUCE plugin
// wrapper). Without this CONTROL-side metadata, AudioProcessor's
// getSampleRate() may remain zero. FxInstance captures that rate
// for its fixed-duration bypass envelope when the lease is built.
// A zero rate previously clamped to 8 kHz: an intended 8ms fade
// became only 64 frames, i.e. 1.33ms at the real 48 kHz device.
// No realtime mutation, graph rebuild, latency change or UI step.
setRateAndBufferSizeDetails(rate,
std::max(1,maximumExpectedSamplesPerBlock));
sampleRate_=rate;
setLatencySamples(dsp_.latencySamples());
}
}
void releaseResources() override {ready_=false;}
void processBlock(juce::AudioBuffer<float>& samples,juce::MidiBuffer&) override {
Expand Down
95 changes: 72 additions & 23 deletions engine/builtin_fx/HarmonyMusicKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
// Conservative confidence/hold; uncertain music falls back to Manual Key.
// No FFT allocation, locks, async worker, trig or heap in process().
namespace armix::harmony {
// Do not indefinitely pin an obsolete key when a new candidate is stable but
// moderately confident. Strong evidence can retarget after 0.8s; weaker yet
// valid evidence needs 2.0s of consecutive agreement. A candidate that
// oscillates resets its hold in evaluate(), so this never forces ambiguity.
constexpr bool acceptMusicKeyCandidate(bool currentValid,bool sameKey,
float confidence,int stableWindows) noexcept {
if(stableWindows<8)return false;
return !currentValid||sameKey||confidence>0.10f||stableWindows>=20;
}
struct MusicKeyEstimate final {
int key=-1,scale=-1; // -1 means no reliable tonal evidence
float confidence=0.f;
Expand All @@ -19,6 +28,12 @@ struct MusicKeyEstimate final {
class MusicKeyDetector final {
public:
static constexpr int bins=36; // C3..B5, 3 octaves of music harmonics
// A 100ms chroma boundary must never score 24 tonal candidates in ONE
// ASIO sample. Spread two scores per decimated sample (<2.1ms total).
static constexpr int maxKeyScoresPerDecimatedSample=2;
static constexpr int maxRotorNormalizationsPerDecimatedSample=3;
int peakKeyScoresPerSample() const noexcept {return peakKeyScores_;}
int peakRotorNormalizationsPerSample() const noexcept {return peakNormalizations_;}
bool prepare(double rate) noexcept {
if(!std::isfinite(rate)||rate<32000.||rate>192000.)return false;
decimation_=std::max(1,static_cast<int>(std::round(rate/6000.)));
Expand All @@ -40,7 +55,10 @@ class MusicKeyDetector final {
stereoFold_.reset();
inC_.fill(0.f);inS_.fill(0.f);
oscC_.fill(1.f);oscS_.fill(0.f);hist_.fill(0.f);
downCount_=0;periodCount_=0;normalizeCount_=0;
downCount_=0;periodCount_=0;normalizeCount_=0;normalizeIndex_=bins;
scoringActive_=false;scoringIndex_=0;scoringBest_=-2.f;
scoringSecond_=-2.f;scoringRoot_=-1;scoringScale_=-1;
scoringHist_.fill(0.f);peakKeyScores_=0;peakNormalizations_=0;
framesSeen_=0;candidate_=-1;candidateScale_=-1;candidateHold_=0;
silentFrames_=0;uncertainFrames_=0;windowEnergy_=0.;filtered_=0.f;
current_={};
Expand All @@ -64,20 +82,31 @@ class MusicKeyDetector final {
oscC_[k]=c*rotorC_[k]-s*rotorS_[k];
oscS_[k]=s*rotorC_[k]+c*rotorS_[k];
}
// Rotor housekeeping is also partitioned. Normalizing all 36
// oscillators in the same ASIO sample caused an avoidable spike.
if(++normalizeCount_>=4096){
normalizeCount_=0;
for(int k=0;k<bins;++k){
const float size=std::sqrt(oscC_[k]*oscC_[k]+oscS_[k]*oscS_[k]);
if(size>0.5f&&std::isfinite(size)){
oscC_[k]/=size;oscS_[k]/=size;
}else {oscC_[k]=1.f;oscS_[k]=0.f;}
}
normalizeCount_=0;normalizeIndex_=0;
}
int normalized=0;
while(normalizeIndex_<bins &&
normalized<maxRotorNormalizationsPerDecimatedSample){
const int k=normalizeIndex_++;
const float size=std::sqrt(oscC_[k]*oscC_[k]+oscS_[k]*oscS_[k]);
if(size>0.5f&&std::isfinite(size)){
oscC_[k]/=size;oscS_[k]/=size;
}else {oscC_[k]=1.f;oscS_[k]=0.f;}
++normalized;
}
peakNormalizations_=std::max(peakNormalizations_,normalized);
if(++periodCount_>=period_){
periodCount_=0;
evaluate(static_cast<float>(std::sqrt(windowEnergy_/period_)));
windowEnergy_=0.;
}
// Score order matches the historical major-then-minor, root 0..11
// loop. The scoring input is a frozen chroma snapshot, so the
// answer is independent of host callback partition boundaries.
continueScoring();
}
}
MusicKeyEstimate estimate() const noexcept{return current_;}
Expand All @@ -101,6 +130,7 @@ class MusicKeyDetector final {
return dot/std::sqrt(cc*pp+1.e-12f);
}
void evaluate(float rms) noexcept {
scoringActive_=false;
if(!std::isfinite(rms)||rms<0.002f) {
if(++silentFrames_>=10) {
current_={};hist_.fill(0.f);
Expand All @@ -123,18 +153,31 @@ class MusicKeyDetector final {
hist_[k]=hist_[k]*0.91f+0.09f*(chroma[k]/sum);
if(framesSeen_<1000)++framesSeen_;
if(framesSeen_<12)return; // at least ~1.2 seconds of music
float best=-2.f,second=-2.f;
int bestRoot=-1,bestScale=-1;
for(int scale=0;scale<2;++scale)
for(int root=0;root<12;++root) {
const float score=correlation(hist_,scale?minorProfile:majorProfile,root);
if(score>best){
second=best;best=score;bestRoot=root;bestScale=scale;
} else second=std::max(second,score);
}
// This is a KEY estimate, not chord detection. Relative minors and
// sparse/sustained notes often remain ambiguous: keep manual fallback.
const float confidence=std::clamp(best-second,0.f,1.f);
scoringHist_=hist_; // fixed-size snapshot; zero allocation
scoringIndex_=0;scoringBest_=-2.f;scoringSecond_=-2.f;
scoringRoot_=-1;scoringScale_=-1;scoringActive_=true;
}
void continueScoring() noexcept {
if(!scoringActive_)return;
int scored=0;
while(scoringIndex_<24 && scored<maxKeyScoresPerDecimatedSample){
const int scale=scoringIndex_/12,root=scoringIndex_%12;
const float score=correlation(scoringHist_,
scale?minorProfile:majorProfile,root);
if(score>scoringBest_){
scoringSecond_=scoringBest_;scoringBest_=score;
scoringRoot_=root;scoringScale_=scale;
} else scoringSecond_=std::max(scoringSecond_,score);
++scoringIndex_;++scored;
}
peakKeyScores_=std::max(peakKeyScores_,scored);
if(scoringIndex_<24)return;
scoringActive_=false;
const float best=scoringBest_;
const int bestRoot=scoringRoot_,bestScale=scoringScale_;
// This is KEY estimation, not chord detection. A complete, frozen
// evaluation commits atomically only after all 24 candidate scores.
const float confidence=std::clamp(best-scoringSecond_,0.f,1.f);
if(best<0.48f||confidence<0.045f) {
candidate_=-1;candidateScale_=-1;candidateHold_=0;
// A new ambiguous musical section cannot retain the previous
Expand All @@ -145,12 +188,18 @@ class MusicKeyDetector final {
uncertainFrames_=0;
if(bestRoot!=candidate_||bestScale!=candidateScale_){
candidate_=bestRoot;candidateScale_=bestScale;candidateHold_=1;
}else if(candidateHold_<12)++candidateHold_;
if(candidateHold_>=8 && (!current_.valid() ||
(current_.key==bestRoot&&current_.scale==bestScale) || confidence>0.10f))
}else if(candidateHold_<20)++candidateHold_;
if(acceptMusicKeyCandidate(current_.valid(),
current_.key==bestRoot&&current_.scale==bestScale,
confidence,candidateHold_))
current_={bestRoot,bestScale,confidence};
}
int decimation_=0,downCount_=0,period_=0,periodCount_=0,normalizeCount_=0;
int normalizeIndex_=bins,scoringIndex_=0;
int scoringRoot_=-1,scoringScale_=-1,peakKeyScores_=0,peakNormalizations_=0;
float scoringBest_=-2.f,scoringSecond_=-2.f;
bool scoringActive_=false;
std::array<float,12> scoringHist_{};
int framesSeen_=0,silentFrames_=0,uncertainFrames_=0;
int candidate_=-1,candidateScale_=-1,candidateHold_=0;
float sampleAlpha_=0.f,lowpassAlpha_=0.f,filtered_=0.f;
Expand Down
137 changes: 132 additions & 5 deletions tests/harmony_bypass_reliability_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ static float singer(int n) noexcept {
return static_cast<float>(.16*std::sin(6.283185307179586*220.*t)+
.045*std::sin(6.283185307179586*440.*t));
}
static LiveChannelFxGraph::InstanceLease makeHarmony() {
static LiveChannelFxGraph::InstanceLease makeHarmony(
double rate=48000.,int quantum=64,float wet=1.f,float space=1.f) {
auto p=std::make_unique<ChannelHarmonyProcessor>();
p->prepareToPlay(48000.,64);
assert(p->isReady() && p->getLatencySamples()==1536);
p->prepareToPlay(rate,quantum);
const int declaredLatency=2*static_cast<int>(std::round(rate*.016));
assert(p->isReady() && p->getLatencySamples()==declaredLatency);
// Native host reads this JUCE metadata when constructing FxInstance.
// Old direct preparation left getSampleRate() unknown/zero, reducing
// the 8ms envelope to 64 samples and producing too-fast bypass ramps.
assert(std::abs(p->getSampleRate()-rate)<1.e-6);
armix::harmony::Settings rich;
rich.voices=5;rich.wet=1.f;rich.space=1.f;
rich.voices=5;rich.wet=wet;rich.space=space;
assert(p->queueAccepted(rich,9001));
return std::make_shared<LiveChannelFxGraph::FxInstance>(
std::move(p),ChannelHarmonyProcessor::catalogIdentity,
"ArSon Harmony",64);
"ArSon Harmony",quantum);
}
int main() {
constexpr ChannelId vocal{7301};
Expand Down Expand Up @@ -145,6 +151,127 @@ int main() {
assert(samplesCheckedWet>10000&&samplesCheckedDry>10000);
assert(maxOldBypassDifference>.003f); // old skipped DSP history visibly diverges
assert(!switched.rackBypassed(vocal));

// Cross-product regression across WET 0/50/100 and Space 0/60/100.
// Native FxInstance::render() is used at 48/96k and three ASIO periods.
// Includes a singing section, a silence/room-tail section, rack OFF/ON,
// slot OFF/ON, reentry after the singer returns, and exact dry PDC.
// Keep the original always-on/old-behavior A/B probe above, too.
struct Scenario { double rate;int quantum;float wet,space; };
constexpr std::array<Scenario,9> cases{{
{48000.,64,0.f,0.f},{48000.,128,0.f,.6f},
{48000.,256,0.f,1.f},{48000.,64,.5f,0.f},
{96000.,128,.5f,.6f},{96000.,256,.5f,1.f},
{96000.,64,1.f,0.f},{96000.,128,1.f,.6f},
{96000.,256,1.f,1.f}
}};
double worstExcess=0.;
int verifiedWet=0,verifiedDry=0;
for(const auto cfg:cases) {
auto a=makeHarmony(cfg.rate,cfg.quantum,cfg.wet,cfg.space);
auto b=makeHarmony(cfg.rate,cfg.quantum,cfg.wet,cfg.space);
assert(a->latencySamples==b->latencySamples);
DynamicChannelFxLeaseBank::Assignment ref,probe;
ref.id=vocal;ref.format=ProcessingFormat::stereo;
ref.inserts.push_back(a);
probe.id=vocal;probe.format=ProcessingFormat::stereo;
probe.inserts.push_back(b);
DynamicChannelFxLeaseBank on,cycle;
const std::array<DynamicChannelFxLeaseBank::Assignment,1> ar{ref},br{probe};
assert(DynamicChannelFxLeaseBank::prepare(3,1,ar,on)==
DynamicChannelFxLeaseBank::Fault::none);
assert(DynamicChannelFxLeaseBank::prepare(3,1,br,cycle)==
DynamicChannelFxLeaseBank::Fault::none);
const int total=static_cast<int>(cfg.rate*.85);
const int latency=b->latencySamples;
const auto input=[&](int n) noexcept -> float {
if(n<0)return 0.f;
const double time=static_cast<double>(n)/cfg.rate;
double amplitude=1.;
if(time>=.5&&time<.505)amplitude=1.-(time-.5)/.005;
else if(time>=.505&&time<.73)amplitude=0.;
else if(time>=.73&&time<.735)amplitude=(time-.73)/.005;
const double phase=6.283185307179586*220.*time;
return static_cast<float>(amplitude*(.16*std::sin(phase)+
.045*std::sin(phase*2.)));
};
bool wasSlotOff=false,wasRackOff=false;
float lastWetL=0.f,lastWetR=0.f,lastOutL=0.f,lastOutR=0.f,lastDry=0.f;
int wetCount=0,dryCount=0;
for(int offset=0;offset<total;offset+=cfg.quantum) {
const int count=std::min(cfg.quantum,total-offset);
const double at=static_cast<double>(offset)/cfg.rate;
const bool slotOff=at>=.20&&at<.36;
const bool rackOff=at>=.61&&at<.73;
if(slotOff!=wasSlotOff){
assert(cycle.setInsertBypass(3,1,vocal,0,b->instanceId,slotOff));
wasSlotOff=slotOff;
}
if(rackOff!=wasRackOff){
assert(cycle.setRackBypass(3,1,vocal,rackOff));
wasRackOff=rackOff;
}
std::array<float,256> rl{},rr{},pl{},pr{};
for(int i=0;i<count;++i)
rl[i]=rr[i]=pl[i]=pr[i]=input(offset+i);
on(vocal,rl.data(),rr.data(),count);
cycle(vocal,pl.data(),pr.data(),count);
for(int i=0;i<count;++i) {
const int n=offset+i;
const float delayed=input(n-latency);
const double time=static_cast<double>(n)/cfg.rate;
const bool stableDry=(time>=.212&&time<.36)||
(time>=.622&&time<.73);
const bool stableWet=(time>=.38&&time<.61)||
(time>=.75);
const float wetSlope=std::max(std::abs(rl[i]-lastWetL),
std::abs(rr[i]-lastWetR));
const float drySlope=std::abs(delayed-lastDry);
const float actualSlope=std::max(std::abs(pl[i]-lastOutL),
std::abs(pr[i]-lastOutR));
if(n>latency){
worstExcess=std::max(worstExcess,static_cast<double>(
actualSlope-std::max(wetSlope,drySlope)));
assert(actualSlope-std::max(wetSlope,drySlope)<.045f);
}
lastWetL=rl[i];lastWetR=rr[i];
lastOutL=pl[i];lastOutR=pr[i];lastDry=delayed;
assert(std::isfinite(pl[i])&&std::isfinite(pr[i]));
assert(std::abs(pl[i])<=4.f&&std::abs(pr[i])<=4.f);
if(stableDry){
assert(std::abs(pl[i]-delayed)<.00003f&&
std::abs(pr[i]-delayed)<.00003f);
++dryCount;
}
if(stableWet){
// The exact live DSP state must match a never-bypassed
// lease, not merely have a visually smooth envelope.
assert(std::abs(pl[i]-rl[i])<.00003f&&
std::abs(pr[i]-rr[i])<.00003f);
++wetCount;
}
}
}
assert(wetCount>0&&dryCount>0);
verifiedWet+=wetCount;verifiedDry+=dryCount;
assert(!cycle.rackBypassed(vocal));
assert(!b->bypassRequested.load(std::memory_order_acquire));
// Validate the envelope actually captured by FxInstance, not a
// detached envelope-only fixture with a hard-coded sample rate.
// This test fails if the JUCE AudioProcessor metadata is zero/stale.
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<int>(std::round(cfg.rate*.008));
assert(std::abs(fadeFrames-expectedFade)<=1);
}
std::cout<<"Harmony multi-rate wet/space bypass matrix PASS; "
<<"scenarios="<<cases.size()
<<" wet_pcm="<<verifiedWet<<" dry_pcm="<<verifiedDry
<<" worst_slope_excess="<<worstExcess<<"\\n";
std::cout<<"Harmony native ChannelId warm bypass, dry PDC and state parity PASS; "
<<"wet_samples="<<samplesCheckedWet<<" dry_samples="<<samplesCheckedDry
<<" max_wet_difference="<<maxWetDifference
Expand Down
Loading
Loading