Add Guide

This commit is contained in:
Chris Cannam
2022-05-19 16:31:21 +01:00
parent 5584ccc059
commit cf602218f5
6 changed files with 228 additions and 60 deletions

View File

@@ -164,11 +164,6 @@ void gettimeofday(struct timeval *tv, void *tz)
tv->tv_sec = (long)((now.ns100 - 116444736000000000LL) / 10000000LL);
}
void usleep(unsigned long usec)
{
::Sleep(usec == 0 ? 0 : usec < 1000 ? 1 : usec / 1000);
}
#endif
void system_specific_initialise()

View File

@@ -92,10 +92,6 @@ struct timeval { long tv_sec; long tv_usec; };
void gettimeofday(struct timeval *p, void *tz);
#endif // _WIN32
#ifdef _MSC_VER
void usleep(unsigned long);
#endif // _MSC_VER
inline double mod(double x, double y) { return x - (y * floor(x / y)); }
inline float modf(float x, float y) { return x - (y * float(floor(x / y))); }
@@ -121,11 +117,6 @@ extern void system_memorybarrier();
}
#define MBARRIER() RubberBand::system_memorybarrier()
#define DLOPEN(a,b) LoadLibrary((a).toStdWString().c_str())
#define DLSYM(a,b) GetProcAddress((HINSTANCE)(a),(b))
#define DLCLOSE(a) FreeLibrary((HINSTANCE)(a))
#define DLERROR() ""
#else // !_WIN32
#include <sys/mman.h>
@@ -154,11 +145,6 @@ extern void system_memorybarrier();
# endif
#endif
#define DLOPEN(a,b) dlopen((a).toStdString().c_str(),(b))
#define DLSYM(a,b) dlsym((a),(b))
#define DLCLOSE(a) dlclose((a))
#define DLERROR() dlerror()
#endif // !_WIN32
#ifdef NO_THREADING

View File

