Prevent spinlock copy construction
[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_insert->effective_latency ();
411         samplecnt_t samples_remain = _buffer_size + latency;
412
413         /* Note: https://discourse.ardour.org/t/plugins-ladspa-questions/101292/15
414          * Capture the complete response from the beginning, and more than "latency" samples,
415          * Then unwrap the phase-response corresponding to reported latency, leaving the
416          * magnitude unchanged.
417          */
418
419         _impulse_fft->reset ();
420
421         while (samples_remain > 0) {
422
423                 samplecnt_t n_samples = std::min (samples_remain, block_size);
424                 _plugin->connect_and_run (_bufferset, sample_pos, sample_pos + n_samples, 1.0, in_map, out_map, n_samples, 0);
425                 samples_remain -= n_samples;
426
427                 /* zero input buffers */
428                 if (sample_pos == 0 && samples_remain > 0) {
429                         for (uint32_t i = 0; i < inputs; ++i) {
430                                 _bufferset.get_audio (i).data()[0] = 0.f;
431                         }
432                 }
433
434 #ifndef NDEBUG
435                 if (samples_remain > 0) {
436                         for (uint32_t i = 0; i < inputs; ++i) {
437                                 pframes_t unused;
438                                 assert (_bufferset.get_audio (i).check_silence (block_size, unused));
439                         }
440                 }
441 #endif
442
443                 if (sample_pos + n_samples > latency) {
444                         samplecnt_t dst_off = sample_pos >= latency ? sample_pos - latency : 0;
445                         samplecnt_t src_off = sample_pos >= latency ? 0 : latency - sample_pos;
446                         samplecnt_t n_copy = std::min (n_samples, sample_pos + n_samples - latency);
447
448                         assert (dst_off + n_copy <= _buffer_size);
449                         assert (src_off + n_copy <= _block_size);
450
451                         for (uint32_t i = 0; i < outputs; ++i) {
452                                 memcpy (
453                                                 &(_collect_bufferset.get_audio (i).data()[dst_off]),
454                                                 &(_bufferset.get_audio (inputs + i).data()[src_off]),
455                                                 n_copy * sizeof (float));
456                         }
457                 }
458
459                 sample_pos += n_samples;
460         }
461
462         for (uint32_t i = 0; i < outputs; ++i) {
463                 _impulse_fft->analyze (_collect_bufferset.get_audio (i).data());
464         }
465         _impulse_fft->calculate ();
466
467         _analysis_area->queue_draw ();
468
469         ARDOUR_UI::instance ()->drop_process_buffers ();
470 }
471
472 void
473 PluginEqGui::update_pointer_info( float x)
474 {
475         /* find the bin corresponding to x (see plot_impulse_amplitude) */
476         int i = roundf ((powf (10, _log_max * x / _analysis_width) - 1.0) * _impulse_fft->bins() / _log_coeff);
477         float dB = power_to_dB (_impulse_fft->power_at_bin (i));
478         /* calc freq corresponding to bin */
479         const int freq = std::max (1, (int) roundf ((float)i / (float)_impulse_fft->bins() * _samplerate / 2.f));
480
481         _pointer_in_area_freq = round (_analysis_width * log10f (1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max);
482
483         std::stringstream ss;
484         ss << std::fixed;
485         if (freq >= 10000) {
486                 ss <<  std::setprecision (1) << freq / 1000.0 << "kHz";
487         } else if (freq >= 1000) {
488                 ss <<  std::setprecision (2) << freq / 1000.0 << "kHz";
489         } else {
490                 ss <<  std::setprecision (0) << freq << "Hz";
491         }
492         ss << " " << std::setw (6) << std::setprecision (1) << std::showpos << dB;
493         ss << std::setw (0) << "dB";
494
495         if (_phase_button->get_active ()) {
496                 float phase = 180. * _impulse_fft->phase_at_bin (i) / M_PI;
497                 ss << " " << std::setw (6) << std::setprecision (1) << std::showpos << phase;
498                 ss << std::setw (0) << "\u00B0";
499         }
500         _pointer_info->set_text (ss.str());
501 }
502
503 bool
504 PluginEqGui::analysis_area_mouseover (GdkEventMotion *event)
505 {
506         update_pointer_info (event->x);
507         _pointer_in_area_xpos = event->x;
508         _analysis_area->queue_draw ();
509         return true;
510 }
511
512 bool
513 PluginEqGui::analysis_area_mouseexit (GdkEventCrossing *)
514 {
515         _pointer_info->set_text ("");
516         _pointer_in_area_xpos = -1;
517         _analysis_area->queue_draw ();
518         return true;
519 }
520
521 bool
522 PluginEqGui::expose_analysis_area (GdkEventExpose *)
523 {
524         redraw_analysis_area ();
525         return true;
526 }
527
528 void
529 PluginEqGui::draw_analysis_scales (cairo_t *ref_cr)
530 {
531         // TODO: check whether we need rounding
532         _analysis_scale_surface = cairo_surface_create_similar (cairo_get_target (ref_cr),
533                         CAIRO_CONTENT_COLOR,
534                         _analysis_width,
535                         _analysis_height);
536
537         cairo_t *cr = cairo_create (_analysis_scale_surface);
538
539         cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
540         cairo_rectangle (cr, 0.0, 0.0, _analysis_width, _analysis_height);
541         cairo_fill (cr);
542
543         draw_scales_power (_analysis_area, cr);
544
545         if (_phase_button->get_active ()) {
546                 draw_scales_phase (_analysis_area, cr);
547         }
548
549         cairo_destroy (cr);
550 }
551
552 void
553 PluginEqGui::redraw_analysis_area ()
554 {
555         cairo_t *cr;
556
557         cr = gdk_cairo_create (GDK_DRAWABLE(_analysis_area->get_window()->gobj()));
558
559         if (_analysis_scale_surface == 0) {
560                 draw_analysis_scales (cr);
561         }
562
563         cairo_copy_page (cr);
564
565         cairo_set_source_surface (cr, _analysis_scale_surface, 0.0, 0.0);
566         cairo_paint (cr);
567
568         cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
569
570         if (_phase_button->get_active()) {
571                 plot_impulse_phase (_analysis_area, cr);
572         }
573
574         plot_impulse_amplitude (_analysis_area, cr);
575
576         if (_pointer_in_area_xpos >= 0) {
577                 update_pointer_info (_pointer_in_area_xpos);
578         }
579
580         if (_live_signal_combo->get_active_row_number() > 0) {
581                 plot_signal_amplitude_difference (_analysis_area, cr);
582         }
583
584         if (_pointer_in_area_xpos >= 0 && _pointer_in_area_freq > 0) {
585                 const double dashed[] = {0.0, 2.0};
586                 cairo_set_dash (cr, dashed, 2, 0);
587                 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
588                 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
589                 cairo_set_line_width (cr, 1.0);
590                 cairo_move_to (cr, _pointer_in_area_freq - .5, -.5);
591                 cairo_line_to (cr, _pointer_in_area_freq - .5, _analysis_height - .5);
592                 cairo_stroke (cr);
593         }
594
595         cairo_destroy (cr);
596 }
597
598 #define PHASE_PROPORTION 0.5
599
600 void
601 PluginEqGui::draw_scales_phase (Gtk::Widget*, cairo_t *cr)
602 {
603         float y;
604         cairo_font_extents_t extents;
605         cairo_font_extents (cr, &extents);
606
607         char buf[256];
608         cairo_text_extents_t t_ext;
609
610         for (uint32_t i = 0; i < 5; i++) {
611
612                 y = _analysis_height / 2.0 - (float)i * (_analysis_height / 8.0) * PHASE_PROPORTION;
613
614                 cairo_set_source_rgb (cr, .8, .9, 0.2);
615                 if (i == 0) {
616                         snprintf (buf,256, "0\u00b0");
617                 } else {
618                         snprintf (buf,256, "%d\u00b0", (i * 45));
619                 }
620                 cairo_text_extents (cr, buf, &t_ext);
621                 cairo_move_to (cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
622                 cairo_show_text (cr, buf);
623
624                 if (i == 0) {
625                         continue;
626                 }
627
628                 y = roundf (y) - .5;
629
630                 cairo_set_source_rgba (cr, .8, .9, .2, 0.4);
631                 cairo_move_to (cr, 0.0,             y);
632                 cairo_line_to (cr, _analysis_width, y);
633                 cairo_set_line_width (cr, 1);
634                 cairo_stroke (cr);
635
636                 y = _analysis_height / 2.0 + (float)i * (_analysis_height / 8.0) * PHASE_PROPORTION;
637
638                 // label
639                 snprintf (buf,256, "-%d\u00b0", (i * 45));
640                 cairo_set_source_rgb (cr, .8, .9, 0.2);
641                 cairo_text_extents (cr, buf, &t_ext);
642                 cairo_move_to (cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
643                 cairo_show_text (cr, buf);
644
645                 y = roundf (y) - .5;
646                 // line
647                 cairo_set_source_rgba (cr, .8, .9, .2, 0.4);
648                 cairo_move_to (cr, 0.0,             y);
649                 cairo_line_to (cr, _analysis_width, y);
650
651                 cairo_set_line_width (cr, 1);
652                 cairo_stroke (cr);
653         }
654 }
655
656 void
657 PluginEqGui::plot_impulse_phase (Gtk::Widget *w, cairo_t *cr)
658 {
659         float x,y;
660
661         int prevX = 0;
662         float avgY = 0.0;
663         int avgNum = 0;
664
665         // float width  = w->get_width();
666         float height = w->get_height ();
667         float analysis_height_2 = _analysis_height / 2.f;
668
669         cairo_set_source_rgba (cr, 0.95, 0.3, 0.2, 1.0);
670         for (uint32_t i = 0; i < _impulse_fft->bins() - 1; ++i) {
671                 // x coordinate of bin i
672                 x  = log10f (1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
673                 x *= _analysis_width;
674                 y  = analysis_height_2 - (_impulse_fft->phase_at_bin (i) / M_PI) * analysis_height_2 * PHASE_PROPORTION;
675
676                 if (i == 0) {
677                         cairo_move_to (cr, x, y);
678                         avgY = 0;
679                         avgNum = 0;
680                 } else if (rint (x) > prevX || i == _impulse_fft->bins() - 1) {
681                         avgY = avgY / (float)avgNum;
682                         if (avgY > (height * 10.0)) {
683                                 avgY = height * 10.0;
684                         }
685                         if (avgY < (-height * 10.0)) {
686                                 avgY = -height * 10.0;
687                         }
688
689                         cairo_line_to (cr, prevX, avgY);
690
691                         avgY = 0;
692                         avgNum = 0;
693                 }
694
695                 prevX = rint (x);
696                 avgY += y;
697                 avgNum++;
698         }
699
700         cairo_set_line_width (cr, 2.0);
701         cairo_stroke (cr);
702 }
703
704 void
705 PluginEqGui::draw_scales_power (Gtk::Widget */*w*/, cairo_t *cr)
706 {
707         if (_impulse_fft == 0) {
708                 return;
709         }
710
711         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 };
712         float divisor = _samplerate / 2.0 / _impulse_fft->bins();
713         float x;
714
715         cairo_set_line_width (cr, 1.5);
716         cairo_set_font_size (cr, 9);
717
718         cairo_font_extents_t extents;
719         cairo_font_extents (cr, &extents);
720         // float fontXOffset = extents.descent + 1.0;
721
722         char buf[256];
723
724         for (uint32_t i = 0; scales[i] != -1.0; ++i) {
725                 float bin = scales[i] / divisor;
726
727                 x  = log10f (1.0 + bin / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
728                 x *= _analysis_width;
729
730                 if (scales[i] < 1000.0) {
731                         snprintf (buf, 256, "%0.0f", scales[i]);
732                 } else {
733                         snprintf (buf, 256, "%0.0fk", scales[i]/1000.0);
734                 }
735
736                 cairo_set_source_rgb (cr, 0.4, 0.4, 0.4);
737
738                 cairo_move_to (cr, x - extents.height, 3.0);
739
740                 cairo_rotate (cr, M_PI / 2.0);
741                 cairo_show_text (cr, buf);
742                 cairo_rotate (cr, -M_PI / 2.0);
743                 cairo_stroke (cr);
744
745                 cairo_set_source_rgb (cr, 0.3, 0.3, 0.3);
746                 cairo_move_to (cr, x, _analysis_height);
747                 cairo_line_to (cr, x, 0.0);
748                 cairo_stroke (cr);
749         }
750
751         float y;
752
753         //double dashes[] = { 1.0, 3.0, 4.5, 3.0 };
754         double dashes[] = { 3.0, 5.0 };
755
756         for (float dB = 0.0; dB < _max_dB; dB += _step_dB) {
757                 snprintf (buf, 256, "+%0.0f", dB);
758
759                 y  = (_max_dB - dB) / (_max_dB - _min_dB);
760                 //std::cerr << " y = " << y << std::endl;
761                 y *= _analysis_height;
762
763                 if (dB != 0.0) {
764                         cairo_set_source_rgb (cr, 0.4, 0.4, 0.4);
765                         cairo_move_to (cr, 1.0,     y + extents.height + 1.0);
766                         cairo_show_text (cr, buf);
767                         cairo_stroke (cr);
768                 }
769
770                 cairo_set_source_rgb (cr, 0.2, 0.2, 0.2);
771                 cairo_move_to (cr, 0,               y);
772                 cairo_line_to (cr, _analysis_width, y);
773                 cairo_stroke (cr);
774
775                 if (dB == 0.0) {
776                         cairo_set_dash (cr, dashes, 2, 0.0);
777                 }
778         }
779
780         for (float dB = - _step_dB; dB > _min_dB; dB -= _step_dB) {
781                 snprintf (buf, 256, "%0.0f", dB);
782
783                 y  = (_max_dB - dB) / (_max_dB - _min_dB);
784                 y *= _analysis_height;
785
786                 cairo_set_source_rgb (cr, 0.4, 0.4, 0.4);
787                 cairo_move_to (cr, 1.0, y - extents.descent - 1.0);
788                 cairo_show_text (cr, buf);
789                 cairo_stroke (cr);
790
791                 cairo_set_source_rgb (cr, 0.2, 0.2, 0.2);
792                 cairo_move_to (cr, 0,               y);
793                 cairo_line_to (cr, _analysis_width, y);
794                 cairo_stroke (cr);
795         }
796
797         cairo_set_dash (cr, 0, 0, 0.0);
798 }
799
800 void
801 PluginEqGui::plot_impulse_amplitude (Gtk::Widget *w, cairo_t *cr)
802 {
803         float x,y;
804         int prevX = 0;
805         float avgY = 0.0;
806         int avgNum = 0;
807
808         // float width  = w->get_width();
809         float height = w->get_height ();
810
811         cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
812         cairo_set_line_width (cr, 2.5);
813
814         for (uint32_t i = 0; i < _impulse_fft->bins() - 1; ++i) {
815                 // x coordinate of bin i
816                 x  = log10f (1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
817                 x *= _analysis_width;
818
819                 float yCoeff = (power_to_dB (_impulse_fft->power_at_bin (i)) - _min_dB) / (_max_dB - _min_dB);
820
821                 y = _analysis_height - _analysis_height * yCoeff;
822
823                 if (i == 0) {
824                         cairo_move_to (cr, x, y);
825                         avgY = 0;
826                         avgNum = 0;
827                 } else if (rint (x) > prevX || i == _impulse_fft->bins() - 1) {
828                         avgY = avgY / (float)avgNum;
829                         if (avgY > (height * 10.0)) {
830                                 avgY = height * 10.0;
831                         }
832                         if (avgY < (-height * 10.0)) {
833                                 avgY = -height * 10.0;
834                         }
835                         cairo_line_to (cr, prevX, avgY);
836
837                         avgY = 0;
838                         avgNum = 0;
839                 }
840
841                 prevX = rint (x);
842                 avgY += y;
843                 avgNum++;
844         }
845
846         cairo_stroke (cr);
847 }
848
849 void
850 PluginEqGui::plot_signal_amplitude_difference (Gtk::Widget *w, cairo_t *cr)
851 {
852         float x,y;
853
854         int prevX = 0;
855         float avgY = 0.0;
856         int avgNum = 0;
857
858         float height = w->get_height();
859
860         cairo_set_source_rgb (cr, 0.0, 1.0, 0.0);
861         cairo_set_line_width (cr, 1.5);
862
863         for (uint32_t i = 0; i < _signal_input_fft->bins() - 1; ++i) {
864                 // x coordinate of bin i
865                 x  = log10f (1.0 + (float)i / (float)_signal_input_fft->bins() * _log_coeff) / _log_max;
866                 x *= _analysis_width;
867
868                 float power_out = _signal_output_fft->power_at_bin (i) + 1e-30;
869                 float power_in  = _signal_input_fft ->power_at_bin (i) + 1e-30;
870                 float power;
871                 switch (_live_signal_combo->get_active_row_number()) {
872                         case 2:
873                                 power = power_to_dB (power_in);
874                                 break;
875                         case 3:
876                                 power = power_to_dB (power_out);
877                                 break;
878                         case 4:
879                                 power = power_to_dB (power_in) + 40;
880                                 break;
881                         case 5:
882                                 power = power_to_dB (power_out) + 40;
883                                 break;
884                         default:
885                                 power = power_to_dB (power_out / power_in);
886                                 break;
887                 }
888
889                 assert (!ISINF(power));
890                 assert (!ISNAN(power));
891
892                 float yCoeff = (power - _min_dB) / (_max_dB - _min_dB);
893
894                 y = _analysis_height - _analysis_height*yCoeff;
895
896                 if (i == 0) {
897                         cairo_move_to (cr, x, y);
898
899                         avgY = 0;
900                         avgNum = 0;
901                 } else if (rint (x) > prevX || i == _impulse_fft->bins() - 1) {
902                         avgY = avgY / (float)avgNum;
903                         if (avgY > (height * 10.0)) {
904                                 avgY = height * 10.0;
905                         }
906                         if (avgY < (-height * 10.0)) {
907                                 avgY = -height * 10.0;
908                         }
909                         cairo_line_to (cr, prevX, avgY);
910
911                         avgY = 0;
912                         avgNum = 0;
913
914                 }
915
916                 prevX = rint (x);
917                 avgY += y;
918                 avgNum++;
919         }
920
921         cairo_stroke (cr);
922 }