diff src/OnsetDetectionFunction.cpp @ 117:ca2d83d29814 tip master

Merge branch 'release/1.0.5'
author Adam Stark <adamstark.uk@gmail.com>
date Fri, 18 Aug 2023 20:07:33 +0200
parents 54c657d621dd
children
line wrap: on
line diff
--- a/src/OnsetDetectionFunction.cpp	Sat Jun 18 10:50:06 2016 +0100
+++ b/src/OnsetDetectionFunction.cpp	Fri Aug 18 20:07:33 2023 +0200
@@ -23,7 +23,7 @@
 #include "OnsetDetectionFunction.h"
 
 //=======================================================================
-OnsetDetectionFunction::OnsetDetectionFunction (int hopSize_,int frameSize_)
+OnsetDetectionFunction::OnsetDetectionFunction (int hopSize_, int frameSize_)
  :  onsetDetectionFunctionType (ComplexSpectralDifferenceHWR), windowType (HanningWindow)
 {
     // indicate that we have not initialised yet
@@ -37,7 +37,7 @@
 }
 
 //=======================================================================
-OnsetDetectionFunction::OnsetDetectionFunction(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType_)
+OnsetDetectionFunction::OnsetDetectionFunction (int hopSize_, int frameSize_, int onsetDetectionFunctionType_, int windowType_)
  :  onsetDetectionFunctionType (ComplexSpectralDifferenceHWR), windowType (HanningWindow)
 {	
 	// indicate that we have not initialised yet
@@ -69,7 +69,7 @@
 }
 
 //=======================================================================
-void OnsetDetectionFunction::initialise(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType_)
+void OnsetDetectionFunction::initialise (int hopSize_, int frameSize_, int onsetDetectionFunctionType_, int windowType_)
 {
 	hopSize = hopSize_; // set hopsize
 	frameSize = frameSize_; // set framesize
@@ -140,7 +140,7 @@
 #ifdef USE_KISS_FFT
     complexOut.resize (frameSize);
     
-    for (int i = 0; i < frameSize;i++)
+    for (int i = 0; i < frameSize; i++)
     {
         complexOut[i].resize(2);
     }
@@ -181,14 +181,11 @@
 	double odfSample;
 		
 	// shift audio samples back in frame by hop size
-	for (int i = 0; i < (frameSize-hopSize);i++)
-	{
-		frame[i] = frame[i+hopSize];
-	}
+    std::rotate (frame.begin(), frame.begin() + hopSize, frame.end());
 	
 	// add new samples to frame from input buffer
 	int j = 0;
-	for (int i = (frameSize-hopSize);i < frameSize;i++)
+	for (int i = (frameSize - hopSize); i < frameSize; i++)
 	{
 		frame[i] = buffer[j];
 		j++;
@@ -269,11 +266,11 @@
 //=======================================================================
 void OnsetDetectionFunction::performFFT()
 {
-    int fsize2 = (frameSize/2);
+    int fsize2 = (frameSize / 2);
     
 #ifdef USE_FFTW
 	// window frame and copy to complex array, swapping the first and second half of the signal
-	for (int i = 0;i < fsize2;i++)
+	for (int i = 0; i < fsize2; i++)
 	{
 		complexIn[i][0] = frame[i + fsize2] * window[i + fsize2];
 		complexIn[i][1] = 0.0;
@@ -318,7 +315,7 @@
 	sum = 0;	// initialise sum
 	
 	// sum the squares of the samples
-	for (int i = 0;i < frameSize;i++)
+	for (int i = 0; i < frameSize; i++)
 	{
 		sum = sum + (frame[i] * frame[i]);
 	}
@@ -363,15 +360,15 @@
 	// perform the FFT
 	performFFT();
 	
-	// compute first (N/2)+1 mag values
-	for (int i = 0;i < (frameSize/2)+1;i++)
+	// compute first (N / 2) + 1 mag values
+	for (int i = 0; i < (frameSize / 2) + 1; i++)
 	{
 		magSpec[i] = sqrt (pow (complexOut[i][0], 2) + pow (complexOut[i][1], 2));
 	}
-	// mag spec symmetric above (N/2)+1 so copy previous values
-	for (int i = (frameSize/2)+1; i < frameSize; i++)
+	// mag spec symmetric above (N / 2) + 1 so copy previous values
+	for (int i = (frameSize / 2) + 1; i < frameSize; i++)
 	{
-		magSpec[i] = magSpec[frameSize-i];
+		magSpec[i] = magSpec[frameSize - i];
 	}
 	
 	sum = 0;	// initialise sum to zero
@@ -384,7 +381,7 @@
 		// ensure all difference values are positive
 		if (diff < 0)
 		{
-			diff = diff*-1;
+			diff = diff * -1;
 		}
 		
 		// add difference to sum
@@ -406,20 +403,20 @@
 	// perform the FFT
 	performFFT();
 	
-	// compute first (N/2)+1 mag values
-	for (int i = 0;i < (frameSize/2) + 1; i++)
+	// compute first (N / 2) + 1 mag values
+	for (int i = 0; i < (frameSize / 2) + 1; i++)
 	{
 		magSpec[i] = sqrt (pow (complexOut[i][0],2) + pow (complexOut[i][1],2));
 	}
-	// mag spec symmetric above (N/2)+1 so copy previous values
-	for (int i = (frameSize/2)+1;i < frameSize;i++)
+	// mag spec symmetric above (N / 2) + 1 so copy previous values
+	for (int i = (frameSize / 2) + 1; i < frameSize; i++)
 	{
-		magSpec[i] = magSpec[frameSize-i];
+		magSpec[i] = magSpec[frameSize - i];
 	}
 	
 	sum = 0;	// initialise sum to zero
 	
-	for (int i = 0;i < frameSize;i++)
+	for (int i = 0; i < frameSize; i++)
 	{
 		// calculate difference
 		diff = magSpec[i] - prevMagSpec[i];
@@ -451,7 +448,7 @@
 	sum = 0; // initialise sum to zero
 	
 	// compute phase values from fft output and sum deviations
-	for (int i = 0;i < frameSize;i++)
+	for (int i = 0; i < frameSize; i++)
 	{
 		// calculate phase value
 		phase[i] = atan2 (complexOut[i][1], complexOut[i][0]);
@@ -463,13 +460,13 @@
 		// if bin is not just a low energy bin then examine phase deviation
 		if (magSpec[i] > 0.1)
 		{
-			dev = phase[i] - (2*prevPhase[i]) + prevPhase2[i];	// phase deviation
+			dev = phase[i] - (2 * prevPhase[i]) + prevPhase2[i];	// phase deviation
 			pdev = princarg (dev);	// wrap into [-pi,pi] range
 		
 			// make all values positive
 			if (pdev < 0)	
 			{
-				pdev = pdev*-1;
+				pdev = pdev * -1;
 			}
 						
 			// add to sum
@@ -497,7 +494,7 @@
 	sum = 0; // initialise sum to zero
 	
 	// compute phase values from fft output and sum deviations
-	for (int i = 0;i < frameSize;i++)
+	for (int i = 0; i < frameSize; i++)
 	{
 		// calculate phase value
 		phase[i] = atan2 (complexOut[i][1], complexOut[i][0]);
@@ -537,7 +534,7 @@
 	sum = 0; // initialise sum to zero
 	
 	// compute phase values from fft output and sum deviations
-	for (int i = 0;i < frameSize;i++)
+	for (int i = 0; i < frameSize; i++)
 	{
 		// calculate phase value
 		phase[i] = atan2 (complexOut[i][1], complexOut[i][0]);
@@ -588,7 +585,7 @@
 		magSpec[i] = sqrt (pow (complexOut[i][0],2) + pow (complexOut[i][1],2));
 		
 		
-		sum = sum + (magSpec[i] * ((double) (i+1)));
+		sum = sum + (magSpec[i] * ((double) (i + 1)));
 		
 		// store values for next calculation
 		prevMagSpec[i] = magSpec[i];
@@ -609,7 +606,7 @@
 	sum = 0; // initialise sum to zero
 	
 	// compute phase values from fft output and sum deviations
-	for (int i = 0;i < frameSize;i++)
+	for (int i = 0; i < frameSize; i++)
 	{		
 		// calculate magnitude value
 		magSpec[i] = sqrt (pow (complexOut[i][0],2) + pow (complexOut[i][1],2));
@@ -622,7 +619,7 @@
 			mag_diff = -mag_diff;
 		}
 		
-		sum = sum + (mag_diff * ((double) (i+1)));
+		sum = sum + (mag_diff * ((double) (i + 1)));
 		
 		// store values for next calculation
 		prevMagSpec[i] = magSpec[i];
@@ -643,7 +640,7 @@
 	sum = 0; // initialise sum to zero
 	
 	// compute phase values from fft output and sum deviations
-	for (int i = 0;i < frameSize;i++)
+	for (int i = 0; i < frameSize; i++)
 	{		
 		// calculate magnitude value
 		magSpec[i] = sqrt (pow (complexOut[i][0],2) + pow (complexOut[i][1],2));
@@ -653,7 +650,7 @@
 		
 		if (mag_diff > 0)
 		{
-			sum = sum + (mag_diff * ((double) (i+1)));
+			sum = sum + (mag_diff * ((double) (i + 1)));
 		}
 
 		// store values for next calculation
@@ -673,7 +670,7 @@
 {
 	double N;		// variable to store framesize minus 1
 	
-	N = (double) (frameSize-1);	// framesize minus 1
+	N = (double) (frameSize - 1);	// framesize minus 1
 	
 	// Hanning window calculation
 	for (int n = 0; n < frameSize; n++)
@@ -688,13 +685,13 @@
 	double N;		// variable to store framesize minus 1
 	double n_val;	// double version of index 'n'
 	
-	N = (double) (frameSize-1);	// framesize minus 1
+	N = (double) (frameSize - 1);	// framesize minus 1
 	n_val = 0;
 	
 	// Hamming window calculation
-	for (int n = 0;n < frameSize;n++)
+	for (int n = 0; n < frameSize; n++)
 	{
-		window[n] = 0.54 - (0.46 * cos (2 * pi * (n_val/N)));
+		window[n] = 0.54 - (0.46 * cos (2 * pi * (n_val / N)));
 		n_val = n_val+1;
 	}
 }
@@ -705,14 +702,14 @@
 	double N;		// variable to store framesize minus 1
 	double n_val;	// double version of index 'n'
 	
-	N = (double) (frameSize-1);	// framesize minus 1
+	N = (double) (frameSize - 1);	// framesize minus 1
 	n_val = 0;
 	
 	// Blackman window calculation
-	for (int n = 0;n < frameSize;n++)
+	for (int n = 0; n < frameSize; n++)
 	{
-		window[n] = 0.42 - (0.5*cos(2*pi*(n_val/N))) + (0.08*cos(4*pi*(n_val/N)));
-		n_val = n_val+1;
+		window[n] = 0.42 - (0.5 * cos (2 * pi * (n_val / N))) + (0.08 * cos (4 * pi * (n_val / N)));
+		n_val = n_val + 1;
 	}
 }
 
@@ -725,28 +722,28 @@
 	
 	alpha = 0.5;
 	
-	N = (double) (frameSize-1);	// framesize minus 1
+	N = (double) (frameSize - 1);	// framesize minus 1
 		
 	// Tukey window calculation
 	
-	n_val = (double) (-1*((frameSize/2)))+1;
+	n_val = (double) (-1 * ((frameSize / 2))) + 1;
 
-	for (int n = 0;n < frameSize;n++)	// left taper
+	for (int n = 0; n < frameSize; n++)	// left taper
 	{
-		if ((n_val >= 0) && (n_val <= (alpha*(N/2))))
+		if ((n_val >= 0) && (n_val <= (alpha * (N / 2))))
 		{
 			window[n] = 1.0;
 		}
-		else if ((n_val <= 0) && (n_val >= (-1*alpha*(N/2))))
+		else if ((n_val <= 0) && (n_val >= (-1 * alpha * (N / 2))))
 		{
 			window[n] = 1.0;
 		}
 		else
 		{
-			window[n] = 0.5*(1+cos(pi*(((2*n_val)/(alpha*N))-1)));
+			window[n] = 0.5 * (1 + cos (pi * (((2 * n_val) / (alpha * N)) - 1)));
 		}
 
-		n_val = n_val+1;			 
+		n_val = n_val + 1;
 	}
 
 }
@@ -755,7 +752,7 @@
 void OnsetDetectionFunction::calculateRectangularWindow()
 {
 	// Rectangular window calculation
-	for (int n = 0;n < frameSize;n++)
+	for (int n = 0; n < frameSize; n++)
 	{
 		window[n] = 1.0;
 	}