changeset 2:b4c899822b4e

added in slope bang using median
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Wed, 19 Oct 2011 21:08:45 +0100
parents a7b9c6885eb8
children 979125db34ab
files Source/AubioOnsetDetector.cpp Source/AubioOnsetDetector.h Source/aubioOnsetDetect~.cpp
diffstat 3 files changed, 84 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Source/AubioOnsetDetector.cpp	Tue Oct 18 14:52:27 2011 +0100
+++ b/Source/AubioOnsetDetector.cpp	Wed Oct 19 21:08:45 2011 +0100
@@ -40,6 +40,15 @@
 	lastMedianOnsetFrame = 0;
 	currentFrame = 0;
 	aubioLongTermAverage = 1;
+	lastDfValue = 0;
+	bestSlopeValue = 0;
+	recentValueIndex = 0;
+	lastSlopeOnsetFrame = 0;
+	bestSlopeMedian = 10;
+	slopeFallenBelowMedian = true;
+	
+	for (int i = 0;i< numberOfDetectionValues;i++)
+		recentRawDetectionValues[i] = 1;
 	
 }
 
@@ -80,6 +89,9 @@
 				
 			anrMedianProcessedOnsetFound = checkForMedianOnset(rawDetectionValue);
 				
+			bestSlopeValue = getBestSlopeValue(rawDetectionValue); 
+			anrBestSlopeOnset =	checkForSlopeOnset(bestSlopeValue);
+				
 	//	smpl_t my_sample_value;
 		peakPickedDetectionValue = aubio_peakpick_pimrt_getval(parms); 
 	//peakPickedDetectionValue = my_sample_value;
@@ -123,8 +135,11 @@
 	//check for onset relative to our rising and falling median threshold
 	if (dfvalue > medianDetectionValue * thresholdRelativeToMedian && 
 		dfvalue > aubioLongTermAverage &&
+		//lastDfValue < medianDetectionValue &&
 		 1000*framesToSeconds(currentFrame - lastMedianOnsetFrame) > cutoffForRepeatOnsetsMillis){
+		printf("frame diff between onsets %6.1f", (1000*framesToSeconds(currentFrame - lastMedianOnsetFrame)) );
 			onsetDetected = true;
+			lastMedianOnsetFrame = currentFrame;
 		}
 		
 		aubioLongTermAverage *= 0.999;
@@ -136,10 +151,67 @@
 			medianDetectionValue += 0.01*medianSpeed*(dfvalue - medianDetectionValue);
 	
 	currentFrame++;
+	lastDfValue = dfvalue;
+
 	
 	return onsetDetected;
 }
 
+double AubioOnsetDetector::getBestSlopeValue(float dfvalue){
+//the idea is we want a high slope
+	recentRawDetectionValues[recentValueIndex] = dfvalue;
+	double bestValue = 0;
+	for (int i = 1;i < numberOfDetectionValues;i++){
+		double angle = 0;
+		int otherIndex = (recentValueIndex - i + numberOfDetectionValues)%numberOfDetectionValues;
+		double testValue = 0;
+		if (otherIndex > 0 && recentRawDetectionValues[otherIndex] > 0){
+			angle = atan((float)(i * dfvalue)/ (numberOfDetectionValues*(dfvalue-recentRawDetectionValues[otherIndex])) );
+			testValue = (dfvalue - recentRawDetectionValues[otherIndex]) * cos(angle);
+		}
+		
+		if (testValue > bestValue)
+			bestValue = testValue;
+	}
+	
+	recentValueIndex++;
+	
+	if (recentValueIndex == numberOfDetectionValues)
+	recentValueIndex = 0;
+
+		
+	return bestValue;
+	
+}
+
+
+
+
+bool AubioOnsetDetector :: checkForSlopeOnset(float bestValue){
+	bool onsetDetected = false;
+	//check for onset relative to our processed slope function
+	//a mix between increase in value and the gradient of that increase
+	
+	if (bestValue > bestSlopeMedian * thresholdRelativeToMedian && 
+		1000*framesToSeconds(currentFrame - lastSlopeOnsetFrame) > cutoffForRepeatOnsetsMillis
+		&& slopeFallenBelowMedian
+		){
+		printf("frame diff between onsets %6.1f", (1000*framesToSeconds(currentFrame - lastMedianOnsetFrame)) );
+		onsetDetected = true;
+		lastSlopeOnsetFrame = currentFrame;
+		slopeFallenBelowMedian = false;
+	}
+	
+	
+	if (bestValue > bestSlopeMedian)
+		bestSlopeMedian += (bestValue - bestSlopeMedian)*0.1;
+	else{
+		bestSlopeMedian *= 0.995;
+		slopeFallenBelowMedian = true;;
+	}
+	return onsetDetected;
+}
+
 double  AubioOnsetDetector::framesToSeconds(float frames){
 	double seconds = frames * buffersize / 44100.;
 	return seconds;
--- a/Source/AubioOnsetDetector.h	Tue Oct 18 14:52:27 2011 +0100
+++ b/Source/AubioOnsetDetector.h	Wed Oct 19 21:08:45 2011 +0100
@@ -44,6 +44,9 @@
 	float threshold,  threshold2;
 	float rawDetectionValue, peakPickedDetectionValue;
 
+	static const int numberOfDetectionValues = 16;
+	float recentRawDetectionValues[numberOfDetectionValues];
+	int recentValueIndex;
 	
 	float medianDetectionValue, aubioLongTermAverage ;
 	bool anrMedianProcessedOnsetFound;//simple median method by Andrew Robertson
@@ -52,6 +55,14 @@
 	float thresholdRelativeToMedian;
 	long cutoffForRepeatOnsetsMillis;
 	float medianSpeed;
+	float lastDfValue;
+	float bestSlopeValue;
+	double getBestSlopeValue(float dfvalue);
+	double bestSlopeMedian;
+	bool anrBestSlopeOnset;
+	long lastSlopeOnsetFrame;
+	bool slopeFallenBelowMedian;
+	bool checkForSlopeOnset(float bestValue);
 	
 };
 
--- a/Source/aubioOnsetDetect~.cpp	Tue Oct 18 14:52:27 2011 +0100
+++ b/Source/aubioOnsetDetect~.cpp	Wed Oct 19 21:08:45 2011 +0100
@@ -223,7 +223,7 @@
 		if (x->onsetDetector->anrMedianProcessedOnsetFound)
 				outlet_bang(x->medianBangOutlet);
 		
-		if (x->onsetDetector->aubioOnsetFound)
+		if (x->onsetDetector->anrBestSlopeOnset)
 			outlet_bang(x->bangoutlet);