Skip to content
Open
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
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ clean:
# --------------------------------------------------------------

.PHONY: dgl plugins tests

# Headless meter regression tests.
test-meter:
bash tests/run-meter-tests.sh "$(CURDIR)"

.PHONY: test-meter
5 changes: 3 additions & 2 deletions plugins/LiveCut/LiveCut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,8 @@ class HyperCut : public Plugin
const double bar = timePos.bbt.bar - 1;
const double beat = timePos.bbt.beat - 1;
const double beatFraction = timePos.bbt.ticksPerBeat > 0.0 ? timePos.bbt.tick / timePos.bbt.ticksPerBeat : 0.0;
ppqPos = (bar * beats_per_bar) + beat + beatFraction;
// BBT beats use the time-signature denominator; PPQ uses quarter notes.
ppqPos = ((bar * beats_per_bar) + beat + beatFraction) * (4.0 / beat_unit);
freeRunPpq = ppqPos;
}
else
Expand Down Expand Up @@ -1460,7 +1461,7 @@ class HyperCut : public Plugin
}
};

if (positionInMeasure != oldpositionInMeasure)
if (positionInMeasure != oldpositionInMeasure || bbcutter.NeedsResync())
{
oldpositionInMeasure = positionInMeasure;
advance(oldmeasure, oldpositionInMeasure);
Expand Down
39 changes: 36 additions & 3 deletions plugins/LiveCut/lib/BBCutter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,12 @@ void SQPusherCutProc::ChooseCuts(std::vector<CutInfo> &cuts,

if(fill==true)
{
// Two pre-baked slots in the original table (cs[6][3], cs[8][2]) are empty. Skip them:
// LivePlayer::OnBlock() ignores an empty list, which would keep replaying the previous cuts
// while the scheduler moved on — exactly the kind of stuck 16th-note pattern being fixed.
while(fillpos<cs[fillnumber].size() && cs[fillnumber][fillpos].empty())
++fillpos;

if(fillpos<cs[fillnumber].size())
{
lastkind = kBlockFill;
Expand All @@ -450,7 +456,10 @@ void SQPusherCutProc::ChooseCuts(std::vector<CutInfo> &cuts,
cuts[i].amp = Math::randomfloat(minamp,maxamp);
cuts[i].cents = Math::randomfloat(mindetune,maxdetune);
}
unitsdone = long(double(subdiv)*beatsdone/4.0);
// Beats in the fill segment -> grid units. unitsdone is passed by value, so advancing it
// here has no effect on the caller; the block length is what tells BBCutter::Unit when to
// choose the next segment of the pre-baked fill.
unitsinblock = std::max(1L, long(double(subdiv)*beatsdone/4.0 + 0.5));
if(unitsinblock>unitsleft)
unitsinblock=unitsleft; //will automatically interrupt cutsequence before its end
fillpos++;
Expand All @@ -468,7 +477,11 @@ void SQPusherCutProc::ChooseCuts(std::vector<CutInfo> &cuts,
if(beatpos == 2)
temp=1;

unitsinblock = long(double(temp*8.0)/double(subdiv));
// temp quavers (or temp*2 semiquavers) last temp*0.5 beats, i.e. temp*subdiv/8 grid units.
// The old temp*8/subdiv was inverted: at subdiv 8 it happened to be right, but above 8 it
// shrank towards 0 (always 0 at 32), so BBCutter::Block re-armed the player on every unit
// and only the first 16th of each freshly chosen pattern was ever heard.
unitsinblock = std::max(1L, long(double(temp*subdiv)/8.0 + 0.5));
if(unitsinblock>unitsleft)
unitsinblock=unitsleft; //will automatically interrupt cutsequence before its end

Expand Down Expand Up @@ -1132,6 +1145,7 @@ BBCutter::BBCutter(LivePlayer &player)
, slicestart(0)
, mincutquarters(0.0)
, phraselimit(0)
, resyncPending(false)
, strategy(&cutproc11)
{
procs.reserve(kNumCutProcs);
Expand Down Expand Up @@ -1217,6 +1231,7 @@ void BBCutter::SetSampleRate(double v)

void BBCutter::SetTimeInfos(double bpm,double num,double den,double srate)
{
const bool meterChanged = numerator != num || denominator != den;
bool changed = false;
if(tempo != bpm)
{
Expand All @@ -1239,7 +1254,16 @@ void BBCutter::SetTimeInfos(double bpm,double num,double den,double srate)
changed = true;
}
if(changed)
{
// SamplesPerBar must follow the current meter, not the constructor's 4/4.
beatsPerBar = 4.0 * numerator / denominator;
UpdateRates();
}
if(meterChanged)
{
ResetPhrase();
resyncPending = true;
}
}

void BBCutter::UpdateRates()
Expand Down Expand Up @@ -1300,7 +1324,16 @@ void BBCutter::Unit(long bar, long sd)

void BBCutter::SetPosition(long bar, long sd)
{
const long delta = sd - (unitsdone % long(UnitsPerBar(subdiv,numerator,denominator)));
if(resyncPending)
{
resyncPending = false;
// Rejoin immediately, including buffers arriving after unit zero.
Phrase(bar, sd);
unitsdone = sd;
unitsinblock = unitsinsideblock = 0;
}
// sd already counts subdivisions of one whole bar, in every meter.
const long delta = sd - (unitsdone % subdiv);
unitsinsideblock += delta;
unitsdone += delta;
Unit(bar,sd);
Expand Down
2 changes: 2 additions & 0 deletions plugins/LiveCut/lib/BBCutter.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ class BBCutter
inline unsigned long GetGateMask() const { return gate.GetMask(); }
// forget the current phrase: a fresh phrase starts on the next bar line
void ResetPhrase() { totalunits = 0; unitsdone = 0; unitsinblock = 0; unitsinsideblock = 0; }
inline bool NeedsResync() const { return resyncPending; }
// cap new phrases to this many bars (0 = no cap), used by fill mode
void SetPhraseLimit(long bars) { phraselimit = bars; }
// minimum cut length in quarter notes (0 = no limit). Shorter cuts are merged.
Expand Down Expand Up @@ -656,6 +657,7 @@ class BBCutter
GateCutProc gate;
double mincutquarters;
long phraselimit;
bool resyncPending;
CutProc *strategy;
std::vector<CutProc *> procs;
ListenerManager listenermanager;
Expand Down
198 changes: 198 additions & 0 deletions tests/meter_regression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
#include "DistrhoPluginInternal.hpp"
#include "DistrhoVST3TimePosition.hpp"
#include "BBCutter.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <limits>
#include <vector>
using namespace DISTRHO;

static int failures = 0;
static void check(bool ok, const char* text) {
if(!ok) { std::fprintf(stderr,"FAIL: %s\n",text); ++failures; }
}
static bool near(double a,double b) { return std::fabs(a-b)<1.e-7; }
static v3_process_context context(int n,int d,double q,double start) {
v3_process_context c={};
c.state=V3_PROCESS_CTX_PROJECT_TIME_VALID|V3_PROCESS_CTX_TIME_SIG_VALID|V3_PROCESS_CTX_BAR_POSITION_VALID|V3_PROCESS_CTX_TEMPO_VALID|V3_PROCESS_CTX_PLAYING;
c.time_sig_numerator=n;c.time_sig_denom=d;
c.project_time_quarters=q;c.bar_position_quarters=start;c.bpm=190.;
return c;
}
static void timingTests() {
TimePosition::BarBeatTick b;
fillVST3BarBeatTick(context(19,16,4.75,4.75),b);
check(b.valid&&b.bar==2&&b.beat==1&&near(b.tick,0.),"19/16 bar boundary");
fillVST3BarBeatTick(context(19,16,4.875,4.75),b);
check(b.beat==1&&near(b.tick,960.),"19/16 fractional beat");
fillVST3BarBeatTick(context(1,16,.25,.25),b);
check(b.valid&&b.bar==2&&b.beat==1,"1/16 bar boundary");
fillVST3BarBeatTick(context(17,32,2.125,2.125),b);
check(b.valid&&b.bar==2&&b.beat==1,"17/32 bar boundary");
fillVST3BarBeatTick(context(7,8,4.25,4.),b);
check(b.beat==1&&near(b.tick,960.)&&near(b.barStartTick,15360.),"host bar start after meter change");
fillVST3BarBeatTick(context(19,16,-.125,-4.75),b);
check(b.valid&&b.bar==0&&b.beat==19&&near(b.tick,960.),"negative count-in");
auto c=context(19,16,4.75,0.);
c.state&=~V3_PROCESS_CTX_BAR_POSITION_VALID;
fillVST3BarBeatTick(c,b);
check(b.bar==2&&b.beat==1,"fallback without host bar start");
c=context(19,0,0.,0.);fillVST3BarBeatTick(c,b);
check(!b.valid,"invalid denominator");
c=context(19,16,std::numeric_limits<double>::quiet_NaN(),0.);
fillVST3BarBeatTick(c,b);check(!b.valid,"non-finite position");
c=context(4,4,0.,0.);c.state=0;fillVST3BarBeatTick(c,b);
check(!b.valid,"missing timing flags");
LivePlayer player;BBCutter cutter(player);
cutter.SetTimeInfos(190.,19.,16.,48000.);
check(near(cutter.SamplesPerBar(),48000.*60./190.*4.75),"19/16 capture duration");
cutter.SetTimeInfos(190.,7.,8.,48000.);
check(near(cutter.SamplesPerBar(),48000.*60./190.*3.5),"changed capture duration");
}
static void set(PluginExporter& p,const char* symbol,float value) {
for(uint32_t i=0;i<p.getParameterCount();++i)
if(p.getParameterSymbol(i)==symbol){p.setParameterValue(i,value);return;}
std::fprintf(stderr,"Missing parameter: %s\n",symbol);
check(false,"parameter symbol exists");
}
struct Meter { int n,d; };

static double audioTest(const std::vector<Meter>& meters,int subdiv,bool loop,
double sr=8000.,int frames=8,int algo=3,int warp=-1,int seed=42,int bars=8) {
d_nextBufferSize=frames;d_nextSampleRate=sr;
PluginExporter p(nullptr,nullptr,nullptr,nullptr);
const float values[]={0.f,1.f,2.f,.5f,1.5f,0.f};
set(p,"livecut_cutproc",values[algo]);
set(p,"livecut_subdiv",float(subdiv));
set(p,"livecut_seed",float(seed));
set(p,"livecut_mix",1.f);set(p,"livecut_slicer",1.f);
set(p,"livecut_minamp",1.f);set(p,"livecut_maxamp",1.f);
set(p,"livecut_duty",1.f);set(p,"livecut_fillduty",1.f);
set(p,"livecut_minpitch",0.f);set(p,"livecut_maxpitch",0.f);
set(p,"livecut_fade",.01f);
for(const char* sym:{"livecut_crusher","livecut_comb","livecut_filter",
"livecut_reverb","livecut_chroma","livecut_transient","livecut_freeze",
"livecut_limiter","livecut_fillmode"})set(p,sym,0.f);
set(p,"livecut_gate",algo==5?1.f:0.f);
set(p,"livecut_gatemode",1.f);
set(p,"livecut_gatepat1",65535.f);set(p,"livecut_gatepat2",65535.f);
if(warp>=0){
set(p,"livecut_straight",warp==0?1.f:0.f);
set(p,"livecut_regular",warp==1?1.f:0.f);
set(p,"livecut_ritard",warp==3?1.f:0.f);
set(p,"livecut_speed",.9f);
}
const double bpm=190.;
std::vector<double> starts(1,0.);
double longestBar=0.;
for(const auto& m:meters){
double len=m.n*4./m.d;
starts.push_back(starts.back()+len*bars);
longestBar=std::max(longestBar,len*60./bpm);
}
const double duration=starts.back()*60./bpm;
const double warmup=meters[0].n*4./meters[0].d*60./bpm+2.*frames/sr;
const int divisions[]={6,8,12,16,18,24,32};
// SQPusher rounds fractional-beat fills to its integer unit grid.
const double allowance=.008+2.*frames/sr+
(algo==2?.6*longestBar/divisions[subdiv]:0.);
std::vector<float> a(frames),b(frames),l(frames),r(frames);
const float* in[]={a.data(),b.data()};
float* out[]={l.data(),r.data()};
long zero=0,longest=0;bool finite=true;
for(long f=0;f<long(duration*sr);f+=frames){
double q=f/sr*bpm/60.;
size_t seg=0;
while(seg+1<meters.size()&&q>=starts[seg+1])++seg;
const Meter m=meters[seg];
const double barLength=m.n*4./m.d;
double local=q-starts[seg];
if(loop)local=std::fmod(local,barLength*4.);
const double pos=starts[seg]+local;
const double barStart=starts[seg]+std::floor(local/barLength)*barLength;
auto c=context(m.n,m.d,pos,barStart);
TimePosition t;t.clear();t.playing=true;t.frame=f;
t.bbt.beatsPerMinute=bpm;
fillVST3BarBeatTick(c,t.bbt);p.setTimePosition(t);
for(int i=0;i<frames;++i)a[i]=b[i]=.2f*std::sin(float(f+i)*.11f);
p.run(in,out,frames);
for(int i=0;i<frames;++i){
finite=finite&&std::isfinite(l[i])&&std::isfinite(r[i]);
if((f+i)/sr>=warmup&&std::fabs(l[i])+std::fabs(r[i])<1.e-8)++zero;
else zero=0;
longest=std::max(longest,zero);
}
}
check(finite,"audio has no NaN/Inf");
double silence=longest/sr;
// Startup grace is never reapplied at meter changes.
if(silence>allowance){
std::fprintf(stderr,
"FAIL: AUDIO algo=%d warp=%d subdiv=%d loop=%d seed=%d sr=%.0f buffer=%d silence=%.5fs allowed=%.5fs\n",
algo,warp,subdiv,loop,seed,sr,frames,silence,allowance);
++failures;
}
return silence;
}
static void resyncTests() {
const int divs[]={6,8,12,16,18,24,32};
for(int algo=0;algo<6;++algo)
for(int div:divs)
for(int sd=0;sd<div;++sd){
LivePlayer player;BBCutter cutter(player);
cutter.SetCutProc(algo);cutter.SetSubdiv(div);
cutter.SetMinAmp(1.f);cutter.SetMaxAmp(1.f);
cutter.SetDutyCycle(1.f);cutter.SetFillDutyCycle(1.f);
cutter.SetGateMode(1);cutter.SetGatePattern(0xfffffffful);
cutter.SetTimeInfos(190.,9.,16.,48000.);cutter.SetPosition(0,0);
cutter.SetTimeInfos(190.,12.,16.,48000.);
check(cutter.NeedsResync(),"meter change requests immediate dispatch");
cutter.SetPosition(1,sd);
check(!cutter.NeedsResync()&&cutter.GetUnitsDone()==sd+1&&
cutter.GetUnitsInsideBlock()==1,"immediate block at every host unit");
check(player.GetNumCuts()>0&&player.GetBufferSize()>0,"playable cuts after resync");
cutter.SetTimeInfos(190.,12.,16.,48000.);
check(!cutter.NeedsResync(),"unchanged meter preserves phrase");
}
}
int main(int argc,char**) {
const std::vector<Meter> user={
{4,4},{4,4},{9,16},{12,16},{7,16},{9,16},{12,16},
{9,16},{12,16},{7,16},{9,16},{12,16}};
if(argc>1){
audioTest(user,3,false,48000.,256,1,0,42,1);
return failures?1:0;
}
timingTests();resyncTests();int cases=0;
const Meter meters[]={
{4,4},{3,4},{5,4},{6,8},{7,8},{5,8},{19,16},{1,16},{17,32}};
const std::vector<Meter> mixed={
{4,4},{4,4},{19,16},{7,8},{5,16},{17,32},{3,4},{9,16},{12,16}};
const char* names[]={"CutProc11","WarpCut","SQPusher","Euclid","Ratchet","Gate"};
for(int algo=0;algo<6;++algo){
const int before=failures;
for(const auto& m:meters)for(int div=0;div<7;++div)
for(bool loop:{false,true}){
audioTest({m},div,loop,8000.,8,algo);++cases;
}
for(int div=0;div<7;++div)for(int buffer:{64,256})for(int seed:{1,42}){
audioTest(user,div,false,48000.,buffer,algo,-1,seed,1);++cases;
audioTest(mixed,div,false,44100.,buffer,algo,-1,seed,1);++cases;
}
std::printf("%s: %s\n",failures==before?"PASS":"FAIL",names[algo]);
std::fflush(stdout);
}
for(int warp=0;warp<4;++warp){
const int before=failures;
for(int div=0;div<7;++div)for(int buffer:{64,256})for(int seed:{1,42}){
audioTest(user,div,false,48000.,buffer,1,warp,seed,1);++cases;
}
std::printf("%s: WarpCut mode %d\n",failures==before?"PASS":"FAIL",warp);
std::fflush(stdout);
}
std::printf(
"%s: timing + resync checks + %d audio cases; all 6 algorithms and 4 WarpCut modes.\n",
failures?"FAILED":"PASS",cases);
return failures?1:0;
}
15 changes: 15 additions & 0 deletions tests/run-meter-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -euo pipefail
source_dir="${1:?Usage: bash tests/run_tests.sh /path/to/patched/hypercut}"
test_dir="$(cd "$(dirname "$0")" && pwd)"
probe_dir="$(mktemp -d "${TMPDIR:-/tmp}/hypercut-meter-tests.XXXXXX")"
trap 'rm -rf "$probe_dir"' EXIT
"${CXX:-c++}" -std=c++14 -O2 -Wno-deprecated-declarations -DDISTRHO_PLUGIN_TARGET_VST3 \
-I"$source_dir/plugins/LiveCut" -I"$source_dir/plugins/LiveCut/lib" \
-I"$source_dir/dpf/distrho" -I"$source_dir/dpf/distrho/src" \
"$test_dir/meter_regression.cpp" "$source_dir/plugins/LiveCut/LiveCut.cpp" \
"$source_dir/plugins/LiveCut/lib/BBCutter.cpp" "$source_dir/plugins/LiveCut/lib/BitCrusher.cpp" \
"$source_dir/plugins/LiveCut/lib/Comb.cpp" "$source_dir/plugins/LiveCut/lib/DelayLine.cpp" \
"$source_dir/plugins/LiveCut/lib/FirstOrderLowpass.cpp" "$source_dir/dpf/distrho/src/DistrhoPlugin.cpp" \
-o "$probe_dir/meter-regression"
"$probe_dir/meter-regression" "${@:2}"