annotate dsp/tempotracking/DownBeat.cpp @ 387:00f66226db5b

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