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>
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.
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.
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.
24 using std::min; using std::max;
30 #include <glibmm/refptr.h>
34 #include <gtkmm/widget.h>
35 #include <gtkmm/style.h>
36 #include <gtkmm/treemodel.h>
37 #include <gtkmm/treepath.h>
39 #include "pbd/stl_delete.h"
43 #include "fft_graph.h"
44 #include "analysis_window.h"
45 #include "public_editor.h"
53 FFTGraph::FFTGraph (int windowSize)
66 _show_normalized = false;
67 _show_proportional = false;
74 setWindowSize (windowSize);
75 set_events (Gdk::POINTER_MOTION_MASK | Gdk::LEAVE_NOTIFY_MASK | Gdk::BUTTON_PRESS_MASK);
79 FFTGraph::setWindowSize (int windowSize)
82 Glib::Threads::Mutex::Lock lm (_a_window->track_list_lock);
83 setWindowSize_internal (windowSize);
85 setWindowSize_internal (windowSize);
90 FFTGraph::setWindowSize_internal (int windowSize)
92 // remove old tracklist & graphs
94 _a_window->clear_tracklist ();
97 _windowSize = windowSize;
98 _dataSize = windowSize / 2;
100 fftwf_destroy_plan (_plan);
115 if (_logScale != 0) {
120 // When destroying, window size is set to zero to free up memory
121 if (windowSize == 0) {
125 // FFT input & output buffers
126 _in = (float *) fftwf_malloc (sizeof (float) * _windowSize);
127 _out = (float *) fftwf_malloc (sizeof (float) * _windowSize);
130 _hanning = (float *) malloc (sizeof (float) * _windowSize);
132 // normalize the window
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)));
140 double isum = 2.0 / sum;
142 for (unsigned int i = 0; i < _windowSize; i++) {
146 _logScale = (int *) malloc (sizeof (int) * _dataSize);
148 for (unsigned int i = 0; i < _dataSize; i++) {
151 _plan = fftwf_plan_r2r_1d (_windowSize, _in, _out, FFTW_R2HC, FFTW_MEASURE);
154 FFTGraph::~FFTGraph ()
156 // This will free everything
160 cairo_surface_destroy (_surface);
165 FFTGraph::on_expose_event (GdkEventExpose* event)
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);
171 cairo_set_source_surface(cr, _surface, 0, 0);
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;
179 std::stringstream ss;
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";
185 ss << std::setprecision (0) << std::fixed << freq << " Hz";
187 layout->set_text (ss.str ());
189 layout->get_pixel_size (lw, lh);
192 const float y0 = _ann_y - lh - 7;
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;
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);
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);
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 ());
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);
227 FFTGraph::on_motion_notify_event (GdkEventMotion* ev)
231 x = (int) floor (ev->x);
232 y = (int) floor (ev->y);
234 if (x <= hl_margin + 1 || x >= width - hr_margin) {
237 if (y <= _yoff || y >= height - v_margin - 1) {
241 if (x == _ann_x && y == _ann_y) {
247 if (_ann_area.width == 0 || _ann_area.height == 0) {
250 queue_draw_area (_ann_area.x, _ann_area.y, _ann_area.width, _ann_area.height + 1);
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);
261 FFTGraph::on_leave_notify_event (GdkEventCrossing *)
263 if (_ann_x == -1 && _ann_y == -1) {
266 _ann_x = _ann_y = -1;
267 if (_ann_area.width == 0 || _ann_area.height == 0) {
270 queue_draw_area (_ann_area.x, _ann_area.y, _ann_area.width, _ann_area.height + 1);
272 _ann_area.width = _ann_area.height = 0;
277 FFTGraph::prepareResult (Gdk::Color color, string trackname)
279 FFTResult *res = new FFTResult (this, color, trackname);
285 FFTGraph::set_analysis_window (AnalysisWindow *a_window)
287 _a_window = a_window;
291 FFTGraph::draw_scales (cairo_t* cr)
293 int label_height = v_margin;
295 cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0);
296 cairo_rectangle (cr, 0, 0, width, height);
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
319 layout = create_pango_layout ("");
320 layout->set_font_description (get_style ()->get_font ());
323 // Draw x-axis scale 1/3 octaves centered around 1K
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; }
332 const float pos = currentScaleWidth * logf (freq / _fft_start) / _fft_log_base;
333 const int coord = floor (hl_margin + pos);
335 if (coord < overlap) {
339 std::stringstream ss;
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";
345 ss << std::setprecision (0) << std::fixed << freq << "Hz";
347 layout->set_text (ss.str ());
349 layout->get_pixel_size (lw, lh);
350 overlap = coord + lw + 3;
352 if (coord + lw / 2 > width - hr_margin - 2) {
355 if (v_margin / 2 + lh > label_height) {
356 label_height = v_margin / 2 + lh;
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);
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 ());
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; }
376 const float pos = currentScaleWidth * logf (freq / _fft_start) / _fft_log_base;
377 const int coord = floor (hl_margin + pos);
379 if (x != 0 && coord > overlap) {
383 std::stringstream ss;
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";
389 ss << std::setprecision (0) << std::fixed << freq << "Hz";
391 layout->set_text (ss.str ());
393 layout->get_pixel_size (lw, lh);
395 overlap = coord - lw - 3;
397 if (coord - lw / 2 < hl_margin + 2) {
401 // just get overlap position
404 if (v_margin / 2 + lh > label_height) {
405 label_height = v_margin / 2 + lh;
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);
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 ());
427 cairo_t* cr = cairo_create (_surface);
429 _yoff = draw_scales (cr);
431 if (_a_window == 0) {
437 Glib::Threads::Mutex::Lock lm (_a_window->track_list_lock);
439 if (!_a_window->track_list_ready) {
448 TreeNodeChildren track_rows = _a_window->track_list.get_model ()->children ();
450 if (!_show_normalized) {
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];
460 // disregard fft analysis from empty signals
461 if (res->minimum (_show_proportional) == res->maximum (_show_proportional)) {
464 // don't include invisible graphs
465 if (!row[_a_window->tlcols.visible]) {
469 minf = std::min (minf, res->minimum (_show_proportional));
470 maxf = std::max (maxf, res->maximum (_show_proportional));
474 // clamp range, > -200dBFS, at least 24dB (two y-axis labels) range
475 minf = std::max (-200.f, minf);
482 if (maxf - minf < 24) {
487 cairo_set_line_width (cr, 1.5);
488 cairo_translate (cr, hl_margin + 1, _yoff);
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);
495 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
497 int btm_lbl = fft_pane_size_h;
500 layout->set_text (_("dBFS"));
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;
509 for (int x = -6; x >= -200; x -= 12) {
510 float yp = 1.5 + fft_pane_size_h - rint ((x - minf) * pixels_per_db);
513 std::stringstream ss;
515 layout->set_text (ss.str ());
517 layout->get_pixel_size (lw, lh);
519 if (yp + 2 + lh / 2 > btm_lbl) {
522 if (yp < 2 + lh / 2) {
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 ());
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);
537 cairo_rectangle (cr, 1, 1, fft_pane_size_w, fft_pane_size_h);
540 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
541 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
543 for (TreeIter i = track_rows.begin (); i != track_rows.end (); i++) {
544 TreeModel::Row row = *i;
546 // don't show graphs for tracks which are deselected
547 if (!row[_a_window->tlcols.visible]) {
551 FFTResult *res = row[_a_window->tlcols.graph];
553 // don't show graphs for empty signals
554 if (res->minimum (_show_proportional) == res->maximum (_show_proportional)) {
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);
567 // Draw the line of maximum values
569 for (unsigned int x = 1; x < res->length () - 1; ++x) {
570 mpp = std::max (mpp, res->maxAt (x, _show_proportional));
572 if (_logScale[x] == _logScale[x + 1]) {
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);
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));
588 if (_logScale[x] == _logScale[x + 1]) {
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);
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);
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);
610 for (unsigned int x = 0; x < res->length () - 1; x++) {
611 mpp = std::max (mpp, res->avgAt (x, _show_proportional));
613 if (_logScale[x] == _logScale[x + 1]) {
617 mpp = fmax (mpp, minf);
618 mpp = fmin (mpp, maxf);
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);
626 cairo_set_source_rgb (cr, res->get_color ().get_red_p (), res->get_color ().get_green_p (), res->get_color ().get_blue_p ());
634 FFTGraph::on_size_request (Gtk::Requisition* requisition)
636 width = max (requisition->width, minScaleWidth + hl_margin + hr_margin);
637 height = max (requisition->height, minScaleHeight + 2 + v_margin * 2);
639 requisition->width = width;;
640 requisition->height = height;
644 FFTGraph::on_size_allocate (Gtk::Allocation & alloc)
646 width = alloc.get_width ();
647 height = alloc.get_height ();
651 DrawingArea::on_size_allocate (alloc);
655 FFTGraph::update_size ()
657 samplecnt_t SR = PublicEditor::instance ().session ()->nominal_sample_rate ();
658 _fft_start = SR / (double)_dataSize;
660 _fft_log_base = logf (.5 * _dataSize);
661 currentScaleWidth = width - hl_margin - hr_margin;
663 for (unsigned int i = 1; i < _dataSize; ++i) {
664 _logScale[i] = floor (currentScaleWidth * logf (.5 * i) / _fft_log_base);
667 cairo_surface_destroy (_surface);
669 _surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);