colinf's patch to make the cursor be the dbl vertical arrow when over the track resiz...
[ardour2.git] / gtk2_ardour / fft_graph.cc
blob077c1e162c3f913fab2050106a0672068b4b4341
1 /*
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.
20 #include <iostream>
22 #include <glibmm.h>
23 #include <glibmm/refptr.h>
25 #include <gdkmm/gc.h>
27 #include <gtkmm/widget.h>
28 #include <gtkmm/style.h>
29 #include <gtkmm/treemodel.h>
30 #include <gtkmm/treepath.h>
32 #include <pbd/stl_delete.h>
34 #include <math.h>
36 #include "fft_graph.h"
37 #include "analysis_window.h"
39 using namespace std;
40 using namespace Gtk;
41 using namespace Gdk;
43 FFTGraph::FFTGraph(int windowSize)
45 _logScale = 0;
47 _in = 0;
48 _out = 0;
49 _hanning = 0;
50 _logScale = 0;
52 _a_window = 0;
54 _show_minmax = false;
55 _show_normalized = false;
57 setWindowSize(windowSize);
60 void
61 FFTGraph::setWindowSize(int windowSize)
63 if (_a_window) {
64 Glib::Mutex::Lock lm (_a_window->track_list_lock);
65 setWindowSize_internal(windowSize);
66 } else {
67 setWindowSize_internal(windowSize);
71 void
72 FFTGraph::setWindowSize_internal(int windowSize)
74 // remove old tracklist & graphs
75 if (_a_window) {
76 _a_window->clear_tracklist();
79 _windowSize = windowSize;
80 _dataSize = windowSize / 2;
81 if (_in != 0) {
82 fftwf_destroy_plan(_plan);
83 free(_in);
84 _in = 0;
87 if (_out != 0) {
88 free(_out);
89 _out = 0;
92 if (_hanning != 0) {
93 free(_hanning);
94 _hanning = 0;
97 if (_logScale != 0) {
98 free(_logScale);
99 _logScale = 0;
102 // When destroying, window size is set to zero to free up memory
103 if (windowSize == 0)
104 return;
106 // FFT input & output buffers
107 _in = (float *) fftwf_malloc(sizeof(float) * _windowSize);
108 _out = (float *) fftwf_malloc(sizeof(float) * _windowSize);
110 // Hanning window
111 _hanning = (float *) malloc(sizeof(float) * _windowSize);
114 // normalize the window
115 double sum = 0.0;
117 for (int i=0; i < _windowSize; i++) {
118 _hanning[i]=0.81f * ( 0.5f - (0.5f * (float) cos(2.0f * M_PI * (float)i / (float)(_windowSize))));
119 sum += _hanning[i];
122 double isum = 1.0 / sum;
124 for (int i=0; i < _windowSize; i++) {
125 _hanning[i] *= isum;
128 _logScale = (int *) malloc(sizeof(int) * _dataSize);
129 //float count = 0;
130 for (int i = 0; i < _dataSize; i++) {
131 _logScale[i] = 0;
133 _plan = fftwf_plan_r2r_1d(_windowSize, _in, _out, FFTW_R2HC, FFTW_ESTIMATE);
136 FFTGraph::~FFTGraph()
138 // This will free everything
139 setWindowSize(0);
142 bool
143 FFTGraph::on_expose_event (GdkEventExpose* event)
145 redraw();
146 return true;
149 FFTResult *
150 FFTGraph::prepareResult(Gdk::Color color, string trackname)
152 FFTResult *res = new FFTResult(this, color, trackname);
154 return res;
158 void
159 FFTGraph::set_analysis_window(AnalysisWindow *a_window)
161 _a_window = a_window;
164 void
165 FFTGraph::draw_scales(Glib::RefPtr<Gdk::Window> window)
168 Glib::RefPtr<Gtk::Style> style = get_style();
169 Glib::RefPtr<Gdk::GC> black = style->get_black_gc();
170 Glib::RefPtr<Gdk::GC> white = style->get_white_gc();
172 window->draw_rectangle(black, true, 0, 0, width, height);
175 * 4 5
176 * _ _
177 * | |
178 * 1 | | 2
179 * |________|
183 // Line 1
184 window->draw_line(white, h_margin, v_margin, h_margin, height - v_margin );
186 // Line 2
187 window->draw_line(white, width - h_margin + 1, v_margin, width - h_margin + 1, height - v_margin );
189 // Line 3
190 window->draw_line(white, h_margin, height - v_margin, width - h_margin, height - v_margin );
192 #define DB_METRIC_LENGTH 8
193 // Line 4
194 window->draw_line(white, h_margin - DB_METRIC_LENGTH, v_margin, h_margin, v_margin );
196 // Line 5
197 window->draw_line(white, width - h_margin + 1, v_margin, width - h_margin + DB_METRIC_LENGTH, v_margin );
201 if (graph_gc == 0) {
202 graph_gc = GC::create( get_window() );
205 Color grey;
207 grey.set_rgb_p(0.2, 0.2, 0.2);
209 graph_gc->set_rgb_fg_color( grey );
211 if (layout == 0) {
212 layout = create_pango_layout ("");
213 layout->set_font_description (get_style()->get_font());
216 // Draw logscale
217 int logscale_pos = 0;
218 int position_on_scale;
221 /* TODO, write better scales and change the log function so that octaves are of equal pixel length
222 float scale_points[10] = { 55.0, 110.0, 220.0, 440.0, 880.0, 1760.0, 3520.0, 7040.0, 14080.0, 28160.0 };
224 for (int x = 0; x < 10; x++) {
226 // i = 0.. _dataSize-1
227 float freq_at_bin = (SR/2.0) * ((double)i / (double)_dataSize);
231 freq_at_pixel = FFT_START * exp( FFT_RANGE * pixel / (double)(currentScaleWidth - 1) );
235 for (int x = 1; x < 8; x++) {
236 position_on_scale = (int)floor( (double)currentScaleWidth*(double)x/8.0);
238 while (_logScale[logscale_pos] < position_on_scale)
239 logscale_pos++;
241 int coord = (int)(v_margin + 1.0 + position_on_scale);
243 int SR = 44100;
245 int rate_at_pos = (int)((double)(SR/2) * (double)logscale_pos / (double)_dataSize);
247 char buf[32];
248 if (rate_at_pos < 1000)
249 snprintf(buf,32,"%dHz",rate_at_pos);
250 else
251 snprintf(buf,32,"%dk",(int)floor( (float)rate_at_pos/(float)1000) );
253 std::string label = buf;
255 layout->set_text(label);
257 window->draw_line(graph_gc, coord, v_margin, coord, height - v_margin - 1);
259 int width, height;
260 layout->get_pixel_size (width, height);
262 window->draw_layout(white, coord - width / 2, v_margin / 2, layout);
268 void
269 FFTGraph::redraw()
271 Glib::Mutex::Lock lm (_a_window->track_list_lock);
273 draw_scales(get_window());
276 if (_a_window == 0)
277 return;
279 if (!_a_window->track_list_ready)
280 return;
282 cairo_t *cr;
283 cr = gdk_cairo_create(GDK_DRAWABLE(get_window()->gobj()));
284 cairo_set_line_width(cr, 1.5);
285 cairo_translate(cr, (float)v_margin + 1.0, (float)h_margin);
289 // Find "session wide" min & max
290 float min = 1000000000000.0;
291 float max = -1000000000000.0;
293 TreeNodeChildren track_rows = _a_window->track_list.get_model()->children();
295 for (TreeIter i = track_rows.begin(); i != track_rows.end(); i++) {
297 TreeModel::Row row = *i;
298 FFTResult *res = row[_a_window->tlcols.graph];
300 // disregard fft analysis from empty signals
301 if (res->minimum() == res->maximum()) {
302 continue;
305 if ( res->minimum() < min) {
306 min = res->minimum();
309 if ( res->maximum() > max) {
310 max = res->maximum();
314 if (!_show_normalized) {
315 min = -150.0f;
316 max = 0.0f;
319 //int graph_height = height - 2 * h_margin;
323 float fft_pane_size_w = (float)(width - 2*v_margin) - 1.0;
324 float fft_pane_size_h = (float)(height - 2*h_margin);
326 double pixels_per_db = (double)fft_pane_size_h / (double)(max - min);
328 cairo_rectangle(cr, 0.0, 0.0, fft_pane_size_w, fft_pane_size_h);
329 cairo_clip(cr);
331 for (TreeIter i = track_rows.begin(); i != track_rows.end(); i++) {
333 TreeModel::Row row = *i;
335 // don't show graphs for tracks which are deselected
336 if (!row[_a_window->tlcols.visible]) {
337 continue;
340 FFTResult *res = row[_a_window->tlcols.graph];
342 // don't show graphs for empty signals
343 if (res->minimum() == res->maximum()) {
344 continue;
347 float mpp;
349 if (_show_minmax) {
350 mpp = -1000000.0;
352 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);
353 cairo_move_to(cr, 0.5f + (float)_logScale[0], 0.5f + (float)( fft_pane_size_h - (int)floor( (res->maxAt(0) - min) * pixels_per_db) ));
355 // Draw the line of maximum values
356 for (int x = 1; x < res->length(); x++) {
357 if (res->maxAt(x) > mpp)
358 mpp = res->maxAt(x);
359 mpp = fmax(mpp, min);
360 mpp = fmin(mpp, max);
362 // If the next point on the log scale is at the same location,
363 // don't draw yet
364 if (x + 1 < res->length() && _logScale[x] == _logScale[x + 1]) {
365 continue;
368 float X = 0.5f + (float)_logScale[x];
369 float Y = 0.5f + (float)( fft_pane_size_h - (int)floor( (mpp - min) * pixels_per_db) );
371 cairo_line_to(cr, X, Y);
373 mpp = -1000000.0;
376 mpp = +10000000.0;
377 // Draw back to the start using the minimum value
378 for (int x = res->length()-1; x >= 0; x--) {
379 if (res->minAt(x) < mpp)
380 mpp = res->minAt(x);
381 mpp = fmax(mpp, min);
382 mpp = fmin(mpp, max);
384 // If the next point on the log scale is at the same location,
385 // don't draw yet
386 if (x - 1 > 0 && _logScale[x] == _logScale[x - 1]) {
387 continue;
390 float X = 0.5f + (float)_logScale[x];
391 float Y = 0.5f + (float)( fft_pane_size_h - (int)floor( (mpp - min) * pixels_per_db) );
393 cairo_line_to(cr, X, Y );
395 mpp = +10000000.0;
398 cairo_close_path(cr);
400 cairo_fill(cr);
405 // Set color from track
406 cairo_set_source_rgb(cr, res->get_color().get_red_p(), res->get_color().get_green_p(), res->get_color().get_blue_p());
408 mpp = -1000000.0;
410 cairo_move_to(cr, 0.5, fft_pane_size_h-0.5);
412 for (int x = 0; x < res->length(); x++) {
415 if (res->avgAt(x) > mpp)
416 mpp = res->avgAt(x);
417 mpp = fmax(mpp, min);
418 mpp = fmin(mpp, max);
420 // If the next point on the log scale is at the same location,
421 // don't draw yet
422 if (x + 1 < res->length() && _logScale[x] == _logScale[x + 1]) {
423 continue;
426 cairo_line_to(cr, 0.5f + (float)_logScale[x], 0.5f + (float)( fft_pane_size_h - (int)floor( (mpp - min) * pixels_per_db) ));
428 mpp = -1000000.0;
431 cairo_stroke(cr);
434 cairo_destroy(cr);
437 void
438 FFTGraph::on_size_request(Gtk::Requisition* requisition)
440 width = max(requisition->width, minScaleWidth + h_margin * 2);
441 height = max(requisition->height, minScaleHeight + 2 + v_margin * 2);
443 update_size();
445 requisition->width = width;;
446 requisition->height = height;
449 void
450 FFTGraph::on_size_allocate(Gtk::Allocation & alloc)
452 width = alloc.get_width();
453 height = alloc.get_height();
455 update_size();
457 DrawingArea::on_size_allocate (alloc);
460 void
461 FFTGraph::update_size()
463 currentScaleWidth = width - h_margin*2;
464 currentScaleHeight = height - 2 - v_margin*2;
466 float SR = 44100;
467 float FFT_START = SR/(double)_dataSize;
468 float FFT_END = SR/2.0;
469 float FFT_RANGE = log( FFT_END / FFT_START);
470 float pixel = 0;
471 for (int i = 0; i < _dataSize; i++) {
472 float freq_at_bin = (SR/2.0) * ((double)i / (double)_dataSize);
473 float freq_at_pixel;
474 pixel--;
475 do {
476 pixel++;
477 freq_at_pixel = FFT_START * exp( FFT_RANGE * pixel / (double)(currentScaleWidth - 1) );
478 } while (freq_at_bin > freq_at_pixel);
480 _logScale[i] = (int)floor(pixel);