From 1213a672f744ada92f114d398691f3036a8d3b9c Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Mon, 9 Nov 2026 11:00:19 +0700 Subject: [PATCH] perf(harmony): sliding lag energy and scoped synthetic callback CPU profiling --- CMakeLists.txt | 6 +- docs/ARSON_HARMONY_ENGINE_AUDIT_20261108.md | 7 ++ engine/builtin_fx/HarmonyDsp.h | 21 +++++- tests/harmony_cpu_profile_test.cpp | 71 +++++++++++++++++++++ tests/harmony_dsp_test.cpp | 19 ++++++ 5 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 tests/harmony_cpu_profile_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d3637a007..cee805af8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,6 +118,10 @@ if(BUILD_TESTING) add_executable(ARMixHarmonyDspTests tests/harmony_dsp_test.cpp) target_compile_features(ARMixHarmonyDspTests PRIVATE cxx_std_20) add_test(NAME NativeHarmonyMultivoicePitchAndRealtimeFinitePCM COMMAND ARMixHarmonyDspTests) + # Non-gating synthetic engine-only CPU profile (not ASIO certification). + add_executable(ARMixHarmonyCpuProfileTests tests/harmony_cpu_profile_test.cpp) + target_compile_features(ARMixHarmonyCpuProfileTests PRIVATE cxx_std_20) + add_test(NAME NativeHarmonySyntheticCallbackCpuProfile COMMAND ARMixHarmonyCpuProfileTests) add_executable(ARMixHarmonyMusicGuideTests tests/harmony_music_key_test.cpp) target_compile_features(ARMixHarmonyMusicGuideTests PRIVATE cxx_std_20) add_test(NAME NativeHarmonyCrossChannelMusicKeyAndManualFallback COMMAND ARMixHarmonyMusicGuideTests) @@ -129,7 +133,7 @@ if(BUILD_TESTING) add_test(NAME ApprovedNativeHarmonyEditorAckAndSingleGui COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_harmony_assembly_test.py") add_executable(ARMixMixerRoutingTests tests/mixer_routing_plan_test.cpp) target_compile_features(ARMixMixerRoutingTests PRIVATE cxx_std_20) - add_dependencies(ARMixMixerRoutingTests ARMixHarmonyDspTests ARMixHarmonyMusicGuideTests ARMixHarmonyChannelPcmTests) + add_dependencies(ARMixMixerRoutingTests ARMixHarmonyDspTests ARMixHarmonyMusicGuideTests ARMixHarmonyChannelPcmTests ARMixHarmonyCpuProfileTests) add_test(NAME TenStripStaticRoutingAndMaster COMMAND ARMixMixerRoutingTests) # M3D atomic native Session: the accepted Console roster cannot advance # without a successful epoch/revision-gated PCM publication. diff --git a/docs/ARSON_HARMONY_ENGINE_AUDIT_20261108.md b/docs/ARSON_HARMONY_ENGINE_AUDIT_20261108.md index 2a205e6fb..551de6261 100644 --- a/docs/ARSON_HARMONY_ENGINE_AUDIT_20261108.md +++ b/docs/ARSON_HARMONY_ENGINE_AUDIT_20261108.md @@ -49,3 +49,10 @@ A 1536-sample lookback at 48 kHz is **32 ms DSP delay**, before device input + o - Rubber Band v4 technical docs expose formant options and exact latency API, but licensing, ASIO callback suitability and measured overhead must be independently assessed; do not add by assumption. **Status:** engineering candidate only; PR stays Draft pending exact-HEAD qualification and physical vocal FAT. + +## 2026-11-09 follow-up — pitch CPU and measurable callback budget (post-merge) + +- Roll the shifted window's energy from one candidate lag to the next. Keep the existing dot-product, confidence threshold, quantizer and FX/PDC/GUI contracts. Each adjacent lag changes by one outgoing + one incoming squared sample instead of recomputing 160 squares. Only floating-point rounding may differ. +- Extend deterministic C++ PCM regressions to a multi-harmonic A2 sample with 64/128/257-frame chunks. Existing E2/C6, dry-latency alignment, automation and music-guide tests remain in force. +- Add `NativeHarmonySyntheticCallbackCpuProfile` to CTest. Synthetic engine-only p50/p95/p99/max data covers 48 kHz (64/128/256 samples), 96 kHz (128 samples), with 2/5 voices. Timing is recorded only outside engine->process(), but Windows runner contention makes absolute timing unsuitable for a CI hard gate. +- No quantitative speedup claim without controlled before/after on one machine. No ASIO driver, JUCE graph, real-world multichannel CPU, formant quality or ear monitoring is measured by this synthetic profile. Physical singer FAT remains release blocking. diff --git a/engine/builtin_fx/HarmonyDsp.h b/engine/builtin_fx/HarmonyDsp.h index 56d245e84..e21c05527 100644 --- a/engine/builtin_fx/HarmonyDsp.h +++ b/engine/builtin_fx/HarmonyDsp.h @@ -270,14 +270,29 @@ class Engine final { } float best=-1.f;int bestLag=0; std::array correlations{}; + // Adjacent lagged windows overlap in span-1 samples. Their energy + // moves with one departing/one entering squared sample per lag. + // Preserve the original dot product and confidence thresholds while + // avoiding another span-sized energy sum for each candidate. + double lagEnergy=0.; + for(int j=0;j(j+lowLag_)]; + lagEnergy+=y*y; + } for(int lag=lowLag_;lag<=highLag_;++lag){ - double xy=0.,yy=0.; + if(lag>lowLag_){ + const double leaving=samples[static_cast(lag-1)]; + const double entering=samples[static_cast(span+lag-1)]; + lagEnergy+=entering*entering-leaving*leaving; + } + double xy=0.; for(int j=0;j(j)]; const double y=samples[static_cast(j+lag)]; - xy+=x*y;yy+=y*y; + xy+=x*y; } - const float correlation=static_cast(xy/std::sqrt(energy*yy+1.e-14)); + const float correlation=static_cast( + xy/std::sqrt(energy*std::max(0.,lagEnergy)+1.e-14)); correlations[static_cast(lag)]=correlation; if(correlation>best){best=correlation;bestLag=lag;} } diff --git a/tests/harmony_cpu_profile_test.cpp b/tests/harmony_cpu_profile_test.cpp new file mode 100644 index 000000000..deadc8bcd --- /dev/null +++ b/tests/harmony_cpu_profile_test.cpp @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Non-gating, reproducible synthetic ENGINE-only callback CPU profile. +// Does not measure JUCE graph scheduling, ASIO driver or singer ear latency. +#include "../engine/builtin_fx/HarmonyDsp.h" +#include +#include +#include +#include +#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){ + auto engine=std::make_unique(); + if(!engine->prepare(rate)){std::cerr<<"Harmony prepare failed\n";std::exit(2);} + armix::harmony::Settings settings; + settings.voices=voices;settings.wet=0.55f;settings.correction=true; + if(!engine->setSettings(settings)){std::cerr<<"Harmony settings rejected\n";std::exit(2);} + std::array l{},r{}; + 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)); + l[static_cast(i)]=x; + r[static_cast(i)]=x; + phase+=step; + if(phase>=6.283185307179586)phase-=6.283185307179586; + } + const auto started=Clock::now(); + engine->process(l.data(),r.data(),block); + const auto finished=Clock::now(); + if(blockIndex>=warmup) + elapsed[static_cast(blockIndex-warmup)]= + std::chrono::duration(finished-started).count(); + for(int i=0;i(i)])|| + !std::isfinite(r[static_cast(i)])){ + std::cerr<<"Nonfinite Harmony PCM in profile\n";std::exit(2); + } + } + const double deadlineUs=1.e6*static_cast(block)/rate; + const auto misses=std::count_if(elapsed.begin(),elapsed.end(), + [deadlineUs](double elapsedUs){return elapsedUs>deadlineUs;}); + std::sort(elapsed.begin(),elapsed.end()); + std::cout<(); + need(pitched->prepare(48000.),"harmonic tracking prepare failed"); + constexpr int samples=24000; + std::vector hl(samples),hr(samples); + for(int i=0;i(i)/48000.; + hl[i]=hr[i]=static_cast( + .17*std::sin(6.283185307179586*110.*t)+ + .04*std::sin(6.283185307179586*220.*t)+ + .02*std::sin(6.283185307179586*330.*t)); + } + for(int i=0;iprocess(hl.data()+i,hr.data()+i,std::min(block,samples-i)); + need(pitched->voiced()&&pitched->observedMidiNote()==45, + "rolling pitch energy altered singer-like A2 classification"); + } std::cout<<"Native Harmony DSP: deterministic PCM, 2-5 voices, octave/scale, stereo, silence and 32-192k PASS\n"; }