Chris@1
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@1
|
2
|
Chris@1
|
3 /*
|
Chris@3
|
4 Vamp feature extraction plugin for the BeatRoot beat tracker.
|
Chris@1
|
5
|
Chris@3
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@3
|
7 This file copyright 2011 Simon Dixon, Chris Cannam and QMUL.
|
Chris@1
|
8
|
Chris@3
|
9 This program is free software; you can redistribute it and/or
|
Chris@3
|
10 modify it under the terms of the GNU General Public License as
|
Chris@3
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@3
|
12 License, or (at your option) any later version. See the file
|
Chris@3
|
13 COPYING included with this distribution for more information.
|
Chris@1
|
14 */
|
Chris@1
|
15
|
Chris@1
|
16 #ifndef _BEATROOT_PROCESSOR_H_
|
Chris@1
|
17 #define _BEATROOT_PROCESSOR_H_
|
Chris@1
|
18
|
Chris@4
|
19 #include "Peaks.h"
|
Chris@6
|
20 #include "Event.h"
|
Chris@6
|
21 #include "BeatTracker.h"
|
Chris@4
|
22
|
Chris@2
|
23 #include <vector>
|
Chris@3
|
24 #include <cmath>
|
Chris@2
|
25
|
Chris@2
|
26 using std::vector;
|
Chris@2
|
27
|
Chris@1
|
28 class BeatRootProcessor
|
Chris@1
|
29 {
|
Chris@9
|
30 public:
|
Chris@9
|
31 int getFFTSize() const { return fftSize; }
|
Chris@9
|
32 int getHopSize() const { return hopSize; }
|
Chris@9
|
33
|
Chris@1
|
34 protected:
|
Chris@1
|
35 /** Sample rate of audio */
|
Chris@1
|
36 float sampleRate;
|
Chris@1
|
37
|
Chris@1
|
38 /** Spacing of audio frames (determines the amount of overlap or
|
Chris@1
|
39 * skip between frames). This value is expressed in
|
Chris@1
|
40 * seconds. (Default = 0.020s) */
|
Chris@1
|
41 double hopTime;
|
Chris@1
|
42
|
Chris@1
|
43 /** The approximate size of an FFT frame in seconds. (Default =
|
Chris@1
|
44 * 0.04644s). The value is adjusted so that <code>fftSize</code>
|
Chris@1
|
45 * is always power of 2. */
|
Chris@1
|
46 double fftTime;
|
Chris@1
|
47
|
Chris@1
|
48 /** Spacing of audio frames in samples (see <code>hopTime</code>) */
|
Chris@1
|
49 int hopSize;
|
Chris@1
|
50
|
Chris@1
|
51 /** The size of an FFT frame in samples (see <code>fftTime</code>) */
|
Chris@1
|
52 int fftSize;
|
Chris@1
|
53
|
Chris@1
|
54 /** The number of overlapping frames of audio data which have been read. */
|
Chris@1
|
55 int frameCount;
|
Chris@1
|
56
|
Chris@1
|
57 /** RMS amplitude of the current frame. */
|
Chris@1
|
58 double frameRMS;
|
Chris@1
|
59
|
Chris@1
|
60 /** Long term average frame energy (in frequency domain representation). */
|
Chris@1
|
61 double ltAverage;
|
Chris@1
|
62
|
Chris@1
|
63 /** Spectral flux onset detection function, indexed by frame. */
|
Chris@4
|
64 vector<double> spectralFlux;
|
Chris@1
|
65
|
Chris@1
|
66 /** A mapping function for mapping FFT bins to final frequency bins.
|
Chris@1
|
67 * The mapping is linear (1-1) until the resolution reaches 2 points per
|
Chris@1
|
68 * semitone, then logarithmic with a semitone resolution. e.g. for
|
Chris@1
|
69 * 44.1kHz sampling rate and fftSize of 2048 (46ms), bin spacing is
|
Chris@1
|
70 * 21.5Hz, which is mapped linearly for bins 0-34 (0 to 732Hz), and
|
Chris@1
|
71 * logarithmically for the remaining bins (midi notes 79 to 127, bins 35 to
|
Chris@1
|
72 * 83), where all energy above note 127 is mapped into the final bin. */
|
Chris@1
|
73 vector<int> freqMap;
|
Chris@1
|
74
|
Chris@1
|
75 /** The number of entries in <code>freqMap</code>. Note that the length of
|
Chris@1
|
76 * the array is greater, because its size is not known at creation time. */
|
Chris@1
|
77 int freqMapSize;
|
Chris@1
|
78
|
Chris@1
|
79 /** The magnitude spectrum of the most recent frame. Used for
|
Chris@1
|
80 * calculating the spectral flux. */
|
Chris@1
|
81 vector<double> prevFrame;
|
Chris@1
|
82
|
Chris@1
|
83 /** The magnitude spectrum of the current frame. */
|
Chris@1
|
84 vector<double> newFrame;
|
Chris@1
|
85
|
Chris@1
|
86 /** The magnitude spectra of all frames, used for plotting the spectrogram. */
|
Chris@1
|
87 vector<vector<double> > frames; //!!! do we need this? much cheaper to lose it if we don't
|
Chris@1
|
88
|
Chris@1
|
89 /** The RMS energy of all frames. */
|
Chris@3
|
90 // vector<double> energy; //!!! unused in beat tracking?
|
Chris@1
|
91
|
Chris@1
|
92 /** The estimated onset times from peak-picking the onset
|
Chris@1
|
93 * detection function(s). */
|
Chris@1
|
94 vector<double> onsets;
|
Chris@1
|
95
|
Chris@1
|
96 /** The estimated onset times and their saliences. */
|
Chris@6
|
97 EventList onsetList;
|
Chris@1
|
98
|
Chris@1
|
99 /** Flag for suppressing all standard output messages except results. */
|
Chris@2
|
100 static bool silent;
|
Chris@1
|
101
|
Chris@1
|
102 /** RMS frame energy below this value results in the frame being
|
Chris@1
|
103 * set to zero, so that normalisation does not have undesired
|
Chris@1
|
104 * side-effects. */
|
Chris@2
|
105 static double silenceThreshold; //!!!??? energy of what? should not be static?
|
Chris@1
|
106
|
Chris@1
|
107 /** For dynamic range compression, this value is added to the log
|
Chris@1
|
108 * magnitude in each frequency bin and any remaining negative
|
Chris@1
|
109 * values are then set to zero.
|
Chris@1
|
110 */
|
Chris@2
|
111 static double rangeThreshold; //!!! sim
|
Chris@1
|
112
|
Chris@1
|
113 /** Determines method of normalisation. Values can be:<ul>
|
Chris@1
|
114 * <li>0: no normalisation</li>
|
Chris@1
|
115 * <li>1: normalisation by current frame energy</li>
|
Chris@1
|
116 * <li>2: normalisation by exponential average of frame energy</li>
|
Chris@1
|
117 * </ul>
|
Chris@1
|
118 */
|
Chris@2
|
119 static int normaliseMode;
|
Chris@1
|
120
|
Chris@1
|
121 /** Ratio between rate of sampling the signal energy (for the
|
Chris@1
|
122 * amplitude envelope) and the hop size */
|
Chris@3
|
123 // static int energyOversampleFactor; //!!! not used?
|
Chris@1
|
124
|
Chris@1
|
125 public:
|
Chris@1
|
126
|
Chris@1
|
127 /** Constructor: note that streams are not opened until the input
|
Chris@1
|
128 * file is set (see <code>setInputFile()</code>). */
|
Chris@8
|
129 BeatRootProcessor(float sr) :
|
Chris@8
|
130 sampleRate(sr) {
|
Chris@1
|
131 frameRMS = 0;
|
Chris@1
|
132 ltAverage = 0;
|
Chris@1
|
133 frameCount = 0;
|
Chris@1
|
134 hopSize = 0;
|
Chris@1
|
135 fftSize = 0;
|
Chris@9
|
136 hopTime = 0.010;
|
Chris@9
|
137 fftTime = 0.04644;
|
Chris@9
|
138 hopSize = lrint(sampleRate * hopTime);
|
Chris@9
|
139 fftSize = lrint(pow(2, lrint( log(fftTime * sampleRate) / log(2))));
|
Chris@1
|
140 } // constructor
|
Chris@1
|
141
|
Chris@9
|
142 void reset() {
|
Chris@9
|
143 init();
|
Chris@9
|
144 }
|
Chris@9
|
145
|
Chris@2
|
146 protected:
|
Chris@3
|
147 /** Allocates memory for arrays, based on parameter settings */
|
Chris@3
|
148 void init() {
|
Chris@3
|
149 makeFreqMap(fftSize, sampleRate);
|
Chris@3
|
150 prevFrame.clear();
|
Chris@3
|
151 for (int i = 0; i < freqMapSize; i++) prevFrame.push_back(0);
|
Chris@3
|
152 frameCount = 0;
|
Chris@3
|
153 frameRMS = 0;
|
Chris@3
|
154 ltAverage = 0;
|
Chris@3
|
155 spectralFlux.clear();
|
Chris@3
|
156 } // init()
|
Chris@1
|
157
|
Chris@3
|
158 /** Creates a map of FFT frequency bins to comparison bins.
|
Chris@3
|
159 * Where the spacing of FFT bins is less than 0.5 semitones, the mapping is
|
Chris@3
|
160 * one to one. Where the spacing is greater than 0.5 semitones, the FFT
|
Chris@3
|
161 * energy is mapped into semitone-wide bins. No scaling is performed; that
|
Chris@3
|
162 * is the energy is summed into the comparison bins. See also
|
Chris@3
|
163 * processFrame()
|
Chris@3
|
164 */
|
Chris@3
|
165 void makeFreqMap(int fftSize, float sampleRate) {
|
Chris@3
|
166 freqMap.resize(fftSize/2+1);
|
Chris@3
|
167 double binWidth = sampleRate / fftSize;
|
Chris@3
|
168 int crossoverBin = (int)(2 / (pow(2, 1/12.0) - 1));
|
Chris@3
|
169 int crossoverMidi = (int)lrint(log(crossoverBin*binWidth/440)/
|
Chris@3
|
170 log(2) * 12 + 69);
|
Chris@3
|
171 int i = 0;
|
Chris@9
|
172 while (i <= crossoverBin && i <= fftSize/2)
|
Chris@3
|
173 freqMap[i++] = i;
|
Chris@3
|
174 while (i <= fftSize/2) {
|
Chris@3
|
175 double midi = log(i*binWidth/440) / log(2) * 12 + 69;
|
Chris@3
|
176 if (midi > 127)
|
Chris@3
|
177 midi = 127;
|
Chris@3
|
178 freqMap[i++] = crossoverBin + (int)lrint(midi) - crossoverMidi;
|
Chris@3
|
179 }
|
Chris@3
|
180 freqMapSize = freqMap[i-1] + 1;
|
Chris@3
|
181 } // makeFreqMap()
|
Chris@1
|
182
|
Chris@9
|
183 /** Processes a frame of frequency-domain audio data by mapping
|
Chris@9
|
184 * the frequency bins into a part-linear part-logarithmic array,
|
Chris@9
|
185 * then computing the spectral flux then (optionally) normalising
|
Chris@9
|
186 * and calculating onsets.
|
Chris@3
|
187 */
|
Chris@3
|
188 void processFrame(const float *const *inputBuffers) {
|
Chris@3
|
189 newFrame.clear();
|
Chris@3
|
190 for (int i = 0; i < freqMapSize; i++) {
|
Chris@3
|
191 newFrame.push_back(0);
|
Chris@3
|
192 }
|
Chris@3
|
193 double flux = 0;
|
Chris@3
|
194 for (int i = 0; i <= fftSize/2; i++) {
|
Chris@3
|
195 double mag = sqrt(inputBuffers[0][i*2] * inputBuffers[0][i*2] +
|
Chris@3
|
196 inputBuffers[0][i*2+1] * inputBuffers[0][i*2+1]);
|
Chris@3
|
197 if (mag > prevFrame[i]) flux += mag - prevFrame[i];
|
Chris@3
|
198 prevFrame[i] = mag;
|
Chris@3
|
199 newFrame[freqMap[i]] += mag;
|
Chris@3
|
200 }
|
Chris@3
|
201 spectralFlux.push_back(flux);
|
Chris@3
|
202 frames.push_back(newFrame);
|
Chris@3
|
203 // for (int i = 0; i < freqMapSize; i++)
|
Chris@3
|
204 // [frameCount][i] = newFrame[i];
|
Chris@3
|
205 /*
|
Chris@3
|
206 int index = cbIndex - (fftSize - hopSize);
|
Chris@3
|
207 if (index < 0)
|
Chris@3
|
208 index += fftSize;
|
Chris@3
|
209 int sz = (fftSize - hopSize) / energyOversampleFactor;
|
Chris@3
|
210 for (int j = 0; j < energyOversampleFactor; j++) {
|
Chris@3
|
211 double newEnergy = 0;
|
Chris@3
|
212 for (int i = 0; i < sz; i++) {
|
Chris@3
|
213 newEnergy += circBuffer[index] * circBuffer[index];
|
Chris@3
|
214 if (++index == fftSize)
|
Chris@3
|
215 index = 0;
|
Chris@3
|
216 }
|
Chris@3
|
217 energy[frameCount * energyOversampleFactor + j] =
|
Chris@3
|
218 newEnergy / sz <= 1e-6? 0: log(newEnergy / sz) + 13.816;
|
Chris@3
|
219 }*/
|
Chris@1
|
220
|
Chris@3
|
221 double decay = frameCount >= 200? 0.99:
|
Chris@3
|
222 (frameCount < 100? 0: (frameCount - 100) / 100.0);
|
Chris@1
|
223
|
Chris@3
|
224 //!!! uh-oh -- frameRMS has not been calculated (it came from time-domain signal) -- will always appear silent
|
Chris@1
|
225
|
Chris@3
|
226 if (ltAverage == 0)
|
Chris@3
|
227 ltAverage = frameRMS;
|
Chris@3
|
228 else
|
Chris@3
|
229 ltAverage = ltAverage * decay + frameRMS * (1.0 - decay);
|
Chris@3
|
230 if (frameRMS <= silenceThreshold)
|
Chris@3
|
231 for (int i = 0; i < freqMapSize; i++)
|
Chris@3
|
232 frames[frameCount][i] = 0;
|
Chris@3
|
233 else {
|
Chris@3
|
234 if (normaliseMode == 1)
|
Chris@3
|
235 for (int i = 0; i < freqMapSize; i++)
|
Chris@3
|
236 frames[frameCount][i] /= frameRMS;
|
Chris@3
|
237 else if (normaliseMode == 2)
|
Chris@3
|
238 for (int i = 0; i < freqMapSize; i++)
|
Chris@3
|
239 frames[frameCount][i] /= ltAverage;
|
Chris@3
|
240 for (int i = 0; i < freqMapSize; i++) {
|
Chris@3
|
241 frames[frameCount][i] = log(frames[frameCount][i]) + rangeThreshold;
|
Chris@3
|
242 if (frames[frameCount][i] < 0)
|
Chris@3
|
243 frames[frameCount][i] = 0;
|
Chris@3
|
244 }
|
Chris@3
|
245 }
|
Chris@1
|
246 // weightedPhaseDeviation();
|
Chris@1
|
247 // if (debug)
|
Chris@1
|
248 // System.err.printf("PhaseDev: t=%7.3f phDev=%7.3f RMS=%7.3f\n",
|
Chris@1
|
249 // frameCount * hopTime,
|
Chris@1
|
250 // phaseDeviation[frameCount],
|
Chris@1
|
251 // frameRMS);
|
Chris@3
|
252 frameCount++;
|
Chris@3
|
253 } // processFrame()
|
Chris@1
|
254
|
Chris@3
|
255 /** Processes a complete file of audio data. */
|
Chris@3
|
256 void processFile() {
|
Chris@3
|
257 /*
|
Chris@3
|
258 while (pcmInputStream != null) {
|
Chris@3
|
259 // Profile.start(0);
|
Chris@3
|
260 processFrame();
|
Chris@3
|
261 // Profile.log(0);
|
Chris@3
|
262 if (Thread.currentThread().isInterrupted()) {
|
Chris@3
|
263 System.err.println("info: INTERRUPTED in processFile()");
|
Chris@3
|
264 return;
|
Chris@3
|
265 }
|
Chris@3
|
266 }
|
Chris@3
|
267 */
|
Chris@1
|
268 // double[] x1 = new double[phaseDeviation.length];
|
Chris@1
|
269 // for (int i = 0; i < x1.length; i++) {
|
Chris@1
|
270 // x1[i] = i * hopTime;
|
Chris@1
|
271 // phaseDeviation[i] = (phaseDeviation[i] - 0.4) * 100;
|
Chris@1
|
272 // }
|
Chris@1
|
273 // double[] x2 = new double[energy.length];
|
Chris@1
|
274 // for (int i = 0; i < x2.length; i++)
|
Chris@1
|
275 // x2[i] = i * hopTime / energyOversampleFactor;
|
Chris@1
|
276 // // plot.clear();
|
Chris@1
|
277 // plot.addPlot(x1, phaseDeviation, Color.green, 7);
|
Chris@1
|
278 // plot.addPlot(x2, energy, Color.red, 7);
|
Chris@1
|
279 // plot.setTitle("Test phase deviation");
|
Chris@1
|
280 // plot.fitAxes();
|
Chris@1
|
281
|
Chris@1
|
282 // double[] slope = new double[energy.length];
|
Chris@1
|
283 // double hop = hopTime / energyOversampleFactor;
|
Chris@1
|
284 // Peaks.getSlope(energy, hop, 15, slope);
|
Chris@4
|
285 // vector<Integer> peaks = Peaks.findPeaks(slope, (int)lrint(0.06 / hop), 10);
|
Chris@1
|
286
|
Chris@3
|
287 double hop = hopTime;
|
Chris@4
|
288 Peaks::normalise(spectralFlux);
|
Chris@4
|
289 vector<int> peaks = Peaks::findPeaks(spectralFlux, (int)lrint(0.06 / hop), 0.35, 0.84, true);
|
Chris@5
|
290 onsets.clear();
|
Chris@5
|
291 onsets.resize(peaks.size(), 0);
|
Chris@4
|
292 vector<int>::iterator it = peaks.begin();
|
Chris@6
|
293 onsetList.clear();
|
Chris@6
|
294 double minSalience = Peaks::min(spectralFlux);
|
Chris@6
|
295 for (int i = 0; i < onsets.size(); i++) {
|
Chris@4
|
296 int index = *it;
|
Chris@4
|
297 ++it;
|
Chris@3
|
298 onsets[i] = index * hop;
|
Chris@6
|
299 Event e = BeatTracker::newBeat(onsets[i], 0);
|
Chris@1
|
300 // if (debug)
|
Chris@1
|
301 // System.err.printf("Onset: %8.3f %8.3f %8.3f\n",
|
Chris@1
|
302 // onsets[i], energy[index], slope[index]);
|
Chris@1
|
303 // e.salience = slope[index]; // or combination of energy + slope??
|
Chris@3
|
304 // Note that salience must be non-negative or the beat tracking system fails!
|
Chris@3
|
305 e.salience = spectralFlux[index] - minSalience;
|
Chris@6
|
306 onsetList.push_back(e);
|
Chris@3
|
307 }
|
Chris@1
|
308
|
Chris@3
|
309 //!!! This onsetList is then fed in to BeatTrackDisplay::beatTrack
|
Chris@1
|
310
|
Chris@3
|
311 } // processFile()
|
Chris@3
|
312
|
Chris@3
|
313 }; // class AudioProcessor
|
Chris@1
|
314
|
Chris@1
|
315
|
Chris@1
|
316 #endif
|