e7a0fd75b6ffadb8904307c04f72f99e32c18b4b
[ardour.git] / gtk2_ardour / fft_graph.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <iostream>
21
22 #include <glibmm.h>
23 #include <glibmm/refptr.h>
24
25 #include <gdkmm/gc.h>
26
27 #include <gtkmm/widget.h>
28 #include <gtkmm/style.h>
29 #include <gtkmm/treemodel.h>
30 #include <gtkmm/treepath.h>
31
32 #include <pbd/stl_delete.h>
33
34 #include <math.h>
35
36 #include "fft_graph.h"
37 #include "analysis_window.h"
38
39 using namespace std;
40 using namespace Gtk;
41 using namespace Gdk;
42
43 FFTGraph::FFTGraph(int windowSize)
44 {
45         _logScale = 0;
46         
47         _in       = 0;
48         _out      = 0;
49         _hanning  = 0;
50         _logScale = 0;
51
52         _a_window = 0;
53
54         setWindowSize(windowSize);
55 }
56
57 void
58 FFTGraph::setWindowSize(int windowSize)
59 {
60         if (_a_window) {
61                 Glib::Mutex::Lock lm  (_a_window->track_list_lock);
62                 setWindowSize_internal(windowSize);
63         } else {
64                 setWindowSize_internal(windowSize);
65         }
66 }
67
68 void
69 FFTGraph::setWindowSize_internal(int windowSize)
70 {
71         // remove old tracklist & graphs
72         if (_a_window) {
73                 _a_window->clear_tracklist();
74         }
75         
76         _windowSize = windowSize;
77         _dataSize = windowSize / 2;
78         if (_in != 0) {
79                 fftwf_destroy_plan(_plan);
80                 free(_in);
81                 _in = 0;
82         }
83         
84         if (_out != 0) {
85                 free(_out);
86                 _out = 0;
87         }
88         
89         if (_hanning != 0) {
90                 free(_hanning);
91                 _hanning = 0;
92         }
93
94         if (_logScale != 0) {
95                 free(_logScale);
96                 _logScale = 0;
97         }
98
99         // When destroying, window size is set to zero to free up memory
100         if (windowSize == 0)
101                 return;
102
103         // FFT input & output buffers
104         _in      = (float *) fftwf_malloc(sizeof(float) * _windowSize);
105         _out     = (float *) fftwf_malloc(sizeof(float) * _windowSize);
106
107         // Hanning window
108         _hanning = (float *) malloc(sizeof(float) * _windowSize);
109
110
111         // normalize the window
112         double sum = 0.0;
113         
114         for (int i=0; i < _windowSize; i++) {
115                 _hanning[i]=0.81f * ( 0.5f - (0.5f * (float) cos(2.0f * M_PI * (float)i / (float)(_windowSize))));
116                 sum += _hanning[i];
117         }
118
119         double isum = 1.0 / sum;
120         
121         for (int i=0; i < _windowSize; i++) {
122                 _hanning[i] *= isum;
123         }
124         
125         _logScale = (int *) malloc(sizeof(int) * _dataSize);
126         //float count = 0;
127         for (int i = 0; i < _dataSize; i++) {
128                 _logScale[i] = 0;
129         }
130         _plan = fftwf_plan_r2r_1d(_windowSize, _in, _out, FFTW_R2HC, FFTW_ESTIMATE);
131 }
132
133 FFTGraph::~FFTGraph()
134 {
135         // This will free everything
136         setWindowSize(0);
137 }
138
139 bool
140 FFTGraph::on_expose_event (GdkEventExpose* event)
141 {
142         redraw();
143         return true;
144 }
145
146 FFTResult *
147 FFTGraph::prepareResult(Gdk::Color color, string trackname)
148 {
149         FFTResult *res = new FFTResult(this, color, trackname);
150
151         return res;
152 }
153
154 void
155 FFTGraph::analyze(float *window, float *composite)
156 {       
157         int i;
158         // Copy the data and apply the hanning window
159         for (i = 0; i < _windowSize; i++) {
160                 _in[i] = window[ i ] * _hanning[ i ];
161         }
162
163         fftwf_execute(_plan);
164
165         composite[0] += (_out[0] * _out[0]);
166         
167         for (i=1; i < _dataSize - 1; i++) { // TODO: check with Jesse whether this is really correct
168                 composite[i] += (_out[i] * _out[i]) + (_out[_windowSize-i] * _out[_windowSize-i]);
169         }
170 }
171
172 void
173 FFTGraph::set_analysis_window(AnalysisWindow *a_window)
174 {
175         _a_window = a_window;
176 }
177
178 void
179 FFTGraph::draw_scales(Glib::RefPtr<Gdk::Window> window)
180 {
181         
182         Glib::RefPtr<Gtk::Style> style = get_style();
183         Glib::RefPtr<Gdk::GC> black = style->get_black_gc();
184         Glib::RefPtr<Gdk::GC> white = style->get_white_gc();
185         
186         window->draw_rectangle(black, true, 0, 0, width, height);
187         
188         /**
189          *  4          5
190          *  _          _
191          *   |        |
192          * 1 |        | 2
193          *   |________|
194          *        3
195          **/
196
197         // Line 1
198         window->draw_line(white, h_margin, v_margin, h_margin, height - v_margin );
199
200         // Line 2
201         window->draw_line(white, width - h_margin, v_margin, width - h_margin, height - v_margin );
202
203         // Line 3
204         window->draw_line(white, h_margin, height - v_margin, width - h_margin, height - v_margin );
205
206 #define DB_METRIC_LENGTH 8
207         // Line 5
208         window->draw_line(white, h_margin - DB_METRIC_LENGTH, v_margin, h_margin, v_margin );
209         
210         // Line 6
211         window->draw_line(white, width - h_margin, v_margin, width - h_margin + DB_METRIC_LENGTH, v_margin );
212
213
214         if (graph_gc == 0) {
215                 graph_gc = GC::create( get_window() );
216         }
217
218         Color grey;
219
220         grey.set_rgb_p(0.2, 0.2, 0.2);
221         
222         graph_gc->set_rgb_fg_color( grey );
223
224         if (layout == 0) {
225                 layout = create_pango_layout ("");
226                 layout->set_font_description (get_style()->get_font());
227         }
228
229         // Draw logscale
230         int logscale_pos = 0;
231         int position_on_scale;
232         for (int x = 1; x < 8; x++) {
233                 position_on_scale = (int)floor( (double)scaleWidth*(double)x/8.0);
234
235                 while (_logScale[logscale_pos] < position_on_scale)
236                         logscale_pos++;
237                 
238                 int coord = (int)(v_margin + 1.0 + position_on_scale);
239                 
240                 int SR = 44100;
241
242                 int rate_at_pos = (int)((double)(SR/2) * (double)logscale_pos / (double)_dataSize);
243                 
244                 char buf[32];
245                 if (rate_at_pos < 1000)
246                         snprintf(buf,32,"%dHz",rate_at_pos);
247                 else
248                         snprintf(buf,32,"%dk",(int)floor( (float)rate_at_pos/(float)1000) );
249
250                 std::string label = buf;
251                 
252                 layout->set_text(label);
253                 
254                 window->draw_line(graph_gc, coord, v_margin, coord, height - v_margin);
255
256                 int width, height;
257                 layout->get_pixel_size (width, height);
258                 
259                 window->draw_layout(white, coord - width / 2, v_margin / 2, layout);
260                 
261         }
262
263 }
264
265 void
266 FFTGraph::redraw()
267 {       
268         Glib::Mutex::Lock lm  (_a_window->track_list_lock);
269
270         draw_scales(get_window());
271         
272         if (_a_window == 0)
273                 return;
274
275         if (!_a_window->track_list_ready)
276                 return;
277         
278         
279         // Find "session wide" min & max
280         float min =  1000000000000.0;
281         float max = -1000000000000.0;
282         
283         TreeNodeChildren track_rows = _a_window->track_list.get_model()->children();
284         
285         for (TreeIter i = track_rows.begin(); i != track_rows.end(); i++) {
286                 
287                 TreeModel::Row row = *i;
288                 FFTResult *res = row[_a_window->tlcols.graph];
289
290                 // disregard fft analysis from empty signals
291                 if (res->minimum() == res->maximum()) {
292                         continue;
293                 }
294                 
295                 if ( res->minimum() < min) {
296                         min = res->minimum();
297                 }
298
299                 if ( res->maximum() > max) {
300                         max = res->maximum();
301                 }
302         }
303         
304         int graph_height = height - 2 * h_margin;
305
306         if (graph_gc == 0) {
307                 graph_gc = GC::create( get_window() );
308         }
309         
310         
311         double pixels_per_db = (double)graph_height / (double)(max - min);
312         
313         
314         for (TreeIter i = track_rows.begin(); i != track_rows.end(); i++) {
315                 
316                 TreeModel::Row row = *i;
317
318                 // don't show graphs for tracks which are deselected
319                 if (!row[_a_window->tlcols.visible]) {
320                         continue;
321                 }
322                 
323                 FFTResult *res = row[_a_window->tlcols.graph];
324
325                 // don't show graphs for empty signals
326                 if (res->minimum() == res->maximum()) {
327                         continue;
328                 }
329                 
330                 std::string name = row[_a_window->tlcols.trackname];
331
332                 // Set color from track
333                 graph_gc->set_rgb_fg_color( res->get_color() );
334
335                 float mpp = -1000000.0;
336                 int prevx = 0;
337                 float prevSample = min;
338                 
339                 for (int x = 0; x < res->length() - 1; x++) {
340                         
341                         if (res->sampleAt(x) > mpp)
342                                 mpp = res->sampleAt(x);
343                         
344                         // If the next point on the log scale is at the same location,
345                         // don't draw yet
346                         if (x + 1 < res->length() && 
347                                 _logScale[x] == _logScale[x + 1]) {
348                                 continue;
349                         }
350
351                         get_window()->draw_line(
352                                         graph_gc,
353                                         v_margin + 1 + prevx,
354                                         graph_height - (int)floor( (prevSample - min) * pixels_per_db) + h_margin - 1,
355                                         v_margin + 1 + _logScale[x],
356                                         graph_height - (int)floor( (mpp        - min) * pixels_per_db) + h_margin - 1);
357                         
358                         prevx = _logScale[x];
359                         prevSample = mpp;
360                         
361
362                         mpp = -1000000.0;
363                         
364                 }
365         }
366
367 }
368
369 void
370 FFTGraph::on_size_request(Gtk::Requisition* requisition)
371 {
372         width  = scaleWidth  + h_margin * 2;
373         height = scaleHeight + 2 + v_margin * 2;
374
375         if (_logScale != 0) {
376                 free(_logScale);
377         }
378         _logScale = (int *) malloc(sizeof(int) * _dataSize);
379
380         float SR = 44100;
381         float FFT_START = SR/(double)_dataSize;
382         float FFT_END = SR/2.0;
383         float FFT_RANGE = log( FFT_END / FFT_START);
384         float pixel = 0;
385         for (int i = 0; i < _dataSize; i++) {
386                 float freq_at_bin = (SR/2.0) * ((double)i / (double)_dataSize);
387                 float freq_at_pixel = FFT_START * exp( FFT_RANGE * pixel / (double)scaleWidth );
388                 while (freq_at_bin > freq_at_pixel) {
389                         pixel++;
390                         freq_at_pixel = FFT_START * exp( FFT_RANGE * pixel / (double)scaleWidth );
391                 }
392                 _logScale[i] = (int)floor(pixel);
393 //printf("logscale at %d = %3.3f, freq_at_pixel %3.3f, freq_at_bin %3.3f, scaleWidth %d\n", i, pixel, freq_at_pixel, freq_at_bin, scaleWidth);
394         }
395
396         requisition->width  = width;;
397         requisition->height = height;
398 }
399
400 void
401 FFTGraph::on_size_allocate(Gtk::Allocation & alloc)
402 {
403         width = alloc.get_width();
404         height = alloc.get_height();
405         
406         DrawingArea::on_size_allocate (alloc);
407
408 }
409