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