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