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.
23 #include <glibmm/refptr.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"
36 #include "fft_graph.h"
37 #include "analysis_window.h"
43 FFTGraph::FFTGraph(int windowSize
)
55 _show_normalized
= false;
57 setWindowSize(windowSize
);
61 FFTGraph::setWindowSize(int windowSize
)
64 Glib::Mutex::Lock
lm (_a_window
->track_list_lock
);
65 setWindowSize_internal(windowSize
);
67 setWindowSize_internal(windowSize
);
72 FFTGraph::setWindowSize_internal(int windowSize
)
74 // remove old tracklist & graphs
76 _a_window
->clear_tracklist();
79 _windowSize
= windowSize
;
80 _dataSize
= windowSize
/ 2;
82 fftwf_destroy_plan(_plan
);
102 // When destroying, window size is set to zero to free up memory
106 // FFT input & output buffers
107 _in
= (float *) fftwf_malloc(sizeof(float) * _windowSize
);
108 _out
= (float *) fftwf_malloc(sizeof(float) * _windowSize
);
111 _hanning
= (float *) malloc(sizeof(float) * _windowSize
);
114 // normalize the window
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
))));
122 double isum
= 1.0 / sum
;
124 for (int i
=0; i
< _windowSize
; i
++) {
128 _logScale
= (int *) malloc(sizeof(int) * _dataSize
);
130 for (int i
= 0; i
< _dataSize
; i
++) {
133 _plan
= fftwf_plan_r2r_1d(_windowSize
, _in
, _out
, FFTW_R2HC
, FFTW_ESTIMATE
);
136 FFTGraph::~FFTGraph()
138 // This will free everything
143 FFTGraph::on_expose_event (GdkEventExpose
* /*event*/)
150 FFTGraph::prepareResult(Gdk::Color color
, string trackname
)
152 FFTResult
*res
= new FFTResult(this, color
, trackname
);
159 FFTGraph::set_analysis_window(AnalysisWindow
*a_window
)
161 _a_window
= a_window
;
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
);
184 window
->draw_line(white
, h_margin
, v_margin
, h_margin
, height
- v_margin
);
187 window
->draw_line(white
, width
- h_margin
+ 1, v_margin
, width
- h_margin
+ 1, height
- v_margin
);
190 window
->draw_line(white
, h_margin
, height
- v_margin
, width
- h_margin
, height
- v_margin
);
192 #define DB_METRIC_LENGTH 8
194 window
->draw_line(white
, h_margin
- DB_METRIC_LENGTH
, v_margin
, h_margin
, v_margin
);
197 window
->draw_line(white
, width
- h_margin
+ 1, v_margin
, width
- h_margin
+ DB_METRIC_LENGTH
, v_margin
);
202 graph_gc
= GC::create( get_window() );
207 grey
.set_rgb_p(0.2, 0.2, 0.2);
209 graph_gc
->set_rgb_fg_color( grey
);
212 layout
= create_pango_layout ("");
213 layout
->set_font_description (get_style()->get_font());
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
)
241 int coord
= (int)(v_margin
+ 1.0 + position_on_scale
);
245 int rate_at_pos
= (int)((double)(SR
/2) * (double)logscale_pos
/ (double)_dataSize
);
248 if (rate_at_pos
< 1000)
249 snprintf(buf
,32,"%dHz",rate_at_pos
);
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);
260 layout
->get_pixel_size (width
, height
);
262 window
->draw_layout(white
, coord
- width
/ 2, v_margin
/ 2, layout
);
271 Glib::Mutex::Lock
lm (_a_window
->track_list_lock
);
273 draw_scales(get_window());
279 if (!_a_window
->track_list_ready
)
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()) {
305 if ( res
->minimum() < min
) {
306 min
= res
->minimum();
309 if ( res
->maximum() > max
) {
310 max
= res
->maximum();
314 if (!_show_normalized
) {
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
);
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
]) {
340 FFTResult
*res
= row
[_a_window
->tlcols
.graph
];
342 // don't show graphs for empty signals
343 if (res
->minimum() == res
->maximum()) {
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
)
359 mpp
= fmax(mpp
, min
);
360 mpp
= fmin(mpp
, max
);
362 // If the next point on the log scale is at the same location,
364 if (x
+ 1 < res
->length() && _logScale
[x
] == _logScale
[x
+ 1]) {
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
);
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
)
381 mpp
= fmax(mpp
, min
);
382 mpp
= fmin(mpp
, max
);
384 // If the next point on the log scale is at the same location,
386 if (x
- 1 > 0 && _logScale
[x
] == _logScale
[x
- 1]) {
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
);
398 cairo_close_path(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());
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
)
417 mpp
= fmax(mpp
, min
);
418 mpp
= fmin(mpp
, max
);
420 // If the next point on the log scale is at the same location,
422 if (x
+ 1 < res
->length() && _logScale
[x
] == _logScale
[x
+ 1]) {
426 cairo_line_to(cr
, 0.5f
+ (float)_logScale
[x
], 0.5f
+ (float)( fft_pane_size_h
- (int)floor( (mpp
- min
) * pixels_per_db
) ));
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);
445 requisition
->width
= width
;;
446 requisition
->height
= height
;
450 FFTGraph::on_size_allocate(Gtk::Allocation
& alloc
)
452 width
= alloc
.get_width();
453 height
= alloc
.get_height();
457 DrawingArea::on_size_allocate (alloc
);
461 FFTGraph::update_size()
463 currentScaleWidth
= width
- h_margin
*2;
464 currentScaleHeight
= height
- 2 - v_margin
*2;
467 float FFT_START
= SR
/(double)_dataSize
;
468 float FFT_END
= SR
/2.0;
469 float FFT_RANGE
= log( FFT_END
/ FFT_START
);
471 for (int i
= 0; i
< _dataSize
; i
++) {
472 float freq_at_bin
= (SR
/2.0) * ((double)i
/ (double)_dataSize
);
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
);