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)
62 _show_normalized = false;
63 _show_proportional = false;
65 setWindowSize (windowSize);
69 FFTGraph::setWindowSize (int windowSize)
72 Glib::Threads::Mutex::Lock lm (_a_window->track_list_lock);
73 setWindowSize_internal (windowSize);
75 setWindowSize_internal (windowSize);
80 FFTGraph::setWindowSize_internal (int windowSize)
82 // remove old tracklist & graphs
84 _a_window->clear_tracklist ();
87 _windowSize = windowSize;
88 _dataSize = windowSize / 2;
90 fftwf_destroy_plan (_plan);
105 if (_logScale != 0) {
110 // When destroying, window size is set to zero to free up memory
111 if (windowSize == 0) {
115 // FFT input & output buffers
116 _in = (float *) fftwf_malloc (sizeof (float) * _windowSize);
117 _out = (float *) fftwf_malloc (sizeof (float) * _windowSize);
120 _hanning = (float *) malloc (sizeof (float) * _windowSize);
122 // normalize the window
125 for (unsigned int i = 0; i < _windowSize; ++i) {
126 _hanning[i] = 0.5f - (0.5f * (float) cos (2.0f * M_PI * (float)i / (float)(_windowSize)));
130 double isum = 2.0 / sum;
132 for (unsigned int i = 0; i < _windowSize; i++) {
136 _logScale = (int *) malloc (sizeof (int) * _dataSize);
138 for (unsigned int i = 0; i < _dataSize; i++) {
141 _plan = fftwf_plan_r2r_1d (_windowSize, _in, _out, FFTW_R2HC, FFTW_MEASURE);
144 FFTGraph::~FFTGraph ()
146 // This will free everything
151 FFTGraph::on_expose_event (GdkEventExpose* /*event*/)
158 FFTGraph::prepareResult (Gdk::Color color, string trackname)
160 FFTResult *res = new FFTResult (this, color, trackname);
167 FFTGraph::set_analysis_window (AnalysisWindow *a_window)
169 _a_window = a_window;
173 FFTGraph::draw_scales (Glib::RefPtr<Gdk::Window> window)
175 int label_height = v_margin;
177 Glib::RefPtr<Gtk::Style> style = get_style ();
178 Glib::RefPtr<Gdk::GC> black = style->get_black_gc ();
179 Glib::RefPtr<Gdk::GC> white = style->get_white_gc ();
181 window->draw_rectangle (black, true, 0, 0, width, height);
193 window->draw_line (white, hl_margin, v_margin, hl_margin, height - v_margin);
196 window->draw_line (white, width - hr_margin + 1, v_margin, width - hr_margin + 1, height - v_margin);
199 window->draw_line (white, hl_margin, height - v_margin, width - hr_margin, height - v_margin);
202 window->draw_line (white, 3, v_margin, hl_margin, v_margin);
205 window->draw_line (white, width - hr_margin + 1, v_margin, width - 3, v_margin);
209 graph_gc = GC::create (get_window ());
213 grey.set_rgb_p (0.2, 0.2, 0.2);
214 graph_gc->set_rgb_fg_color (grey);
217 layout = create_pango_layout ("");
218 layout->set_font_description (get_style ()->get_font ());
221 // Draw x-axis scale 1/3 octaves centered around 1K
224 // make sure 1K (x=0) is visible
225 for (int x = 0; x < 27; ++x) {
226 float freq = powf (2.f, x / 3.0) * 1000.f;
227 if (freq <= _fft_start) { continue; }
228 if (freq >= _fft_end) { break; }
230 const float pos = currentScaleWidth * logf (freq / _fft_start) / _fft_log_base;
231 const int coord = floor (hl_margin + pos);
233 if (coord < overlap) {
237 std::stringstream ss;
239 ss << std::setprecision (1) << std::fixed << freq / 1000 << "K";
240 } else if (freq >= 1000) {
241 ss << std::setprecision (2) << std::fixed << freq / 1000 << "K";
243 ss << std::setprecision (0) << std::fixed << freq << "Hz";
245 layout->set_text (ss.str ());
247 layout->get_pixel_size (lw, lh);
248 overlap = coord + lw + 3;
250 if (coord + lw / 2 > width - hr_margin - 2) {
253 if (v_margin / 2 + lh > label_height) {
254 label_height = v_margin / 2 + lh;
256 window->draw_line (graph_gc, coord, v_margin, coord, height - v_margin - 1);
257 window->draw_layout (white, coord - lw / 2, v_margin / 2, layout);
260 // now from 1K down to 4Hz
261 for (int x = 0; x > -24; --x) {
262 float freq = powf (2.f, x / 3.0) * 1000.f;
263 if (freq >= _fft_end) { continue; }
264 if (freq <= _fft_start) { break; }
266 const float pos = currentScaleWidth * logf (freq / _fft_start) / _fft_log_base;
267 const int coord = floor (hl_margin + pos);
269 if (x != 0 && coord > overlap) {
273 std::stringstream ss;
275 ss << std::setprecision (1) << std::fixed << freq / 1000 << "K";
276 } else if (freq >= 1000) {
277 ss << std::setprecision (2) << std::fixed << freq / 1000 << "K";
279 ss << std::setprecision (0) << std::fixed << freq << "Hz";
281 layout->set_text (ss.str ());
283 layout->get_pixel_size (lw, lh);
284 overlap = coord - lw - 3;
286 if (coord - lw / 2 < hl_margin + 2) {
290 // just get overlap position
293 if (v_margin / 2 + lh > label_height) {
294 label_height = v_margin / 2 + lh;
296 window->draw_line (graph_gc, coord, v_margin, coord, height - v_margin - 1);
297 window->draw_layout (white, coord - lw / 2, v_margin / 2, layout);
305 Glib::Threads::Mutex::Lock lm (_a_window->track_list_lock);
307 int yoff = draw_scales (get_window ());
312 if (!_a_window->track_list_ready)
318 TreeNodeChildren track_rows = _a_window->track_list.get_model ()->children ();
320 if (!_show_normalized) {
326 for (TreeIter i = track_rows.begin (); i != track_rows.end (); i++) {
327 TreeModel::Row row = *i;
328 FFTResult *res = row[_a_window->tlcols.graph];
330 // disregard fft analysis from empty signals
331 if (res->minimum (_show_proportional) == res->maximum (_show_proportional)) {
334 // don't include invisible graphs
335 if (!row[_a_window->tlcols.visible]) {
339 minf = std::min (minf, res->minimum (_show_proportional));
340 maxf = std::max (maxf, res->maximum (_show_proportional));
344 // clamp range, > -200dBFS, at least 24dB (two y-axis labels) range
345 minf = std::max (-200.f, minf);
350 if (maxf - minf < 24) {
356 cr = gdk_cairo_create (GDK_DRAWABLE (get_window ()->gobj ()));
357 cairo_set_line_width (cr, 1.5);
358 cairo_translate (cr, hl_margin + 1, yoff);
360 float fft_pane_size_w = width - hl_margin - hr_margin;
361 float fft_pane_size_h = height - v_margin - 1 - yoff;
362 double pixels_per_db = (double)fft_pane_size_h / (double)(maxf - minf);
365 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
367 int btm_lbl = fft_pane_size_h;
370 layout->set_text (_("dBFS"));
372 layout->get_pixel_size (lw, lh);
373 cairo_move_to (cr, -2 - lw, fft_pane_size_h - lh / 2);
374 pango_cairo_update_layout (cr, layout->gobj ());
375 pango_cairo_show_layout (cr, layout->gobj ());
376 btm_lbl = fft_pane_size_h - lh;
379 for (int x = -6; x >= -200; x -= 12) {
380 float yp = 1.5 + fft_pane_size_h - rint ((x - minf) * pixels_per_db);
383 std::stringstream ss;
385 layout->set_text (ss.str ());
387 layout->get_pixel_size (lw, lh);
389 if (yp + 2 + lh / 2 > btm_lbl) {
392 if (yp < 2 + lh / 2) {
396 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
397 cairo_move_to (cr, -2 - lw, yp - lh / 2);
398 pango_cairo_update_layout (cr, layout->gobj ());
399 pango_cairo_show_layout (cr, layout->gobj ());
401 cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
402 cairo_move_to (cr, 0, yp);
403 cairo_line_to (cr, fft_pane_size_w, yp);
407 cairo_rectangle (cr, 1, 1, fft_pane_size_w, fft_pane_size_h);
410 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
411 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
413 for (TreeIter i = track_rows.begin (); i != track_rows.end (); i++) {
414 TreeModel::Row row = *i;
416 // don't show graphs for tracks which are deselected
417 if (!row[_a_window->tlcols.visible]) {
421 FFTResult *res = row[_a_window->tlcols.graph];
423 // don't show graphs for empty signals
424 if (res->minimum (_show_proportional) == res->maximum (_show_proportional)) {
433 X = 0.5f + _logScale[0];
434 Y = 1.5f + fft_pane_size_h - pixels_per_db * (res->maxAt (0, _show_proportional) - minf);
435 cairo_move_to (cr, X, Y);
437 // Draw the line of maximum values
439 for (unsigned int x = 1; x < res->length () - 1; ++x) {
440 mpp = std::max (mpp, res->maxAt (x, _show_proportional));
442 if (_logScale[x] == _logScale[x + 1]) {
446 mpp = fmin (mpp, maxf);
447 X = 0.5f + _logScale[x];
448 Y = 1.5f + fft_pane_size_h - pixels_per_db * (mpp - minf);
449 cairo_line_to (cr, X, Y);
454 // Draw back to the start using the minimum value
455 for (int x = res->length () - 1; x >= 0; --x) {
456 mpp = std::min (mpp, res->minAt (x, _show_proportional));
458 if (_logScale[x] == _logScale[x + 1]) {
462 mpp = fmax (mpp, minf);
463 X = 0.5f + _logScale[x];
464 Y = 1.5f + fft_pane_size_h - pixels_per_db * (mpp - minf);
465 cairo_line_to (cr, X, Y);
469 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);
470 cairo_close_path (cr);
474 // draw max of averages
475 X = 0.5f + _logScale[0];
476 Y = 1.5f + fft_pane_size_h - pixels_per_db * (res->avgAt (0, _show_proportional) - minf);
477 cairo_move_to (cr, X, Y);
480 for (unsigned int x = 0; x < res->length () - 1; x++) {
481 mpp = std::max (mpp, res->avgAt (x, _show_proportional));
483 if (_logScale[x] == _logScale[x + 1]) {
487 mpp = fmax (mpp, minf);
488 mpp = fmin (mpp, maxf);
490 X = 0.5f + _logScale[x];
491 Y = 1.5f + fft_pane_size_h - pixels_per_db * (mpp - minf);
492 cairo_line_to (cr, X, Y);
496 cairo_set_source_rgb (cr, res->get_color ().get_red_p (), res->get_color ().get_green_p (), res->get_color ().get_blue_p ());
503 FFTGraph::on_size_request (Gtk::Requisition* requisition)
505 width = max (requisition->width, minScaleWidth + hl_margin + hr_margin);
506 height = max (requisition->height, minScaleHeight + 2 + v_margin * 2);
510 requisition->width = width;;
511 requisition->height = height;
515 FFTGraph::on_size_allocate (Gtk::Allocation & alloc)
517 width = alloc.get_width ();
518 height = alloc.get_height ();
522 DrawingArea::on_size_allocate (alloc);
526 FFTGraph::update_size ()
528 framecnt_t SR = PublicEditor::instance ().session ()->nominal_frame_rate ();
529 _fft_start = SR / (double)_dataSize;
531 _fft_log_base = logf (.5 * _dataSize);
532 currentScaleWidth = width - hl_margin - hr_margin;
534 for (unsigned int i = 1; i < _dataSize; ++i) {
535 _logScale[i] = floor (currentScaleWidth * logf (.5 * i) / _fft_log_base);