diff dsp/tempotracking/DownBeat.cpp @ 56:a0f987c06bec

* Make it possible to retrieve beat spectral difference function from bar detector * Avoid crashes when bar detector is run with very short input
author cannam
date Fri, 27 Feb 2009 10:23:08 +0000
parents 7fe29d8a7eaf
children d241e7701c0c
line wrap: on
line diff
--- a/dsp/tempotracking/DownBeat.cpp	Tue Feb 10 16:37:11 2009 +0000
+++ b/dsp/tempotracking/DownBeat.cpp	Fri Feb 27 10:23:08 2009 +0000
@@ -140,7 +140,8 @@
 
     d_vec_t newspec(m_beatframesize / 2); // magnitude spectrum of current beat
     d_vec_t oldspec(m_beatframesize / 2); // magnitude spectrum of previous beat
-    d_vec_t specdiff;
+
+    m_beatsd.clear();
 
     if (audioLength == 0) return;
 
@@ -191,10 +192,10 @@
 
         // Calculate JS divergence between new and old spectral frames
 
-        specdiff.push_back(measureSpecDiff(oldspec, newspec));
-//        specdiff.push_back(KLDivergence().distanceDistribution(oldspec, newspec, false));
-
-        std::cerr << "specdiff: " << specdiff[specdiff.size()-1] << std::endl;
+        if (i > 0) { // otherwise we have no previous frame
+            m_beatsd.push_back(measureSpecDiff(oldspec, newspec));
+            std::cerr << "specdiff: " << m_beatsd[m_beatsd.size()-1] << std::endl;
+        }
 
         // Copy newspec across to old
 
@@ -216,9 +217,13 @@
 
     // look for beat transition which leads to greatest spectral change
     for (int beat = 0; beat < timesig; ++beat) {
-        for (int example = beat; example < specdiff.size(); example += timesig) {
-            dbcand[beat] += (specdiff[example]) / timesig;
+        int count = 0;
+        for (int example = beat - 1; example < m_beatsd.size(); example += timesig) {
+            if (example < 0) continue;
+            dbcand[beat] += (m_beatsd[example]) / timesig;
+            ++count;
         }
+        if (count > 0) m_beatsd[beat] /= count;
         std::cerr << "dbcand[" << beat << "] = " << dbcand[beat] << std::endl;
     }
 
@@ -280,3 +285,9 @@
     return SD;
 }
 
+void
+DownBeat::getBeatSD(vector<double> &beatsd) const
+{
+    for (int i = 0; i < m_beatsd.size(); ++i) beatsd.push_back(m_beatsd[i]);
+}
+