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)
54 setWindowSize(windowSize);
58 FFTGraph::setWindowSize(int windowSize)
61 Glib::Mutex::Lock lm (_a_window->track_list_lock);
62 setWindowSize_internal(windowSize);
64 setWindowSize_internal(windowSize);
69 FFTGraph::setWindowSize_internal(int windowSize)
71 // remove old tracklist & graphs
73 _a_window->clear_tracklist();
76 _windowSize = windowSize;
77 _dataSize = windowSize / 2;
79 fftwf_destroy_plan(_plan);
99 // When destroying, window size is set to zero to free up memory
103 // FFT input & output buffers
104 _in = (float *) fftwf_malloc(sizeof(float) * _windowSize);
105 _out = (float *) fftwf_malloc(sizeof(float) * _windowSize);
108 _hanning = (float *) malloc(sizeof(float) * _windowSize);
111 // normalize the window
114 for (int i=0; i < _windowSize; i++) {
115 _hanning[i]=0.81f * ( 0.5f - (0.5f * (float) cos(2.0f * M_PI * (float)i / (float)(_windowSize))));
119 double isum = 1.0 / sum;
121 for (int i=0; i < _windowSize; i++) {
125 _logScale = (int *) malloc(sizeof(int) * _dataSize);
126 for (int i = 0; i < _dataSize; i++) {
127 _logScale[i] = (int)floor(log10( 1.0 + i * 9.0 / (double)_dataSize) * (double)scaleWidth);
129 _plan = fftwf_plan_r2r_1d(_windowSize, _in, _out, FFTW_R2HC, FFTW_ESTIMATE);
132 FFTGraph::~FFTGraph()
134 // This will free everything
139 FFTGraph::on_expose_event (GdkEventExpose* event)
146 FFTGraph::prepareResult(Gdk::Color color, string trackname)
148 FFTResult *res = new FFTResult(this, color, trackname);
154 FFTGraph::analyze(float *window, float *composite)
157 // Copy the data and apply the hanning window
158 for (i = 0; i < _windowSize; i++) {
159 _in[i] = window[ i ] * _hanning[ i ];
162 fftwf_execute(_plan);
164 composite[0] += (_out[0] * _out[0]);
166 for (i=1; i < _dataSize - 1; i++) { // TODO: check with Jesse whether this is really correct
167 composite[i] += (_out[i] * _out[i]) + (_out[_windowSize-i] * _out[_windowSize-i]);
172 FFTGraph::set_analysis_window(AnalysisWindow *a_window)
174 _a_window = a_window;
178 FFTGraph::draw_scales(Glib::RefPtr<Gdk::Window> window)
181 Glib::RefPtr<Gtk::Style> style = get_style();
182 Glib::RefPtr<Gdk::GC> black = style->get_black_gc();
183 Glib::RefPtr<Gdk::GC> white = style->get_white_gc();
185 window->draw_rectangle(black, true, 0, 0, width, height);
197 window->draw_line(white, h_margin, v_margin, h_margin, height - v_margin );
200 window->draw_line(white, width - h_margin, v_margin, width - h_margin, height - v_margin );
203 window->draw_line(white, h_margin, height - v_margin, width - h_margin, height - v_margin );
205 #define DB_METRIC_LENGTH 8
207 window->draw_line(white, h_margin - DB_METRIC_LENGTH, v_margin, h_margin, v_margin );
210 window->draw_line(white, width - h_margin, v_margin, width - h_margin + DB_METRIC_LENGTH, v_margin );
214 graph_gc = GC::create( get_window() );
219 grey.set_rgb_p(0.2, 0.2, 0.2);
221 graph_gc->set_rgb_fg_color( grey );
224 layout = create_pango_layout ("");
225 layout->set_font_description (get_style()->get_font());
229 int logscale_pos = 0;
230 int position_on_scale;
231 for (int x = 1; x < 8; x++) {
232 position_on_scale = (int)floor( (double)scaleWidth*(double)x/8.0);
234 while (_logScale[logscale_pos] < position_on_scale)
237 int coord = (int)(v_margin + 1.0 + position_on_scale);
241 int rate_at_pos = (int)((double)(SR/2) * (double)logscale_pos / (double)_dataSize);
244 snprintf(buf,32,"%dhz",rate_at_pos);
246 std::string label = buf;
248 layout->set_text(label);
250 window->draw_line(graph_gc, coord, v_margin, coord, height - v_margin);
253 layout->get_pixel_size (width, height);
255 window->draw_layout(white, coord - width / 2, v_margin / 2, layout);
264 Glib::Mutex::Lock lm (_a_window->track_list_lock);
266 draw_scales(get_window());
271 if (!_a_window->track_list_ready)
275 // Find "session wide" min & max
276 float min = 1000000000000.0;
277 float max = -1000000000000.0;
279 TreeNodeChildren track_rows = _a_window->track_list.get_model()->children();
281 for (TreeIter i = track_rows.begin(); i != track_rows.end(); i++) {
283 TreeModel::Row row = *i;
284 FFTResult *res = row[_a_window->tlcols.graph];
286 // disregard fft analysis from empty signals
287 if (res->minimum() == res->maximum()) {
291 if ( res->minimum() < min) {
292 min = res->minimum();
295 if ( res->maximum() > max) {
296 max = res->maximum();
300 int graph_height = height - 2 * h_margin;
303 graph_gc = GC::create( get_window() );
307 double pixels_per_db = (double)graph_height / (double)(max - min);
310 for (TreeIter i = track_rows.begin(); i != track_rows.end(); i++) {
312 TreeModel::Row row = *i;
314 // don't show graphs for tracks which are deselected
315 if (!row[_a_window->tlcols.visible]) {
319 FFTResult *res = row[_a_window->tlcols.graph];
321 // don't show graphs for empty signals
322 if (res->minimum() == res->maximum()) {
326 std::string name = row[_a_window->tlcols.trackname];
328 // Set color from track
329 graph_gc->set_rgb_fg_color( res->get_color() );
331 float mpp = -1000000.0;
333 float prevSample = min;
335 for (int x = 0; x < res->length() - 1; x++) {
337 if (res->sampleAt(x) > mpp)
338 mpp = res->sampleAt(x);
340 // If the next point on the log scale is at the same location,
342 if (x + 1 < res->length() &&
343 _logScale[x] == _logScale[x + 1]) {
347 get_window()->draw_line(
349 v_margin + 1 + prevx,
350 graph_height - (int)floor( (prevSample - min) * pixels_per_db) + h_margin - 1,
351 v_margin + 1 + _logScale[x],
352 graph_height - (int)floor( (mpp - min) * pixels_per_db) + h_margin - 1);
354 prevx = _logScale[x];
366 FFTGraph::on_size_request(Gtk::Requisition* requisition)
368 width = scaleWidth + h_margin * 2;
369 height = scaleHeight + 2 + v_margin * 2;
371 if (_logScale != 0) {
375 _logScale = (int *) malloc(sizeof(int) * _dataSize);
376 //cerr << "LogScale: " << endl;
377 for (int i = 0; i < _dataSize; i++) {
378 _logScale[i] = (int)floor(log10( 1.0 + i * 9.0 / (double)_dataSize) * (double)scaleWidth);
379 //cerr << i << ":\t" << _logScale[i] << endl;
382 requisition->width = width;;
383 requisition->height = height;
387 FFTGraph::on_size_allocate(Gtk::Allocation & alloc)
389 width = alloc.get_width();
390 height = alloc.get_height();
392 DrawingArea::on_size_allocate (alloc);