diff BeatRootProcessor.cpp @ 15:887c629502a9

refactor: pull method implementations into .cpp files
author Chris Cannam
date Wed, 12 Oct 2011 10:55:52 +0100
parents 1c1e98cd1b2e
children 6afcb5edd7ab
line wrap: on
line diff
--- a/BeatRootProcessor.cpp	Fri Oct 07 14:07:42 2011 +0100
+++ b/BeatRootProcessor.cpp	Wed Oct 12 10:55:52 2011 +0100
@@ -18,3 +18,56 @@
 bool
 BeatRootProcessor::silent = true;
 
+void BeatRootProcessor::processFrame(const float *const *inputBuffers) {
+    double flux = 0;
+    for (int i = 0; i <= fftSize/2; i++) {
+        double mag = sqrt(inputBuffers[0][i*2] * inputBuffers[0][i*2] +
+                          inputBuffers[0][i*2+1] * inputBuffers[0][i*2+1]);
+        if (mag > prevFrame[i]) flux += mag - prevFrame[i];
+        prevFrame[i] = mag;
+    }
+    
+    spectralFlux.push_back(flux);
+    
+} // processFrame()
+
+EventList BeatRootProcessor::beatTrack() {
+
+#ifdef DEBUG_BEATROOT
+    std::cerr << "Spectral flux:" << std::endl;
+    for (int i = 0; i < spectralFlux.size(); ++i) {
+        if ((i % 8) == 0) std::cerr << "\n";
+        std::cerr << spectralFlux[i] << " ";
+    }
+#endif
+		
+    double hop = hopTime;
+    Peaks::normalise(spectralFlux);
+    vector<int> peaks = Peaks::findPeaks(spectralFlux, (int)lrint(0.06 / hop), 0.35, 0.84, true);
+    onsets.clear();
+    onsets.resize(peaks.size(), 0);
+    vector<int>::iterator it = peaks.begin();
+    onsetList.clear();
+    double minSalience = Peaks::min(spectralFlux);
+    for (int i = 0; i < onsets.size(); i++) {
+        int index = *it;
+        ++it;
+        onsets[i] = index * hop;
+        Event e = BeatTracker::newBeat(onsets[i], 0);
+//			if (debug)
+//				System.err.printf("Onset: %8.3f  %8.3f  %8.3f\n",
+//						onsets[i], energy[index], slope[index]);
+//			e.salience = slope[index];	// or combination of energy + slope??
+        // Note that salience must be non-negative or the beat tracking system fails!
+        e.salience = spectralFlux[index] - minSalience;
+        onsetList.push_back(e);
+    }
+
+#ifdef DEBUG_BEATROOT
+    std::cerr << "Onsets: " << onsetList.size() << std::endl;
+#endif
+
+    return BeatTracker::beatTrack(onsetList);
+
+} // processFile()
+