From 1043811c6b07783528b9d5c487879eda540cf6e3 Mon Sep 17 00:00:00 2001 From: oparviai Date: Wed, 13 Jul 2011 08:07:14 +0000 Subject: [PATCH] Added support for writing bpm analysis debug file --- source/SoundTouch/BPMDetect.cpp | 40 ++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/source/SoundTouch/BPMDetect.cpp b/source/SoundTouch/BPMDetect.cpp index 4faa294..8b3d1c6 100644 --- a/source/SoundTouch/BPMDetect.cpp +++ b/source/SoundTouch/BPMDetect.cpp @@ -57,6 +57,7 @@ #include #include #include +#include #include "FIFOSampleBuffer.h" #include "PeakFinder.h" #include "BPMDetect.h" @@ -74,6 +75,37 @@ const float avgdecay = 0.99986f; const float avgnorm = (1 - avgdecay); +//////////////////////////////////////////////////////////////////////////////// + +// Enable following define to create bpm analysis file: + +// #define _CREATE_BPM_DEBUG_FILE + +#ifdef _CREATE_BPM_DEBUG_FILE + + #define DEBUGFILE_NAME "c:\\temp\\soundtouch-bpm-debug.txt" + + static void _SaveDebugData(const float *data, int minpos, int maxpos, double coeff) + { + FILE *fptr = fopen(DEBUGFILE_NAME, "wt"); + int i; + + if (fptr) + { + printf("\n\nWriting BPM debug data into file " DEBUGFILE_NAME "\n\n"); + for (i = minpos; i < maxpos; i ++) + { + fprintf(fptr, "%d\t%.1lf\t%f\n", i, coeff / (double)i, data[i]); + } + fclose(fptr); + } + } +#else + #define _SaveDebugData(a,b,c,d) +#endif + +//////////////////////////////////////////////////////////////////////////////// + BPMDetect::BPMDetect(int numChannels, int aSampleRate) { @@ -326,8 +358,14 @@ void BPMDetect::inputSamples(const SAMPLETYPE *samples, int numSamples) float BPMDetect::getBpm() { double peakPos; + double coeff; PeakFinder peakFinder; + coeff = 60.0 * ((double)sampleRate / (double)decimateBy); + + // save bpm debug analysis data if debug data enabled + _SaveDebugData(xcorr, windowStart, windowLen, coeff); + // find peak position peakPos = peakFinder.detectPeak(xcorr, windowStart, windowLen); @@ -335,5 +373,5 @@ float BPMDetect::getBpm() if (peakPos < 1e-9) return 0.0; // detection failed. // calculate BPM - return (float)(60.0 * (((double)sampleRate / (double)decimateBy) / peakPos)); + return (float) (coeff / peakPos); }