c@279
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@279
|
2
|
c@279
|
3 /*
|
c@279
|
4 QM DSP Library
|
c@279
|
5
|
c@279
|
6 Centre for Digital Music, Queen Mary, University of London.
|
c@279
|
7 This file copyright 2008-2009 Matthew Davies and QMUL.
|
c@279
|
8 All rights reserved.
|
c@279
|
9 */
|
c@279
|
10
|
c@279
|
11 #include "DownBeat.h"
|
c@279
|
12
|
c@279
|
13 #include "maths/MathAliases.h"
|
c@279
|
14 #include "maths/MathUtilities.h"
|
c@279
|
15 #include "dsp/transforms/FFT.h"
|
c@279
|
16
|
c@279
|
17 #include <iostream>
|
c@279
|
18 #include <cstdlib>
|
c@279
|
19
|
c@279
|
20 DownBeat::DownBeat(float originalSampleRate,
|
c@279
|
21 size_t decimationFactor,
|
c@279
|
22 size_t dfIncrement) :
|
c@279
|
23 m_rate(originalSampleRate),
|
c@279
|
24 m_factor(decimationFactor),
|
c@279
|
25 m_increment(dfIncrement),
|
c@279
|
26 m_decimator1(0),
|
c@279
|
27 m_decimator2(0),
|
c@279
|
28 m_buffer(0),
|
c@279
|
29 m_bufsiz(0),
|
c@279
|
30 m_buffill(0),
|
c@279
|
31 m_beatframesize(0),
|
c@279
|
32 m_beatframe(0)
|
c@279
|
33 {
|
c@279
|
34 // beat frame size is next power of two up from 1.3 seconds at the
|
c@279
|
35 // downsampled rate (happens to produce 4096 for 44100 or 48000 at
|
c@279
|
36 // 16x decimation, which is our expected normal situation)
|
c@279
|
37 int bfs = int((m_rate / decimationFactor) * 1.3);
|
c@279
|
38 m_beatframesize = 1;
|
c@279
|
39 while (bfs) { bfs >>= 1; m_beatframesize <<= 1; }
|
c@279
|
40 std::cerr << "rate = " << m_rate << ", bfs = " << m_beatframesize << std::endl;
|
c@279
|
41 m_beatframe = new double[m_beatframesize];
|
c@279
|
42 m_fftRealOut = new double[m_beatframesize];
|
c@279
|
43 m_fftImagOut = new double[m_beatframesize];
|
c@279
|
44 }
|
c@279
|
45
|
c@279
|
46 DownBeat::~DownBeat()
|
c@279
|
47 {
|
c@279
|
48 delete m_decimator1;
|
c@279
|
49 delete m_decimator2;
|
c@279
|
50 if (m_buffer) free(m_buffer);
|
c@279
|
51 delete[] m_decbuf;
|
c@279
|
52 delete[] m_beatframe;
|
c@279
|
53 delete[] m_fftRealOut;
|
c@279
|
54 delete[] m_fftImagOut;
|
c@279
|
55 }
|
c@279
|
56
|
c@279
|
57 void
|
c@279
|
58 DownBeat::makeDecimators()
|
c@279
|
59 {
|
c@279
|
60 if (m_factor < 2) return;
|
c@279
|
61 int highest = Decimator::getHighestSupportedFactor();
|
c@279
|
62 if (m_factor <= highest) {
|
c@279
|
63 m_decimator1 = new Decimator(m_increment, m_factor);
|
c@279
|
64 return;
|
c@279
|
65 }
|
c@279
|
66 m_decimator1 = new Decimator(m_increment, highest);
|
c@279
|
67 m_decimator2 = new Decimator(m_increment / highest, m_factor / highest);
|
c@279
|
68 m_decbuf = new double[m_factor / highest];
|
c@279
|
69 }
|
c@279
|
70
|
c@279
|
71 void
|
c@279
|
72 DownBeat::pushAudioBlock(const double *audio)
|
c@279
|
73 {
|
c@279
|
74 if (m_buffill + (m_increment / m_factor) > m_bufsiz) {
|
c@279
|
75 if (m_bufsiz == 0) m_bufsiz = m_increment * 16;
|
c@279
|
76 else m_bufsiz = m_bufsiz * 2;
|
c@279
|
77 if (!m_buffer) {
|
c@279
|
78 m_buffer = (double *)malloc(m_bufsiz * sizeof(double));
|
c@279
|
79 } else {
|
c@279
|
80 std::cerr << "DownBeat::pushAudioBlock: realloc m_buffer to " << m_bufsiz << std::endl;
|
c@279
|
81 m_buffer = (double *)realloc(m_buffer, m_bufsiz * sizeof(double));
|
c@279
|
82 }
|
c@279
|
83 }
|
c@279
|
84 if (!m_decimator1) makeDecimators();
|
c@279
|
85 if (m_decimator2) {
|
c@279
|
86 m_decimator1->process(audio, m_decbuf);
|
c@279
|
87 m_decimator2->process(m_decbuf, m_buffer + m_buffill);
|
c@279
|
88 } else {
|
c@279
|
89 m_decimator1->process(audio, m_buffer + m_buffill);
|
c@279
|
90 }
|
c@279
|
91 m_buffill += m_increment / m_factor;
|
c@279
|
92 }
|
c@279
|
93
|
c@279
|
94 const double *
|
c@279
|
95 DownBeat::getBufferedAudio(size_t &length) const
|
c@279
|
96 {
|
c@279
|
97 length = m_buffill;
|
c@279
|
98 return m_buffer;
|
c@279
|
99 }
|
c@279
|
100
|
c@279
|
101 void
|
c@279
|
102 DownBeat::findDownBeats(const double *audio,
|
c@279
|
103 size_t audioLength,
|
c@279
|
104 const d_vec_t &beats,
|
c@279
|
105 i_vec_t &downbeats)
|
c@279
|
106 {
|
c@279
|
107 // FIND DOWNBEATS BY PARTITIONING THE INPUT AUDIO FILE INTO BEAT SEGMENTS
|
c@279
|
108 // WHERE THE AUDIO FRAMES ARE DOWNSAMPLED BY A FACTOR OF 16 (fs ~= 2700Hz)
|
c@279
|
109 // THEN TAKING THE JENSEN-SHANNON DIVERGENCE BETWEEN BEAT SYNCHRONOUS SPECTRAL FRAMES
|
c@279
|
110
|
c@279
|
111 // IMPLEMENTATION (MOSTLY) FOLLOWS:
|
c@279
|
112 // DAVIES AND PLUMBLEY "A SPECTRAL DIFFERENCE APPROACH TO EXTRACTING DOWNBEATS IN MUSICAL AUDIO"
|
c@279
|
113 // EUSIPCO 2006, FLORENCE, ITALY
|
c@279
|
114
|
c@279
|
115 d_vec_t newspec(m_beatframesize / 2); // magnitude spectrum of current beat
|
c@279
|
116 d_vec_t oldspec(m_beatframesize / 2); // magnitude spectrum of previous beat
|
c@279
|
117 d_vec_t specdiff;
|
c@279
|
118
|
c@279
|
119 if (audioLength == 0) return;
|
c@279
|
120
|
c@279
|
121 for (size_t i = 0; i + 1 < beats.size(); ++i) {
|
c@279
|
122
|
c@279
|
123 // Copy the extents of the current beat from downsampled array
|
c@279
|
124 // into beat frame buffer
|
c@279
|
125
|
c@279
|
126 size_t beatstart = (beats[i] * m_increment) / m_factor;
|
c@279
|
127 size_t beatend = (beats[i] * m_increment) / m_factor;
|
c@279
|
128 if (beatend >= audioLength) beatend = audioLength - 1;
|
c@279
|
129 if (beatend < beatstart) beatend = beatstart;
|
c@279
|
130 size_t beatlen = beatend - beatstart;
|
c@279
|
131
|
c@279
|
132 // Also apply a Hanning window to the beat frame buffer, sized
|
c@279
|
133 // to the beat extents rather than the frame size. (Because
|
c@279
|
134 // the size varies, it's easier to do this by hand than use
|
c@279
|
135 // our Window abstraction.)
|
c@279
|
136
|
c@279
|
137 for (size_t j = 0; j < beatlen; ++j) {
|
c@279
|
138 double mul = 0.5 * (1.0 - cos(TWO_PI * (double(j) / double(beatlen))));
|
c@279
|
139 m_beatframe[j] = audio[beatstart + j] * mul;
|
c@279
|
140 }
|
c@279
|
141
|
c@279
|
142 for (size_t j = beatlen; j < m_beatframesize; ++j) {
|
c@279
|
143 m_beatframe[j] = 0.0;
|
c@279
|
144 }
|
c@279
|
145
|
c@279
|
146 // Now FFT beat frame
|
c@279
|
147
|
c@279
|
148 FFT::process(m_beatframesize, false,
|
c@279
|
149 m_beatframe, 0, m_fftRealOut, m_fftImagOut);
|
c@279
|
150
|
c@279
|
151 // Calculate magnitudes
|
c@279
|
152
|
c@279
|
153 for (size_t j = 0; j < m_beatframesize/2; ++j) {
|
c@279
|
154 newspec[j] = sqrt(m_fftRealOut[j] * m_fftRealOut[j] +
|
c@279
|
155 m_fftImagOut[j] * m_fftImagOut[j]);
|
c@279
|
156 }
|
c@279
|
157
|
c@279
|
158 // Preserve peaks by applying adaptive threshold
|
c@279
|
159
|
c@279
|
160 MathUtilities::adaptiveThreshold(newspec);
|
c@279
|
161
|
c@279
|
162 // Calculate JS divergence between new and old spectral frames
|
c@279
|
163
|
c@279
|
164 specdiff.push_back(measureSpecDiff(oldspec, newspec));
|
c@279
|
165
|
c@279
|
166 // Copy newspec across to old
|
c@279
|
167
|
c@279
|
168 for (size_t j = 0; j < m_beatframesize/2; ++j) {
|
c@279
|
169 oldspec[j] = newspec[j];
|
c@279
|
170 }
|
c@279
|
171 }
|
c@279
|
172
|
c@279
|
173 // We now have all spectral difference measures in specdiff
|
c@279
|
174
|
c@279
|
175 uint timesig = 4; // SHOULD REPLACE THIS WITH A FIND_METER FUNCTION - OR USER PARAMETER
|
c@279
|
176 d_vec_t dbcand(timesig); // downbeat candidates
|
c@279
|
177
|
c@279
|
178 // look for beat transition which leads to greatest spectral change
|
c@279
|
179 for (int beat = 0; beat < timesig; ++beat) {
|
c@279
|
180 for (int example = beat; example < specdiff.size(); ++example) {
|
c@279
|
181 dbcand[beat] += (specdiff[example]) / timesig;
|
c@279
|
182 }
|
c@279
|
183 }
|
c@279
|
184
|
c@279
|
185 // first downbeat is beat at index of maximum value of dbcand
|
c@279
|
186 int dbind = MathUtilities::getMax(dbcand);
|
c@279
|
187
|
c@279
|
188 // remaining downbeats are at timesig intervals from the first
|
c@279
|
189 for (int i = dbind; i < beats.size(); i += timesig) {
|
c@279
|
190 downbeats.push_back(i);
|
c@279
|
191 }
|
c@279
|
192 }
|
c@279
|
193
|
c@279
|
194 double
|
c@279
|
195 DownBeat::measureSpecDiff(d_vec_t oldspec, d_vec_t newspec)
|
c@279
|
196 {
|
c@279
|
197 // JENSEN-SHANNON DIVERGENCE BETWEEN SPECTRAL FRAMES
|
c@279
|
198
|
c@279
|
199 uint SPECSIZE = 512; // ONLY LOOK AT FIRST 512 SAMPLES OF SPECTRUM.
|
c@279
|
200 if (SPECSIZE > oldspec.size()/4) {
|
c@279
|
201 SPECSIZE = oldspec.size()/4;
|
c@279
|
202 }
|
c@279
|
203 double SD = 0.;
|
c@279
|
204 double sd1 = 0.;
|
c@279
|
205
|
c@279
|
206 double sumnew = 0.;
|
c@279
|
207 double sumold = 0.;
|
c@279
|
208
|
c@279
|
209 for (uint i = 0;i < SPECSIZE;i++)
|
c@279
|
210 {
|
c@279
|
211 newspec[i] +=EPS;
|
c@279
|
212 oldspec[i] +=EPS;
|
c@279
|
213
|
c@279
|
214 sumnew+=newspec[i];
|
c@279
|
215 sumold+=oldspec[i];
|
c@279
|
216 }
|
c@279
|
217
|
c@279
|
218 for (uint i = 0;i < SPECSIZE;i++)
|
c@279
|
219 {
|
c@279
|
220 newspec[i] /= (sumnew);
|
c@279
|
221 oldspec[i] /= (sumold);
|
c@279
|
222
|
c@279
|
223 // IF ANY SPECTRAL VALUES ARE 0 (SHOULDN'T BE ANY!) SET THEM TO 1
|
c@279
|
224 if (newspec[i] == 0)
|
c@279
|
225 {
|
c@279
|
226 newspec[i] = 1.;
|
c@279
|
227 }
|
c@279
|
228
|
c@279
|
229 if (oldspec[i] == 0)
|
c@279
|
230 {
|
c@279
|
231 oldspec[i] = 1.;
|
c@279
|
232 }
|
c@279
|
233
|
c@279
|
234 // JENSEN-SHANNON CALCULATION
|
c@279
|
235 sd1 = 0.5*oldspec[i] + 0.5*newspec[i];
|
c@279
|
236 SD = SD + (-sd1*log(sd1)) + (0.5*(oldspec[i]*log(oldspec[i]))) + (0.5*(newspec[i]*log(newspec[i])));
|
c@279
|
237 }
|
c@279
|
238
|
c@279
|
239 return SD;
|
c@279
|
240 }
|
c@279
|
241
|