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