diff --git a/.github/workflows/player-key-dsp-fast.yml b/.github/workflows/player-key-dsp-fast.yml new file mode 100644 index 000000000..c3efe6b1b --- /dev/null +++ b/.github/workflows/player-key-dsp-fast.yml @@ -0,0 +1,50 @@ +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" + + # 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" 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)}; diff --git a/engine/player/PlayerPitchTempoProcessor.h b/engine/player/PlayerPitchTempoProcessor.h index d38455689..3a5a7cc99 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,30 +43,77 @@ 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; + haveLastOutput_=seamPending_=false; + seamLength_=seamPosition_=0; + seamCorrection_={0.f,0.f}; + currentSemitones_=mappedSemitones_=static_cast(c.keySemitones); stretch_.setTransposeSemitones( static_cast(controls_.keySemitones), 8000.f/static_cast(sampleRate_)); fractionalInput_=0; + smoothedTempoPercent_=static_cast(c.tempoPercent); prepared_=true; return true; } 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; + 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. + seamPending_=true; } Controls applied() const noexcept {return controls_;} 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. @@ -72,26 +125,127 @@ 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; + } + // 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); } + // 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;i0) - stretch_.flush(output,frames,controls_.tempoPercent/100.f); + stretch_.flush(output,frames, + static_cast(smoothedTempoPercent_/100.)); } private: + void primeFromRecentInput() { + // Signalsmith outputSeek() aligns the next processed PCM with the + // current source position. Unlike a plain reset/seek, it synthesises + // the required pre-roll output. All buffers are allocated in prepare(). + const int count=std::clamp( + static_cast(std::ceil(stretch_.outputSeekLength( + static_cast(smoothedTempoPercent_/100.)))), + 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) { + 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); + primeOnNextProcess_=false; + } + void rememberInput(const float* const* in,int frames) noexcept { + // Two bounded stereo memcpy-style copies rather than a modulo and + // branch for every decoded sample; never allocates in process(). + if(frames<=0 || historyCapacity_<=0)return; + const int keep=std::min(frames,historyCapacity_); + const int start=frames-keep; + const int first=std::min(keep,historyCapacity_-historyWrite_); + for(int lane=0;lane<2;++lane) { + auto* dest=history_[lane].data(); + std::copy_n(in[lane]+start,first,dest+historyWrite_); + std::copy_n(in[lane]+start+first,keep-first,dest); + } + historyWrite_=(historyWrite_+keep)%historyCapacity_; + historyCount_=std::min(historyCapacity_,historyCount_+keep); + } signalsmith::stretch::SignalsmithStretch 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; + double fractionalInput_=0,smoothedTempoPercent_=100.; + float currentSemitones_=0.f,mappedSemitones_=0.f; + std::array lastOutput_{},seamCorrection_{}; + int seamLength_=0,seamPosition_=0; + bool prepared_=false,primeOnNextProcess_=false; + bool haveLastOutput_=false,seamPending_=false; }; } // namespace armix::player diff --git a/tests/player_pitch_tempo_test.cpp b/tests/player_pitch_tempo_test.cpp index 661e498b1..5ff02f183 100644 --- a/tests/player_pitch_tempo_test.cpp +++ b/tests/player_pitch_tempo_test.cpp @@ -60,6 +60,203 @@ 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; + 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 + 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()}; + 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(); + 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;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)); + const double audioTimeMs=192.0*quantum*1000.0/sampleRate; + 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. +// 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; + 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;block0 && 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;block0 && 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})); + 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;i 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() { using P=Processor; @@ -77,6 +274,16 @@ int main() { assert(std::abs(neutral.hz-440)<8); assert(up.hz>neutral.hz && down.hz1.24 && down.ratio<.85); + testNeutralBitExact(); + testSmoothTempoEdits(44100); + testSmoothTempoEdits(48000); + testSmoothTempoEdits(96000); + 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); }