6b176fbde8d855a4c0fed04644f569dc972f0ec8
[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 "plugin_eq_gui.h"
22 #include "fft.h"
23
24 #include "ardour_ui.h"
25 #include <ardour/audio_buffer.h>
26 #include <ardour/data_type.h>
27
28 #include <gtkmm/box.h>
29 #include <gtkmm/button.h>
30 #include <gtkmm/checkbutton.h>
31
32 #include <iostream>
33
34
35 PluginEqGui::PluginEqGui(boost::shared_ptr<ARDOUR::PluginInsert> pluginInsert)
36         : _min_dB(-12.0),
37           _max_dB(+12.0),
38           _step_dB(3.0),
39           _impulse_fft(0)
40 {
41         _samplerate = ARDOUR_UI::instance()->the_session()->frame_rate();
42
43         _plugin = pluginInsert->get_impulse_analysis_plugin();
44         _plugin->activate();
45
46         set_buffer_size(4096);
47
48         _log_coeff = (1.0 - 2.0 * (1000.0/(_samplerate/2.0))) / powf(1000.0/(_samplerate/2.0), 2.0); 
49         _log_max = log10f(1 + _log_coeff);
50
51
52         // Setup analysis drawing area
53         _analysis_scale_surface = 0;
54
55         _analysis_area = new Gtk::DrawingArea();
56         _analysis_width = 500.0;
57         _analysis_height = 500.0;
58         _analysis_area->set_size_request(_analysis_width, _analysis_height);
59
60         _analysis_area->signal_expose_event().connect( sigc::mem_fun (*this, &PluginEqGui::expose_analysis_area));
61         _analysis_area->signal_size_allocate().connect( sigc::mem_fun (*this, &PluginEqGui::resize_analysis_area));
62         
63
64         // dB selection
65         dBScaleModel = Gtk::ListStore::create(dBColumns);
66
67         dBScaleCombo = new Gtk::ComboBox(dBScaleModel);
68         dBScaleCombo -> set_title("dB scale");
69
70 #define ADD_DB_ROW(MIN,MAX,STEP,NAME) \
71         { \
72                 Gtk::TreeModel::Row row = *(dBScaleModel->append()); \
73                 row[dBColumns.dBMin]  = (MIN); \
74                 row[dBColumns.dBMax]  = (MAX); \
75                 row[dBColumns.dBStep] = (STEP); \
76                 row[dBColumns.name]   = NAME; \
77         }
78
79         ADD_DB_ROW( -6,  +6, 1, "-6dB .. +6dB");
80         ADD_DB_ROW(-12, +12, 3, "-12dB .. +12dB");
81         ADD_DB_ROW(-24, +24, 5, "-24dB .. +24dB");
82         ADD_DB_ROW(-36, +36, 6, "-36dB .. +36dB");
83
84 #undef ADD_DB_ROW
85
86         dBScaleCombo -> pack_start(dBColumns.name);
87         dBScaleCombo -> set_active(1);
88
89         dBScaleCombo -> signal_changed().connect( sigc::mem_fun(*this, &PluginEqGui::change_dB_scale) );
90
91         Gtk::Label *dBComboLabel = new Gtk::Label("dB scale");  
92
93         Gtk::HBox *dBSelectBin = new Gtk::HBox(false, 5);
94         dBSelectBin->add( *manage(dBComboLabel));
95         dBSelectBin->add( *manage(dBScaleCombo));
96         
97         // Phase checkbutton
98         _phase_button = new Gtk::CheckButton("Show phase");
99         _phase_button->set_active(true);
100         _phase_button->signal_toggled().connect( sigc::mem_fun(*this, &PluginEqGui::redraw_scales));
101
102         // populate table
103         attach( *manage(_analysis_area), 1, 3, 1, 2);
104         attach( *manage(dBSelectBin),    1, 2, 2, 3, Gtk::SHRINK, Gtk::SHRINK);
105         attach( *manage(_phase_button),  2, 3, 2, 3, Gtk::SHRINK, Gtk::SHRINK);
106 }
107
108 PluginEqGui::~PluginEqGui()
109 {
110         if (_analysis_scale_surface) {
111                 cairo_surface_destroy (_analysis_scale_surface);
112         }
113
114         delete _impulse_fft;
115         _plugin->deactivate();
116         
117         // all gui objects are *manage'd by the inherited Table object
118 }
119
120
121 void
122 PluginEqGui::on_hide()
123 {
124         stop_updating();
125         Gtk::Table::on_hide();
126 }
127
128 void
129 PluginEqGui::stop_updating()
130 {
131         if (_update_connection.connected()) {
132                 _update_connection.disconnect();
133         }
134 }
135
136 void
137 PluginEqGui::start_updating()
138 {
139         if (!_update_connection.connected() && is_visible()) {
140                 _update_connection = Glib::signal_timeout().connect( sigc::mem_fun(this, &PluginEqGui::timeout_callback), 250);
141         }
142 }
143
144 void
145 PluginEqGui::on_show()
146 {
147         Gtk::Table::on_show();
148
149         start_updating();
150
151         Gtk::Widget *toplevel = get_toplevel();
152         if (!toplevel) {
153                 std::cerr << "No toplevel widget for PluginEqGui?!?!" << std::endl;
154         }
155
156         if (!_window_unmap_connection.connected()) {
157                 _window_unmap_connection = toplevel->signal_unmap().connect( sigc::mem_fun(this, &PluginEqGui::stop_updating));
158         }
159
160         if (!_window_map_connection.connected()) {
161                 _window_map_connection = toplevel->signal_map().connect( sigc::mem_fun(this, &PluginEqGui::start_updating));
162         }
163
164 }
165
166 void
167 PluginEqGui::change_dB_scale()
168 {
169         Gtk::TreeModel::iterator iter = dBScaleCombo -> get_active();
170
171         Gtk::TreeModel::Row row;
172
173         if(iter && (row = *iter)) {
174                 _min_dB = row[dBColumns.dBMin];
175                 _max_dB = row[dBColumns.dBMax];
176                 _step_dB = row[dBColumns.dBStep];
177                 
178
179                 redraw_scales();
180         }
181 }
182
183 void
184 PluginEqGui::redraw_scales()
185 {
186
187         if (_analysis_scale_surface) {
188                 cairo_surface_destroy (_analysis_scale_surface);
189                 _analysis_scale_surface = 0;
190         }
191
192         _analysis_area->queue_draw();   
193 }
194
195 void
196 PluginEqGui::set_buffer_size(uint32_t size)
197 {
198         if (_buffer_size == size)
199                 return;
200
201         _buffer_size = size;
202
203         if (_impulse_fft) {
204                 delete _impulse_fft;
205                 _impulse_fft = 0;
206         }
207
208         _impulse_fft = new FFT(_buffer_size);
209
210         uint32_t inputs  = _plugin->get_info()->n_inputs.n_audio();
211         uint32_t outputs = _plugin->get_info()->n_outputs.n_audio();
212
213         uint32_t n_chans = std::max(inputs, outputs);
214         _bufferset.ensure_buffers(ARDOUR::DataType::AUDIO, n_chans, _buffer_size);
215
216         ARDOUR::ChanCount chanCount(ARDOUR::DataType::AUDIO, n_chans);
217         _bufferset.set_count(chanCount);
218 }
219
220 void 
221 PluginEqGui::resize_analysis_area(Gtk::Allocation& size)
222 {
223         _analysis_width  = (float)size.get_width();
224         _analysis_height = (float)size.get_height();
225
226         if (_analysis_scale_surface) {
227                 cairo_surface_destroy (_analysis_scale_surface);
228                 _analysis_scale_surface = 0;
229         }
230 }
231
232 bool
233 PluginEqGui::timeout_callback()
234 {
235         run_analysis();
236
237         return true;
238 }
239
240 void
241 PluginEqGui::run_analysis()
242 {
243         uint32_t inputs  = _plugin->get_info()->n_inputs.n_audio();
244         uint32_t outputs = _plugin->get_info()->n_outputs.n_audio();
245
246         // Create the impulse, can't use silence() because consecutive calls won't work
247         for (uint32_t i = 0; i < inputs; ++i) {
248                 ARDOUR::AudioBuffer &buf = _bufferset.get_audio(i);
249                 ARDOUR::Sample *d = buf.data(_buffer_size, 0);
250                 memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size);
251                 *d = 1.0;
252         }
253         uint32_t x,y;
254         x=y=0;
255
256         _plugin->connect_and_run(_bufferset, x, y, _buffer_size, (nframes_t)0);
257
258         // Analyze all output buffers
259         _impulse_fft->reset();
260         for (uint32_t i = 0; i < outputs; ++i) {
261                 _impulse_fft->analyze(_bufferset.get_audio(i).data(_buffer_size, 0));
262         }
263
264         // normalize the output
265         _impulse_fft->calculate();
266
267         // This signals calls expose_analysis_area()
268         _analysis_area->queue_draw();   
269
270 }
271
272 bool
273 PluginEqGui::expose_analysis_area(GdkEventExpose *evt)
274 {
275         redraw_analysis_area();
276
277         return false;
278 }
279
280 void
281 PluginEqGui::draw_analysis_scales(cairo_t *ref_cr)
282 {
283         // TODO: check whether we need rounding
284         _analysis_scale_surface = cairo_surface_create_similar(cairo_get_target(ref_cr), 
285                                                              CAIRO_CONTENT_COLOR, 
286                                                              _analysis_width,
287                                                              _analysis_height);
288
289         cairo_t *cr = cairo_create (_analysis_scale_surface);
290
291         cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
292         cairo_rectangle(cr, 0.0, 0.0, _analysis_width, _analysis_height);
293         cairo_fill(cr);
294
295
296         draw_scales_power(_analysis_area, cr);
297         if (_phase_button->get_active()) {
298                 draw_scales_phase(_analysis_area, cr);
299         }
300         
301         cairo_destroy(cr);
302         
303 }
304
305 void
306 PluginEqGui::redraw_analysis_area()
307 {
308         cairo_t *cr;
309
310         cr = gdk_cairo_create(GDK_DRAWABLE(_analysis_area->get_window()->gobj()));
311
312         if (_analysis_scale_surface == 0) {
313                 draw_analysis_scales(cr);
314         }
315         
316
317         cairo_copy_page(cr);
318
319         cairo_set_source_surface(cr, _analysis_scale_surface, 0.0, 0.0);
320         cairo_paint(cr);
321
322         if (_phase_button->get_active()) {
323                 plot_phase(_analysis_area, cr);
324         }
325         plot_amplitude(_analysis_area, cr);
326
327
328         cairo_destroy(cr);
329
330
331 }
332
333 #define PHASE_PROPORTION 0.6
334
335 void 
336 PluginEqGui::draw_scales_phase(Gtk::Widget *w, cairo_t *cr)
337 {
338         float y;
339         cairo_font_extents_t extents;
340         cairo_font_extents(cr, &extents);
341
342         char buf[256];
343         cairo_text_extents_t t_ext;
344
345         for (uint32_t i = 0; i < 3; i++) {
346
347                 y = _analysis_height/2.0 - (float)i*(_analysis_height/8.0)*PHASE_PROPORTION;
348
349                 cairo_set_source_rgb(cr, .8, .9, 0.2);
350                 if (i == 0) {
351                         snprintf(buf,256, "0\u00b0");
352                 } else {
353                         snprintf(buf,256, "%d\u00b0", (i * 45));
354                 }
355                 cairo_text_extents(cr, buf, &t_ext);
356                 cairo_move_to(cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
357                 cairo_show_text(cr, buf);
358                 
359                 if (i == 0)
360                         continue;
361                 
362
363                 cairo_set_source_rgba(cr, .8, .9, 0.2, 0.6/(float)i);
364                 cairo_move_to(cr, 0.0,            y);
365                 cairo_line_to(cr, _analysis_width, y);
366
367                 
368                 y = _analysis_height/2.0 + (float)i*(_analysis_height/8.0)*PHASE_PROPORTION;
369
370                 // label
371                 snprintf(buf,256, "-%d\u00b0", (i * 45));
372                 cairo_set_source_rgb(cr, .8, .9, 0.2);
373                 cairo_text_extents(cr, buf, &t_ext);
374                 cairo_move_to(cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
375                 cairo_show_text(cr, buf);
376
377                 // line
378                 cairo_set_source_rgba(cr, .8, .9, 0.2, 0.6/(float)i);
379                 cairo_move_to(cr, 0.0,            y);
380                 cairo_line_to(cr, _analysis_width, y);
381
382                 cairo_set_line_width (cr, 0.25 + 1.0/(float)(i+1));
383                 cairo_stroke(cr);
384         }
385 }
386
387 void 
388 PluginEqGui::plot_phase(Gtk::Widget *w, cairo_t *cr)
389 {
390         float x,y;
391
392         int prevX = 0;
393         float avgY = 0.0;
394         int avgNum = 0;
395
396         cairo_set_source_rgba(cr, 0.95, 0.3, 0.2, 1.0);
397         for (uint32_t i = 0; i < _impulse_fft->bins()-1; i++) {
398                 // x coordinate of bin i
399                 x  = log10f(1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
400                 x *= _analysis_width;
401
402                 y  = _analysis_height/2.0 - (_impulse_fft->phase_at_bin(i)/M_PI)*(_analysis_height/2.0)*PHASE_PROPORTION;
403         
404                 if ( i == 0 ) {
405                         cairo_move_to(cr, x, y);
406
407                         avgY = 0;
408                         avgNum = 0;
409                 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
410                         cairo_line_to(cr, prevX, avgY/(float)avgNum);
411
412                         avgY = 0;
413                         avgNum = 0;
414                                 
415                 }       
416
417                 prevX = rint(x);
418                 avgY += y;
419                 avgNum++;
420         }
421
422         cairo_set_line_width (cr, 2.0);
423         cairo_stroke(cr);
424 }
425
426 void
427 PluginEqGui::draw_scales_power(Gtk::Widget *w, cairo_t *cr)
428 {
429         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 };
430         
431         float divisor = _samplerate / 2.0 / _impulse_fft->bins();
432         float x;
433
434         cairo_set_line_width (cr, 1.5);
435         cairo_set_font_size(cr, 9);
436
437         cairo_font_extents_t extents;
438         cairo_font_extents(cr, &extents);
439         float fontXOffset = extents.descent + 1.0;
440
441         char buf[256];
442
443         for (uint32_t i = 0; scales[i] != -1.0; ++i) {
444                 float bin = scales[i] / divisor;
445
446                 x  = log10f(1.0 + bin / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
447                 x *= _analysis_width;
448
449                 if (scales[i] < 1000.0) {
450                         snprintf(buf, 256, "%0.0f", scales[i]);
451                 } else {
452                         snprintf(buf, 256, "%0.0fk", scales[i]/1000.0);
453                 }
454
455                 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
456
457                 cairo_move_to(cr, x + fontXOffset, 3.0);
458
459                 cairo_rotate(cr, M_PI / 2.0);
460                 cairo_show_text(cr, buf);
461                 cairo_rotate(cr, -M_PI / 2.0);
462                 cairo_stroke(cr);
463
464                 cairo_set_source_rgb(cr, 0.3, 0.3, 0.3);
465                 cairo_move_to(cr, x, _analysis_height);
466                 cairo_line_to(cr, x, 0.0);
467                 cairo_stroke(cr);
468         }
469
470         float y;
471
472         //double dashes[] = { 1.0, 3.0, 4.5, 3.0 };
473         double dashes[] = { 3.0, 5.0 };
474
475         for (float dB = 0.0; dB < _max_dB; dB += _step_dB ) {
476                 snprintf(buf, 256, "+%0.0f", dB );
477
478                 y  = ( _max_dB - dB) / ( _max_dB - _min_dB );
479                 //std::cerr << " y = " << y << std::endl;
480                 y *= _analysis_height;
481
482                 if (dB != 0.0) {
483                         cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
484                         cairo_move_to(cr, 1.0,     y + extents.height + 1.0);
485                         cairo_show_text(cr, buf);
486                         cairo_stroke(cr);
487                 }
488
489                 cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
490                 cairo_move_to(cr, 0,     y);
491                 cairo_line_to(cr, _analysis_width, y);
492                 cairo_stroke(cr);
493
494                 if (dB == 0.0) {
495                         cairo_set_dash(cr, dashes, 2, 0.0);
496                 }
497         }
498
499         
500         
501         for (float dB = - _step_dB; dB > _min_dB; dB -= _step_dB ) {
502                 snprintf(buf, 256, "%0.0f", dB );
503
504                 y  = ( _max_dB - dB) / ( _max_dB - _min_dB );
505                 y *= _analysis_height;
506
507                 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
508                 cairo_move_to(cr, 1.0,     y - extents.descent - 1.0);
509                 cairo_show_text(cr, buf);
510                 cairo_stroke(cr);
511
512                 cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
513                 cairo_move_to(cr, 0,     y);
514                 cairo_line_to(cr, _analysis_width, y);
515                 cairo_stroke(cr);
516         }
517
518         cairo_set_dash(cr, 0, 0, 0.0);
519
520 }
521
522 inline float
523 power_to_dB(float a)
524 {
525         return 10.0 * log10f(a);
526 }
527
528 void 
529 PluginEqGui::plot_amplitude(Gtk::Widget *w, cairo_t *cr)
530 {
531         float x,y;
532
533         int prevX = 0;
534         float avgY = 0.0;
535         int avgNum = 0;
536
537         cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
538         cairo_set_line_width (cr, 2.5);
539
540         for (uint32_t i = 0; i < _impulse_fft->bins()-1; i++) {
541                 // x coordinate of bin i
542                 x  = log10f(1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
543                 x *= _analysis_width;
544
545                 float yCoeff = ( power_to_dB(_impulse_fft->power_at_bin(i)) - _min_dB) / (_max_dB - _min_dB);
546
547                 y = _analysis_height - _analysis_height*yCoeff;
548
549                 if ( i == 0 ) {
550                         cairo_move_to(cr, x, y);
551
552                         avgY = 0;
553                         avgNum = 0;
554                 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
555                         cairo_line_to(cr, prevX, avgY/(float)avgNum);
556
557                         avgY = 0;
558                         avgNum = 0;
559                                 
560                 }
561
562                 prevX = rint(x);
563                 avgY += y;
564                 avgNum++;
565         }
566
567         cairo_stroke(cr);
568 }
569