Allow analysis window to plot live pre/post signal analysis
[ardour.git] / gtk2_ardour / plugin_eq_gui.cc
1 /*
2  * Copyright (C) 2018 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2008 Paul Davis
4  * Original Author: Sampo Savolainen
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20
21 #include <algorithm>
22 #include <math.h>
23 #include <iomanip>
24 #include <iostream>
25 #include <sstream>
26
27 #ifdef COMPILER_MSVC
28 # include <float.h>
29 /* isinf() & isnan() are C99 standards, which older MSVC doesn't provide */
30 # define ISINF(val) !((bool)_finite((double)val))
31 # define ISNAN(val) (bool)_isnan((double)val)
32 #else
33 # define ISINF(val) std::isinf((val))
34 # define ISNAN(val) std::isnan((val))
35 #endif
36
37 #include <gtkmm/box.h>
38 #include <gtkmm/button.h>
39 #include <gtkmm/checkbutton.h>
40
41 #include "gtkmm2ext/utils.h"
42
43 #include "ardour/audio_buffer.h"
44 #include "ardour/data_type.h"
45 #include "ardour/chan_mapping.h"
46 #include "ardour/plugin_insert.h"
47 #include "ardour/session.h"
48
49 #include "plugin_eq_gui.h"
50 #include "fft.h"
51 #include "ardour_ui.h"
52 #include "gui_thread.h"
53
54 #include "pbd/i18n.h"
55
56 using namespace ARDOUR;
57
58 PluginEqGui::PluginEqGui (boost::shared_ptr<ARDOUR::PluginInsert> pluginInsert)
59         : _min_dB (-12.0)
60         , _max_dB (+12.0)
61         , _step_dB (3.0)
62         , _block_size (0)
63         , _buffer_size (0)
64         , _signal_buffer_size (0)
65         , _impulse_fft (0)
66         , _signal_input_fft (0)
67         , _signal_output_fft (0)
68         , _plugin_insert (pluginInsert)
69         , _pointer_in_area_xpos (-1)
70 {
71         _signal_analysis_running = false;
72         _samplerate = ARDOUR_UI::instance()->the_session()->sample_rate();
73
74         _log_coeff = (1.0 - 2.0 * (1000.0 / (_samplerate / 2.0))) / powf (1000.0 / (_samplerate / 2.0), 2.0);
75         _log_max = log10f (1 + _log_coeff);
76
77         // Setup analysis drawing area
78         _analysis_scale_surface = 0;
79
80         _analysis_area = new Gtk::DrawingArea();
81         _analysis_width = 256.0;
82         _analysis_height = 256.0;
83         _analysis_area->set_size_request (_analysis_width, _analysis_height);
84
85         _analysis_area->add_events (Gdk::POINTER_MOTION_MASK | Gdk::LEAVE_NOTIFY_MASK | Gdk::BUTTON_PRESS_MASK);
86
87         _analysis_area->signal_expose_event().connect (sigc::mem_fun (*this, &PluginEqGui::expose_analysis_area));
88         _analysis_area->signal_size_allocate().connect (sigc::mem_fun (*this, &PluginEqGui::resize_analysis_area));
89         _analysis_area->signal_motion_notify_event().connect (sigc::mem_fun (*this, &PluginEqGui::analysis_area_mouseover));
90         _analysis_area->signal_leave_notify_event().connect (sigc::mem_fun (*this, &PluginEqGui::analysis_area_mouseexit));
91
92         // dB selection
93         dBScaleModel = Gtk::ListStore::create (dBColumns);
94
95         dBScaleCombo = new Gtk::ComboBox (dBScaleModel, false);
96
97 #define ADD_DB_ROW(MIN,MAX,STEP,NAME) \
98         { \
99                 Gtk::TreeModel::Row row = *(dBScaleModel->append()); \
100                 row[dBColumns.dBMin]  = (MIN); \
101                 row[dBColumns.dBMax]  = (MAX); \
102                 row[dBColumns.dBStep] = (STEP); \
103                 row[dBColumns.name]   = NAME; \
104         }
105
106         ADD_DB_ROW( -6,  +6, 1, "-6dB .. +6dB");
107         ADD_DB_ROW(-12, +12, 3, "-12dB .. +12dB");
108         ADD_DB_ROW(-24, +24, 5, "-24dB .. +24dB");
109         ADD_DB_ROW(-36, +36, 6, "-36dB .. +36dB");
110         ADD_DB_ROW(-64, +64,12, "-64dB .. +64dB");
111
112 #undef ADD_DB_ROW
113
114         dBScaleCombo -> pack_start(dBColumns.name);
115         dBScaleCombo -> set_active(1);
116
117         dBScaleCombo -> signal_changed().connect (sigc::mem_fun(*this, &PluginEqGui::change_dB_scale));
118
119         Gtk::Label *dBComboLabel = new Gtk::Label (_("Range:"));
120
121         Gtk::HBox *dBSelectBin = new Gtk::HBox (false, 4);
122         dBSelectBin->add (*manage(dBComboLabel));
123         dBSelectBin->add (*manage(dBScaleCombo));
124
125         _live_signal_combo = new Gtk::ComboBoxText ();
126         _live_signal_combo->append_text (_("Off"));
127         _live_signal_combo->append_text (_("Output / Input"));
128         _live_signal_combo->append_text (_("Input"));
129         _live_signal_combo->append_text (_("Output"));
130         _live_signal_combo->append_text (_("Input +40dB"));
131         _live_signal_combo->append_text (_("Output +40dB"));
132         _live_signal_combo->set_active (0);
133
134         Gtk::Label *live_signal_label = new Gtk::Label (_("Live signal:"));
135
136         Gtk::HBox *liveSelectBin = new Gtk::HBox (false, 4);
137         liveSelectBin->add (*manage(live_signal_label));
138         liveSelectBin->add (*manage(_live_signal_combo));
139
140         // Phase checkbutton
141         _phase_button = new Gtk::CheckButton (_("Show phase"));
142         _phase_button->set_active (true);
143         _phase_button->signal_toggled().connect (sigc::mem_fun(*this, &PluginEqGui::redraw_scales));
144
145         // Freq/dB info for mouse over
146         _pointer_info = new Gtk::Label ("", 1, 0.5);
147         _pointer_info->set_name ("PluginAnalysisInfoLabel");
148         Gtkmm2ext::set_size_request_to_display_given_text (*_pointer_info, "10.0kHz_000.0dB_180.0\u00B0", 0, 0);
149
150         // populate table
151         attach (*manage(_analysis_area), 0, 4, 0, 1);
152         attach (*manage(dBSelectBin),    0, 1, 1, 2, Gtk::SHRINK, Gtk::SHRINK);
153         attach (*manage(liveSelectBin),  1, 2, 1, 2, Gtk::SHRINK, Gtk::SHRINK, 4, 0);
154         attach (*manage(_phase_button),  2, 3, 1, 2, Gtk::SHRINK, Gtk::SHRINK, 4, 0);
155         attach (*manage(_pointer_info),  3, 4, 1, 2, Gtk::FILL,   Gtk::SHRINK);
156 }
157
158 PluginEqGui::~PluginEqGui ()
159 {
160         stop_listening ();
161
162         if (_analysis_scale_surface) {
163                 cairo_surface_destroy (_analysis_scale_surface);
164         }
165
166         delete _impulse_fft;
167         _impulse_fft = 0;
168         delete _signal_input_fft;
169         _signal_input_fft = 0;
170         delete _signal_output_fft;
171         _signal_output_fft = 0;
172
173         // all gui objects are *manage'd by the inherited Table object
174 }
175
176 static inline float
177 power_to_dB (float a)
178 {
179         return 10.0 * log10f (a);
180 }
181
182 void
183 PluginEqGui::start_listening ()
184 {
185         if (!_plugin) {
186                 _plugin = _plugin_insert->get_impulse_analysis_plugin ();
187         }
188
189         _plugin->activate ();
190         set_buffer_size (8192, 16384);
191         _block_size = 0; // re-initialize the plugin next time.
192
193         /* Connect the realtime signal collection callback */
194         _plugin_insert->AnalysisDataGathered.connect (analysis_connection, invalidator (*this), boost::bind (&PluginEqGui::signal_collect_callback, this, _1, _2), gui_context());
195 }
196
197 void
198 PluginEqGui::stop_listening ()
199 {
200         analysis_connection.disconnect ();
201         _plugin->deactivate ();
202 }
203
204 void
205 PluginEqGui::on_hide ()
206 {
207         stop_updating ();
208         Gtk::Table::on_hide ();
209 }
210
211 void
212 PluginEqGui::stop_updating ()
213 {
214         if (_update_connection.connected ()) {
215                 _update_connection.disconnect ();
216         }
217         _signal_analysis_running = false;
218 }
219
220 void
221 PluginEqGui::start_updating ()
222 {
223         if (!_update_connection.connected() && is_visible()) {
224                 _update_connection = Glib::signal_timeout().connect (sigc::mem_fun (this, &PluginEqGui::timeout_callback), 250);
225         }
226 }
227
228 void
229 PluginEqGui::on_show ()
230 {
231         Gtk::Table::on_show ();
232
233         start_updating ();
234
235         Gtk::Widget *toplevel = get_toplevel ();
236         if (toplevel) {
237                 if (!_window_unmap_connection.connected ()) {
238                         _window_unmap_connection = toplevel->signal_unmap().connect (sigc::mem_fun (this, &PluginEqGui::stop_updating));
239                 }
240
241                 if (!_window_map_connection.connected ()) {
242                         _window_map_connection = toplevel->signal_map().connect (sigc::mem_fun (this, &PluginEqGui::start_updating));
243                 }
244         }
245 }
246
247 void
248 PluginEqGui::change_dB_scale ()
249 {
250         Gtk::TreeModel::iterator iter = dBScaleCombo -> get_active ();
251
252         Gtk::TreeModel::Row row;
253
254         if (iter && (row = *iter)) {
255                 _min_dB = row[dBColumns.dBMin];
256                 _max_dB = row[dBColumns.dBMax];
257                 _step_dB = row[dBColumns.dBStep];
258
259                 redraw_scales ();
260         }
261 }
262
263 void
264 PluginEqGui::redraw_scales ()
265 {
266
267         if (_analysis_scale_surface) {
268                 cairo_surface_destroy (_analysis_scale_surface);
269                 _analysis_scale_surface = 0;
270         }
271
272         _analysis_area->queue_draw ();
273
274         // TODO: Add graph legend!
275 }
276
277 void
278 PluginEqGui::set_buffer_size (uint32_t size, uint32_t signal_size)
279 {
280         if (_buffer_size == size && _signal_buffer_size == signal_size) {
281                 return;
282         }
283
284         GTKArdour::FFT *tmp1 = _impulse_fft;
285         GTKArdour::FFT *tmp2 = _signal_input_fft;
286         GTKArdour::FFT *tmp3 = _signal_output_fft;
287
288         try {
289                 _impulse_fft       = new GTKArdour::FFT (size);
290                 _signal_input_fft  = new GTKArdour::FFT (signal_size);
291                 _signal_output_fft = new GTKArdour::FFT (signal_size);
292         } catch (...) {
293                 // Don't care about lost memory, we're screwed anyhow
294                 _impulse_fft       = tmp1;
295                 _signal_input_fft  = tmp2;
296                 _signal_output_fft = tmp3;
297                 throw;
298         }
299
300         delete tmp1;
301         delete tmp2;
302         delete tmp3;
303
304         _buffer_size = size;
305         _signal_buffer_size = signal_size;
306
307         /* allocate separate in+out buffers, VST cannot process in-place */
308         ARDOUR::ChanCount acount (_plugin->get_info()->n_inputs + _plugin->get_info()->n_outputs);
309         ARDOUR::ChanCount ccount = ARDOUR::ChanCount::max (_plugin->get_info()->n_inputs, _plugin->get_info()->n_outputs);
310
311         for (ARDOUR::DataType::iterator i = ARDOUR::DataType::begin(); i != ARDOUR::DataType::end(); ++i) {
312                 _bufferset.ensure_buffers (*i, acount.get (*i), _buffer_size);
313                 _collect_bufferset.ensure_buffers (*i, ccount.get (*i), _buffer_size);
314         }
315
316         _bufferset.set_count (acount);
317         _collect_bufferset.set_count (ccount);
318 }
319
320 void
321 PluginEqGui::resize_analysis_area (Gtk::Allocation& size)
322 {
323         _analysis_width  = (float)size.get_width();
324         _analysis_height = (float)size.get_height();
325
326         if (_analysis_scale_surface) {
327                 cairo_surface_destroy (_analysis_scale_surface);
328                 _analysis_scale_surface = 0;
329         }
330 }
331
332 bool
333 PluginEqGui::timeout_callback ()
334 {
335         if (!_signal_analysis_running) {
336                 _signal_analysis_running = true;
337                 _plugin_insert -> collect_signal_for_analysis (_signal_buffer_size);
338         }
339
340         run_impulse_analysis ();
341         return true;
342 }
343
344 void
345 PluginEqGui::signal_collect_callback (ARDOUR::BufferSet* in, ARDOUR::BufferSet* out)
346 {
347         ENSURE_GUI_THREAD (*this, &PluginEqGui::signal_collect_callback, in, out);
348
349         _signal_input_fft ->reset ();
350         _signal_output_fft->reset ();
351
352         for (uint32_t i = 0; i < _plugin_insert->input_streams().n_audio(); ++i) {
353                 _signal_input_fft ->analyze (in ->get_audio (i).data(), GTKArdour::FFT::HANN);
354         }
355
356         for (uint32_t i = 0; i < _plugin_insert->output_streams().n_audio(); ++i) {
357                 _signal_output_fft->analyze (out->get_audio (i).data(), GTKArdour::FFT::HANN);
358         }
359
360         _signal_input_fft ->calculate ();
361         _signal_output_fft->calculate ();
362
363         _signal_analysis_running = false;
364         _analysis_area->queue_draw ();
365 }
366
367 void
368 PluginEqGui::run_impulse_analysis ()
369 {
370         /* Allocate some thread-local buffers so that Plugin::connect_and_run can use them */
371         ARDOUR_UI::instance()->get_process_buffers ();
372
373         uint32_t inputs  = _plugin->get_info()->n_inputs.n_audio();
374         uint32_t outputs = _plugin->get_info()->n_outputs.n_audio();
375
376         /* Create the impulse, can't use silence() because consecutive calls won't work */
377         for (uint32_t i = 0; i < inputs; ++i) {
378                 ARDOUR::AudioBuffer& buf = _bufferset.get_audio (i);
379                 ARDOUR::Sample* d = buf.data ();
380                 memset (d, 0, sizeof (ARDOUR::Sample) * _buffer_size);
381                 *d = 1.0;
382         }
383
384         /* Silence collect buffers to copy data to */
385         for (uint32_t i = 0; i < outputs; ++i) {
386                 ARDOUR::AudioBuffer &buf = _collect_bufferset.get_audio (i);
387                 ARDOUR::Sample *d = buf.data ();
388                 memset (d, 0, sizeof (ARDOUR::Sample) * _buffer_size);
389         }
390
391         /* create default linear I/O maps */
392         ARDOUR::ChanMapping in_map (_plugin->get_info()->n_inputs);
393         ARDOUR::ChanMapping out_map (_plugin->get_info()->n_outputs);
394         /* map output buffers after input buffers (no inplace for VST) */
395         out_map.offset_to (DataType::AUDIO, inputs);
396
397         /* run at most at session's block size chunks.
398          *
399          * This is important since VSTs may call audioMasterGetBlockSize
400          * or access various other /real/ session paramaters using the
401          * audioMasterCallback
402          */
403         samplecnt_t block_size = ARDOUR_UI::instance()->the_session()->get_block_size();
404         if (_block_size != block_size) {
405                 _block_size = block_size;
406                 _plugin->set_block_size (block_size);
407         }
408
409         samplepos_t sample_pos = 0;
410         samplecnt_t latency = _plugin->signal_latency ();
411         samplecnt_t samples_remain = _buffer_size + latency;
412
413         _impulse_fft->reset ();
414
415         while (samples_remain > 0) {
416
417                 samplecnt_t n_samples = std::min (samples_remain, block_size);
418                 _plugin->connect_and_run (_bufferset, sample_pos, sample_pos + n_samples, 1.0, in_map, out_map, n_samples, 0);
419                 samples_remain -= n_samples;
420
421                 /* zero input buffers */
422                 if (sample_pos == 0 && samples_remain > 0) {
423                         for (uint32_t i = 0; i < inputs; ++i) {
424                                 _bufferset.get_audio (i).data()[0] = 0.f;
425                         }
426                 }
427
428 #ifndef NDEBUG
429                 if (samples_remain > 0) {
430                         for (uint32_t i = 0; i < inputs; ++i) {
431                                 pframes_t unused;
432                                 assert (_bufferset.get_audio (i).check_silence (block_size, unused));
433                         }
434                 }
435 #endif
436
437                 if (sample_pos + n_samples > latency) {
438                         samplecnt_t dst_off = sample_pos >= latency ? sample_pos - latency : 0;
439                         samplecnt_t src_off = sample_pos >= latency ? 0 : latency - sample_pos;
440                         samplecnt_t n_copy = std::min (n_samples, sample_pos + n_samples - latency);
441
442                         assert (dst_off + n_copy <= _buffer_size);
443                         assert (src_off + n_copy <= _block_size);
444
445                         for (uint32_t i = 0; i < outputs; ++i) {
446                                 memcpy (
447                                                 &(_collect_bufferset.get_audio (i).data()[dst_off]),
448                                                 &(_bufferset.get_audio (inputs + i).data()[src_off]),
449                                                 n_copy * sizeof (float));
450                         }
451                 }
452
453                 sample_pos += n_samples;
454         }
455
456         for (uint32_t i = 0; i < outputs; ++i) {
457                 _impulse_fft->analyze (_collect_bufferset.get_audio (i).data());
458         }
459         _impulse_fft->calculate ();
460
461         _analysis_area->queue_draw ();
462
463         ARDOUR_UI::instance ()->drop_process_buffers ();
464 }
465
466 void
467 PluginEqGui::update_pointer_info( float x)
468 {
469         /* find the bin corresponding to x (see plot_impulse_amplitude) */
470         int i = roundf ((powf (10, _log_max * x / _analysis_width) - 1.0) * _impulse_fft->bins() / _log_coeff);
471         float dB = power_to_dB (_impulse_fft->power_at_bin (i));
472         /* calc freq corresponding to bin */
473         const int freq = std::max (1, (int) roundf ((float)i / (float)_impulse_fft->bins() * _samplerate / 2.f));
474
475         _pointer_in_area_freq = round (_analysis_width * log10f (1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max);
476
477         std::stringstream ss;
478         ss << std::fixed;
479         if (freq >= 10000) {
480                 ss <<  std::setprecision (1) << freq / 1000.0 << "kHz";
481         } else if (freq >= 1000) {
482                 ss <<  std::setprecision (2) << freq / 1000.0 << "kHz";
483         } else {
484                 ss <<  std::setprecision (0) << freq << "Hz";
485         }
486         ss << " " << std::setw (6) << std::setprecision (1) << std::showpos << dB;
487         ss << std::setw (0) << "dB";
488
489         if (_phase_button->get_active ()) {
490                 float phase = 180. * _impulse_fft->phase_at_bin (i) / M_PI;
491                 ss << " " << std::setw (6) << std::setprecision (1) << std::showpos << phase;
492                 ss << std::setw (0) << "\u00B0";
493         }
494         _pointer_info->set_text (ss.str());
495 }
496
497 bool
498 PluginEqGui::analysis_area_mouseover (GdkEventMotion *event)
499 {
500         update_pointer_info (event->x);
501         _pointer_in_area_xpos = event->x;
502         _analysis_area->queue_draw ();
503         return true;
504 }
505
506 bool
507 PluginEqGui::analysis_area_mouseexit (GdkEventCrossing *)
508 {
509         _pointer_info->set_text ("");
510         _pointer_in_area_xpos = -1;
511         _analysis_area->queue_draw ();
512         return true;
513 }
514
515 bool
516 PluginEqGui::expose_analysis_area (GdkEventExpose *)
517 {
518         redraw_analysis_area ();
519         return true;
520 }
521
522 void
523 PluginEqGui::draw_analysis_scales (cairo_t *ref_cr)
524 {
525         // TODO: check whether we need rounding
526         _analysis_scale_surface = cairo_surface_create_similar (cairo_get_target (ref_cr),
527                         CAIRO_CONTENT_COLOR,
528                         _analysis_width,
529                         _analysis_height);
530
531         cairo_t *cr = cairo_create (_analysis_scale_surface);
532
533         cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
534         cairo_rectangle (cr, 0.0, 0.0, _analysis_width, _analysis_height);
535         cairo_fill (cr);
536
537         draw_scales_power (_analysis_area, cr);
538
539         if (_phase_button->get_active ()) {
540                 draw_scales_phase (_analysis_area, cr);
541         }
542
543         cairo_destroy (cr);
544 }
545
546 void
547 PluginEqGui::redraw_analysis_area ()
548 {
549         cairo_t *cr;
550
551         cr = gdk_cairo_create (GDK_DRAWABLE(_analysis_area->get_window()->gobj()));
552
553         if (_analysis_scale_surface == 0) {
554                 draw_analysis_scales (cr);
555         }
556
557         cairo_copy_page (cr);
558
559         cairo_set_source_surface (cr, _analysis_scale_surface, 0.0, 0.0);
560         cairo_paint (cr);
561
562         cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
563
564         if (_phase_button->get_active()) {
565                 plot_impulse_phase (_analysis_area, cr);
566         }
567
568         plot_impulse_amplitude (_analysis_area, cr);
569
570         if (_pointer_in_area_xpos >= 0) {
571                 update_pointer_info (_pointer_in_area_xpos);
572         }
573
574         if (_live_signal_combo->get_active_row_number() > 0) {
575                 plot_signal_amplitude_difference (_analysis_area, cr);
576         }
577
578         if (_pointer_in_area_xpos >= 0 && _pointer_in_area_freq > 0) {
579                 const double dashed[] = {0.0, 2.0};
580                 cairo_set_dash (cr, dashed, 2, 0);
581                 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
582                 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
583                 cairo_set_line_width (cr, 1.0);
584                 cairo_move_to (cr, _pointer_in_area_freq - .5, -.5);
585                 cairo_line_to (cr, _pointer_in_area_freq - .5, _analysis_height - .5);
586                 cairo_stroke (cr);
587         }
588
589         cairo_destroy (cr);
590 }
591
592 #define PHASE_PROPORTION 0.5
593
594 void
595 PluginEqGui::draw_scales_phase (Gtk::Widget*, cairo_t *cr)
596 {
597         float y;
598         cairo_font_extents_t extents;
599         cairo_font_extents (cr, &extents);
600
601         char buf[256];
602         cairo_text_extents_t t_ext;
603
604         for (uint32_t i = 0; i < 5; i++) {
605
606                 y = _analysis_height / 2.0 - (float)i * (_analysis_height / 8.0) * PHASE_PROPORTION;
607
608                 cairo_set_source_rgb (cr, .8, .9, 0.2);
609                 if (i == 0) {
610                         snprintf (buf,256, "0\u00b0");
611                 } else {
612                         snprintf (buf,256, "%d\u00b0", (i * 45));
613                 }
614                 cairo_text_extents (cr, buf, &t_ext);
615                 cairo_move_to (cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
616                 cairo_show_text (cr, buf);
617
618                 if (i == 0) {
619                         continue;
620                 }
621
622                 y = roundf (y) - .5;
623
624                 cairo_set_source_rgba (cr, .8, .9, .2, 0.4);
625                 cairo_move_to (cr, 0.0,             y);
626                 cairo_line_to (cr, _analysis_width, y);
627                 cairo_set_line_width (cr, 1);
628                 cairo_stroke (cr);
629
630                 y = _analysis_height / 2.0 + (float)i * (_analysis_height / 8.0) * PHASE_PROPORTION;
631
632                 // label
633                 snprintf (buf,256, "-%d\u00b0", (i * 45));
634                 cairo_set_source_rgb (cr, .8, .9, 0.2);
635                 cairo_text_extents (cr, buf, &t_ext);
636                 cairo_move_to (cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
637                 cairo_show_text (cr, buf);
638
639                 y = roundf (y) - .5;
640                 // line
641                 cairo_set_source_rgba (cr, .8, .9, .2, 0.4);
642                 cairo_move_to (cr, 0.0,             y);
643                 cairo_line_to (cr, _analysis_width, y);
644
645                 cairo_set_line_width (cr, 1);
646                 cairo_stroke (cr);
647         }
648 }
649
650 void
651 PluginEqGui::plot_impulse_phase (Gtk::Widget *w, cairo_t *cr)
652 {
653         float x,y;
654
655         int prevX = 0;
656         float avgY = 0.0;
657         int avgNum = 0;
658
659         // float width  = w->get_width();
660         float height = w->get_height ();
661         float analysis_height_2 = _analysis_height / 2.f;
662
663         cairo_set_source_rgba (cr, 0.95, 0.3, 0.2, 1.0);
664         for (uint32_t i = 0; i < _impulse_fft->bins() - 1; ++i) {
665                 // x coordinate of bin i
666                 x  = log10f (1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
667                 x *= _analysis_width;
668                 y  = analysis_height_2 - (_impulse_fft->phase_at_bin (i) / M_PI) * analysis_height_2 * PHASE_PROPORTION;
669
670                 if (i == 0) {
671                         cairo_move_to (cr, x, y);
672                         avgY = 0;
673                         avgNum = 0;
674                 } else if (rint (x) > prevX || i == _impulse_fft->bins() - 1) {
675                         avgY = avgY / (float)avgNum;
676                         if (avgY > (height * 10.0)) {
677                                 avgY = height * 10.0;
678                         }
679                         if (avgY < (-height * 10.0)) {
680                                 avgY = -height * 10.0;
681                         }
682
683                         cairo_line_to (cr, prevX, avgY);
684
685                         avgY = 0;
686                         avgNum = 0;
687                 }
688
689                 prevX = rint (x);
690                 avgY += y;
691                 avgNum++;
692         }
693
694         cairo_set_line_width (cr, 2.0);
695         cairo_stroke (cr);
696 }
697
698 void
699 PluginEqGui::draw_scales_power (Gtk::Widget */*w*/, cairo_t *cr)
700 {
701         if (_impulse_fft == 0) {
702                 return;
703         }
704
705         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 };
706         float divisor = _samplerate / 2.0 / _impulse_fft->bins();
707         float x;
708
709         cairo_set_line_width (cr, 1.5);
710         cairo_set_font_size (cr, 9);
711
712         cairo_font_extents_t extents;
713         cairo_font_extents (cr, &extents);
714         // float fontXOffset = extents.descent + 1.0;
715
716         char buf[256];
717
718         for (uint32_t i = 0; scales[i] != -1.0; ++i) {
719                 float bin = scales[i] / divisor;
720
721                 x  = log10f (1.0 + bin / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
722                 x *= _analysis_width;
723
724                 if (scales[i] < 1000.0) {
725                         snprintf (buf, 256, "%0.0f", scales[i]);
726                 } else {
727                         snprintf (buf, 256, "%0.0fk", scales[i]/1000.0);
728                 }
729
730                 cairo_set_source_rgb (cr, 0.4, 0.4, 0.4);
731
732                 cairo_move_to (cr, x - extents.height, 3.0);
733
734                 cairo_rotate (cr, M_PI / 2.0);
735                 cairo_show_text (cr, buf);
736                 cairo_rotate (cr, -M_PI / 2.0);
737                 cairo_stroke (cr);
738
739                 cairo_set_source_rgb (cr, 0.3, 0.3, 0.3);
740                 cairo_move_to (cr, x, _analysis_height);
741                 cairo_line_to (cr, x, 0.0);
742                 cairo_stroke (cr);
743         }
744
745         float y;
746
747         //double dashes[] = { 1.0, 3.0, 4.5, 3.0 };
748         double dashes[] = { 3.0, 5.0 };
749
750         for (float dB = 0.0; dB < _max_dB; dB += _step_dB) {
751                 snprintf (buf, 256, "+%0.0f", dB);
752
753                 y  = (_max_dB - dB) / (_max_dB - _min_dB);
754                 //std::cerr << " y = " << y << std::endl;
755                 y *= _analysis_height;
756
757                 if (dB != 0.0) {
758                         cairo_set_source_rgb (cr, 0.4, 0.4, 0.4);
759                         cairo_move_to (cr, 1.0,     y + extents.height + 1.0);
760                         cairo_show_text (cr, buf);
761                         cairo_stroke (cr);
762                 }
763
764                 cairo_set_source_rgb (cr, 0.2, 0.2, 0.2);
765                 cairo_move_to (cr, 0,               y);
766                 cairo_line_to (cr, _analysis_width, y);
767                 cairo_stroke (cr);
768
769                 if (dB == 0.0) {
770                         cairo_set_dash (cr, dashes, 2, 0.0);
771                 }
772         }
773
774         for (float dB = - _step_dB; dB > _min_dB; dB -= _step_dB) {
775                 snprintf (buf, 256, "%0.0f", dB);
776
777                 y  = (_max_dB - dB) / (_max_dB - _min_dB);
778                 y *= _analysis_height;
779
780                 cairo_set_source_rgb (cr, 0.4, 0.4, 0.4);
781                 cairo_move_to (cr, 1.0, y - extents.descent - 1.0);
782                 cairo_show_text (cr, buf);
783                 cairo_stroke (cr);
784
785                 cairo_set_source_rgb (cr, 0.2, 0.2, 0.2);
786                 cairo_move_to (cr, 0,               y);
787                 cairo_line_to (cr, _analysis_width, y);
788                 cairo_stroke (cr);
789         }
790
791         cairo_set_dash (cr, 0, 0, 0.0);
792 }
793
794 void
795 PluginEqGui::plot_impulse_amplitude (Gtk::Widget *w, cairo_t *cr)
796 {
797         float x,y;
798         int prevX = 0;
799         float avgY = 0.0;
800         int avgNum = 0;
801
802         // float width  = w->get_width();
803         float height = w->get_height ();
804
805         cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
806         cairo_set_line_width (cr, 2.5);
807
808         for (uint32_t i = 0; i < _impulse_fft->bins() - 1; ++i) {
809                 // x coordinate of bin i
810                 x  = log10f (1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
811                 x *= _analysis_width;
812
813                 float yCoeff = (power_to_dB (_impulse_fft->power_at_bin (i)) - _min_dB) / (_max_dB - _min_dB);
814
815                 y = _analysis_height - _analysis_height * yCoeff;
816
817                 if (i == 0) {
818                         cairo_move_to (cr, x, y);
819                         avgY = 0;
820                         avgNum = 0;
821                 } else if (rint (x) > prevX || i == _impulse_fft->bins() - 1) {
822                         avgY = avgY / (float)avgNum;
823                         if (avgY > (height * 10.0)) {
824                                 avgY = height * 10.0;
825                         }
826                         if (avgY < (-height * 10.0)) {
827                                 avgY = -height * 10.0;
828                         }
829                         cairo_line_to (cr, prevX, avgY);
830
831                         avgY = 0;
832                         avgNum = 0;
833                 }
834
835                 prevX = rint (x);
836                 avgY += y;
837                 avgNum++;
838         }
839
840         cairo_stroke (cr);
841 }
842
843 void
844 PluginEqGui::plot_signal_amplitude_difference (Gtk::Widget *w, cairo_t *cr)
845 {
846         float x,y;
847
848         int prevX = 0;
849         float avgY = 0.0;
850         int avgNum = 0;
851
852         float height = w->get_height();
853
854         cairo_set_source_rgb (cr, 0.0, 1.0, 0.0);
855         cairo_set_line_width (cr, 1.5);
856
857         for (uint32_t i = 0; i < _signal_input_fft->bins() - 1; ++i) {
858                 // x coordinate of bin i
859                 x  = log10f (1.0 + (float)i / (float)_signal_input_fft->bins() * _log_coeff) / _log_max;
860                 x *= _analysis_width;
861
862                 float power_out = _signal_output_fft->power_at_bin (i) + 1e-30;
863                 float power_in  = _signal_input_fft ->power_at_bin (i) + 1e-30;
864                 float power;
865                 switch (_live_signal_combo->get_active_row_number()) {
866                         case 2:
867                                 power = power_to_dB (power_in);
868                                 break;
869                         case 3:
870                                 power = power_to_dB (power_out);
871                                 break;
872                         case 4:
873                                 power = power_to_dB (power_in) + 40;
874                                 break;
875                         case 5:
876                                 power = power_to_dB (power_out) + 40;
877                                 break;
878                         default:
879                                 power = power_to_dB (power_out / power_in);
880                                 break;
881                 }
882
883                 assert (!ISINF(power));
884                 assert (!ISNAN(power));
885
886                 float yCoeff = (power - _min_dB) / (_max_dB - _min_dB);
887
888                 y = _analysis_height - _analysis_height*yCoeff;
889
890                 if (i == 0) {
891                         cairo_move_to (cr, x, y);
892
893                         avgY = 0;
894                         avgNum = 0;
895                 } else if (rint (x) > prevX || i == _impulse_fft->bins() - 1) {
896                         avgY = avgY / (float)avgNum;
897                         if (avgY > (height * 10.0)) {
898                                 avgY = height * 10.0;
899                         }
900                         if (avgY < (-height * 10.0)) {
901                                 avgY = -height * 10.0;
902                         }
903                         cairo_line_to (cr, prevX, avgY);
904
905                         avgY = 0;
906                         avgNum = 0;
907
908                 }
909
910                 prevX = rint (x);
911                 avgY += y;
912                 avgNum++;
913         }
914
915         cairo_stroke (cr);
916 }