annotate dsp/tempotracking/DownBeat.cpp @ 281:33e03341d541

* Make it possible to retrieve beat spectral difference function from bar detector * Avoid crashes when bar detector is run with very short input
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 27 Feb 2009 10:23:08 +0000
parents 9c403afdd9e9
children d241e7701c0c
rev   line source
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@280 15 #include "maths/KLDivergence.h"
c@279 16 #include "dsp/transforms/FFT.h"
c@279 17
c@279 18 #include <iostream>
c@279 19 #include <cstdlib>
c@279 20
c@279 21 DownBeat::DownBeat(float originalSampleRate,
c@279 22 size_t decimationFactor,
c@279 23 size_t dfIncrement) :
c@280 24 m_bpb(0),
c@279 25 m_rate(originalSampleRate),
c@279 26 m_factor(decimationFactor),
c@279 27 m_increment(dfIncrement),
c@279 28 m_decimator1(0),
c@279 29 m_decimator2(0),
c@279 30 m_buffer(0),
c@279 31 m_bufsiz(0),
c@279 32 m_buffill(0),
c@279 33 m_beatframesize(0),
c@279 34 m_beatframe(0)
c@279 35 {
c@279 36 // beat frame size is next power of two up from 1.3 seconds at the
c@279 37 // downsampled rate (happens to produce 4096 for 44100 or 48000 at
c@279 38 // 16x decimation, which is our expected normal situation)
c@280 39 m_beatframesize = MathUtilities::nextPowerOfTwo
c@280 40 (int((m_rate / decimationFactor) * 1.3));
c@279 41 std::cerr << "rate = " << m_rate << ", bfs = " << m_beatframesize << std::endl;
c@279 42 m_beatframe = new double[m_beatframesize];
c@279 43 m_fftRealOut = new double[m_beatframesize];
c@279 44 m_fftImagOut = new double[m_beatframesize];
c@279 45 }
c@279 46
c@279 47 DownBeat::~DownBeat()
c@279 48 {
c@279 49 delete m_decimator1;
c@279 50 delete m_decimator2;
c@279 51 if (m_buffer) free(m_buffer);
c@279 52 delete[] m_decbuf;
c@279 53 delete[] m_beatframe;
c@279 54 delete[] m_fftRealOut;
c@279 55 delete[] m_fftImagOut;
c@279 56 }
c@279 57
c@279 58 void
c@280 59 DownBeat::setBeatsPerBar(int bpb)
c@280 60 {
c@280 61 m_bpb = bpb;
c@280 62 }
c@280 63
c@280 64 void
c@279 65 DownBeat::makeDecimators()
c@279 66 {
c@279 67 if (m_factor < 2) return;
c@279 68 int highest = Decimator::getHighestSupportedFactor();
c@279 69 if (m_factor <= highest) {
c@279 70 m_decimator1 = new Decimator(m_increment, m_factor);
c@280 71 std::cerr << "DownBeat: decimator 1 factor " << m_factor << ", size " << m_increment << std::endl;
c@279 72 return;
c@279 73 }
c@279 74 m_decimator1 = new Decimator(m_increment, highest);
c@280 75 std::cerr << "DownBeat: decimator 1 factor " << highest << ", size " << m_increment << std::endl;
c@279 76 m_decimator2 = new Decimator(m_increment / highest, m_factor / highest);
c@280 77 std::cerr << "DownBeat: decimator 2 factor " << m_factor / highest << ", size " << m_increment / highest << std::endl;
c@280 78 m_decbuf = new float[m_increment / highest];
c@279 79 }
c@279 80
c@279 81 void
c@280 82 DownBeat::pushAudioBlock(const float *audio)
c@279 83 {
c@279 84 if (m_buffill + (m_increment / m_factor) > m_bufsiz) {
c@279 85 if (m_bufsiz == 0) m_bufsiz = m_increment * 16;
c@279 86 else m_bufsiz = m_bufsiz * 2;
c@279 87 if (!m_buffer) {
c@280 88 m_buffer = (float *)malloc(m_bufsiz * sizeof(float));
c@279 89 } else {
c@279 90 std::cerr << "DownBeat::pushAudioBlock: realloc m_buffer to " << m_bufsiz << std::endl;
c@280 91 m_buffer = (float *)realloc(m_buffer, m_bufsiz * sizeof(float));
c@279 92 }
c@279 93 }
c@279 94 if (!m_decimator1) makeDecimators();
c@280 95 float rmsin = 0, rmsout = 0;
c@280 96 for (int i = 0; i < m_increment; ++i) {
c@280 97 rmsin += audio[i] * audio[i];
c@280 98 }
c@279 99 if (m_decimator2) {
c@279 100 m_decimator1->process(audio, m_decbuf);
c@279 101 m_decimator2->process(m_decbuf, m_buffer + m_buffill);
c@279 102 } else {
c@279 103 m_decimator1->process(audio, m_buffer + m_buffill);
c@279 104 }
c@280 105 for (int i = 0; i < m_increment / m_factor; ++i) {
c@280 106 rmsout += m_buffer[m_buffill + i] * m_buffer[m_buffill + i];
c@280 107 }
c@280 108 std::cerr << "pushAudioBlock: rms in " << sqrt(rmsin) << ", out " << sqrt(rmsout) << std::endl;
c@279 109 m_buffill += m_increment / m_factor;
c@279 110 }
c@279 111
c@280 112 const float *
c@279 113 DownBeat::getBufferedAudio(size_t &length) const
c@279 114 {
c@279 115 length = m_buffill;
c@279 116 return m_buffer;
c@279 117 }
c@279 118
c@279 119 void
c@280 120 DownBeat::resetAudioBuffer()
c@280 121 {
c@280 122 if (m_buffer) free(m_buffer);
c@280 123 m_buffill = 0;
c@280 124 m_bufsiz = 0;
c@280 125 }
c@280 126
c@280 127 void
c@280 128 DownBeat::findDownBeats(const float *audio,
c@279 129 size_t audioLength,
c@279 130 const d_vec_t &beats,
c@279 131 i_vec_t &downbeats)
c@279 132 {
c@279 133 // FIND DOWNBEATS BY PARTITIONING THE INPUT AUDIO FILE INTO BEAT SEGMENTS
c@279 134 // WHERE THE AUDIO FRAMES ARE DOWNSAMPLED BY A FACTOR OF 16 (fs ~= 2700Hz)
c@279 135 // THEN TAKING THE JENSEN-SHANNON DIVERGENCE BETWEEN BEAT SYNCHRONOUS SPECTRAL FRAMES
c@279 136
c@279 137 // IMPLEMENTATION (MOSTLY) FOLLOWS:
c@279 138 // DAVIES AND PLUMBLEY "A SPECTRAL DIFFERENCE APPROACH TO EXTRACTING DOWNBEATS IN MUSICAL AUDIO"
c@279 139 // EUSIPCO 2006, FLORENCE, ITALY
c@279 140
c@279 141 d_vec_t newspec(m_beatframesize / 2); // magnitude spectrum of current beat
c@279 142 d_vec_t oldspec(m_beatframesize / 2); // magnitude spectrum of previous beat
c@281 143
c@281 144 m_beatsd.clear();
c@279 145
c@279 146 if (audioLength == 0) return;
c@279 147
c@279 148 for (size_t i = 0; i + 1 < beats.size(); ++i) {
c@279 149
c@279 150 // Copy the extents of the current beat from downsampled array
c@279 151 // into beat frame buffer
c@279 152
c@279 153 size_t beatstart = (beats[i] * m_increment) / m_factor;
c@280 154 size_t beatend = (beats[i+1] * m_increment) / m_factor;
c@279 155 if (beatend >= audioLength) beatend = audioLength - 1;
c@279 156 if (beatend < beatstart) beatend = beatstart;
c@279 157 size_t beatlen = beatend - beatstart;
c@279 158
c@279 159 // Also apply a Hanning window to the beat frame buffer, sized
c@279 160 // to the beat extents rather than the frame size. (Because
c@279 161 // the size varies, it's easier to do this by hand than use
c@279 162 // our Window abstraction.)
c@279 163
c@280 164 float rms = 0;
c@279 165 for (size_t j = 0; j < beatlen; ++j) {
c@279 166 double mul = 0.5 * (1.0 - cos(TWO_PI * (double(j) / double(beatlen))));
c@279 167 m_beatframe[j] = audio[beatstart + j] * mul;
c@280 168 rms += m_beatframe[j] * m_beatframe[j];
c@279 169 }
c@280 170 rms = sqrt(rms);
c@280 171 std::cerr << "beat " << i << ": audio rms " << rms << std::endl;
c@279 172
c@279 173 for (size_t j = beatlen; j < m_beatframesize; ++j) {
c@279 174 m_beatframe[j] = 0.0;
c@279 175 }
c@279 176
c@279 177 // Now FFT beat frame
c@279 178
c@279 179 FFT::process(m_beatframesize, false,
c@279 180 m_beatframe, 0, m_fftRealOut, m_fftImagOut);
c@279 181
c@279 182 // Calculate magnitudes
c@279 183
c@279 184 for (size_t j = 0; j < m_beatframesize/2; ++j) {
c@279 185 newspec[j] = sqrt(m_fftRealOut[j] * m_fftRealOut[j] +
c@279 186 m_fftImagOut[j] * m_fftImagOut[j]);
c@279 187 }
c@279 188
c@279 189 // Preserve peaks by applying adaptive threshold
c@279 190
c@279 191 MathUtilities::adaptiveThreshold(newspec);
c@279 192
c@279 193 // Calculate JS divergence between new and old spectral frames
c@279 194
c@281 195 if (i > 0) { // otherwise we have no previous frame
c@281 196 m_beatsd.push_back(measureSpecDiff(oldspec, newspec));
c@281 197 std::cerr << "specdiff: " << m_beatsd[m_beatsd.size()-1] << std::endl;
c@281 198 }
c@279 199
c@279 200 // Copy newspec across to old
c@279 201
c@279 202 for (size_t j = 0; j < m_beatframesize/2; ++j) {
c@279 203 oldspec[j] = newspec[j];
c@279 204 }
c@279 205 }
c@279 206
c@279 207 // We now have all spectral difference measures in specdiff
c@279 208
c@280 209 uint timesig = m_bpb;
c@280 210 if (timesig == 0) timesig = 4;
c@280 211
c@279 212 d_vec_t dbcand(timesig); // downbeat candidates
c@279 213
c@280 214 for (int beat = 0; beat < timesig; ++beat) {
c@280 215 dbcand[beat] = 0;
c@280 216 }
c@280 217
c@279 218 // look for beat transition which leads to greatest spectral change
c@279 219 for (int beat = 0; beat < timesig; ++beat) {
c@281 220 int count = 0;
c@281 221 for (int example = beat - 1; example < m_beatsd.size(); example += timesig) {
c@281 222 if (example < 0) continue;
c@281 223 dbcand[beat] += (m_beatsd[example]) / timesig;
c@281 224 ++count;
c@279 225 }
c@281 226 if (count > 0) m_beatsd[beat] /= count;
c@280 227 std::cerr << "dbcand[" << beat << "] = " << dbcand[beat] << std::endl;
c@279 228 }
c@279 229
c@280 230
c@279 231 // first downbeat is beat at index of maximum value of dbcand
c@279 232 int dbind = MathUtilities::getMax(dbcand);
c@279 233
c@279 234 // remaining downbeats are at timesig intervals from the first
c@279 235 for (int i = dbind; i < beats.size(); i += timesig) {
c@279 236 downbeats.push_back(i);
c@279 237 }
c@279 238 }
c@279 239
c@279 240 double
c@279 241 DownBeat::measureSpecDiff(d_vec_t oldspec, d_vec_t newspec)
c@279 242 {
c@279 243 // JENSEN-SHANNON DIVERGENCE BETWEEN SPECTRAL FRAMES
c@279 244
c@279 245 uint SPECSIZE = 512; // ONLY LOOK AT FIRST 512 SAMPLES OF SPECTRUM.
c@279 246 if (SPECSIZE > oldspec.size()/4) {
c@279 247 SPECSIZE = oldspec.size()/4;
c@279 248 }
c@279 249 double SD = 0.;
c@279 250 double sd1 = 0.;
c@279 251
c@279 252 double sumnew = 0.;
c@279 253 double sumold = 0.;
c@279 254
c@279 255 for (uint i = 0;i < SPECSIZE;i++)
c@279 256 {
c@279 257 newspec[i] +=EPS;
c@279 258 oldspec[i] +=EPS;
c@279 259
c@279 260 sumnew+=newspec[i];
c@279 261 sumold+=oldspec[i];
c@279 262 }
c@279 263
c@279 264 for (uint i = 0;i < SPECSIZE;i++)
c@279 265 {
c@279 266 newspec[i] /= (sumnew);
c@279 267 oldspec[i] /= (sumold);
c@279 268
c@279 269 // IF ANY SPECTRAL VALUES ARE 0 (SHOULDN'T BE ANY!) SET THEM TO 1
c@279 270 if (newspec[i] == 0)
c@279 271 {
c@279 272 newspec[i] = 1.;
c@279 273 }
c@279 274
c@279 275 if (oldspec[i] == 0)
c@279 276 {
c@279 277 oldspec[i] = 1.;
c@279 278 }
c@279 279
c@279 280 // JENSEN-SHANNON CALCULATION
c@279 281 sd1 = 0.5*oldspec[i] + 0.5*newspec[i];
c@279 282 SD = SD + (-sd1*log(sd1)) + (0.5*(oldspec[i]*log(oldspec[i]))) + (0.5*(newspec[i]*log(newspec[i])));
c@279 283 }
c@279 284
c@279 285 return SD;
c@279 286 }
c@279 287
c@281 288 void
c@281 289 DownBeat::getBeatSD(vector<double> &beatsd) const
c@281 290 {
c@281 291 for (int i = 0; i < m_beatsd.size(); ++i) beatsd.push_back(m_beatsd[i]);
c@281 292 }
c@281 293