2022-05-19 09:16:13 +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.
|
|
|
|
|
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_BIN_SEGMENTER_H
|
|
|
|
|
#define RUBBERBAND_BIN_SEGMENTER_H
|
|
|
|
|
|
|
|
|
|
#include "BinClassifier.h"
|
2022-06-10 12:35:15 +01:00
|
|
|
#include "../common/HistogramFilter.h"
|
2022-05-19 13:34:51 +01:00
|
|
|
|
2022-05-19 09:16:13 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
namespace RubberBand {
|
|
|
|
|
|
|
|
|
|
class BinSegmenter
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
struct Segmentation {
|
|
|
|
|
double percussiveBelow;
|
|
|
|
|
double percussiveAbove;
|
|
|
|
|
double residualAbove;
|
2022-05-20 16:48:44 +01:00
|
|
|
explicit Segmentation() :
|
|
|
|
|
percussiveBelow(0.0), percussiveAbove(0.0), residualAbove(0.0) { }
|
2022-05-19 09:16:13 +01:00
|
|
|
Segmentation(double _pb, double _pa, double _ra) :
|
|
|
|
|
percussiveBelow(_pb), percussiveAbove(_pa), residualAbove(_ra) { }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Parameters {
|
|
|
|
|
int fftSize;
|
2022-06-07 11:05:50 +01:00
|
|
|
int binCount;
|
2022-05-19 09:16:13 +01:00
|
|
|
double sampleRate;
|
2022-06-07 11:05:50 +01:00
|
|
|
Parameters(int _fftSize, int _binCount, double _sampleRate) :
|
|
|
|
|
fftSize(_fftSize), binCount(_binCount), sampleRate(_sampleRate) { }
|
2022-05-19 09:16:13 +01:00
|
|
|
};
|
|
|
|
|
|
2022-05-27 14:58:42 +01:00
|
|
|
BinSegmenter(Parameters parameters) :
|
2022-05-19 09:16:13 +01:00
|
|
|
m_parameters(parameters),
|
2022-06-07 11:05:50 +01:00
|
|
|
m_numeric(m_parameters.binCount, 0),
|
2022-06-10 12:35:15 +01:00
|
|
|
m_classFilter(3, 15)
|
2022-05-19 09:16:13 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-27 14:58:42 +01:00
|
|
|
Segmentation segment(const BinClassifier::Classification *classification) {
|
2022-06-07 11:05:50 +01:00
|
|
|
int n = m_parameters.binCount;
|
2022-05-19 09:16:13 +01:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2022-05-27 14:58:42 +01:00
|
|
|
switch (classification[i]) {
|
2022-05-19 09:16:13 +01:00
|
|
|
case BinClassifier::Classification::Harmonic:
|
|
|
|
|
m_numeric[i] = 0; break;
|
|
|
|
|
case BinClassifier::Classification::Percussive:
|
|
|
|
|
m_numeric[i] = 1; break;
|
|
|
|
|
default:
|
|
|
|
|
m_numeric[i] = 2; break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-10 12:35:15 +01:00
|
|
|
HistogramFilter::modalFilter(m_classFilter, m_numeric);
|
2022-06-09 14:16:49 +01:00
|
|
|
/*
|
|
|
|
|
std::cout << "c:";
|
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
|
if (i > 0) std::cout << ",";
|
|
|
|
|
if (m_numeric[i] == 1) {
|
|
|
|
|
std::cout << "1";
|
|
|
|
|
} else {
|
|
|
|
|
std::cout << "0";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
*/
|
2022-05-19 09:16:13 +01:00
|
|
|
double f0 = 0.0;
|
|
|
|
|
for (int i = 1; i < n; ++i) {
|
|
|
|
|
if (m_numeric[i] != 1) {
|
|
|
|
|
f0 = frequencyForBin(i);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
double nyquist = m_parameters.sampleRate / 2.0;
|
|
|
|
|
double f1 = nyquist;
|
|
|
|
|
double f2 = nyquist;
|
|
|
|
|
bool inPercussive = false;
|
2022-06-07 11:05:50 +01:00
|
|
|
for (int i = n - 1; i > 0; --i) {
|
2022-06-09 14:16:40 +01:00
|
|
|
int c = m_numeric[i];
|
|
|
|
|
if (!inPercussive) {
|
|
|
|
|
if (c == 2) { // residual/silent
|
|
|
|
|
continue;
|
|
|
|
|
} else if (c == 1) { // percussive
|
2022-05-19 09:16:13 +01:00
|
|
|
inPercussive = true;
|
|
|
|
|
f2 = frequencyForBin(i);
|
2022-06-09 14:16:40 +01:00
|
|
|
} else { // harmonic
|
|
|
|
|
f1 = f2 = frequencyForBin(i);
|
|
|
|
|
break;
|
2022-05-19 09:16:13 +01:00
|
|
|
}
|
2022-06-09 14:16:40 +01:00
|
|
|
} else { // inPercussive
|
|
|
|
|
if (c != 1) { // non-percussive
|
2022-05-19 09:16:13 +01:00
|
|
|
f1 = frequencyForBin(i);
|
2022-06-09 14:16:40 +01:00
|
|
|
break;
|
2022-05-19 09:16:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-09 14:16:40 +01:00
|
|
|
if (f1 == nyquist && f2 < nyquist) {
|
|
|
|
|
f1 = 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// std::cout << "f0 = " << f0 << ", f1 = " << f1 << ", f2 = " << f2 << std::endl;
|
|
|
|
|
|
2022-05-19 09:16:13 +01:00
|
|
|
return Segmentation(f0, f1, f2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
Parameters m_parameters;
|
|
|
|
|
std::vector<int> m_numeric;
|
2022-06-10 12:35:15 +01:00
|
|
|
HistogramFilter m_classFilter;
|
2022-05-19 09:16:13 +01:00
|
|
|
|
2022-05-23 17:36:26 +01:00
|
|
|
//!!! dupes
|
2022-05-19 16:31:21 +01:00
|
|
|
int binForFrequency(double f) const {
|
2022-05-19 09:16:13 +01:00
|
|
|
return int(round(f * double(m_parameters.fftSize) /
|
|
|
|
|
m_parameters.sampleRate));
|
|
|
|
|
}
|
2022-05-19 16:31:21 +01:00
|
|
|
double frequencyForBin(int b) const {
|
2022-05-19 09:16:13 +01:00
|
|
|
return (double(b) * m_parameters.sampleRate)
|
|
|
|
|
/ double(m_parameters.fftSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BinSegmenter(const BinSegmenter &) =delete;
|
|
|
|
|
BinSegmenter &operator=(const BinSegmenter &) =delete;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|