convert from Glib:: to Glib::Threads for all thread-related API
[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         _show_minmax     = false;
55         _show_normalized = false;
56
57         setWindowSize(windowSize);
58 }
59
60 void
61 FFTGraph::setWindowSize(int windowSize)
62 {
63         if (_a_window) {
64                 Glib::Threads::Mutex::Lock lm  (_a_window->track_list_lock);
65                 setWindowSize_internal(windowSize);
66         } else {
67                 setWindowSize_internal(windowSize);
68         }
69 }
70
71 void
72 FFTGraph::setWindowSize_internal(int windowSize)
73 {
74         // remove old tracklist & graphs
75         if (_a_window) {
76                 _a_window->clear_tracklist();
77         }
78
79         _windowSize = windowSize;
80         _dataSize = windowSize / 2;
81         if (_in != 0) {
82                 fftwf_destroy_plan(_plan);
83                 free(_in);
84                 _in = 0;
85         }
86
87         if (_out != 0) {
88                 free(_out);
89                 _out = 0;
90         }
91
92         if (_hanning != 0) {
93                 free(_hanning);
94                 _hanning = 0;
95         }
96
97         if (_logScale != 0) {
98                 free(_logScale);
99                 _logScale = 0;
100         }
101
102         // When destroying, window size is set to zero to free up memory
103         if (windowSize == 0)
104                 return;
105
106         // FFT input & output buffers
107         _in      = (float *) fftwf_malloc(sizeof(float) * _windowSize);
108         _out     = (float *) fftwf_malloc(sizeof(float) * _windowSize);
109
110         // Hanning window
111         _hanning = (float *) malloc(sizeof(float) * _windowSize);
112
113
114         // normalize the window
115         double sum = 0.0;
116
117         for (int i=0; i < _windowSize; i++) {
118                 _hanning[i]=0.81f * ( 0.5f - (0.5f * (float) cos(2.0f * M_PI * (float)i / (float)(_windowSize))));
119                 sum += _hanning[i];
120         }
121
122         double isum = 1.0 / sum;
123
124         for (int i=0; i < _windowSize; i++) {
125                 _hanning[i] *= isum;
126         }
127
128         _logScale = (int *) malloc(sizeof(int) * _dataSize);
129         //float count = 0;
130         for (int i = 0; i < _dataSize; i++) {
131                 _logScale[i] = 0;
132         }
133         _plan = fftwf_plan_r2r_1d(_windowSize, _in, _out, FFTW_R2HC, FFTW_ESTIMATE);
134 }
135
136 FFTGraph::~FFTGraph()
137 {
138         // This will free everything
139         setWindowSize(0);
140 }
141
142 bool
143 FFTGraph::on_expose_event (GdkEventExpose* /*event*/)
144 {
145         redraw();
146         return true;
147 }
148
149 FFTResult *
150 FFTGraph::prepareResult(Gdk::Color color, string trackname)
151 {
152         FFTResult *res = new FFTResult(this, color, trackname);
153
154         return res;
155 }
156
157
158 void
159 FFTGraph::set_analysis_window(AnalysisWindow *a_window)
160 {
161         _a_window = a_window;
162 }
163
164 void
165 FFTGraph::draw_scales(Glib::RefPtr<Gdk::Window> window)
166 {
167
168         Glib::RefPtr<Gtk::Style> style = get_style();
169         Glib::RefPtr<Gdk::GC> black = style->get_black_gc();
170         Glib::RefPtr<Gdk::GC> white = style->get_white_gc();
171
172         window->draw_rectangle(black, true, 0, 0, width, height);
173
174         /**
175          *  4          5
176          *  _          _
177          *   |        |
178          * 1 |        | 2
179          *   |________|
180          *        3
181          **/
182
183         // Line 1
184         window->draw_line(white, h_margin, v_margin, h_margin, height - v_margin );
185
186         // Line 2
187         window->draw_line(white, width - h_margin + 1, v_margin, width - h_margin + 1, height - v_margin );
188
189         // Line 3
190         window->draw_line(white, h_margin, height - v_margin, width - h_margin, height - v_margin );
191
192 #define DB_METRIC_LENGTH 8
193         // Line 4
194         window->draw_line(white, h_margin - DB_METRIC_LENGTH, v_margin, h_margin, v_margin );
195
196         // Line 5
197         window->draw_line(white, width - h_margin + 1, v_margin, width - h_margin + DB_METRIC_LENGTH, v_margin );
198
199
200
201         if (graph_gc == 0) {
202                 graph_gc = GC::create( get_window() );
203         }
204
205         Color grey;
206
207         grey.set_rgb_p(0.2, 0.2, 0.2);
208
209         graph_gc->set_rgb_fg_color( grey );
210
211         if (layout == 0) {
212                 layout = create_pango_layout ("");
213                 layout->set_font_description (get_style()->get_font());
214         }
215
216         // Draw logscale
217         int logscale_pos = 0;
218         int position_on_scale;
219
220
221 /* TODO, write better scales and change the log function so that octaves are of equal pixel length
222         float scale_points[10] = { 55.0, 110.0, 220.0, 440.0, 880.0, 1760.0, 3520.0, 7040.0, 14080.0, 28160.0 };
223
224         for (int x = 0; x < 10; x++) {
225
226                 // i = 0.. _dataSize-1
227                 float freq_at_bin = (SR/2.0) * ((double)i / (double)_dataSize);
228
229
230
231                         freq_at_pixel = FFT_START * exp( FFT_RANGE * pixel / (double)(currentScaleWidth - 1) );
232         }
233         */
234
235         for (int x = 1; x < 8; x++) {
236                 position_on_scale = (int)floor( (double)currentScaleWidth*(double)x/8.0);
237
238                 while (_logScale[logscale_pos] < position_on_scale)
239                         logscale_pos++;
240
241                 int coord = (int)(v_margin + 1.0 + position_on_scale);
242
243                 int SR = 44100;
244
245                 int rate_at_pos = (int)((double)(SR/2) * (double)logscale_pos / (double)_dataSize);
246
247                 char buf[32];
248                 if (rate_at_pos < 1000)
249                         snprintf(buf,32,"%dHz",rate_at_pos);
250                 else
251                         snprintf(buf,32,"%dk",(int)floor( (float)rate_at_pos/(float)1000) );
252
253                 std::string label = buf;
254
255                 layout->set_text(label);
256
257                 window->draw_line(graph_gc, coord, v_margin, coord, height - v_margin - 1);
258
259                 int width, height;
260                 layout->get_pixel_size (width, height);
261
262                 window->draw_layout(white, coord - width / 2, v_margin / 2, layout);
263
264         }
265
266 }
267
268 void
269 FFTGraph::redraw()
270 {
271         Glib::Threads::Mutex::Lock lm  (_a_window->track_list_lock);
272
273         draw_scales(get_window());
274
275
276         if (_a_window == 0)
277                 return;
278
279         if (!_a_window->track_list_ready)
280                 return;
281
282         cairo_t *cr;
283         cr = gdk_cairo_create(GDK_DRAWABLE(get_window()->gobj()));
284         cairo_set_line_width(cr, 1.5);
285         cairo_translate(cr, (float)v_margin + 1.0, (float)h_margin);
286
287
288
289         // Find "session wide" min & max
290         float min =  1000000000000.0;
291         float max = -1000000000000.0;
292
293         TreeNodeChildren track_rows = _a_window->track_list.get_model()->children();
294
295         for (TreeIter i = track_rows.begin(); i != track_rows.end(); i++) {
296
297                 TreeModel::Row row = *i;
298                 FFTResult *res = row[_a_window->tlcols.graph];
299
300                 // disregard fft analysis from empty signals
301                 if (res->minimum() == res->maximum()) {
302                         continue;
303                 }
304
305                 if ( res->minimum() < min) {
306                         min = res->minimum();
307                 }
308
309                 if ( res->maximum() > max) {
310                         max = res->maximum();
311                 }
312         }
313
314         if (!_show_normalized) {
315                 min = -150.0f;
316                 max = 0.0f;
317         }
318
319         //int graph_height = height - 2 * h_margin;
320
321
322
323         float fft_pane_size_w = (float)(width  - 2*v_margin) - 1.0;
324         float fft_pane_size_h = (float)(height - 2*h_margin);
325
326         double pixels_per_db = (double)fft_pane_size_h / (double)(max - min);
327
328         cairo_rectangle(cr, 0.0, 0.0, fft_pane_size_w, fft_pane_size_h);
329         cairo_clip(cr);
330
331         for (TreeIter i = track_rows.begin(); i != track_rows.end(); i++) {
332
333                 TreeModel::Row row = *i;
334
335                 // don't show graphs for tracks which are deselected
336                 if (!row[_a_window->tlcols.visible]) {
337                         continue;
338                 }
339
340                 FFTResult *res = row[_a_window->tlcols.graph];
341
342                 // don't show graphs for empty signals
343                 if (res->minimum() == res->maximum()) {
344                         continue;
345                 }
346
347                 float mpp;
348
349                 if (_show_minmax) {
350                         mpp = -1000000.0;
351
352                         cairo_set_source_rgba(cr, res->get_color().get_red_p(), res->get_color().get_green_p(), res->get_color().get_blue_p(), 0.30);
353                         cairo_move_to(cr, 0.5f + (float)_logScale[0], 0.5f + (float)( fft_pane_size_h - (int)floor( (res->maxAt(0) - min) * pixels_per_db) ));
354
355                         // Draw the line of maximum values
356                         for (int x = 1; x < res->length(); x++) {
357                                 if (res->maxAt(x) > mpp)
358                                         mpp = res->maxAt(x);
359                                 mpp = fmax(mpp, min);
360                                 mpp = fmin(mpp, max);
361
362                                 // If the next point on the log scale is at the same location,
363                                 // don't draw yet
364                                 if (x + 1 < res->length() && _logScale[x] == _logScale[x + 1]) {
365                                         continue;
366                                 }
367
368                                 float X = 0.5f + (float)_logScale[x];
369                                 float Y = 0.5f + (float)( fft_pane_size_h - (int)floor( (mpp - min) * pixels_per_db) );
370
371                                 cairo_line_to(cr, X, Y);
372
373                                 mpp = -1000000.0;
374                         }
375
376                         mpp = +10000000.0;
377                         // Draw back to the start using the minimum value
378                         for (int x = res->length()-1; x >= 0; x--) {
379                                 if (res->minAt(x) < mpp)
380                                         mpp = res->minAt(x);
381                                 mpp = fmax(mpp, min);
382                                 mpp = fmin(mpp, max);
383
384                                 // If the next point on the log scale is at the same location,
385                                 // don't draw yet
386                                 if (x - 1 > 0 && _logScale[x] == _logScale[x - 1]) {
387                                         continue;
388                                 }
389
390                                 float X = 0.5f + (float)_logScale[x];
391                                 float Y = 0.5f + (float)( fft_pane_size_h - (int)floor( (mpp - min) * pixels_per_db) );
392
393                                 cairo_line_to(cr, X, Y );
394
395                                 mpp = +10000000.0;
396                         }
397
398                         cairo_close_path(cr);
399
400                         cairo_fill(cr);
401                 }
402
403
404
405                 // Set color from track
406                 cairo_set_source_rgb(cr, res->get_color().get_red_p(), res->get_color().get_green_p(), res->get_color().get_blue_p());
407
408                 mpp = -1000000.0;
409
410                 cairo_move_to(cr, 0.5, fft_pane_size_h-0.5);
411
412                 for (int x = 0; x < res->length(); x++) {
413
414
415                         if (res->avgAt(x) > mpp)
416                                 mpp = res->avgAt(x);
417                         mpp = fmax(mpp, min);
418                         mpp = fmin(mpp, max);
419
420                         // If the next point on the log scale is at the same location,
421                         // don't draw yet
422                         if (x + 1 < res->length() && _logScale[x] == _logScale[x + 1]) {
423                                 continue;
424                         }
425
426                         cairo_line_to(cr, 0.5f + (float)_logScale[x], 0.5f + (float)( fft_pane_size_h - (int)floor( (mpp - min) * pixels_per_db) ));
427
428                         mpp = -1000000.0;
429                 }
430
431                 cairo_stroke(cr);
432         }
433
434         cairo_destroy(cr);
435 }
436
437 void
438 FFTGraph::on_size_request(Gtk::Requisition* requisition)
439 {
440         width  = max(requisition->width,  minScaleWidth  + h_margin * 2);
441         height = max(requisition->height, minScaleHeight + 2 + v_margin * 2);
442
443         update_size();
444
445         requisition->width  = width;;
446         requisition->height = height;
447 }
448
449 void
450 FFTGraph::on_size_allocate(Gtk::Allocation & alloc)
451 {
452         width = alloc.get_width();
453         height = alloc.get_height();
454
455         update_size();
456
457         DrawingArea::on_size_allocate (alloc);
458 }
459
460 void
461 FFTGraph::update_size()
462 {
463         currentScaleWidth  = width - h_margin*2;
464         currentScaleHeight = height - 2 - v_margin*2;
465
466         float SR = 44100;
467         float FFT_START = SR/(double)_dataSize;
468         float FFT_END = SR/2.0;
469         float FFT_RANGE = log( FFT_END / FFT_START);
470         float pixel = 0;
471         for (int i = 0; i < _dataSize; i++) {
472                 float freq_at_bin = (SR/2.0) * ((double)i / (double)_dataSize);
473                 float freq_at_pixel;
474                 pixel--;
475                 do {
476                         pixel++;
477                         freq_at_pixel = FFT_START * exp( FFT_RANGE * pixel / (double)(currentScaleWidth - 1) );
478                 } while (freq_at_bin > freq_at_pixel);
479
480                 _logScale[i] = (int)floor(pixel);
481         }
482 }
483