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