From b361e77c2a0cc5f1f0bb38b9d40cbe9bef134e07 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 14 Nov 2026 07:50:23 +0700 Subject: [PATCH 01/14] feat(player): high-overlap KEY quality, warm DSP re-entry and smoothed transposition --- engine/player/PlayerPitchTempoProcessor.h | 91 ++++++++++++++++++++--- 1 file changed, 82 insertions(+), 9 deletions(-) diff --git a/engine/player/PlayerPitchTempoProcessor.h b/engine/player/PlayerPitchTempoProcessor.h index d38455689..d77d79c2c 100644 --- a/engine/player/PlayerPitchTempoProcessor.h +++ b/engine/player/PlayerPitchTempoProcessor.h @@ -2,8 +2,10 @@ #pragma once #include "../../third_party/signalsmith/signalsmith-stretch.h" #include +#include #include #include +#include namespace armix::player { // WORKER THREAD ONLY. Signalsmith DSP state, FFT storage and scratch buffers @@ -12,6 +14,10 @@ namespace armix::player { // Tempo is rate (80% slower / 120% faster) and does not set musical pitch. // Key transpose is independent of tempo. Both stereo channels share one // phase-coherent processing instance rather than divergent mono stretchers. +// Normal playback stays bit-exact and free of FFT load. Re-entering DSP after +// bypass primes the spectral window from a bounded recent-input history; +// otherwise the first KEY gesture after a long neutral passage can start +// with a fresh, empty analysis window and produce an audible dropout. class PlayerPitchTempoProcessor final { public: struct Controls { @@ -37,9 +43,20 @@ class PlayerPitchTempoProcessor final { return false; sampleRate_=sampleRate; controls_=c; - // Cheaper is the documented 100 ms / 40 ms quality/perf preset; - // split computation spreads FFT bursts on the decoder worker. - stretch_.presetCheaper(2,static_cast(sampleRate_),true); + // Prefer the 120 ms / 30 ms default (higher-overlap) preset for + // polyphonic backing music; split FFT work across worker blocks. + // This is NOT called on the ASIO/audio callback. + stretch_.presetDefault(2,static_cast(sampleRate_),true); + const auto maxHistory=static_cast(std::ceil( + stretch_.outputSeekLength(1.2f)))+8; + historyCapacity_=std::max(1,maxHistory); + for(int lane=0;lane<2;++lane) { + history_[lane].assign(static_cast(historyCapacity_),0.f); + prime_[lane].assign(static_cast(historyCapacity_),0.f); + } + historyWrite_=historyCount_=0; + primeOnNextProcess_=false; + currentSemitones_=static_cast(c.keySemitones); stretch_.setTransposeSemitones( static_cast(controls_.keySemitones), 8000.f/static_cast(sampleRate_)); @@ -49,10 +66,16 @@ class PlayerPitchTempoProcessor final { } void setControls(Controls c) { if(!prepared_ || !valid(c) || controls_==c)return; - if(controls_.keySemitones!=c.keySemitones) - stretch_.setTransposeSemitones( - static_cast(c.keySemitones), - 8000.f/static_cast(sampleRate_)); + const bool wasActive=active(); + const bool willBeActive=c.keySemitones!=0 || c.tempoPercent!=100; + // Both transitions occur exclusively on the decoding worker. + if(!wasActive && willBeActive) { + currentSemitones_=0.f; + primeOnNextProcess_=true; + } else if(wasActive && !willBeActive) { + currentSemitones_=0.f; + primeOnNextProcess_=false; + } controls_=c; } Controls applied() const noexcept {return controls_;} @@ -72,12 +95,27 @@ class PlayerPitchTempoProcessor final { std::copy_n(in[0],nOut,out[0]); std::copy_n(in[1],nOut,out[1]); } else { + // Smooth transposition in the worker at block cadence. At 48k + // with 1024-frame blocks the 60 ms time constant spans several + // FFT updates, avoiding a single hard frequency-map jump. + const float target=static_cast(controls_.keySemitones); + if(std::abs(currentSemitones_-target)>0.0001f) { + const float alpha=1.f-std::exp( + -static_cast(nOut)/(0.060f*sampleRate_)); + currentSemitones_+=(target-currentSemitones_)*alpha; + if(std::abs(currentSemitones_-target)<0.0001f) + currentSemitones_=target; + } + stretch_.setTransposeSemitones(currentSemitones_, + 8000.f/static_cast(sampleRate_)); + if(primeOnNextProcess_)primeFromRecentInput(); stretch_.process(in,nInput,out,nOut); } for(int c=0;c<2;++c) for(int i=0;i(std::ceil(stretch_.outputSeekLength( + static_cast(controls_.tempoPercent)/100.f))), + 1,historyCapacity_); + const int available=std::min(historyCount_,count); + const int missing=count-available; + for(int lane=0;lane<2;++lane) { + std::fill_n(prime_[lane].data(),missing,0.f); + for(int i=0;i(missing+i)]= + history_[lane][static_cast(pos)]; + } + } + const float* const previous[]={prime_[0].data(),prime_[1].data()}; + stretch_.outputSeek(previous,count); + primeOnNextProcess_=false; + } + void rememberInput(const float* const* in,int frames) noexcept { + // No separate DSP warm-up during a neutral song: bounded two-lane + // history lets the next active block start without a cold FFT window. + for(int i=0;i(historyWrite_)]=in[0][i]; + history_[1][static_cast(historyWrite_)]=in[1][i]; + if(++historyWrite_==historyCapacity_)historyWrite_=0; + if(historyCount_ stretch_{23}; - int sampleRate_=0; + std::array,2> history_,prime_; + int sampleRate_=0,historyCapacity_=0,historyWrite_=0,historyCount_=0; Controls controls_{}; double fractionalInput_=0; - bool prepared_=false; + float currentSemitones_=0.f; + bool prepared_=false,primeOnNextProcess_=false; }; } // namespace armix::player From 7d2898e1a8323ca69b01941500dc923ee527c687 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 14 Nov 2026 07:51:31 +0700 Subject: [PATCH 02/14] test(player): hot key/tempo changes across 44.1, 48 and 96 kHz with silence and stereo guard --- tests/player_pitch_tempo_test.cpp | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/player_pitch_tempo_test.cpp b/tests/player_pitch_tempo_test.cpp index 661e498b1..0ae3c8506 100644 --- a/tests/player_pitch_tempo_test.cpp +++ b/tests/player_pitch_tempo_test.cpp @@ -60,6 +60,61 @@ Evidence test(Processor::Controls c) { assert(correlation<-.9); // phase coherence of linked stereo shift return {hz,static_cast(outL.size())/frames,correlation}; } +// Simulate a real performer changing the key after a long dry passage, then +// changing tempo, bypassing the DSP and re-entering at a different pitch. +// Previous unit coverage only checked constant controls on synthetic sine. +void testLiveControlTransitions(int sampleRate) { + Processor processor; + constexpr int quantum=512; + assert(processor.prepare(sampleRate,{0,100})); + std::array sourceL{},sourceR{}; + std::array resultL{},resultR{}; + std::int64_t sourceFrame=0; + int longestSilentRun=0,silentRun=0; + double recentEnergy=0; + for(int block=0;block<192;++block) { + if(block==32)processor.setControls({4,100}); // bypass -> transposed + if(block==64)processor.setControls({4,88}); // independent tempo + if(block==96)processor.setControls({0,100}); // DSP -> bit-exact dry + if(block==128)processor.setControls({-3,112}); // bypass -> DSP again + if(block==160)processor.setControls({-3,100}); // active tempo change + const int nInput=processor.nextInputFrames(quantum); + assert(nInput>0 && nInput<=static_cast(sourceL.size())); + for(int i=0;i(sourceFrame+i)/sampleRate; + const float sample=static_cast( + 0.36*std::sin(2*pi*220*time)+ + 0.14*std::sin(2*pi*660*time)+ + 0.07*std::sin(2*pi*1760*time)); + sourceL[i]=sample; + sourceR[i]=-sample; + } + sourceFrame+=nInput; + const float* const inputs[]={sourceL.data(),sourceR.data()}; + float* const outputs[]={resultL.data(),resultR.data()}; + assert(processor.process(inputs,nInput,outputs,quantum)); + for(int i=0;i12) { + if(std::abs(x)<0.00001f) { + ++silentRun; + longestSilentRun=std::max(longestSilentRun,silentRun); + } else silentRun=0; + } + if(block==47 || block==79 || block==111 || + block==143 || block==175)recentEnergy+=x*x; + } + } + // A cold 120ms FFT analysis window on every dry->DSP transition would + // fail this, even though a constant-pitch frequency test still passes. + assert(longestSilentRun5.0); + assert(!processor.process(nullptr,0,nullptr,512)); + std::printf("M2 hot-gesture: %d Hz, maximum near-silence %d frames\\n", + sampleRate,longestSilentRun); +} } int main() { using P=Processor; @@ -77,6 +132,9 @@ int main() { assert(std::abs(neutral.hz-440)<8); assert(up.hz>neutral.hz && down.hz1.24 && down.ratio<.85); + testLiveControlTransitions(44100); + testLiveControlTransitions(48000); + testLiveControlTransitions(96000); std::printf("M2: pass neutral %.1fHz, +3st/80%% %.1fHz, -2st/120%% %.1fHz; stereo=%.3f\n", neutral.hz,up.hz,down.hz,up.correlation); } From 5428798440bd947676392df6a5df5b73d0913e9e Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 14 Nov 2026 07:52:38 +0700 Subject: [PATCH 03/14] perf(player): bulk-copy stereo prime history and avoid steady KEY frequency-map rebuild --- engine/player/PlayerPitchTempoProcessor.h | 31 +++++++++++++++-------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/engine/player/PlayerPitchTempoProcessor.h b/engine/player/PlayerPitchTempoProcessor.h index d77d79c2c..a5638aeea 100644 --- a/engine/player/PlayerPitchTempoProcessor.h +++ b/engine/player/PlayerPitchTempoProcessor.h @@ -56,7 +56,7 @@ class PlayerPitchTempoProcessor final { } historyWrite_=historyCount_=0; primeOnNextProcess_=false; - currentSemitones_=static_cast(c.keySemitones); + currentSemitones_=mappedSemitones_=static_cast(c.keySemitones); stretch_.setTransposeSemitones( static_cast(controls_.keySemitones), 8000.f/static_cast(sampleRate_)); @@ -106,8 +106,12 @@ class PlayerPitchTempoProcessor final { if(std::abs(currentSemitones_-target)<0.0001f) currentSemitones_=target; } - stretch_.setTransposeSemitones(currentSemitones_, - 8000.f/static_cast(sampleRate_)); + // Avoid repeated pow/sqrt frequency-map rebuilds at steady KEY. + if(mappedSemitones_!=currentSemitones_) { + stretch_.setTransposeSemitones( + currentSemitones_,8000.f/static_cast(sampleRate_)); + mappedSemitones_=currentSemitones_; + } if(primeOnNextProcess_)primeFromRecentInput(); stretch_.process(in,nInput,out,nOut); } @@ -150,21 +154,26 @@ class PlayerPitchTempoProcessor final { primeOnNextProcess_=false; } void rememberInput(const float* const* in,int frames) noexcept { - // No separate DSP warm-up during a neutral song: bounded two-lane - // history lets the next active block start without a cold FFT window. - for(int i=0;i(historyWrite_)]=in[0][i]; - history_[1][static_cast(historyWrite_)]=in[1][i]; - if(++historyWrite_==historyCapacity_)historyWrite_=0; - if(historyCount_ stretch_{23}; std::array,2> history_,prime_; int sampleRate_=0,historyCapacity_=0,historyWrite_=0,historyCount_=0; Controls controls_{}; double fractionalInput_=0; - float currentSemitones_=0.f; + float currentSemitones_=0.f,mappedSemitones_=0.f; bool prepared_=false,primeOnNextProcess_=false; }; } // namespace armix::player From 4b18d768332f340ffc019e8e0b799f34b91401b6 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 14 Nov 2026 07:53:57 +0700 Subject: [PATCH 04/14] ci(player): scoped Linux pitch DSP regression without installer or Windows build --- .github/workflows/player-key-dsp-fast.yml | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .github/workflows/player-key-dsp-fast.yml diff --git a/.github/workflows/player-key-dsp-fast.yml b/.github/workflows/player-key-dsp-fast.yml new file mode 100644 index 000000000..4707bc0c7 --- /dev/null +++ b/.github/workflows/player-key-dsp-fast.yml @@ -0,0 +1,30 @@ +name: ArSon Player KEY DSP (focused fast native test) +on: + pull_request: + paths: + - 'engine/player/PlayerPitchTempoProcessor.h' + - 'tests/player_pitch_tempo_test.cpp' + - '.github/workflows/player-key-dsp-fast.yml' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: arson-player-key-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + native-key: + name: Native stereo KEY/TEMPO and hot-gesture PCM (no JUCE/installer) + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v4 + - name: Compile the standalone real DSP against vendored Signalsmith + run: | + g++ -std=c++20 -O2 -DNDEBUG -pthread \ + tests/player_pitch_tempo_test.cpp \ + -o "$RUNNER_TEMP/arson-player-key-test" + - name: Run deterministic stereo, tempo, bypass and 44.1/48/96k gestures + run: "$RUNNER_TEMP/arson-player-key-test" From 5d5c9833eb9b9aaf1b4df1947c07e2bd5a36e41c Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 14 Nov 2026 07:56:57 +0700 Subject: [PATCH 05/14] test(player): report non-gating worker DSP processing cost per audio duration --- tests/player_pitch_tempo_test.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/player_pitch_tempo_test.cpp b/tests/player_pitch_tempo_test.cpp index 0ae3c8506..2dd5de002 100644 --- a/tests/player_pitch_tempo_test.cpp +++ b/tests/player_pitch_tempo_test.cpp @@ -72,6 +72,7 @@ void testLiveControlTransitions(int sampleRate) { std::int64_t sourceFrame=0; int longestSilentRun=0,silentRun=0; double recentEnergy=0; + double processTimeMs=0; for(int block=0;block<192;++block) { if(block==32)processor.setControls({4,100}); // bypass -> transposed if(block==64)processor.setControls({4,88}); // independent tempo @@ -92,7 +93,10 @@ void testLiveControlTransitions(int sampleRate) { sourceFrame+=nInput; const float* const inputs[]={sourceL.data(),sourceR.data()}; float* const outputs[]={resultL.data(),resultR.data()}; + const auto started=std::chrono::steady_clock::now(); assert(processor.process(inputs,nInput,outputs,quantum)); + processTimeMs+=std::chrono::duration( + std::chrono::steady_clock::now()-started).count(); for(int i=0;i5.0); assert(!processor.process(nullptr,0,nullptr,512)); - std::printf("M2 hot-gesture: %d Hz, maximum near-silence %d frames\\n", - sampleRate,longestSilentRun); + const double audioTimeMs=192.0*quantum*1000.0/sampleRate; + std::printf("M2 hot-gesture: %d Hz, silence %d frames, DSP %.1fms / %.1fms audio (CI reference)\n", + sampleRate,longestSilentRun,processTimeMs,audioTimeMs); } } int main() { From 76b8912ed3a45663dd6f6d61f12ebc357b72741a Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 14 Nov 2026 08:04:10 +0700 Subject: [PATCH 06/14] fix(player): expose analysis latency for complete song-tail drain and optimize warm seek --- engine/player/PlayerPitchTempoProcessor.h | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/engine/player/PlayerPitchTempoProcessor.h b/engine/player/PlayerPitchTempoProcessor.h index a5638aeea..71058b900 100644 --- a/engine/player/PlayerPitchTempoProcessor.h +++ b/engine/player/PlayerPitchTempoProcessor.h @@ -122,6 +122,12 @@ class PlayerPitchTempoProcessor final { rememberInput(in,nInput); return true; } + // Signalsmith requires BOTH an analysis-input drain and a synthesis + // output flush at EOF. Omitting the input side loses the last FFT window + // of a song despite reporting the complete decoded source length. + int inputLatency() const noexcept { + return prepared_&&active()?stretch_.inputLatency():0; + } int outputLatency() const noexcept { return prepared_&&active()?stretch_.outputLatency():0; } @@ -140,14 +146,17 @@ class PlayerPitchTempoProcessor final { 1,historyCapacity_); const int available=std::min(historyCount_,count); const int missing=count-available; + const int historyStart=(historyWrite_+historyCapacity_-available) + %historyCapacity_; + const int first=std::min(available,historyCapacity_-historyStart); for(int lane=0;lane<2;++lane) { - std::fill_n(prime_[lane].data(),missing,0.f); - for(int i=0;i(missing+i)]= - history_[lane][static_cast(pos)]; - } + auto* dest=prime_[lane].data(); + std::fill_n(dest,missing,0.f); + // Contiguous circular-buffer slices: no per-sample modulo or + // branch in the expensive re-entry setup. + std::copy_n(history_[lane].data()+historyStart,first,dest+missing); + std::copy_n(history_[lane].data(),available-first, + dest+missing+first); } const float* const previous[]={prime_[0].data(),prime_[1].data()}; stretch_.outputSeek(previous,count); From d9a48a8b8b05514e74b80c574438516b2fd7f0e0 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 14 Nov 2026 08:04:58 +0700 Subject: [PATCH 07/14] fix(player): preserve final song audio by draining Signalsmith analysis window before EOF flush --- engine/player/NativePlayerDeck.h | 48 ++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/engine/player/NativePlayerDeck.h b/engine/player/NativePlayerDeck.h index ea2c796b6..4a66b495e 100644 --- a/engine/player/NativePlayerDeck.h +++ b/engine/player/NativePlayerDeck.h @@ -288,8 +288,14 @@ class NativePlayerDeck final { juce::AudioBuffer input(2,1536),output(2,1024); std::uint64_t consumed=0,written=0; const auto estimateTotal=[&]() -> std::uint64_t { + // Signalsmith's delay spans BOTH its input-side analysis + // window and output-side synthesis window. Count the final + // zero-input drain or the displayed duration hides music + // still pending in the spectral overlap at EOF. return written+static_cast(std::ceil( (sourceTotal-consumed)*100.0/controls.tempoPercent)) + +static_cast(std::ceil( + pitch.inputLatency()*100.0/controls.tempoPercent)) +static_cast(pitch.outputLatency()); }; length_.store(estimateTotal(),std::memory_order_release); @@ -355,12 +361,48 @@ class NativePlayerDeck final { state_.store(State::ready,std::memory_order_release); } } - // Drain the final spectral overlap OFF realtime. Without this, - // transposed music loses its last FFT window at EOF. + // Complete the spectral analysis window before reading the + // synthesis tail. Signalsmith explicitly requires feeding + // inputLatency() frames of zero PCM at EOF; flush() alone only + // drains the synthesis side and can truncate the song's outro. + // The same bounded 1024-frame buffers and worker->ring transfer + // are reused. No disk/FFT/allocations run on ASIO callback. if(!failed && !obsolete(version) && consumed==sourceTotal && pitch.active()) { + int analysisToDrain=pitch.inputLatency(); + if(analysisToDrain<0 || analysisToDrain>deviceHz) + failed=true; + while(!failed && !obsolete(version) && analysisToDrain>0) { + if(ring_.bufferedFrames()>highWater) { + std::this_thread::sleep_for(std::chrono::milliseconds(3)); + continue; + } + const int nOutput=std::min(1024,std::max(1, + static_cast(std::ceil( + analysisToDrain*100.0/controls.tempoPercent)))); + int nInput=pitch.nextInputFrames(nOutput); + nInput=std::clamp(nInput,1,analysisToDrain); + input.clear();output.clear(); + const float* const silencePcm[]={ + input.getReadPointer(0),input.getReadPointer(1)}; + float* const tailPcm[]={ + output.getWritePointer(0),output.getWritePointer(1)}; + if(!pitch.process(silencePcm,nInput,tailPcm,nOutput)) { + failed=true;break; + } + const auto transferred=ring_.write( + tailPcm[0],tailPcm[1], + static_cast(nOutput)); + if(transferred!=static_cast(nOutput)) { + failed=true;break; + } + analysisToDrain-=nInput; + written+=transferred; + } + // The output buffer already contains extra analysis-side + // audio. The final flush below handles ONLY synthesis delay. const int tail=pitch.outputLatency(); - if(tail>0 && tail0 && tail ending(2,tail); float* const ends[]={ending.getWritePointer(0), ending.getWritePointer(1)}; From a7682bf868ea4f6d3850c77183bb6c8e241f5af5 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 14 Nov 2026 08:05:37 +0700 Subject: [PATCH 08/14] test(player): assert song-outro spectral tail across KEY/TEMPO, sample rates and report gesture seams --- tests/player_pitch_tempo_test.cpp | 90 ++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/tests/player_pitch_tempo_test.cpp b/tests/player_pitch_tempo_test.cpp index 2dd5de002..4c0adb18e 100644 --- a/tests/player_pitch_tempo_test.cpp +++ b/tests/player_pitch_tempo_test.cpp @@ -73,6 +73,8 @@ void testLiveControlTransitions(int sampleRate) { int longestSilentRun=0,silentRun=0; double recentEnergy=0; double processTimeMs=0; + float previousOutput=0.f; + float worstControlSeam=0.f; for(int block=0;block<192;++block) { if(block==32)processor.setControls({4,100}); // bypass -> transposed if(block==64)processor.setControls({4,88}); // independent tempo @@ -97,6 +99,11 @@ void testLiveControlTransitions(int sampleRate) { assert(processor.process(inputs,nInput,outputs,quantum)); processTimeMs+=std::chrono::duration( std::chrono::steady_clock::now()-started).count(); + if(block==32 || block==64 || block==96 || + block==128 || block==160) + worstControlSeam=std::max(worstControlSeam, + std::abs(resultL[0]-previousOutput)); + previousOutput=resultL[quantum-1]; for(int i=0;i5.0); assert(!processor.process(nullptr,0,nullptr,512)); const double audioTimeMs=192.0*quantum*1000.0/sampleRate; - std::printf("M2 hot-gesture: %d Hz, silence %d frames, DSP %.1fms / %.1fms audio (CI reference)\n", - sampleRate,longestSilentRun,processTimeMs,audioTimeMs); + std::printf("M2 hot-gesture: %d Hz, silence %d frames, seam %.3f, DSP %.1fms / %.1fms audio (CI reference)\n", + sampleRate,longestSilentRun,worstControlSeam,processTimeMs,audioTimeMs); +} +// Offline fixed-file QA: a spectral stretcher must receive inputLatency() +// zero-input frames and then flush outputLatency() frames. Verifies song +// ending isn't lost after pitch-shifting or time-stretching a full mix. +void testEndOfSongTail(int sampleRate,Processor::Controls controls) { + Processor processor; + assert(processor.prepare(sampleRate,controls)); + const int totalInput=sampleRate/3; + constexpr int quantum=512; + std::array sourceL{},sourceR{}; + std::array outputL{},outputR{}; + int fed=0; + std::int64_t produced=0; + auto energy=[](const float* left,const float* right,int count) { + double sum=0; + for(int i=0;i0 && nInput<=static_cast(sourceL.size())); + if(nInput>totalInput-fed) { + nOutput=std::min(quantum,static_cast(std::ceil( + (totalInput-fed)*100.0/controls.tempoPercent))); + nInput=totalInput-fed; + } + for(int i=0;i(0.35*std::sin(330*t) + +0.11*std::sin(790*t) + +((fed+i)>totalInput-sampleRate/16 + ?0.2*std::sin(1930*t):0.0)); + sourceL[i]=v;sourceR[i]=-v; + } + const float* const input[]={sourceL.data(),sourceR.data()}; + float* const output[]={outputL.data(),outputR.data()}; + assert(processor.process(input,nInput,output,nOutput)); + energy(outputL.data(),outputR.data(),nOutput); + fed+=nInput;produced+=nOutput; + } + const int analysisDelay=processor.inputLatency(); + const int synthesisDelay=processor.outputLatency(); + assert(analysisDelay>0 && synthesisDelay>0); + assert(analysisDelay0) { + const int nOutput=std::min(quantum,std::max(1, + static_cast(std::ceil( + remaining*100.0/controls.tempoPercent)))); + int nInput=processor.nextInputFrames(nOutput); + nInput=std::clamp(nInput,1,remaining); + sourceL.fill(0.f);sourceR.fill(0.f); + const float* const input[]={sourceL.data(),sourceR.data()}; + float* const output[]={outputL.data(),outputR.data()}; + assert(processor.process(input,nInput,output,nOutput)); + recoveredTailEnergy+=energy(outputL.data(),outputR.data(),nOutput); + remaining-=nInput;produced+=nOutput; + } + std::vector lastL(synthesisDelay),lastR(synthesisDelay); + float* const ends[]={lastL.data(),lastR.data()}; + processor.flush(ends,synthesisDelay); + recoveredTailEnergy+=energy(ends[0],ends[1],synthesisDelay); + produced+=synthesisDelay; + const double expected=totalInput*100.0/controls.tempoPercent+ + analysisDelay*100.0/controls.tempoPercent+synthesisDelay; + assert(std::abs(produced-expected)0.01); // last tones survive the ending + std::printf("M2 EOF: %d Hz, KEY %+d, tempo %d%%, recovered %.2f energy, delay %d+%d frames\n", + sampleRate,controls.keySemitones,controls.tempoPercent, + recoveredTailEnergy,analysisDelay,synthesisDelay); } } int main() { @@ -140,6 +223,9 @@ int main() { testLiveControlTransitions(44100); testLiveControlTransitions(48000); testLiveControlTransitions(96000); + testEndOfSongTail(44100,{3,80}); + testEndOfSongTail(48000,{-4,120}); + testEndOfSongTail(96000,{4,100}); std::printf("M2: pass neutral %.1fHz, +3st/80%% %.1fHz, -2st/120%% %.1fHz; stereo=%.3f\n", neutral.hz,up.hz,down.hz,up.correlation); } From fb97163671a2a6a5a6d373fabd36ca3d9befea70 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 14 Nov 2026 08:07:37 +0700 Subject: [PATCH 09/14] fix(player): prevent KEY/TEMPO toggle clicks with bounded worker-only 5ms seam correction --- engine/player/PlayerPitchTempoProcessor.h | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/engine/player/PlayerPitchTempoProcessor.h b/engine/player/PlayerPitchTempoProcessor.h index 71058b900..71683afd5 100644 --- a/engine/player/PlayerPitchTempoProcessor.h +++ b/engine/player/PlayerPitchTempoProcessor.h @@ -56,6 +56,9 @@ class PlayerPitchTempoProcessor final { } historyWrite_=historyCount_=0; primeOnNextProcess_=false; + haveLastOutput_=seamPending_=false; + seamLength_=seamPosition_=0; + seamCorrection_={0.f,0.f}; currentSemitones_=mappedSemitones_=static_cast(c.keySemitones); stretch_.setTransposeSemitones( static_cast(controls_.keySemitones), @@ -77,6 +80,9 @@ class PlayerPitchTempoProcessor final { primeOnNextProcess_=false; } controls_=c; + // Mark precisely one boundary. No continuously running smoother: + // the expensive FFT always remains in the decoder worker. + seamPending_=true; } Controls applied() const noexcept {return controls_;} bool active() const noexcept { @@ -115,10 +121,39 @@ class PlayerPitchTempoProcessor final { if(primeOnNextProcess_)primeFromRecentInput(); stretch_.process(in,nInput,out,nOut); } + // A spectral state switch or bypass can move the PCM waveform by + // several tenths of full scale at ONE sample, creating a broadband + // click even when pitch, tempo and stereo phase tests all pass. + // Correct only the *discontinuity offset* with a 5 ms C1-continuous + // smoothstep envelope. This is worker-only, zero heap, and costs + // NOTHING between control gestures. No destructive limiter/clamp. + if(seamPending_) { + seamPending_=false; + if(haveLastOutput_) { + seamLength_=std::max(8,sampleRate_/200); + seamPosition_=0; + seamCorrection_[0]=lastOutput_[0]-out[0][0]; + seamCorrection_[1]=lastOutput_[1]-out[1][0]; + } + } + if(seamPosition_(seamPosition_+i)/ + static_cast(seamLength_-1); + const float smooth=1.f-t*t*(3.f-2.f*t); + out[0][i]+=seamCorrection_[0]*smooth; + out[1][i]+=seamCorrection_[1]*smooth; + } + seamPosition_+=n; + } for(int c=0;c<2;++c) for(int i=0;i lastOutput_{},seamCorrection_{}; + int seamLength_=0,seamPosition_=0; bool prepared_=false,primeOnNextProcess_=false; + bool haveLastOutput_=false,seamPending_=false; }; } // namespace armix::player From eacd1ae4e5dc18283f944b24fb56ea1ba5d49f64 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 14 Nov 2026 08:07:49 +0700 Subject: [PATCH 10/14] test(player): gate control-boundary discontinuity and prove bit-exact neutral bypass --- tests/player_pitch_tempo_test.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/player_pitch_tempo_test.cpp b/tests/player_pitch_tempo_test.cpp index 4c0adb18e..8d7e902dd 100644 --- a/tests/player_pitch_tempo_test.cpp +++ b/tests/player_pitch_tempo_test.cpp @@ -121,6 +121,9 @@ void testLiveControlTransitions(int sampleRate) { // A cold 120ms FFT analysis window on every dry->DSP transition would // fail this, even though a constant-pitch frequency test still passes. assert(longestSilentRun5.0); assert(!processor.process(nullptr,0,nullptr,512)); const double audioTimeMs=192.0*quantum*1000.0/sampleRate; @@ -130,6 +133,23 @@ void testLiveControlTransitions(int sampleRate) { // Offline fixed-file QA: a spectral stretcher must receive inputLatency() // zero-input frames and then flush outputLatency() frames. Verifies song // ending isn't lost after pitch-shifting or time-stretching a full mix. +void testNeutralBitExact() { + Processor processor; + assert(processor.prepare(48000,{0,100})); + constexpr int n=257; + std::array inL{},inR{},outL{},outR{}; + for(int i=0;i(.71*std::sin(i*.19)); + inR[i]=static_cast(.31*std::cos(i*.23)); + } + const float* const in[]={inL.data(),inR.data()}; + float* const out[]={outL.data(),outR.data()}; + assert(processor.process(in,n,out,n)); + for(int i=0;ineutral.hz && down.hz1.24 && down.ratio<.85); + testNeutralBitExact(); testLiveControlTransitions(44100); testLiveControlTransitions(48000); testLiveControlTransitions(96000); From 73d4f8e026feb07a2aade134353c4759f17b79e1 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 14 Nov 2026 08:09:43 +0700 Subject: [PATCH 11/14] feat(player): slew independent tempo gestures over 80ms with fractional sample accounting --- engine/player/PlayerPitchTempoProcessor.h | 33 ++++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/engine/player/PlayerPitchTempoProcessor.h b/engine/player/PlayerPitchTempoProcessor.h index 71683afd5..3a5a7cc99 100644 --- a/engine/player/PlayerPitchTempoProcessor.h +++ b/engine/player/PlayerPitchTempoProcessor.h @@ -64,6 +64,7 @@ class PlayerPitchTempoProcessor final { static_cast(controls_.keySemitones), 8000.f/static_cast(sampleRate_)); fractionalInput_=0; + smoothedTempoPercent_=static_cast(c.tempoPercent); prepared_=true; return true; } @@ -78,7 +79,10 @@ class PlayerPitchTempoProcessor final { } else if(wasActive && !willBeActive) { currentSemitones_=0.f; primeOnNextProcess_=false; + smoothedTempoPercent_=100.; } + if(!wasActive && willBeActive) + smoothedTempoPercent_=100.; // ramp into tempo from neutral controls_=c; // Mark precisely one boundary. No continuously running smoother: // the expensive FFT always remains in the decoder worker. @@ -88,8 +92,28 @@ class PlayerPitchTempoProcessor final { bool active() const noexcept { return controls_.keySemitones!=0 || controls_.tempoPercent!=100; } + double smoothedTempoPercent() const noexcept { + return smoothedTempoPercent_; + } int nextInputFrames(int outputFrames) noexcept { - return inputFramesForOutput(outputFrames,controls_,fractionalInput_); + if(!prepared_ || outputFrames<=0)return 0; + if(active()) { + // Tempo rate follows an 80 ms block-independent one-pole + // envelope, so changing speed does not instantaneously jump + // the time map. The fractional accumulator still ensures + // long-run sample-accurate average consumption of decoder PCM. + const double alpha=1.-std::exp( + -static_cast(outputFrames)/(sampleRate_*0.080)); + smoothedTempoPercent_+= + (controls_.tempoPercent-smoothedTempoPercent_)*alpha; + if(std::abs(controls_.tempoPercent-smoothedTempoPercent_)<1e-7) + smoothedTempoPercent_=controls_.tempoPercent; + } else smoothedTempoPercent_=100.; + const double requested=outputFrames*smoothedTempoPercent_/100. + +fractionalInput_; + const int frames=static_cast(std::floor(requested)); + fractionalInput_=requested-frames; + return frames; } // in and out contain TWO separate PCM lanes, each sized for the supplied // count. No file IO here. Process is allowed to use heap on this WORKER. @@ -168,7 +192,8 @@ class PlayerPitchTempoProcessor final { } void flush(float* const* output,int frames) { if(prepared_&&active() && frames>0) - stretch_.flush(output,frames,controls_.tempoPercent/100.f); + stretch_.flush(output,frames, + static_cast(smoothedTempoPercent_/100.)); } private: void primeFromRecentInput() { @@ -177,7 +202,7 @@ class PlayerPitchTempoProcessor final { // the required pre-roll output. All buffers are allocated in prepare(). const int count=std::clamp( static_cast(std::ceil(stretch_.outputSeekLength( - static_cast(controls_.tempoPercent)/100.f))), + static_cast(smoothedTempoPercent_/100.)))), 1,historyCapacity_); const int available=std::min(historyCount_,count); const int missing=count-available; @@ -216,7 +241,7 @@ class PlayerPitchTempoProcessor final { std::array,2> history_,prime_; int sampleRate_=0,historyCapacity_=0,historyWrite_=0,historyCount_=0; Controls controls_{}; - double fractionalInput_=0; + double fractionalInput_=0,smoothedTempoPercent_=100.; float currentSemitones_=0.f,mappedSemitones_=0.f; std::array lastOutput_{},seamCorrection_{}; int seamLength_=0,seamPosition_=0; From 5da313dd305ea760bc687df0929d9b0c2c0f6264 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 14 Nov 2026 08:10:01 +0700 Subject: [PATCH 12/14] test(player): verify monotonic tempo slew and exact neutral recovery at 44.1/48/96k --- tests/player_pitch_tempo_test.cpp | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/player_pitch_tempo_test.cpp b/tests/player_pitch_tempo_test.cpp index 8d7e902dd..4a46d0013 100644 --- a/tests/player_pitch_tempo_test.cpp +++ b/tests/player_pitch_tempo_test.cpp @@ -133,6 +133,38 @@ void testLiveControlTransitions(int sampleRate) { // Offline fixed-file QA: a spectral stretcher must receive inputLatency() // zero-input frames and then flush outputLatency() frames. Verifies song // ending isn't lost after pitch-shifting or time-stretching a full mix. +// Tempo edits should use a bounded, sample-rate-independent transition +// instead of hard-switching source consumption from 80% to 120% in one block. +void testSmoothTempoEdits(int sampleRate) { + Processor processor; + assert(processor.prepare(sampleRate,{0,100})); + constexpr int quantum=512; + processor.setControls({0,80}); + double prior=100.; + for(int block=0;block<48;++block) { + const int read=processor.nextInputFrames(quantum); + assert(read>0 && read<=quantum); + const double current=processor.smoothedTempoPercent(); + assert(current<=prior+1e-7 && current>=80.); + if(block==0)assert(current>80. && current<100.); + prior=current; + } + assert(std::abs(prior-80.)<0.05); + processor.setControls({0,120}); + prior=80.; + for(int block=0;block<48;++block) { + const int read=processor.nextInputFrames(quantum); + assert(read>0 && read<=static_cast(quantum*1.2)+1); + const double current=processor.smoothedTempoPercent(); + assert(current>=prior-1e-7 && current<=120.); + if(block==0)assert(current>80. && current<120.); + prior=current; + } + assert(std::abs(prior-120.)<0.05); + processor.setControls({0,100}); + assert(processor.nextInputFrames(quantum)==quantum); + assert(processor.smoothedTempoPercent()==100.); +} void testNeutralBitExact() { Processor processor; assert(processor.prepare(48000,{0,100})); @@ -241,6 +273,9 @@ int main() { assert(up.hz>neutral.hz && down.hz1.24 && down.ratio<.85); testNeutralBitExact(); + testSmoothTempoEdits(44100); + testSmoothTempoEdits(48000); + testSmoothTempoEdits(96000); testLiveControlTransitions(44100); testLiveControlTransitions(48000); testLiveControlTransitions(96000); From 193388045ca5e43f335b7eea9926d88d03941a04 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 14 Nov 2026 08:10:14 +0700 Subject: [PATCH 13/14] test(player): normalize tempo-convergence duration across device sample rates --- tests/player_pitch_tempo_test.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/player_pitch_tempo_test.cpp b/tests/player_pitch_tempo_test.cpp index 4a46d0013..5ff02f183 100644 --- a/tests/player_pitch_tempo_test.cpp +++ b/tests/player_pitch_tempo_test.cpp @@ -139,9 +139,11 @@ void testSmoothTempoEdits(int sampleRate) { Processor processor; assert(processor.prepare(sampleRate,{0,100})); constexpr int quantum=512; + const int blocks=static_cast(std::ceil( + 0.8*sampleRate/quantum)); // same 800 ms across every sample rate processor.setControls({0,80}); double prior=100.; - for(int block=0;block<48;++block) { + for(int block=0;block0 && read<=quantum); const double current=processor.smoothedTempoPercent(); @@ -152,7 +154,7 @@ void testSmoothTempoEdits(int sampleRate) { assert(std::abs(prior-80.)<0.05); processor.setControls({0,120}); prior=80.; - for(int block=0;block<48;++block) { + for(int block=0;block0 && read<=static_cast(quantum*1.2)+1); const double current=processor.smoothedTempoPercent(); From 0d40354238084f19d0d40851873739a912fc3d24 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Sat, 14 Nov 2026 08:12:55 +0700 Subject: [PATCH 14/14] ci(player): non-gating same-runner reference profile for cheaper FFT preset --- .github/workflows/player-key-dsp-fast.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/.github/workflows/player-key-dsp-fast.yml b/.github/workflows/player-key-dsp-fast.yml index 4707bc0c7..c3efe6b1b 100644 --- a/.github/workflows/player-key-dsp-fast.yml +++ b/.github/workflows/player-key-dsp-fast.yml @@ -28,3 +28,23 @@ jobs: -o "$RUNNER_TEMP/arson-player-key-test" - name: Run deterministic stereo, tempo, bypass and 44.1/48/96k gestures run: "$RUNNER_TEMP/arson-player-key-test" + + # Reference comparison isolates only the FFT preset on the SAME + # ephemeral runner. It cannot qualify perceptual sound or Windows ASIO. + # Never block the real DSP test on a deliberately cheaper alternative. + - name: Compare cheaper preset CPU on identical synthetic audio (non-gating) + continue-on-error: true + run: | + python3 - <<'PY' + from pathlib import Path + p=Path('engine/player/PlayerPitchTempoProcessor.h') + src=p.read_text(encoding='utf-8') + needle='stretch_.presetDefault(2,' + assert src.count(needle)==1, 'expected exact source preset' + p.write_text(src.replace(needle,'stretch_.presetCheaper(2,'),encoding='utf-8') + PY + g++ -std=c++20 -O2 -DNDEBUG -pthread \ + tests/player_pitch_tempo_test.cpp \ + -o "$RUNNER_TEMP/arson-player-cheaper-reference" + echo "REFERENCE_CHEAPER_PRESET (same runner, not deployed):" + "$RUNNER_TEMP/arson-player-cheaper-reference"