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