2022-05-19 16:31:21 +01:00
|
|
|
/* -*- 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.
|
2023-01-10 11:10:06 +00:00
|
|
|
Copyright 2007-2023 Particular Programs Ltd.
|
2022-05-19 16:31:21 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2022-06-21 16:06:16 +01:00
|
|
|
#include "../common/Log.h"
|
2022-07-14 10:02:39 +01:00
|
|
|
#include "../common/Profiler.h"
|
2022-06-21 16:06:16 +01:00
|
|
|
|
2022-05-24 12:00:54 +01:00
|
|
|
#include <functional>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
2022-05-19 16:31:21 +01:00
|
|
|
namespace RubberBand
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
class Guide
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
struct FftBand {
|
|
|
|
|
int fftSize;
|
2022-05-24 15:39:28 +01:00
|
|
|
double f0;
|
|
|
|
|
double f1;
|
|
|
|
|
FftBand(int _s, double _f0, double _f1) :
|
2022-05-19 16:31:21 +01:00
|
|
|
fftSize(_s), f0(_f0), f1(_f1) { }
|
2022-05-19 17:28:38 +01:00
|
|
|
FftBand() :
|
|
|
|
|
fftSize(0), f0(0.f), f1(0.f) { }
|
2022-05-19 16:31:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct PhaseLockBand {
|
|
|
|
|
int p;
|
2022-05-24 15:39:28 +01:00
|
|
|
double beta;
|
|
|
|
|
double f0;
|
|
|
|
|
double f1;
|
|
|
|
|
PhaseLockBand(int _p, double _beta, double _f0, double _f1) :
|
2022-05-19 16:31:21 +01:00
|
|
|
p(_p), beta(_beta), f0(_f0), f1(_f1) { }
|
2022-05-19 17:28:38 +01:00
|
|
|
PhaseLockBand() :
|
|
|
|
|
p(0), beta(1.0), f0(0.f), f1(0.f) { }
|
2022-05-19 16:31:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Range {
|
|
|
|
|
bool present;
|
2022-05-24 15:39:28 +01:00
|
|
|
double f0;
|
|
|
|
|
double f1;
|
|
|
|
|
Range(bool _present, double _f0, double _f1) :
|
2022-05-19 17:28:38 +01:00
|
|
|
present(_present), f0(_f0), f1(_f1) { }
|
|
|
|
|
Range() :
|
|
|
|
|
present(false), f0(0.f), f1(0.f) { }
|
2022-05-19 16:31:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Guidance {
|
|
|
|
|
FftBand fftBands[3];
|
2022-08-03 14:16:17 +01:00
|
|
|
int fftBandCount;
|
2022-06-15 10:39:41 +01:00
|
|
|
PhaseLockBand phaseLockBands[4];
|
2022-08-03 14:16:17 +01:00
|
|
|
int phaseLockBandCount;
|
2022-05-19 16:31:21 +01:00
|
|
|
Range kick;
|
2022-06-13 09:40:26 +01:00
|
|
|
Range preKick;
|
2022-06-15 10:39:41 +01:00
|
|
|
Range highUnlocked;
|
2022-05-19 16:31:21 +01:00
|
|
|
Range phaseReset;
|
|
|
|
|
Range channelLock;
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-19 17:28:38 +01:00
|
|
|
struct BandLimits {
|
2022-05-19 16:31:21 +01:00
|
|
|
int fftSize;
|
2022-05-24 15:39:28 +01:00
|
|
|
double f0min;
|
|
|
|
|
double f1max;
|
2022-05-24 16:54:05 +01:00
|
|
|
int b0min;
|
|
|
|
|
int b1max;
|
|
|
|
|
BandLimits(int _fftSize, double _rate, double _f0min, double _f1max) :
|
|
|
|
|
fftSize(_fftSize), f0min(_f0min), f1max(_f1max),
|
|
|
|
|
b0min(int(floor(f0min * fftSize / _rate))),
|
|
|
|
|
b1max(int(ceil(f1max * fftSize / _rate))) { }
|
2022-05-19 17:28:38 +01:00
|
|
|
BandLimits() :
|
2022-05-24 16:54:05 +01:00
|
|
|
fftSize(0), f0min(0.f), f1max(0.f), b0min(0), b1max(0) { }
|
2022-05-19 17:28:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Configuration {
|
2022-05-20 16:48:44 +01:00
|
|
|
int longestFftSize;
|
2022-05-24 12:00:54 +01:00
|
|
|
int shortestFftSize;
|
|
|
|
|
int classificationFftSize;
|
2022-05-19 17:28:38 +01:00
|
|
|
BandLimits fftBandLimits[3];
|
2022-08-03 14:16:17 +01:00
|
|
|
int fftBandLimitCount;
|
|
|
|
|
Configuration() :
|
|
|
|
|
longestFftSize(0), shortestFftSize(0), classificationFftSize(0),
|
|
|
|
|
fftBandLimitCount(0) { }
|
2022-05-19 17:28:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Parameters {
|
2022-05-19 16:31:21 +01:00
|
|
|
double sampleRate;
|
2022-07-14 13:44:22 +01:00
|
|
|
bool singleWindowMode;
|
|
|
|
|
Parameters(double _sampleRate, bool _singleWindow) :
|
2022-07-14 11:55:21 +01:00
|
|
|
sampleRate(_sampleRate),
|
2022-07-14 13:44:22 +01:00
|
|
|
singleWindowMode(_singleWindow) { }
|
2022-05-19 16:31:21 +01:00
|
|
|
};
|
|
|
|
|
|
2022-06-21 16:06:16 +01:00
|
|
|
Guide(Parameters parameters, Log log) :
|
2022-05-19 17:28:38 +01:00
|
|
|
m_parameters(parameters),
|
2022-08-03 14:16:17 +01:00
|
|
|
m_log(log)
|
2022-05-19 17:28:38 +01:00
|
|
|
{
|
|
|
|
|
double rate = m_parameters.sampleRate;
|
2022-08-03 14:16:17 +01:00
|
|
|
double nyquist = rate / 2.0;
|
2022-05-24 16:54:05 +01:00
|
|
|
|
2022-07-14 13:44:22 +01:00
|
|
|
m_log.log(1, "Guide: rate and single-window mode",
|
|
|
|
|
rate, m_parameters.singleWindowMode);
|
2022-07-14 11:55:21 +01:00
|
|
|
|
2022-09-26 16:02:13 +01:00
|
|
|
int classificationFftSize = roundUpDiv(parameters.sampleRate, 32);
|
|
|
|
|
|
|
|
|
|
int minClassificationFftSize = 1024;
|
|
|
|
|
if (classificationFftSize < minClassificationFftSize) {
|
|
|
|
|
m_log.log(1, "Guide: sample rate is too low to work well");
|
|
|
|
|
m_log.log(1, "Guide: rounding up classification FFT size from and to", classificationFftSize, minClassificationFftSize);
|
|
|
|
|
classificationFftSize = minClassificationFftSize;
|
|
|
|
|
}
|
2022-08-03 14:16:17 +01:00
|
|
|
|
|
|
|
|
m_configuration.classificationFftSize = classificationFftSize;
|
|
|
|
|
|
|
|
|
|
m_log.log(1, "Guide: classification FFT size",
|
|
|
|
|
m_configuration.classificationFftSize);
|
|
|
|
|
|
2022-07-14 13:44:22 +01:00
|
|
|
if (m_parameters.singleWindowMode) {
|
2022-08-03 14:16:17 +01:00
|
|
|
|
|
|
|
|
// Single-window mode
|
|
|
|
|
|
|
|
|
|
m_configuration.longestFftSize = classificationFftSize;
|
|
|
|
|
m_configuration.shortestFftSize = classificationFftSize;
|
|
|
|
|
|
|
|
|
|
m_defaultLower = nyquist;
|
2022-07-14 13:44:22 +01:00
|
|
|
m_minLower = m_defaultLower;
|
|
|
|
|
m_maxLower = m_defaultLower;
|
2022-08-03 14:16:17 +01:00
|
|
|
|
|
|
|
|
m_defaultHigher = nyquist;
|
2022-07-14 13:44:22 +01:00
|
|
|
m_minHigher = m_defaultHigher;
|
|
|
|
|
m_maxHigher = m_defaultHigher;
|
2022-08-03 14:16:17 +01:00
|
|
|
|
|
|
|
|
m_configuration.fftBandLimitCount = 1;
|
|
|
|
|
|
|
|
|
|
m_configuration.fftBandLimits[0] =
|
|
|
|
|
BandLimits(classificationFftSize, rate, 0.0, nyquist);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
// The normal multi-window mode
|
|
|
|
|
|
|
|
|
|
m_configuration.longestFftSize = classificationFftSize * 2;
|
|
|
|
|
m_configuration.shortestFftSize = classificationFftSize / 2;
|
|
|
|
|
|
|
|
|
|
m_defaultLower = 700.0;
|
|
|
|
|
m_minLower = 500.0;
|
|
|
|
|
m_maxLower = 1100.0;
|
|
|
|
|
|
|
|
|
|
m_defaultHigher = 4800.0;
|
|
|
|
|
m_minHigher = 4000.0;
|
|
|
|
|
m_maxHigher = 7000.0;
|
|
|
|
|
|
|
|
|
|
m_configuration.fftBandLimitCount = 3;
|
|
|
|
|
|
|
|
|
|
m_configuration.fftBandLimits[0] =
|
|
|
|
|
BandLimits(m_configuration.longestFftSize,
|
|
|
|
|
rate, 0.0, m_maxLower);
|
2022-06-22 13:42:58 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
// This is the classification and fallback FFT: we need it
|
|
|
|
|
// to go up to Nyquist so we can seamlessly switch to it
|
|
|
|
|
// for longer stretches, and down to 0.0 so we can use it
|
|
|
|
|
// for unity in offline mode
|
|
|
|
|
|
|
|
|
|
m_configuration.fftBandLimits[1] =
|
|
|
|
|
BandLimits(classificationFftSize,
|
|
|
|
|
rate, 0.0, nyquist);
|
2022-05-24 16:54:05 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
m_configuration.fftBandLimits[2] =
|
|
|
|
|
BandLimits(m_configuration.shortestFftSize,
|
|
|
|
|
rate, m_minHigher, nyquist);
|
|
|
|
|
}
|
2022-05-19 17:28:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Configuration &getConfiguration() const {
|
|
|
|
|
return m_configuration;
|
|
|
|
|
}
|
2022-05-19 16:31:21 +01:00
|
|
|
|
2022-06-09 14:29:51 +01:00
|
|
|
void updateGuidance(double ratio,
|
2022-06-14 15:01:44 +01:00
|
|
|
int outhop,
|
2022-07-06 10:40:10 +01:00
|
|
|
const process_t *const magnitudes,
|
|
|
|
|
const process_t *const prevMagnitudes,
|
|
|
|
|
const process_t *const nextMagnitudes,
|
2022-06-09 14:29:51 +01:00
|
|
|
const BinSegmenter::Segmentation &segmentation,
|
|
|
|
|
const BinSegmenter::Segmentation &prevSegmentation,
|
|
|
|
|
const BinSegmenter::Segmentation &nextSegmentation,
|
2022-07-06 10:40:10 +01:00
|
|
|
process_t meanMagnitude,
|
2022-06-17 15:01:26 +01:00
|
|
|
int unityCount,
|
2022-06-24 10:51:40 +01:00
|
|
|
bool realtime,
|
2022-07-04 10:52:50 +01:00
|
|
|
bool tighterChannelLock,
|
2022-06-09 14:29:51 +01:00
|
|
|
Guidance &guidance) const {
|
2022-05-19 16:31:21 +01:00
|
|
|
|
2022-07-14 10:02:39 +01:00
|
|
|
Profiler profiler("Guide::updateGuidance");
|
|
|
|
|
|
2022-06-17 15:01:26 +01:00
|
|
|
bool hadPhaseReset = guidance.phaseReset.present;
|
|
|
|
|
|
|
|
|
|
guidance.phaseReset.present = false;
|
2022-05-19 16:31:21 +01:00
|
|
|
guidance.kick.present = false;
|
2022-06-13 09:40:26 +01:00
|
|
|
guidance.preKick.present = false;
|
2022-06-15 10:39:41 +01:00
|
|
|
guidance.highUnlocked.present = false;
|
2022-06-17 15:01:26 +01:00
|
|
|
guidance.channelLock.present = false;
|
2022-05-19 16:31:21 +01:00
|
|
|
|
2022-05-27 11:45:28 +01:00
|
|
|
double nyquist = m_parameters.sampleRate / 2.0;
|
|
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
if (m_parameters.singleWindowMode) {
|
|
|
|
|
|
|
|
|
|
// All the fft and phase-lock bands are fixed in this
|
|
|
|
|
// mode. We'll still need to continue to set up phase
|
|
|
|
|
// reset ranges etc, including the unity case.
|
|
|
|
|
|
|
|
|
|
guidance.fftBandCount = 1;
|
|
|
|
|
guidance.fftBands[0].fftSize = m_configuration.classificationFftSize;
|
|
|
|
|
guidance.fftBands[0].f0 = 0.0;
|
|
|
|
|
guidance.fftBands[0].f1 = nyquist;
|
|
|
|
|
|
|
|
|
|
guidance.phaseLockBandCount = 3;
|
|
|
|
|
|
|
|
|
|
guidance.phaseLockBands[0].p = 1;
|
|
|
|
|
guidance.phaseLockBands[0].beta = betaFor(1200.0, ratio);
|
|
|
|
|
guidance.phaseLockBands[0].f0 = 0.0;
|
|
|
|
|
guidance.phaseLockBands[0].f1 = 1600.0;
|
|
|
|
|
|
|
|
|
|
guidance.phaseLockBands[1].p = 2;
|
2022-09-29 15:57:11 +01:00
|
|
|
guidance.phaseLockBands[1].beta = betaFor(5000.0, ratio);
|
2022-08-03 14:16:17 +01:00
|
|
|
guidance.phaseLockBands[1].f0 = 1600.0;
|
|
|
|
|
guidance.phaseLockBands[1].f1 = 7000.0;
|
|
|
|
|
|
|
|
|
|
guidance.phaseLockBands[2].p = 5;
|
|
|
|
|
guidance.phaseLockBands[2].beta = betaFor(10000.0, ratio);
|
|
|
|
|
guidance.phaseLockBands[2].f0 = 7000.0;
|
|
|
|
|
guidance.phaseLockBands[2].f1 = nyquist;
|
2022-06-17 15:01:26 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
if (outhop > 256) {
|
|
|
|
|
guidance.phaseLockBands[2].p = 4;
|
|
|
|
|
}
|
2022-09-26 16:02:13 +01:00
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
|
if (guidance.phaseLockBands[i].f0 > nyquist) {
|
|
|
|
|
guidance.phaseLockBands[i].f0 = nyquist;
|
|
|
|
|
}
|
|
|
|
|
if (guidance.phaseLockBands[i].f1 > nyquist) {
|
|
|
|
|
guidance.phaseLockBands[i].f1 = nyquist;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-03 14:16:17 +01:00
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
// The normal multi-window mode
|
|
|
|
|
|
|
|
|
|
guidance.fftBandCount = 3;
|
|
|
|
|
guidance.fftBands[0].fftSize = m_configuration.longestFftSize;
|
|
|
|
|
guidance.fftBands[1].fftSize = m_configuration.classificationFftSize;
|
|
|
|
|
guidance.fftBands[2].fftSize = m_configuration.shortestFftSize;
|
|
|
|
|
|
|
|
|
|
guidance.phaseLockBandCount = 4;
|
|
|
|
|
|
|
|
|
|
// This is a vital stop case for PhaseAdvance
|
|
|
|
|
guidance.phaseLockBands[3].f1 = nyquist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We've set the counts, and for single-window mode we've set
|
|
|
|
|
// the band ranges as well - in normal multi-window mode we
|
|
|
|
|
// still have to do that, but we should do these first
|
|
|
|
|
|
2022-06-17 15:01:26 +01:00
|
|
|
if (meanMagnitude < 1.0e-6) {
|
|
|
|
|
updateForSilence(guidance);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (unityCount > 0) {
|
|
|
|
|
updateForUnity(guidance,
|
|
|
|
|
hadPhaseReset,
|
2022-06-24 10:51:40 +01:00
|
|
|
segmentation,
|
|
|
|
|
realtime);
|
2022-05-27 11:45:28 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-19 16:31:21 +01:00
|
|
|
guidance.channelLock.present = true;
|
|
|
|
|
guidance.channelLock.f0 = 0.0;
|
2022-07-04 10:52:50 +01:00
|
|
|
|
|
|
|
|
if (tighterChannelLock) {
|
|
|
|
|
guidance.channelLock.f1 = nyquist;
|
|
|
|
|
} else {
|
|
|
|
|
guidance.channelLock.f1 = 600.0;
|
|
|
|
|
}
|
2022-05-19 16:31:21 +01:00
|
|
|
|
2022-08-04 12:08:15 +01:00
|
|
|
if (!m_parameters.singleWindowMode) {
|
|
|
|
|
|
|
|
|
|
bool kick =
|
|
|
|
|
(segmentation.percussiveBelow > 40.0) &&
|
|
|
|
|
(prevSegmentation.percussiveBelow < 40.0) &&
|
|
|
|
|
checkPotentialKick(magnitudes, prevMagnitudes);
|
|
|
|
|
|
|
|
|
|
bool futureKick = !kick &&
|
|
|
|
|
(nextSegmentation.percussiveBelow > 40.0) &&
|
|
|
|
|
(segmentation.percussiveBelow < 40.0) &&
|
|
|
|
|
checkPotentialKick(nextMagnitudes, magnitudes);
|
|
|
|
|
|
|
|
|
|
if (kick) {
|
|
|
|
|
guidance.kick.present = true;
|
|
|
|
|
guidance.kick.f0 = 0.0;
|
|
|
|
|
guidance.kick.f1 = segmentation.percussiveBelow;
|
|
|
|
|
} else if (futureKick) {
|
|
|
|
|
guidance.preKick.present = true;
|
|
|
|
|
guidance.preKick.f0 = 0.0;
|
|
|
|
|
guidance.preKick.f1 = nextSegmentation.percussiveBelow;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-13 09:40:26 +01:00
|
|
|
/*
|
|
|
|
|
std::cout << "d:"
|
|
|
|
|
<< prevSegmentation.percussiveBelow << ","
|
|
|
|
|
<< segmentation.percussiveBelow << ","
|
|
|
|
|
<< nextSegmentation.percussiveBelow << ","
|
|
|
|
|
<< checkPotentialKick(magnitudes, prevMagnitudes) << ","
|
|
|
|
|
<< checkPotentialKick(nextMagnitudes, magnitudes) << ","
|
|
|
|
|
<< (kick ? "K" : "N") << ","
|
|
|
|
|
<< (futureKick ? "F" : "N") << std::endl;
|
|
|
|
|
*/
|
2022-05-19 16:31:21 +01:00
|
|
|
|
|
|
|
|
if (segmentation.residualAbove > segmentation.percussiveAbove) {
|
2022-06-15 10:39:41 +01:00
|
|
|
guidance.highUnlocked.present = true;
|
|
|
|
|
guidance.highUnlocked.f0 = segmentation.percussiveAbove;
|
|
|
|
|
guidance.highUnlocked.f1 = segmentation.residualAbove;
|
2022-05-19 16:31:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double bigGap = 4000.0;
|
2022-06-09 17:23:26 +01:00
|
|
|
if (segmentation.residualAbove >
|
2022-05-19 16:31:21 +01:00
|
|
|
segmentation.percussiveAbove + bigGap &&
|
|
|
|
|
prevSegmentation.residualAbove <
|
|
|
|
|
prevSegmentation.percussiveAbove + bigGap) {
|
|
|
|
|
guidance.phaseReset.present = true;
|
|
|
|
|
guidance.phaseReset.f0 = std::min(segmentation.percussiveAbove,
|
|
|
|
|
nextSegmentation.percussiveAbove);
|
2022-06-09 17:23:26 +01:00
|
|
|
guidance.phaseReset.f1 = std::max(segmentation.residualAbove,
|
|
|
|
|
nextSegmentation.residualAbove);
|
2022-05-19 16:31:21 +01:00
|
|
|
if (guidance.phaseReset.f0 < 200.0) {
|
|
|
|
|
guidance.phaseReset.f0 = 0.0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-19 17:28:38 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
if (!m_parameters.singleWindowMode) {
|
|
|
|
|
|
|
|
|
|
// The normal multi-window mode. For single-window we did
|
|
|
|
|
// this already.
|
|
|
|
|
|
|
|
|
|
double prevLower = guidance.fftBands[0].f1;
|
|
|
|
|
double lower = descendToValley(prevLower, magnitudes);
|
|
|
|
|
if (lower > m_maxLower || lower < m_minLower) {
|
|
|
|
|
lower = m_defaultLower;
|
|
|
|
|
}
|
2022-06-09 14:29:51 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
double prevHigher = guidance.fftBands[1].f1;
|
|
|
|
|
double higher = descendToValley(prevHigher, magnitudes);
|
|
|
|
|
if (higher > m_maxHigher || higher < m_minHigher) {
|
|
|
|
|
higher = m_defaultHigher;
|
|
|
|
|
}
|
2022-06-09 14:29:51 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
guidance.fftBands[0].f0 = 0.0;
|
|
|
|
|
guidance.fftBands[0].f1 = lower;
|
2022-06-07 14:13:24 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
guidance.fftBands[1].f0 = lower;
|
|
|
|
|
guidance.fftBands[1].f1 = higher;
|
2022-05-19 16:31:21 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
guidance.fftBands[2].f0 = higher;
|
|
|
|
|
guidance.fftBands[2].f1 = nyquist;
|
2022-06-14 15:01:44 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
if (outhop > 256) {
|
|
|
|
|
guidance.fftBands[1].f1 = nyquist;
|
|
|
|
|
guidance.fftBands[2].f0 = nyquist;
|
|
|
|
|
}
|
2022-05-19 16:31:21 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
double mid = std::max(lower, 1600.0);
|
2022-05-19 16:31:21 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
guidance.phaseLockBands[0].p = 1;
|
|
|
|
|
guidance.phaseLockBands[0].beta = betaFor(300.0, ratio);
|
|
|
|
|
guidance.phaseLockBands[0].f0 = 0.0;
|
|
|
|
|
guidance.phaseLockBands[0].f1 = lower;
|
2022-05-19 16:31:21 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
guidance.phaseLockBands[1].p = 2;
|
|
|
|
|
guidance.phaseLockBands[1].beta = betaFor(1600.0, ratio);
|
|
|
|
|
guidance.phaseLockBands[1].f0 = lower;
|
|
|
|
|
guidance.phaseLockBands[1].f1 = mid;
|
2022-05-19 16:31:21 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
guidance.phaseLockBands[2].p = 3;
|
2022-09-29 15:57:11 +01:00
|
|
|
guidance.phaseLockBands[2].beta = betaFor(5000.0, ratio);
|
2022-08-03 14:16:17 +01:00
|
|
|
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;
|
2022-05-19 16:31:21 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
if (outhop > 256) {
|
|
|
|
|
guidance.phaseLockBands[3].p = 3;
|
|
|
|
|
}
|
2022-07-14 11:55:21 +01:00
|
|
|
}
|
2022-06-15 10:39:41 +01:00
|
|
|
|
|
|
|
|
if (ratio > 2.0) {
|
|
|
|
|
|
|
|
|
|
// For very long stretches, diffuse is better than
|
|
|
|
|
// metallic - gradually unlock the higher frequencies and
|
|
|
|
|
// reduce the channel lock
|
|
|
|
|
|
|
|
|
|
double channelLimit = guidance.channelLock.f1;
|
|
|
|
|
channelLimit = channelLimit - (ratio - 2.0) * 150.0;
|
|
|
|
|
if (channelLimit < 100.0) channelLimit = 100.0;
|
|
|
|
|
guidance.channelLock.f1 = channelLimit;
|
|
|
|
|
|
|
|
|
|
double unlockedAbove = 12000.0 - (ratio - 2.0) * 400.0;
|
|
|
|
|
if (unlockedAbove < channelLimit) unlockedAbove = channelLimit;
|
|
|
|
|
if (guidance.highUnlocked.present) {
|
|
|
|
|
guidance.highUnlocked.f0 = std::min(guidance.highUnlocked.f0,
|
|
|
|
|
unlockedAbove);
|
|
|
|
|
} else {
|
|
|
|
|
guidance.highUnlocked.f0 = unlockedAbove;
|
|
|
|
|
}
|
|
|
|
|
guidance.highUnlocked.f1 = nyquist;
|
|
|
|
|
guidance.highUnlocked.present = true;
|
|
|
|
|
}
|
2022-05-27 11:45:28 +01:00
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
/*
|
2022-05-24 12:00:54 +01:00
|
|
|
std::ostringstream str;
|
|
|
|
|
str << "Guidance: FFT bands: ["
|
|
|
|
|
<< guidance.fftBands[0].fftSize << " from "
|
|
|
|
|
<< guidance.fftBands[0].f0 << " to " << guidance.fftBands[0].f1
|
|
|
|
|
<< ", "
|
|
|
|
|
<< guidance.fftBands[1].fftSize << " from "
|
|
|
|
|
<< guidance.fftBands[1].f0 << " to " << guidance.fftBands[1].f1
|
|
|
|
|
<< ", "
|
|
|
|
|
<< guidance.fftBands[2].fftSize << " from "
|
|
|
|
|
<< guidance.fftBands[2].f0 << " to " << guidance.fftBands[2].f1
|
|
|
|
|
<< "]; phase reset range: ["
|
|
|
|
|
<< guidance.phaseReset.present << " from "
|
|
|
|
|
<< guidance.phaseReset.f0 << " to " << guidance.phaseReset.f1
|
|
|
|
|
<< "]" << std::endl;
|
2022-08-03 14:16:17 +01:00
|
|
|
|
|
|
|
|
m_log.log(1, str.str().c_str());
|
|
|
|
|
*/
|
2022-05-19 16:31:21 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-22 13:42:58 +01:00
|
|
|
void setDebugLevel(int level) {
|
|
|
|
|
m_log.setDebugLevel(level);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-19 16:31:21 +01:00
|
|
|
protected:
|
|
|
|
|
Parameters m_parameters;
|
2022-06-21 16:06:16 +01:00
|
|
|
Log m_log;
|
2022-05-19 17:28:38 +01:00
|
|
|
Configuration m_configuration;
|
|
|
|
|
|
2022-05-24 15:21:13 +01:00
|
|
|
double m_minLower;
|
|
|
|
|
double m_minHigher;
|
2022-05-19 17:28:38 +01:00
|
|
|
double m_defaultLower;
|
|
|
|
|
double m_defaultHigher;
|
|
|
|
|
double m_maxLower;
|
|
|
|
|
double m_maxHigher;
|
2022-05-19 16:31:21 +01:00
|
|
|
|
2022-06-17 15:01:26 +01:00
|
|
|
void updateForSilence(Guidance &guidance) const {
|
|
|
|
|
// std::cout << "phase reset on silence" << std::endl;
|
|
|
|
|
double nyquist = m_parameters.sampleRate / 2.0;
|
2022-08-03 14:16:17 +01:00
|
|
|
if (!m_parameters.singleWindowMode) {
|
|
|
|
|
guidance.fftBands[0].f0 = 0.0;
|
|
|
|
|
guidance.fftBands[0].f1 = 0.0;
|
|
|
|
|
guidance.fftBands[1].f0 = 0.0;
|
|
|
|
|
guidance.fftBands[1].f1 = nyquist;
|
|
|
|
|
guidance.fftBands[2].f0 = nyquist;
|
|
|
|
|
guidance.fftBands[2].f1 = nyquist;
|
|
|
|
|
}
|
2022-06-17 15:01:26 +01:00
|
|
|
guidance.phaseReset.present = true;
|
|
|
|
|
guidance.phaseReset.f0 = 0.0;
|
|
|
|
|
guidance.phaseReset.f1 = nyquist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateForUnity(Guidance &guidance,
|
|
|
|
|
bool hadPhaseReset,
|
2022-06-24 10:51:40 +01:00
|
|
|
const BinSegmenter::Segmentation &segmentation,
|
|
|
|
|
bool realtime) const {
|
2022-06-17 15:01:26 +01:00
|
|
|
|
|
|
|
|
// std::cout << "unity" << std::endl;
|
|
|
|
|
|
|
|
|
|
double nyquist = m_parameters.sampleRate / 2.0;
|
|
|
|
|
|
2022-06-24 10:51:40 +01:00
|
|
|
if (!realtime) {
|
|
|
|
|
// ratio can't change, so we are just running 1.0 ratio
|
|
|
|
|
// throughout
|
2022-08-03 14:16:17 +01:00
|
|
|
if (!m_parameters.singleWindowMode) {
|
|
|
|
|
guidance.fftBands[0].f0 = 0.0;
|
|
|
|
|
guidance.fftBands[0].f1 = 0.0;
|
|
|
|
|
guidance.fftBands[1].f0 = 0.0;
|
|
|
|
|
guidance.fftBands[1].f1 = nyquist;
|
|
|
|
|
guidance.fftBands[2].f0 = nyquist;
|
|
|
|
|
guidance.fftBands[2].f1 = nyquist;
|
|
|
|
|
}
|
2022-06-24 10:51:40 +01:00
|
|
|
guidance.phaseReset.present = true;
|
|
|
|
|
guidance.phaseReset.f0 = 0.0;
|
|
|
|
|
guidance.phaseReset.f1 = nyquist;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-03 14:16:17 +01:00
|
|
|
if (!m_parameters.singleWindowMode) {
|
|
|
|
|
guidance.fftBands[0].f0 = 0.0;
|
|
|
|
|
guidance.fftBands[0].f1 = m_minLower;
|
|
|
|
|
guidance.fftBands[1].f0 = m_minLower;
|
|
|
|
|
guidance.fftBands[1].f1 = m_minHigher;
|
|
|
|
|
guidance.fftBands[2].f0 = m_minHigher;
|
2022-07-14 12:07:43 +01:00
|
|
|
guidance.fftBands[2].f1 = nyquist;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 15:01:26 +01:00
|
|
|
guidance.phaseReset.present = true;
|
|
|
|
|
|
|
|
|
|
if (!hadPhaseReset) {
|
|
|
|
|
guidance.phaseReset.f0 = 16000.0;
|
|
|
|
|
guidance.phaseReset.f1 = nyquist;
|
|
|
|
|
// std::cout << "f0 = " << guidance.phaseReset.f0 << std::endl;
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
guidance.phaseReset.f0 *= 0.9;
|
|
|
|
|
guidance.phaseReset.f1 *= 1.1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (guidance.phaseReset.f0 < segmentation.residualAbove) {
|
|
|
|
|
guidance.phaseReset.f0 = std::min(guidance.phaseReset.f0,
|
|
|
|
|
segmentation.percussiveAbove);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (guidance.phaseReset.f1 > 16000.0) {
|
|
|
|
|
guidance.phaseReset.f1 = nyquist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (guidance.phaseReset.f0 < 100.0) {
|
|
|
|
|
guidance.phaseReset.f0 = 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (guidance.phaseReset.f0 > 0.0) {
|
|
|
|
|
// std::cout << unityCount << ": f0 = " << guidance.phaseReset.f0
|
|
|
|
|
// << ", f1 = " << guidance.phaseReset.f1 << std::endl;
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 10:40:10 +01:00
|
|
|
bool checkPotentialKick(const process_t *const magnitudes,
|
|
|
|
|
const process_t *const prevMagnitudes) const {
|
2022-06-13 10:08:05 +01:00
|
|
|
int b = binForFrequency(200.0, m_configuration.classificationFftSize,
|
|
|
|
|
m_parameters.sampleRate);
|
2022-07-06 10:40:10 +01:00
|
|
|
process_t here = 0.0, there = 0.0;
|
2022-05-19 16:31:21 +01:00
|
|
|
for (int i = 1; i <= b; ++i) {
|
|
|
|
|
here += magnitudes[i];
|
|
|
|
|
}
|
|
|
|
|
for (int i = 1; i <= b; ++i) {
|
|
|
|
|
there += prevMagnitudes[i];
|
|
|
|
|
}
|
2022-05-24 15:39:28 +01:00
|
|
|
return (here > 10.e-3 && here > there * 1.4);
|
2022-05-19 16:31:21 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-06 10:40:10 +01:00
|
|
|
double descendToValley(double f, const process_t *const magnitudes) const {
|
2022-06-30 09:32:39 +01:00
|
|
|
if (f == 0.0 || f == m_parameters.sampleRate/2.0) {
|
|
|
|
|
// These are special cases
|
|
|
|
|
return f;
|
|
|
|
|
}
|
2022-06-13 10:08:05 +01:00
|
|
|
int b = binForFrequency(f, m_configuration.classificationFftSize,
|
|
|
|
|
m_parameters.sampleRate);
|
2022-06-30 09:32:39 +01:00
|
|
|
int n = m_configuration.classificationFftSize/2;
|
2022-09-26 16:02:13 +01:00
|
|
|
if (b > n) b = n;
|
2022-06-09 14:29:51 +01:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
2022-06-30 09:32:39 +01:00
|
|
|
if (b < n && magnitudes[b+1] < magnitudes[b]) {
|
2022-06-09 14:29:51 +01:00
|
|
|
++b;
|
2022-06-30 09:32:39 +01:00
|
|
|
} else if (b > 0 && magnitudes[b-1] < magnitudes[b]) {
|
2022-06-09 14:29:51 +01:00
|
|
|
--b;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-13 10:08:05 +01:00
|
|
|
double sf = frequencyForBin(b, m_configuration.classificationFftSize,
|
|
|
|
|
m_parameters.sampleRate);
|
2022-06-08 09:57:12 +01:00
|
|
|
return sf;
|
2022-05-19 16:31:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|