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