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