Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
163 changes: 144 additions & 19 deletions engine/player/NativePlayerDeck.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
#pragma once
#include "PreparedStereoPcmRing.h"
#include "PlayerPitchTempoProcessor.h"
#include "../core/RealtimeSlew64.h"
#include <juce_audio_formats/juce_audio_formats.h>
#include <atomic>
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<std::mutex> lock(commandMutex_);
Expand Down Expand Up @@ -170,6 +191,15 @@ class NativePlayerDeck final {
}

private:
using Controls=PlayerPitchTempoProcessor::Controls;
static constexpr std::uint32_t packControls(Controls c) noexcept {
return (static_cast<std::uint32_t>(c.tempoPercent)<<8) |
static_cast<std::uint32_t>(c.keySemitones+6);
}
static constexpr Controls unpackControls(std::uint32_t bits) noexcept {
return {static_cast<int>(bits&255u)-6,
static_cast<int>((bits>>8)&255u)};
}
void restart() noexcept {
ready_.store(false,std::memory_order_release);
played_.store(0,std::memory_order_relaxed);
Expand Down Expand Up @@ -232,44 +262,135 @@ class NativePlayerDeck final {
}
const bool mono=reader->numChannels==1;
const double ratio=reader->sampleRate/static_cast<double>(deviceHz);
const auto total=static_cast<std::uint64_t>(std::ceil(
const auto sourceTotal=static_cast<std::uint64_t>(std::ceil(
static_cast<double>(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<double>(deviceHz));
juce::AudioBuffer<float> staging(2,1024);
length_.store(total,std::memory_order_release);
std::uint64_t written=0;
while(!obsolete(version) && written<total) {
if(ring_.bufferedFrames()>ring_.capacityFrames()-2048) {
resampled.prepareToPlay(1536,static_cast<double>(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<float> input(2,1536),output(2,1024);
std::uint64_t consumed=0,written=0;
const auto estimateTotal=[&]() -> std::uint64_t {
return written+static_cast<std::uint64_t>(std::ceil(
(sourceTotal-consumed)*100.0/controls.tempoPercent))
+static_cast<std::uint64_t>(pitch.outputLatency());
};
length_.store(estimateTotal(),std::memory_order_release);
bool failed=false;
// 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<std::size_t>(
ring_.capacityFrames()-2048,
std::max<std::size_t>(8192,static_cast<std::size_t>(deviceHz/4)));
while(!obsolete(version) && consumed<sourceTotal) {
if(ring_.bufferedFrames()>highWater) {
std::this_thread::sleep_for(std::chrono::milliseconds(3));
continue;
}
const int n=static_cast<int>(std::min<std::uint64_t>(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<static_cast<std::uint64_t>(nInput)) {
nOutput=std::max(1,static_cast<int>(std::ceil(
remaining*100.0/controls.tempoPercent)));
nOutput=std::min(1024,nOutput);
nInput=static_cast<int>(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<std::size_t>(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<std::size_t>(nOutput));
if(transferred!=static_cast<std::size_t>(nOutput)) {
failed=true;break;
}
consumed+=static_cast<std::uint64_t>(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<deviceHz*2) {
juce::AudioBuffer<float> ending(2,tail);
float* const ends[]={ending.getWritePointer(0),
ending.getWritePointer(1)};
pitch.flush(ends,tail);
Comment on lines +362 to +367

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Include analysis latency when draining the song ending

For non-neutral playback, the stretcher is fed from its initial empty state without an outputSeek pre-roll, so pending audio includes both input analysis latency and output synthesis latency. Draining only outputLatency() truncates the ending before setting EOF. At 48 kHz the configured library reports 2,400 input-latency frames and 4,320 output-latency frames; a reproduction at 80% tempo with audio confined to the last 512 source frames yielded energy of only 0.67 with this drain, versus 51.90 when the additional 3,000 tempo-adjusted input-latency frames were drained. Account for the full processing delay, or prime and compensate it explicitly, so backing tracks retain their final notes.

Useful? React with 👍 / 👎.

for(int offset=0;offset<tail && !obsolete(version);) {
if(ring_.bufferedFrames()>highWater) {
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<std::size_t>(n));
if(copied!=static_cast<std::size_t>(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();
}
Expand All @@ -282,6 +403,10 @@ class NativePlayerDeck final {
std::atomic<std::uint64_t> generation_{0},played_{0},length_{0};
std::atomic<std::uint32_t> readers_{0};
std::atomic<int> 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<std::uint32_t> requestedControls_{(100u<<8)|6u};
std::atomic<std::uint32_t> appliedControls_{(100u<<8)|6u};
std::atomic<State> state_{State::empty};
armix::dsp::RealtimeSlew64 gain_; // AUDIO only
std::uint64_t audioGeneration_=0; // AUDIO only, no race with file thread
Expand Down
97 changes: 97 additions & 0 deletions engine/player/PlayerPitchTempoProcessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// SPDX-License-Identifier: AGPL-3.0-only
#pragma once
#include "../../third_party/signalsmith/signalsmith-stretch.h"
#include <algorithm>
#include <cmath>
#include <cstdint>

namespace armix::player {
// WORKER THREAD ONLY. Signalsmith DSP state, FFT storage and scratch buffers
// are allocated off realtime. Never instantiate or call from JUCE callback.
//
// Tempo is rate (80% slower / 120% faster) and does not set musical pitch.
// Key transpose is independent of tempo. Both stereo channels share one
// phase-coherent processing instance rather than divergent mono stretchers.
class PlayerPitchTempoProcessor final {
public:
struct Controls {
int keySemitones=0;
int tempoPercent=100;
bool operator==(const Controls&) const = default;
};
static bool valid(Controls c) noexcept {
return c.keySemitones>=-6 && c.keySemitones<=6 &&
c.tempoPercent>=80 && c.tempoPercent<=120;
}
static int inputFramesForOutput(int outputFrames,Controls c,
double& remainder) noexcept {
if(!valid(c)||outputFrames<=0)return 0;
const double requested=outputFrames*
(static_cast<double>(c.tempoPercent)/100.0)+remainder;
const int frames=static_cast<int>(std::floor(requested));
remainder=requested-frames;
return frames;
}
bool prepare(int sampleRate,Controls c) {
if(sampleRate<8000 || sampleRate>384000 || !valid(c))
return false;
sampleRate_=sampleRate;
controls_=c;
// Cheaper is the documented 100 ms / 40 ms quality/perf preset;
// split computation spreads FFT bursts on the decoder worker.
stretch_.presetCheaper(2,static_cast<float>(sampleRate_),true);
stretch_.setTransposeSemitones(
static_cast<float>(controls_.keySemitones),
8000.f/static_cast<float>(sampleRate_));
fractionalInput_=0;
prepared_=true;
return true;
}
void setControls(Controls c) {
if(!prepared_ || !valid(c) || controls_==c)return;
if(controls_.keySemitones!=c.keySemitones)
stretch_.setTransposeSemitones(
static_cast<float>(c.keySemitones),
8000.f/static_cast<float>(sampleRate_));
controls_=c;
}
Controls applied() const noexcept {return controls_;}
bool active() const noexcept {
return controls_.keySemitones!=0 || controls_.tempoPercent!=100;
}
int nextInputFrames(int outputFrames) noexcept {
return inputFramesForOutput(outputFrames,controls_,fractionalInput_);
}
// in and out contain TWO separate PCM lanes, each sized for the supplied
// count. No file IO here. Process is allowed to use heap on this WORKER.
bool process(const float* const* in,int nInput,float* const* out,int nOut) {
if(!prepared_ || !in || !out || !in[0] || !in[1] ||
!out[0] || !out[1] || nInput<0 || nOut<=0)return false;
if(!active()) {
if(nInput!=nOut)return false;
std::copy_n(in[0],nOut,out[0]);
std::copy_n(in[1],nOut,out[1]);
} else {
stretch_.process(in,nInput,out,nOut);
Comment on lines +70 to +75

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Synchronize DSP history when entering or leaving neutral bypass

When a performer resets KEY/TEMPO to 0/100 during playback, this branch stops advancing the stretcher but leaves its buffered audio intact. Applying another non-neutral setting later resumes that old history rather than the current song position, replaying earlier audio; entering DSP from an initially neutral song also starts with an unprimed buffer. A standalone reproduction using this processor produced substantial old-tone output after processing one second of silence through neutral bypass. Keep the DSP history synchronized or explicitly re-prime it and handle the latency transition when switching paths.

Useful? React with 👍 / 👎.

}
for(int c=0;c<2;++c)
for(int i=0;i<nOut;++i)
if(!std::isfinite(out[c][i]))
return false; // source/FFT corruption fails closed
return true;
}
int outputLatency() const noexcept {
return prepared_&&active()?stretch_.outputLatency():0;
}
void flush(float* const* output,int frames) {
if(prepared_&&active() && frames>0)
stretch_.flush(output,frames,controls_.tempoPercent/100.f);
}
private:
signalsmith::stretch::SignalsmithStretch<float> stretch_{23};
int sampleRate_=0;
Controls controls_{};
double fractionalInput_=0;
bool prepared_=false;
};
} // namespace armix::player
12 changes: 12 additions & 0 deletions engine/player/PlayerSetlistPolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<trackCount)?selected:
((current>=0 && current<trackCount)?current:-1);
return {index,index>=0 && index==current};
}
static bool resumeCurrentOnPlay(std::string_view phase,int current) noexcept {
return current>=0 &&
(phase=="PAUSED" || phase=="PREPARING" || phase=="READY");
Expand Down
Loading
Loading