diff --git a/.github/workflows/harmony-fast-dsp.yml b/.github/workflows/harmony-fast-dsp.yml index 6fe40feeb..99a5a108f 100644 --- a/.github/workflows/harmony-fast-dsp.yml +++ b/.github/workflows/harmony-fast-dsp.yml @@ -25,6 +25,10 @@ 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: Profile legacy versus exact-output epoch scan on same runner (non-gating) + run: | + g++ -std=c++20 -O2 -Wall -Wextra tests/harmonice_epoch_cpu_profile_test.cpp -o /tmp/harmonice-epoch-cpu + /tmp/harmonice-epoch-cpu - 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 diff --git a/CMakeLists.txt b/CMakeLists.txt index e897e42d0..45c5ad3f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -136,6 +136,12 @@ if(BUILD_TESTING) add_executable(ARMixHarmonyPitchSyncTests tests/harmony_pitch_synchronous_test.cpp) target_compile_features(ARMixHarmonyPitchSyncTests PRIVATE cxx_std_20) add_test(NAME NativeHarmonyPitchSynchronousFormantCandidateAndBlockParity COMMAND ARMixHarmonyPitchSyncTests) + # Non-gating microprofile of exact-output reference versus ring-cursor + # epoch scan. Must not be represented as total mixer/ASIO timing. + add_executable(ARMixHarmoniceEpochCpuProfile tests/harmonice_epoch_cpu_profile_test.cpp) + target_compile_features(ARMixHarmoniceEpochCpuProfile PRIVATE cxx_std_20) + add_test(NAME NativeHarmoniceEpochSameRunnerCpuComparison + COMMAND ARMixHarmoniceEpochCpuProfile) add_executable(ARMixHarmonySpatialTests tests/harmony_stage_spatial_test.cpp) target_compile_features(ARMixHarmonySpatialTests PRIVATE cxx_std_20) add_test(NAME NativeHarmonyBackingStageSpatialDepthAndMonoPDC COMMAND ARMixHarmonySpatialTests) diff --git a/engine/builtin_fx/HarmonyPitchSynchronous.h b/engine/builtin_fx/HarmonyPitchSynchronous.h index 9395ea1ca..31e7a0b28 100644 --- a/engine/builtin_fx/HarmonyPitchSynchronous.h +++ b/engine/builtin_fx/HarmonyPitchSynchronous.h @@ -13,7 +13,10 @@ namespace armix::harmony { // Positive waveform peaks are approximate epochs, NOT proven glottal closures. // This must pass actual singer A/B before acoustic release qualification. // All state is fixed; no allocation, locks, FFT or IO in process(). -template +// The default ring-cursor walk is sample-exact to the original lookup. +// False compiles the historical age/subtraction loop for SAME-RUNNER offline +// A/B; this is a compile-time switch, never a GUI/session/RT branch. +template class PitchSynchronousVoice final { static_assert(Capacity >= 4096 && (Capacity & (Capacity-1)) == 0); static_assert(Voices > 0 && Voices <= 8); @@ -95,11 +98,27 @@ class PitchSynchronousVoice final { if(end( - static_cast(time)-t); - const float value=input[(writeIndex+Capacity-age)&(Capacity-1)]; - if(value>best){best=value;peak=t;} + if constexpr(FastEpochWalk) { + // Adjacent integer sample times occupy adjacent ring cells. One + // initial age/index calculation replaces the repeated 64-bit + // per-candidate age and index arithmetic, without changing sample + // access order, threshold or earliest-max tie behavior. + const auto firstAge=static_cast( + static_cast(time)-start); + auto ring=(writeIndex+Capacity-firstAge)&(Capacity-1); + for(auto t=start;t<=end;++t) { + const float value=input[ring]; + if(value>best){best=value;peak=t;} + ring=(ring+1)&(Capacity-1); + } + }else{ + // Legacy reference, built only by the offline PCM/CPU verifier. + for(auto t=start;t<=end;++t) { + const auto age=static_cast( + static_cast(time)-t); + const float value=input[(writeIndex+Capacity-age)&(Capacity-1)]; + if(value>best){best=value;peak=t;} + } } return peak; } diff --git a/tests/harmonice_epoch_cpu_profile_test.cpp b/tests/harmonice_epoch_cpu_profile_test.cpp new file mode 100644 index 000000000..3a5ac77ce --- /dev/null +++ b/tests/harmonice_epoch_cpu_profile_test.cpp @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Non-gating, same-runner old/new pitch-epoch workload comparison. +// Timing is synthetic PSOLA-only, not full mixer, driver or vocal naturalness. +#include "../engine/builtin_fx/HarmonyPitchSynchronous.h" +#include +#include +#include +#include +#include +#include +#include +#include +namespace { +using Clock=std::chrono::steady_clock; +constexpr int block=64,warmup=128,measured=1024; +constexpr double pi2=6.283185307179586; +struct Result { + double p50=0.,p95=0.,p99=0.,p999=0.,maximum=0.,checksum=0.; +}; +template +Result measure(double rate,double f0){ + using Impl=armix::harmony::PitchSynchronousVoice<16384,6,Fast>; + Impl shifter; + std::array ring{}; + std::array times{}; + const int frames=(warmup+measured)*block; + std::vector input(static_cast(frames)); + for(int t=0;t(t)]=static_cast( + .17*std::sin(phase)+.06*std::sin(2.*phase+.3)+ + .028*std::sin(3.*phase+.17)); + } + double sum=0.; + for(int b=0;b(t)&16383; + ring[at]=input[static_cast(t)]; + for(int v=0;v<5;++v){ + const auto output=shifter.process( + v,static_cast(t),at,ring, + static_cast(rate/f0),1.2f, + static_cast(rate*.032)+static_cast(v*rate*.004)); + if(!std::isfinite(output.sample)){ + std::cerr<<"PSOLA nonfinite\n";std::exit(2); + } + sum+=static_cast(output.sample); + } + } + const auto after=Clock::now(); + if(b>=warmup) + times[static_cast(b-warmup)]= + std::chrono::duration(after-before).count(); + } + std::sort(times.begin(),times.end()); + const auto at=[&](double p)->double{ + return times[static_cast(std::ceil(p*measured)-1)]; + }; + return {at(.5),at(.95),at(.99),at(.999),times.back(),sum}; +} +void compare(double rate,double f0){ + // Order is constant, same binary and runner; scheduling noise remains. + const auto original=measure(rate,f0); + const auto optimized=measure(rate,f0); + if(std::abs(original.checksum-optimized.checksum)>1.e-9){ + std::cerr<<"SAME INPUT PSOLA reference checksum changed\n"; + std::exit(2); + } + const double deadline=1.e6*block/rate; + std::cout< using armix::harmony::PitchSynchronousVoice; using Shifter=PitchSynchronousVoice<16384,6>; +using ReferenceShifter=PitchSynchronousVoice<16384,6,false>; static void need(bool yes,const char* reason){ if(!yes){std::cerr< static std::vector render(double rate,double f0,double ratio,int block){ - auto p=Shifter{}; + auto p=PitchSynchronousVoice<16384,6,Fast>{}; std::array ring{}; const int n=static_cast(rate*.65); std::vector out(static_cast(n)); @@ -60,11 +62,15 @@ int main(){ {{48000.,82.40689,1.2}},{{96000.,440.,.75}}, {{192000.,880.,1.4}} }}){ - const auto a=render(v[0],v[1],v[2],64); - const auto b=render(v[0],v[1],v[2],257); + const auto a=render(v[0],v[1],v[2],64); + const auto b=render(v[0],v[1],v[2],257); + const auto legacy=render(v[0],v[1],v[2],64); need(a.size()==b.size(),"different source length"); - for(std::size_t i=0;i