1 #include <dsp/onsets/DetectionFunction.h>
2 #include <dsp/tempotracking/TempoTrackV2.h>
3 
4 // Class header comes after library includes here since our preprocessor
5 // definitions interfere with qm-dsp's headers.
6 #include "analyzer/plugins/analyzerqueenmarybeats.h"
7 
8 #include "analyzer/constants.h"
9 
10 namespace mixxx {
11 namespace {
12 
13 // This determines the resolution of the resulting BeatMap.
14 // ~12 ms (86 Hz) is a fair compromise between accuracy and analysis speed,
15 // also matching the preferred window/step sizes from BeatTrack VAMP.
16 // For a 44.1 kHz track, we go in 512 sample steps
17 // TODO: kStepSecs and the waveform sample rate of 441
18 // (defined in AnalyzerWaveform::initialize) do not align well and thus
19 // generate interference. Currently we are at this odd factor: 441 * 0.01161 = 5.12.
20 // This should be adjusted to be an integer.
21 constexpr float kStepSecs = 0.01161f;
22 // results in 43 Hz @ 44.1 kHz / 47 Hz @ 48 kHz / 47 Hz @ 96 kHz
23 constexpr int kMaximumBinSizeHz = 50; // Hz
24 
makeDetectionFunctionConfig(int stepSize,int windowSize)25 DFConfig makeDetectionFunctionConfig(int stepSize, int windowSize) {
26     // These are the defaults for the VAMP beat tracker plugin we used in Mixxx
27     // 2.0.
28     DFConfig config;
29     config.DFType = DF_COMPLEXSD;
30     config.stepSize = stepSize;
31     config.frameLength = windowSize;
32     config.dbRise = 3;
33     config.adaptiveWhitening = false;
34     config.whiteningRelaxCoeff = -1;
35     config.whiteningFloor = -1;
36     return config;
37 }
38 
39 } // namespace
40 
AnalyzerQueenMaryBeats()41 AnalyzerQueenMaryBeats::AnalyzerQueenMaryBeats()
42         : m_iSampleRate(0),
43           m_windowSize(0),
44           m_stepSize(0) {
45 }
46 
~AnalyzerQueenMaryBeats()47 AnalyzerQueenMaryBeats::~AnalyzerQueenMaryBeats() {
48 }
49 
initialize(int samplerate)50 bool AnalyzerQueenMaryBeats::initialize(int samplerate) {
51     m_detectionResults.clear();
52     m_iSampleRate = samplerate;
53     m_stepSize = static_cast<int>(m_iSampleRate * kStepSecs);
54     m_windowSize = MathUtilities::nextPowerOfTwo(m_iSampleRate / kMaximumBinSizeHz);
55     m_pDetectionFunction = std::make_unique<DetectionFunction>(
56             makeDetectionFunctionConfig(m_stepSize, m_windowSize));
57     qDebug() << "input sample rate is " << m_iSampleRate << ", step size is " << m_stepSize;
58 
59     m_helper.initialize(
60             m_windowSize, m_stepSize, [this](double* pWindow, size_t) {
61                 // TODO(rryan) reserve?
62                 m_detectionResults.push_back(
63                         m_pDetectionFunction->processTimeDomain(pWindow));
64                 return true;
65             });
66     return true;
67 }
68 
processSamples(const CSAMPLE * pIn,const int iLen)69 bool AnalyzerQueenMaryBeats::processSamples(const CSAMPLE* pIn, const int iLen) {
70     DEBUG_ASSERT(iLen % kAnalysisChannels == 0);
71     if (!m_pDetectionFunction) {
72         return false;
73     }
74 
75     return m_helper.processStereoSamples(pIn, iLen);
76 }
77 
finalize()78 bool AnalyzerQueenMaryBeats::finalize() {
79     m_helper.finalize();
80 
81     int nonZeroCount = static_cast<int>(m_detectionResults.size());
82     while (nonZeroCount > 0 && m_detectionResults.at(nonZeroCount - 1) <= 0.0) {
83         --nonZeroCount;
84     }
85 
86     std::vector<double> df;
87     std::vector<double> beatPeriod;
88     std::vector<double> tempi;
89     const auto required_size = std::max(0, nonZeroCount - 2);
90     df.reserve(required_size);
91     beatPeriod.reserve(required_size);
92 
93     // skip first 2 results as it might have detect noise as onset
94     // that's how vamp does and seems works best this way
95     for (int i = 2; i < nonZeroCount; ++i) {
96         df.push_back(m_detectionResults.at(i));
97         beatPeriod.push_back(0.0);
98     }
99 
100     TempoTrackV2 tt(m_iSampleRate, m_stepSize);
101     tt.calculateBeatPeriod(df, beatPeriod, tempi);
102 
103     std::vector<double> beats;
104     tt.calculateBeats(df, beatPeriod, beats);
105 
106     m_resultBeats.reserve(static_cast<int>(beats.size()));
107     for (size_t i = 0; i < beats.size(); ++i) {
108         // we add the halve m_stepSize here, because the beat
109         // is detected between the two samples.
110         double result = (beats.at(i) * m_stepSize) + m_stepSize / 2;
111         m_resultBeats.push_back(result);
112     }
113 
114     m_pDetectionFunction.reset();
115     return true;
116 }
117 
118 } // namespace mixxx
119