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);
127 for (int i = 0; i < _dataSize; i++) {
130 _plan = fftwf_plan_r2r_1d(_windowSize, _in, _out, FFTW_R2HC, FFTW_ESTIMATE);
133 FFTGraph::~FFTGraph()
135 // This will free everything
140 FFTGraph::on_expose_event (GdkEventExpose* event)
147 FFTGraph::prepareResult(Gdk::Color color, string trackname)
149 FFTResult *res = new FFTResult(this, color, trackname);
155 FFTGraph::analyze(float *window, float *composite)
158 // Copy the data and apply the hanning window
159 for (i = 0; i < _windowSize; i++) {
160 _in[i] = window[ i ] * _hanning[ i ];
163 fftwf_execute(_plan);
165 composite[0] += (_out[0] * _out[0]);
167 for (i=1; i < _dataSize - 1; i++) { // TODO: check with Jesse whether this is really correct
168 composite[i] += (_out[i] * _out[i]) + (_out[_windowSize-i] * _out[_windowSize-i]);
173 FFTGraph::set_analysis_window(AnalysisWindow *a_window)
175 _a_window = a_window;
179 FFTGraph::draw_scales(Glib::RefPtr<Gdk::Window> window)
182 Glib::RefPtr<Gtk::Style> style = get_style();
183 Glib::RefPtr<Gdk::GC> black = style->get_black_gc();
184 Glib::RefPtr<Gdk::GC> white = style->get_white_gc();
186 window->draw_rectangle(black, true, 0, 0, width, height);
198 window->draw_line(white, h_margin, v_margin, h_margin, height - v_margin );
201 window->draw_line(white, width - h_margin, v_margin, width - h_margin, height - v_margin );
204 window->draw_line(white, h_margin, height - v_margin, width - h_margin, height - v_margin );
206 #define DB_METRIC_LENGTH 8
208 window->draw_line(white, h_margin - DB_METRIC_LENGTH, v_margin, h_margin, v_margin );
211 window->draw_line(white, width - h_margin, v_margin, width - h_margin + DB_METRIC_LENGTH, v_margin );
215 graph_gc = GC::create( get_window() );
220 grey.set_rgb_p(0.2, 0.2, 0.2);
222 graph_gc->set_rgb_fg_color( grey );
225 layout = create_pango_layout ("");
226 layout->set_font_description (get_style()->get_font());
230 int logscale_pos = 0;
231 int position_on_scale;
232 for (int x = 1; x < 8; x++) {
233 position_on_scale = (int)floor( (double)scaleWidth*(double)x/8.0);
235 while (_logScale[logscale_pos] < position_on_scale)
238 int coord = (int)(v_margin + 1.0 + position_on_scale);
242 int rate_at_pos = (int)((double)(SR/2) * (double)logscale_pos / (double)_dataSize);
245 if (rate_at_pos < 1000)
246 snprintf(buf,32,"%dHz",rate_at_pos);
248 snprintf(buf,32,"%dk",(int)floor( (float)rate_at_pos/(float)1000) );
250 std::string label = buf;
252 layout->set_text(label);
254 window->draw_line(graph_gc, coord, v_margin, coord, height - v_margin);
257 layout->get_pixel_size (width, height);
259 window->draw_layout(white, coord - width / 2, v_margin / 2, layout);
268 Glib::Mutex::Lock lm (_a_window->track_list_lock);
270 draw_scales(get_window());
275 if (!_a_window->track_list_ready)
279 // Find "session wide" min & max
280 float min = 1000000000000.0;
281 float max = -1000000000000.0;
283 TreeNodeChildren track_rows = _a_window->track_list.get_model()->children();
285 for (TreeIter i = track_rows.begin(); i != track_rows.end(); i++) {
287 TreeModel::Row row = *i;
288 FFTResult *res = row[_a_window->tlcols.graph];
290 // disregard fft analysis from empty signals
291 if (res->minimum() == res->maximum()) {
295 if ( res->minimum() < min) {
296 min = res->minimum();
299 if ( res->maximum() > max) {
300 max = res->maximum();
304 int graph_height = height - 2 * h_margin;
307 graph_gc = GC::create( get_window() );
311 double pixels_per_db = (double)graph_height / (double)(max - min);
314 for (TreeIter i = track_rows.begin(); i != track_rows.end(); i++) {
316 TreeModel::Row row = *i;
318 // don't show graphs for tracks which are deselected
319 if (!row[_a_window->tlcols.visible]) {
323 FFTResult *res = row[_a_window->tlcols.graph];
325 // don't show graphs for empty signals
326 if (res->minimum() == res->maximum()) {
330 std::string name = row[_a_window->tlcols.trackname];
332 // Set color from track
333 graph_gc->set_rgb_fg_color( res->get_color() );
335 float mpp = -1000000.0;
337 float prevSample = min;
339 for (int x = 0; x < res->length() - 1; x++) {
341 if (res->sampleAt(x) > mpp)
342 mpp = res->sampleAt(x);
344 // If the next point on the log scale is at the same location,
346 if (x + 1 < res->length() &&
347 _logScale[x] == _logScale[x + 1]) {
351 get_window()->draw_line(
353 v_margin + 1 + prevx,
354 graph_height - (int)floor( (prevSample - min) * pixels_per_db) + h_margin - 1,
355 v_margin + 1 + _logScale[x],
356 graph_height - (int)floor( (mpp - min) * pixels_per_db) + h_margin - 1);
358 prevx = _logScale[x];
370 FFTGraph::on_size_request(Gtk::Requisition* requisition)
372 width = scaleWidth + h_margin * 2;
373 height = scaleHeight + 2 + v_margin * 2;
375 if (_logScale != 0) {
378 _logScale = (int *) malloc(sizeof(int) * _dataSize);
381 float FFT_START = SR/(double)_dataSize;
382 float FFT_END = SR/2.0;
383 float FFT_RANGE = log( FFT_END / FFT_START);
385 for (int i = 0; i < _dataSize; i++) {
386 float freq_at_bin = (SR/2.0) * ((double)i / (double)_dataSize);
387 float freq_at_pixel = FFT_START * exp( FFT_RANGE * pixel / (double)scaleWidth );
388 while (freq_at_bin > freq_at_pixel) {
390 freq_at_pixel = FFT_START * exp( FFT_RANGE * pixel / (double)scaleWidth );
392 _logScale[i] = (int)floor(pixel);
393 //printf("logscale at %d = %3.3f, freq_at_pixel %3.3f, freq_at_bin %3.3f, scaleWidth %d\n", i, pixel, freq_at_pixel, freq_at_bin, scaleWidth);
396 requisition->width = width;;
397 requisition->height = height;
401 FFTGraph::on_size_allocate(Gtk::Allocation & alloc)
403 width = alloc.get_width();
404 height = alloc.get_height();
406 DrawingArea::on_size_allocate (alloc);