Make EQ Gui optional and seize updating the graph when the analysis is not visible...
[ardour.git] / gtk2_ardour / fft.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sampo Savolainen
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     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20 #include "fft.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25
26 FFT::FFT(uint32_t windowSize)
27         : _window_size(windowSize),
28           _data_size(_window_size/2),
29         _iterations(0)
30 {
31         _fftInput  = (float *) fftwf_malloc(sizeof(float) * _window_size);
32
33         _fftOutput = (float *) fftwf_malloc(sizeof(float) * _window_size);
34
35         _power_at_bin  = (float *) malloc(sizeof(float) * _data_size);
36         _phase_at_bin  = (float *) malloc(sizeof(float) * _data_size);
37
38         _plan = fftwf_plan_r2r_1d(_window_size, _fftInput, _fftOutput, FFTW_R2HC, FFTW_ESTIMATE);
39
40         reset();
41 }
42
43 void
44 FFT::reset()
45 {
46         memset(_power_at_bin, 0, sizeof(float) * _data_size);
47         memset(_phase_at_bin, 0, sizeof(float) * _data_size);
48         
49         _iterations = 0;
50 }
51
52 void
53 FFT::analyze(ARDOUR::Sample *input)
54 {
55         _iterations++;
56
57         memcpy(_fftInput, input, sizeof(float) * _window_size);
58
59         fftwf_execute(_plan);
60
61         _power_at_bin[0] += _fftOutput[0] * _fftOutput[0];
62         _phase_at_bin[0] += 0.0;
63
64         float power;
65         float phase;
66
67 #define Re (_fftOutput[i])
68 #define Im (_fftOutput[_window_size-i])
69         for (uint32_t i=1; i < _data_size - 1; i++) { 
70
71                 power = (Re * Re) + (Im * Im);
72                 phase = atanf(Im / Re);
73         
74                 if (Re < 0.0 && Im > 0.0) {
75                         phase += M_PI;
76                 } else if (Re < 0.0 && Im < 0.0) {
77                         phase -= M_PI;
78                 }
79
80                 _power_at_bin[i] += power;
81                 _phase_at_bin[i] += phase;
82         }
83 #undef Re
84 #undef Im
85 }
86
87 void
88 FFT::calculate()
89 {
90         if (_iterations > 1) {
91                 for (uint32_t i=0; i < _data_size - 1; i++) { 
92                         _power_at_bin[i] /= (float)_iterations;
93                         _phase_at_bin[i] /= (float)_iterations;
94                 }
95                 _iterations = 1;
96         }
97 }
98
99
100 FFT::~FFT()
101 {
102         fftwf_destroy_plan(_plan);
103         free(_power_at_bin);
104         free(_phase_at_bin);
105         free(_fftOutput);
106         free(_fftInput);
107 }