most of the 2.X->3.0 commit (up to rev 4299) except for gtk2_ardour/editor_canvas...
[ardour.git] / gtk2_ardour / plugin_eq_gui.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sampo Savolainen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "plugin_eq_gui.h"
22 #include "fft.h"
23
24 #include "ardour_ui.h"
25 #include "gui_thread.h"
26 #include <ardour/audio_buffer.h>
27 #include <ardour/data_type.h>
28
29 #include <gtkmm/box.h>
30 #include <gtkmm/button.h>
31 #include <gtkmm/checkbutton.h>
32
33 #include <iostream>
34
35
36 PluginEqGui::PluginEqGui(boost::shared_ptr<ARDOUR::PluginInsert> pluginInsert)
37         : _min_dB(-12.0),
38           _max_dB(+12.0),
39           _step_dB(3.0),
40           _impulse_fft(0),
41           _signal_input_fft(0),
42           _signal_output_fft(0),
43           _plugin_insert(pluginInsert)
44 {
45         _signal_analysis_running = false;
46         _samplerate = ARDOUR_UI::instance()->the_session()->frame_rate();
47
48         _plugin = _plugin_insert->get_impulse_analysis_plugin();
49         _plugin->activate();
50
51         set_buffer_size(4096, 16384);
52         //set_buffer_size(4096, 4096);
53
54         _log_coeff = (1.0 - 2.0 * (1000.0/(_samplerate/2.0))) / powf(1000.0/(_samplerate/2.0), 2.0); 
55         _log_max = log10f(1 + _log_coeff);
56
57
58         // Setup analysis drawing area
59         _analysis_scale_surface = 0;
60
61         _analysis_area = new Gtk::DrawingArea();
62         _analysis_width = 500.0;
63         _analysis_height = 500.0;
64         _analysis_area->set_size_request(_analysis_width, _analysis_height);
65
66         _analysis_area->signal_expose_event().connect( sigc::mem_fun (*this, &PluginEqGui::expose_analysis_area));
67         _analysis_area->signal_size_allocate().connect( sigc::mem_fun (*this, &PluginEqGui::resize_analysis_area));
68         
69
70         // dB selection
71         dBScaleModel = Gtk::ListStore::create(dBColumns);
72
73         dBScaleCombo = new Gtk::ComboBox(dBScaleModel);
74         dBScaleCombo -> set_title("dB scale");
75
76 #define ADD_DB_ROW(MIN,MAX,STEP,NAME) \
77         { \
78                 Gtk::TreeModel::Row row = *(dBScaleModel->append()); \
79                 row[dBColumns.dBMin]  = (MIN); \
80                 row[dBColumns.dBMax]  = (MAX); \
81                 row[dBColumns.dBStep] = (STEP); \
82                 row[dBColumns.name]   = NAME; \
83         }
84
85         ADD_DB_ROW( -6,  +6, 1, "-6dB .. +6dB");
86         ADD_DB_ROW(-12, +12, 3, "-12dB .. +12dB");
87         ADD_DB_ROW(-24, +24, 5, "-24dB .. +24dB");
88         ADD_DB_ROW(-36, +36, 6, "-36dB .. +36dB");
89         ADD_DB_ROW(-64, +64,12, "-64dB .. +64dB");
90
91 #undef ADD_DB_ROW
92
93         dBScaleCombo -> pack_start(dBColumns.name);
94         dBScaleCombo -> set_active(1);
95
96         dBScaleCombo -> signal_changed().connect( sigc::mem_fun(*this, &PluginEqGui::change_dB_scale) );
97
98         Gtk::Label *dBComboLabel = new Gtk::Label("dB scale");  
99
100         Gtk::HBox *dBSelectBin = new Gtk::HBox(false, 5);
101         dBSelectBin->add( *manage(dBComboLabel));
102         dBSelectBin->add( *manage(dBScaleCombo));
103         
104         // Phase checkbutton
105         _phase_button = new Gtk::CheckButton("Show phase");
106         _phase_button->set_active(true);
107         _phase_button->signal_toggled().connect( sigc::mem_fun(*this, &PluginEqGui::redraw_scales));
108
109         // populate table
110         attach( *manage(_analysis_area), 1, 3, 1, 2);
111         attach( *manage(dBSelectBin),    1, 2, 2, 3, Gtk::SHRINK, Gtk::SHRINK);
112         attach( *manage(_phase_button),  2, 3, 2, 3, Gtk::SHRINK, Gtk::SHRINK);
113
114
115         // Connect the realtime signal collection callback
116         _plugin_insert->AnalysisDataGathered.connect( sigc::mem_fun(*this, &PluginEqGui::signal_collect_callback ));
117 }
118
119 PluginEqGui::~PluginEqGui()
120 {
121         if (_analysis_scale_surface) {
122                 cairo_surface_destroy (_analysis_scale_surface);
123         }
124
125         delete _impulse_fft;
126         delete _signal_input_fft;
127         delete _signal_output_fft;
128
129         _plugin->deactivate();
130         
131         // all gui objects are *manage'd by the inherited Table object
132 }
133
134
135 void
136 PluginEqGui::on_hide()
137 {
138         stop_updating();
139         Gtk::Table::on_hide();
140 }
141
142 void
143 PluginEqGui::stop_updating()
144 {
145         if (_update_connection.connected()) {
146                 _update_connection.disconnect();
147         }
148 }
149
150 void
151 PluginEqGui::start_updating()
152 {
153         if (!_update_connection.connected() && is_visible()) {
154                 _update_connection = Glib::signal_timeout().connect( sigc::mem_fun(this, &PluginEqGui::timeout_callback), 250);
155         }
156 }
157
158 void
159 PluginEqGui::on_show()
160 {
161         Gtk::Table::on_show();
162
163         start_updating();
164
165         Gtk::Widget *toplevel = get_toplevel();
166         if (!toplevel) {
167                 std::cerr << "No toplevel widget for PluginEqGui?!?!" << std::endl;
168         }
169
170         if (!_window_unmap_connection.connected()) {
171                 _window_unmap_connection = toplevel->signal_unmap().connect( sigc::mem_fun(this, &PluginEqGui::stop_updating));
172         }
173
174         if (!_window_map_connection.connected()) {
175                 _window_map_connection = toplevel->signal_map().connect( sigc::mem_fun(this, &PluginEqGui::start_updating));
176         }
177
178 }
179
180 void
181 PluginEqGui::change_dB_scale()
182 {
183         Gtk::TreeModel::iterator iter = dBScaleCombo -> get_active();
184
185         Gtk::TreeModel::Row row;
186
187         if(iter && (row = *iter)) {
188                 _min_dB = row[dBColumns.dBMin];
189                 _max_dB = row[dBColumns.dBMax];
190                 _step_dB = row[dBColumns.dBStep];
191                 
192
193                 redraw_scales();
194         }
195 }
196
197 void
198 PluginEqGui::redraw_scales()
199 {
200
201         if (_analysis_scale_surface) {
202                 cairo_surface_destroy (_analysis_scale_surface);
203                 _analysis_scale_surface = 0;
204         }
205
206         _analysis_area->queue_draw();   
207
208         // TODO: Add graph legend!
209 }
210
211 void
212 PluginEqGui::set_buffer_size(uint32_t size, uint32_t signal_size)
213 {
214         if (_buffer_size == size && _signal_buffer_size == signal_size)
215                 return;
216
217
218         FFT *tmp1 = _impulse_fft;
219         FFT *tmp2 = _signal_input_fft;
220         FFT *tmp3 = _signal_output_fft;
221
222         try {
223                 _impulse_fft       = new FFT(size); 
224                 _signal_input_fft  = new FFT(signal_size); 
225                 _signal_output_fft = new FFT(signal_size); 
226         } catch( ... ) {
227                 // Don't care about lost memory, we're screwed anyhow
228                 _impulse_fft       = tmp1;
229                 _signal_input_fft  = tmp2;
230                 _signal_output_fft = tmp3;
231                 throw;
232         }
233
234         if (tmp1) delete tmp1;
235         if (tmp2) delete tmp1;
236         if (tmp3) delete tmp1;
237                 
238         _buffer_size = size;
239         _signal_buffer_size = signal_size;
240
241         // These are for impulse analysis only, the signal analysis uses the actual
242         // number of I/O's for the plugininsert
243         uint32_t inputs  = _plugin->get_info()->n_inputs.n_audio();
244         uint32_t outputs = _plugin->get_info()->n_outputs.n_audio();
245
246         // buffers for the signal analysis are ensured inside PluginInsert
247         uint32_t n_chans = std::max(inputs, outputs);
248         _bufferset.ensure_buffers(ARDOUR::DataType::AUDIO, n_chans, _buffer_size);
249
250         ARDOUR::ChanCount chanCount(ARDOUR::DataType::AUDIO, n_chans);
251         _bufferset.set_count(chanCount);
252 }
253
254 void 
255 PluginEqGui::resize_analysis_area(Gtk::Allocation& size)
256 {
257         _analysis_width  = (float)size.get_width();
258         _analysis_height = (float)size.get_height();
259
260         if (_analysis_scale_surface) {
261                 cairo_surface_destroy (_analysis_scale_surface);
262                 _analysis_scale_surface = 0;
263         }
264 }
265
266 bool
267 PluginEqGui::timeout_callback()
268 {
269         if (!_signal_analysis_running) {
270                 _signal_analysis_running = true;
271                 _plugin_insert -> collect_signal_for_analysis(_signal_buffer_size);
272         }
273         run_impulse_analysis();
274
275         return true;
276 }
277
278 void
279 PluginEqGui::signal_collect_callback(ARDOUR::BufferSet *in, ARDOUR::BufferSet *out)
280 {
281         ENSURE_GUI_THREAD(bind (mem_fun (*this, &PluginEqGui::signal_collect_callback), in, out));
282
283         _signal_input_fft ->reset();
284         _signal_output_fft->reset();
285
286         for (uint32_t i = 0; i < _plugin_insert->input_streams().n_audio(); ++i) {
287                 _signal_input_fft ->analyze(in ->get_audio(i).data(_signal_buffer_size, 0), FFT::HANN);
288         }
289         
290         for (uint32_t i = 0; i < _plugin_insert->output_streams().n_audio(); ++i) {
291                 _signal_output_fft->analyze(out->get_audio(i).data(_signal_buffer_size, 0), FFT::HANN);
292         }
293
294         _signal_input_fft ->calculate();
295         _signal_output_fft->calculate();
296
297         _signal_analysis_running = false;
298
299         // This signals calls expose_analysis_area()
300         _analysis_area->queue_draw();   
301 }
302
303 void
304 PluginEqGui::run_impulse_analysis()
305 {
306         uint32_t inputs  = _plugin->get_info()->n_inputs.n_audio();
307         uint32_t outputs = _plugin->get_info()->n_outputs.n_audio();
308
309         // Create the impulse, can't use silence() because consecutive calls won't work
310         for (uint32_t i = 0; i < inputs; ++i) {
311                 ARDOUR::AudioBuffer &buf = _bufferset.get_audio(i);
312                 ARDOUR::Sample *d = buf.data(_buffer_size, 0);
313                 memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size);
314                 *d = 1.0;
315         }
316         uint32_t x,y;
317         x=y=0;
318
319         _plugin->connect_and_run(_bufferset, x, y, _buffer_size, (nframes_t)0);
320
321         // Analyze all output buffers
322         _impulse_fft->reset();
323         for (uint32_t i = 0; i < outputs; ++i) {
324                 _impulse_fft->analyze(_bufferset.get_audio(i).data(_buffer_size, 0));
325         }
326
327         // normalize the output
328         _impulse_fft->calculate();
329
330         // This signals calls expose_analysis_area()
331         _analysis_area->queue_draw();   
332
333 }
334
335 bool
336 PluginEqGui::expose_analysis_area(GdkEventExpose *evt)
337 {
338         redraw_analysis_area();
339
340         return false;
341 }
342
343 void
344 PluginEqGui::draw_analysis_scales(cairo_t *ref_cr)
345 {
346         // TODO: check whether we need rounding
347         _analysis_scale_surface = cairo_surface_create_similar(cairo_get_target(ref_cr), 
348                                                              CAIRO_CONTENT_COLOR, 
349                                                              _analysis_width,
350                                                              _analysis_height);
351
352         cairo_t *cr = cairo_create (_analysis_scale_surface);
353
354         cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
355         cairo_rectangle(cr, 0.0, 0.0, _analysis_width, _analysis_height);
356         cairo_fill(cr);
357
358
359         draw_scales_power(_analysis_area, cr);
360         if (_phase_button->get_active()) {
361                 draw_scales_phase(_analysis_area, cr);
362         }
363         
364         cairo_destroy(cr);
365         
366 }
367
368 void
369 PluginEqGui::redraw_analysis_area()
370 {
371         cairo_t *cr;
372
373         cr = gdk_cairo_create(GDK_DRAWABLE(_analysis_area->get_window()->gobj()));
374
375         if (_analysis_scale_surface == 0) {
376                 draw_analysis_scales(cr);
377         }
378         
379
380         cairo_copy_page(cr);
381
382         cairo_set_source_surface(cr, _analysis_scale_surface, 0.0, 0.0);
383         cairo_paint(cr);
384
385         if (_phase_button->get_active()) {
386                 plot_impulse_phase(_analysis_area, cr);
387         }
388         plot_impulse_amplitude(_analysis_area, cr);
389
390         // TODO: make this optional
391         plot_signal_amplitude_difference(_analysis_area, cr);
392
393         cairo_destroy(cr);
394
395
396 }
397
398 #define PHASE_PROPORTION 0.5
399
400 void 
401 PluginEqGui::draw_scales_phase(Gtk::Widget *w, cairo_t *cr)
402 {
403         float y;
404         cairo_font_extents_t extents;
405         cairo_font_extents(cr, &extents);
406
407         char buf[256];
408         cairo_text_extents_t t_ext;
409
410         for (uint32_t i = 0; i < 3; i++) {
411
412                 y = _analysis_height/2.0 - (float)i*(_analysis_height/8.0)*PHASE_PROPORTION;
413
414                 cairo_set_source_rgb(cr, .8, .9, 0.2);
415                 if (i == 0) {
416                         snprintf(buf,256, "0\u00b0");
417                 } else {
418                         snprintf(buf,256, "%d\u00b0", (i * 45));
419                 }
420                 cairo_text_extents(cr, buf, &t_ext);
421                 cairo_move_to(cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
422                 cairo_show_text(cr, buf);
423                 
424                 if (i == 0)
425                         continue;
426                 
427
428                 cairo_set_source_rgba(cr, .8, .9, 0.2, 0.6/(float)i);
429                 cairo_move_to(cr, 0.0,            y);
430                 cairo_line_to(cr, _analysis_width, y);
431
432                 
433                 y = _analysis_height/2.0 + (float)i*(_analysis_height/8.0)*PHASE_PROPORTION;
434
435                 // label
436                 snprintf(buf,256, "-%d\u00b0", (i * 45));
437                 cairo_set_source_rgb(cr, .8, .9, 0.2);
438                 cairo_text_extents(cr, buf, &t_ext);
439                 cairo_move_to(cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
440                 cairo_show_text(cr, buf);
441
442                 // line
443                 cairo_set_source_rgba(cr, .8, .9, 0.2, 0.6/(float)i);
444                 cairo_move_to(cr, 0.0,            y);
445                 cairo_line_to(cr, _analysis_width, y);
446
447                 cairo_set_line_width (cr, 0.25 + 1.0/(float)(i+1));
448                 cairo_stroke(cr);
449         }
450 }
451
452 void 
453 PluginEqGui::plot_impulse_phase(Gtk::Widget *w, cairo_t *cr)
454 {
455         float x,y;
456
457         int prevX = 0;
458         float avgY = 0.0;
459         int avgNum = 0;
460
461         // float width  = w->get_width();
462         float height = w->get_height();
463
464         cairo_set_source_rgba(cr, 0.95, 0.3, 0.2, 1.0);
465         for (uint32_t i = 0; i < _impulse_fft->bins()-1; i++) {
466                 // x coordinate of bin i
467                 x  = log10f(1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
468                 x *= _analysis_width;
469
470                 y  = _analysis_height/2.0 - (_impulse_fft->phase_at_bin(i)/M_PI)*(_analysis_height/2.0)*PHASE_PROPORTION;
471         
472                 if ( i == 0 ) {
473                         cairo_move_to(cr, x, y);
474
475                         avgY = 0;
476                         avgNum = 0;
477                 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
478                         avgY = avgY/(float)avgNum;
479                         if (avgY > (height * 10.0) ) avgY = height * 10.0;
480                         if (avgY < (-height * 10.0) ) avgY = -height * 10.0;
481                         cairo_line_to(cr, prevX, avgY);
482                         //cairo_line_to(cr, prevX, avgY/(float)avgNum);
483
484                         avgY = 0;
485                         avgNum = 0;
486                                 
487                 }       
488
489                 prevX = rint(x);
490                 avgY += y;
491                 avgNum++;
492         }
493
494         cairo_set_line_width (cr, 2.0);
495         cairo_stroke(cr);
496 }
497
498 void
499 PluginEqGui::draw_scales_power(Gtk::Widget *w, cairo_t *cr)
500 {
501         static float scales[] = { 30.0, 70.0, 125.0, 250.0, 500.0, 1000.0, 2000.0, 5000.0, 10000.0, 15000.0, 20000.0, -1.0 };
502         
503         float divisor = _samplerate / 2.0 / _impulse_fft->bins();
504         float x;
505
506         cairo_set_line_width (cr, 1.5);
507         cairo_set_font_size(cr, 9);
508
509         cairo_font_extents_t extents;
510         cairo_font_extents(cr, &extents);
511         // float fontXOffset = extents.descent + 1.0;
512
513         char buf[256];
514
515         for (uint32_t i = 0; scales[i] != -1.0; ++i) {
516                 float bin = scales[i] / divisor;
517
518                 x  = log10f(1.0 + bin / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
519                 x *= _analysis_width;
520
521                 if (scales[i] < 1000.0) {
522                         snprintf(buf, 256, "%0.0f", scales[i]);
523                 } else {
524                         snprintf(buf, 256, "%0.0fk", scales[i]/1000.0);
525                 }
526
527                 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
528
529                 //cairo_move_to(cr, x + fontXOffset, 3.0);
530                 cairo_move_to(cr, x - extents.height, 3.0);
531
532                 cairo_rotate(cr, M_PI / 2.0);
533                 cairo_show_text(cr, buf);
534                 cairo_rotate(cr, -M_PI / 2.0);
535                 cairo_stroke(cr);
536
537                 cairo_set_source_rgb(cr, 0.3, 0.3, 0.3);
538                 cairo_move_to(cr, x, _analysis_height);
539                 cairo_line_to(cr, x, 0.0);
540                 cairo_stroke(cr);
541         }
542
543         float y;
544
545         //double dashes[] = { 1.0, 3.0, 4.5, 3.0 };
546         double dashes[] = { 3.0, 5.0 };
547
548         for (float dB = 0.0; dB < _max_dB; dB += _step_dB ) {
549                 snprintf(buf, 256, "+%0.0f", dB );
550
551                 y  = ( _max_dB - dB) / ( _max_dB - _min_dB );
552                 //std::cerr << " y = " << y << std::endl;
553                 y *= _analysis_height;
554
555                 if (dB != 0.0) {
556                         cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
557                         cairo_move_to(cr, 1.0,     y + extents.height + 1.0);
558                         cairo_show_text(cr, buf);
559                         cairo_stroke(cr);
560                 }
561
562                 cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
563                 cairo_move_to(cr, 0,     y);
564                 cairo_line_to(cr, _analysis_width, y);
565                 cairo_stroke(cr);
566
567                 if (dB == 0.0) {
568                         cairo_set_dash(cr, dashes, 2, 0.0);
569                 }
570         }
571
572         
573         
574         for (float dB = - _step_dB; dB > _min_dB; dB -= _step_dB ) {
575                 snprintf(buf, 256, "%0.0f", dB );
576
577                 y  = ( _max_dB - dB) / ( _max_dB - _min_dB );
578                 y *= _analysis_height;
579
580                 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
581                 cairo_move_to(cr, 1.0,     y - extents.descent - 1.0);
582                 cairo_show_text(cr, buf);
583                 cairo_stroke(cr);
584
585                 cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
586                 cairo_move_to(cr, 0,     y);
587                 cairo_line_to(cr, _analysis_width, y);
588                 cairo_stroke(cr);
589         }
590
591         cairo_set_dash(cr, 0, 0, 0.0);
592
593 }
594
595 inline float
596 power_to_dB(float a)
597 {
598         return 10.0 * log10f(a);
599 }
600
601 void 
602 PluginEqGui::plot_impulse_amplitude(Gtk::Widget *w, cairo_t *cr)
603 {
604         float x,y;
605
606         int prevX = 0;
607         float avgY = 0.0;
608         int avgNum = 0;
609
610         // float width  = w->get_width();
611         float height = w->get_height();
612
613         cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
614         cairo_set_line_width (cr, 2.5);
615
616         for (uint32_t i = 0; i < _impulse_fft->bins()-1; i++) {
617                 // x coordinate of bin i
618                 x  = log10f(1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
619                 x *= _analysis_width;
620
621                 float yCoeff = ( power_to_dB(_impulse_fft->power_at_bin(i)) - _min_dB) / (_max_dB - _min_dB);
622
623                 y = _analysis_height - _analysis_height*yCoeff;
624
625                 if ( i == 0 ) {
626                         cairo_move_to(cr, x, y);
627
628                         avgY = 0;
629                         avgNum = 0;
630                 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
631                         avgY = avgY/(float)avgNum;
632                         if (avgY > (height * 10.0) ) avgY = height * 10.0;
633                         if (avgY < (-height * 10.0) ) avgY = -height * 10.0;
634                         cairo_line_to(cr, prevX, avgY);
635                         //cairo_line_to(cr, prevX, avgY/(float)avgNum);
636
637                         avgY = 0;
638                         avgNum = 0;
639                                 
640                 }
641
642                 prevX = rint(x);
643                 avgY += y;
644                 avgNum++;
645         }
646
647         cairo_stroke(cr);
648 }
649
650 void
651 PluginEqGui::plot_signal_amplitude_difference(Gtk::Widget *w, cairo_t *cr)
652 {
653         float x,y;
654
655         int prevX = 0;
656         float avgY = 0.0;
657         int avgNum = 0;
658
659         // float width  = w->get_width();
660         float height = w->get_height();
661
662         cairo_set_source_rgb(cr, 0.0, 1.0, 0.0);
663         cairo_set_line_width (cr, 2.5);
664
665         for (uint32_t i = 0; i < _signal_input_fft->bins()-1; i++) {
666                 // x coordinate of bin i
667                 x  = log10f(1.0 + (float)i / (float)_signal_input_fft->bins() * _log_coeff) / _log_max;
668                 x *= _analysis_width;
669
670                 float power_out = power_to_dB(_signal_output_fft->power_at_bin(i));
671                 float power_in  = power_to_dB(_signal_input_fft ->power_at_bin(i));
672                 float power = power_out - power_in;
673                 
674                 // for SaBer
675                 /*
676                 double p = 10.0 * log10( 1.0 + (double)_signal_output_fft->power_at_bin(i) - (double)
677  - _signal_input_fft ->power_at_bin(i));
678                 //p *= 1000000.0;
679                 float power = (float)p;
680
681                 if ( (i % 1000) == 0) {
682                         std::cerr << i << ": " << power << std::endl;
683                 }
684                 */
685
686                 if (isinf(power)) {
687                         if (power < 0) {
688                                 power = _min_dB - 1.0;
689                         } else {
690                                 power = _max_dB - 1.0;
691                         }
692                 } else if (isnan(power)) {
693                         power = _min_dB - 1.0;
694                 }
695
696                 float yCoeff = ( power - _min_dB) / (_max_dB - _min_dB);
697
698                 y = _analysis_height - _analysis_height*yCoeff;
699
700                 if ( i == 0 ) {
701                         cairo_move_to(cr, x, y);
702
703                         avgY = 0;
704                         avgNum = 0;
705                 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
706                         avgY = avgY/(float)avgNum;
707                         if (avgY > (height * 10.0) ) avgY = height * 10.0;
708                         if (avgY < (-height * 10.0) ) avgY = -height * 10.0;
709                         cairo_line_to(cr, prevX, avgY);
710
711                         avgY = 0;
712                         avgNum = 0;
713                                 
714                 }
715
716                 prevX = rint(x);
717                 avgY += y;
718                 avgNum++;
719         }
720
721         cairo_stroke(cr);
722
723         
724 }