Repository navigation
feat(player): independent KEY/TEMPO with real native worker DSP and per-song stage controls #496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // SPDX-License-Identifier: AGPL-3.0-only | ||
| #pragma once | ||
| #include "../../third_party/signalsmith/signalsmith-stretch.h" | ||
| #include <algorithm> | ||
| #include <cmath> | ||
| #include <cstdint> | ||
|
|
||
| namespace armix::player { | ||
| // WORKER THREAD ONLY. Signalsmith DSP state, FFT storage and scratch buffers | ||
| // are allocated off realtime. Never instantiate or call from JUCE callback. | ||
| // | ||
| // 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. | ||
| class PlayerPitchTempoProcessor final { | ||
| public: | ||
| struct Controls { | ||
| int keySemitones=0; | ||
| int tempoPercent=100; | ||
| bool operator==(const Controls&) const = default; | ||
| }; | ||
| static bool valid(Controls c) noexcept { | ||
| return c.keySemitones>=-6 && c.keySemitones<=6 && | ||
| c.tempoPercent>=80 && c.tempoPercent<=120; | ||
| } | ||
| static int inputFramesForOutput(int outputFrames,Controls c, | ||
| double& remainder) noexcept { | ||
| if(!valid(c)||outputFrames<=0)return 0; | ||
| const double requested=outputFrames* | ||
| (static_cast<double>(c.tempoPercent)/100.0)+remainder; | ||
| const int frames=static_cast<int>(std::floor(requested)); | ||
| remainder=requested-frames; | ||
| return frames; | ||
| } | ||
| bool prepare(int sampleRate,Controls c) { | ||
| if(sampleRate<8000 || sampleRate>384000 || !valid(c)) | ||
| 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<float>(sampleRate_),true); | ||
| stretch_.setTransposeSemitones( | ||
| static_cast<float>(controls_.keySemitones), | ||
| 8000.f/static_cast<float>(sampleRate_)); | ||
| fractionalInput_=0; | ||
| prepared_=true; | ||
| return true; | ||
| } | ||
| void setControls(Controls c) { | ||
| if(!prepared_ || !valid(c) || controls_==c)return; | ||
| if(controls_.keySemitones!=c.keySemitones) | ||
| stretch_.setTransposeSemitones( | ||
| static_cast<float>(c.keySemitones), | ||
| 8000.f/static_cast<float>(sampleRate_)); | ||
| controls_=c; | ||
| } | ||
| Controls applied() const noexcept {return controls_;} | ||
| bool active() const noexcept { | ||
| return controls_.keySemitones!=0 || controls_.tempoPercent!=100; | ||
| } | ||
| int nextInputFrames(int outputFrames) noexcept { | ||
| return inputFramesForOutput(outputFrames,controls_,fractionalInput_); | ||
| } | ||
| // 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. | ||
| bool process(const float* const* in,int nInput,float* const* out,int nOut) { | ||
| if(!prepared_ || !in || !out || !in[0] || !in[1] || | ||
| !out[0] || !out[1] || nInput<0 || nOut<=0)return false; | ||
| if(!active()) { | ||
| if(nInput!=nOut)return false; | ||
| std::copy_n(in[0],nOut,out[0]); | ||
| std::copy_n(in[1],nOut,out[1]); | ||
| } else { | ||
| stretch_.process(in,nInput,out,nOut); | ||
|
Comment on lines
+70
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a performer resets KEY/TEMPO to 0/100 during playback, this branch stops advancing the stretcher but leaves its buffered audio intact. Applying another non-neutral setting later resumes that old history rather than the current song position, replaying earlier audio; entering DSP from an initially neutral song also starts with an unprimed buffer. A standalone reproduction using this processor produced substantial old-tone output after processing one second of silence through neutral bypass. Keep the DSP history synchronized or explicitly re-prime it and handle the latency transition when switching paths. Useful? React with 👍 / 👎. |
||
| } | ||
| for(int c=0;c<2;++c) | ||
| for(int i=0;i<nOut;++i) | ||
| if(!std::isfinite(out[c][i])) | ||
| return false; // source/FFT corruption fails closed | ||
| return true; | ||
| } | ||
| int outputLatency() const noexcept { | ||
| return prepared_&&active()?stretch_.outputLatency():0; | ||
| } | ||
| void flush(float* const* output,int frames) { | ||
| if(prepared_&&active() && frames>0) | ||
| stretch_.flush(output,frames,controls_.tempoPercent/100.f); | ||
| } | ||
| private: | ||
| signalsmith::stretch::SignalsmithStretch<float> stretch_{23}; | ||
| int sampleRate_=0; | ||
| Controls controls_{}; | ||
| double fractionalInput_=0; | ||
| bool prepared_=false; | ||
| }; | ||
| } // namespace armix::player | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For non-neutral playback, the stretcher is fed from its initial empty state without an
outputSeekpre-roll, so pending audio includes both input analysis latency and output synthesis latency. Draining onlyoutputLatency()truncates the ending before setting EOF. At 48 kHz the configured library reports 2,400 input-latency frames and 4,320 output-latency frames; a reproduction at 80% tempo with audio confined to the last 512 source frames yielded energy of only 0.67 with this drain, versus 51.90 when the additional 3,000 tempo-adjusted input-latency frames were drained. Account for the full processing delay, or prime and compensate it explicitly, so backing tracks retain their final notes.Useful? React with 👍 / 👎.