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