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