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@93: #ifdef USE_FFTW
adamstark@38: #include "fftw3.h"
adamstark@93: #endif
adamstark@93:
adamstark@93: #ifdef USE_KISS_FFT
adamstark@93: #include "kiss_fft.h"
adamstark@93: #endif
adamstark@93:
adamstark@64: #include
adamstark@38:
adamstark@57: //=======================================================================
adamstark@60: /** The type of onset detection function to calculate */
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@60: /** The type of window to use when calculating onset detection function samples */
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@61: //=======================================================================
adamstark@61: /** A class for calculating onset detection functions. */
adamstark@38: class OnsetDetectionFunction
adamstark@38: {
adamstark@38: public:
adamstark@52:
adamstark@66: /** Constructor that defaults the onset detection function type to ComplexSpectralDifferenceHWR
adamstark@66: * and the window type to HanningWindow
adamstark@66: * @param hopSize_ the hop size in audio samples
adamstark@66: * @param frameSize_ the frame size in audio samples
adamstark@66: */
adamstark@111: OnsetDetectionFunction (int hopSize, int frameSize);
adamstark@66:
adamstark@66:
adamstark@60: /** Constructor
adamstark@60: * @param hopSize_ the hop size in audio samples
adamstark@60: * @param frameSize_ the frame size in audio samples
adamstark@60: * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType)
adamstark@60: * @param windowType the type of window to use (see WindowType)
adamstark@60: */
adamstark@111: OnsetDetectionFunction (int hopSize, int frameSize, int onsetDetectionFunctionType, int windowType);
adamstark@52:
adamstark@52: /** Destructor */
adamstark@52: ~OnsetDetectionFunction();
adamstark@52:
adamstark@66: /** Initialisation function for only updating hop size and frame size (and not window type
adamstark@66: * or onset detection function type
adamstark@66: * @param hopSize_ the hop size in audio samples
adamstark@66: * @param frameSize_ the frame size in audio samples
adamstark@66: */
adamstark@111: void initialise (int hopSize, int frameSize);
adamstark@66:
adamstark@60: /** Initialisation Function
adamstark@60: * @param hopSize_ the hop size in audio samples
adamstark@60: * @param frameSize_ the frame size in audio samples
adamstark@60: * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType)
adamstark@60: * @param windowType the type of window to use (see WindowType)
adamstark@60: */
adamstark@111: void initialise (int hopSize, int frameSize, int onsetDetectionFunctionType, int windowType);
adamstark@38:
adamstark@60: /** Process input frame and calculate detection function sample
adamstark@60: * @param buffer a pointer to an array containing the audio samples to be processed
adamstark@60: * @returns the onset detection function sample
adamstark@60: */
adamstark@92: double calculateOnsetDetectionFunctionSample (double* buffer);
adamstark@52:
adamstark@60: /** Set the detection function type
adamstark@60: * @param onsetDetectionFunctionType_ the type of onset detection function to use - (see OnsetDetectionFunctionType)
adamstark@60: */
adamstark@111: void setOnsetDetectionFunctionType (int onsetDetectionFunctionType);
adamstark@38:
adamstark@38: private:
adamstark@38:
adamstark@60: /** Perform the FFT on the data in 'frame' */
adamstark@59: void performFFT();
adamstark@38:
adamstark@59: //=======================================================================
adamstark@60: /** Calculate energy envelope detection function sample */
adamstark@59: double energyEnvelope();
adamstark@52:
adamstark@60: /** Calculate energy difference detection function sample */
adamstark@59: double energyDifference();
adamstark@52:
adamstark@60: /** Calculate spectral difference detection function sample */
adamstark@59: double spectralDifference();
adamstark@52:
adamstark@60: /** Calculate spectral difference (half wave rectified) detection function sample */
adamstark@59: double spectralDifferenceHWR();
adamstark@52:
adamstark@60: /** Calculate phase deviation detection function sample */
adamstark@59: double phaseDeviation();
adamstark@52:
adamstark@60: /** Calculate complex spectral difference detection function sample */
adamstark@59: double complexSpectralDifference();
adamstark@52:
adamstark@60: /** Calculate complex spectral difference detection function sample (half-wave rectified) */
adamstark@59: double complexSpectralDifferenceHWR();
adamstark@52:
adamstark@60: /** Calculate high frequency content detection function sample */
adamstark@59: double highFrequencyContent();
adamstark@52:
adamstark@60: /** Calculate high frequency spectral difference detection function sample */
adamstark@59: double highFrequencySpectralDifference();
adamstark@52:
adamstark@60: /** Calculate high frequency spectral difference detection function sample (half-wave rectified) */
adamstark@59: double highFrequencySpectralDifferenceHWR();
adamstark@38:
adamstark@59: //=======================================================================
adamstark@60: /** Calculate a Rectangular window */
adamstark@59: void calculateRectangularWindow();
adamstark@52:
adamstark@60: /** Calculate a Hanning window */
adamstark@59: void calculateHanningWindow();
adamstark@52:
adamstark@60: /** Calculate a Hamming window */
adamstark@59: void calclulateHammingWindow();
adamstark@52:
adamstark@60: /** Calculate a Blackman window */
adamstark@59: void calculateBlackmanWindow();
adamstark@52:
adamstark@60: /** Calculate a Tukey window */
adamstark@59: void calculateTukeyWindow();
adamstark@38:
adamstark@59: //=======================================================================
adamstark@60: /** Set phase values between [-pi, pi]
adamstark@60: * @param phaseVal the phase value to process
adamstark@60: * @returns the wrapped phase value
adamstark@60: */
adamstark@59: double princarg(double phaseVal);
adamstark@38:
adamstark@93: void initialiseFFT();
adamstark@93: void freeFFT();
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@66: int windowType; /**< type of window used in calculations */
adamstark@93:
adamstark@93: //=======================================================================
adamstark@93: #ifdef USE_FFTW
adamstark@59: fftw_plan p; /**< fftw plan */
adamstark@92: fftw_complex* complexIn; /**< to hold complex fft values for input */
adamstark@92: fftw_complex* complexOut; /**< to hold complex fft values for output */
adamstark@93: #endif
adamstark@93:
adamstark@93: #ifdef USE_KISS_FFT
adamstark@93: kiss_fft_cfg cfg; /**< Kiss FFT configuration */
adamstark@93: kiss_fft_cpx* fftIn; /**< FFT input samples, in complex form */
adamstark@93: kiss_fft_cpx* fftOut; /**< FFT output samples, in complex form */
adamstark@93: std::vector > complexOut;
adamstark@93: #endif
adamstark@38:
adamstark@93: //=======================================================================
adamstark@64: bool initialised; /**< flag indicating whether buffers and FFT plans are initialised */
adamstark@52:
adamstark@64: std::vector frame; /**< audio frame */
adamstark@64: std::vector window; /**< window */
adamstark@38:
adamstark@59: double prevEnergySum; /**< to hold the previous energy sum value */
adamstark@38:
adamstark@64: std::vector magSpec; /**< magnitude spectrum */
adamstark@64: std::vector prevMagSpec; /**< previous magnitude spectrum */
adamstark@38:
adamstark@64: std::vector phase; /**< FFT phase values */
adamstark@64: std::vector prevPhase; /**< previous phase values */
adamstark@64: std::vector prevPhase2; /**< second order previous phase values */
adamstark@38: };
adamstark@38:
adamstark@38:
adamstark@111: #endif