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