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@27: #include
adamstark@5:
adamstark@20: //=======================================================================
adamstark@23: /** The type of onset detection function to calculate */
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@23: /** The type of window to use when calculating onset detection function samples */
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@24: //=======================================================================
adamstark@24: /** A class for calculating onset detection functions. */
adamstark@5: class OnsetDetectionFunction
adamstark@5: {
adamstark@5: public:
adamstark@15:
adamstark@23: /** Constructor
adamstark@23: * @param hopSize_ the hop size in audio samples
adamstark@23: * @param frameSize_ the frame size in audio samples
adamstark@23: * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType)
adamstark@23: * @param windowType the type of window to use (see WindowType)
adamstark@23: */
adamstark@22: OnsetDetectionFunction(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType);
adamstark@15:
adamstark@15: /** Destructor */
adamstark@15: ~OnsetDetectionFunction();
adamstark@15:
adamstark@23: /** Initialisation Function
adamstark@23: * @param hopSize_ the hop size in audio samples
adamstark@23: * @param frameSize_ the frame size in audio samples
adamstark@23: * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType)
adamstark@23: * @param windowType the type of window to use (see WindowType)
adamstark@23: */
adamstark@22: void initialise(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType);
adamstark@5:
adamstark@23: /** Process input frame and calculate detection function sample
adamstark@23: * @param buffer a pointer to an array containing the audio samples to be processed
adamstark@23: * @returns the onset detection function sample
adamstark@23: */
adamstark@22: double calculateOnsetDetectionFunctionSample(double *buffer);
adamstark@15:
adamstark@23: /** Set the detection function type
adamstark@23: * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType)
adamstark@23: */
adamstark@22: void setOnsetDetectionFunctionType(int onsetDetectionFunctionType_);
adamstark@5:
adamstark@5: private:
adamstark@5:
adamstark@23: /** Perform the FFT on the data in 'frame' */
adamstark@22: void performFFT();
adamstark@5:
adamstark@22: //=======================================================================
adamstark@23: /** Calculate energy envelope detection function sample */
adamstark@22: double energyEnvelope();
adamstark@15:
adamstark@23: /** Calculate energy difference detection function sample */
adamstark@22: double energyDifference();
adamstark@15:
adamstark@23: /** Calculate spectral difference detection function sample */
adamstark@22: double spectralDifference();
adamstark@15:
adamstark@23: /** Calculate spectral difference (half wave rectified) detection function sample */
adamstark@22: double spectralDifferenceHWR();
adamstark@15:
adamstark@23: /** Calculate phase deviation detection function sample */
adamstark@22: double phaseDeviation();
adamstark@15:
adamstark@23: /** Calculate complex spectral difference detection function sample */
adamstark@22: double complexSpectralDifference();
adamstark@15:
adamstark@23: /** Calculate complex spectral difference detection function sample (half-wave rectified) */
adamstark@22: double complexSpectralDifferenceHWR();
adamstark@15:
adamstark@23: /** Calculate high frequency content detection function sample */
adamstark@22: double highFrequencyContent();
adamstark@15:
adamstark@23: /** Calculate high frequency spectral difference detection function sample */
adamstark@22: double highFrequencySpectralDifference();
adamstark@15:
adamstark@23: /** Calculate high frequency spectral difference detection function sample (half-wave rectified) */
adamstark@22: double highFrequencySpectralDifferenceHWR();
adamstark@5:
adamstark@22: //=======================================================================
adamstark@23: /** Calculate a Rectangular window */
adamstark@22: void calculateRectangularWindow();
adamstark@15:
adamstark@23: /** Calculate a Hanning window */
adamstark@22: void calculateHanningWindow();
adamstark@15:
adamstark@23: /** Calculate a Hamming window */
adamstark@22: void calclulateHammingWindow();
adamstark@15:
adamstark@23: /** Calculate a Blackman window */
adamstark@22: void calculateBlackmanWindow();
adamstark@15:
adamstark@23: /** Calculate a Tukey window */
adamstark@22: void calculateTukeyWindow();
adamstark@5:
adamstark@22: //=======================================================================
adamstark@23: /** Set phase values between [-pi, pi]
adamstark@23: * @param phaseVal the phase value to process
adamstark@23: * @returns the wrapped phase value
adamstark@23: */
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@27: bool initialised; /**< flag indicating whether buffers and FFT plans are initialised */
adamstark@15:
adamstark@27: std::vector frame; /**< audio frame */
adamstark@27: std::vector window; /**< window */
adamstark@5:
adamstark@22: double prevEnergySum; /**< to hold the previous energy sum value */
adamstark@5:
adamstark@27: std::vector magSpec; /**< magnitude spectrum */
adamstark@27: std::vector prevMagSpec; /**< previous magnitude spectrum */
adamstark@5:
adamstark@27: std::vector phase; /**< FFT phase values */
adamstark@27: std::vector prevPhase; /**< previous phase values */
adamstark@27: std::vector prevPhase2; /**< second order previous phase values */
adamstark@5:
adamstark@5: };
adamstark@5:
adamstark@5:
adamstark@5: #endif