-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeatRootProcessor.h
168 lines (136 loc) · 5.52 KB
/
BeatRootProcessor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Vamp feature extraction plugin for the BeatRoot beat tracker.
Centre for Digital Music, Queen Mary, University of London.
This file copyright 2011 Simon Dixon, Chris Cannam and QMUL.
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.
*/
#ifndef _BEATROOT_PROCESSOR_H_
#define _BEATROOT_PROCESSOR_H_
#include "Peaks.h"
#include "Event.h"
#include "BeatTracker.h"
#include <vector>
#include <cmath>
#ifdef DEBUG_BEATROOT
#include <iostream>
#endif
using std::vector;
class BeatRootProcessor
{
public:
int getFFTSize() const { return fftSize; }
int getHopSize() const { return hopSize; }
protected:
/** Sample rate of audio */
float sampleRate;
/** Spacing of audio frames (determines the amount of overlap or
* skip between frames). This value is expressed in
* seconds. (Default = 0.020s) */
double hopTime;
/** The approximate size of an FFT frame in seconds. (Default =
* 0.04644s). The value is adjusted so that <code>fftSize</code>
* is always power of 2. */
double fftTime;
/** Spacing of audio frames in samples (see <code>hopTime</code>) */
int hopSize;
/** The size of an FFT frame in samples (see <code>fftTime</code>) */
int fftSize;
/** Spectral flux onset detection function, indexed by frame. */
vector<double> spectralFlux;
/** A mapping function for mapping FFT bins to final frequency bins.
* The mapping is linear (1-1) until the resolution reaches 2 points per
* semitone, then logarithmic with a semitone resolution. e.g. for
* 44.1kHz sampling rate and fftSize of 2048 (46ms), bin spacing is
* 21.5Hz, which is mapped linearly for bins 0-34 (0 to 732Hz), and
* logarithmically for the remaining bins (midi notes 79 to 127, bins 35 to
* 83), where all energy above note 127 is mapped into the final bin. */
vector<int> freqMap;
/** The number of entries in <code>freqMap</code>. Note that the length of
* the array is greater, because its size is not known at creation time. */
int freqMapSize;
/** The magnitude spectrum of the most recent frame. Used for
* calculating the spectral flux. */
vector<double> prevFrame;
/** The estimated onset times from peak-picking the onset
* detection function(s). */
vector<double> onsets;
/** The estimated onset times and their saliences. */
EventList onsetList;
/** User-specifiable processing parameters. */
AgentParameters agentParameters;
/** Flag for suppressing all standard output messages except results. */
static bool silent;
public:
/** Constructor: note that streams are not opened until the input
* file is set (see <code>setInputFile()</code>). */
BeatRootProcessor(float sr, AgentParameters parameters) :
sampleRate(sr),
hopTime(0.010),
fftTime(0.04644),
hopSize(0),
fftSize(0),
agentParameters(parameters)
{
hopSize = lrint(sampleRate * hopTime);
fftSize = lrint(pow(2, lrint( log(fftTime * sampleRate) / log(2))));
init();
} // constructor
void reset() {
init();
}
/** Processes a frame of frequency-domain audio data by mapping
* the frequency bins into a part-linear part-logarithmic array,
* then computing the spectral flux then (optionally) normalising
* and calculating onsets.
*/
void processFrame(const float *const *inputBuffers);
/** Tracks beats once all frames have been processed by processFrame
*/
EventList beatTrack(EventList *optionalUnfilledBeatReturn);
protected:
/** Allocates or re-allocates memory for arrays, based on parameter settings */
void init() {
#ifdef DEBUG_BEATROOT
std::cerr << "BeatRootProcessor::init()" << std::endl;
#endif
makeFreqMap(fftSize, sampleRate);
prevFrame.clear();
for (int i = 0; i <= fftSize/2; i++) prevFrame.push_back(0);
spectralFlux.clear();
onsets.clear();
onsetList.clear();
} // init()
/** Creates a map of FFT frequency bins to comparison bins.
* Where the spacing of FFT bins is less than 0.5 semitones, the mapping is
* one to one. Where the spacing is greater than 0.5 semitones, the FFT
* energy is mapped into semitone-wide bins. No scaling is performed; that
* is the energy is summed into the comparison bins. See also
* processFrame()
*/
void makeFreqMap(int fftSize, float sampleRate) {
freqMap.resize(fftSize/2+1);
double binWidth = sampleRate / fftSize;
int crossoverBin = (int)(2 / (pow(2, 1/12.0) - 1));
int crossoverMidi = (int)lrint(log(crossoverBin*binWidth/440)/
log(2) * 12 + 69);
int i = 0;
while (i <= crossoverBin && i <= fftSize/2) {
freqMap[i] = i;
++i;
}
while (i <= fftSize/2) {
double midi = log(i*binWidth/440) / log(2) * 12 + 69;
if (midi > 127)
midi = 127;
freqMap[i] = crossoverBin + (int)lrint(midi) - crossoverMidi;
++i;
}
freqMapSize = freqMap[i-1] + 1;
} // makeFreqMap()
}; // class AudioProcessor
#endif