2 Copyright (C) 2006 Paul Davis
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.
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.
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.
21 using std::min; using std::max;
27 #include <glibmm/refptr.h>
31 #include <gtkmm/widget.h>
32 #include <gtkmm/style.h>
33 #include <gtkmm/treemodel.h>
34 #include <gtkmm/treepath.h>
36 #include "pbd/stl_delete.h"
40 #include "fft_graph.h"
41 #include "analysis_window.h"
42 #include "public_editor.h"
50 FFTGraph::FFTGraph (int windowSize)
63 _show_normalized = false;
64 _show_proportional = false;
71 setWindowSize (windowSize);
72 set_events (Gdk::POINTER_MOTION_MASK | Gdk::LEAVE_NOTIFY_MASK | Gdk::BUTTON_PRESS_MASK);
76 FFTGraph::setWindowSize (int windowSize)
79 Glib::Threads::Mutex::Lock lm (_a_window->track_list_lock);
80 setWindowSize_internal (windowSize);
82 setWindowSize_internal (windowSize);
87 FFTGraph::setWindowSize_internal (int windowSize)
89 // remove old tracklist & graphs
91 _a_window->clear_tracklist ();
94 _windowSize = windowSize;
95 _dataSize = windowSize / 2;
97 fftwf_destroy_plan (_plan);
112 if (_logScale != 0) {
117 // When destroying, window size is set to zero to free up memory
118 if (windowSize == 0) {
122 // FFT input & output buffers
123 _in = (float *) fftwf_malloc (sizeof (float) * _windowSize);
124 _out = (float *) fftwf_malloc (sizeof (float) * _windowSize);
127 _hanning = (float *) malloc (sizeof (float) * _windowSize);
129 // normalize the window
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)));
137 double isum = 2.0 / sum;
139 for (unsigned int i = 0; i < _windowSize; i++) {
143 _logScale = (int *) malloc (sizeof (int) * _dataSize);
145 for (unsigned int i = 0; i < _dataSize; i++) {
148 _plan = fftwf_plan_r2r_1d (_windowSize, _in, _out, FFTW_R2HC, FFTW_MEASURE);
151 FFTGraph::~FFTGraph ()
153 // This will free everything
157 cairo_surface_destroy (_surface);
162 FFTGraph::on_expose_event (GdkEventExpose* event)
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);
168 cairo_set_source_surface(cr, _surface, 0, 0);
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;
176 std::stringstream ss;
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";
182 ss << std::setprecision (0) << std::fixed << freq << " Hz";
184 layout->set_text (ss.str ());
186 layout->get_pixel_size (lw, lh);
189 const float y0 = _ann_y - lh - 7;
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;
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);
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);
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 ());
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);
224 FFTGraph::on_motion_notify_event (GdkEventMotion* ev)
228 x = (int) floor (ev->x);
229 y = (int) floor (ev->y);
231 if (x <= hl_margin + 1 || x >= width - hr_margin) {
234 if (y <= _yoff || y >= height - v_margin - 1) {
238 if (x == _ann_x && y == _ann_y) {
244 if (_ann_area.width == 0 || _ann_area.height == 0) {
247 queue_draw_area (_ann_area.x, _ann_area.y, _ann_area.width, _ann_area.height + 1);
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);
258 FFTGraph::on_leave_notify_event (GdkEventCrossing *)
260 if (_ann_x == -1 && _ann_y == -1) {
263 _ann_x = _ann_y = -1;
264 if (_ann_area.width == 0 || _ann_area.height == 0) {
267 queue_draw_area (_ann_area.x, _ann_area.y, _ann_area.width, _ann_area.height + 1);
269 _ann_area.width = _ann_area.height = 0;
274 FFTGraph::prepareResult (Gdk::Color color, string trackname)
276 FFTResult *res = new FFTResult (this, color, trackname);
282 FFTGraph::set_analysis_window (AnalysisWindow *a_window)
284 _a_window = a_window;
288 FFTGraph::draw_scales (cairo_t* cr)
290 int label_height = v_margin;
292 cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0);
293 cairo_rectangle (cr, 0, 0, width, height);
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
316 layout = create_pango_layout ("");
317 layout->set_font_description (get_style ()->get_font ());
320 // Draw x-axis scale 1/3 octaves centered around 1K
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; }
329 const float pos = currentScaleWidth * logf (freq / _fft_start) / _fft_log_base;
330 const int coord = floor (hl_margin + pos);
332 if (coord < overlap) {
336 std::stringstream ss;
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";
342 ss << std::setprecision (0) << std::fixed << freq << "Hz";
344 layout->set_text (ss.str ());
346 layout->get_pixel_size (lw, lh);
347 overlap = coord + lw + 3;
349 if (coord + lw / 2 > width - hr_margin - 2) {
352 if (v_margin / 2 + lh > label_height) {
353 label_height = v_margin / 2 + lh;
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);
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 ());
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; }
373 const float pos = currentScaleWidth * logf (freq / _fft_start) / _fft_log_base;
374 const int coord = floor (hl_margin + pos);
376 if (x != 0 && coord > overlap) {
380 std::stringstream ss;
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";
386 ss << std::setprecision (0) << std::fixed << freq << "Hz";
388 layout->set_text (ss.str ());
390 layout->get_pixel_size (lw, lh);
392 overlap = coord - lw - 3;
394 if (coord - lw / 2 < hl_margin + 2) {
398 // just get overlap position
401 if (v_margin / 2 + lh > label_height) {
402 label_height = v_margin / 2 + lh;
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);
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 ());
424 cairo_t* cr = cairo_create (_surface);
426 _yoff = draw_scales (cr);
428 if (_a_window == 0) {
434 Glib::Threads::Mutex::Lock lm (_a_window->track_list_lock);
436 if (!_a_window->track_list_ready) {
445 TreeNodeChildren track_rows = _a_window->track_list.get_model ()->children ();
447 if (!_show_normalized) {
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];
457 // disregard fft analysis from empty signals
458 if (res->minimum (_show_proportional) == res->maximum (_show_proportional)) {
461 // don't include invisible graphs
462 if (!row[_a_window->tlcols.visible]) {
466 minf = std::min (minf, res->minimum (_show_proportional));
467 maxf = std::max (maxf, res->maximum (_show_proportional));
471 // clamp range, > -200dBFS, at least 24dB (two y-axis labels) range
472 minf = std::max (-200.f, minf);
479 if (maxf - minf < 24) {
484 cairo_set_line_width (cr, 1.5);
485 cairo_translate (cr, hl_margin + 1, _yoff);
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);
492 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
494 int btm_lbl = fft_pane_size_h;
497 layout->set_text (_("dBFS"));
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;
506 for (int x = -6; x >= -200; x -= 12) {
507 float yp = 1.5 + fft_pane_size_h - rint ((x - minf) * pixels_per_db);
510 std::stringstream ss;
512 layout->set_text (ss.str ());
514 layout->get_pixel_size (lw, lh);
516 if (yp + 2 + lh / 2 > btm_lbl) {
519 if (yp < 2 + lh / 2) {
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 ());
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);
534 cairo_rectangle (cr, 1, 1, fft_pane_size_w, fft_pane_size_h);
537 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
538 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
540 for (TreeIter i = track_rows.begin (); i != track_rows.end (); i++) {
541 TreeModel::Row row = *i;
543 // don't show graphs for tracks which are deselected
544 if (!row[_a_window->tlcols.visible]) {
548 FFTResult *res = row[_a_window->tlcols.graph];
550 // don't show graphs for empty signals
551 if (res->minimum (_show_proportional) == res->maximum (_show_proportional)) {
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);
564 // Draw the line of maximum values
566 for (unsigned int x = 1; x < res->length () - 1; ++x) {
567 mpp = std::max (mpp, res->maxAt (x, _show_proportional));
569 if (_logScale[x] == _logScale[x + 1]) {
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);
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));
585 if (_logScale[x] == _logScale[x + 1]) {
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);
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);
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);
607 for (unsigned int x = 0; x < res->length () - 1; x++) {
608 mpp = std::max (mpp, res->avgAt (x, _show_proportional));
610 if (_logScale[x] == _logScale[x + 1]) {
614 mpp = fmax (mpp, minf);
615 mpp = fmin (mpp, maxf);
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);
623 cairo_set_source_rgb (cr, res->get_color ().get_red_p (), res->get_color ().get_green_p (), res->get_color ().get_blue_p ());
631 FFTGraph::on_size_request (Gtk::Requisition* requisition)
633 width = max (requisition->width, minScaleWidth + hl_margin + hr_margin);
634 height = max (requisition->height, minScaleHeight + 2 + v_margin * 2);
636 requisition->width = width;;
637 requisition->height = height;
641 FFTGraph::on_size_allocate (Gtk::Allocation & alloc)
643 width = alloc.get_width ();
644 height = alloc.get_height ();
648 DrawingArea::on_size_allocate (alloc);
652 FFTGraph::update_size ()
654 samplecnt_t SR = PublicEditor::instance ().session ()->nominal_sample_rate ();
655 _fft_start = SR / (double)_dataSize;
657 _fft_log_base = logf (.5 * _dataSize);
658 currentScaleWidth = width - hl_margin - hr_margin;
660 for (unsigned int i = 1; i < _dataSize; ++i) {
661 _logScale[i] = floor (currentScaleWidth * logf (.5 * i) / _fft_log_base);
664 cairo_surface_destroy (_surface);
666 _surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);