diff --git a/.github/workflows/harmony-fast-dsp.yml b/.github/workflows/harmony-fast-dsp.yml index cf4324086..9f56232df 100644 --- a/.github/workflows/harmony-fast-dsp.yml +++ b/.github/workflows/harmony-fast-dsp.yml @@ -25,10 +25,22 @@ jobs: run: | g++ -std=c++20 -O2 -Wall -Wextra tests/harmony_pitch_synchronous_test.cpp -o /tmp/arson-harmony-psola /tmp/arson-harmony-psola + - name: Prove low-register 65 Hz causal lookback and reject unsafe 16 ms claim + run: | + g++ -std=c++20 -O2 -Wall -Wextra tests/harmony_low_register_quality_gate_test.cpp -o /tmp/harmonice-low-singer + /tmp/harmonice-low-singer - name: Compile and run complete engine dry, pitch, stereo and graph-independent PCM fixtures run: | g++ -std=c++20 -O2 -Wall -Wextra tests/harmony_dsp_test.cpp -o /tmp/arson-harmony-engine /tmp/arson-harmony-engine + - name: Compile offline 32/24/16 ms PDC and partition safety matrix (NOT singer QA) + run: | + g++ -std=c++20 -O2 -Wall -Wextra tests/harmony_latency_candidate_test.cpp -o /tmp/harmonice-latency-study + /tmp/harmonice-latency-study + - name: Measure 32/24/16 ms DSP-only CPU tails on same Linux runner (non-gating) + run: | + g++ -std=c++20 -O2 -Wall -Wextra tests/harmony_cpu_profile_test.cpp -o /tmp/harmonice-cpu-study + /tmp/harmonice-cpu-study --latency-quick - name: Verify stage separation, mono and exact PCM run: | g++ -std=c++20 -O2 -Wall -Wextra tests/harmony_stage_spatial_test.cpp -o /tmp/arson-harmony-spatial @@ -37,3 +49,4 @@ jobs: run: | g++ -std=c++20 -O2 -Wall -Wextra tools/harmony_offline_ab.cpp -o /tmp/arson-harmony-ab /tmp/arson-harmony-ab --self-test + python3 tests/harmonice_quality_report_contract_test.py /tmp/arson-harmony-ab diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d826bbb0..f7567e8e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,6 +127,21 @@ if(BUILD_TESTING) add_executable(ARMixHarmonyOfflineAb tools/harmony_offline_ab.cpp) target_compile_features(ARMixHarmonyOfflineAb PRIVATE cxx_std_20) add_test(NAME NativeHarmonyOfflineDryAndABRenderHarness COMMAND ARMixHarmonyOfflineAb --self-test) + add_test(NAME NativeHarmoniceSyntheticWavABAndQualityReport + COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/harmonice_quality_report_contract_test.py" + "$") + # P1 experimental grain comparison: no change to the shipping 32 ms DSP. + # CTest only proves bounded PCM/PDC/partition parity, not vocal quality. + add_executable(ARMixHarmoniceLatencyStudyTests tests/harmony_latency_candidate_test.cpp) + target_compile_features(ARMixHarmoniceLatencyStudyTests PRIVATE cxx_std_20) + add_test(NAME NativeHarmoniceOfflineLatencyCandidatePcmSafety + COMMAND ARMixHarmoniceLatencyStudyTests) + # Explicit causal low-singer bound: 16ms cannot safely promise PSOLA + # coverage across the documented 65Hz source range. + add_executable(ARMixHarmoniceLowSingerGate tests/harmony_low_register_quality_gate_test.cpp) + target_compile_features(ARMixHarmoniceLowSingerGate PRIVATE cxx_std_20) + add_test(NAME NativeHarmoniceLowSingerPsolaCausalCoverage + COMMAND ARMixHarmoniceLowSingerGate) add_executable(ARMixHarmonyPitchSyncTests tests/harmony_pitch_synchronous_test.cpp) target_compile_features(ARMixHarmonyPitchSyncTests PRIVATE cxx_std_20) add_test(NAME NativeHarmonyPitchSynchronousFormantCandidateAndBlockParity COMMAND ARMixHarmonyPitchSyncTests) diff --git a/docs/HARMONICE_P1_QUALITY_LATENCY_EVIDENCE_20261110.md b/docs/HARMONICE_P1_QUALITY_LATENCY_EVIDENCE_20261110.md new file mode 100644 index 000000000..1725436c4 --- /dev/null +++ b/docs/HARMONICE_P1_QUALITY_LATENCY_EVIDENCE_20261110.md @@ -0,0 +1,61 @@ +# Harmonice P1 — latency/quality engineering evidence, 2026-11-10 + +**Status: experimental; NOT PRODUCTION APPROVED.** Shipping native `ChannelHarmonyProcessor` calls only `Engine::prepare(rate)`, retaining the approved **32 ms algorithmic latency**. Do not advertise a new live latency, guaranteed acoustic naturalness or physical ASIO XRUN result. + +## Checkpoints / independent ownership + +- Baseline main `6bcf88af30629d2e20026bfa8e0a84163c06c29e`. +- Reliability parent [#509](https://github.com/masarray/arsonmix/pull/509) at `618cb67c476ffe41904175cc87890f075c9c3b21`, exact SHA Windows [run 42735079669](https://github.com/masarray/arsonmix/actions/runs/42735079669): 80/80 CTest PASS, FAT/Inno preview artifacts. 8 ms sample-rate-correct bypass and one-shot Music guide detector reset verified at that specific SHA. +- This experiment [#514](https://github.com/masarray/arsonmix/pull/514), stacked over #509, **isolates offline-only 24/16 ms preparations**. Neither mixer/device ownership nor live plugin default PDC is changed. +- Rejected CPU cursor experiment [#515](https://github.com/masarray/arsonmix/pull/515): exact same PCM but inconsistent callback tails and worse measured p50/p95; closed **unmerged**. +- Shared owner PR [#512](https://github.com/masarray/arsonmix/pull/512) overlaps #509's JUCE processor and bypass/music tests. Do **not** blindly merge #509 or its stack with #512; reconcile on current main only when the shared mixer owner is ready. Vocal Throw #507 remains separate and owner-listening blocked. + +## Real latency, fixed PDC and causal PSOLA limits + +Granular algorithmic delay = two 16 ms grains. Candidate delay is still **twice the actual grain length**, rounded to device-rate frames: no underreporting to JUCE/PDC. Synthetic native PCM fixture verifies Wet0 exact delayed impulse, bounded wet output, opposite-polarity stereo, stable output for callback partitions 64/127/256, at 44.1/48/96 kHz. Actual sample counts: + +| Rate | 32 ms reference | 24 ms study | 16 ms study | +|---|---:|---:|---:| +| 44.1 kHz | 1412 | 1058 | 706 | +| 48 kHz | 1536 | 1152 | 768 | +| 96 kHz | 3072 | 2304 | 1536 | + +**Important negative quality result, based on DSP source physics and actual shifter PCM:** `HarmonyPitchSynchronous` requires `fixedDelay >= 1.2 * sourcePeriod`. At **65 Hz**, the causal minimum is **18.46 ms**. Consequently **16 ms cannot use the pitch-synchronous path at 65 Hz**; the real shifter returns not-ready and the engine falls back to its earlier granular shifter. That fallback is an existing intended safety mechanism, but not evidence of formant-stable natural backing singers. **16 ms is rejected as a full-range quality candidate** under the current architecture. At 65 Hz 24 ms and 32 ms do produce pitch-synchronous output, proven at 44.1/48/96 kHz; 82/110/220 Hz controls also checked with 64/127 partition identity. This does *not* mean 24 ms is automatically acoustically acceptable; it merely clears one physical feasibility gate. + +Candidate 24 ms reduces nominal algorithmic latency by **8 ms**, but no live runtime switch exists. True round-trip latency additionally includes ASIO buffers, input/output converters, device and other FX graph/PDC; it has **not been measured**. + +## Reproducible PCM and listening instrumentation + +Compile the existing native console tool, then run: + +```bash +# Optional reproducible DIAGNOSTIC samples (not human singing) +ARMixHarmonyOfflineAb --synthetic-corpus /tmp/harmonice-synthetic +# Human listening source; keep original singer recordings private +ARMixHarmonyOfflineAb --latency-study path/to/original-vocal.wav /tmp/harmonice-listen +``` + +Accepted source: original mono/stereo PCM16/PCM24 or IEEE float32 WAV, 32–192 kHz, <=60 sec. The second command writes a reference 32 ms wet mix/dry lead and 24/16 ms candidate wet mix/dry lead **time-aligned to 32 ms**, plus `latency-study.json` with actual PDC frames, alignment offsets, source-to-source RMS difference, backing-only RMS, stereo-side/mono-fold RMS, peak sample, largest adjacent-sample step, and 65 Hz causal support. These are **diagnostics, not perceptual naturalness scores**. The WAVs are not loudness-matched; level-match separately and blind-randomize before judging sound. + +The synthetic corpus includes low C2 (65 Hz), A3 (220 Hz), A4 (440 Hz), opposite-phase stereo A3, deterministically generated vowel-like harmonics, syllable onset, release and breath-like noise. Every fixture, WAV container and JSON report has an executable Python contract. There are **no original adult singer recordings in this repository** and no acoustic certification implied. + +## CPU: same-runner synthetic DSP evidence, non-gating + +Previous [Linux benchmark run 42752957451](https://github.com/masarray/arsonmix/actions/runs/42752957451), five backing voices, live Music key detector, 64 samples, Wet 50%, Space 60%, sustained synthetic A3: + +| Rate | 32 ms p99.9 | 24 ms p99.9 | 16 ms p99.9 | +|---|---:|---:|---:| +| 48 kHz | 31.24 µs | 26.42 µs | 27.04 µs | +| 96 kHz | 23.82 µs | 27.40 µs | 25.02 µs | + +No consistent synthetic CPU win or p99.9 promise; host/machine noise and lack of Windows ASIO integration make this an exploratory measurement only. + +## Decision / next acceptance gates + +1. **Retain 32 ms production PDC and shipping sound unchanged.** 16 ms not allowed to ship as full-range candidate. +2. 24 ms is **research-only**, pending human adult singer original recordings from low bass through soprano, consonant attacks, vibrato and stereo anti-phase, perceptually level-matched and aligned blind A/B against the exact 32 ms reference SHA; reject on metallic artifacts, formant deviation, unstable notes, stereo fold and altered room tails. +3. Windows full MSVC+CTest at **the final exact candidate SHA** plus FAT provenance if a preview is needed. PR draft quick CI is not proof of the Windows full job. Keep expensive workflows selective. +4. Physical 30-min Windows ASIO singer session with 64/128/256, 48/96 kHz where supported, guide drop/reacquisition, rack/slot bypass, device loss/restart, WIRE/SOURCE ONLY, .smix, Compressor/Player. Measure actual round-trip latency and XRUNs. +5. Neither #514 nor any QA mirror may be merged/published as stable while these gates remain open. Do not bring Vocal Throw into this task. + +**Quality verdict:** exact dry timing and deterministic synthetic bounds are proven; low-note 16 ms physics disqualifies it; 24 ms remains a plausible, *unqualified* listening candidate; real-singer naturalness and Windows ASIO performance remain **NOT VERIFIED**. diff --git a/engine/builtin_fx/ChannelHarmonyProcessor.h b/engine/builtin_fx/ChannelHarmonyProcessor.h index a5eb34930..b54cea5cc 100644 --- a/engine/builtin_fx/ChannelHarmonyProcessor.h +++ b/engine/builtin_fx/ChannelHarmonyProcessor.h @@ -33,7 +33,7 @@ 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); @@ -41,7 +41,16 @@ class ChannelHarmonyProcessor final : public juce::AudioProcessor, 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_) { + // FxInstance samples the AudioProcessor rate once, when its fixed + // 8 ms bypass envelope is prepared. Native built-ins are prepared + // directly; without JUCE rate metadata it otherwise sees zero + // and fades in only 64 samples at real 48/96 kHz device rates. + setRateAndBufferSizeDetails(rate, + std::max(1,maximumExpectedSamplesPerBlock)); + sampleRate_=rate; + setLatencySamples(dsp_.latencySamples()); + } } void releaseResources() override {ready_=false;} void processBlock(juce::AudioBuffer& samples,juce::MidiBuffer&) override { @@ -50,9 +59,16 @@ class ChannelHarmonyProcessor final : public juce::AudioProcessor, consumeRealtimeEdits(); // Missing/deleted/disconnected source must not leave a stale accepted // tonal estimate running indefinitely. Manual Key is the fallback. + const int guideTimeout=static_cast(sampleRate_*2.); + const bool guideWasFresh=guideAge_<=guideTimeout; guideAge_=std::min(guideAge_+samples.getNumSamples(), static_cast(sampleRate_*3.)); - if(guideAge_>static_cast(sampleRate_*2.)){ + if(guideWasFresh&&guideAge_>guideTimeout){ + // The guide source went missing. A detector that still retains + // old chroma would publish the old key on the FIRST resumed + // source block, before gathering fresh evidence. Invalidate once + // at the missing-source boundary, not at every audio callback. + guide_.reset(); dsp_.setGuideKey(-1,-1); guideObservedKey_.store(-1,std::memory_order_relaxed); guideObservedScale_.store(-1,std::memory_order_relaxed); diff --git a/engine/builtin_fx/HarmonyDsp.h b/engine/builtin_fx/HarmonyDsp.h index e1d60272c..6e28ad464 100644 --- a/engine/builtin_fx/HarmonyDsp.h +++ b/engine/builtin_fx/HarmonyDsp.h @@ -73,10 +73,24 @@ class Engine final { // history = 15360, so 16384 frames retain the full rate/ratio budget. // Halves the three large audio rings per instance without RT allocation. static constexpr std::size_t capacity=16384; - bool prepare(double rate) noexcept { + // Production ALWAYS uses the established 32 ms PDC. This opt-in entry + // exists ONLY for offline source-aligned latency/sound studies. Never + // select a shorter grain from session state, UI, or the ASIO callback. + bool prepare(double rate) noexcept {return prepareWithGrain(rate,0);} + bool prepareForOfflineLatencyStudy(double rate,int algorithmicMs) noexcept { + if(!std::isfinite(rate)||rate<32000.||rate>192000.|| + (algorithmicMs!=24&&algorithmicMs!=16))return false; + // Device-rate frames, never a fixed-size millisecond approximation. + const int candidateGrain=static_cast(std::lround( + rate*static_cast(algorithmicMs)/2000.)); + return prepareWithGrain(rate,candidateGrain); + } +private: + bool prepareWithGrain(double rate,int candidateGrain) noexcept { if(!std::isfinite(rate)||rate<32000.||rate>192000.)return false; rate_=rate; - grain_=std::clamp(static_cast(std::round(rate*0.016)),256,3072); + grain_=candidateGrain>0?candidateGrain: + std::clamp(static_cast(std::round(rate*0.016)),256,3072); latency_=2*grain_; decimation_=std::max(1,static_cast(std::round(rate/12000.))); detectEvery_=std::max(128,static_cast(rate/150.)); @@ -127,6 +141,7 @@ class Engine final { processed_=false;settings_=Settings{}; return setSettings(settings_); } +public: // Prepared on the CONTROL thread. Immutable edit data is copied through // the existing SPSC; applying it on the callback needs only fixed copies. struct PreparedSettings { diff --git a/tests/harmonice_quality_report_contract_test.py b/tests/harmonice_quality_report_contract_test.py new file mode 100644 index 000000000..71dfdb1eb --- /dev/null +++ b/tests/harmonice_quality_report_contract_test.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 +"""Harmonice offline quality-study contract. Synthetic output != singer qualification. + +This verifier tests the real C++ WAV renderer, not just isolated fake metrics: +four synthetic sources, 32/24/16ms source alignment, float32 RIFF integrity, +candidate causal bass limitation, dry-lead sample identity and honest metadata. +""" +import json +import math +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +CASES = { + "SYNTHETIC_low_C2_65Hz": False, + "SYNTHETIC_mid_A3_220Hz": False, + "SYNTHETIC_high_A4_440Hz": False, + "SYNTHETIC_antiphase_A3_220Hz": True, +} + + +def float32_stereo(path: Path): + data = path.read_bytes() + assert len(data) >= 44 and data[:4] == b"RIFF" and data[8:12] == b"WAVE", path + assert int.from_bytes(data[4:8], "little") + 8 == len(data), path + cursor, audio, fmt = 12, None, None + while cursor + 8 <= len(data): + tag = data[cursor:cursor + 4] + n = int.from_bytes(data[cursor + 4:cursor + 8], "little") + begin = cursor + 8 + assert begin + n <= len(data), path + if tag == b"fmt ": + fmt = struct.unpack_from("= 48000 * 8 + return audio + + +def check_file_pair(base: Path, aligned: Path, reference_offset: int, tolerance=0.0): + a, b = float32_stereo(base), float32_stereo(aligned) + assert len(a) == len(b), (base, aligned) + for offset in range(reference_offset * 8, len(a), 8): + al, ar = struct.unpack_from("= 0 + assert candidate["maximumAbsSample"] <= 4 + check_file_pair( + reference, output / f"harmonice_{ms}ms_dry_aligned.wav", + 1536 + 256 + ) + print(f"HARMONICE_SYNTHETIC_CORPUS_VALID source={name} " + "dry_alignment=sample_exact 16ms_bass_gate=blocked " + "no_human_singer_quality_claim=true") + print("Harmonice four-fixture WAV/JSON quality contract PASS (synthetic only)") + + +if __name__ == "__main__": + if len(sys.argv) != 2: + raise SystemExit("usage: harmonice_quality_report_contract_test.py ") + main(sys.argv[1]) diff --git a/tests/harmony_bypass_reliability_test.cpp b/tests/harmony_bypass_reliability_test.cpp index a36135bbc..c23806e1d 100644 --- a/tests/harmony_bypass_reliability_test.cpp +++ b/tests/harmony_bypass_reliability_test.cpp @@ -28,6 +28,9 @@ static LiveChannelFxGraph::InstanceLease makeHarmony( p->prepareToPlay(rate,quantum); const int declaredLatency=2*static_cast(std::round(rate*.016)); assert(p->isReady() && p->getLatencySamples()==declaredLatency); + // Prepared built-in host metadata drives the live FxInstance envelope. + // A zero JUCE rate shortened 8 ms to just 64 frames at 48/96 kHz. + assert(std::abs(p->getSampleRate()-rate)<1.e-6); armix::harmony::Settings rich; rich.voices=5;rich.wet=wet;rich.space=space; assert(p->queueAccepted(rich,9001)); @@ -252,6 +255,18 @@ int main() { verifiedWet+=wetCount;verifiedDry+=dryCount; assert(!cycle.rackBypassed(vocal)); assert(!b->bypassRequested.load(std::memory_order_acquire)); + // Test the actual FX lease envelope, not an isolated Fade helper. + // Every WET/Space and callback-size variant must retain 8 ms at the + // device rate rather than falling back to 64 samples. + 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(std::round(cfg.rate*.008)); + assert(std::abs(fadeFrames-expectedFade)<=1); } std::cout<<"Harmony multi-rate wet/space bypass matrix PASS; " <<"scenarios="< @@ -11,43 +12,66 @@ #include #include #include - +#include namespace { using Clock=std::chrono::steady_clock; constexpr int warmup=256; -constexpr int measured=512; -void profile(double rate,int block,int voices,bool musicGuide=false){ +constexpr int measured=1024; // >=1000 observations for a meaningful p99.9 rank +enum class Material {Sustain,Silence,Transitions,Dense}; +const char* name(Material m){ + switch(m){ + case Material::Sustain:return "sustain"; + case Material::Silence:return "silence"; + case Material::Transitions:return "voiced_unvoiced"; + case Material::Dense:return "dense_harmonics"; + } + return "unknown"; +} +std::size_t rank(double percentile){ + return std::min(static_cast(measured-1), + static_cast(std::ceil(percentile*measured)-1)); +} +void profile(double rate,int block,int voices,bool musicGuide=false, + float wet=.55f,float space=.60f,int studyMs=32, + Material material=Material::Sustain){ auto engine=std::make_unique(); - if(!engine->prepare(rate)){std::cerr<<"Harmony prepare failed\n";std::exit(2);} + const bool ok=studyMs==32?engine->prepare(rate): + engine->prepareForOfflineLatencyStudy(rate,studyMs); + if(!ok){std::cerr<<"Harmony prepare failed\n";std::exit(2);} armix::harmony::Settings settings; - settings.voices=voices;settings.wet=0.55f;settings.correction=true; + settings.voices=voices;settings.wet=wet;settings.space=space; + settings.correction=true; settings.guideMode=musicGuide?1:0; settings.guideChannelId=musicGuide?770:0; armix::harmony::MusicKeyDetector guide; if(musicGuide&&!guide.prepare(rate)){ - std::cerr<<"Music guide prepare failed\\n";std::exit(2); + std::cerr<<"Music guide prepare failed\n";std::exit(2); + } + if(!engine->setSettings(settings)){ + std::cerr<<"Harmony settings rejected\n";std::exit(2); } - if(!engine->setSettings(settings)){std::cerr<<"Harmony settings rejected\n";std::exit(2);} std::array l{},r{},musicL{},musicR{}; std::array elapsed{}; double phase=0.; const double step=6.283185307179586*220./rate; for(int blockIndex=0;blockIndex(0.16*std::sin(phase)+ - 0.04*std::sin(2.*phase)); + const bool active=material!=Material::Silence&& + (material!=Material::Transitions||(blockIndex/24)%2==0); + const double extra=material==Material::Dense? + .06*std::sin(3.*phase)+.04*std::sin(5.*phase):0.; + const float x=active?static_cast( + .16*std::sin(phase)+.04*std::sin(2.*phase)+extra):0.f; l[static_cast(i)]=x; r[static_cast(i)]=x; if(musicGuide){ - // Different ChannelId source; no music PCM mixed into vocal. - // Exclude guide-tone fixture synthesis from timed callback. const double t=static_cast(blockIndex*block+i)/rate; musicL[static_cast(i)]= - musicR[static_cast(i)]=static_cast( - .06*(std::sin(6.283185307179586*261.625565*t)+ - std::sin(6.283185307179586*329.627557*t)+ - std::sin(6.283185307179586*391.995436*t))); + musicR[static_cast(i)]=static_cast( + .06*(std::sin(6.283185307179586*261.625565*t)+ + std::sin(6.283185307179586*329.627557*t)+ + std::sin(6.283185307179586*391.995436*t))); } phase+=step; if(phase>=6.283185307179586)phase-=6.283185307179586; @@ -74,28 +98,60 @@ void profile(double rate,int block,int voices,bool musicGuide=false){ [deadlineUs](double elapsedUs){return elapsedUs>deadlineUs;}); std::sort(elapsed.begin(),elapsed.end()); std::cout<2||(argc==2&&mode!="--matrix"&&mode!="--latency-quick")){ + std::cerr<<"Usage: harmony_cpu_profile_test [--matrix|--latency-quick]\n"; + return 2; + } + if(mode=="--latency-quick"){ + // Same runner, same source PCM and settings for all grain candidates. + // Comparison is CPU only and says NOTHING about human-singer quality. + for(double rate:{48000.,96000.}) + for(int ms:{32,24,16}) + for(Material m:{Material::Sustain,Material::Dense}) + profile(rate,64,5,true,.5f,.6f,ms,m); + return 0; + } + if(mode=="--matrix"){ + for(double rate:{44100.,48000.,96000.,192000.}) + for(int block:{64,128,256}) + for(int voices:{2,3,4,5}) + for(float wet:{0.f,.5f,1.f}) + for(float space:{0.f,.6f,1.f}) + profile(rate,block,voices,false,wet,space); + for(double rate:{44100.,48000.,96000.,192000.}) + for(int block:{64,128,256}) + for(Material m:{Material::Sustain,Material::Silence, + Material::Transitions,Material::Dense}) + profile(rate,block,5,true,1.f,1.f,32,m); + return 0; + } + // Existing quick non-gating CTest: complete engine baseline at ASIO-like + // periods. The extended grid is optional to avoid wasting paid CI. for(int voices:{3,4,5}) for(double rate:{48000.,96000.}) for(int block:{64,128,256}) profile(rate,block,voices); - // Device upper-bound stress: never interpreted as physical ASIO proof. for(int block:{64,128,256})profile(192000.,block,5); - // Real guide detector + vocal DSP in ONE timed region, without - // synthesizing the separate music source inside the measurement. for(double rate:{48000.,96000.,192000.}) profile(rate,64,5,true); -} \ No newline at end of file +} diff --git a/tests/harmony_latency_candidate_test.cpp b/tests/harmony_latency_candidate_test.cpp new file mode 100644 index 000000000..7482d4c2b --- /dev/null +++ b/tests/harmony_latency_candidate_test.cpp @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Exploratory P1 grain/latency comparison. These are PCM SAFETY checks, +// not auditory quality approval. The shipping DSP still prepares at 32 ms. +#include "../engine/builtin_fx/HarmonyDsp.h" +#include +#include +#include +#include +#include +#include +namespace { +using armix::harmony::Engine; +using armix::harmony::Settings; +constexpr double tau=6.2831853071795864769; +void check(bool value,const char* why){ + if(!value){std::cerr<<"Harmonice latency study: "< make(double rate,int milliseconds,const Settings& s){ + auto e=std::make_unique(); // fixed DSP rings are not stack objects + check(milliseconds==32?e->prepare(rate): + e->prepareForOfflineLatencyStudy(rate,milliseconds),"prepare rejected"); + check(e->setSettings(s),"settings rejected"); + const int grain=static_cast(std::round( + rate*static_cast(milliseconds)/2000.)); + check(e->latencySamples()==2*grain,"latency does not match physical grain"); + return e; +} +void dryImpulse(double rate,int ms,int block){ + Settings dry;dry.wet=0.f; + auto e=make(rate,ms,dry); + const int latency=e->latencySamples(); + const int total=latency+std::max(32,block)+1; + std::vector left(static_cast(total),0.f); + std::vector right(static_cast(total),0.f); + left[0]=.5f;right[0]=-.25f; + for(int pos=0;posprocess(left.data()+pos,right.data()+pos,std::min(block,total-pos)); + for(int i=0;i(i)]-l)<1.e-6f&& + std::abs(right[static_cast(i)]-r)<1.e-6f, + "Wet0 dry PDC not sample exact"); + } +} +struct Audio {std::vector left,right;}; +Audio vocal(double rate,bool opposed){ + const int n=static_cast(std::round(rate*.55)); + Audio a{std::vector(static_cast(n)), + std::vector(static_cast(n))}; + for(int i=0;i(i)/rate; + const float v=static_cast( + .16*std::sin(tau*220.*t)+.045*std::sin(tau*440.*t+.2)+ + .022*std::sin(tau*660.*t+.1)); + a.left[static_cast(i)]=v; + a.right[static_cast(i)]=opposed?-v:v; + } + return a; +} +Audio render(double rate,int ms,int block,const Audio& source){ + Settings s;s.voices=5;s.wet=.75f;s.space=.60f;s.correction=false; + auto e=make(rate,ms,s); + auto out=source; + const int total=static_cast(out.left.size()); + for(int pos=0;posprocess(out.left.data()+pos,out.right.data()+pos, + std::min(block,total-pos)); + check(e->voiced()&&e->observedMidiNote()==57, + "fixed singer A3 tonal evidence lost"); + double backing=0.; + const int delay=e->latencySamples(); + for(int i=total/2;i(i); + const auto earlier=static_cast(i-delay); + const float l=out.left[at],r=out.right[at]; + check(std::isfinite(l)&&std::isfinite(r)&& + std::abs(l)<=4.f&&std::abs(r)<=4.f, + "nonfinite/unbounded candidate PCM"); + const double dl=l-source.left[earlier]; + const double dr=r-source.right[earlier]; + backing+=dl*dl+dr*dr; + } + check(backing>.001,"wet backing disappeared in candidate"); + return out; +} +} +int main(){ + auto invalid=std::make_unique(); + check(!invalid->prepareForOfflineLatencyStudy(48000.,8), + "unsupported 8 ms candidate admitted"); + check(!invalid->prepareForOfflineLatencyStudy(48000.,32), + "offline API incorrectly duplicates production preparation"); + for(double rate:{44100.,48000.,96000.}){ + for(int ms:{32,24,16}){ + for(int block:{64,127,256})dryImpulse(rate,ms,block); + for(bool opposed:{false,true}){ + const auto input=vocal(rate,opposed); + const auto reference=render(rate,ms,64,input); + for(int block:{127,256}){ + const auto alternate=render(rate,ms,block,input); + for(std::size_t i=0;i +#include +#include +#include +#include +#include +#include +#include +namespace { +using armix::harmony::Engine; +using Shifter=armix::harmony::PitchSynchronousVoice; +constexpr double twopi=6.2831853071795864769; +void need(bool ok,const char* why){if(!ok){std::cerr<(std::lround(rate*latencyMs/2000.)); +} +int countPitchSynchronousSamples(double rate,double hz,int latencyMs,int chunk){ + auto p=std::make_unique(); + auto pcm=std::make_unique>(); + pcm->fill(0.f); + const int frames=static_cast(rate*.38); + int ready=0; + for(int offset=0;offset(i)/rate; + const float vocal=static_cast( + .17*std::sin(twopi*hz*t)+ + .05*std::sin(twopi*2.*hz*t+.2)+ + .026*std::sin(twopi*3.*hz*t+.34)); + const auto cursor=static_cast(i)&(Engine::capacity-1); + (*pcm)[cursor]=vocal; + const auto y=p->process(0,static_cast(i),cursor, + *pcm,static_cast(rate/hz),1.5f, + static_cast(grainFrames(rate,latencyMs)*2)); + need(std::isfinite(y.sample)&&std::abs(y.sample)<2.f, + "pitch synchronous PCM is not finite and bounded"); + if(y.ready)++ready; + } + } + return ready; +} +} +int main(){ + for(double rate:{44100.,48000.,96000.}){ + for(double hz:{65.,82.406889,110.,220.}){ + for(int latency:{32,24,16}){ + const double declaredMs=2000.*grainFrames(rate,latency)/rate; + const double minimumCausalMs=1200./hz; + const bool theoreticallySupported=declaredMs>=minimumCausalMs; + const int count64=countPitchSynchronousSamples(rate,hz,latency,64); + const int count127=countPitchSynchronousSamples(rate,hz,latency,127); + need((count64>0)==theoreticallySupported, + "candidate PSOLA low-note causal support disagrees with real PCM"); + need(count64==count127, + "low-note candidate PSOLA readiness depends on callback partition"); + std::cout<0, + "24ms candidate lost low-register PSOLA despite causal lookback"); + // Only the production path may remain automatically selected by native + // FX. New study modes must never silently reduce the live declaration. + auto production=std::make_unique(); + need(production->prepare(48000.)&&production->latencySamples()==1536, + "shipping Harmonice PDC is not 1536 frames at 48k"); + std::cout<<"Harmonice low-register causal quality gate PASS;" + " 16ms remains research-only, no acoustic claim\n"; +} diff --git a/tests/harmony_music_bank_test.cpp b/tests/harmony_music_bank_test.cpp index ba4adbc72..e01d848e2 100644 --- a/tests/harmony_music_bank_test.cpp +++ b/tests/harmony_music_bank_test.cpp @@ -67,10 +67,51 @@ int main(){ // ages out safely without touching physical routing/output authority. for(int i=0;i<950;++i) { float l[128]{},r[128]{}; - bank(vocal,l,r,128); + for(int j=0;j<128;++j) { + const double t=static_cast(i*128+j)/48000.; + l[j]=r[j]=static_cast(.20*std::sin( + 6.28318530718*220.*t)); + } + bank(vocal,l,r,128); // singer remains live during guide loss + for(int j=0;j<128;++j) + assert(std::isfinite(l[j])&&std::isfinite(r[j])); } + assert(live->observedNote()>=0); // no lead/pitch-analysis interruption assert(live->observedMusicKey()==-1); assert(live->effectiveKeyCode()==14); // music absent: manual D minor + // Regression: resuming a missing music ChannelId must NOT reuse its + // previously acquired key after two seconds of missing PCM. The old + // detector retained a valid C-major estimate and immediately published + // it on the first resumed source block, before any new tonal evidence. + { + float resumedL[128]{},resumedR[128]{}; + for(int j=0;j<128;++j){ + const double t=static_cast(j)/48000.; + resumedL[j]=resumedR[j]=static_cast(.09*( + std::sin(6.28318530718*220.*t)+ + std::sin(6.28318530718*261.625565*t)+ + std::sin(6.28318530718*329.627557*t))); + } + const float originalL=resumedL[17],originalR=resumedR[17]; + bank(music,resumedL,resumedR,128); + assert(resumedL[17]==originalL&&resumedR[17]==originalR); + assert(live->observedMusicKey()==-1); + assert(live->effectiveKeyCode()==14); // no stale C major flash + } + // Fresh A-minor guide must eventually retune after reacquisition. + for(int block=0;block<1450;++block){ + float l[128]{},r[128]{}; + for(int j=0;j<128;++j){ + const double t=static_cast(block*128+j)/48000.; + l[j]=r[j]=static_cast(.09*( + std::sin(6.28318530718*220.*t)+ + std::sin(6.28318530718*261.625565*t)+ + std::sin(6.28318530718*329.627557*t))); + } + bank(music,l,r,128); + } + assert(live->observedMusicKey()==9&&live->observedMusicScale()==1); + assert(live->effectiveKeyCode()==21); juce::MemoryBlock saved; live->getStateInformation(saved); // Realtime processor owns fixed DSP and reflection rings; production diff --git a/tools/harmony_offline_ab.cpp b/tools/harmony_offline_ab.cpp index de612a792..e7b55f493 100644 --- a/tools/harmony_offline_ab.cpp +++ b/tools/harmony_offline_ab.cpp @@ -3,6 +3,7 @@ // Synthesized self-test is only a build and integrity gate, not an ear-test. #include "../engine/builtin_fx/HarmonyDsp.h" #include +#include #include #include #include @@ -87,9 +88,12 @@ static void writeWave(const fs::path& path,const Wave& wave){ if(!out)throw std::runtime_error("WAV output write failed"); } static Wave process(const Wave& input,Settings settings,bool synchronous,int block=127, - bool enhancedDepth=true,bool focus=true,bool presence=true){ + bool enhancedDepth=true,bool focus=true,bool presence=true, + int offlineLatencyMs=32){ auto e=std::make_unique(); - if(!e->prepare(input.rate)||!e->setSettings(settings)) + const bool prepared=offlineLatencyMs==32?e->prepare(input.rate): + e->prepareForOfflineLatencyStudy(input.rate,offlineLatencyMs); + if(!prepared||!e->setSettings(settings)) throw std::runtime_error("native Harmony configuration invalid"); e->setPitchSynchronousCandidateForOfflineReference(synchronous); e->setStageDepthForOfflineReference(enhancedDepth); @@ -176,6 +180,182 @@ static void renderAb(const Wave& input,const fs::path& folder){ settings.space=1.f;writeWave(folder/"07_new_space_100.wav",process(input,settings,true)); generateReport(folder,input,dry,old,modern,backing,previousDepth,previousPresence); } +// Exploratory, physical source-to-source grain study. Candidate WAVs are +// delayed to the ORIGINAL 32 ms lead arrival before perceptual A/B. This +// alignment is offline only and does not hide or change the candidate's +// declared PDC. RMS deltas cannot prove formant transparency or naturalness. +static Wave delayedCopy(const Wave& in,int frames){ + if(frames<0)throw std::runtime_error("negative offline alignment"); + Wave out=in; + for(std::size_t i=in.left.size();i-->0;){ + out.left[i]=i>=static_cast(frames)? + in.left[i-static_cast(frames)]:0.f; + out.right[i]=i>=static_cast(frames)? + in.right[i-static_cast(frames)]:0.f; + } + return out; +} +static double alignedDifferenceRms(const Wave& a,const Wave& b,int skip){ + if(a.left.size()!=b.left.size()||skip<0) + throw std::runtime_error("unmatched WAV alignment"); + double sum=0.;std::size_t used=0; + for(std::size_t i=static_cast(skip);i(used)):0.; +} +// Lightweight offline diagnostics of the ALIGNED output. These are not +// perceptual scores and must never enter the realtime audio callback. +static double sideRms(const Wave& v){ + double sum=0.; + for(std::size_t i=0;i(rate*1.60); + Wave out;out.rate=rate; + out.left.resize(static_cast(frames)); + out.right.resize(static_cast(frames)); + constexpr double twoPi=6.2831853071795864769; + std::uint32_t noise=0x574f4341u; // repeatable across Windows/Linux + for(int i=0;i(i)/rate; + // A brief consonant-like noise onset and periodic late release. + double amp=0.; + if(t<.05)amp=.11*(t/.05); + else if(t<.85)amp=.11; + else if(t<1.02)amp=.11*(1.02-t)/.17; + else if(t<1.18)amp=0.; + else if(t<1.22)amp=.095*(t-1.18)/.04; + else if(t<1.48)amp=.095; + else amp=.095*std::max(0.,(1.60-t)/.12); + const double phase=twoPi*hz*t+(.015*std::sin(twoPi*4.4*t)); + const double vowel=std::sin(phase)+ + .40*std::sin(2*phase+.2)+.19*std::sin(3*phase+.31)+ + .09*std::sin(4*phase+.12); + noise=noise*1664525u+1013904223u; + const double white=double((noise>>8)&0xffffu)/32767.5-1.; + const double burst=(t>=.015&&t<.055)||(t>=1.17&&t<1.21)? + .015*white:0.; + const float sample=static_cast(amp*vowel+burst); + const auto at=static_cast(i); + out.left[at]=sample; + out.right[at]=antiphase?-sample:sample; + } + return out; +} +static void renderLatencyStudy(const Wave& input,const fs::path& folder); +static void makeSyntheticCorpus(const fs::path& folder){ + // Names disclose synthetic origin so nobody mistakes this package for + // male/female human-singer listening or formant preservation evidence. + fs::create_directories(folder); + struct Fixture {const char* name;double hz;bool antiphase;}; + for(const auto& cfg:std::array{{ + {"SYNTHETIC_low_C2_65Hz",65.,false}, + {"SYNTHETIC_mid_A3_220Hz",220.,false}, + {"SYNTHETIC_high_A4_440Hz",440.,false}, + {"SYNTHETIC_antiphase_A3_220Hz",220.,true} + }}){ + const auto input=syntheticVocal(48000,cfg.hz,cfg.antiphase); + writeWave(folder/(std::string(cfg.name)+".wav"),input); + renderLatencyStudy(input,folder/cfg.name); + } + std::ofstream description(folder/"READ_ME_FIRST.txt"); + description<<"SYNTHETIC Harmonice DSP/A-B diagnostics ONLY.\n" + "These generated waveforms are NOT recordings of humans,\n" + "cannot qualify naturalness, real-singer formants, consonants,\n" + "live ASIO round-trip latency, crackles or physical XRUNs.\n" + "Listen to aligned 32/24/16ms WAVs as a debugging aid only.\n" + "For acoustic acceptance, rerun --latency-study with genuine\n" + "consented adult-singer WAVs at identical source settings.\n"; + if(!description)throw std::runtime_error("corpus README write failed"); +} +static void renderLatencyStudy(const Wave& input,const fs::path& folder){ + fs::create_directories(folder); + Settings settings; + settings.voices=5;settings.wet=.65f;settings.space=.60f; + settings.correction=false;settings.gainDb=0.f; + auto dry=settings;dry.wet=0.f; + const Wave baseline=process(input,settings,true); + const Wave baselineDry=process(input,dry,true); + writeWave(folder/"harmonice_32ms_reference.wav",baseline); + writeWave(folder/"harmonice_32ms_dry.wav",baselineDry); + std::ofstream report(folder/"latency-study.json"); + if(!report)throw std::runtime_error("latency study report open failed"); + const int baselineDelay=2*static_cast(std::round(input.rate*.016)); + report<<"{\n \"sourceRate\": "<(std::round( + input.rate*static_cast(ms)/2000.)); + const Wave candidate=process(input,settings,true,127,true,true,true,ms); + const Wave candidateDry=process(input,dry,true,127,true,true,true,ms); + const int align=baselineDelay-frames; + const auto aligned=delayedCopy(candidate,align); + const auto alignedDry=delayedCopy(candidateDry,align); + const std::string prefix="harmonice_"+std::to_string(ms)+"ms"; + writeWave(folder/(prefix+"_aligned.wav"),aligned); + writeWave(folder/(prefix+"_dry_aligned.wav"),alignedDry); + if(!first)report<<",\n"; + first=false; + const Wave backing=subtract(aligned,alignedDry); + const double candidateBackingRms=rms(backing); + const double minimumPitchSyncHz=1.2*input.rate/frames; + report<<" {\"targetMs\": "<(std::round(v.rate*.016)); + for(int ms:{24,16}){ + const auto shorter=process(v,s,true,127,true,true,true,ms); + const int shorterFrames=2*static_cast(std::round( + v.rate*static_cast(ms)/2000.)); + const auto aligned=delayedCopy(shorter,baselineFrames-shorterFrames); + if(alignedDifferenceRms(dry32,aligned,baselineFrames+256)>1.e-7) + return 6; + } + std::cout<<"Offline native A/B and candidate dry alignment passed" + " (synthetic only)\n"; + return 0; + } + if(argc==3&&std::string(argv[1])=="--synthetic-corpus"){ + makeSyntheticCorpus(argv[2]); + std::cout<<"Synthetic-only WAV corpus and aligned A/B diagnostics written; " + "no human-singer qualification.\n"; + return 0; + } + if(argc==4&&std::string(argv[1])=="--latency-study"){ + renderLatencyStudy(readWave(argv[2]),argv[3]); + std::cout<<"32/24/16 ms latency A/B WAVs aligned. Human listening NOT verified.\n"; return 0; } - if(argc!=3){std::cerr<<"usage: harmony_offline_ab input.wav output_directory\n";return 1;} + if(argc!=3){std::cerr<<"usage: harmony_offline_ab input.wav output_directory" + " | harmony_offline_ab --latency-study input.wav output_directory" + " | harmony_offline_ab --synthetic-corpus output_directory\n";return 1;} renderAb(readWave(argv[1]),argv[2]); std::cout<<"WAV A/B written. No human-singing/formant qualification implied.\n"; return 0;