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