c@279: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@279: c@279: /* c@279: QM DSP Library c@279: c@279: Centre for Digital Music, Queen Mary, University of London. c@279: This file copyright 2008-2009 Matthew Davies and QMUL. c@279: All rights reserved. c@279: */ c@279: c@279: #include "DownBeat.h" c@279: c@279: #include "maths/MathAliases.h" c@279: #include "maths/MathUtilities.h" c@280: #include "maths/KLDivergence.h" c@279: #include "dsp/transforms/FFT.h" c@279: c@279: #include c@279: #include c@279: c@279: DownBeat::DownBeat(float originalSampleRate, c@279: size_t decimationFactor, c@279: size_t dfIncrement) : c@280: m_bpb(0), c@279: m_rate(originalSampleRate), c@279: m_factor(decimationFactor), c@279: m_increment(dfIncrement), c@279: m_decimator1(0), c@279: m_decimator2(0), c@279: m_buffer(0), c@283: m_decbuf(0), c@279: m_bufsiz(0), c@279: m_buffill(0), c@279: m_beatframesize(0), c@279: m_beatframe(0) c@279: { c@279: // beat frame size is next power of two up from 1.3 seconds at the c@279: // downsampled rate (happens to produce 4096 for 44100 or 48000 at c@279: // 16x decimation, which is our expected normal situation) c@280: m_beatframesize = MathUtilities::nextPowerOfTwo c@280: (int((m_rate / decimationFactor) * 1.3)); c@282: // std::cerr << "rate = " << m_rate << ", bfs = " << m_beatframesize << std::endl; c@279: m_beatframe = new double[m_beatframesize]; c@279: m_fftRealOut = new double[m_beatframesize]; c@279: m_fftImagOut = new double[m_beatframesize]; c@289: m_fft = new FFTReal(m_beatframesize); c@279: } c@279: c@279: DownBeat::~DownBeat() c@279: { c@279: delete m_decimator1; c@279: delete m_decimator2; c@279: if (m_buffer) free(m_buffer); c@279: delete[] m_decbuf; c@279: delete[] m_beatframe; c@279: delete[] m_fftRealOut; c@279: delete[] m_fftImagOut; c@289: delete m_fft; c@279: } c@279: c@279: void c@280: DownBeat::setBeatsPerBar(int bpb) c@280: { c@280: m_bpb = bpb; c@280: } c@280: c@280: void c@279: DownBeat::makeDecimators() c@279: { c@283: // std::cerr << "m_factor = " << m_factor << std::endl; c@279: if (m_factor < 2) return; c@279: int highest = Decimator::getHighestSupportedFactor(); c@279: if (m_factor <= highest) { c@279: m_decimator1 = new Decimator(m_increment, m_factor); c@282: // std::cerr << "DownBeat: decimator 1 factor " << m_factor << ", size " << m_increment << std::endl; c@279: return; c@279: } c@279: m_decimator1 = new Decimator(m_increment, highest); c@282: // std::cerr << "DownBeat: decimator 1 factor " << highest << ", size " << m_increment << std::endl; c@279: m_decimator2 = new Decimator(m_increment / highest, m_factor / highest); c@282: // std::cerr << "DownBeat: decimator 2 factor " << m_factor / highest << ", size " << m_increment / highest << std::endl; c@280: m_decbuf = new float[m_increment / highest]; c@279: } c@279: c@279: void c@280: DownBeat::pushAudioBlock(const float *audio) c@279: { c@279: if (m_buffill + (m_increment / m_factor) > m_bufsiz) { c@279: if (m_bufsiz == 0) m_bufsiz = m_increment * 16; c@279: else m_bufsiz = m_bufsiz * 2; c@279: if (!m_buffer) { c@280: m_buffer = (float *)malloc(m_bufsiz * sizeof(float)); c@279: } else { c@282: // std::cerr << "DownBeat::pushAudioBlock: realloc m_buffer to " << m_bufsiz << std::endl; c@280: m_buffer = (float *)realloc(m_buffer, m_bufsiz * sizeof(float)); c@279: } c@279: } c@283: if (!m_decimator1 && m_factor > 1) makeDecimators(); c@283: // float rmsin = 0, rmsout = 0; c@283: // for (int i = 0; i < m_increment; ++i) { c@283: // rmsin += audio[i] * audio[i]; c@283: // } c@279: if (m_decimator2) { c@279: m_decimator1->process(audio, m_decbuf); c@279: m_decimator2->process(m_decbuf, m_buffer + m_buffill); c@283: } else if (m_decimator1) { c@283: m_decimator1->process(audio, m_buffer + m_buffill); c@279: } else { c@283: // just copy across (m_factor is presumably 1) c@283: for (int i = 0; i < m_increment; ++i) { c@283: (m_buffer + m_buffill)[i] = audio[i]; c@283: } c@279: } c@283: // for (int i = 0; i < m_increment / m_factor; ++i) { c@283: // rmsout += m_buffer[m_buffill + i] * m_buffer[m_buffill + i]; c@283: // } c@282: // std::cerr << "pushAudioBlock: rms in " << sqrt(rmsin) << ", out " << sqrt(rmsout) << std::endl; c@279: m_buffill += m_increment / m_factor; c@279: } c@279: c@280: const float * c@279: DownBeat::getBufferedAudio(size_t &length) const c@279: { c@279: length = m_buffill; c@279: return m_buffer; c@279: } c@279: c@279: void c@280: DownBeat::resetAudioBuffer() c@280: { c@280: if (m_buffer) free(m_buffer); c@283: m_buffer = 0; c@280: m_buffill = 0; c@280: m_bufsiz = 0; c@280: } c@280: c@280: void c@280: DownBeat::findDownBeats(const float *audio, c@279: size_t audioLength, c@279: const d_vec_t &beats, c@279: i_vec_t &downbeats) c@279: { c@279: // FIND DOWNBEATS BY PARTITIONING THE INPUT AUDIO FILE INTO BEAT SEGMENTS c@279: // WHERE THE AUDIO FRAMES ARE DOWNSAMPLED BY A FACTOR OF 16 (fs ~= 2700Hz) c@279: // THEN TAKING THE JENSEN-SHANNON DIVERGENCE BETWEEN BEAT SYNCHRONOUS SPECTRAL FRAMES c@279: c@279: // IMPLEMENTATION (MOSTLY) FOLLOWS: c@279: // DAVIES AND PLUMBLEY "A SPECTRAL DIFFERENCE APPROACH TO EXTRACTING DOWNBEATS IN MUSICAL AUDIO" c@279: // EUSIPCO 2006, FLORENCE, ITALY c@279: c@279: d_vec_t newspec(m_beatframesize / 2); // magnitude spectrum of current beat c@279: d_vec_t oldspec(m_beatframesize / 2); // magnitude spectrum of previous beat c@281: c@281: m_beatsd.clear(); c@279: c@279: if (audioLength == 0) return; c@279: c@279: for (size_t i = 0; i + 1 < beats.size(); ++i) { c@279: c@279: // Copy the extents of the current beat from downsampled array c@279: // into beat frame buffer c@279: c@279: size_t beatstart = (beats[i] * m_increment) / m_factor; c@280: size_t beatend = (beats[i+1] * m_increment) / m_factor; c@279: if (beatend >= audioLength) beatend = audioLength - 1; c@279: if (beatend < beatstart) beatend = beatstart; c@279: size_t beatlen = beatend - beatstart; c@279: c@279: // Also apply a Hanning window to the beat frame buffer, sized c@279: // to the beat extents rather than the frame size. (Because c@279: // the size varies, it's easier to do this by hand than use c@279: // our Window abstraction.) c@279: c@283: // std::cerr << "beatlen = " << beatlen << std::endl; c@283: c@283: // float rms = 0; c@283: for (size_t j = 0; j < beatlen && j < m_beatframesize; ++j) { c@279: double mul = 0.5 * (1.0 - cos(TWO_PI * (double(j) / double(beatlen)))); c@279: m_beatframe[j] = audio[beatstart + j] * mul; c@283: // rms += m_beatframe[j] * m_beatframe[j]; c@279: } c@283: // rms = sqrt(rms); c@282: // std::cerr << "beat " << i << ": audio rms " << rms << std::endl; c@279: c@279: for (size_t j = beatlen; j < m_beatframesize; ++j) { c@279: m_beatframe[j] = 0.0; c@279: } c@279: c@279: // Now FFT beat frame c@279: c@289: m_fft->process(false, m_beatframe, m_fftRealOut, m_fftImagOut); c@279: c@279: // Calculate magnitudes c@279: c@279: for (size_t j = 0; j < m_beatframesize/2; ++j) { c@279: newspec[j] = sqrt(m_fftRealOut[j] * m_fftRealOut[j] + c@279: m_fftImagOut[j] * m_fftImagOut[j]); c@279: } c@279: c@279: // Preserve peaks by applying adaptive threshold c@279: c@279: MathUtilities::adaptiveThreshold(newspec); c@279: c@279: // Calculate JS divergence between new and old spectral frames c@279: c@281: if (i > 0) { // otherwise we have no previous frame c@281: m_beatsd.push_back(measureSpecDiff(oldspec, newspec)); c@282: // std::cerr << "specdiff: " << m_beatsd[m_beatsd.size()-1] << std::endl; c@281: } c@279: c@279: // Copy newspec across to old c@279: c@279: for (size_t j = 0; j < m_beatframesize/2; ++j) { c@279: oldspec[j] = newspec[j]; c@279: } c@279: } c@279: c@279: // We now have all spectral difference measures in specdiff c@279: c@295: unsigned int timesig = m_bpb; c@280: if (timesig == 0) timesig = 4; c@280: c@279: d_vec_t dbcand(timesig); // downbeat candidates c@279: c@280: for (int beat = 0; beat < timesig; ++beat) { c@280: dbcand[beat] = 0; c@280: } c@280: c@279: // look for beat transition which leads to greatest spectral change c@279: for (int beat = 0; beat < timesig; ++beat) { c@281: int count = 0; c@281: for (int example = beat - 1; example < m_beatsd.size(); example += timesig) { c@281: if (example < 0) continue; c@281: dbcand[beat] += (m_beatsd[example]) / timesig; c@281: ++count; c@279: } c@281: if (count > 0) m_beatsd[beat] /= count; c@282: // std::cerr << "dbcand[" << beat << "] = " << dbcand[beat] << std::endl; c@279: } c@279: c@280: c@279: // first downbeat is beat at index of maximum value of dbcand c@279: int dbind = MathUtilities::getMax(dbcand); c@279: c@279: // remaining downbeats are at timesig intervals from the first c@279: for (int i = dbind; i < beats.size(); i += timesig) { c@279: downbeats.push_back(i); c@279: } c@279: } c@279: c@279: double c@279: DownBeat::measureSpecDiff(d_vec_t oldspec, d_vec_t newspec) c@279: { c@279: // JENSEN-SHANNON DIVERGENCE BETWEEN SPECTRAL FRAMES c@279: c@295: unsigned int SPECSIZE = 512; // ONLY LOOK AT FIRST 512 SAMPLES OF SPECTRUM. c@279: if (SPECSIZE > oldspec.size()/4) { c@279: SPECSIZE = oldspec.size()/4; c@279: } c@279: double SD = 0.; c@279: double sd1 = 0.; c@279: c@279: double sumnew = 0.; c@279: double sumold = 0.; c@279: c@295: for (unsigned int i = 0;i < SPECSIZE;i++) c@279: { c@279: newspec[i] +=EPS; c@279: oldspec[i] +=EPS; c@279: c@279: sumnew+=newspec[i]; c@279: sumold+=oldspec[i]; c@279: } c@279: c@295: for (unsigned int i = 0;i < SPECSIZE;i++) c@279: { c@279: newspec[i] /= (sumnew); c@279: oldspec[i] /= (sumold); c@279: c@279: // IF ANY SPECTRAL VALUES ARE 0 (SHOULDN'T BE ANY!) SET THEM TO 1 c@279: if (newspec[i] == 0) c@279: { c@279: newspec[i] = 1.; c@279: } c@279: c@279: if (oldspec[i] == 0) c@279: { c@279: oldspec[i] = 1.; c@279: } c@279: c@279: // JENSEN-SHANNON CALCULATION c@279: sd1 = 0.5*oldspec[i] + 0.5*newspec[i]; c@279: SD = SD + (-sd1*log(sd1)) + (0.5*(oldspec[i]*log(oldspec[i]))) + (0.5*(newspec[i]*log(newspec[i]))); c@279: } c@279: c@279: return SD; c@279: } c@279: c@281: void c@281: DownBeat::getBeatSD(vector &beatsd) const c@281: { c@281: for (int i = 0; i < m_beatsd.size(); ++i) beatsd.push_back(m_beatsd[i]); c@281: } c@281: