Mercurial > hg > btrack
diff src/OnsetDetectionFunction.h @ 72:f4d9410f187e
flow: Merged <release> '1.0.0' to <master> ('master').
author | Adam Stark <adamstark@users.noreply.github.com> |
---|---|
date | Tue, 08 Jul 2014 12:32:27 +0100 |
parents | b387d8327729 |
children | f6708e4c69f1 |
line wrap: on
line diff
--- a/src/OnsetDetectionFunction.h Tue Jan 21 01:45:36 2014 +0000 +++ b/src/OnsetDetectionFunction.h Tue Jul 08 12:32:27 2014 +0100 @@ -19,72 +19,174 @@ */ //======================================================================= -#ifndef __RTONSETDF_H -#define __RTONSETDF_H +#ifndef __ONSETDETECTIONFUNCTION_H +#define __ONSETDETECTIONFUNCTION_H #include "fftw3.h" +#include <vector> +//======================================================================= +/** The type of onset detection function to calculate */ +enum OnsetDetectionFunctionType +{ + EnergyEnvelope, + EnergyDifference, + SpectralDifference, + SpectralDifferenceHWR, + PhaseDeviation, + ComplexSpectralDifference, + ComplexSpectralDifferenceHWR, + HighFrequencyContent, + HighFrequencySpectralDifference, + HighFrequencySpectralDifferenceHWR +}; + +//======================================================================= +/** The type of window to use when calculating onset detection function samples */ +enum WindowType +{ + RectangularWindow, + HanningWindow, + HammingWindow, + BlackmanWindow, + TukeyWindow +}; + +//======================================================================= +/** A class for calculating onset detection functions. */ class OnsetDetectionFunction { public: - OnsetDetectionFunction(); // Constructor - OnsetDetectionFunction(int arg_hsize,int arg_fsize,int arg_df_type,int arg_win_type); // Constructor (with arguments) - ~OnsetDetectionFunction(); // Destructor - void initialise(int arg_hsize,int arg_fsize,int arg_df_type,int arg_win_type); // Initialisation Function + + /** Constructor that defaults the onset detection function type to ComplexSpectralDifferenceHWR + * and the window type to HanningWindow + * @param hopSize_ the hop size in audio samples + * @param frameSize_ the frame size in audio samples + */ + OnsetDetectionFunction(int hopSize_,int frameSize_); + + + /** Constructor + * @param hopSize_ the hop size in audio samples + * @param frameSize_ the frame size in audio samples + * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType) + * @param windowType the type of window to use (see WindowType) + */ + OnsetDetectionFunction(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType_); + + /** Destructor */ + ~OnsetDetectionFunction(); + + /** Initialisation function for only updating hop size and frame size (and not window type + * or onset detection function type + * @param hopSize_ the hop size in audio samples + * @param frameSize_ the frame size in audio samples + */ + void initialise(int hopSize_,int frameSize_); + + /** Initialisation Function + * @param hopSize_ the hop size in audio samples + * @param frameSize_ the frame size in audio samples + * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType) + * @param windowType the type of window to use (see WindowType) + */ + void initialise(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType_); - double getDFsample(double inputbuffer[]); // process input buffer and calculate detection function sample - void set_df_type(int arg_df_type); // set the detection function type + /** Process input frame and calculate detection function sample + * @param buffer a pointer to an array containing the audio samples to be processed + * @returns the onset detection function sample + */ + double calculateOnsetDetectionFunctionSample(double *buffer); + + /** Set the detection function type + * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType) + */ + void setOnsetDetectionFunctionType(int onsetDetectionFunctionType_); private: - void perform_FFT(); // perform the FFT on the data in 'frame' + /** Perform the FFT on the data in 'frame' */ + void performFFT(); - double energy_envelope(); // calculate energy envelope detection function sample - double energy_difference(); // calculate energy difference detection function sample - double spectral_difference(); // calculate spectral difference detection function sample - double spectral_difference_hwr(); // calculate spectral difference (half wave rectified) detection function sample - double phase_deviation(); // calculate phase deviation detection function sample - double complex_spectral_difference(); // calculate complex spectral difference detection function sample - double complex_spectral_difference_hwr(); // calculate complex spectral difference detection function sample (half-wave rectified) - double high_frequency_content(); // calculate high frequency content detection function sample - double high_frequency_spectral_difference(); // calculate high frequency spectral difference detection function sample - double high_frequency_spectral_difference_hwr(); // calculate high frequency spectral difference detection function sample (half-wave rectified) + //======================================================================= + /** Calculate energy envelope detection function sample */ + double energyEnvelope(); + + /** Calculate energy difference detection function sample */ + double energyDifference(); + + /** Calculate spectral difference detection function sample */ + double spectralDifference(); + + /** Calculate spectral difference (half wave rectified) detection function sample */ + double spectralDifferenceHWR(); + + /** Calculate phase deviation detection function sample */ + double phaseDeviation(); + + /** Calculate complex spectral difference detection function sample */ + double complexSpectralDifference(); + + /** Calculate complex spectral difference detection function sample (half-wave rectified) */ + double complexSpectralDifferenceHWR(); + + /** Calculate high frequency content detection function sample */ + double highFrequencyContent(); + + /** Calculate high frequency spectral difference detection function sample */ + double highFrequencySpectralDifference(); + + /** Calculate high frequency spectral difference detection function sample (half-wave rectified) */ + double highFrequencySpectralDifferenceHWR(); - void set_win_rectangular(); // calculate a Rectangular window - void set_win_hanning(); // calculate a Hanning window - void set_win_hamming(); // calculate a Hamming window - void set_win_blackman(); // calculate a Blackman window - void set_win_tukey(); // calculate a Tukey window + //======================================================================= + /** Calculate a Rectangular window */ + void calculateRectangularWindow(); + + /** Calculate a Hanning window */ + void calculateHanningWindow(); + + /** Calculate a Hamming window */ + void calclulateHammingWindow(); + + /** Calculate a Blackman window */ + void calculateBlackmanWindow(); + + /** Calculate a Tukey window */ + void calculateTukeyWindow(); + //======================================================================= + /** Set phase values between [-pi, pi] + * @param phaseVal the phase value to process + * @returns the wrapped phase value + */ + double princarg(double phaseVal); - double princarg(double phaseval); // set phase values between [-pi, pi] + double pi; /**< pi, the constant */ - double pi; // pi, the constant + int frameSize; /**< audio framesize */ + int hopSize; /**< audio hopsize */ + int onsetDetectionFunctionType; /**< type of detection function */ + int windowType; /**< type of window used in calculations */ - int framesize; // audio framesize - int hopsize; // audio hopsize - int df_type; // type of detection function + fftw_plan p; /**< fftw plan */ + fftw_complex *complexIn; /**< to hold complex fft values for input */ + fftw_complex *complexOut; /**< to hold complex fft values for output */ - fftw_plan p; // create fft plan - fftw_complex *in; // to hold complex fft values for input - fftw_complex *out; // to hold complex fft values for output + bool initialised; /**< flag indicating whether buffers and FFT plans are initialised */ + + std::vector<double> frame; /**< audio frame */ + std::vector<double> window; /**< window */ - int initialised; // flag indicating whether buffers and FFT plans have been initialised + double prevEnergySum; /**< to hold the previous energy sum value */ - - double *frame; // audio frame - double *window; // window - double *wframe; // windowed frame + std::vector<double> magSpec; /**< magnitude spectrum */ + std::vector<double> prevMagSpec; /**< previous magnitude spectrum */ - double energy_sum_old; // to hold the previous energy sum value - - double *mag; // magnitude spectrum - double *mag_old; // previous magnitude spectrum - - double *phase; // FFT phase values - double *phase_old; // previous phase values - double *phase_old_2; // second order previous phase values + std::vector<double> phase; /**< FFT phase values */ + std::vector<double> prevPhase; /**< previous phase values */ + std::vector<double> prevPhase2; /**< second order previous phase values */ };