From 4a1550f5f0f088ffd84669823771d5b48cb2d2a7 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Mon, 9 Nov 2026 20:17:31 +0700 Subject: [PATCH 1/3] feat(player): M2 independent KEY and TEMPO with native Signalsmith worker DSP and per-song controls --- CMakeLists.txt | 9 + engine/player/NativePlayerDeck.h | 159 +- engine/player/PlayerPitchTempoProcessor.h | 97 ++ site/preview-v2/assets/arson-player.js | 20 +- .../assets/mixer-contextual-buttons.css | 26 + .../assets/mixer-contextual-buttons.js | 5 + src/LiveAudioEngine.h | 11 + src/Main.cpp | 69 +- ...pproved_preview_mobile_navigation_smoke.py | 13 + tests/player_native_decode_test.cpp | 52 + tests/player_pitch_tempo_test.cpp | 82 + third_party/signalsmith/LICENSE-LINEAR.txt | 21 + third_party/signalsmith/LICENSE-STRETCH.txt | 21 + third_party/signalsmith/README.md | 24 + .../signalsmith/signalsmith-linear/fft.h | 1386 +++++++++++++++++ .../signalsmith/signalsmith-linear/stft.h | 700 +++++++++ third_party/signalsmith/signalsmith-stretch.h | 1060 +++++++++++++ 17 files changed, 3727 insertions(+), 28 deletions(-) create mode 100644 engine/player/PlayerPitchTempoProcessor.h create mode 100644 tests/player_pitch_tempo_test.cpp create mode 100644 third_party/signalsmith/LICENSE-LINEAR.txt create mode 100644 third_party/signalsmith/LICENSE-STRETCH.txt create mode 100644 third_party/signalsmith/README.md create mode 100644 third_party/signalsmith/signalsmith-linear/fft.h create mode 100644 third_party/signalsmith/signalsmith-linear/stft.h create mode 100644 third_party/signalsmith/signalsmith-stretch.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 622f606aa..eb89c69c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -155,6 +155,15 @@ if(BUILD_TESTING) target_compile_features(ARMixPlayerDecodeTests PRIVATE cxx_std_20) add_test(NAME ArSonPlayerRealWavDecodeResampleAndTransport COMMAND ARMixPlayerDecodeTests) add_dependencies(ARMixMixerRoutingTests ARMixPlayerDecodeTests) + # M2: independent stereo key/tempo, documented MIT-vendored Signalsmith. + # Build under the existing Windows mixer test target; no new CI lane. + add_executable(ARMixPlayerPitchTempoTests tests/player_pitch_tempo_test.cpp) + target_include_directories(ARMixPlayerPitchTempoTests PRIVATE + "${CMAKE_CURRENT_SOURCE_DIR}/third_party/signalsmith") + target_compile_features(ARMixPlayerPitchTempoTests PRIVATE cxx_std_20) + add_test(NAME ArSonPlayerM2IndependentKeyTempoStereoPCM + COMMAND ARMixPlayerPitchTempoTests) + add_dependencies(ARMixMixerRoutingTests ARMixPlayerPitchTempoTests) # Windows release workflow builds ARMixMixerRoutingTests explicitly, then runs # all CTests. Make its native session regression a build dependency so the # new executable is actually present (without editing the CI target list). diff --git a/engine/player/NativePlayerDeck.h b/engine/player/NativePlayerDeck.h index 893c73745..af43d273e 100644 --- a/engine/player/NativePlayerDeck.h +++ b/engine/player/NativePlayerDeck.h @@ -1,6 +1,7 @@ // SPDX-License-Identifier: AGPL-3.0-only #pragma once #include "PreparedStereoPcmRing.h" +#include "PlayerPitchTempoProcessor.h" #include "../core/RealtimeSlew64.h" #include #include @@ -28,6 +29,9 @@ class NativePlayerDeck final { bool outputReady=false; juce::String title,error; double deviceRate=0; + int keySemitones=0,tempoPercent=100; + int requestedKey=0,requestedTempo=100; + bool processingChangePending=false; }; NativePlayerDeck() { // 2.7 seconds at 48k, bounded independent of song length. @@ -61,6 +65,12 @@ class NativePlayerDeck final { std::memory_order_release); wake_.notify_one(); } + bool setPitchTempo(int keySemitones,int tempoPercent) noexcept { + const PlayerPitchTempoProcessor::Controls c{keySemitones,tempoPercent}; + if(!PlayerPitchTempoProcessor::valid(c))return false; + requestedControls_.store(packControls(c),std::memory_order_release); + return true; // accepted intent; actual DSP readback in snapshot() + } void load(const juce::File& file,bool autoPlay=true) { ready_.store(false,std::memory_order_release); wantedPlay_.store(autoPlay,std::memory_order_release); @@ -110,10 +120,21 @@ class NativePlayerDeck final { s.generation=generation_.load(std::memory_order_acquire); s.deviceRate=rate_.load(std::memory_order_acquire); s.outputReady=ready_.load(std::memory_order_acquire); + const auto applied=unpackControls( + appliedControls_.load(std::memory_order_acquire)); + const auto requested=unpackControls( + requestedControls_.load(std::memory_order_acquire)); + s.keySemitones=applied.keySemitones; + s.tempoPercent=applied.tempoPercent; + s.requestedKey=requested.keySemitones; + s.requestedTempo=requested.tempoPercent; + s.processingChangePending=(s.keySemitones!=s.requestedKey || + s.tempoPercent!=s.requestedTempo); switch(error_.load(std::memory_order_relaxed)) { case 1:s.error="File missing or unreadable";break; case 2:s.error="Unsupported or corrupt music format";break; case 3:s.error="Audio output not configured";break; + case 4:s.error="Music pitch/tempo processing failed";break; default:break; } {std::lock_guard lock(commandMutex_); @@ -170,6 +191,15 @@ class NativePlayerDeck final { } private: + using Controls=PlayerPitchTempoProcessor::Controls; + static constexpr std::uint32_t packControls(Controls c) noexcept { + return (static_cast(c.tempoPercent)<<8) | + static_cast(c.keySemitones+6); + } + static constexpr Controls unpackControls(std::uint32_t bits) noexcept { + return {static_cast(bits&255u)-6, + static_cast((bits>>8)&255u)}; + } void restart() noexcept { ready_.store(false,std::memory_order_release); played_.store(0,std::memory_order_relaxed); @@ -232,44 +262,131 @@ class NativePlayerDeck final { } const bool mono=reader->numChannels==1; const double ratio=reader->sampleRate/static_cast(deviceHz); - const auto total=static_cast(std::ceil( + const auto sourceTotal=static_cast(std::ceil( static_cast(reader->lengthInSamples)/ratio)); - if(!total) { + if(!sourceTotal) { + state_.store(State::error,std::memory_order_release); + continue; + } + using Controls=PlayerPitchTempoProcessor::Controls; + Controls controls=unpackControls( + requestedControls_.load(std::memory_order_acquire)); + PlayerPitchTempoProcessor pitch; + if(!pitch.prepare(deviceHz,controls)) { + error_.store(4,std::memory_order_release); state_.store(State::error,std::memory_order_release); continue; } + appliedControls_.store(packControls(controls), + std::memory_order_release); juce::AudioFormatReaderSource source(reader.release(),true); juce::ResamplingAudioSource resampled(&source,false,2); resampled.setResamplingRatio(ratio); - resampled.prepareToPlay(1024,static_cast(deviceHz)); - juce::AudioBuffer staging(2,1024); - length_.store(total,std::memory_order_release); - std::uint64_t written=0; - while(!obsolete(version) && writtenring_.capacityFrames()-2048) { + resampled.prepareToPlay(1536,static_cast(deviceHz)); + // Source is device-rate stereo, but independent KEY/TEMPO DSP is + // computed ONLY here on the decoder worker; not in JUCE/ASIO RT. + juce::AudioBuffer input(2,1536),output(2,1024); + std::uint64_t consumed=0,written=0; + const auto estimateTotal=[&]() -> std::uint64_t { + return written+static_cast(std::ceil( + (sourceTotal-consumed)*100.0/controls.tempoPercent)) + +static_cast(pitch.outputLatency()); + }; + length_.store(estimateTotal(),std::memory_order_release); + bool failed=false; + // 0.75 s of decoded music gives disk/decode headroom without + // waiting multiple seconds before a KEY/TEMPO control is heard. + const auto highWater=std::min( + ring_.capacityFrames()-2048, + std::max(8192,static_cast(deviceHz*3/4))); + while(!obsolete(version) && consumedhighWater) { std::this_thread::sleep_for(std::chrono::milliseconds(3)); continue; } - const int n=static_cast(std::min(1024,total-written)); - staging.clear(); - resampled.getNextAudioBlock({&staging,0,n}); - if(mono)staging.copyFrom(1,0,staging,0,0,n); + Controls requested=unpackControls( + requestedControls_.load(std::memory_order_acquire)); + if(requested!=controls && PlayerPitchTempoProcessor::valid(requested)) { + pitch.setControls(requested); // DSP worker, no callback code + controls=requested; + appliedControls_.store(packControls(controls), + std::memory_order_release); + } + int nOutput=1024; + int nInput=pitch.nextInputFrames(nOutput); + const auto remaining=sourceTotal-consumed; + if(remaining(nInput)) { + nOutput=std::max(1,static_cast(std::ceil( + remaining*100.0/controls.tempoPercent))); + nOutput=std::min(1024,nOutput); + nInput=static_cast(remaining); + } + if(nInput<=0 || nInput>1536 || nOutput<=0) { + failed=true;break; + } + input.clear();output.clear(); + resampled.getNextAudioBlock({&input,0,nInput}); + if(mono)input.copyFrom(1,0,input,0,0,nInput); if(obsolete(version))break; - const auto transferred=ring_.write( - staging.getReadPointer(0),staging.getReadPointer(1), - static_cast(n)); + const float* const sourcePcm[]={input.getReadPointer(0), + input.getReadPointer(1)}; + float* const outputPcm[]={output.getWritePointer(0), + output.getWritePointer(1)}; + if(!pitch.process(sourcePcm,nInput,outputPcm,nOutput)) { + failed=true;break; + } + const auto transferred=ring_.write(outputPcm[0],outputPcm[1], + static_cast(nOutput)); + if(transferred!=static_cast(nOutput)) { + failed=true;break; + } + consumed+=static_cast(nInput); written+=transferred; + length_.store(estimateTotal(),std::memory_order_release); if(!ready_.load(std::memory_order_relaxed) && - (ring_.bufferedFrames()>=4096 || written==total)) { + (ring_.bufferedFrames()>=4096 || consumed==sourceTotal)) { ready_.store(true,std::memory_order_release); if(!wantedPlay_.load(std::memory_order_acquire) && state_.load(std::memory_order_relaxed)!=State::stopped) state_.store(State::ready,std::memory_order_release); } } - if(!obsolete(version) && written==total) { - eof_.store(true,std::memory_order_release); - ready_.store(true,std::memory_order_release); + // Drain the final spectral overlap OFF realtime. Without this, + // transposed music loses its last FFT window at EOF. + if(!failed && !obsolete(version) && consumed==sourceTotal && + pitch.active()) { + const int tail=pitch.outputLatency(); + if(tail>0 && tail ending(2,tail); + float* const ends[]={ending.getWritePointer(0), + ending.getWritePointer(1)}; + pitch.flush(ends,tail); + for(int offset=0;offsethighWater) { + std::this_thread::sleep_for(std::chrono::milliseconds(3)); + continue; + } + const int n=std::min(1024,tail-offset); + const auto copied=ring_.write(ends[0]+offset,ends[1]+offset, + static_cast(n)); + if(copied!=static_cast(n)) { + failed=true;break; + } + written+=copied;offset+=n; + } + } + } + if(!obsolete(version)) { + if(failed) { + ready_.store(false,std::memory_order_release); + wantedPlay_.store(false,std::memory_order_release); + error_.store(4,std::memory_order_release); + state_.store(State::error,std::memory_order_release); + } else if(consumed==sourceTotal) { + length_.store(written,std::memory_order_release); + eof_.store(true,std::memory_order_release); + ready_.store(true,std::memory_order_release); + } } resampled.releaseResources(); } @@ -282,6 +399,10 @@ class NativePlayerDeck final { std::atomic generation_{0},played_{0},length_{0}; std::atomic readers_{0}; std::atomic rate_{0},error_{0}; + // Packed atomic makes KEY+TEMPO a single coherent command, avoiding + // a half-new pitch with half-old speed on concurrent UI/worker threads. + std::atomic requestedControls_{(100u<<8)|6u}; + std::atomic appliedControls_{(100u<<8)|6u}; std::atomic state_{State::empty}; armix::dsp::RealtimeSlew64 gain_; // AUDIO only std::uint64_t audioGeneration_=0; // AUDIO only, no race with file thread diff --git a/engine/player/PlayerPitchTempoProcessor.h b/engine/player/PlayerPitchTempoProcessor.h new file mode 100644 index 000000000..d38455689 --- /dev/null +++ b/engine/player/PlayerPitchTempoProcessor.h @@ -0,0 +1,97 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#pragma once +#include "../../third_party/signalsmith/signalsmith-stretch.h" +#include +#include +#include + +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(c.tempoPercent)/100.0)+remainder; + const int frames=static_cast(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(sampleRate_),true); + stretch_.setTransposeSemitones( + static_cast(controls_.keySemitones), + 8000.f/static_cast(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(c.keySemitones), + 8000.f/static_cast(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); + } + for(int c=0;c<2;++c) + for(int i=0;i0) + stretch_.flush(output,frames,controls_.tempoPercent/100.f); + } +private: + signalsmith::stretch::SignalsmithStretch stretch_{23}; + int sampleRate_=0; + Controls controls_{}; + double fractionalInput_=0; + bool prepared_=false; +}; +} // namespace armix::player diff --git a/site/preview-v2/assets/arson-player.js b/site/preview-v2/assets/arson-player.js index de2bee66a..6541af974 100644 --- a/site/preview-v2/assets/arson-player.js +++ b/site/preview-v2/assets/arson-player.js @@ -36,6 +36,11 @@ send(model.transport?.state==="PLAYING"?"playerPause":"playerPlay"); }); $("playerStop").addEventListener("click",()=>send("playerStop")); + $("playerKeyMinus").addEventListener("click",()=>send("playerKeyStep",-1)); + $("playerKeyPlus").addEventListener("click",()=>send("playerKeyStep",1)); + $("playerTempoMinus").addEventListener("click",()=>send("playerTempoStep",-1)); + $("playerTempoPlus").addEventListener("click",()=>send("playerTempoStep",1)); + $("playerPitchReset").addEventListener("click",()=>send("playerPitchTempoReset")); $("playerChooseOutput").addEventListener("click",()=>send("playerChooseOutput")); $("playerNext").addEventListener("click",()=>send("playerNext")); $("playerAutoNext").addEventListener("click",()=> @@ -106,6 +111,16 @@ $("playerProgress").value=+t.durationSeconds>0? Math.min(1,Math.max(0,+t.currentSeconds/(+t.durationSeconds))):0; $("playerPlay").textContent=state==="PLAYING"?"Ⅱ Pause":"▶ Play"; + const editingIndex=model.current>=0?model.current:model.selected; + const chosen=model.tracks[editingIndex]; + const key=Number(chosen?.keySemitones??t.requestedKey??0); + const tempo=Number(chosen?.tempoPercent??t.requestedTempo??100); + $("playerKeyValue").textContent=(key>0?"+":"")+key+" st"; + $("playerTempoValue").textContent=tempo+"%"; + $("playerKeyMinus").disabled=key<=-6; + $("playerKeyPlus").disabled=key>=6; + $("playerTempoMinus").disabled=tempo<=80; + $("playerTempoPlus").disabled=tempo>=120; $("playerAutoNext").textContent=model.autoNext?"Auto Next · On":"Auto Next · Off"; $("playerAutoNext").setAttribute("aria-pressed",String(model.autoNext)); const detail=model.error||t.error||({ @@ -120,7 +135,10 @@ "Music playing · output muted", ERROR:"Unable to decode this file" }[state]||""); - showFeedback(detail,Boolean(model.error||t.error)); + const indicator=(t.processingChangePending && !model.error && !t.error && + state!=="EMPTY" && state!=="ERROR") + ?"Preparing music KEY/TEMPO in decoder…":detail; + showFeedback(indicator,Boolean(model.error||t.error)); $("playerChooseOutput").hidden=!native || !Boolean(model.chooseOutput); $("playerCount").textContent=model.tracks.length+" songs"; renderTracks(); diff --git a/site/preview-v2/assets/mixer-contextual-buttons.css b/site/preview-v2/assets/mixer-contextual-buttons.css index d80749d4e..84f8b2ef8 100644 --- a/site/preview-v2/assets/mixer-contextual-buttons.css +++ b/site/preview-v2/assets/mixer-contextual-buttons.css @@ -296,3 +296,29 @@ body :is(.mx-peq-global,.mx-comp-power,.mx-harm-power):focus-visible{outline:2px color:#baf2e4;white-space:nowrap;font-size:11px} #arsonPlayer #playerChooseOutput:hover{background:#286355} #arsonPlayer #playerChooseOutput[hidden]{display:none!important} + +/* M2 independent KEY/TEMPO: a single compact instrument-grade control row. */ +#arsonPlayer .ap-pitchtempo{display:flex;align-items:center;gap:6px; + padding:8px 11px 7px;border-bottom:1px solid #293f45; + background:#182a31;flex-wrap:nowrap;justify-content:space-between} +#arsonPlayer .ap-pitchctrl{display:flex;align-items:center;gap:3px; + min-width:0;flex-shrink:1} +#arsonPlayer .ap-pitchctrl span{font-size:10px;color:#8eb2b9; + letter-spacing:.035em;margin-right:2px} +#arsonPlayer .ap-pitchctrl button{min-width:24px;width:24px;height:27px; + border:1px solid #3e5c64;border-radius:5px;background:#213c44; + color:#badfdf;line-height:1;font-size:14px} +#arsonPlayer .ap-pitchctrl button:hover{background:#29515a} +#arsonPlayer .ap-pitchctrl output{width:40px;text-align:center; + font-variant-numeric:tabular-nums;color:#8ee3c8;font-size:11px} +#arsonPlayer #playerTempoValue{width:39px} +#arsonPlayer .ap-reset{border:1px solid #3c555e;background:#20333d; + border-radius:5px;padding:5px 6px;font-size:10px;color:#a9bfc3} +@media(max-width:390px){ + #arsonPlayer .ap-pitchtempo{padding-left:7px;padding-right:7px;gap:3px} + #arsonPlayer .ap-pitchctrl{gap:2px} + #arsonPlayer .ap-pitchctrl span{font-size:9px} + #arsonPlayer .ap-pitchctrl output{width:35px} + #arsonPlayer #playerTempoValue{width:36px} + #arsonPlayer .ap-reset{padding:5px} +} diff --git a/site/preview-v2/assets/mixer-contextual-buttons.js b/site/preview-v2/assets/mixer-contextual-buttons.js index 2da71c971..54a05ea26 100644 --- a/site/preview-v2/assets/mixer-contextual-buttons.js +++ b/site/preview-v2/assets/mixer-contextual-buttons.js @@ -53,6 +53,11 @@
ArSon PlayerMusic · Setlist
PLAYER A · CURRENT
No music loaded
00:0000:00
+
+
KEY0 st
+
TEMPO100%
+ +
Add music to begin
Setlist0 songs
diff --git a/src/LiveAudioEngine.h b/src/LiveAudioEngine.h index 0d35127a4..77cc2b4c5 100644 --- a/src/LiveAudioEngine.h +++ b/src/LiveAudioEngine.h @@ -1648,6 +1648,11 @@ class LiveAudioEngine final : private juce::AudioIODeviceCallback playerDeck_.play(); return {}; } + bool setPlayerPitchTempo(int semitones,int percent) noexcept { + // A per-song operator intent. No sample-rate/device reconfiguration: + // the decoder worker consumes more/fewer backing-track PCM frames. + return playerDeck_.setPitchTempo(semitones,percent); + } void playerPause() noexcept {playerDeck_.pause();} void playerStop() noexcept {playerDeck_.stop();} juce::var playerStatus() const { @@ -1674,6 +1679,12 @@ class LiveAudioEngine final : private juce::AudioIODeviceCallback static_cast(status.duration)/status.deviceRate:0.0); data->setProperty("underrunFrames",juce::String( static_cast(status.underruns))); + data->setProperty("keySemitones",status.keySemitones); + data->setProperty("tempoPercent",status.tempoPercent); + data->setProperty("requestedKey",status.requestedKey); + data->setProperty("requestedTempo",status.requestedTempo); + data->setProperty("processingChangePending", + status.processingChangePending); data->setProperty("generation",juce::String( static_cast(status.generation))); data->setProperty("channelId",juce::String( diff --git a/src/Main.cpp b/src/Main.cpp index e7b88e1a8..92ea1d50f 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -377,6 +377,8 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr std::unique_ptr chooser_; std::unique_ptr playerFileChooser_; std::vector playerLibrary_; // one compact default setlist + struct PlayerTrackSettings {int key=0,tempo=100;}; + std::vector playerTrackSettings_; // parallel stable entry IDs int playerSelected_=-1,playerCurrent_=-1; int playerOutputPendingIndex_=-1; // only after explicit Choose output gesture bool playerAutoNext_=false; @@ -2382,17 +2384,19 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr const auto file=settingsDirectory().getChildFile("player-library.json"); if(!file.existsAsFile())return; auto json=juce::JSON::parse(file.loadFileAsString()); - if(!json.isObject() || - static_cast(json.getProperty("version",0))!=1) { + const int version=static_cast(json.getProperty("version",0)); + if(!json.isObject() || (version!=1 && version!=2)) { playerError_="Saved setlist could not be read; music files were not changed"; return; } const auto tracks=json.getProperty("files",{}); if(const auto* list=tracks.getArray()) { - for(const auto& path:*list) { + for(const auto& item:*list) { if(playerLibrary_.size()>=2000)break; - if(!path.isString())continue; - const auto text=path.toString(); + if((version==1 && !item.isString()) || + (version==2 && !item.isObject()))continue; + const auto text=version==1?item.toString(): + item.getProperty("path","").toString(); if(text.isEmpty() || text.length()>4096)continue; const juce::File entry(text); if(!juce::File::isAbsolutePath(text) || @@ -2400,7 +2404,16 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr std::find(playerLibrary_.begin(),playerLibrary_.end(),entry) !=playerLibrary_.end()) continue; + PlayerTrackSettings controls; + if(version==2) { + controls.key=static_cast(item.getProperty("key",0)); + controls.tempo=static_cast(item.getProperty("tempo",100)); + if(controls.key< -6 || controls.key>6 || + controls.tempo<80 || controls.tempo>120) + controls={}; // corrupt automation cannot alter audio + } playerLibrary_.push_back(entry); + playerTrackSettings_.push_back(controls); } } playerAutoNext_=json.getProperty("autoNext",false).isBool() && @@ -2409,11 +2422,16 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr } void persistPlayerLibrary() { auto document=std::make_unique(); - document->setProperty("version",1); + document->setProperty("version",2); document->setProperty("autoNext",playerAutoNext_); juce::Array files; - for(const auto& f:playerLibrary_) - files.add(f.getFullPathName()); + for(std::size_t i=0;i(); + entry->setProperty("path",playerLibrary_[i].getFullPathName()); + entry->setProperty("key",playerTrackSettings_[i].key); + entry->setProperty("tempo",playerTrackSettings_[i].tempo); + files.add(juce::var(entry.release())); + } document->setProperty("files",juce::var(files)); const auto target=settingsDirectory().getChildFile("player-library.json"); juce::TemporaryFile staging(target); @@ -2442,6 +2460,8 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr song->setProperty("title", playerLibrary_[i].getFileNameWithoutExtension()); song->setProperty("missing",!playerLibrary_[i].existsAsFile()); + song->setProperty("keySemitones",playerTrackSettings_[i].key); + song->setProperty("tempoPercent",playerTrackSettings_[i].tempo); rows.add(juce::var(song.release())); } obj->setProperty("tracks",juce::var(rows)); @@ -2480,6 +2500,7 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr if(first==static_cast(-1)) first=safe->playerLibrary_.size(); safe->playerLibrary_.push_back(file); + safe->playerTrackSettings_.push_back({}); } if(safe->playerSelected_<0 && first!=static_cast(-1)) safe->playerSelected_=static_cast(first); @@ -2517,6 +2538,14 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr phase!="EMPTY") { playerError_=engine_.playerPlay(); } else { + // Song-level KEY/TEMPO is installed before decoder generation + // starts. Never transpose microphone input or alter ASIO clock. + const auto controls=playerTrackSettings_[ + static_cast(playerSelected_)]; + if(!engine_.setPlayerPitchTempo(controls.key,controls.tempo)) { + playerError_="Invalid KEY or TEMPO settings"; + emitPlayerState();return; + } const auto error=engine_.loadPlayerMusic( playerLibrary_[static_cast(playerSelected_)]); playerError_=error; @@ -2559,6 +2588,30 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr } if(action=="playerSubscribe")return emitPlayerState(true); if(action=="playerAdd")return choosePlayerFiles(); + if(action=="playerKeyStep" || action=="playerTempoStep" || + action=="playerPitchTempoReset") { + const int i=playerCurrent_>=0?playerCurrent_:playerSelected_; + if(i<0 || i>=static_cast(playerTrackSettings_.size())) { + playerError_="Add Music to set a song's KEY or TEMPO"; + emitPlayerState();return; + } + const int direction=static_cast(value); + auto next=playerTrackSettings_[static_cast(i)]; + if(action=="playerPitchTempoReset")next={}; + else if((direction==1 || direction==-1) && + action=="playerKeyStep") + next.key=std::clamp(next.key+direction,-6,6); + else if(direction==1 || direction==-1) + next.tempo=std::clamp(next.tempo+direction*5,80,120); + else return; // invalid native control intent cannot mutate DSP + if(engine_.setPlayerPitchTempo(next.key,next.tempo)) { + playerTrackSettings_[static_cast(i)]=next; + playerError_.clear(); + persistPlayerLibrary(); + emitPlayerState(true); + } + return; + } if(action=="playerPlay")return playTransportMusic(); if(action=="playerPause") { engine_.playerPause();emitPlayerState();return; diff --git a/tests/approved_preview_mobile_navigation_smoke.py b/tests/approved_preview_mobile_navigation_smoke.py index d513e7a45..a156320f8 100644 --- a/tests/approved_preview_mobile_navigation_smoke.py +++ b/tests/approved_preview_mobile_navigation_smoke.py @@ -126,6 +126,19 @@ async def verify_phone(browser, width: int, height: int): # contextual output chooser MUST NOT impersonate a device selector. assert await page.locator("#playerChooseOutput").is_hidden() assert await page.locator("#playerChooseOutput").count() == 1 + # M2 controls are compact and must never create WebAudio or a + # competing callback on public /preview/ without native JUCE. + assert await page.locator("#playerKeyValue").inner_text() == "0 st" + assert await page.locator("#playerTempoValue").inner_text() == "100%" + for control in ("#playerKeyMinus","#playerKeyPlus", + "#playerTempoMinus","#playerTempoPlus", + "#playerPitchReset"): + button=page.locator(control) + bounds=await button.bounding_box() + assert bounds and bounds["x"]>=0 and bounds["x"]+bounds["width"]<=width+2 + await page.locator("#playerKeyPlus").click() + assert "desktop app" in await page.locator("#playerFeedback").inner_text() + assert await page.locator("#playerKeyValue").inner_text() == "0 st" await page.locator("#playerAdd").click() feedback = await page.locator("#playerFeedback").inner_text() assert "desktop app" in feedback, ("public preview must not fake playback", feedback) diff --git a/tests/player_native_decode_test.cpp b/tests/player_native_decode_test.cpp index 17a1fb9f2..1a75e778f 100644 --- a/tests/player_native_decode_test.cpp +++ b/tests/player_native_decode_test.cpp @@ -106,5 +106,57 @@ int main() { deck.pull(left.data(),right.data(),256); assert(left[0]==0.f && right[0]==0.f); } + // M2 real JUCE media → sample-rate converter → independent stereo DSP + // → prepared FIFO; this runs without a physical audio device. + { + armix::player::NativePlayerDeck player; + player.deviceRate(48000.0); + assert(player.setPitchTempo(3,80)); + assert(!player.setPitchTempo(7,80)); // invalid cannot mutate DSP + assert(!player.setPitchTempo(3,121)); + player.load(file,true); + bool prepared=false; + for(int i=0;i<700;++i) { + if(player.isPlayingRequested() && + !player.snapshot().processingChangePending) { + prepared=true;break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + } + assert(prepared); + const auto preparedStatus=player.snapshot(); + assert(preparedStatus.keySemitones==3); + assert(preparedStatus.tempoPercent==80); + assert(preparedStatus.duration>18000); // slower: more output frames + std::array l{},r{}; + double energy=0,stereo=0; + for(int i=0;i<48;++i) { + player.pull(l.data(),r.data(),256); + for(int n=0;n<256;++n) { + assert(std::isfinite(l[n]) && std::isfinite(r[n])); + energy+=static_cast(l[n])*l[n]; + stereo+=static_cast(l[n])*r[n]; + } + } + assert(energy>0.1 && stereo<0); + // Independent KEY and TEMPO changes are accepted by the decoder + // worker without resetting or manipulating the ASIO device clock. + assert(player.setPitchTempo(-2,120)); + const auto request=player.snapshot(); + assert(request.requestedKey==-2 && request.requestedTempo==120); + assert(request.deviceRate==48000.0); + player.stop(); + player.play(); + bool newSettings=false; + for(int i=0;i<700;++i) { + const auto st=player.snapshot(); + if(st.keySemitones==-2 && st.tempoPercent==120 && + player.isPlayingRequested()) { + newSettings=true;break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + } + assert(newSettings); + } assert(file.deleteFile()); } diff --git a/tests/player_pitch_tempo_test.cpp b/tests/player_pitch_tempo_test.cpp new file mode 100644 index 000000000..661e498b1 --- /dev/null +++ b/tests/player_pitch_tempo_test.cpp @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#include "../engine/player/PlayerPitchTempoProcessor.h" +#ifdef NDEBUG +#undef NDEBUG +#endif +#include +#include +#include +#include +#include +#include +#include +#include +namespace { +constexpr int rate=48000,frames=48000; +constexpr double pi=3.14159265358979323846; +using Processor=armix::player::PlayerPitchTempoProcessor; +struct Evidence {double hz=0,ratio=0,correlation=0;}; +Evidence test(Processor::Controls c) { + Processor processor; + assert(processor.prepare(rate,c)); + std::vector inputL(frames),inputR(frames),outL,outR; + for(int i=0;i(.45*std::sin(2*pi*440.*i/rate)); + inputL[i]=v;inputR[i]=-v; // 2 real lanes, opposite phase + } + int read=0; + constexpr int quantum=512; + std::array left{},right{}; + while(readframes)break; + const float* const inputs[]={inputL.data()+read,inputR.data()+read}; + float* const outputs[]={left.data(),right.data()}; + assert(processor.process(inputs,inFrames,outputs,quantum)); + outL.insert(outL.end(),left.begin(),left.end()); + outR.insert(outR.end(),right.begin(),right.end()); + read+=inFrames; + } + const double expectedOutput=frames*100.0/c.tempoPercent; + assert(std::abs(static_cast(outL.size())-expectedOutput)<1025); + assert(outL.size()==outR.size()); + const int start=static_cast(std::min( + outL.size()/2,20000)); + const int count=std::min(8192,static_cast(outL.size())-start-1); + assert(count>=4000); + int crossings=0; + double power=0,xy=0,x2=0,y2=0; + for(int i=0;i0 && outL[start+i-1]<=0 && x>0)++crossings; + } + assert(power>10); // nonzero actual pitch-shifted backing PCM + const double hz=rate*crossings/static_cast(count); + const double correlation=xy/std::sqrt(x2*y2); + const double expectedHz=440.*std::pow(2.,c.keySemitones/12.); + assert(std::abs(hz-expectedHz)<18); + assert(correlation<-.9); // phase coherence of linked stereo shift + return {hz,static_cast(outL.size())/frames,correlation}; +} +} +int main() { + using P=Processor; + double rem=0; + assert(!P::valid({-7,100})); + assert(!P::valid({3,125})); + assert(P::valid({-6,80})&&P::valid({6,120})); + for(int i=0,total=0;i<1000;++i) + total+=P::inputFramesForOutput(512,{1,87},rem); + assert(std::abs(rem)<1 && rem>=0); + assert(P::inputFramesForOutput(0,{0,100},rem)==0); + const auto neutral=test({0,100}); + const auto up=test({3,80}); + const auto down=test({-2,120}); + assert(std::abs(neutral.hz-440)<8); + assert(up.hz>neutral.hz && down.hz1.24 && down.ratio<.85); + std::printf("M2: pass neutral %.1fHz, +3st/80%% %.1fHz, -2st/120%% %.1fHz; stereo=%.3f\n", + neutral.hz,up.hz,down.hz,up.correlation); +} diff --git a/third_party/signalsmith/LICENSE-LINEAR.txt b/third_party/signalsmith/LICENSE-LINEAR.txt new file mode 100644 index 000000000..c2569820c --- /dev/null +++ b/third_party/signalsmith/LICENSE-LINEAR.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Signalsmith Audio + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/third_party/signalsmith/LICENSE-STRETCH.txt b/third_party/signalsmith/LICENSE-STRETCH.txt new file mode 100644 index 000000000..889031e63 --- /dev/null +++ b/third_party/signalsmith/LICENSE-STRETCH.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Geraint Luff / Signalsmith Audio Ltd. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/third_party/signalsmith/README.md b/third_party/signalsmith/README.md new file mode 100644 index 000000000..3a9dfe2db --- /dev/null +++ b/third_party/signalsmith/README.md @@ -0,0 +1,24 @@ +# Signalsmith libraries vendored for ArSon Player M2 + +This directory contains **verbatim**, version-pinned upstream C++ files for +worker-thread pitch/time stretching. Both projects are MIT licensed; their full +licenses are included next to these headers. No runtime download, Python package, +additional audio callback, or network operation is used by ArSonMix. + +| Component | Exact upstream commit | Included files | +| --- | --- | --- | +| Signalsmith Stretch (version 1.3.2) | `88061fd4eda56a2353c5d65c74aa3f4ccf76f9da` | `signalsmith-stretch.h`, `LICENSE-STRETCH.txt` | +| Signalsmith Linear | `7bb1c8f36794d6980080ce817c91f8da8fe56a66` | `signalsmith-linear/stft.h`, `signalsmith-linear/fft.h`, `LICENSE-LINEAR.txt` | + +Upstream sources: +- https://github.com/Signalsmith-Audio/signalsmith-stretch +- https://github.com/Signalsmith-Audio/linear + +The original source headers are unmodified. Integrator controls, DSP lifecycle, +stereo processing, independent tempo, tests and realtime-boundary invariants are +in `engine/player/PlayerPitchTempoProcessor.h` and +`tests/player_pitch_tempo_test.cpp`. + +**Product boundary:** This DSP runs only on the existing ArSon Player decoder +worker and sends pre-rendered stereo PCM to the previously verified native +ChannelId mixer. It is not allowed to call from the JUCE device callback. diff --git a/third_party/signalsmith/signalsmith-linear/fft.h b/third_party/signalsmith/signalsmith-linear/fft.h new file mode 100644 index 000000000..49986af01 --- /dev/null +++ b/third_party/signalsmith/signalsmith-linear/fft.h @@ -0,0 +1,1386 @@ +#ifndef SIGNALSMITH_AUDIO_LINEAR_FFT_H +#define SIGNALSMITH_AUDIO_LINEAR_FFT_H + +#include +#include +#include +#include + +#if defined(__FAST_MATH__) && (__apple_build_version__ >= 16000000) && (__apple_build_version__ <= 16000099) && !defined(SIGNALSMITH_IGNORE_BROKEN_APPLECLANG) +# error Apple Clang 16.0.0 generates incorrect SIMD for ARM. If you HAVE to use this version of Clang, turn off -ffast-math. +#endif + +#ifndef M_PI +# define M_PI 3.14159265358979323846 +#endif + +namespace signalsmith { namespace linear { + +namespace _impl { + // Helpers for complex arithmetic, ignoring the NaN/Inf edge-cases you get without `-ffast-math` + template + void complexMul(std::complex *a, const std::complex *b, const std::complex *c, size_t size) { + for (size_t i = 0; i < size; ++i) { + auto bi = b[i], ci = c[i]; + a[i] = {bi.real()*ci.real() - bi.imag()*ci.imag(), bi.imag()*ci.real() + bi.real()*ci.imag()}; + } + } + template + void complexMulConj(std::complex *a, const std::complex *b, const std::complex *c, size_t size) { + for (size_t i = 0; i < size; ++i) { + auto bi = b[i], ci = c[i]; + a[i] = {bi.real()*ci.real() + bi.imag()*ci.imag(), bi.imag()*ci.real() - bi.real()*ci.imag()}; + } + } + template + void complexMul(V *ar, V *ai, const V *br, const V *bi, const V *cr, const V *ci, size_t size) { + for (size_t i = 0; i < size; ++i) { + V rr = br[i]*cr[i] - bi[i]*ci[i]; + V ri = br[i]*ci[i] + bi[i]*cr[i]; + ar[i] = rr; + ai[i] = ri; + } + } + template + void complexMulConj(V *ar, V *ai, const V *br, const V *bi, const V *cr, const V *ci, size_t size) { + for (size_t i = 0; i < size; ++i) { + V rr = cr[i]*br[i] + ci[i]*bi[i]; + V ri = cr[i]*bi[i] - ci[i]*br[i]; + ar[i] = rr; + ai[i] = ri; + } + } + + // Input: aStride elements next to each other -> output with bStride + template + void interleaveCopy(const V *a, V *b, size_t bStride) { + for (size_t bi = 0; bi < bStride; ++bi) { + const V *offsetA = a + bi*aStride; + V *offsetB = b + bi; + for (size_t ai = 0; ai < aStride; ++ai) { + offsetB[ai*bStride] = offsetA[ai]; + } + } + } + template + void interleaveCopy(const V *a, V *b, size_t aStride, size_t bStride) { + for (size_t bi = 0; bi < bStride; ++bi) { + const V *offsetA = a + bi*aStride; + V *offsetB = b + bi; + for (size_t ai = 0; ai < aStride; ++ai) { + offsetB[ai*bStride] = offsetA[ai]; + } + } + } + template + void interleaveCopy(const V *aReal, const V *aImag, V *bReal, V *bImag, size_t bStride) { + for (size_t bi = 0; bi < bStride; ++bi) { + const V *offsetAr = aReal + bi*aStride; + const V *offsetAi = aImag + bi*aStride; + V *offsetBr = bReal + bi; + V *offsetBi = bImag + bi; + for (size_t ai = 0; ai < aStride; ++ai) { + offsetBr[ai*bStride] = offsetAr[ai]; + offsetBi[ai*bStride] = offsetAi[ai]; + } + } + } + template + void interleaveCopy(const V *aReal, const V *aImag, V *bReal, V *bImag, size_t aStride, size_t bStride) { + for (size_t bi = 0; bi < bStride; ++bi) { + const V *offsetAr = aReal + bi*aStride; + const V *offsetAi = aImag + bi*aStride; + V *offsetBr = bReal + bi; + V *offsetBi = bImag + bi; + for (size_t ai = 0; ai < aStride; ++ai) { + offsetBr[ai*bStride] = offsetAr[ai]; + offsetBi[ai*bStride] = offsetAi[ai]; + } + } + } +} + +/// Fairly simple and very portable power-of-2 FFT +template +struct SimpleFFT { + using Complex = std::complex; + + SimpleFFT(size_t size=0) { + resize(size); + } + + void resize(size_t size) { + twiddles.resize(size*3/4); + for (size_t i = 0; i < size*3/4; ++i) { + Sample twiddlePhase = -2*M_PI*i/size; + twiddles[i] = std::polar(Sample(1), twiddlePhase); + } + working.resize(size); + } + + void fft(const Complex *time, Complex *freq) { + size_t size = working.size(); + if (size <= 1) { + *freq = *time; + return; + } + fftPass(size, 1, time, freq, working.data()); + } + + void ifft(const Complex *freq, Complex *time) { + size_t size = working.size(); + if (size <= 1) { + *time = *freq; + return; + } + fftPass(size, 1, freq, time, working.data()); + } + + void fft(const Sample *inR, const Sample *inI, Sample *outR, Sample *outI) { + size_t size = working.size(); + if (size <= 1) { + *outR = *inR; + *outI = *inI; + return; + } + Sample *workingR = (Sample *)working.data(), *workingI = workingR + size; + fftPass(size, 1, inR, inI, outR, outI, workingR, workingI); + } + void ifft(const Sample *inR, const Sample *inI, Sample *outR, Sample *outI) { + size_t size = working.size(); + if (size <= 1) { + *outR = *inR; + *outI = *inI; + return; + } + Sample *workingR = (Sample *)working.data(), *workingI = workingR + size; + fftPass(size, 1, inR, inI, outR, outI, workingR, workingI); + } +private: + std::vector twiddles; + std::vector working; + + template + static Complex mul(const Complex &a, const Complex &b) { + return conjB ? Complex{ + a.real()*b.real() + a.imag()*b.imag(), + a.imag()*b.real() - a.real()*b.imag() + } : Complex{ + a.real()*b.real() - a.imag()*b.imag(), + a.imag()*b.real() + a.real()*b.imag() + }; + } + + // Calculate a [size]-point FFT, where each element is a block of [stride] values + template + void fftPass(size_t size, size_t stride, const Complex *input, Complex *output, Complex *working) { + if (size/4 > 1) { + // Calculate four quarter-size FFTs + fftPass(size/4, stride*4, input, working, output); + combine4(size, stride, working, output); + } else if (size == 4) { + combine4(4, stride, input, output); + } else { + // 2-point FFT + for (size_t s = 0; s < stride; ++s) { + Complex a = input[s]; + Complex b = input[s + stride]; + output[s] = a + b; + output[s + stride] = a - b; + } + } + } + + // Combine interleaved results into a single spectrum + template + void combine4(size_t size, size_t stride, const Complex *input, Complex *output) const { + auto twiddleStep = working.size()/size; + for (size_t i = 0; i < size/4; ++i) { + Complex twiddleB = twiddles[i*twiddleStep]; + Complex twiddleC = twiddles[i*2*twiddleStep]; + Complex twiddleD = twiddles[i*3*twiddleStep]; + + const Complex *inputA = input + 4*i*stride; + const Complex *inputB = input + (4*i + 1)*stride; + const Complex *inputC = input + (4*i + 2)*stride; + const Complex *inputD = input + (4*i + 3)*stride; + Complex *outputA = output + i*stride; + Complex *outputB = output + (i + size/4)*stride; + Complex *outputC = output + (i + size/4*2)*stride; + Complex *outputD = output + (i + size/4*3)*stride; + for (size_t s = 0; s < stride; ++s) { + Complex a = inputA[s]; + Complex b = mul(inputB[s], twiddleB); + Complex c = mul(inputC[s], twiddleC); + Complex d = mul(inputD[s], twiddleD); + Complex ac0 = a + c, ac1 = a - c; + Complex bd0 = b + d, bd1 = inverse ? (b - d) : (d - b); + Complex bd1i = {-bd1.imag(), bd1.real()}; + outputA[s] = ac0 + bd0; + outputB[s] = ac1 + bd1i; + outputC[s] = ac0 - bd0; + outputD[s] = ac1 - bd1i; + } + } + } + + // The same thing, but translated for split-complex input/output + template + void fftPass(size_t size, size_t stride, const Sample *inputR, const Sample *inputI, Sample *outputR, Sample *outputI, Sample *workingR, Sample *workingI) const { + if (size/4 > 1) { + // Calculate four quarter-size FFTs + fftPass(size/4, stride*4, inputR, inputI, workingR, workingI, outputR, outputI); + combine4(size, stride, workingR, workingI, outputR, outputI); + } else if (size == 4) { + combine4(4, stride, inputR, inputI, outputR, outputI); + } else { + // 2-point FFT + for (size_t s = 0; s < stride; ++s) { + Sample ar = inputR[s], ai = inputI[s]; + Sample br = inputR[s + stride], bi = inputI[s + stride]; + outputR[s] = ar + br; + outputI[s] = ai + bi; + outputR[s + stride] = ar - br; + outputI[s + stride] = ai - bi; + } + } + } + + // Combine interleaved results into a single spectrum + template + void combine4(size_t size, size_t stride, const Sample *inputR, const Sample *inputI, Sample *outputR, Sample *outputI) const { + auto twiddleStep = working.size()/size; + for (size_t i = 0; i < size/4; ++i) { + Complex twiddleB = twiddles[i*twiddleStep]; + Complex twiddleC = twiddles[i*2*twiddleStep]; + Complex twiddleD = twiddles[i*3*twiddleStep]; + + const Sample *inputAr = inputR + 4*i*stride, *inputAi = inputI + 4*i*stride; + const Sample *inputBr = inputR + (4*i + 1)*stride, *inputBi = inputI + (4*i + 1)*stride; + const Sample *inputCr = inputR + (4*i + 2)*stride, *inputCi = inputI + (4*i + 2)*stride; + const Sample *inputDr = inputR + (4*i + 3)*stride, *inputDi = inputI + (4*i + 3)*stride; + Sample *outputAr = outputR + i*stride, *outputAi = outputI + i*stride; + Sample *outputBr = outputR + (i + size/4)*stride, *outputBi = outputI + (i + size/4)*stride; + Sample *outputCr = outputR + (i + size/4*2)*stride, *outputCi = outputI + (i + size/4*2)*stride; + Sample *outputDr = outputR + (i + size/4*3)*stride, *outputDi = outputI + (i + size/4*3)*stride; + for (size_t s = 0; s < stride; ++s) { + Complex a = {inputAr[s], inputAi[s]}; + Complex b = mul({inputBr[s], inputBi[s]}, twiddleB); + Complex c = mul({inputCr[s], inputCi[s]}, twiddleC); + Complex d = mul({inputDr[s], inputDi[s]}, twiddleD); + Complex ac0 = a + c, ac1 = a - c; + Complex bd0 = b + d, bd1 = inverse ? (b - d) : (d - b); + Complex bd1i = {-bd1.imag(), bd1.real()}; + outputAr[s] = ac0.real() + bd0.real(); + outputAi[s] = ac0.imag() + bd0.imag(); + outputBr[s] = ac1.real() + bd1i.real(); + outputBi[s] = ac1.imag() + bd1i.imag(); + outputCr[s] = ac0.real() - bd0.real(); + outputCi[s] = ac0.imag() - bd0.imag(); + outputDr[s] = ac1.real() - bd1i.real(); + outputDi[s] = ac1.imag() - bd1i.imag(); + } + } + } +}; + +/// A power-of-2 only FFT, specialised with platform-specific fast implementations where available +template +struct Pow2FFT { + static constexpr bool prefersSplit = true; // whether this FFT implementation is faster when given split-complex inputs + using Complex = std::complex; + + Pow2FFT(size_t size=0) { + resize(size); + } + // Allow move, but not copy + Pow2FFT(const Pow2FFT &other) = delete; + Pow2FFT(Pow2FFT &&other) : tmp(std::move(other.tmp)), simpleFFT(std::move(other.simpleFFT)) {} + + void resize(size_t size) { + simpleFFT.resize(size); + tmp.resize(size); + } + + void fft(const Complex *time, Complex *freq) { + simpleFFT.fft(time, freq); + } + void fft(const Sample *inR, const Sample *inI, Sample *outR, Sample *outI) { + simpleFFT.fft(inR, inI, outR, outI); + } + + void ifft(const Complex *freq, Complex *time) { + simpleFFT.ifft(freq, time); + } + void ifft(const Sample *inR, const Sample *inI, Sample *outR, Sample *outI) { + simpleFFT.ifft(inR, inI, outR, outI); + } + +private: + std::vector tmp; + SimpleFFT simpleFFT; +}; + +/// An FFT which can handle multiples of 3 and 5, and can be computed in chunks +template +struct SplitFFT { + using Complex = std::complex; + static constexpr bool prefersSplit = Pow2FFT::prefersSplit; + + static constexpr size_t maxSplit = splitComputation ? 4 : 1; + static constexpr size_t minInnerSize = 32; + + static size_t fastSizeAbove(size_t size) { + size_t pow2 = 1; + while (pow2 < 16 && pow2 < size) pow2 *= 2; + while (pow2*8 < size) pow2 *= 2; + size_t multiple = (size + pow2 - 1)/pow2; // will be 1-8 + if (multiple == 7) ++multiple; + return multiple*pow2; + } + + SplitFFT(size_t size=0) { + resize(size); + } + + void resize(size_t size) { + innerSize = 1; + outerSize = size; + + dftTmp.resize(0); + dftTwists.resize(0); + plan.resize(0); + if (!size) return; + + // Inner size = largest power of 2 such that either the inner size >= minInnerSize, or we have the target number of splits + while (!(outerSize&1) && (outerSize > maxSplit || innerSize < minInnerSize)) { + innerSize *= 2; + outerSize /= 2; + } + tmpFreq.resize(size); + innerFFT.resize(innerSize); + + outerTwiddles.resize(innerSize*(outerSize - 1)); + outerTwiddlesR.resize(innerSize*(outerSize - 1)); + outerTwiddlesI.resize(innerSize*(outerSize - 1)); + for (size_t i = 0; i < innerSize; ++i) { + for (size_t s = 1; s < outerSize; ++s) { + Sample twiddlePhase = Sample(-2*M_PI*i/innerSize*s/outerSize); + outerTwiddles[i + (s - 1)*innerSize] = std::polar(Sample(1), twiddlePhase); + } + } + for (size_t i = 0; i < outerTwiddles.size(); ++i) { + outerTwiddlesR[i] = outerTwiddles[i].real(); + outerTwiddlesI[i] = outerTwiddles[i].imag(); + } + + StepType interleaveStep = StepType::interleaveOrderN; + StepType finalStep = StepType::finalOrderN; + if (outerSize == 2) { + interleaveStep = StepType::interleaveOrder2; + finalStep = StepType::finalOrder2; + } + if (outerSize == 3) { + interleaveStep = StepType::interleaveOrder3; + finalStep = StepType::finalOrder3; + } + if (outerSize == 4) { + interleaveStep = StepType::interleaveOrder4; + finalStep = StepType::finalOrder4; + } + if (outerSize == 5) { + interleaveStep = StepType::interleaveOrder5; + finalStep = StepType::finalOrder5; + } + + if (outerSize <= 1) { + if (size > 0) plan.push_back(Step{StepType::passthrough, 0}); + } else { + plan.push_back({interleaveStep, 0}); + plan.push_back({StepType::firstFFT, 0}); + for (size_t s = 1; s < outerSize; ++s) { + plan.push_back({StepType::middleFFT, s*innerSize}); + } + plan.push_back({StepType::twiddles, 0}); + plan.push_back({finalStep, 0}); + + if (finalStep == StepType::finalOrderN) { + dftTmp.resize(outerSize); + dftTwists.resize(outerSize); + for (size_t s = 0; s < outerSize; ++s) { + Sample dftPhase = Sample(-2*M_PI*s/outerSize); + dftTwists[s] = std::polar(Sample(1), dftPhase); + } + } + } + } + + size_t size() const { + return innerSize*outerSize; + } + size_t steps() const { + return plan.size(); + } + + void fft(const Complex *time, Complex *freq) { + for (auto &step : plan) { + fftStep(step, time, freq); + } + } + void fft(size_t step, const Complex *time, Complex *freq) { + fftStep(plan[step], time, freq); + } + void fft(const Sample *inR, const Sample *inI, Sample *outR, Sample *outI) { + for (auto &step : plan) { + fftStep(step, inR, inI, outR, outI); + } + } + void fft(size_t step, const Sample *inR, const Sample *inI, Sample *outR, Sample *outI) { + fftStep(plan[step], inR, inI, outR, outI); + } + + void ifft(const Complex *freq, Complex *time) { + for (auto &step : plan) { + fftStep(step, freq, time); + } + } + void ifft(size_t step, const Complex *freq, Complex *time) { + fftStep(plan[step], freq, time); + } + void ifft(const Sample *inR, const Sample *inI, Sample *outR, Sample *outI) { + for (auto &step : plan) { + fftStep(step, inR, inI, outR, outI); + } + } + void ifft(size_t step, const Sample *inR, const Sample *inI, Sample *outR, Sample *outI) { + fftStep(plan[step], inR, inI, outR, outI); + } +private: + using InnerFFT = Pow2FFT; + InnerFFT innerFFT; + + size_t innerSize, outerSize; + std::vector tmpFreq; + std::vector outerTwiddles; + std::vector outerTwiddlesR, outerTwiddlesI; + std::vector dftTwists, dftTmp; + + enum class StepType { + passthrough, + interleaveOrder2, interleaveOrder3, interleaveOrder4, interleaveOrder5, interleaveOrderN, + firstFFT, middleFFT, + twiddles, + finalOrder2, finalOrder3, finalOrder4, finalOrder5, finalOrderN + }; + struct Step { + StepType type; + size_t offset; + }; + std::vector plan; + + template + void fftStep(Step step, const Complex *time, Complex *freq) { + switch (step.type) { + case (StepType::passthrough): { + if (inverse) { + innerFFT.ifft(time, freq); + } else { + innerFFT.fft(time, freq); + } + break; + } + case (StepType::interleaveOrder2): { + _impl::interleaveCopy<2>(time, tmpFreq.data(), innerSize); + break; + } + case (StepType::interleaveOrder3): { + _impl::interleaveCopy<3>(time, tmpFreq.data(), innerSize); + break; + } + case (StepType::interleaveOrder4): { + _impl::interleaveCopy<4>(time, tmpFreq.data(), innerSize); + break; + } + case (StepType::interleaveOrder5): { + _impl::interleaveCopy<5>(time, tmpFreq.data(), innerSize); + break; + } + case (StepType::interleaveOrderN): { + _impl::interleaveCopy(time, tmpFreq.data(), outerSize, innerSize); + break; + } + case (StepType::firstFFT): { + if (inverse) { + innerFFT.ifft(tmpFreq.data(), freq); + } else { + innerFFT.fft(tmpFreq.data(), freq); + } + break; + } + case (StepType::middleFFT): { + Complex *offsetOut = freq + step.offset; + if (inverse) { + innerFFT.ifft(tmpFreq.data() + step.offset, offsetOut); + } else { + innerFFT.fft(tmpFreq.data() + step.offset, offsetOut); + } + break; + } + case (StepType::twiddles): { + if (inverse) { + _impl::complexMulConj(freq + innerSize, freq + innerSize, outerTwiddles.data(), innerSize*(outerSize - 1)); + } else { + _impl::complexMul(freq + innerSize, freq + innerSize, outerTwiddles.data(), innerSize*(outerSize - 1)); + } + break; + } + case StepType::finalOrder2: + finalPass2(freq); + break; + case StepType::finalOrder3: + finalPass3(freq); + break; + case StepType::finalOrder4: + finalPass4(freq); + break; + case StepType::finalOrder5: + finalPass5(freq); + break; + case StepType::finalOrderN: + finalPassN(freq); + break; + } + } + template + void fftStep(Step step, const Sample *inR, const Sample *inI, Sample *outR, Sample *outI) { + Sample *tmpR = (Sample *)tmpFreq.data(), *tmpI = tmpR + tmpFreq.size(); + switch (step.type) { + case (StepType::passthrough): { + if (inverse) { + innerFFT.ifft(inR, inI, outR, outI); + } else { + innerFFT.fft(inR, inI, outR, outI); + } + break; + } + case (StepType::interleaveOrder2): { + _impl::interleaveCopy<2>(inR, tmpR, innerSize); + _impl::interleaveCopy<2>(inI, tmpI, innerSize); + break; + } + case (StepType::interleaveOrder3): { + _impl::interleaveCopy<3>(inR, tmpR, innerSize); + _impl::interleaveCopy<3>(inI, tmpI, innerSize); + break; + } + case (StepType::interleaveOrder4): { + _impl::interleaveCopy<4>(inR, tmpR, innerSize); + _impl::interleaveCopy<4>(inI, tmpI, innerSize); + break; + } + case (StepType::interleaveOrder5): { + _impl::interleaveCopy<5>(inR, tmpR, innerSize); + _impl::interleaveCopy<5>(inI, tmpI, innerSize); + break; + } + case (StepType::interleaveOrderN): { + _impl::interleaveCopy(inR, inI, tmpR, tmpI, outerSize, innerSize); + break; + } + case (StepType::firstFFT): { + if (inverse) { + innerFFT.ifft(tmpR, tmpI, outR, outI); + } else { + innerFFT.fft(tmpR, tmpI, outR, outI); + } + break; + } + case (StepType::middleFFT): { + size_t offset = step.offset; + Sample *offsetOutR = outR + offset; + Sample *offsetOutI = outI + offset; + if (inverse) { + innerFFT.ifft(tmpR + offset, tmpI + offset, offsetOutR, offsetOutI); + } else { + innerFFT.fft(tmpR + offset, tmpI + offset, offsetOutR, offsetOutI); + } + break; + } + case(StepType::twiddles): { + auto *twiddlesR = outerTwiddlesR.data(); + auto *twiddlesI = outerTwiddlesI.data(); + if (inverse) { + _impl::complexMulConj(outR + innerSize, outI + innerSize, outR + innerSize, outI + innerSize, twiddlesR, twiddlesI, innerSize*(outerSize - 1)); + } else { + _impl::complexMul(outR + innerSize, outI + innerSize, outR + innerSize, outI + innerSize, twiddlesR, twiddlesI, innerSize*(outerSize - 1)); + } + break; + } + case StepType::finalOrder2: + finalPass2(outR, outI); + break; + case StepType::finalOrder3: + finalPass3(outR, outI); + break; + case StepType::finalOrder4: + finalPass4(outR, outI); + break; + case StepType::finalOrder5: + finalPass5(outR, outI); + break; + case StepType::finalOrderN: + finalPassN(outR, outI); + break; + } + } + + void finalPass2(Complex *f0) { + auto *f1 = f0 + innerSize; + for (size_t i = 0; i < innerSize; ++i) { + Complex a = f0[i], b = f1[i]; + f0[i] = a + b; + f1[i] = a - b; + } + } + void finalPass2(Sample *f0r, Sample *f0i) { + auto *f1r = f0r + innerSize; + auto *f1i = f0i + innerSize; + for (size_t i = 0; i < innerSize; ++i) { + Sample ar = f0r[i], ai = f0i[i]; + Sample br = f1r[i], bi = f1i[i]; + f0r[i] = ar + br; + f0i[i] = ai + bi; + f1r[i] = ar - br; + f1i[i] = ai - bi; + } + } + template + void finalPass3(Complex *f0) { + auto *f1 = f0 + innerSize; + auto *f2 = f0 + innerSize*2; + const Complex tw1{Sample(-0.5), Sample(-std::sqrt(0.75)*(inverse ? -1 : 1))}; + for (size_t i = 0; i < innerSize; ++i) { + Complex a = f0[i], b = f1[i], c = f2[i]; + Complex bc0 = b + c, bc1 = b - c; + f0[i] = a + bc0; + f1[i] = { + a.real() + bc0.real()*tw1.real() - bc1.imag()*tw1.imag(), + a.imag() + bc0.imag()*tw1.real() + bc1.real()*tw1.imag() + }; + f2[i] = { + a.real() + bc0.real()*tw1.real() + bc1.imag()*tw1.imag(), + a.imag() + bc0.imag()*tw1.real() - bc1.real()*tw1.imag() + }; + } + } + template + void finalPass3(Sample *f0r, Sample *f0i) { + auto *f1r = f0r + innerSize; + auto *f1i = f0i + innerSize; + auto *f2r = f0r + innerSize*2; + auto *f2i = f0i + innerSize*2; + const Sample tw1r = -0.5, tw1i = -std::sqrt(0.75)*(inverse ? -1 : 1); + + for (size_t i = 0; i < innerSize; ++i) { + Sample ar = f0r[i], ai = f0i[i], br = f1r[i], bi = f1i[i], cr = f2r[i], ci = f2i[i]; + + f0r[i] = ar + br + cr; + f0i[i] = ai + bi + ci; + f1r[i] = ar + br*tw1r - bi*tw1i + cr*tw1r + ci*tw1i; + f1i[i] = ai + bi*tw1r + br*tw1i - cr*tw1i + ci*tw1r; + f2r[i] = ar + br*tw1r + bi*tw1i + cr*tw1r - ci*tw1i; + f2i[i] = ai + bi*tw1r - br*tw1i + cr*tw1i + ci*tw1r; + } + } + template + void finalPass4(Complex *f0) { + auto *f1 = f0 + innerSize; + auto *f2 = f0 + innerSize*2; + auto *f3 = f0 + innerSize*3; + for (size_t i = 0; i < innerSize; ++i) { + Complex a = f0[i], b = f1[i], c = f2[i], d = f3[i]; + + Complex ac0 = a + c, ac1 = a - c; + Complex bd0 = b + d, bd1 = inverse ? (b - d) : (d - b); + Complex bd1i = {-bd1.imag(), bd1.real()}; + f0[i] = ac0 + bd0; + f1[i] = ac1 + bd1i; + f2[i] = ac0 - bd0; + f3[i] = ac1 - bd1i; + } + } + template + void finalPass4(Sample *f0r, Sample *f0i) { + auto *f1r = f0r + innerSize; + auto *f1i = f0i + innerSize; + auto *f2r = f0r + innerSize*2; + auto *f2i = f0i + innerSize*2; + auto *f3r = f0r + innerSize*3; + auto *f3i = f0i + innerSize*3; + for (size_t i = 0; i < innerSize; ++i) { + Sample ar = f0r[i], ai = f0i[i], br = f1r[i], bi = f1i[i], cr = f2r[i], ci = f2i[i], dr = f3r[i], di = f3i[i]; + + Sample ac0r = ar + cr, ac0i = ai + ci; + Sample ac1r = ar - cr, ac1i = ai - ci; + Sample bd0r = br + dr, bd0i = bi + di; + Sample bd1r = br - dr, bd1i = bi - di; + + f0r[i] = ac0r + bd0r; + f0i[i] = ac0i + bd0i; + f1r[i] = inverse ? (ac1r - bd1i) : (ac1r + bd1i); + f1i[i] = inverse ? (ac1i + bd1r) : (ac1i - bd1r); + f2r[i] = ac0r - bd0r; + f2i[i] = ac0i - bd0i; + f3r[i] = inverse ? (ac1r + bd1i) : (ac1r - bd1i); + f3i[i] = inverse ? (ac1i - bd1r) : (ac1i + bd1r); + } + } + template + void finalPass5(Complex *f0) { + auto *f1 = f0 + innerSize; + auto *f2 = f0 + innerSize*2; + auto *f3 = f0 + innerSize*3; + auto *f4 = f0 + innerSize*4; + const Sample tw1r = 0.30901699437494745; + const Sample tw1i = -0.9510565162951535*(inverse ? -1 : 1); + const Sample tw2r = -0.8090169943749473; + const Sample tw2i = -0.5877852522924732*(inverse ? -1 : 1); + for (size_t i = 0; i < innerSize; ++i) { + Complex a = f0[i], b = f1[i], c = f2[i], d = f3[i], e = f4[i]; + + Complex be0 = b + e, be1 = {e.imag() - b.imag(), b.real() - e.real()}; // (b - e)*i + Complex cd0 = c + d, cd1 = {d.imag() - c.imag(), c.real() - d.real()}; + + Complex bcde01 = be0*tw1r + cd0*tw2r; + Complex bcde02 = be0*tw2r + cd0*tw1r; + Complex bcde11 = be1*tw1i + cd1*tw2i; + Complex bcde12 = be1*tw2i - cd1*tw1i; + + f0[i] = a + be0 + cd0; + f1[i] = a + bcde01 + bcde11; + f2[i] = a + bcde02 + bcde12; + f3[i] = a + bcde02 - bcde12; + f4[i] = a + bcde01 - bcde11; + } + } + template + void finalPass5(Sample *f0r, Sample *f0i) { + auto *f1r = f0r + innerSize; + auto *f1i = f0i + innerSize; + auto *f2r = f0r + innerSize*2; + auto *f2i = f0i + innerSize*2; + auto *f3r = f0r + innerSize*3; + auto *f3i = f0i + innerSize*3; + auto *f4r = f0r + innerSize*4; + auto *f4i = f0i + innerSize*4; + + const Sample tw1r = 0.30901699437494745; + const Sample tw1i = -0.9510565162951535*(inverse ? -1 : 1); + const Sample tw2r = -0.8090169943749473; + const Sample tw2i = -0.5877852522924732*(inverse ? -1 : 1); + for (size_t i = 0; i < innerSize; ++i) { + Sample ar = f0r[i], ai = f0i[i], br = f1r[i], bi = f1i[i], cr = f2r[i], ci = f2i[i], dr = f3r[i], di = f3i[i], er = f4r[i], ei = f4i[i]; + + Sample be0r = br + er, be0i = bi + ei; + Sample be1r = ei - bi, be1i = br - er; + Sample cd0r = cr + dr, cd0i = ci + di; + Sample cd1r = di - ci, cd1i = cr - dr; + + Sample bcde01r = be0r*tw1r + cd0r*tw2r, bcde01i = be0i*tw1r + cd0i*tw2r; + Sample bcde02r = be0r*tw2r + cd0r*tw1r, bcde02i = be0i*tw2r + cd0i*tw1r; + Sample bcde11r = be1r*tw1i + cd1r*tw2i, bcde11i = be1i*tw1i + cd1i*tw2i; + Sample bcde12r = be1r*tw2i - cd1r*tw1i, bcde12i = be1i*tw2i - cd1i*tw1i; + + f0r[i] = ar + be0r + cd0r; + f0i[i] = ai + be0i + cd0i; + f1r[i] = ar + bcde01r + bcde11r; + f1i[i] = ai + bcde01i + bcde11i; + f2r[i] = ar + bcde02r + bcde12r; + f2i[i] = ai + bcde02i + bcde12i; + f3r[i] = ar + bcde02r - bcde12r; + f3i[i] = ai + bcde02i - bcde12i; + f4r[i] = ar + bcde01r - bcde11r; + f4i[i] = ai + bcde01i - bcde11i; + } + } + + template + void finalPassN(Complex *f0) { + for (size_t i = 0; i < innerSize; ++i) { + Complex *offsetFreq = f0 + i; + Complex sum = 0; + for (size_t i2 = 0; i2 < outerSize; ++i2) { + sum += (dftTmp[i2] = offsetFreq[i2*innerSize]); + } + offsetFreq[0] = sum; + + for (size_t f = 1; f < outerSize; ++f) { + Complex sum = dftTmp[0]; + + for (size_t i2 = 1; i2 < outerSize; ++i2) { + size_t twistIndex = (i2*f)%outerSize; + Complex twist = inverse ? std::conj(dftTwists[twistIndex]) : dftTwists[twistIndex]; + sum += Complex{ + dftTmp[i2].real()*twist.real() - dftTmp[i2].imag()*twist.imag(), + dftTmp[i2].imag()*twist.real() + dftTmp[i2].real()*twist.imag() + }; + } + + offsetFreq[f*innerSize] = sum; + } + } + } + template + void finalPassN(Sample *f0r, Sample *f0i) { + Sample *tmpR = (Sample *)dftTmp.data(), *tmpI = tmpR + outerSize; + + for (size_t i = 0; i < innerSize; ++i) { + Sample *offsetR = f0r + i; + Sample *offsetI = f0i + i; + Sample sumR = 0, sumI = 0; + for (size_t i2 = 0; i2 < outerSize; ++i2) { + sumR += (tmpR[i2] = offsetR[i2*innerSize]); + sumI += (tmpI[i2] = offsetI[i2*innerSize]); + } + offsetR[0] = sumR; + offsetI[0] = sumI; + + for (size_t f = 1; f < outerSize; ++f) { + Sample sumR = *tmpR, sumI = *tmpI; + + for (size_t i2 = 1; i2 < outerSize; ++i2) { + size_t twistIndex = (i2*f)%outerSize; + Complex twist = inverse ? std::conj(dftTwists[twistIndex]) : dftTwists[twistIndex]; + sumR += tmpR[i2]*twist.real() - tmpI[i2]*twist.imag(); + sumI += tmpI[i2]*twist.real() + tmpR[i2]*twist.imag(); + } + + offsetR[f*innerSize] = sumR; + offsetI[f*innerSize] = sumI; + } + } + } +}; + +template +using FFT = SplitFFT; + +// Wraps a complex FFT into a real one +template> +struct SimpleRealFFT { + using Complex = std::complex; + + static constexpr bool prefersSplit = ComplexFFT::prefersSplit; + + SimpleRealFFT(size_t size=0) { + resize(size); + } + + void resize(size_t size) { + complexFft.resize(size); + tmpTime.resize(size); + tmpFreq.resize(size); + } + + void fft(const Sample *time, Complex *freq) { + for (size_t i = 0; i < tmpTime.size(); ++i) { + tmpTime[i] = time[i]; + } + complexFft.fft(tmpTime.data(), tmpFreq.data()); + for (size_t i = 0; i < tmpFreq.size()/2; ++i) { + freq[i] = tmpFreq[i]; + } + freq[0] = { + tmpFreq[0].real(), + tmpFreq[tmpFreq.size()/2].real() + }; + } + void fft(const Sample *inR, Sample *outR, Sample *outI) { + Sample *tmpFreqR = (Sample *)tmpFreq.data(), *tmpFreqI = tmpFreqR + tmpFreq.size(); + for (size_t i = 0; i < tmpTime.size()/2; ++i) { + tmpTime[i] = 0; + } + complexFft.fft(inR, (const Sample *)tmpTime.data(), tmpFreqR, tmpFreqI); + for (size_t i = 0; i < tmpTime.size()/2; ++i) { + outR[i] = tmpFreqR[i]; + outI[i] = tmpFreqI[i]; + } + outI[0] = tmpFreqR[tmpFreq.size()/2]; + } + + void ifft(const Complex *freq, Sample *time) { + tmpFreq[0] = freq[0].real(); + tmpFreq[tmpFreq.size()/2] = freq[0].imag(); + for (size_t i = 1; i < tmpFreq.size()/2; ++i) { + tmpFreq[i] = freq[i]; + tmpFreq[tmpFreq.size() - i] = std::conj(freq[i]); + } + complexFft.ifft(tmpFreq.data(), tmpTime.data()); + for (size_t i = 0; i < tmpTime.size(); ++i) { + time[i] = tmpTime[i].real(); + } + } + void ifft(const Sample *inR, const Sample *inI, Sample *outR) { + Sample *tmpFreqR = (Sample *)tmpFreq.data(), *tmpFreqI = tmpFreqR + tmpFreq.size(); + tmpFreqR[0] = inR[0]; + tmpFreqR[tmpFreq.size()/2] = inI[0]; + tmpFreqI[0] = 0; + tmpFreqI[tmpFreq.size()/2] = 0; + for (size_t i = 1; i < tmpFreq.size()/2; ++i) { + tmpFreqR[i] = inR[i]; + tmpFreqI[i] = inI[i]; + tmpFreqR[tmpFreq.size() - i] = inR[i]; + tmpFreqI[tmpFreq.size() - i] = -inI[i]; + } + complexFft.ifft(tmpFreqR, tmpFreqI, outR, (Sample *)tmpTime.data()); + } + +private: + ComplexFFT complexFft; + std::vector tmpTime, tmpFreq; +}; + +/// A default power-of-2 FFT, specialised with platform-specific fast implementations where available +template +struct Pow2RealFFT : public SimpleRealFFT { + static constexpr bool prefersSplit = SimpleRealFFT::prefersSplit; + + using SimpleRealFFT::SimpleRealFFT; + + // Prevent copying, since it might be a problem for specialisations + Pow2RealFFT(const Pow2RealFFT &other) = delete; + // Pass move-constructor through, just to be explicit about it + Pow2RealFFT(Pow2RealFFT &&other) : SimpleRealFFT(std::move(other)) {} +}; + +/// A Real FFT which can handle multiples of 3 and 5, and can be computed in chunks +template +struct RealFFT { + using Complex = std::complex; + static constexpr bool prefersSplit = SplitFFT::prefersSplit; + + static size_t fastSizeAbove(size_t size) { + return ComplexFFT::fastSizeAbove((size + 1)/2)*2; + } + + RealFFT(size_t size=0) { + resize(size); + } + + void resize(size_t size) { + size_t hSize = size/2; + complexFft.resize(hSize); + tmpFreq.resize(hSize); + tmpTime.resize(hSize); + + twiddles.resize(hSize/2 + 1); + + if (!halfBinShift) { + for (size_t i = 0; i < twiddles.size(); ++i) { + Sample rotPhase = i*(-2*M_PI/size) - M_PI/2; // bake rotation by (-i) into twiddles + twiddles[i] = std::polar(Sample(1), rotPhase); + } + } else { + for (size_t i = 0; i < twiddles.size(); ++i) { + Sample rotPhase = (i + 0.5)*(-2*M_PI/size) - M_PI/2; + twiddles[i] = std::polar(Sample(1), rotPhase); + } + + halfBinTwists.resize(hSize); + for (size_t i = 0; i < hSize; ++i) { + Sample twistPhase = -2*M_PI*i/size; + halfBinTwists[i] = std::polar(Sample(1), twistPhase); + } + } + } + + size_t size() const { + return complexFft.size()*2; + } + size_t steps() const { + return complexFft.steps() + (splitComputation ? 3 : 2); + } + + void fft(const Sample *time, Complex *freq) { + for (size_t s = 0; s < steps(); ++s) { + fft(s, time, freq); + } + } + void fft(size_t step, const Sample *time, Complex *freq) { + if (complexPrefersSplit) { + size_t hSize = complexFft.size(); + Sample *tmpTimeR = (Sample *)tmpTime.data(), *tmpTimeI = tmpTimeR + hSize; + Sample *tmpFreqR = (Sample *)tmpFreq.data(), *tmpFreqI = tmpFreqR + hSize; + if (step-- == 0) { + size_t hSize = complexFft.size(); + if (halfBinShift) { + for (size_t i = 0; i < hSize; ++i) { + Sample tr = time[2*i], ti = time[2*i + 1]; + Complex twist = halfBinTwists[i]; + tmpTimeR[i] = tr*twist.real() - ti*twist.imag(); + tmpTimeI[i] = ti*twist.real() + tr*twist.imag(); + } + } else { + for (size_t i = 0; i < hSize; ++i) { + tmpTimeR[i] = time[2*i]; + tmpTimeI[i] = time[2*i + 1]; + } + } + } else if (step < complexFft.steps()) { + complexFft.fft(step, tmpTimeR, tmpTimeI, tmpFreqR, tmpFreqI); + } else { + if (!halfBinShift) { + Sample bin0r = tmpFreqR[0], bin0i = tmpFreqI[0]; + freq[0] = {bin0r + bin0i, bin0r - bin0i}; + } + + size_t startI = halfBinShift ? 0 : 1; + size_t endI = hSize/2 + 1; + if (splitComputation) { // Do this last twiddle in two halves + if (step == complexFft.steps()) { + endI = (startI + endI)/2; + } else { + startI = (startI + endI)/2; + } + } + for (size_t i = startI; i < endI; ++i) { + size_t conjI = halfBinShift ? (hSize - 1 - i) : (hSize - i); + Complex twiddle = twiddles[i]; + + Sample oddR = (tmpFreqR[i] + tmpFreqR[conjI])*Sample(0.5); + Sample oddI = (tmpFreqI[i] - tmpFreqI[conjI])*Sample(0.5); + Sample evenIR = (tmpFreqR[i] - tmpFreqR[conjI])*Sample(0.5); + Sample evenII = (tmpFreqI[i] + tmpFreqI[conjI])*Sample(0.5); + Sample evenRotMinusIR = evenIR*twiddle.real() - evenII*twiddle.imag(); + Sample evenRotMinusII = evenII*twiddle.real() + evenIR*twiddle.imag(); + + freq[i] = {oddR + evenRotMinusIR, oddI + evenRotMinusII}; + freq[conjI] = {oddR - evenRotMinusIR, evenRotMinusII - oddI}; + } + } + } else { + bool canUseTime = !halfBinShift && !(size_t(time)%alignof(Complex)); + if (step-- == 0) { + size_t hSize = complexFft.size(); + if (halfBinShift) { + for (size_t i = 0; i < hSize; ++i) { + Sample tr = time[2*i], ti = time[2*i + 1]; + Complex twist = halfBinTwists[i]; + tmpTime[i] = { + tr*twist.real() - ti*twist.imag(), + ti*twist.real() + tr*twist.imag() + }; + } + } else if (!canUseTime) { + std::memcpy(static_cast(tmpTime.data()), static_cast(time), sizeof(Complex)*hSize); + } + } else if (step < complexFft.steps()) { + complexFft.fft(step, canUseTime ? (const Complex *)time : tmpTime.data(), tmpFreq.data()); + } else { + if (!halfBinShift) { + Complex bin0 = tmpFreq[0]; + freq[0] = { // pack DC & Nyquist together + bin0.real() + bin0.imag(), + bin0.real() - bin0.imag() + }; + } + + size_t hSize = complexFft.size(); + size_t startI = halfBinShift ? 0 : 1; + size_t endI = hSize/2 + 1; + if (splitComputation) { // Do this last twiddle in two halves + if (step == complexFft.steps()) { + endI = (startI + endI)/2; + } else { + startI = (startI + endI)/2; + } + } + for (size_t i = startI; i < endI; ++i) { + size_t conjI = halfBinShift ? (hSize - 1 - i) : (hSize - i); + Complex twiddle = twiddles[i]; + + Complex odd = (tmpFreq[i] + std::conj(tmpFreq[conjI]))*Sample(0.5); + Complex evenI = (tmpFreq[i] - std::conj(tmpFreq[conjI]))*Sample(0.5); + Complex evenRotMinusI = { // twiddle includes a factor of -i + evenI.real()*twiddle.real() - evenI.imag()*twiddle.imag(), + evenI.imag()*twiddle.real() + evenI.real()*twiddle.imag() + }; + + freq[i] = odd + evenRotMinusI; + freq[conjI] = {odd.real() - evenRotMinusI.real(), evenRotMinusI.imag() - odd.imag()}; + } + } + } + } + void fft(const Sample *inR, Sample *outR, Sample *outI) { + for (size_t s = 0; s < steps(); ++s) { + fft(s, inR, outR, outI); + } + } + void fft(size_t step, const Sample *inR, Sample *outR, Sample *outI) { + size_t hSize = complexFft.size(); + Sample *tmpTimeR = (Sample *)tmpTime.data(), *tmpTimeI = tmpTimeR + hSize; + Sample *tmpFreqR = (Sample *)tmpFreq.data(), *tmpFreqI = tmpFreqR + hSize; + if (step-- == 0) { + size_t hSize = complexFft.size(); + if (halfBinShift) { + for (size_t i = 0; i < hSize; ++i) { + Sample tr = inR[2*i], ti = inR[2*i + 1]; + Complex twist = halfBinTwists[i]; + tmpTimeR[i] = tr*twist.real() - ti*twist.imag(); + tmpTimeI[i] = ti*twist.real() + tr*twist.imag(); + } + } else { + for (size_t i = 0; i < hSize; ++i) { + tmpTimeR[i] = inR[2*i]; + tmpTimeI[i] = inR[2*i + 1]; + } + } + } else if (step < complexFft.steps()) { + complexFft.fft(step, tmpTimeR, tmpTimeI, tmpFreqR, tmpFreqI); + } else { + if (!halfBinShift) { + Sample bin0r = tmpFreqR[0], bin0i = tmpFreqI[0]; + outR[0] = bin0r + bin0i; + outI[0] = bin0r - bin0i; + } + + size_t startI = halfBinShift ? 0 : 1; + size_t endI = hSize/2 + 1; + if (splitComputation) { // Do this last twiddle in two halves + if (step == complexFft.steps()) { + endI = (startI + endI)/2; + } else { + startI = (startI + endI)/2; + } + } + for (size_t i = startI; i < endI; ++i) { + size_t conjI = halfBinShift ? (hSize - 1 - i) : (hSize - i); + Complex twiddle = twiddles[i]; + + Sample oddR = (tmpFreqR[i] + tmpFreqR[conjI])*Sample(0.5); + Sample oddI = (tmpFreqI[i] - tmpFreqI[conjI])*Sample(0.5); + Sample evenIR = (tmpFreqR[i] - tmpFreqR[conjI])*Sample(0.5); + Sample evenII = (tmpFreqI[i] + tmpFreqI[conjI])*Sample(0.5); + Sample evenRotMinusIR = evenIR*twiddle.real() - evenII*twiddle.imag(); + Sample evenRotMinusII = evenII*twiddle.real() + evenIR*twiddle.imag(); + + outR[i] = oddR + evenRotMinusIR; + outI[i] = oddI + evenRotMinusII; + outR[conjI] = oddR - evenRotMinusIR; + outI[conjI] = evenRotMinusII - oddI; + } + } + } + + void ifft(const Complex *freq, Sample *time) { + for (size_t s = 0; s < steps(); ++s) { + ifft(s, freq, time); + } + } + void ifft(size_t step, const Complex *freq, Sample *time) { + if (complexPrefersSplit) { + size_t hSize = complexFft.size(); + Sample *tmpTimeR = (Sample *)tmpTime.data(), *tmpTimeI = tmpTimeR + hSize; + Sample *tmpFreqR = (Sample *)tmpFreq.data(), *tmpFreqI = tmpFreqR + hSize; + + bool splitFirst = splitComputation && (step-- == 0); + if (splitFirst || step-- == 0) { + Complex bin0 = freq[0]; + if (!halfBinShift) { + tmpFreqR[0] = bin0.real() + bin0.imag(); + tmpFreqI[0] = bin0.real() - bin0.imag(); + } + size_t startI = halfBinShift ? 0 : 1; + size_t endI = hSize/2 + 1; + if (splitComputation) { // Do this first twiddle in two halves + if (splitFirst) { + endI = (startI + endI)/2; + } else { + startI = (startI + endI)/2; + } + } + for (size_t i = startI; i < endI; ++i) { + size_t conjI = halfBinShift ? (hSize - 1 - i) : (hSize - i); + Complex twiddle = twiddles[i]; + + Complex odd = freq[i] + std::conj(freq[conjI]); + Complex evenRotMinusI = freq[i] - std::conj(freq[conjI]); + Complex evenI = { // Conjugate twiddle + evenRotMinusI.real()*twiddle.real() + evenRotMinusI.imag()*twiddle.imag(), + evenRotMinusI.imag()*twiddle.real() - evenRotMinusI.real()*twiddle.imag() + }; + + tmpFreqR[i] = odd.real() + evenI.real(); + tmpFreqI[i] = odd.imag() + evenI.imag(); + tmpFreqR[conjI] = odd.real() - evenI.real(); + tmpFreqI[conjI] = evenI.imag() - odd.imag(); + } + } else if (step < complexFft.steps()) { + complexFft.ifft(step, tmpFreqR, tmpFreqI, tmpTimeR, tmpTimeI); + } else { + size_t hSize = complexFft.size(); + if (halfBinShift) { + for (size_t i = 0; i < hSize; ++i) { + Sample tr = tmpTimeR[i], ti = tmpTimeI[i]; + Complex twist = halfBinTwists[i]; + time[2*i] = tr*twist.real() + ti*twist.imag(); + time[2*i + 1] = ti*twist.real() - tr*twist.imag(); + } + } else { + for (size_t i = 0; i < hSize; ++i) { + time[2*i] = tmpTimeR[i]; + time[2*i + 1] = tmpTimeI[i]; + } + } + } + } else { + bool canUseTime = !halfBinShift && !(size_t(time)%alignof(Complex)); + bool splitFirst = splitComputation && (step-- == 0); + if (splitFirst || step-- == 0) { + Complex bin0 = freq[0]; + if (!halfBinShift) { + tmpFreq[0] = { + bin0.real() + bin0.imag(), + bin0.real() - bin0.imag() + }; + } + size_t hSize = complexFft.size(); + size_t startI = halfBinShift ? 0 : 1; + size_t endI = hSize/2 + 1; + if (splitComputation) { // Do this first twiddle in two halves + if (splitFirst) { + endI = (startI + endI)/2; + } else { + startI = (startI + endI)/2; + } + } + for (size_t i = startI; i < endI; ++i) { + size_t conjI = halfBinShift ? (hSize - 1 - i) : (hSize - i); + Complex twiddle = twiddles[i]; + + Complex odd = freq[i] + std::conj(freq[conjI]); + Complex evenRotMinusI = freq[i] - std::conj(freq[conjI]); + Complex evenI = { // Conjugate twiddle + evenRotMinusI.real()*twiddle.real() + evenRotMinusI.imag()*twiddle.imag(), + evenRotMinusI.imag()*twiddle.real() - evenRotMinusI.real()*twiddle.imag() + }; + + tmpFreq[i] = odd + evenI; + tmpFreq[conjI] = {odd.real() - evenI.real(), evenI.imag() - odd.imag()}; + } + } else if (step < complexFft.steps()) { + // Can't just use time as (Complex *), since it might not be aligned properly + complexFft.ifft(step, tmpFreq.data(), canUseTime ? (Complex *)time : tmpTime.data()); + } else { + size_t hSize = complexFft.size(); + if (halfBinShift) { + for (size_t i = 0; i < hSize; ++i) { + Complex t = tmpTime[i]; + Complex twist = halfBinTwists[i]; + time[2*i] = t.real()*twist.real() + t.imag()*twist.imag(); + time[2*i + 1] = t.imag()*twist.real() - t.real()*twist.imag(); + } + } else if (!canUseTime) { + std::memcpy(static_cast(time), static_cast(tmpTime.data()), sizeof(Complex)*hSize); + } + } + } + } + void ifft(const Sample *inR, const Sample *inI, Sample *outR) { + for (size_t s = 0; s < steps(); ++s) { + ifft(s, inR, inI, outR); + } + } + void ifft(size_t step, const Sample *inR, const Sample *inI, Sample *outR) { + size_t hSize = complexFft.size(); + Sample *tmpTimeR = (Sample *)tmpTime.data(), *tmpTimeI = tmpTimeR + hSize; + Sample *tmpFreqR = (Sample *)tmpFreq.data(), *tmpFreqI = tmpFreqR + hSize; + + bool splitFirst = splitComputation && (step-- == 0); + if (splitFirst || step-- == 0) { + Sample bin0r = inR[0], bin0i = inI[0]; + if (!halfBinShift) { + tmpFreqR[0] = bin0r + bin0i; + tmpFreqI[0] = bin0r - bin0i; + } + size_t startI = halfBinShift ? 0 : 1; + size_t endI = hSize/2 + 1; + if (splitComputation) { // Do this first twiddle in two halves + if (splitFirst) { + endI = (startI + endI)/2; + } else { + startI = (startI + endI)/2; + } + } + for (size_t i = startI; i < endI; ++i) { + size_t conjI = halfBinShift ? (hSize - 1 - i) : (hSize - i); + Complex twiddle = twiddles[i]; + Sample fir = inR[i], fii = inI[i]; + Sample fcir = inR[conjI], fcii = inI[conjI]; + + Complex odd = {fir + fcir, fii - fcii}; + Complex evenRotMinusI = {fir - fcir, fii + fcii}; + Complex evenI = { // Conjugate twiddle + evenRotMinusI.real()*twiddle.real() + evenRotMinusI.imag()*twiddle.imag(), + evenRotMinusI.imag()*twiddle.real() - evenRotMinusI.real()*twiddle.imag() + }; + + tmpFreqR[i] = odd.real() + evenI.real(); + tmpFreqI[i] = odd.imag() + evenI.imag(); + tmpFreqR[conjI] = odd.real() - evenI.real(); + tmpFreqI[conjI] = evenI.imag() - odd.imag(); + } + } else if (step < complexFft.steps()) { + // Can't just use time as (Complex *), since it might not be aligned properly + complexFft.ifft(step, tmpFreqR, tmpFreqI, tmpTimeR, tmpTimeI); + } else { + if (halfBinShift) { + for (size_t i = 0; i < hSize; ++i) { + Sample tr = tmpTimeR[i], ti = tmpTimeI[i]; + Complex twist = halfBinTwists[i]; + outR[2*i] = tr*twist.real() + ti*twist.imag(); + outR[2*i + 1] = ti*twist.real() - tr*twist.imag(); + } + } else { + for (size_t i = 0; i < hSize; ++i) { + outR[2*i] = tmpTimeR[i]; + outR[2*i + 1] = tmpTimeI[i]; + } + } + } + } +private: + using ComplexFFT = SplitFFT; + ComplexFFT complexFft; + + static constexpr bool complexPrefersSplit = ComplexFFT::prefersSplit; + std::vector tmpFreq, tmpTime; + std::vector twiddles, halfBinTwists; +}; + +template +using ModifiedRealFFT = RealFFT; + +}} // namespace + +// Override `Pow2FFT` / `Pow2RealFFT` templates with faster implementations +#if defined(SIGNALSMITH_USE_PFFFT) || defined(SIGNALSMITH_USE_PFFFT_DOUBLE) +# if defined(SIGNALSMITH_USE_PFFFT) +# include "./platform/fft-pffft.h" +# endif +# if defined(SIGNALSMITH_USE_PFFFT_DOUBLE) +# include "./platform/fft-pffft-double.h" +# endif +#elif defined(SIGNALSMITH_USE_ACCELERATE) +# include "./platform/fft-accelerate.h" +#elif defined(SIGNALSMITH_USE_IPP) +# include "./platform/fft-ipp.h" +#elif defined(SIGNALSMITH_USE_CMSISDSP) +# include "./platform/fft-cmsisdsp.h" +#elif defined(SIGNALSMITH_USE_XSIMD) +// This is the same as the basic one +# include "./platform/fft-linear.h" +#endif + +#endif // include guard diff --git a/third_party/signalsmith/signalsmith-linear/stft.h b/third_party/signalsmith/signalsmith-linear/stft.h new file mode 100644 index 000000000..eaa1aa4d1 --- /dev/null +++ b/third_party/signalsmith/signalsmith-linear/stft.h @@ -0,0 +1,700 @@ +#ifndef SIGNALSMITH_AUDIO_LINEAR_STFT_H +#define SIGNALSMITH_AUDIO_LINEAR_STFT_H + +#include "./fft.h" + +namespace signalsmith { namespace linear { + +enum { + STFT_SPECTRUM_PACKED=0, + STFT_SPECTRUM_MODIFIED=1, + STFT_SPECTRUM_UNPACKED=2, +}; + +/// A self-normalising STFT, with variable position/window for output blocks +template +struct DynamicSTFT { + static constexpr bool modified = (spectrumType == STFT_SPECTRUM_MODIFIED); + static constexpr bool unpacked = (spectrumType == STFT_SPECTRUM_UNPACKED); + RealFFT fft; + + using Complex = std::complex; + + enum class WindowShape {ignore, acg, kaiser}; + static constexpr WindowShape acg = WindowShape::acg; + static constexpr WindowShape kaiser = WindowShape::kaiser; + enum class Normalisation {none, synthesis, roundTrip}; + static constexpr Normalisation normNone = Normalisation::none; + static constexpr Normalisation normSynthesis = Normalisation::synthesis; + static constexpr Normalisation normRoundTrip = Normalisation::roundTrip; + + void configure(size_t inChannels, size_t outChannels, size_t blockSamples, size_t extraInputHistory=0, size_t intervalSamples=0, Sample asymmetry=0, size_t fftMinSize=0) { + _analysisChannels = inChannels; + _synthesisChannels = outChannels; + _blockSamples = blockSamples; + if (!fftMinSize) fftMinSize = blockSamples; + _fftSamples = fft.fastSizeAbove((fftMinSize + 1)/2)*2; + fft.resize(_fftSamples); + _fftBins = _fftSamples/2 + (spectrumType == STFT_SPECTRUM_UNPACKED); + + _inputLengthSamples = _blockSamples + extraInputHistory; + input.buffer.resize(_inputLengthSamples*_analysisChannels); + + output.buffer.resize(_blockSamples*_synthesisChannels); + output.windowProducts.resize(_blockSamples); + spectrumBuffer.resize(_fftBins*std::max(_analysisChannels, _synthesisChannels)); + timeBuffer.resize(std::max(_fftSamples, _blockSamples)); + + _analysisWindow.resize(_blockSamples); + _synthesisWindow.resize(_blockSamples); + setInterval(intervalSamples ? intervalSamples : blockSamples/4, acg, asymmetry); + + reset(); + } + + size_t blockSamples() const { + return _blockSamples; + } + size_t fftSamples() const { + return _fftSamples; + } + size_t defaultInterval() const { + return _defaultInterval; + } + size_t bands() const { + return _fftBins; + } + size_t analysisLatency() const { + return _blockSamples - _analysisOffset; + } + size_t synthesisLatency() const { + return _synthesisOffset; + } + size_t latency() const { + return synthesisLatency() + analysisLatency(); + } + Sample binToFreq(Sample b) const { + return (modified ? b + Sample(0.5) : b)/_fftSamples; + } + Sample freqToBin(Sample f) const { + return modified ? f*_fftSamples - Sample(0.5) : f*_fftSamples; + } + + void reset(Sample productWeight=1) { + input.pos = _blockSamples; + output.pos = 0; + for (auto &v : input.buffer) v = 0; + for (auto &v : output.buffer) v = 0; + for (auto &v : spectrumBuffer) v = 0; + for (auto &v : output.windowProducts) v = 0; + if (norm != normNone) { + addWindowProduct(); + for (int i = int(_blockSamples) - int(_defaultInterval) - 1; i >= 0; --i) { + output.windowProducts[i] += output.windowProducts[i + _defaultInterval]; + } + } + for (auto &v : output.windowProducts) v = v*productWeight + almostZero; + moveOutput(_defaultInterval); // ready for first block immediately + } + + void writeInput(size_t channel, size_t offset, size_t length, const Sample *inputArray) { + Sample *buffer = input.buffer.data() + channel*_inputLengthSamples; + + size_t offsetPos = (input.pos + offset)%_inputLengthSamples; + size_t inputWrapIndex = _inputLengthSamples - offsetPos; + size_t chunk1 = std::min(length, inputWrapIndex); + for (size_t i = 0; i < chunk1; ++i) { + size_t i2 = offsetPos + i; + buffer[i2] = inputArray[i]; + } + for (size_t i = chunk1; i < length; ++i) { + size_t i2 = i + offsetPos -_inputLengthSamples; + buffer[i2] = inputArray[i]; + } + } + void writeInput(size_t channel, size_t length, const Sample *inputArray) { + writeInput(channel, 0, length, inputArray); + } + void moveInput(size_t samples, bool clearInput=false) { + if (clearInput) { + size_t inputWrapIndex = _inputLengthSamples - input.pos; + size_t chunk1 = std::min(samples, inputWrapIndex); + for (size_t c = 0; c < _analysisChannels; ++c) { + Sample *buffer = input.buffer.data() + c*_inputLengthSamples; + for (size_t i = 0; i < chunk1; ++i) { + size_t i2 = input.pos + i; + buffer[i2] = 0; + } + for (size_t i = chunk1; i < samples; ++i) { + size_t i2 = i + input.pos - _inputLengthSamples; + buffer[i2] = 0; + } + } + } + + input.pos = (input.pos + samples)%_inputLengthSamples; + _samplesSinceAnalysis += samples; + } + size_t samplesSinceAnalysis() const { + return _samplesSinceAnalysis; + } + + /// When no more synthesis is expected, let output taper away to 0 based on windowing. Otherwise, the output will be scaled as if there's just a very long block interval, which can exaggerate artefacts and numerical errors. You still can't read more than `blockSamples()` into the future. + void finishOutput(Sample strength=1, size_t offset=0) { + Sample maxWindowProduct = 0; + + size_t chunk1 = std::max(offset, std::min(_blockSamples, _blockSamples - output.pos)); + + for (size_t i = offset; i < chunk1; ++i) { + size_t i2 = output.pos + i; + Sample &wp = output.windowProducts[i2]; + maxWindowProduct = std::max(wp, maxWindowProduct); + wp += (maxWindowProduct - wp)*strength; + } + for (size_t i = chunk1; i < _blockSamples; ++i) { + size_t i2 = i + output.pos - _blockSamples; + Sample &wp = output.windowProducts[i2]; + maxWindowProduct = std::max(wp, maxWindowProduct); + wp += (maxWindowProduct - wp)*strength; + } + } + + void readOutput(size_t channel, size_t offset, size_t length, Sample *outputArray) { + Sample *buffer = output.buffer.data() + channel*_blockSamples; + size_t offsetPos = (output.pos + offset)%_blockSamples; + size_t outputWrapIndex = _blockSamples - offsetPos; + size_t chunk1 = std::min(length, outputWrapIndex); + for (size_t i = 0; i < chunk1; ++i) { + size_t i2 = offsetPos + i; + outputArray[i] = buffer[i2]/output.windowProducts[i2]; + } + for (size_t i = chunk1; i < length; ++i) { + size_t i2 = i + offsetPos - _blockSamples; + outputArray[i] = buffer[i2]/output.windowProducts[i2]; + } + } + void readOutput(size_t channel, size_t length, Sample *outputArray) { + return readOutput(channel, 0, length, outputArray); + } + void addOutput(size_t channel, size_t offset, size_t length, const Sample *newOutputArray) { + length = std::min(_blockSamples, length); + Sample *buffer = output.buffer.data() + channel*_blockSamples; + size_t offsetPos = (output.pos + offset)%_blockSamples; + size_t outputWrapIndex = _blockSamples - offsetPos; + size_t chunk1 = std::min(length, outputWrapIndex); + for (size_t i = 0; i < chunk1; ++i) { + size_t i2 = offsetPos + i; + buffer[i2] += newOutputArray[i]*output.windowProducts[i2]; + } + for (size_t i = chunk1; i < length; ++i) { + size_t i2 = i + offsetPos - _blockSamples; + buffer[i2] += newOutputArray[i]*output.windowProducts[i2]; + } + } + void addOutput(size_t channel, size_t length, const Sample *newOutputArray) { + return addOutput(channel, 0, length, newOutputArray); + } + void replaceOutput(size_t channel, size_t offset, size_t length, const Sample *newOutputArray) { + length = std::min(_blockSamples, length); + Sample *buffer = output.buffer.data() + channel*_blockSamples; + size_t offsetPos = (output.pos + offset)%_blockSamples; + size_t outputWrapIndex = _blockSamples - offsetPos; + size_t chunk1 = std::min(length, outputWrapIndex); + for (size_t i = 0; i < chunk1; ++i) { + size_t i2 = offsetPos + i; + buffer[i2] = newOutputArray[i]*output.windowProducts[i2]; + } + for (size_t i = chunk1; i < length; ++i) { + size_t i2 = i + offsetPos - _blockSamples; + buffer[i2] = newOutputArray[i]*output.windowProducts[i2]; + } + } + void replaceOutput(size_t channel, size_t length, const Sample *newOutputArray) { + return replaceOutput(channel, 0, length, newOutputArray); + } + void moveOutput(size_t samples) { + if (samples == 1) { // avoid all the loops/chunks if we can + for (size_t c = 0; c < _synthesisChannels; ++c) { + output.buffer[output.pos + c*_blockSamples] = 0; + } + output.windowProducts[output.pos] = almostZero; + if (++output.pos >= _blockSamples) output.pos = 0; + return; + } + // Zero the output buffer as we cross it + size_t outputWrapIndex = _blockSamples - output.pos; + size_t chunk1 = std::min(samples, outputWrapIndex); + for (size_t c = 0; c < _synthesisChannels; ++c) { + Sample *buffer = output.buffer.data() + c*_blockSamples; + for (size_t i = 0; i < chunk1; ++i) { + size_t i2 = output.pos + i; + buffer[i2] = 0; + } + for (size_t i = chunk1; i < samples; ++i) { + size_t i2 = i + output.pos - _blockSamples; + buffer[i2] = 0; + } + } + for (size_t i = 0; i < chunk1; ++i) { + size_t i2 = output.pos + i; + output.windowProducts[i2] = almostZero; + } + for (size_t i = chunk1; i < samples; ++i) { + size_t i2 = i + output.pos - _blockSamples; + output.windowProducts[i2] = almostZero; + } + output.pos = (output.pos + samples)%_blockSamples; + _samplesSinceSynthesis += samples; + } + size_t samplesSinceSynthesis() const { + return _samplesSinceSynthesis; + } + + Complex * spectrum(size_t channel) { + return spectrumBuffer.data() + channel*_fftBins; + } + const Complex * spectrum(size_t channel) const { + return spectrumBuffer.data() + channel*_fftBins; + } + + Sample * analysisWindow() { + return _analysisWindow.data(); + } + const Sample * analysisWindow() const { + return _analysisWindow.data(); + } + // Sets the centre index of the window + void analysisOffset(size_t offset) { + _analysisOffset = offset; + } + size_t analysisOffset() const { + return _analysisOffset; + } + Sample * synthesisWindow() { + return _synthesisWindow.data(); + } + const Sample * synthesisWindow() const { + return _synthesisWindow.data(); + } + // Sets the centre index of the window + void synthesisOffset(size_t offset) { + _synthesisOffset = offset; + } + size_t synthesisOffset() const { + return _synthesisOffset; + } + + void setInterval(size_t defaultInterval, WindowShape windowShape=WindowShape::ignore, Sample asymmetry=0) { + _defaultInterval = defaultInterval; + if (windowShape == WindowShape::ignore) return; + + if (windowShape == acg) { + auto window = ApproximateConfinedGaussian::withBandwidth(double(_blockSamples)/defaultInterval); + window.fill(_synthesisWindow, _blockSamples, asymmetry, false); + } else if (windowShape == kaiser) { + auto window = Kaiser::withBandwidth(double(_blockSamples)/defaultInterval, true); + window.fill(_synthesisWindow, _blockSamples, asymmetry, true); + } + + _analysisOffset = _synthesisOffset = _blockSamples/2; + if (_analysisChannels == 0) { + for (auto &v : _analysisWindow) v = 1; + } else if (asymmetry == 0) { + forcePerfectReconstruction(_synthesisWindow, _blockSamples, _defaultInterval); + for (size_t i = 0; i < _blockSamples; ++i) { + _analysisWindow[i] = _synthesisWindow[i]; + } + } else { + for (size_t i = 0; i < _blockSamples; ++i) { + _analysisWindow[i] = _synthesisWindow[_blockSamples - 1 - i]; + } + } + // Set offsets to peak's index + for (size_t i = 0; i < _blockSamples; ++i) { + if (_analysisWindow[i] > _analysisWindow[_analysisOffset]) _analysisOffset = i; + if (_synthesisWindow[i] > _synthesisWindow[_synthesisOffset]) _synthesisOffset = i; + } + } + void setNormalisation(Normalisation mode) { + norm = mode; + } + + void analyse(size_t samplesInPast=0) { + for (size_t s = 0; s < analyseSteps(); ++s) { + analyseStep(s, samplesInPast); + } + } + size_t analyseSteps() const { + return splitComputation ? _analysisChannels*(fft.steps() + 1) : _analysisChannels; + } + void analyseStep(size_t step, std::size_t samplesInPast=0) { + size_t fftSteps = splitComputation ? fft.steps() : 0; + size_t channel = step/(fftSteps + 1); + step -= channel*(fftSteps + 1); + + if (step-- == 0) { // extra step at start of each channel: copy windowed input into buffer + size_t offsetPos = (_inputLengthSamples*2 + input.pos - _blockSamples - samplesInPast)%_inputLengthSamples; + size_t inputWrapIndex = _inputLengthSamples - offsetPos; + size_t chunk1 = std::min(_analysisOffset, inputWrapIndex); + size_t chunk2 = std::max(_analysisOffset, std::min(_blockSamples, inputWrapIndex)); + + _samplesSinceAnalysis = samplesInPast; + size_t bufferSize = timeBuffer.size(); // Use this rather than FFT size to handle undersized-FFT case + Sample *buffer = input.buffer.data() + channel*_inputLengthSamples; + for (size_t i = 0; i < chunk1; ++i) { + Sample w = modified ? -_analysisWindow[i] : _analysisWindow[i]; + size_t ti = i + (bufferSize - _analysisOffset); + size_t bi = offsetPos + i; + timeBuffer[ti] = buffer[bi]*w; + } + for (size_t i = chunk1; i < _analysisOffset; ++i) { + Sample w = modified ? -_analysisWindow[i] : _analysisWindow[i]; + size_t ti = i + (bufferSize - _analysisOffset); + size_t bi = i + offsetPos - _inputLengthSamples; + timeBuffer[ti] = buffer[bi]*w; + } + for (size_t i = _analysisOffset; i < chunk2; ++i) { + Sample w = _analysisWindow[i]; + size_t ti = i - _analysisOffset; + size_t bi = offsetPos + i; + timeBuffer[ti] = buffer[bi]*w; + } + for (size_t i = chunk2; i < _blockSamples; ++i) { + Sample w = _analysisWindow[i]; + size_t ti = i - _analysisOffset; + size_t bi = i + offsetPos - _inputLengthSamples; + timeBuffer[ti] = buffer[bi]*w; + } + if (_fftSamples >= _blockSamples) { + for (size_t i = _blockSamples - _analysisOffset; i < _fftSamples - _analysisOffset; ++i) { + timeBuffer[i] = 0; + } + } else { // undersized FFT - move first part of analysis window forwards in the buffer + size_t delta = _blockSamples - _fftSamples; + for (size_t i = _fftSamples - _analysisOffset; i < _blockSamples - _analysisOffset; ++i) { + timeBuffer[i] += timeBuffer[delta + i]; + } + for (size_t i = _blockSamples - _analysisOffset; i < _fftSamples; ++i) { + timeBuffer[i] = timeBuffer[delta + i]; + } + } + if (hookTimeInput) hookTimeInput(hookContext, channel, timeBuffer.data(), _fftSamples - _analysisOffset, _fftSamples, _blockSamples - _analysisOffset); + if (splitComputation) return; + } + auto *spectrumPtr = spectrum(channel); + if (splitComputation) { + fft.fft(step, timeBuffer.data(), spectrumPtr); + if (unpacked && step == fft.steps() - 1) { + spectrumPtr[_fftBins - 1] = spectrumPtr[0].imag(); + spectrumPtr[0].imag(0); + } + } else { + fft.fft(timeBuffer.data(), spectrum(channel)); + if (unpacked) { + spectrumPtr[_fftBins - 1] = spectrumPtr[0].imag(); + spectrumPtr[0].imag(0); + } + } + } + + void synthesise() { + for (size_t s = 0; s < synthesiseSteps(); ++s) { + synthesiseStep(s); + } + } + size_t synthesiseSteps() const { + return splitComputation ? (_synthesisChannels*(fft.steps() + 1) + 1) : _synthesisChannels; + } + void synthesiseStep(size_t step) { + if (step == 0) { // Extra first step which adds in the effective gain for a pure analysis-synthesis cycle + addWindowProduct(); + if (splitComputation) return; + } + if (splitComputation) --step; + + size_t fftSteps = splitComputation ? fft.steps() : 0; + size_t channel = step/(fftSteps + 1); + step -= channel*(fftSteps + 1); + + auto *spectrumPtr = spectrum(channel); + if (unpacked && step == 0) { // re-pack + spectrumPtr[0].imag(spectrumPtr[_fftBins - 1].real()); + } + + if (splitComputation) { + if (step < fftSteps) { + fft.ifft(step, spectrumPtr, timeBuffer.data()); + return; + } + } else { + fft.ifft(spectrumPtr, timeBuffer.data()); + } + + // extra step after each channel's FFT + Sample *buffer = output.buffer.data() + channel*_blockSamples; + size_t outputWrapIndex = _blockSamples - output.pos; + size_t chunk1 = std::min(_synthesisOffset, outputWrapIndex); + size_t chunk2 = std::min(_blockSamples, std::max(_synthesisOffset, outputWrapIndex)); + + if (hookTimeOutput) hookTimeOutput(hookContext, channel, timeBuffer.data(), _fftSamples - _synthesisOffset, _fftSamples, _blockSamples - _synthesisOffset); + + // Use the FFT size (rather than `timeBuffer.size()`) to handle the undersized-FFT case + for (size_t i = 0; i < chunk1; ++i) { + Sample w = modified ? -_synthesisWindow[i] : _synthesisWindow[i]; + size_t ti = i + (_fftSamples - _synthesisOffset); + size_t bi = output.pos + i; + buffer[bi] += timeBuffer[ti]*w; + } + for (size_t i = chunk1; i < _synthesisOffset; ++i) { + Sample w = modified ? -_synthesisWindow[i] : _synthesisWindow[i]; + size_t ti = i + (_fftSamples - _synthesisOffset); + size_t bi = i + output.pos - _blockSamples; + buffer[bi] += timeBuffer[ti]*w; + } + for (size_t i = _synthesisOffset; i < chunk2; ++i) { + Sample w = _synthesisWindow[i]; + size_t ti = i - _synthesisOffset; + size_t bi = output.pos + i; + buffer[bi] += timeBuffer[ti]*w; + } + for (size_t i = chunk2; i < _blockSamples; ++i) { + Sample w = _synthesisWindow[i]; + size_t ti = i - _synthesisOffset; + size_t bi = i + output.pos - _blockSamples; + buffer[bi] += timeBuffer[ti]*w; + } + } + +#define COMPAT_SPELLING(name, alt) \ + template \ + auto alt(Args &&...args) -> decltype(name(std::forward(args)...)) { \ + return name(std::forward(args)...); \ + } + COMPAT_SPELLING(analyse, analyze) + COMPAT_SPELLING(analyseStep, analyzeStep) + COMPAT_SPELLING(analyseSteps, analyzeSteps) + COMPAT_SPELLING(synthesise, synthesize) + COMPAT_SPELLING(synthesiseStep, synthesizeStep) + COMPAT_SPELLING(synthesiseSteps, synthesizeSteps) + + /// Input (only available so we can save/restore the input state) + struct Input { + void swap(Input &other) { + std::swap(pos, other.pos); + std::swap(buffer, other.buffer); + } + + Input & operator=(const Input &other) { + pos = other.pos; + buffer.assign(other.buffer.begin(), other.buffer.end()); + return *this; + } + private: + friend struct DynamicSTFT; + size_t pos = 0; + std::vector buffer; + }; + Input input; + + /// Output (only available so we can save/restore the output state) + struct Output { + void swap(Output &other) { + std::swap(pos, other.pos); + std::swap(buffer, other.buffer); + std::swap(windowProducts, other.windowProducts); + } + + Output & operator=(const Output &other) { + pos = other.pos; + buffer.assign(other.buffer.begin(), other.buffer.end()); + windowProducts.assign(other.windowProducts.begin(), other.windowProducts.end()); + return *this; + } + private: + friend struct DynamicSTFT; + size_t pos = 0; + std::vector buffer; + std::vector windowProducts; + }; + Output output; + +protected: + void *hookContext = nullptr; + + /* 0 .... postEnd, ____, preStart .... preEnd */ + typedef void (*TimeHook)(void *, size_t channel, Sample *timeBuffer, size_t preStart, size_t preEnd, size_t postEnd); + TimeHook hookTimeInput = nullptr; + TimeHook hookTimeOutput = nullptr; + +private: + static constexpr Sample almostZero = 1e-30; + + size_t _analysisChannels, _synthesisChannels, _inputLengthSamples, _blockSamples, _fftSamples, _fftBins; + size_t _defaultInterval = 0; + + std::vector _analysisWindow, _synthesisWindow; + size_t _analysisOffset = 0, _synthesisOffset = 0; + + std::vector spectrumBuffer; + std::vector timeBuffer; + + size_t _samplesSinceSynthesis = 0, _samplesSinceAnalysis = 0; + + Normalisation norm = normRoundTrip; + void addWindowProduct() { + _samplesSinceSynthesis = 0; + + int windowShift = int(_synthesisOffset) - int(_analysisOffset); + size_t wMin = std::max(0, windowShift); + size_t wMax = std::min(_blockSamples, int(_blockSamples) + windowShift); + + Sample *windowProduct = output.windowProducts.data(); + size_t outputWrapIndex = _blockSamples - output.pos; + size_t chunk1 = std::min(wMax, std::max(wMin, outputWrapIndex)); + Sample scaling = _fftSamples; + if (norm == normRoundTrip) { + for (size_t i = wMin; i < chunk1; ++i) { + Sample wa = _analysisWindow[i - windowShift]; + Sample ws = _synthesisWindow[i]; + size_t bi = output.pos + i; + windowProduct[bi] += wa*ws*scaling; + } + for (size_t i = chunk1; i < wMax; ++i) { + Sample wa = _analysisWindow[i - windowShift]; + Sample ws = _synthesisWindow[i]; + size_t bi = i + output.pos - _blockSamples; + windowProduct[bi] += wa*ws*scaling; + } + } else if (norm == normSynthesis) { + for (size_t i = wMin; i < chunk1; ++i) { + Sample ws = _synthesisWindow[i]; + size_t bi = output.pos + i; + windowProduct[bi] += ws*scaling; + } + for (size_t i = chunk1; i < wMax; ++i) { + Sample ws = _synthesisWindow[i]; + size_t bi = i + output.pos - _blockSamples; + windowProduct[bi] += ws*scaling; + } + } else { + for (size_t i = wMin; i < chunk1; ++i) { + size_t bi = output.pos + i; + windowProduct[bi] = scaling; + } + for (size_t i = chunk1; i < wMax; ++i) { + size_t bi = i + output.pos - _blockSamples; + windowProduct[bi] = scaling; + } + } + } + + // Copied from DSP library `windows.h` + class Kaiser { + inline static double bessel0(double x) { + const double significanceLimit = 1e-4; + double result = 0; + double term = 1; + double m = 0; + while (term > significanceLimit) { + result += term; + ++m; + term *= (x*x)/(4*m*m); + } + + return result; + } + double beta; + double invB0; + + static double heuristicBandwidth(double bandwidth) { + return bandwidth + 8/((bandwidth + 3)*(bandwidth + 3)) + 0.25*std::max(3 - bandwidth, 0.0); + } + public: + Kaiser(double beta) : beta(beta), invB0(1/bessel0(beta)) {} + + static Kaiser withBandwidth(double bandwidth, bool heuristicOptimal=false) { + return Kaiser(bandwidthToBeta(bandwidth, heuristicOptimal)); + } + static double bandwidthToBeta(double bandwidth, bool heuristicOptimal=false) { + if (heuristicOptimal) { // Heuristic based on numerical search + bandwidth = heuristicBandwidth(bandwidth); + } + bandwidth = std::max(bandwidth, 2.0); + double alpha = std::sqrt(bandwidth*bandwidth*0.25 - 1); + return alpha*M_PI; + } + + template + void fill(Data &&data, size_t size, double warp, bool isForSynthesis) const { + double invSize = 1.0/size; + size_t offsetI = (size&1) ? 1 : (isForSynthesis ? 0 : 2); + for (size_t i = 0; i < size; ++i) { + double r = (2*i + offsetI)*invSize - 1; + r = (r + warp)/(1 + r*warp); + double arg = std::sqrt(1 - r*r); + data[i] = bessel0(beta*arg)*invB0; + } + if (warp) { + // Warp window vertically as well, to restore some width + for (size_t i = 0; i < size; ++i) { + data[i] *= std::sqrt((warp + 1)/(1 + warp*(2*data[i] - 1))); + } + } + } + }; + + class ApproximateConfinedGaussian { + double gaussianFactor; + + double gaussian(double x) const { + return std::exp(-x*x*gaussianFactor); + } + public: + static double bandwidthToSigma(double bandwidth) { + return 0.3/std::sqrt(bandwidth); + } + static ApproximateConfinedGaussian withBandwidth(double bandwidth) { + return ApproximateConfinedGaussian(bandwidthToSigma(bandwidth)); + } + + ApproximateConfinedGaussian(double sigma) : gaussianFactor(0.0625/(sigma*sigma)) {} + + /// Fills an arbitrary container + template + void fill(Data &&data, size_t size, double warp, bool isForSynthesis) const { + double invSize = 1.0/size; + double offsetScale = gaussian(1)/(gaussian(3) + gaussian(-1)); + double norm = 1/(gaussian(0) - 2*offsetScale*(gaussian(2))); + size_t offsetI = (size&1) ? 1 : (isForSynthesis ? 0 : 2); + for (size_t i = 0; i < size; ++i) { + double r = (2*i + offsetI)*invSize - 1; + r = (r + warp)/(1 + r*warp); + data[i] = norm*(gaussian(r) - offsetScale*(gaussian(r - 2) + gaussian(r + 2))); + } + if (warp) { + // Warp window vertically as well, to restore some width + for (size_t i = 0; i < size; ++i) { + data[i] *= std::sqrt((warp + 1)/(1 + warp*(2*data[i] - 1))); + } + } + } + }; + + template + void forcePerfectReconstruction(Data &&data, size_t windowLength, size_t interval) { + for (size_t i = 0; i < interval; ++i) { + double sum2 = 0; + for (size_t index = i; index < windowLength; index += interval) { + sum2 += data[index]*data[index]; + } + double factor = 1/std::sqrt(sum2); + for (size_t index = i; index < windowLength; index += interval) { + data[index] *= factor; + } + } + } +}; + +}} // namespace + +#endif // include guard diff --git a/third_party/signalsmith/signalsmith-stretch.h b/third_party/signalsmith/signalsmith-stretch.h new file mode 100644 index 000000000..cd13f2e98 --- /dev/null +++ b/third_party/signalsmith/signalsmith-stretch.h @@ -0,0 +1,1060 @@ +#ifndef SIGNALSMITH_STRETCH_H +#define SIGNALSMITH_STRETCH_H + +#include "signalsmith-linear/stft.h" // https://github.com/Signalsmith-Audio/linear + +#include +#include +#include +#include +#include +#include +#include + +namespace signalsmith { namespace stretch { + +namespace _impl { + template + static std::complex mul(const std::complex &a, const std::complex &b) { + return conjugateSecond ? std::complex{ + b.real()*a.real() + b.imag()*a.imag(), + b.real()*a.imag() - b.imag()*a.real() + } : std::complex{ + a.real()*b.real() - a.imag()*b.imag(), + a.real()*b.imag() + a.imag()*b.real() + }; + } + template + static V norm(const std::complex &a) { + V r = a.real(), i = a.imag(); + return r*r + i*i; + } +} + +template +struct SignalsmithStretch { + static constexpr size_t version[3] = {1, 3, 2}; + + SignalsmithStretch() : randomEngine(std::random_device{}()) {} + SignalsmithStretch(long seed) : randomEngine(seed) {} + + // The difference between the internal position (centre of a block) and the input samples you're supplying + int inputLatency() const { + return int(stft.analysisLatency()); + } + int outputLatency() const { + return int(stft.synthesisLatency() + _splitComputation*stft.defaultInterval()); + } + + void reset() { + stft.reset(0.1); + stashedInput = stft.input; + stashedOutput = stft.output; + + prevInputOffset = -1; + _channelBands.assign(_channelBands.size(), Band()); + silenceCounter = 0; + didSeek = false; + blockProcess = {}; + freqEstimateWeighted = freqEstimateWeight = 0; + } + + // Configures using a default preset + void presetDefault(int nChannels, Sample sampleRate, bool splitComputation=false) { + configure(nChannels, static_cast(sampleRate*0.12), static_cast(sampleRate*0.03), splitComputation); + } + void presetCheaper(int nChannels, Sample sampleRate, bool splitComputation=true) { + configure(nChannels, static_cast(sampleRate*0.1), static_cast(sampleRate*0.04), splitComputation); + } + + // Manual setup + void configure(int nChannels, int blockSamples, int intervalSamples, bool splitComputation=false) { + _splitComputation = splitComputation; + channels = nChannels; + stft.configure(channels, channels, blockSamples, intervalSamples + 1); + stft.setInterval(intervalSamples, stft.kaiser); + stft.reset(Sample(0.1)); + stashedInput = stft.input; + stashedOutput = stft.output; + + bands = int(stft.bands()); + _channelBands.assign(bands*channels, Band()); + + peaks.reserve(bands/2); + energy.resize(bands); + smoothedEnergy.resize(bands); + outputMap.resize(bands); + channelPredictions.resize(channels*bands); + + blockProcess = {}; + formantMetric.resize(bands + 2); + + tmpProcessBuffer.resize(blockSamples + intervalSamples); + tmpPreRollBuffer.resize(outputLatency()*channels); + } + // For querying the existing config + int blockSamples() const { + return int(stft.blockSamples()); + } + int intervalSamples() const { + return int(stft.defaultInterval()); + } + bool splitComputation() const { + return _splitComputation; + } + + /// Frequency multiplier, and optional tonality limit (as multiple of sample-rate) + void setTransposeFactor(Sample multiplier, Sample tonalityLimit=0) { + freqMultiplier = multiplier; + if (tonalityLimit > 0) { + freqTonalityLimit = tonalityLimit/std::sqrt(multiplier); // compromise between input and output limits + } else { + freqTonalityLimit = 1; + } + customFreqMap = nullptr; + } + void setTransposeSemitones(Sample semitones, Sample tonalityLimit=0) { + setTransposeFactor(std::pow(2, semitones/12), tonalityLimit); + } + // Sets a custom frequency map - should be monotonically increasing + void setFreqMap(std::function inputToOutput) { + customFreqMap = inputToOutput; + } + + void setFormantFactor(Sample multiplier, bool compensatePitch=false) { + formantMultiplier = multiplier; + invFormantMultiplier = 1/multiplier; + formantCompensation = compensatePitch; + } + void setFormantSemitones(Sample semitones, bool compensatePitch=false) { + setFormantFactor(std::pow(2, semitones/12), compensatePitch); + } + // Rough guesstimate of the fundamental frequency, used for formant analysis. 0 means attempting to detect the pitch + void setFormantBase(Sample baseFreq=0) { + formantBaseFreq = baseFreq; + } + + // Provide previous input ("pre-roll") to smoothly change the input location without interrupting the output. This doesn't do any calculation, just copies intput to a buffer. + // You should ideally feed it `seekLength()` frames of input, unless it's directly after a `.reset()` (in which case `.outputSeek()` might be a better choice) + template + void seek(Inputs &&inputs, int inputSamples, double playbackRate) { + tmpProcessBuffer.resize(0); + tmpProcessBuffer.resize(stft.blockSamples() + stft.defaultInterval()); + + int startIndex = std::max(0, inputSamples - int(tmpProcessBuffer.size())); // start position in input + int padStart = int(tmpProcessBuffer.size() + startIndex) - inputSamples; // start position in tmpProcessBuffer + + Sample totalEnergy = 0; + for (int c = 0; c < channels; ++c) { + auto &&inputChannel = inputs[c]; + for (int i = startIndex; i < inputSamples; ++i) { + Sample s = inputChannel[i]; + totalEnergy += s*s; + tmpProcessBuffer[i - startIndex + padStart] = s; + } + + stft.writeInput(c, tmpProcessBuffer.size(), tmpProcessBuffer.data()); + } + stft.moveInput(tmpProcessBuffer.size()); + if (totalEnergy >= noiseFloor) { + silenceCounter = 0; + silenceFirst = true; + } + didSeek = true; + seekTimeFactor = (playbackRate*stft.defaultInterval() > 1) ? 1/playbackRate : stft.defaultInterval(); + } + int seekLength() const { + return int(stft.blockSamples() + stft.defaultInterval()); + } + + // Moves the input position *and* pre-calculates some output, so that the next samples returned from `.process()` are aligned to the beginning of the sample. + // The time-stretch rate is inferred from `inputLength`, so use `.outputSeekLength()` to get a correct value for that. + template + void outputSeek(Inputs &&inputs, int inputLength) { + // TODO: add fade-out parameter to avoid clicks, instead of doing a full reset + reset(); + // Assume we've been handed enough surplus input to produce `outputLatency()` samples of pre-roll + int surplusInput = std::max(inputLength - inputLatency(), 0); + Sample playbackRate = surplusInput/Sample(outputLatency()); + + // Move the input position to the start of the sound + int seekSamples = inputLength - surplusInput; + seek(inputs, seekSamples, playbackRate); + + tmpPreRollBuffer.resize(outputLatency()*channels); + struct BufferOutput { + Sample *samples; + int length; + + Sample * operator[](int c) { + return samples + c*length; + } + } preRollOutput{tmpPreRollBuffer.data(), outputLatency()}; + + // Use the surplus input to produce pre-roll output + OffsetIO offsetInput{inputs, seekSamples}; + process(offsetInput, surplusInput, preRollOutput, preRollOutput.length); + + // put the thing down, flip it and reverse it + for (auto &v : tmpPreRollBuffer) v = -v; + for (int c = 0; c < channels; ++c) { + std::reverse(preRollOutput[c], preRollOutput[c] + preRollOutput.length); + stft.addOutput(c, preRollOutput.length, preRollOutput[c]); + } + } + int outputSeekLength(Sample playbackRate) const { + return inputLatency() + playbackRate*outputLatency(); + } + + template + void process(Inputs &&inputs, int inputSamples, Outputs &&outputs, int outputSamples) { +#ifdef SIGNALSMITH_STRETCH_PROFILE_PROCESS_START + SIGNALSMITH_STRETCH_PROFILE_PROCESS_START(inputSamples, outputSamples); +#endif + int prevCopiedInput = 0; + auto copyInput = [&](int toIndex){ + + int length = std::min(int(stft.blockSamples() + stft.defaultInterval()), toIndex - prevCopiedInput); + tmpProcessBuffer.resize(length); + int offset = toIndex - length; + for (int c = 0; c < channels; ++c) { + auto &&inputBuffer = inputs[c]; + for (int i = 0; i < length; ++i) { + tmpProcessBuffer[i] = inputBuffer[i + offset]; + } + stft.writeInput(c, length, tmpProcessBuffer.data()); + } + stft.moveInput(length); + prevCopiedInput = toIndex; + }; + + Sample totalEnergy = 0; + for (int c = 0; c < channels; ++c) { + auto &&inputChannel = inputs[c]; + for (int i = 0; i < inputSamples; ++i) { + Sample s = inputChannel[i]; + totalEnergy += s*s; + } + } + + if (totalEnergy < noiseFloor) { + if (silenceCounter >= 2*stft.blockSamples()) { + if (silenceFirst) { // first block of silence processing + silenceFirst = false; + //stft.reset(); + blockProcess = {}; + for (auto &b : _channelBands) { + b.input = b.prevInput = b.output = 0; + b.inputEnergy = 0; + } + } + + if (inputSamples > 0) { + // copy from the input, wrapping around if needed + for (int outputIndex = 0; outputIndex < outputSamples; ++outputIndex) { + int inputIndex = outputIndex%inputSamples; + for (int c = 0; c < channels; ++c) { + outputs[c][outputIndex] = inputs[c][inputIndex]; + } + } + } else { + for (int c = 0; c < channels; ++c) { + auto &&outputChannel = outputs[c]; + for (int outputIndex = 0; outputIndex < outputSamples; ++outputIndex) { + outputChannel[outputIndex] = 0; + } + } + } + + // Store input in history buffer + copyInput(inputSamples); + return; + } else { + silenceCounter += inputSamples; + } + } else { + silenceCounter = 0; + silenceFirst = true; + } + + for (int outputIndex = 0; outputIndex < outputSamples; ++outputIndex) { + bool newBlock = blockProcess.samplesSinceLast >= stft.defaultInterval(); + if (newBlock) { + blockProcess.step = 0; + blockProcess.steps = 0; // how many processing steps this block will have + blockProcess.samplesSinceLast = 0; + + // Time to process a spectrum! Where should it come from in the input? + int inputOffset = static_cast(std::round(outputIndex*Sample(inputSamples)/outputSamples)); + int inputInterval = inputOffset - prevInputOffset; + prevInputOffset = inputOffset; + + copyInput(inputOffset); + stashedInput = stft.input; // save the input state, since that's what we'll analyse later + if (_splitComputation) { + stashedOutput = stft.output; // save the current output, and read from it + stft.moveOutput(stft.defaultInterval()); // the actual input jumps forward in time by one interval, ready for the synthesis + } + + blockProcess.newSpectrum = didSeek || (inputInterval > 0); + blockProcess.mappedFrequencies = customFreqMap || freqMultiplier != 1; + if (blockProcess.newSpectrum) { + // make sure the previous input is the correct distance in the past (give or take 1 sample) + blockProcess.reanalysePrev = didSeek || std::abs(inputInterval - int(stft.defaultInterval())) > 1; + if (blockProcess.reanalysePrev) blockProcess.steps += stft.analyseSteps() + 1; + + // analyse a new input + blockProcess.steps += stft.analyseSteps() + 1; + } + + blockProcess.processFormants = formantMultiplier != 1 || (formantCompensation && blockProcess.mappedFrequencies); + + blockProcess.timeFactor = didSeek ? seekTimeFactor : stft.defaultInterval()/static_cast(std::max(1, inputInterval)); + didSeek = false; + + updateProcessSpectrumSteps(); + blockProcess.steps += processSpectrumSteps; + + blockProcess.steps += stft.synthesiseSteps() + 1; + } + + size_t processToStep = newBlock ? blockProcess.steps : 0; + if (_splitComputation) { + Sample processRatio = Sample(blockProcess.samplesSinceLast + 1)/stft.defaultInterval(); + processToStep = std::min(blockProcess.steps, static_cast((blockProcess.steps + 0.999f)*processRatio)); + } + + while (blockProcess.step < processToStep) { + size_t step = blockProcess.step++; +#ifdef SIGNALSMITH_STRETCH_PROFILE_PROCESS_STEP + SIGNALSMITH_STRETCH_PROFILE_PROCESS_STEP(step, blockProcess.steps); +#endif + if (blockProcess.newSpectrum) { + if (blockProcess.reanalysePrev) { + // analyse past input + if (step < stft.analyseSteps()) { + stashedInput.swap(stft.input); + stft.analyseStep(step, stft.defaultInterval()); + stashedInput.swap(stft.input); + continue; + } + step -= stft.analyseSteps(); + if (step < 1) { + // Copy previous analysis to our band objects + for (int c = 0; c < channels; ++c) { + auto channelBands = bandsForChannel(c); + auto *spectrumBands = stft.spectrum(c); + for (int b = 0; b < bands; ++b) { + channelBands[b].prevInput = spectrumBands[b]; + } + } + continue; + } + step -= 1; + } + + // Analyse latest (stashed) input + if (step < stft.analyseSteps()) { + stashedInput.swap(stft.input); + stft.analyseStep(step); + stashedInput.swap(stft.input); + continue; + } + step -= stft.analyseSteps(); + if (step < 1) { + // Copy analysed spectrum into our band objects + for (int c = 0; c < channels; ++c) { + auto channelBands = bandsForChannel(c); + auto *spectrumBands = stft.spectrum(c); + for (int b = 0; b < bands; ++b) { + channelBands[b].input = spectrumBands[b]; + } + } + continue; + } + step -= 1; + } + + if (step < processSpectrumSteps) { + processSpectrum(step); + continue; + } + step -= processSpectrumSteps; + + if (step < 1) { + // Copy band objects into spectrum + for (int c = 0; c < channels; ++c) { + auto channelBands = bandsForChannel(c); + auto *spectrumBands = stft.spectrum(c); + for (int b = 0; b < bands; ++b) { + spectrumBands[b] = channelBands[b].output; + } + } + continue; + } + step -= 1; + + if (step < stft.synthesiseSteps()) { + stft.synthesiseStep(step); + continue; + } + } +#ifdef SIGNALSMITH_STRETCH_PROFILE_PROCESS_ENDSTEP + SIGNALSMITH_STRETCH_PROFILE_PROCESS_ENDSTEP(); +#endif + + ++blockProcess.samplesSinceLast; + if (_splitComputation) stashedOutput.swap(stft.output); + for (int c = 0; c < channels; ++c) { + auto &&outputChannel = outputs[c]; + Sample v = 0; + stft.readOutput(c, 1, &v); + outputChannel[outputIndex] = v; + } + stft.moveOutput(1); + if (_splitComputation) stashedOutput.swap(stft.output); + } + + copyInput(inputSamples); + prevInputOffset -= inputSamples; +#ifdef SIGNALSMITH_STRETCH_PROFILE_PROCESS_END + SIGNALSMITH_STRETCH_PROFILE_PROCESS_END(); +#endif + } + + // Read the remaining output, providing no further input. If `outputSamples` is more than one interval, it will compute additional blocks assuming a zero-valued input + template + void flush(Outputs &&outputs, int outputSamples, Sample playbackRate=0) { + struct Zeros { + struct Channel { + Sample operator[](int) { + return 0; + } + }; + Channel operator[](int) { + return {}; + } + } zeros; + // If we're asked for more than an interval of extra output, then zero-pad the input + int outputBlock = std::max(0, outputSamples - stft.defaultInterval()); + if (outputBlock > 0) process(zeros, outputBlock*playbackRate, outputs, outputBlock); + + int tailSamples = outputSamples - outputBlock; // at most one interval + tmpProcessBuffer.resize(tailSamples); + stft.finishOutput(1); + for (int c = 0; c < channels; ++c) { + stft.readOutput(c, tailSamples, tmpProcessBuffer.data()); + auto &&outputChannel = outputs[c]; + for (int i = 0; i < tailSamples; ++i) { + outputChannel[outputBlock + i] = tmpProcessBuffer[i]; + } + stft.readOutput(c, tailSamples, tailSamples, tmpProcessBuffer.data()); + for (int i = 0; i < tailSamples; ++i) { + outputChannel[outputBlock + tailSamples - 1 - i] -= tmpProcessBuffer[i]; + } + } + stft.reset(0.1f); + // Reset the phase-vocoder stuff, so the next block gets a fresh start + for (int c = 0; c < channels; ++c) { + auto channelBands = bandsForChannel(c); + for (int b = 0; b < bands; ++b) { + channelBands[b].prevInput = channelBands[b].output = 0; + } + } + } + + // Process a complete audio buffer all in one go + template + bool exact(Inputs &&inputs, int inputSamples, Outputs &&outputs, int outputSamples) { + Sample playbackRate = inputSamples/Sample(outputSamples); + auto seekLength = outputSeekLength(playbackRate); + if (inputSamples < seekLength) { + // to short for this - zero the output just to be polite + for (int c = 0; c < channels; ++c) { + auto &&channel = outputs[c]; + for (int i = 0; i < outputSamples; ++i) { + channel[i] = 0; + } + } + return false; + } + + outputSeek(inputs, seekLength); + + int outputIndex = outputSamples - seekLength/playbackRate; + OffsetIO offsetInput{inputs, seekLength}; + process(offsetInput, inputSamples - seekLength, outputs, outputIndex); + + OffsetIO offsetOutput{outputs, outputIndex}; + flush(offsetOutput, outputSamples - outputIndex, playbackRate); + return true; + } + +private: + bool _splitComputation = false; + struct { + size_t samplesSinceLast = std::numeric_limits::max(); + size_t steps = 0; + size_t step = 0; + + bool newSpectrum = false; + bool reanalysePrev = false; + bool mappedFrequencies = false; + bool processFormants = false; + Sample timeFactor; + } blockProcess; + + using Complex = std::complex; + static constexpr Sample noiseFloor{Sample(1e-15)}; + static constexpr Sample maxCleanStretch{2}; // time-stretch ratio before we start randomising phases + size_t silenceCounter = 0; + bool silenceFirst = true; + + Sample freqMultiplier = 1, freqTonalityLimit = 0.5; + std::function customFreqMap = nullptr; + + bool formantCompensation = false; // compensate for pitch/freq change + Sample formantMultiplier = 1, invFormantMultiplier = 1; + + using STFT = signalsmith::linear::DynamicSTFT; + STFT stft; + typename STFT::Input stashedInput; + typename STFT::Output stashedOutput; + + std::vector tmpProcessBuffer, tmpPreRollBuffer; + + int channels = 0, bands = 0; + int prevInputOffset = -1; + bool didSeek = false; + Sample seekTimeFactor = 1; + + Sample bandToFreq(Sample b) const { + return stft.binToFreq(b); + } + Sample freqToBand(Sample f) const { + return stft.freqToBin(f); + } + + struct Band { + Complex input, prevInput{0}; + Complex output{0}; + Sample inputEnergy; + }; + std::vector _channelBands; + Band * bandsForChannel(int channel) { + return _channelBands.data() + channel*bands; + } + template + Complex getBand(int channel, int index) { + if (index < 0 || index >= bands) return 0; + return _channelBands[index + channel*bands].*member; + } + template + Complex getFractional(int channel, int lowIndex, Sample fractional) { + Complex low = getBand(channel, lowIndex); + Complex high = getBand(channel, lowIndex + 1); + return low + (high - low)*fractional; + } + template + Complex getFractional(int channel, Sample inputIndex) { + int lowIndex = static_cast(std::floor(inputIndex)); + Sample fracIndex = inputIndex - lowIndex; + return getFractional(channel, lowIndex, fracIndex); + } + template + Sample getBand(int channel, int index) { + if (index < 0 || index >= bands) return 0; + return _channelBands[index + channel*bands].*member; + } + template + Sample getFractional(int channel, int lowIndex, Sample fractional) { + Sample low = getBand(channel, lowIndex); + Sample high = getBand(channel, lowIndex + 1); + return low + (high - low)*fractional; + } + template + Sample getFractional(int channel, Sample inputIndex) { + int lowIndex = std::floor(inputIndex); + Sample fracIndex = inputIndex - lowIndex; + return getFractional(channel, lowIndex, fracIndex); + } + + struct Peak { + Sample input, output; + }; + std::vector peaks; + std::vector energy, smoothedEnergy; + struct PitchMapPoint { + Sample inputBin, freqGrad; + }; + std::vector outputMap; + + struct Prediction { + Sample energy = 0; + Complex input; + + Complex makeOutput(Complex phase) { + Sample phaseNorm = _impl::norm(phase); + if (phaseNorm <= noiseFloor) { + phase = input; // prediction is too weak, fall back to the input + phaseNorm = _impl::norm(input) + noiseFloor; + } + return phase*std::sqrt(energy/phaseNorm); + } + }; + std::vector channelPredictions; + Prediction * predictionsForChannel(int c) { + return channelPredictions.data() + c*bands; + } + + // If RandomEngine=void, use std::default_random_engine; + using RandomEngineImpl = typename std::conditional< + std::is_void::value, + std::default_random_engine, + RandomEngine + >::type; + RandomEngineImpl randomEngine; + + size_t processSpectrumSteps = 0; + static constexpr size_t splitMainPrediction = 8; // it's just heavy, since we're blending up to 4 different phase predictions + void updateProcessSpectrumSteps() { + processSpectrumSteps = 0; + if (blockProcess.newSpectrum) processSpectrumSteps += channels; + if (blockProcess.mappedFrequencies) { + processSpectrumSteps += smoothEnergySteps; + processSpectrumSteps += 1; // findPeaks + } + processSpectrumSteps += 1; // updating the output map + processSpectrumSteps += channels; // preliminary phase-vocoder prediction + processSpectrumSteps += splitMainPrediction; + if (blockProcess.newSpectrum) processSpectrumSteps += 1; // .input -> .prevInput + if (blockProcess.processFormants) processSpectrumSteps += 3; + } + void processSpectrum(size_t step) { + Sample timeFactor = blockProcess.timeFactor; + + Sample smoothingBins = Sample(stft.fftSamples())/stft.defaultInterval(); + int longVerticalStep = static_cast(std::round(smoothingBins)); + timeFactor = std::max(timeFactor, 1/maxCleanStretch); + bool randomTimeFactor = (timeFactor > maxCleanStretch); + std::uniform_real_distribution timeFactorDist(maxCleanStretch*2*randomTimeFactor - timeFactor, timeFactor); + + if (blockProcess.newSpectrum) { + if (step < size_t(channels)) { + int channel = int(step); + auto bins = bandsForChannel(channel); + + Complex rot = std::polar(Sample(1), bandToFreq(0)*stft.defaultInterval()*Sample(2*M_PI)); + Sample freqStep = bandToFreq(1) - bandToFreq(0); + Complex rotStep = std::polar(Sample(1), freqStep*stft.defaultInterval()*Sample(2*M_PI)); + + for (int b = 0; b < bands; ++b) { + auto &bin = bins[b]; + bin.output = _impl::mul(bin.output, rot); + bin.prevInput = _impl::mul(bin.prevInput, rot); + rot = _impl::mul(rot, rotStep); + } + return; + } + step -= channels; + } + if (blockProcess.mappedFrequencies) { + if (step < smoothEnergySteps) { + smoothEnergy(step, smoothingBins); + return; + } + step -= smoothEnergySteps; + if (step-- == 0) { + findPeaks(); + return; + } + } + if (step-- == 0) { + if (blockProcess.mappedFrequencies) { + updateOutputMap(); + } else { // we're not pitch-shifting, so no need to find peaks etc. + for (int c = 0; c < channels; ++c) { + Band *bins = bandsForChannel(c); + for (int b = 0; b < bands; ++b) { + bins[b].inputEnergy = _impl::norm(bins[b].input); + } + } + + for (int b = 0; b < bands; ++b) { + outputMap[b] = {Sample(b), 1}; + } + } + return; + } + if (blockProcess.processFormants) { + if (step < 3) { + updateFormants(step); + return; + } + step -= 3; + } + // Preliminary output prediction from phase-vocoder + if (step < size_t(channels)) { + int c = int(step); + Band *bins = bandsForChannel(c); + auto *predictions = predictionsForChannel(c); + for (int b = 0; b < bands; ++b) { + auto mapPoint = outputMap[b]; + int lowIndex = static_cast(std::floor(mapPoint.inputBin)); + Sample fracIndex = mapPoint.inputBin - lowIndex; + + Prediction &prediction = predictions[b]; + Sample prevEnergy = prediction.energy; + prediction.energy = getFractional<&Band::inputEnergy>(c, lowIndex, fracIndex); + prediction.energy *= std::max(0, mapPoint.freqGrad); // scale the energy according to local stretch factor + prediction.input = getFractional<&Band::input>(c, lowIndex, fracIndex); + + auto &outputBin = bins[b]; + Complex prevInput = getFractional<&Band::prevInput>(c, lowIndex, fracIndex); + Complex freqTwist = _impl::mul(prediction.input, prevInput); + Complex phase = _impl::mul(outputBin.output, freqTwist); + outputBin.output = phase/(std::max(prevEnergy, prediction.energy) + noiseFloor); + } + return; + } + step -= channels; + + if (step < splitMainPrediction) { + // Re-predict using phase differences between frequencies + size_t chunk = step; + int startB = int(bands*chunk/splitMainPrediction); + int endB = int(bands*(chunk + 1)/splitMainPrediction); + for (int b = startB; b < endB; ++b) { + // Find maximum-energy channel and calculate that + int maxChannel = 0; + Sample maxEnergy = predictionsForChannel(0)[b].energy; + for (int c = 1; c < channels; ++c) { + Sample e = predictionsForChannel(c)[b].energy; + if (e > maxEnergy) { + maxChannel = c; + maxEnergy = e; + } + } + + auto *predictions = predictionsForChannel(maxChannel); + auto &prediction = predictions[b]; + auto *bins = bandsForChannel(maxChannel); + auto &outputBin = bins[b]; + + Complex phase = 0; + auto mapPoint = outputMap[b]; + + // Upwards vertical steps + if (b > 0) { + Sample binTimeFactor = randomTimeFactor ? timeFactorDist(randomEngine) : timeFactor; + Complex downInput = getFractional<&Band::input>(maxChannel, mapPoint.inputBin - binTimeFactor); + Complex shortVerticalTwist = _impl::mul(prediction.input, downInput); + + auto &downBin = bins[b - 1]; + phase += _impl::mul(downBin.output, shortVerticalTwist); + + if (b >= longVerticalStep) { + Complex longDownInput = getFractional<&Band::input>(maxChannel, mapPoint.inputBin - longVerticalStep*binTimeFactor); + Complex longVerticalTwist = _impl::mul(prediction.input, longDownInput); + + auto &longDownBin = bins[b - longVerticalStep]; + phase += _impl::mul(longDownBin.output, longVerticalTwist); + } + } + // Downwards vertical steps + if (b < bands - 1) { + auto &upPrediction = predictions[b + 1]; + auto &upMapPoint = outputMap[b + 1]; + + Sample binTimeFactor = randomTimeFactor ? timeFactorDist(randomEngine) : timeFactor; + Complex downInput = getFractional<&Band::input>(maxChannel, upMapPoint.inputBin - binTimeFactor); + Complex shortVerticalTwist = _impl::mul(upPrediction.input, downInput); + + auto &upBin = bins[b + 1]; + phase += _impl::mul(upBin.output, shortVerticalTwist); + + if (b < bands - longVerticalStep) { + auto &longUpPrediction = predictions[b + longVerticalStep]; + auto &longUpMapPoint = outputMap[b + longVerticalStep]; + + Complex longDownInput = getFractional<&Band::input>(maxChannel, longUpMapPoint.inputBin - longVerticalStep*binTimeFactor); + Complex longVerticalTwist = _impl::mul(longUpPrediction.input, longDownInput); + + auto &longUpBin = bins[b + longVerticalStep]; + phase += _impl::mul(longUpBin.output, longVerticalTwist); + } + } + + outputBin.output = prediction.makeOutput(phase); + + // All other bins are locked in phase + for (int c = 0; c < channels; ++c) { + if (c != maxChannel) { + auto &channelBin = bandsForChannel(c)[b]; + auto &channelPrediction = predictionsForChannel(c)[b]; + + Complex channelTwist = _impl::mul(channelPrediction.input, prediction.input); + Complex channelPhase = _impl::mul(outputBin.output, channelTwist); + channelBin.output = channelPrediction.makeOutput(channelPhase); + } + } + } + return; + } + step -= splitMainPrediction; + + if (blockProcess.newSpectrum) { + if (step-- == 0) { + for (auto &bin : _channelBands) { + bin.prevInput = bin.input; + } + } + } + } + + // Produces smoothed energy across all channels + static constexpr size_t smoothEnergySteps = 3; + Sample smoothEnergyState = 0; + void smoothEnergy(size_t step, Sample smoothingBins) { + Sample smoothingSlew = 1/(1 + smoothingBins*Sample(0.5)); + if (step-- == 0) { + for (auto &e : energy) e = 0; + for (int c = 0; c < channels; ++c) { + Band *bins = bandsForChannel(c); + for (int b = 0; b < bands; ++b) { + Sample e = _impl::norm(bins[b].input); + bins[b].inputEnergy = e; // Used for interpolating prediction energy + energy[b] += e; + } + } + for (int b = 0; b < bands; ++b) { + smoothedEnergy[b] = energy[b]; + } + smoothEnergyState = 0; + return; + } + + // The two other steps are repeated smoothing passes, down and up + Sample e = smoothEnergyState; + for (int b = bands - 1; b >= 0; --b) { + e += (smoothedEnergy[b] - e)*smoothingSlew; + smoothedEnergy[b] = e; + } + for (int b = 0; b < bands; ++b) { + e += (smoothedEnergy[b] - e)*smoothingSlew; + smoothedEnergy[b] = e; + } + smoothEnergyState = e; + } + + Sample mapFreq(Sample freq) const { + if (customFreqMap) return customFreqMap(freq); + if (freq > freqTonalityLimit) { + return freq + (freqMultiplier - 1)*freqTonalityLimit; + } + return freq*freqMultiplier; + } + + // Identifies spectral peaks using energy across all channels + void findPeaks() { + peaks.resize(0); + + int start = 0; + while (start < bands) { + if (energy[start] > smoothedEnergy[start]) { + int end = start; + Sample bandSum = 0, energySum = 0; + while (end < bands && energy[end] > smoothedEnergy[end]) { + bandSum += end*energy[end]; + energySum += energy[end]; + ++end; + } + Sample avgBand = bandSum/energySum; + Sample avgFreq = bandToFreq(avgBand); + peaks.emplace_back(Peak{avgBand, freqToBand(mapFreq(avgFreq))}); + + start = end; + } + ++start; + } + } + + void updateOutputMap() { + if (peaks.empty()) { + for (int b = 0; b < bands; ++b) { + outputMap[b] = {Sample(b), 1}; + } + return; + } + Sample bottomOffset = peaks[0].input - peaks[0].output; + for (int b = 0; b < std::min(bands, static_cast(std::ceil(peaks[0].output))); ++b) { + outputMap[b] = {b + bottomOffset, 1}; + } + // Interpolate between points + for (size_t p = 1; p < peaks.size(); ++p) { + const Peak &prev = peaks[p - 1], &next = peaks[p]; + Sample rangeScale = 1/(next.output - prev.output); + Sample outOffset = prev.input - prev.output; + Sample outScale = next.input - next.output - prev.input + prev.output; + Sample gradScale = outScale*rangeScale; + int startBin = std::max(0, static_cast(std::ceil(prev.output))); + int endBin = std::min(bands, static_cast(std::ceil(next.output))); + for (int b = startBin; b < endBin; ++b) { + Sample r = (b - prev.output)*rangeScale; + Sample h = r*r*(3 - 2*r); + Sample outB = b + outOffset + h*outScale; + + Sample gradH = 6*r*(1 - r); + Sample gradB = 1 + gradH*gradScale; + + outputMap[b] = {outB, gradB}; + } + } + Sample topOffset = peaks.back().input - peaks.back().output; + for (int b = std::max(0, static_cast(peaks.back().output)); b < bands; ++b) { + outputMap[b] = {b + topOffset, 1}; + } + } + + // If we mapped formants the same way as mapFreq(), this would be the inverse + Sample invMapFormant(Sample freq) const { + if (freq*invFormantMultiplier > freqTonalityLimit) { + return freq + (1 - formantMultiplier)*freqTonalityLimit; + } + return freq*invFormantMultiplier; + } + + Sample freqEstimateWeighted = 0; + Sample freqEstimateWeight = 0; + Sample estimateFrequency() { + // 3 highest peaks in the input + std::array peakIndices{0, 0, 0}; + for (int b = 1; b < bands - 1; ++b) { + Sample e = formantMetric[b]; + // local maxima only + if (e < formantMetric[b - 1] || e <= formantMetric[b + 1]) continue; + + if (e > formantMetric[peakIndices[0]]) { + if (e > formantMetric[peakIndices[1]]) { + if (e > formantMetric[peakIndices[2]]) { + peakIndices = {peakIndices[1], peakIndices[2], b}; + } else { + peakIndices = {peakIndices[1], b, peakIndices[2]}; + } + } else { + peakIndices[0] = b; + } + } + } + + // VERY rough pitch estimation + int peakEstimate = peakIndices[2]; + if (formantMetric[peakIndices[1]] > formantMetric[peakIndices[2]]*0.1) { + int diff = std::abs(peakEstimate - peakIndices[1]); + if (diff > peakEstimate/8 && diff < peakEstimate*7/8) peakEstimate = peakEstimate%diff; + if (formantMetric[peakIndices[0]] > formantMetric[peakIndices[2]]*0.01) { + diff = std::abs(peakEstimate - peakIndices[0]); + if (diff > peakEstimate/8 && diff < peakEstimate*7/8) peakEstimate = peakEstimate%diff; + } + } + Sample weight = formantMetric[peakIndices[2]]; + // Smooth it out a bit + freqEstimateWeighted += (peakEstimate*weight - freqEstimateWeighted)*Sample(0.25); + freqEstimateWeight += (weight - freqEstimateWeight)*Sample(0.25); + + return freqEstimateWeighted/(freqEstimateWeight + Sample(1e-30)); + } + + Sample freqEstimate; + + std::vector formantMetric; + Sample formantBaseFreq = 0; + void updateFormants(size_t step) { + if (step-- == 0) { + for (auto &e : formantMetric) e = 0; + for (int c = 0; c < channels; ++c) { + Band *bins = bandsForChannel(c); + for (int b = 0; b < bands; ++b) { + formantMetric[b] += bins[b].inputEnergy; + } + } + + freqEstimate = freqToBand(formantBaseFreq); + if (formantBaseFreq <= 0) freqEstimate = estimateFrequency(); + } else if (step-- == 0) { + Sample decay = 1 - 1/(freqEstimate*Sample(0.5) + 1); + Sample e = 0; + for (size_t repeat = 0; repeat < 2; ++repeat) { + for (int b = bands - 1; b >= 0; --b) { + e = std::max(formantMetric[b], e*decay); + formantMetric[b] = e; + } + for (int b = 0; b < bands; ++b) { + e = std::max(formantMetric[b], e*decay); + formantMetric[b] = e; + } + } + decay = 1/decay; + for (size_t repeat = 0; repeat < 2; ++repeat) { + for (int b = bands - 1; b >= 0; --b) { + e = std::min(formantMetric[b], e*decay); + formantMetric[b] = e; + } + for (int b = 0; b < bands; ++b) { + e = std::min(formantMetric[b], e*decay); + formantMetric[b] = e; + } + } + } else { + auto getFormant = [&](Sample band) -> Sample { + if (band < 0) return 0; + band = std::min(band, static_cast(bands)); + int floorBand = std::floor(band); + Sample fracBand = band - floorBand; + Sample low = formantMetric[floorBand], high = formantMetric[floorBand + 1]; + return low + (high - low)*fracBand; + }; + + for (int b = 0; b < bands; ++b) { + Sample inputF = bandToFreq(static_cast(b)); + Sample outputF = formantCompensation ? mapFreq(inputF) : inputF; + outputF = invMapFormant(outputF); + + Sample inputE = formantMetric[b]; + Sample targetE = getFormant(freqToBand(outputF)); + + Sample formantRatio = targetE/(inputE + Sample(1e-30)); + Sample energyRatio = formantRatio; + + for (int c = 0; c < channels; ++c) { + Band *bins = bandsForChannel(c); + // This is what's used to decide the output energy, so this affects the output + bins[b].inputEnergy *= energyRatio; + } + } + } + } + + // Proxy class to avoid copying/allocating anything + template + struct OffsetIO { + Io &io; + int offset; + + struct Channel { + Io &io; + int channel; + int offset; + + auto operator[](int i) -> decltype(io[0][0]) { + return io[channel][i + offset]; + } + }; + Channel operator[](int c) { + return {io, c, offset}; + } + }; +}; + +}} // namespace +#endif // include guard From 7aa88469dfda710df0e1dc1903ac4d5bb0b9d19a Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Mon, 9 Nov 2026 21:12:26 +0700 Subject: [PATCH 2/3] fix(player): bound live key/tempo response and test edits during continuous native playback --- engine/player/NativePlayerDeck.h | 10 +++-- tests/player_native_decode_test.cpp | 69 +++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/engine/player/NativePlayerDeck.h b/engine/player/NativePlayerDeck.h index af43d273e..ea2c796b6 100644 --- a/engine/player/NativePlayerDeck.h +++ b/engine/player/NativePlayerDeck.h @@ -294,11 +294,15 @@ class NativePlayerDeck final { }; length_.store(estimateTotal(),std::memory_order_release); bool failed=false; - // 0.75 s of decoded music gives disk/decode headroom without - // waiting multiple seconds before a KEY/TEMPO control is heard. + // Keep only ~250ms of queued music. A 750ms prebuffer made + // live KEY/TEMPO gestures audibly late, and could decode short + // songs to EOF before an edit was seen. The ring's 2.7s storage + // still provides a bounded fault barrier; only its fill target + // changes, leaving the vocal ASIO callback entirely untouched. + // Scale with native rate so 44.1/48/96k behave consistently. const auto highWater=std::min( ring_.capacityFrames()-2048, - std::max(8192,static_cast(deviceHz*3/4))); + std::max(8192,static_cast(deviceHz/4))); while(!obsolete(version) && consumedhighWater) { std::this_thread::sleep_for(std::chrono::milliseconds(3)); diff --git a/tests/player_native_decode_test.cpp b/tests/player_native_decode_test.cpp index 1a75e778f..20c72a060 100644 --- a/tests/player_native_decode_test.cpp +++ b/tests/player_native_decode_test.cpp @@ -158,5 +158,74 @@ int main() { } assert(newSettings); } + // M2 LIVE edits must be accepted and processed WHILE the same song + // is still playing. A previous test only checked Stop→Play, which + // could conceal stalled edits after the decoder had read to EOF. + { + const auto longFile=juce::File::getSpecialLocation( + juce::File::tempDirectory).getNonexistentChildFile( + "arson-player-live-tempo-test",".wav",false); + { + std::unique_ptr w( + wave.createWriterFor(longFile.createOutputStream().release(), + 44100.0,2,16,{},0)); + assert(w!=nullptr); + juce::AudioBuffer music(2,44100*8); + for(int n=0;n(.3*std::sin( + 2.0*3.14159265358979323846*330.0*n/44100.0)); + music.setSample(0,n,v); + music.setSample(1,n,-v); + } + assert(w->writeFromAudioSampleBuffer(music,0, + music.getNumSamples())); + } + { + armix::player::NativePlayerDeck player; + player.deviceRate(48000.0); + player.load(longFile,true); + bool started=false; + for(int i=0;i<600;++i) { + if(player.isPlayingRequested()) {started=true;break;} + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + } + assert(started); + std::array left{},right{}; + for(int i=0;i<24;++i) { + player.pull(left.data(),right.data(),256); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + const auto original=player.snapshot(); + assert(original.played>0 && original.deviceRate==48000); + const auto originalGeneration=original.generation; + const auto changeDuringPlayback=[&](int key,int tempo) { + assert(player.setPitchTempo(key,tempo)); + bool applied=false; + for(int i=0;i<750;++i) { + player.pull(left.data(),right.data(),256); + const auto current=player.snapshot(); + for(int n=0;n<256;++n) { + assert(std::isfinite(left[n])); + assert(std::isfinite(right[n])); + } + if(current.keySemitones==key && + current.tempoPercent==tempo && + !current.processingChangePending) { + assert(current.generation==originalGeneration); + assert(current.deviceRate==48000); + applied=true; + break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + assert(applied); // no Stop, Play or hardware rebind required + }; + changeDuringPlayback(3,80); + changeDuringPlayback(-2,120); + changeDuringPlayback(0,100); // normal 0/100 passthrough restored + assert(player.snapshot().played>original.played); + } // ensure native decoder closes the file before Windows deletion + assert(longFile.deleteFile()); + } assert(file.deleteFile()); } From 30de4b81e697eeea0c2548e7fb7757ddd650e33d Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Mon, 9 Nov 2026 21:26:24 +0700 Subject: [PATCH 3/3] feat(player): prepare queued song KEY/TEMPO without retuning live CURRENT --- engine/player/PlayerSetlistPolicy.h | 12 ++++++++++++ site/preview-v2/assets/arson-player.js | 11 ++++++++++- src/Main.cpp | 13 ++++++++++--- tests/player_native_decode_test.cpp | 9 +++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/engine/player/PlayerSetlistPolicy.h b/engine/player/PlayerSetlistPolicy.h index b5d4142f2..db2b1976e 100644 --- a/engine/player/PlayerSetlistPolicy.h +++ b/engine/player/PlayerSetlistPolicy.h @@ -6,6 +6,18 @@ namespace armix::player { // GUI-independent stage-playback decisions. No side effects, allocation, // hardware ownership, graph mutation or background work. struct SetlistPolicy final { + struct PitchTempoTarget { + int index=-1; + bool updateLiveDeck=false; + }; + static PitchTempoTarget pitchTempoTarget( + int current,int selected,int trackCount) noexcept { + // Selection stages NEXT without interrupting CURRENT. Edits to a + // selected next song remain queued presets until it is played. + const int index=(selected>=0 && selected=0 && current=0 && index==current}; + } static bool resumeCurrentOnPlay(std::string_view phase,int current) noexcept { return current>=0 && (phase=="PAUSED" || phase=="PREPARING" || phase=="READY"); diff --git a/site/preview-v2/assets/arson-player.js b/site/preview-v2/assets/arson-player.js index 6541af974..1b7d349e7 100644 --- a/site/preview-v2/assets/arson-player.js +++ b/site/preview-v2/assets/arson-player.js @@ -111,8 +111,17 @@ $("playerProgress").value=+t.durationSeconds>0? Math.min(1,Math.max(0,+t.currentSeconds/(+t.durationSeconds))):0; $("playerPlay").textContent=state==="PLAYING"?"Ⅱ Pause":"▶ Play"; - const editingIndex=model.current>=0?model.current:model.selected; + const editingIndex=model.selected>=0?model.selected:model.current; const chosen=model.tracks[editingIndex]; + // These controls are song presets: NEXT can be prepared during a live + // CURRENT performance. The JUCE engine only applies when appropriate. + const editingNext=editingIndex>=0 && editingIndex!==model.current; + const targetDescription=editingNext?"queued next song":"current song"; + const pitchRow=root.querySelector(".ap-pitchtempo"); + if(pitchRow)pitchRow.setAttribute("aria-label", + "Music pitch and playback speed for "+targetDescription); + $("playerKeyValue").title="Key for "+targetDescription; + $("playerTempoValue").title="Tempo for "+targetDescription; const key=Number(chosen?.keySemitones??t.requestedKey??0); const tempo=Number(chosen?.tempoPercent??t.requestedTempo??100); $("playerKeyValue").textContent=(key>0?"+":"")+key+" st"; diff --git a/src/Main.cpp b/src/Main.cpp index 92ea1d50f..d6391cc08 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -2590,8 +2590,11 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr if(action=="playerAdd")return choosePlayerFiles(); if(action=="playerKeyStep" || action=="playerTempoStep" || action=="playerPitchTempoReset") { - const int i=playerCurrent_>=0?playerCurrent_:playerSelected_; - if(i<0 || i>=static_cast(playerTrackSettings_.size())) { + const auto target=armix::player::SetlistPolicy::pitchTempoTarget( + playerCurrent_,playerSelected_, + static_cast(playerTrackSettings_.size())); + const int i=target.index; + if(i<0) { playerError_="Add Music to set a song's KEY or TEMPO"; emitPlayerState();return; } @@ -2604,7 +2607,11 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr else if(direction==1 || direction==-1) next.tempo=std::clamp(next.tempo+direction*5,80,120); else return; // invalid native control intent cannot mutate DSP - if(engine_.setPlayerPitchTempo(next.key,next.tempo)) { + // A performer must be able to tune NEXT while CURRENT is + // playing without changing CURRENT audio. Only send live DSP + // commands for the currently loaded song. + if(!target.updateLiveDeck || + engine_.setPlayerPitchTempo(next.key,next.tempo)) { playerTrackSettings_[static_cast(i)]=next; playerError_.clear(); persistPlayerLibrary(); diff --git a/tests/player_native_decode_test.cpp b/tests/player_native_decode_test.cpp index 20c72a060..54ea9008b 100644 --- a/tests/player_native_decode_test.cpp +++ b/tests/player_native_decode_test.cpp @@ -13,6 +13,15 @@ int main() { using Policy=armix::player::SetlistPolicy; + // PREPARE NEXT must not retune the CURRENT backing track, even while + // the performer presses +/- repeatedly on the queued song's controls. + assert(Policy::pitchTempoTarget(0,1,3).index==1); + assert(!Policy::pitchTempoTarget(0,1,3).updateLiveDeck); + assert(Policy::pitchTempoTarget(0,0,3).updateLiveDeck); + assert(Policy::pitchTempoTarget(2,-1,3).updateLiveDeck); + assert(Policy::pitchTempoTarget(-1,1,3).index==1); + assert(!Policy::pitchTempoTarget(-1,1,3).updateLiveDeck); + assert(Policy::pitchTempoTarget(-1,99,3).index==-1); // Live stage semantics: queued NEXT must not hijack pause/resume. assert(Policy::resumeCurrentOnPlay("PAUSED",3)); assert(Policy::resumeCurrentOnPlay("PREPARING",3));