adamstark@38: //======================================================================= adamstark@38: /** @file OnsetDetectionFunction.h adamstark@38: * @brief A class for calculating onset detection functions adamstark@38: * @author Adam Stark adamstark@38: * @copyright Copyright (C) 2008-2014 Queen Mary University of London adamstark@38: * adamstark@38: * This program is free software: you can redistribute it and/or modify adamstark@38: * it under the terms of the GNU General Public License as published by adamstark@38: * the Free Software Foundation, either version 3 of the License, or adamstark@38: * (at your option) any later version. adamstark@38: * adamstark@38: * This program is distributed in the hope that it will be useful, adamstark@38: * but WITHOUT ANY WARRANTY; without even the implied warranty of adamstark@38: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the adamstark@38: * GNU General Public License for more details. adamstark@38: * adamstark@38: * You should have received a copy of the GNU General Public License adamstark@38: * along with this program. If not, see . adamstark@38: */ adamstark@38: //======================================================================= adamstark@38: adamstark@59: #ifndef __ONSETDETECTIONFUNCTION_H adamstark@59: #define __ONSETDETECTIONFUNCTION_H adamstark@38: adamstark@38: #include "fftw3.h" adamstark@38: adamstark@57: //======================================================================= adamstark@57: enum OnsetDetectionFunctionType adamstark@57: { adamstark@57: EnergyEnvelope, adamstark@57: EnergyDifference, adamstark@57: SpectralDifference, adamstark@57: SpectralDifferenceHWR, adamstark@57: PhaseDeviation, adamstark@57: ComplexSpectralDifference, adamstark@57: ComplexSpectralDifferenceHWR, adamstark@57: HighFrequencyContent, adamstark@57: HighFrequencySpectralDifference, adamstark@57: HighFrequencySpectralDifferenceHWR adamstark@57: }; adamstark@57: adamstark@57: //======================================================================= adamstark@57: enum WindowType adamstark@57: { adamstark@57: RectangularWindow, adamstark@57: HanningWindow, adamstark@57: HammingWindow, adamstark@57: BlackmanWindow, adamstark@57: TukeyWindow adamstark@57: }; adamstark@57: adamstark@38: class OnsetDetectionFunction adamstark@38: { adamstark@38: public: adamstark@52: adamstark@52: /** Constructor */ adamstark@59: OnsetDetectionFunction(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType); adamstark@52: adamstark@52: /** Destructor */ adamstark@52: ~OnsetDetectionFunction(); adamstark@52: adamstark@52: /** Initialisation Function */ adamstark@59: void initialise(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType); adamstark@38: adamstark@59: /** process input frame and calculate detection function sample */ adamstark@59: double calculateOnsetDetectionFunctionSample(double *buffer); adamstark@52: adamstark@52: /** set the detection function type */ adamstark@59: void setOnsetDetectionFunctionType(int onsetDetectionFunctionType_); adamstark@38: adamstark@38: private: adamstark@38: adamstark@52: /** perform the FFT on the data in 'frame' */ adamstark@59: void performFFT(); adamstark@38: adamstark@59: //======================================================================= adamstark@52: /** calculate energy envelope detection function sample */ adamstark@59: double energyEnvelope(); adamstark@52: adamstark@52: /** calculate energy difference detection function sample */ adamstark@59: double energyDifference(); adamstark@52: adamstark@52: /** calculate spectral difference detection function sample */ adamstark@59: double spectralDifference(); adamstark@52: adamstark@52: /** calculate spectral difference (half wave rectified) detection function sample */ adamstark@59: double spectralDifferenceHWR(); adamstark@52: adamstark@52: /** calculate phase deviation detection function sample */ adamstark@59: double phaseDeviation(); adamstark@52: adamstark@52: /** calculate complex spectral difference detection function sample */ adamstark@59: double complexSpectralDifference(); adamstark@52: adamstark@52: /** calculate complex spectral difference detection function sample (half-wave rectified) */ adamstark@59: double complexSpectralDifferenceHWR(); adamstark@52: adamstark@52: /** calculate high frequency content detection function sample */ adamstark@59: double highFrequencyContent(); adamstark@52: adamstark@52: /** calculate high frequency spectral difference detection function sample */ adamstark@59: double highFrequencySpectralDifference(); adamstark@52: adamstark@52: /** calculate high frequency spectral difference detection function sample (half-wave rectified) */ adamstark@59: double highFrequencySpectralDifferenceHWR(); adamstark@38: adamstark@59: //======================================================================= adamstark@52: /** calculate a Rectangular window */ adamstark@59: void calculateRectangularWindow(); adamstark@52: adamstark@52: /** calculate a Hanning window */ adamstark@59: void calculateHanningWindow(); adamstark@52: adamstark@52: /** calculate a Hamming window */ adamstark@59: void calclulateHammingWindow(); adamstark@52: adamstark@52: /** calculate a Blackman window */ adamstark@59: void calculateBlackmanWindow(); adamstark@52: adamstark@52: /** calculate a Tukey window */ adamstark@59: void calculateTukeyWindow(); adamstark@38: adamstark@59: //======================================================================= adamstark@52: /** set phase values between [-pi, pi] */ adamstark@59: double princarg(double phaseVal); adamstark@38: adamstark@38: adamstark@52: double pi; /**< pi, the constant */ adamstark@38: adamstark@59: int frameSize; /**< audio framesize */ adamstark@59: int hopSize; /**< audio hopsize */ adamstark@59: int onsetDetectionFunctionType; /**< type of detection function */ adamstark@38: adamstark@59: fftw_plan p; /**< fftw plan */ adamstark@59: fftw_complex *complexIn; /**< to hold complex fft values for input */ adamstark@59: fftw_complex *complexOut; /**< to hold complex fft values for output */ adamstark@38: adamstark@52: int initialised; /**< flag indicating whether buffers and FFT plans are initialised */ adamstark@52: adamstark@52: double *frame; /**< audio frame */ adamstark@52: double *window; /**< window */ adamstark@38: adamstark@59: double prevEnergySum; /**< to hold the previous energy sum value */ adamstark@38: adamstark@59: double *magSpec; /**< magnitude spectrum */ adamstark@59: double *prevMagSpec; /**< previous magnitude spectrum */ adamstark@38: adamstark@52: double *phase; /**< FFT phase values */ adamstark@59: double *prevPhase; /**< previous phase values */ adamstark@59: double *prevPhase2; /**< second order previous phase values */ adamstark@38: adamstark@38: }; adamstark@38: adamstark@38: adamstark@38: #endif