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