OSC: Changed gainVCA to gainfader as VCA is already used.
[ardour.git] / gtk2_ardour / fft_result.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3         Written by 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
21 #include "fft_result.h"
22 #include "fft_graph.h"
23 #include <cstdlib>
24 #include <cstring>
25 #include <string>
26 #include <cmath>
27
28 #include <iostream>
29
30 using namespace std;
31
32 FFTResult::FFTResult(FFTGraph *graph, Gdk::Color color, string trackname)
33 {
34         _graph = graph;
35
36         _windowSize = _graph->windowSize();
37         _dataSize   = _windowSize / 2;
38
39         _averages = 0;
40
41         _data_avg = (float *) malloc(sizeof(float) * _dataSize);
42         memset(_data_avg,0,sizeof(float) * _dataSize);
43
44         _data_min = (float *) malloc(sizeof(float) * _dataSize);
45         _data_max = (float *) malloc(sizeof(float) * _dataSize);
46
47         for (int i = 0; i < _dataSize; i++) {
48                 _data_min[i] = FLT_MAX;
49                 _data_max[i] = FLT_MIN;
50         }
51
52         _color     = color;
53         _trackname = trackname;
54 }
55
56 void
57 FFTResult::analyzeWindow(float *window)
58 {
59         float *_hanning = _graph->_hanning;
60         float *_in = _graph->_in;
61         float *_out = _graph->_out;
62
63         int i;
64         // Copy the data and apply the hanning window
65         for (i = 0; i < _windowSize; i++) {
66                 _in[i] = window[ i ] * _hanning[ i ];
67         }
68
69         fftwf_execute(_graph->_plan);
70
71         float b = _out[0] * _out[0];
72
73         _data_avg[0] += b;
74         if (b < _data_min[0]) _data_min[0] = b;
75         if (b > _data_max[0]) _data_max[0] = b;
76
77         for (i=1; i < _dataSize - 1; i++) { // TODO: check with Jesse whether this is really correct
78                 b = (_out[i] * _out[i]);
79
80                 _data_avg[i] += b;  // + (_out[_windowSize-i] * _out[_windowSize-i]);, TODO: thanks to Stefan Kost
81
82                 if (_data_min[i] > b)  _data_min[i] = b;
83                 if (_data_max[i] < b ) _data_max[i] = b;
84         }
85
86
87         _averages++;
88 }
89
90 void
91 FFTResult::finalize()
92 {
93         if (_averages == 0) {
94                 _minimum = 0.0;
95                 _maximum = 0.0;
96                 return;
97         }
98
99         // Average & scale
100         for (int i = 0; i < _dataSize; i++) {
101                 _data_avg[i] /= _averages;
102                 _data_avg[i]  = 10.0f * log10f(_data_avg[i]);
103
104                 _data_min[i]  = 10.0f * log10f(_data_min[i]);
105                 if (_data_min[i] < -10000.0f) {
106                         _data_min[i] = -10000.0f;
107                 }
108                 _data_max[i]  = 10.0f * log10f(_data_max[i]);
109         }
110
111         // find min & max
112         _minimum = _maximum = _data_avg[0];
113
114         for (int i = 1; i < _dataSize; i++) {
115                 if (_data_avg[i] < _minimum        && !isinf(_data_avg[i])) {
116                         _minimum = _data_avg[i];
117                 } else if (_data_avg[i] > _maximum && !isinf(_data_avg[i])) {
118                         _maximum = _data_avg[i];
119                 }
120         }
121
122         _averages = 0;
123 }
124
125 FFTResult::~FFTResult()
126 {
127         free(_data_avg);
128         free(_data_min);
129         free(_data_max);
130 }
131
132
133 float
134 FFTResult::avgAt(int x)
135 {
136         if (x < 0 || x>= _dataSize)
137                 return 0.0f;
138
139         return _data_avg[x];
140 }
141
142 float
143 FFTResult::minAt(int x)
144 {
145         if (x < 0 || x>= _dataSize)
146                 return 0.0f;
147
148         return _data_min[x];
149 }
150
151 float
152 FFTResult::maxAt(int x)
153 {
154         if (x < 0 || x>= _dataSize)
155                 return 0.0f;
156
157         return _data_max[x];
158 }
159