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