]> git.sesse.net Git - kdenlive/blob - src/audioscopes/ffttools.h
9014d47e54fb8234e83a6e4f96df85ea450537f7
[kdenlive] / src / audioscopes / ffttools.h
1 /***************************************************************************
2  *   Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com)      *
3  *   This file is part of kdenlive. See www.kdenlive.org.                  *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  ***************************************************************************/
10
11 #ifndef FFTTOOLS_H
12 #define FFTTOOLS_H
13
14 #include <QVector>
15 #include <QHash>
16 #include <tools/kiss_fftr.h>
17
18 class FFTTools
19 {
20 public:
21     FFTTools();
22     ~FFTTools();
23
24     enum WindowType { Window_Rect, Window_Triangle, Window_Hamming };
25
26     /** Creates a vector containing the factors for the selected window functions.
27         The last element in the vector (at position size+1) contains the area of
28         this window function compared to the rectangular window (e.g. for a triangular
29         window the factor will be 0.5).
30         Knowing this factor is important for the Fourier Transformation as the
31         values in the frequency domain will be scaled by this factor and need to be
32         re-scaled for proper dB display.
33         The additional parameter describes:
34         * Nothing for the Rectangular window
35         * The left and right start values for the Triangular window (i.e. ranges between param and 1;
36           default is 0)
37         * Nothing for the Hamming window
38     */
39     static const QVector<float> window(const WindowType windowType, const int size, const float param = 0);
40
41     static const QString windowSignature(const WindowType windowType, const int size, const float param = 0);
42
43     /** Returns a signature for a kiss_fft configuration
44         used as a hash in the cache */
45     static const QString cfgSignature(const int size);
46
47     /** Calculates the Fourier Tranformation of the input audio frame.
48         The resulting values will be given in relative dezibel: The maximum power is 0 dB, lower powers have
49         negative dB values.
50         * audioFrame: Interleaved format with #numChannels channels
51         * freqSpectrum: Array pointer to write the data into
52         * windowSize must be divisible by 2,
53         * freqSpectrum has to be of size windowSize/2
54         For windowType and param see the FFTTools::window() function above.
55     */
56     void fftNormalized(const QVector<int16_t> audioFrame, const uint channel, const uint numChannels, float *freqSpectrum,
57                        const WindowType windowType, const uint windowSize, const float param = 0);
58
59 private:
60     QHash<QString, kiss_fftr_cfg> m_fftCfgs; // FFT cfg cache
61     QHash<QString, QVector<float> > m_windowFunctions; // Window function cache
62
63 };
64
65 #endif // FFTTOOLS_H