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