@@ -112,11 +112,11 @@ protected:
std::vector<int> m_numeric;
MovingMedian<int> m_classFilter;
int binForFrequency(double f) {
int binForFrequency(double f) const {
return int(round(f * double(m_parameters.fftSize) /
m_parameters.sampleRate));
}
double frequencyForBin(int b) {
double frequencyForBin(int b) const {
return (double(b) * m_parameters.sampleRate)
/ double(m_parameters.fftSize);
}

220
src/finer/Guide.h Normal file
View File

@@ -0,0 +1,220 @@
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Rubber Band Library
An audio time-stretching and pitch-shifting library.
Copyright 2007-2022 Particular Programs Ltd.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
Alternatively, if you have a valid commercial licence for the
Rubber Band Library obtained by agreement with the copyright
holders, you may redistribute and/or modify it under the terms
described in that licence.
If you wish to distribute code using the Rubber Band Library
under terms other than those of the GNU General Public License,
you must obtain a valid commercial licence before doing so.
*/
#ifndef RUBBERBAND_GUIDE_H
#define RUBBERBAND_GUIDE_H
namespace RubberBand
{
class Guide
{
public:
struct FftBand {
int fftSize;
float f0;
float f1;
FftBand(int _s, float _f0, float _f1) :
fftSize(_s), f0(_f0), f1(_f1) { }
};
struct PhaseLockBand {
int p;
float beta;
float f0;
float f1;
PhaseLockBand(int _p, float _beta, float _f0, float _f1) :
p(_p), beta(_beta), f0(_f0), f1(_f1) { }
};
struct Range {
bool present;
float f0;
float f1;
Range() : present(false), f0(0.f), f1(0.f) { }
};
struct Guidance {
FftBand fftBands[3];
PhaseLockBand phaseLockBands[5];
Range kick;
Range lowPercussive;
Range highPercussive;
Range phaseReset;
Range channelLock;
};
struct Parameters {
int fftSize;
double sampleRate;
Parameters(int _fftSize, double _sampleRate) :
fftSize(_fftSize), sampleRate(_sampleRate) { }
};
Guide(Parameters parameters) :
m_parameters(parameters) { }
void calculate(double ratio,
const float *const magnitudes,
const int *const troughs,
const float *const prevMagnitudes,
const BinSegmenter::Segmentation &segmentation,
const BinSegmenter::Segmentation &prevSegmentation,
const BinSegmenter::Segmentation &nextSegmentation,
Guidance &guidance) const {
bool potentialKick = checkPotentialKick(magnitudes, prevMagnitudes);
guidance.kick.present = false;
guidance.lowPercussive.present = false;
guidance.highPercussive.present = false;
guidance.phaseReset.present = false;
guidance.channelLock.present = true;
guidance.channelLock.f0 = 0.0;
guidance.channelLock.f1 = 600.0;
if (segmentation.percussiveBelow > 40.0) {
guidance.lowPercussive.present = true;
guidance.lowPercussive.f0 = 0.0;
guidance.lowPercussive.f1 = segmentation.percussiveBelow;
}
if (potentialKick && prevSegmentation.percussiveBelow < 40.0) {
guidance.kick = guidance.lowPercussive;
}
if (segmentation.residualAbove > segmentation.percussiveAbove) {
guidance.highPercussive.present = true;
guidance.highPercussive.f0 = segmentation.percussiveAbove;
guidance.highPercussive.f1 = segmentation.residualAbove;
}
double bigGap = 4000.0;
if (ratio > 1.0 &&
segmentation.residualAbove >
segmentation.percussiveAbove + bigGap &&
prevSegmentation.residualAbove <
prevSegmentation.percussiveAbove + bigGap) {
guidance.phaseReset.present = true;
guidance.phaseReset.f0 = std::min(segmentation.percussiveAbove,
nextSegmentation.percussiveAbove);
if (guidance.phaseReset.f0 < 200.0) {
guidance.phaseReset.f0 = 0.0;
}
guidance.phaseReset.f1 = std::max(segmentation.residualAbove,
nextSegmentation.residualAbove);
}
double higher = snapToTrough(4800.0, troughs);
double lower = snapToTrough(700.0, troughs);
double nyquist = m_parameters.sampleRate / 2.0;
guidance.fftBands[0].fftSize = roundUp(int(ceil(nyquist/8.0)));
guidance.fftBands[0].f0 = 0.0;
guidance.fftBands[0].f1 = lower;
guidance.fftBands[1].fftSize = roundUp(int(ceil(nyquist/16.0)));
guidance.fftBands[1].f0 = lower;
guidance.fftBands[1].f1 = higher;
guidance.fftBands[2].fftSize = roundUp(int(ceil(nyquist/32.0)));
guidance.fftBands[2].f0 = higher;
guidance.fftBands[2].f1 = nyquist;
double mid = std::max(lower, 1600.0);
guidance.phaseLockBands[0].p = 1;
guidance.phaseLockBands[0].beta = betaFor(300.0, ratio);
guidance.phaseLockBands[0].f0 = 0.0;
guidance.phaseLockBands[0].f1 = lower;
guidance.phaseLockBands[1].p = 2;
guidance.phaseLockBands[1].beta = betaFor(1600.0, ratio);
guidance.phaseLockBands[1].f0 = lower;
guidance.phaseLockBands[1].f1 = mid;
guidance.phaseLockBands[2].p = 3;
guidance.phaseLockBands[2].beta = betaFor(5000.0, ratio);
guidance.phaseLockBands[2].f0 = mid;
guidance.phaseLockBands[2].f1 = higher;
guidance.phaseLockBands[3].p = 4;
guidance.phaseLockBands[3].beta = betaFor(10000.0, ratio);
guidance.phaseLockBands[3].f0 = higher;
guidance.phaseLockBands[3].f1 = nyquist;
}
protected:
Parameters m_parameters;
int binForFrequency(double f) const {
return int(round(f * double(m_parameters.fftSize) /
m_parameters.sampleRate));
}
double frequencyForBin(int b) const {
return (double(b) * m_parameters.sampleRate)
/ double(m_parameters.fftSize);
}
// near-dupe with R2 RubberBandStretcher::Impl
int roundUp(int value) const {
if (value < 1) return 1;
if (!(value & (value - 1))) return value;
int bits = 0;
while (value) { ++bits; value >>= 1; }
value = 1 << bits;
return value;
}
bool checkPotentialKick(const float *const magnitudes,
const float *const prevMagnitudes) const {
int b = binForFrequency(200.0);
float here = 0.0, there = 0.0;
for (int i = 1; i <= b; ++i) {
here += magnitudes[i];
}
for (int i = 1; i <= b; ++i) {
there += prevMagnitudes[i];
}
return (here > 10.e-3f && here > there * 1.4f);
}
double snapToTrough(double f, const int *const troughs) const {
return frequencyForBin(troughs[binForFrequency(f)]);
}
double betaFor(double f, double ratio) const {
double b = (2.0 + ratio) / 3.0;
double limit = 10000.0;
if (f > limit) {
return b;
} else {
return 1.0 + f * (b - 1.0) / limit;
}
}
};
}
#endif

View File

@@ -42,7 +42,7 @@ public:
// a value greater than the p nearest neighbours on each side. The
// array must have length n where n is the size passed the the
// constructor.
void findNearestAndNextPeaks(const T *const v,
void findNearestAndNextPeaks(const T *v,
int p,
int *nearest,
int *next = nullptr)
@@ -55,7 +55,7 @@ public:
// optionally next, starting to write at index rangeStart - so
// these arrays must have the full length even if rangeCount is
// shorter. Leave the rest of nearest and/or next unmodified.
void findNearestAndNextPeaks(const T *const v,
void findNearestAndNextPeaks(const T *v,
int rangeStart,
int rangeCount,
int p,

View File

@@ -28,6 +28,7 @@
#include <memory>
#include "BinSegmenter.h"
#include "Guide.h"
#include "Peak.h"
#include "../common/FFT.h"
@@ -39,7 +40,7 @@ namespace RubberBand
class R3StretcherImpl
{
public:
R3StretcherImpl(int sampleRate, int channels);
R3StretcherImpl(double sampleRate, int channels);
~R3StretcherImpl();
void reset();
@@ -51,46 +52,12 @@ public:
double getPitchScale() const;
protected:
int m_sampleRate;
double m_sampleRate;
int m_channels;
double m_timeRatio;
double m_pitchScale;
struct FftBand {
int fftSize;
float f0;
float f1;
FftBand(int _s, float _f0, float _f1) :
fftSize(_s), f0(_f0), f1(_f1) { }
};
struct PhaseLockBand {
int p;
float beta;
float f0;
float f1;
PhaseLockBand(int _p, float _beta, float _f0, float _f1) :
p(_p), beta(_beta), f0(_f0), f1(_f1) { }
};
struct Range {
bool present;
float f0;
float f1;
Range() : present(false), f0(0.f), f1(0.f) { }
};
struct Guidance {
FftBand fftBands[3];
PhaseLockBand phaseLockBands[5];
Range kick;
Range lowPercussive;
Range phaseReset;
Range highPercussive;
Range channelLock;
};
struct ChannelScaleData {
int fftSize;
int bufSize; // size of every array here: fftSize/2 + 1
@@ -133,7 +100,7 @@ protected:
BinSegmenter::Segmentation segmentation;
BinSegmenter::Segmentation prevSegmentation;
BinSegmenter::Segmentation nextSegmentation;
Guidance guidance;
Guide::Guidance guidance;
};
std::map<int, std::shared_ptr<FFT>> m_ffts;