]> git.sesse.net Git - kdenlive/blob - src/audioscopes/ffttools.h
Reorganize and cleanup build structure
[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 "kiss_fft/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
60     /** This is linear interpolation with the special property that it preserves peaks, which is required
61         for e.g. showing correct Decibel values (where the peak values are of interest because of clipping which
62         may occur for too strong frequencies; The lower values are smeared by the window function anyway).
63         Consider f = {0, 100, 0}
64                  x = {0.5,  1.5}: With default linear interpolation x0 and x1 would both be mapped to 50.
65         This function maps x1 (the first position after the peak) to 100.
66
67         @param in           The source vector containing the data
68         @param targetSize   Number of interpolation nodes between ...
69         @param left         the left array index in the in-vector and ...
70         @param right        the right array index (both inclusive).
71         @param fill         If right lies outside of the array bounds (which is perfectly fine here) then this value
72                             will be used for filling the missing information.
73         */
74     static const QVector<float> interpolatePeakPreserving(const QVector<float> in, const uint targetSize, uint left = 0, uint right = 0, float fill = 0.0);
75
76 private:
77     QHash<QString, kiss_fftr_cfg> m_fftCfgs; // FFT cfg cache
78     QHash<QString, QVector<float> > m_windowFunctions; // Window function cache
79
80 };
81
82 #endif // FFTTOOLS_H