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 /** Spectral flux onset detection function, indexed by frame. */
|
Chris@4
|
55 vector<double> spectralFlux;
|
Chris@1
|
56
|
Chris@1
|
57 /** A mapping function for mapping FFT bins to final frequency bins.
|
Chris@1
|
58 * The mapping is linear (1-1) until the resolution reaches 2 points per
|
Chris@1
|
59 * semitone, then logarithmic with a semitone resolution. e.g. for
|
Chris@1
|
60 * 44.1kHz sampling rate and fftSize of 2048 (46ms), bin spacing is
|
Chris@1
|
61 * 21.5Hz, which is mapped linearly for bins 0-34 (0 to 732Hz), and
|
Chris@1
|
62 * logarithmically for the remaining bins (midi notes 79 to 127, bins 35 to
|
Chris@1
|
63 * 83), where all energy above note 127 is mapped into the final bin. */
|
Chris@1
|
64 vector<int> freqMap;
|
Chris@1
|
65
|
Chris@1
|
66 /** The number of entries in <code>freqMap</code>. Note that the length of
|
Chris@1
|
67 * the array is greater, because its size is not known at creation time. */
|
Chris@1
|
68 int freqMapSize;
|
Chris@1
|
69
|
Chris@1
|
70 /** The magnitude spectrum of the most recent frame. Used for
|
Chris@1
|
71 * calculating the spectral flux. */
|
Chris@1
|
72 vector<double> prevFrame;
|
Chris@1
|
73
|
Chris@1
|
74 /** The estimated onset times from peak-picking the onset
|
Chris@1
|
75 * detection function(s). */
|
Chris@1
|
76 vector<double> onsets;
|
Chris@1
|
77
|
Chris@1
|
78 /** The estimated onset times and their saliences. */
|
Chris@6
|
79 EventList onsetList;
|
Chris@1
|
80
|
Chris@1
|
81 /** Flag for suppressing all standard output messages except results. */
|
Chris@2
|
82 static bool silent;
|
Chris@1
|
83
|
Chris@1
|
84 public:
|
Chris@1
|
85
|
Chris@1
|
86 /** Constructor: note that streams are not opened until the input
|
Chris@1
|
87 * file is set (see <code>setInputFile()</code>). */
|
Chris@8
|
88 BeatRootProcessor(float sr) :
|
Chris@8
|
89 sampleRate(sr) {
|
Chris@1
|
90 hopSize = 0;
|
Chris@1
|
91 fftSize = 0;
|
Chris@9
|
92 hopTime = 0.010;
|
Chris@9
|
93 fftTime = 0.04644;
|
Chris@9
|
94 hopSize = lrint(sampleRate * hopTime);
|
Chris@9
|
95 fftSize = lrint(pow(2, lrint( log(fftTime * sampleRate) / log(2))));
|
Chris@1
|
96 } // constructor
|
Chris@1
|
97
|
Chris@9
|
98 void reset() {
|
Chris@9
|
99 init();
|
Chris@9
|
100 }
|
Chris@9
|
101
|
Chris@10
|
102 /** Processes a frame of frequency-domain audio data by mapping
|
Chris@10
|
103 * the frequency bins into a part-linear part-logarithmic array,
|
Chris@10
|
104 * then computing the spectral flux then (optionally) normalising
|
Chris@10
|
105 * and calculating onsets.
|
Chris@10
|
106 */
|
Chris@10
|
107 void processFrame(const float *const *inputBuffers) {
|
Chris@10
|
108 double flux = 0;
|
Chris@10
|
109 for (int i = 0; i <= fftSize/2; i++) {
|
Chris@10
|
110 double mag = sqrt(inputBuffers[0][i*2] * inputBuffers[0][i*2] +
|
Chris@10
|
111 inputBuffers[0][i*2+1] * inputBuffers[0][i*2+1]);
|
Chris@10
|
112 if (mag > prevFrame[i]) flux += mag - prevFrame[i];
|
Chris@10
|
113 prevFrame[i] = mag;
|
Chris@10
|
114 }
|
Chris@10
|
115
|
Chris@10
|
116 spectralFlux.push_back(flux);
|
Chris@10
|
117
|
Chris@10
|
118 } // processFrame()
|
Chris@10
|
119
|
Chris@10
|
120 /** Tracks beats once all frames have been processed by processFrame
|
Chris@10
|
121 */
|
Chris@10
|
122 EventList beatTrack() {
|
Chris@10
|
123
|
Chris@10
|
124 double hop = hopTime;
|
Chris@10
|
125 Peaks::normalise(spectralFlux);
|
Chris@10
|
126 vector<int> peaks = Peaks::findPeaks(spectralFlux, (int)lrint(0.06 / hop), 0.35, 0.84, true);
|
Chris@10
|
127 onsets.clear();
|
Chris@10
|
128 onsets.resize(peaks.size(), 0);
|
Chris@10
|
129 vector<int>::iterator it = peaks.begin();
|
Chris@10
|
130 onsetList.clear();
|
Chris@10
|
131 double minSalience = Peaks::min(spectralFlux);
|
Chris@10
|
132 for (int i = 0; i < onsets.size(); i++) {
|
Chris@10
|
133 int index = *it;
|
Chris@10
|
134 ++it;
|
Chris@10
|
135 onsets[i] = index * hop;
|
Chris@10
|
136 Event e = BeatTracker::newBeat(onsets[i], 0);
|
Chris@10
|
137 // if (debug)
|
Chris@10
|
138 // System.err.printf("Onset: %8.3f %8.3f %8.3f\n",
|
Chris@10
|
139 // onsets[i], energy[index], slope[index]);
|
Chris@10
|
140 // e.salience = slope[index]; // or combination of energy + slope??
|
Chris@10
|
141 // Note that salience must be non-negative or the beat tracking system fails!
|
Chris@10
|
142 e.salience = spectralFlux[index] - minSalience;
|
Chris@10
|
143 onsetList.push_back(e);
|
Chris@10
|
144 }
|
Chris@10
|
145
|
Chris@10
|
146 return BeatTracker::beatTrack(onsetList);
|
Chris@10
|
147
|
Chris@10
|
148 } // processFile()
|
Chris@10
|
149
|
Chris@2
|
150 protected:
|
Chris@3
|
151 /** Allocates memory for arrays, based on parameter settings */
|
Chris@3
|
152 void init() {
|
Chris@3
|
153 makeFreqMap(fftSize, sampleRate);
|
Chris@3
|
154 prevFrame.clear();
|
Chris@10
|
155 for (int i = 0; i <= fftSize/2; i++) prevFrame.push_back(0);
|
Chris@3
|
156 spectralFlux.clear();
|
Chris@3
|
157 } // init()
|
Chris@1
|
158
|
Chris@3
|
159 /** Creates a map of FFT frequency bins to comparison bins.
|
Chris@3
|
160 * Where the spacing of FFT bins is less than 0.5 semitones, the mapping is
|
Chris@3
|
161 * one to one. Where the spacing is greater than 0.5 semitones, the FFT
|
Chris@3
|
162 * energy is mapped into semitone-wide bins. No scaling is performed; that
|
Chris@3
|
163 * is the energy is summed into the comparison bins. See also
|
Chris@3
|
164 * processFrame()
|
Chris@3
|
165 */
|
Chris@3
|
166 void makeFreqMap(int fftSize, float sampleRate) {
|
Chris@3
|
167 freqMap.resize(fftSize/2+1);
|
Chris@3
|
168 double binWidth = sampleRate / fftSize;
|
Chris@3
|
169 int crossoverBin = (int)(2 / (pow(2, 1/12.0) - 1));
|
Chris@3
|
170 int crossoverMidi = (int)lrint(log(crossoverBin*binWidth/440)/
|
Chris@3
|
171 log(2) * 12 + 69);
|
Chris@3
|
172 int i = 0;
|
Chris@9
|
173 while (i <= crossoverBin && i <= fftSize/2)
|
Chris@3
|
174 freqMap[i++] = i;
|
Chris@3
|
175 while (i <= fftSize/2) {
|
Chris@3
|
176 double midi = log(i*binWidth/440) / log(2) * 12 + 69;
|
Chris@3
|
177 if (midi > 127)
|
Chris@3
|
178 midi = 127;
|
Chris@3
|
179 freqMap[i++] = crossoverBin + (int)lrint(midi) - crossoverMidi;
|
Chris@3
|
180 }
|
Chris@3
|
181 freqMapSize = freqMap[i-1] + 1;
|
Chris@3
|
182 } // makeFreqMap()
|
Chris@1
|
183
|
Chris@3
|
184 }; // class AudioProcessor
|
Chris@1
|
185
|
Chris@1
|
186
|
Chris@1
|
187 #endif
|