abed0c70d7417b9b0a8bff31e0db36ff543599d2
[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 #ifdef COMPILER_MSVC
20 #include <algorithm>
21 using std::min; using std::max;
22 #endif
23
24 #include <iostream>
25
26 #include <glibmm.h>
27 #include <glibmm/refptr.h>
28
29 #include <gdkmm/gc.h>
30
31 #include <gtkmm/widget.h>
32 #include <gtkmm/style.h>
33 #include <gtkmm/treemodel.h>
34 #include <gtkmm/treepath.h>
35
36 #include "pbd/stl_delete.h"
37
38 #include <math.h>
39
40 #include "fft_graph.h"
41 #include "analysis_window.h"
42 #include "public_editor.h"
43
44 #include "pbd/i18n.h"
45
46 using namespace std;
47 using namespace Gtk;
48 using namespace Gdk;
49
50 FFTGraph::FFTGraph (int windowSize)
51 {
52         _logScale = 0;
53
54         _in       = 0;
55         _out      = 0;
56         _hanning  = 0;
57         _logScale = 0;
58
59         _surface  = 0;
60         _a_window = 0;
61
62         _show_minmax       = false;
63         _show_normalized   = false;
64         _show_proportional = false;
65
66         _ann_x = _ann_y = -1;
67         _yoff = v_margin;
68         _ann_area.width = 0;
69         _ann_area.height = 0;
70
71         setWindowSize (windowSize);
72         set_events (Gdk::POINTER_MOTION_MASK | Gdk::LEAVE_NOTIFY_MASK | Gdk::BUTTON_PRESS_MASK);
73 }
74
75 void
76 FFTGraph::setWindowSize (int windowSize)
77 {
78         if (_a_window) {
79                 Glib::Threads::Mutex::Lock lm  (_a_window->track_list_lock);
80                 setWindowSize_internal (windowSize);
81         } else {
82                 setWindowSize_internal (windowSize);
83         }
84 }
85
86 void
87 FFTGraph::setWindowSize_internal (int windowSize)
88 {
89         // remove old tracklist & graphs
90         if (_a_window) {
91                 _a_window->clear_tracklist ();
92         }
93
94         _windowSize = windowSize;
95         _dataSize = windowSize / 2;
96         if (_in != 0) {
97                 fftwf_destroy_plan (_plan);
98                 free (_in);
99                 _in = 0;
100         }
101
102         if (_out != 0) {
103                 free (_out);
104                 _out = 0;
105         }
106
107         if (_hanning != 0) {
108                 free (_hanning);
109                 _hanning = 0;
110         }
111
112         if (_logScale != 0) {
113                 free (_logScale);
114                 _logScale = 0;
115         }
116
117         // When destroying, window size is set to zero to free up memory
118         if (windowSize == 0) {
119                 return;
120         }
121
122         // FFT input & output buffers
123         _in  = (float *) fftwf_malloc (sizeof (float) * _windowSize);
124         _out = (float *) fftwf_malloc (sizeof (float) * _windowSize);
125
126         // Hanning window
127         _hanning = (float *) malloc (sizeof (float) * _windowSize);
128
129         // normalize the window
130         double sum = 0.0;
131
132         for (unsigned int i = 0; i < _windowSize; ++i) {
133                 _hanning[i] = 0.5f - (0.5f * (float) cos (2.0f * M_PI * (float)i / (float)(_windowSize)));
134                 sum += _hanning[i];
135         }
136
137         double isum = 2.0 / sum;
138
139         for (unsigned int i = 0; i < _windowSize; i++) {
140                 _hanning[i] *= isum;
141         }
142
143         _logScale = (int *) malloc (sizeof (int) * _dataSize);
144
145         for (unsigned int i = 0; i < _dataSize; i++) {
146                 _logScale[i] = 0;
147         }
148         _plan = fftwf_plan_r2r_1d (_windowSize, _in, _out, FFTW_R2HC, FFTW_MEASURE);
149 }
150
151 FFTGraph::~FFTGraph ()
152 {
153         // This will free everything
154         setWindowSize (0);
155
156         if (_surface) {
157                 cairo_surface_destroy (_surface);
158         }
159 }
160
161 bool
162 FFTGraph::on_expose_event (GdkEventExpose* event)
163 {
164         cairo_t* cr = gdk_cairo_create (GDK_DRAWABLE (get_window ()->gobj ()));
165         cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
166         cairo_clip (cr);
167
168         cairo_set_source_surface(cr, _surface, 0, 0);
169         cairo_paint (cr);
170
171
172         if (_ann_x > 0 && _ann_y > 0) {
173                 const float x = _ann_x - hl_margin;
174                 const float freq = expf(_fft_log_base * x / currentScaleWidth) * _fft_start;
175
176                 std::stringstream ss;
177                 if (freq >= 10000) {
178                         ss <<  std::setprecision (1) << std::fixed << freq / 1000 << " kHz";
179                 } else if (freq >= 1000) {
180                         ss <<  std::setprecision (2) << std::fixed << freq / 1000 << " kHz";
181                 } else {
182                         ss <<  std::setprecision (0) << std::fixed << freq << " Hz";
183                 }
184                 layout->set_text (ss.str ());
185                 int lw, lh;
186                 layout->get_pixel_size (lw, lh);
187                 lw|=1; lh|=1;
188
189                 const float y0 = _ann_y - lh - 7;
190
191                 _ann_area.x = _ann_x - 1 - lw * .5;
192                 _ann_area.y =  y0 - 1;
193                 _ann_area.width = lw + 3;
194                 _ann_area.height = lh + 8;
195
196                 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.7);
197                 cairo_rectangle (cr, _ann_x - 1 - lw * .5, y0 - 1, lw + 2, lh + 2);
198                 cairo_fill (cr);
199
200                 cairo_move_to (cr, _ann_x , _ann_y - 0.5);
201                 cairo_rel_line_to (cr, -3.0, -5.5);
202                 cairo_rel_line_to (cr, 6, 0);
203                 cairo_close_path (cr);
204                 cairo_fill (cr);
205
206                 cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0);
207                 cairo_move_to (cr, _ann_x - lw / 2, y0);
208                 pango_cairo_update_layout (cr, layout->gobj ());
209                 pango_cairo_show_layout (cr, layout->gobj ());
210
211         }
212
213 #ifdef HARLEQUIN_DEBUGGING
214         cairo_rectangle (cr, 0, 0, width, height);
215         cairo_set_source_rgba (cr, (random() % 255) / 255.f, (random() % 255) / 255.f, 0.0, 0.5);
216         cairo_fill (cr);
217 #endif
218
219         cairo_destroy (cr);
220         return true;
221 }
222
223 bool
224 FFTGraph::on_motion_notify_event (GdkEventMotion* ev)
225 {
226         gint x, y;
227
228         x = (int) floor (ev->x);
229         y = (int) floor (ev->y);
230
231         if (x <= hl_margin + 1 || x >= width  - hr_margin) {
232                 x = -1;
233         }
234         if (y <= _yoff || y >= height - v_margin - 1) {
235                 y = -1;
236         }
237
238         if (x == _ann_x && y == _ann_y) {
239                 return true;
240         }
241         _ann_x = x;
242         _ann_y = y;
243
244         if (_ann_area.width == 0 || _ann_area.height == 0) {
245                 queue_draw ();
246         } else {
247                 queue_draw_area (_ann_area.x, _ann_area.y, _ann_area.width, _ann_area.height + 1);
248         }
249
250         if (_ann_x > 0 &&_ann_y > 0) {
251                 queue_draw_area (_ann_x - _ann_area.width, _ann_y - _ann_area.height - 1, _ann_area.width * 2, _ann_area.height + 2);
252         }
253
254         return true;
255 }
256
257 bool
258 FFTGraph::on_leave_notify_event (GdkEventCrossing *)
259 {
260         if (_ann_x == -1 && _ann_y == -1) {
261                 return true;
262         }
263         _ann_x = _ann_y = -1;
264         if (_ann_area.width == 0 || _ann_area.height == 0) {
265                 queue_draw ();
266         } else {
267                 queue_draw_area (_ann_area.x, _ann_area.y, _ann_area.width, _ann_area.height + 1);
268         }
269         _ann_area.width = _ann_area.height = 0;
270         return false;
271 }
272
273 FFTResult *
274 FFTGraph::prepareResult (Gdk::Color color, string trackname)
275 {
276         FFTResult *res = new FFTResult (this, color, trackname);
277
278         return res;
279 }
280
281 void
282 FFTGraph::set_analysis_window (AnalysisWindow *a_window)
283 {
284         _a_window = a_window;
285 }
286
287 int
288 FFTGraph::draw_scales (cairo_t* cr)
289 {
290         int label_height = v_margin;
291
292         cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0);
293         cairo_rectangle (cr, 0, 0, width, height);
294         cairo_fill (cr);
295
296         /*
297          *  1          5
298          *  _          _
299          *   |        |
300          * 2 |        | 4
301          *   |________|
302          *        3
303          */
304
305         cairo_set_line_width (cr, 1.0);
306         cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
307         cairo_move_to (cr, 3                      , .5 + v_margin);
308         cairo_line_to (cr, .5 + hl_margin         , .5 + v_margin);  // 1
309         cairo_line_to (cr, .5 + hl_margin         , .5 + height - v_margin); // 2
310         cairo_line_to (cr, 1.5 + width - hr_margin, .5 + height - v_margin); // 3
311         cairo_line_to (cr, 1.5 + width - hr_margin, .5 + v_margin); // 4
312         cairo_line_to (cr, width - 3              , .5 + v_margin); // 5
313         cairo_stroke (cr);
314
315         if (layout == 0) {
316                 layout = create_pango_layout ("");
317                 layout->set_font_description (get_style ()->get_font ());
318         }
319
320         // Draw x-axis scale 1/3 octaves centered around 1K
321         int overlap = 0;
322
323         // make sure 1K (x=0) is visible
324         for (int x = 0; x < 27; ++x) {
325                 float freq = powf (2.f, x / 3.0) * 1000.f;
326                 if (freq <= _fft_start) { continue; }
327                 if (freq >= _fft_end) { break; }
328
329                 const float pos = currentScaleWidth * logf (freq / _fft_start) / _fft_log_base;
330                 const int coord = floor (hl_margin + pos);
331
332                 if (coord < overlap) {
333                         continue;
334                 }
335
336                 std::stringstream ss;
337                 if (freq >= 10000) {
338                         ss <<  std::setprecision (1) << std::fixed << freq / 1000 << "k";
339                 } else if (freq >= 1000) {
340                         ss <<  std::setprecision (2) << std::fixed << freq / 1000 << "k";
341                 } else {
342                         ss <<  std::setprecision (0) << std::fixed << freq << "Hz";
343                 }
344                 layout->set_text (ss.str ());
345                 int lw, lh;
346                 layout->get_pixel_size (lw, lh);
347                 overlap = coord + lw + 3;
348
349                 if (coord + lw / 2 > width - hr_margin - 2) {
350                         break;
351                 }
352                 if (v_margin / 2 + lh > label_height) {
353                         label_height = v_margin / 2 + lh;
354                 }
355
356                 cairo_set_source_rgba (cr, 0.2, 0.2, 0.2, 1.0);
357                 cairo_move_to (cr, coord, v_margin);
358                 cairo_line_to (cr, coord, height - v_margin - 1);
359                 cairo_stroke (cr);
360
361                 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
362                 cairo_move_to (cr, coord - lw / 2, v_margin / 2);
363                 pango_cairo_update_layout (cr, layout->gobj ());
364                 pango_cairo_show_layout (cr, layout->gobj ());
365         }
366
367         // now from 1K down to 4Hz
368         for (int x = 0; x > -24; --x) {
369                 float freq = powf (2.f, x / 3.0) * 1000.f;
370                 if (freq >= _fft_end) { continue; }
371                 if (freq <= _fft_start) { break; }
372
373                 const float pos = currentScaleWidth * logf (freq / _fft_start) / _fft_log_base;
374                 const int coord = floor (hl_margin + pos);
375
376                 if (x != 0 && coord > overlap) {
377                         continue;
378                 }
379
380                 std::stringstream ss;
381                 if (freq >= 10000) {
382                         ss <<  std::setprecision (1) << std::fixed << freq / 1000 << "k";
383                 } else if (freq >= 1000) {
384                         ss <<  std::setprecision (2) << std::fixed << freq / 1000 << "k";
385                 } else {
386                         ss <<  std::setprecision (0) << std::fixed << freq << "Hz";
387                 }
388                 layout->set_text (ss.str ());
389                 int lw, lh;
390                 layout->get_pixel_size (lw, lh);
391
392                 overlap = coord - lw - 3;
393
394                 if (coord - lw / 2 < hl_margin + 2) {
395                         break;
396                 }
397                 if (x == 0) {
398                         // just get overlap position
399                         continue;
400                 }
401                 if (v_margin / 2 + lh > label_height) {
402                         label_height = v_margin / 2 + lh;
403                 }
404
405
406                 cairo_set_source_rgba (cr, 0.2, 0.2, 0.2, 1.0);
407                 cairo_move_to (cr, coord, v_margin);
408                 cairo_line_to (cr, coord, height - v_margin - 1);
409                 cairo_stroke (cr);
410
411                 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
412                 cairo_move_to (cr, coord - lw / 2, v_margin / 2);
413                 pango_cairo_update_layout (cr, layout->gobj ());
414                 pango_cairo_show_layout (cr, layout->gobj ());
415         }
416
417         return label_height;
418 }
419
420 void
421 FFTGraph::redraw ()
422 {
423         Glib::Threads::Mutex::Lock lm  (_a_window->track_list_lock);
424
425         assert (_surface);
426         cairo_t* cr = cairo_create (_surface);
427
428         _yoff = draw_scales (cr);
429
430         if (_a_window == 0) {
431                 cairo_destroy (cr);
432                 queue_draw ();
433                 return;
434         }
435
436         if (!_a_window->track_list_ready) {
437                 cairo_destroy (cr);
438                 queue_draw ();
439                 return;
440         }
441
442         float minf;
443         float maxf;
444
445         TreeNodeChildren track_rows = _a_window->track_list.get_model ()->children ();
446
447         if (!_show_normalized) {
448                 maxf =    0.0f;
449                 minf = -108.0f;
450         } else  {
451                 minf =  999.0f;
452                 maxf = -999.0f;
453                 for (TreeIter i = track_rows.begin (); i != track_rows.end (); i++) {
454                         TreeModel::Row row = *i;
455                         FFTResult *res = row[_a_window->tlcols.graph];
456
457                         // disregard fft analysis from empty signals
458                         if (res->minimum (_show_proportional) == res->maximum (_show_proportional)) {
459                                 continue;
460                         }
461                         // don't include invisible graphs
462                         if (!row[_a_window->tlcols.visible]) {
463                                 continue;
464                         }
465
466                         minf = std::min (minf, res->minimum (_show_proportional));
467                         maxf = std::max (maxf, res->maximum (_show_proportional));
468                 }
469         }
470
471         // clamp range, > -200dBFS, at least 24dB (two y-axis labels) range
472         minf = std::max (-200.f, minf);
473         if (maxf <= minf) {
474                 cairo_destroy (cr);
475                 queue_draw ();
476                 return;
477         }
478
479         if (maxf - minf < 24) {
480                 maxf += 6.f;
481                 minf = maxf - 24.f;
482         }
483
484         cairo_set_line_width (cr, 1.5);
485         cairo_translate (cr, hl_margin + 1, _yoff);
486
487         float fft_pane_size_w = width  - hl_margin - hr_margin;
488         float fft_pane_size_h = height - v_margin - 1 - _yoff;
489         double pixels_per_db = (double)fft_pane_size_h / (double)(maxf - minf);
490
491         // draw y-axis dB
492         cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
493
494         int btm_lbl = fft_pane_size_h;
495         {
496                 // y-axis legend
497                 layout->set_text (_("dBFS"));
498                 int lw, lh;
499                 layout->get_pixel_size (lw, lh);
500                 cairo_move_to (cr, -2 - lw, fft_pane_size_h - lh / 2);
501                 pango_cairo_update_layout (cr, layout->gobj ());
502                 pango_cairo_show_layout (cr, layout->gobj ());
503                 btm_lbl = fft_pane_size_h - lh;
504         }
505
506         for (int x = -6; x >= -200; x -= 12) {
507                 float yp = 1.5 + fft_pane_size_h - rint ((x - minf) * pixels_per_db);
508
509                 assert (layout);
510                 std::stringstream ss;
511                 ss << x;
512                 layout->set_text (ss.str ());
513                 int lw, lh;
514                 layout->get_pixel_size (lw, lh);
515
516                 if (yp + 2 + lh / 2 > btm_lbl) {
517                         continue;
518                 }
519                 if (yp < 2 + lh / 2) {
520                         continue;
521                 }
522
523                 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
524                 cairo_move_to (cr, -2 - lw, yp - lh / 2);
525                 pango_cairo_update_layout (cr, layout->gobj ());
526                 pango_cairo_show_layout (cr, layout->gobj ());
527
528                 cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
529                 cairo_move_to (cr, 0, yp);
530                 cairo_line_to (cr, fft_pane_size_w, yp);
531                 cairo_stroke (cr);
532         }
533
534         cairo_rectangle (cr, 1, 1, fft_pane_size_w, fft_pane_size_h);
535         cairo_clip (cr);
536
537         cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
538         cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
539
540         for (TreeIter i = track_rows.begin (); i != track_rows.end (); i++) {
541                 TreeModel::Row row = *i;
542
543                 // don't show graphs for tracks which are deselected
544                 if (!row[_a_window->tlcols.visible]) {
545                         continue;
546                 }
547
548                 FFTResult *res = row[_a_window->tlcols.graph];
549
550                 // don't show graphs for empty signals
551                 if (res->minimum (_show_proportional) == res->maximum (_show_proportional)) {
552                         continue;
553                 }
554
555                 float mpp;
556                 float X,Y;
557
558                 if (_show_minmax) {
559
560                         X = 0.5f + _logScale[0];
561                         Y = 1.5f + fft_pane_size_h - pixels_per_db * (res->maxAt (0, _show_proportional) - minf);
562                         cairo_move_to (cr, X, Y);
563
564                         // Draw the line of maximum values
565                         mpp = minf;
566                         for (unsigned int x = 1; x < res->length () - 1; ++x) {
567                                 mpp = std::max (mpp, res->maxAt (x, _show_proportional));
568
569                                 if (_logScale[x] == _logScale[x + 1]) {
570                                         continue;
571                                 }
572
573                                 mpp = fmin (mpp, maxf);
574                                 X = 0.5f + _logScale[x];
575                                 Y = 1.5f + fft_pane_size_h - pixels_per_db * (mpp - minf);
576                                 cairo_line_to (cr, X, Y);
577                                 mpp = minf;
578                         }
579
580                         mpp = maxf;
581                         // Draw back to the start using the minimum value
582                         for (int x = res->length () - 1; x >= 0; --x) {
583                                 mpp = std::min (mpp, res->minAt (x, _show_proportional));
584
585                                 if (_logScale[x] == _logScale[x + 1]) {
586                                         continue;
587                                 }
588
589                                 mpp = fmax (mpp, minf);
590                                 X = 0.5f + _logScale[x];
591                                 Y = 1.5f + fft_pane_size_h - pixels_per_db * (mpp - minf);
592                                 cairo_line_to (cr, X, Y);
593                                 mpp = maxf;
594                         }
595
596                         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);
597                         cairo_close_path (cr);
598                         cairo_fill (cr);
599                 }
600
601                 // draw max of averages
602                 X = 0.5f + _logScale[0];
603                 Y = 1.5f + fft_pane_size_h - pixels_per_db * (res->avgAt (0, _show_proportional) - minf);
604                 cairo_move_to (cr, X, Y);
605
606                 mpp = minf;
607                 for (unsigned int x = 0; x < res->length () - 1; x++) {
608                         mpp = std::max (mpp, res->avgAt (x, _show_proportional));
609
610                         if (_logScale[x] == _logScale[x + 1]) {
611                                 continue;
612                         }
613
614                         mpp = fmax (mpp, minf);
615                         mpp = fmin (mpp, maxf);
616
617                         X = 0.5f + _logScale[x];
618                         Y = 1.5f + fft_pane_size_h - pixels_per_db * (mpp - minf);
619                         cairo_line_to (cr, X, Y);
620                         mpp = minf;
621                 }
622
623                 cairo_set_source_rgb (cr, res->get_color ().get_red_p (), res->get_color ().get_green_p (), res->get_color ().get_blue_p ());
624                 cairo_stroke (cr);
625         }
626         cairo_destroy (cr);
627         queue_draw ();
628 }
629
630 void
631 FFTGraph::on_size_request (Gtk::Requisition* requisition)
632 {
633         width  = max (requisition->width,  minScaleWidth  + hl_margin + hr_margin);
634         height = max (requisition->height, minScaleHeight + 2 + v_margin * 2);
635
636         requisition->width  = width;;
637         requisition->height = height;
638 }
639
640 void
641 FFTGraph::on_size_allocate (Gtk::Allocation & alloc)
642 {
643         width = alloc.get_width ();
644         height = alloc.get_height ();
645
646         update_size ();
647
648         DrawingArea::on_size_allocate (alloc);
649 }
650
651 void
652 FFTGraph::update_size ()
653 {
654         framecnt_t SR = PublicEditor::instance ().session ()->nominal_frame_rate ();
655         _fft_start = SR / (double)_dataSize;
656         _fft_end = .5 * SR;
657         _fft_log_base = logf (.5 * _dataSize);
658         currentScaleWidth  = width - hl_margin - hr_margin;
659         _logScale[0] = 0;
660         for (unsigned int i = 1; i < _dataSize; ++i) {
661                 _logScale[i] = floor (currentScaleWidth * logf (.5 * i) / _fft_log_base);
662         }
663         if (_surface) {
664                 cairo_surface_destroy (_surface);
665         }
666         _surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
667         redraw ();
668 }