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