2 Copyright (C) 2008 Paul Davis
3 Author: Sampo Savolainen
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.
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.
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.
21 #include "plugin_eq_gui.h"
24 #include "ardour_ui.h"
25 #include "gui_thread.h"
26 #include <ardour/audio_buffer.h>
27 #include <ardour/data_type.h>
29 #include <gtkmm/box.h>
30 #include <gtkmm/button.h>
31 #include <gtkmm/checkbutton.h>
36 PluginEqGui::PluginEqGui(boost::shared_ptr<ARDOUR::PluginInsert> pluginInsert)
42 _signal_output_fft(0),
43 _plugin_insert(pluginInsert)
45 _signal_analysis_running = false;
46 _samplerate = ARDOUR_UI::instance()->the_session()->frame_rate();
48 _plugin = _plugin_insert->get_impulse_analysis_plugin();
51 set_buffer_size(4096, 16384);
52 //set_buffer_size(4096, 4096);
54 _log_coeff = (1.0 - 2.0 * (1000.0/(_samplerate/2.0))) / powf(1000.0/(_samplerate/2.0), 2.0);
55 _log_max = log10f(1 + _log_coeff);
58 // Setup analysis drawing area
59 _analysis_scale_surface = 0;
61 _analysis_area = new Gtk::DrawingArea();
62 _analysis_width = 500.0;
63 _analysis_height = 500.0;
64 _analysis_area->set_size_request(_analysis_width, _analysis_height);
66 _analysis_area->signal_expose_event().connect( sigc::mem_fun (*this, &PluginEqGui::expose_analysis_area));
67 _analysis_area->signal_size_allocate().connect( sigc::mem_fun (*this, &PluginEqGui::resize_analysis_area));
71 dBScaleModel = Gtk::ListStore::create(dBColumns);
73 dBScaleCombo = new Gtk::ComboBox(dBScaleModel);
74 dBScaleCombo -> set_title("dB scale");
76 #define ADD_DB_ROW(MIN,MAX,STEP,NAME) \
78 Gtk::TreeModel::Row row = *(dBScaleModel->append()); \
79 row[dBColumns.dBMin] = (MIN); \
80 row[dBColumns.dBMax] = (MAX); \
81 row[dBColumns.dBStep] = (STEP); \
82 row[dBColumns.name] = NAME; \
85 ADD_DB_ROW( -6, +6, 1, "-6dB .. +6dB");
86 ADD_DB_ROW(-12, +12, 3, "-12dB .. +12dB");
87 ADD_DB_ROW(-24, +24, 5, "-24dB .. +24dB");
88 ADD_DB_ROW(-36, +36, 6, "-36dB .. +36dB");
89 ADD_DB_ROW(-64, +64,12, "-64dB .. +64dB");
93 dBScaleCombo -> pack_start(dBColumns.name);
94 dBScaleCombo -> set_active(1);
96 dBScaleCombo -> signal_changed().connect( sigc::mem_fun(*this, &PluginEqGui::change_dB_scale) );
98 Gtk::Label *dBComboLabel = new Gtk::Label("dB scale");
100 Gtk::HBox *dBSelectBin = new Gtk::HBox(false, 5);
101 dBSelectBin->add( *manage(dBComboLabel));
102 dBSelectBin->add( *manage(dBScaleCombo));
105 _phase_button = new Gtk::CheckButton("Show phase");
106 _phase_button->set_active(true);
107 _phase_button->signal_toggled().connect( sigc::mem_fun(*this, &PluginEqGui::redraw_scales));
110 attach( *manage(_analysis_area), 1, 3, 1, 2);
111 attach( *manage(dBSelectBin), 1, 2, 2, 3, Gtk::SHRINK, Gtk::SHRINK);
112 attach( *manage(_phase_button), 2, 3, 2, 3, Gtk::SHRINK, Gtk::SHRINK);
115 // Connect the realtime signal collection callback
116 _plugin_insert->AnalysisDataGathered.connect( sigc::mem_fun(*this, &PluginEqGui::signal_collect_callback ));
119 PluginEqGui::~PluginEqGui()
121 if (_analysis_scale_surface) {
122 cairo_surface_destroy (_analysis_scale_surface);
126 delete _signal_input_fft;
127 delete _signal_output_fft;
129 _plugin->deactivate();
131 // all gui objects are *manage'd by the inherited Table object
136 PluginEqGui::on_hide()
139 Gtk::Table::on_hide();
143 PluginEqGui::stop_updating()
145 if (_update_connection.connected()) {
146 _update_connection.disconnect();
151 PluginEqGui::start_updating()
153 if (!_update_connection.connected() && is_visible()) {
154 _update_connection = Glib::signal_timeout().connect( sigc::mem_fun(this, &PluginEqGui::timeout_callback), 250);
159 PluginEqGui::on_show()
161 Gtk::Table::on_show();
165 Gtk::Widget *toplevel = get_toplevel();
167 std::cerr << "No toplevel widget for PluginEqGui?!?!" << std::endl;
170 if (!_window_unmap_connection.connected()) {
171 _window_unmap_connection = toplevel->signal_unmap().connect( sigc::mem_fun(this, &PluginEqGui::stop_updating));
174 if (!_window_map_connection.connected()) {
175 _window_map_connection = toplevel->signal_map().connect( sigc::mem_fun(this, &PluginEqGui::start_updating));
181 PluginEqGui::change_dB_scale()
183 Gtk::TreeModel::iterator iter = dBScaleCombo -> get_active();
185 Gtk::TreeModel::Row row;
187 if(iter && (row = *iter)) {
188 _min_dB = row[dBColumns.dBMin];
189 _max_dB = row[dBColumns.dBMax];
190 _step_dB = row[dBColumns.dBStep];
198 PluginEqGui::redraw_scales()
201 if (_analysis_scale_surface) {
202 cairo_surface_destroy (_analysis_scale_surface);
203 _analysis_scale_surface = 0;
206 _analysis_area->queue_draw();
208 // TODO: Add graph legend!
212 PluginEqGui::set_buffer_size(uint32_t size, uint32_t signal_size)
214 if (_buffer_size == size && _signal_buffer_size == signal_size)
218 FFT *tmp1 = _impulse_fft;
219 FFT *tmp2 = _signal_input_fft;
220 FFT *tmp3 = _signal_output_fft;
223 _impulse_fft = new FFT(size);
224 _signal_input_fft = new FFT(signal_size);
225 _signal_output_fft = new FFT(signal_size);
227 // Don't care about lost memory, we're screwed anyhow
229 _signal_input_fft = tmp2;
230 _signal_output_fft = tmp3;
234 if (tmp1) delete tmp1;
235 if (tmp2) delete tmp1;
236 if (tmp3) delete tmp1;
239 _signal_buffer_size = signal_size;
241 // These are for impulse analysis only, the signal analysis uses the actual
242 // number of I/O's for the plugininsert
243 uint32_t inputs = _plugin->get_info()->n_inputs.n_audio();
244 uint32_t outputs = _plugin->get_info()->n_outputs.n_audio();
246 // buffers for the signal analysis are ensured inside PluginInsert
247 uint32_t n_chans = std::max(inputs, outputs);
248 _bufferset.ensure_buffers(ARDOUR::DataType::AUDIO, n_chans, _buffer_size);
250 ARDOUR::ChanCount chanCount(ARDOUR::DataType::AUDIO, n_chans);
251 _bufferset.set_count(chanCount);
255 PluginEqGui::resize_analysis_area(Gtk::Allocation& size)
257 _analysis_width = (float)size.get_width();
258 _analysis_height = (float)size.get_height();
260 if (_analysis_scale_surface) {
261 cairo_surface_destroy (_analysis_scale_surface);
262 _analysis_scale_surface = 0;
267 PluginEqGui::timeout_callback()
269 if (!_signal_analysis_running) {
270 _signal_analysis_running = true;
271 _plugin_insert -> collect_signal_for_analysis(_signal_buffer_size);
273 run_impulse_analysis();
279 PluginEqGui::signal_collect_callback(ARDOUR::BufferSet *in, ARDOUR::BufferSet *out)
281 ENSURE_GUI_THREAD(bind (mem_fun (*this, &PluginEqGui::signal_collect_callback), in, out));
283 _signal_input_fft ->reset();
284 _signal_output_fft->reset();
286 for (uint32_t i = 0; i < _plugin_insert->input_streams().n_audio(); ++i) {
287 _signal_input_fft ->analyze(in ->get_audio(i).data(_signal_buffer_size, 0), FFT::HANN);
290 for (uint32_t i = 0; i < _plugin_insert->output_streams().n_audio(); ++i) {
291 _signal_output_fft->analyze(out->get_audio(i).data(_signal_buffer_size, 0), FFT::HANN);
294 _signal_input_fft ->calculate();
295 _signal_output_fft->calculate();
297 _signal_analysis_running = false;
299 // This signals calls expose_analysis_area()
300 _analysis_area->queue_draw();
304 PluginEqGui::run_impulse_analysis()
306 uint32_t inputs = _plugin->get_info()->n_inputs.n_audio();
307 uint32_t outputs = _plugin->get_info()->n_outputs.n_audio();
309 // Create the impulse, can't use silence() because consecutive calls won't work
310 for (uint32_t i = 0; i < inputs; ++i) {
311 ARDOUR::AudioBuffer &buf = _bufferset.get_audio(i);
312 ARDOUR::Sample *d = buf.data(_buffer_size, 0);
313 memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size);
319 _plugin->connect_and_run(_bufferset, x, y, _buffer_size, (nframes_t)0);
321 // Analyze all output buffers
322 _impulse_fft->reset();
323 for (uint32_t i = 0; i < outputs; ++i) {
324 _impulse_fft->analyze(_bufferset.get_audio(i).data(_buffer_size, 0));
327 // normalize the output
328 _impulse_fft->calculate();
330 // This signals calls expose_analysis_area()
331 _analysis_area->queue_draw();
336 PluginEqGui::expose_analysis_area(GdkEventExpose *evt)
338 redraw_analysis_area();
344 PluginEqGui::draw_analysis_scales(cairo_t *ref_cr)
346 // TODO: check whether we need rounding
347 _analysis_scale_surface = cairo_surface_create_similar(cairo_get_target(ref_cr),
352 cairo_t *cr = cairo_create (_analysis_scale_surface);
354 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
355 cairo_rectangle(cr, 0.0, 0.0, _analysis_width, _analysis_height);
359 draw_scales_power(_analysis_area, cr);
360 if (_phase_button->get_active()) {
361 draw_scales_phase(_analysis_area, cr);
369 PluginEqGui::redraw_analysis_area()
373 cr = gdk_cairo_create(GDK_DRAWABLE(_analysis_area->get_window()->gobj()));
375 if (_analysis_scale_surface == 0) {
376 draw_analysis_scales(cr);
382 cairo_set_source_surface(cr, _analysis_scale_surface, 0.0, 0.0);
385 if (_phase_button->get_active()) {
386 plot_impulse_phase(_analysis_area, cr);
388 plot_impulse_amplitude(_analysis_area, cr);
390 // TODO: make this optional
391 plot_signal_amplitude_difference(_analysis_area, cr);
398 #define PHASE_PROPORTION 0.5
401 PluginEqGui::draw_scales_phase(Gtk::Widget *w, cairo_t *cr)
404 cairo_font_extents_t extents;
405 cairo_font_extents(cr, &extents);
408 cairo_text_extents_t t_ext;
410 for (uint32_t i = 0; i < 3; i++) {
412 y = _analysis_height/2.0 - (float)i*(_analysis_height/8.0)*PHASE_PROPORTION;
414 cairo_set_source_rgb(cr, .8, .9, 0.2);
416 snprintf(buf,256, "0\u00b0");
418 snprintf(buf,256, "%d\u00b0", (i * 45));
420 cairo_text_extents(cr, buf, &t_ext);
421 cairo_move_to(cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
422 cairo_show_text(cr, buf);
428 cairo_set_source_rgba(cr, .8, .9, 0.2, 0.6/(float)i);
429 cairo_move_to(cr, 0.0, y);
430 cairo_line_to(cr, _analysis_width, y);
433 y = _analysis_height/2.0 + (float)i*(_analysis_height/8.0)*PHASE_PROPORTION;
436 snprintf(buf,256, "-%d\u00b0", (i * 45));
437 cairo_set_source_rgb(cr, .8, .9, 0.2);
438 cairo_text_extents(cr, buf, &t_ext);
439 cairo_move_to(cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
440 cairo_show_text(cr, buf);
443 cairo_set_source_rgba(cr, .8, .9, 0.2, 0.6/(float)i);
444 cairo_move_to(cr, 0.0, y);
445 cairo_line_to(cr, _analysis_width, y);
447 cairo_set_line_width (cr, 0.25 + 1.0/(float)(i+1));
453 PluginEqGui::plot_impulse_phase(Gtk::Widget *w, cairo_t *cr)
461 // float width = w->get_width();
462 float height = w->get_height();
464 cairo_set_source_rgba(cr, 0.95, 0.3, 0.2, 1.0);
465 for (uint32_t i = 0; i < _impulse_fft->bins()-1; i++) {
466 // x coordinate of bin i
467 x = log10f(1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
468 x *= _analysis_width;
470 y = _analysis_height/2.0 - (_impulse_fft->phase_at_bin(i)/M_PI)*(_analysis_height/2.0)*PHASE_PROPORTION;
473 cairo_move_to(cr, x, y);
477 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
478 avgY = avgY/(float)avgNum;
479 if (avgY > (height * 10.0) ) avgY = height * 10.0;
480 if (avgY < (-height * 10.0) ) avgY = -height * 10.0;
481 cairo_line_to(cr, prevX, avgY);
482 //cairo_line_to(cr, prevX, avgY/(float)avgNum);
494 cairo_set_line_width (cr, 2.0);
499 PluginEqGui::draw_scales_power(Gtk::Widget *w, cairo_t *cr)
501 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 };
503 float divisor = _samplerate / 2.0 / _impulse_fft->bins();
506 cairo_set_line_width (cr, 1.5);
507 cairo_set_font_size(cr, 9);
509 cairo_font_extents_t extents;
510 cairo_font_extents(cr, &extents);
511 // float fontXOffset = extents.descent + 1.0;
515 for (uint32_t i = 0; scales[i] != -1.0; ++i) {
516 float bin = scales[i] / divisor;
518 x = log10f(1.0 + bin / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
519 x *= _analysis_width;
521 if (scales[i] < 1000.0) {
522 snprintf(buf, 256, "%0.0f", scales[i]);
524 snprintf(buf, 256, "%0.0fk", scales[i]/1000.0);
527 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
529 //cairo_move_to(cr, x + fontXOffset, 3.0);
530 cairo_move_to(cr, x - extents.height, 3.0);
532 cairo_rotate(cr, M_PI / 2.0);
533 cairo_show_text(cr, buf);
534 cairo_rotate(cr, -M_PI / 2.0);
537 cairo_set_source_rgb(cr, 0.3, 0.3, 0.3);
538 cairo_move_to(cr, x, _analysis_height);
539 cairo_line_to(cr, x, 0.0);
545 //double dashes[] = { 1.0, 3.0, 4.5, 3.0 };
546 double dashes[] = { 3.0, 5.0 };
548 for (float dB = 0.0; dB < _max_dB; dB += _step_dB ) {
549 snprintf(buf, 256, "+%0.0f", dB );
551 y = ( _max_dB - dB) / ( _max_dB - _min_dB );
552 //std::cerr << " y = " << y << std::endl;
553 y *= _analysis_height;
556 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
557 cairo_move_to(cr, 1.0, y + extents.height + 1.0);
558 cairo_show_text(cr, buf);
562 cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
563 cairo_move_to(cr, 0, y);
564 cairo_line_to(cr, _analysis_width, y);
568 cairo_set_dash(cr, dashes, 2, 0.0);
574 for (float dB = - _step_dB; dB > _min_dB; dB -= _step_dB ) {
575 snprintf(buf, 256, "%0.0f", dB );
577 y = ( _max_dB - dB) / ( _max_dB - _min_dB );
578 y *= _analysis_height;
580 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
581 cairo_move_to(cr, 1.0, y - extents.descent - 1.0);
582 cairo_show_text(cr, buf);
585 cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
586 cairo_move_to(cr, 0, y);
587 cairo_line_to(cr, _analysis_width, y);
591 cairo_set_dash(cr, 0, 0, 0.0);
598 return 10.0 * log10f(a);
602 PluginEqGui::plot_impulse_amplitude(Gtk::Widget *w, cairo_t *cr)
610 // float width = w->get_width();
611 float height = w->get_height();
613 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
614 cairo_set_line_width (cr, 2.5);
616 for (uint32_t i = 0; i < _impulse_fft->bins()-1; i++) {
617 // x coordinate of bin i
618 x = log10f(1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
619 x *= _analysis_width;
621 float yCoeff = ( power_to_dB(_impulse_fft->power_at_bin(i)) - _min_dB) / (_max_dB - _min_dB);
623 y = _analysis_height - _analysis_height*yCoeff;
626 cairo_move_to(cr, x, y);
630 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
631 avgY = avgY/(float)avgNum;
632 if (avgY > (height * 10.0) ) avgY = height * 10.0;
633 if (avgY < (-height * 10.0) ) avgY = -height * 10.0;
634 cairo_line_to(cr, prevX, avgY);
635 //cairo_line_to(cr, prevX, avgY/(float)avgNum);
651 PluginEqGui::plot_signal_amplitude_difference(Gtk::Widget *w, cairo_t *cr)
659 // float width = w->get_width();
660 float height = w->get_height();
662 cairo_set_source_rgb(cr, 0.0, 1.0, 0.0);
663 cairo_set_line_width (cr, 2.5);
665 for (uint32_t i = 0; i < _signal_input_fft->bins()-1; i++) {
666 // x coordinate of bin i
667 x = log10f(1.0 + (float)i / (float)_signal_input_fft->bins() * _log_coeff) / _log_max;
668 x *= _analysis_width;
670 float power_out = power_to_dB(_signal_output_fft->power_at_bin(i));
671 float power_in = power_to_dB(_signal_input_fft ->power_at_bin(i));
672 float power = power_out - power_in;
676 double p = 10.0 * log10( 1.0 + (double)_signal_output_fft->power_at_bin(i) - (double)
677 - _signal_input_fft ->power_at_bin(i));
679 float power = (float)p;
681 if ( (i % 1000) == 0) {
682 std::cerr << i << ": " << power << std::endl;
688 power = _min_dB - 1.0;
690 power = _max_dB - 1.0;
692 } else if (isnan(power)) {
693 power = _min_dB - 1.0;
696 float yCoeff = ( power - _min_dB) / (_max_dB - _min_dB);
698 y = _analysis_height - _analysis_height*yCoeff;
701 cairo_move_to(cr, x, y);
705 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
706 avgY = avgY/(float)avgNum;
707 if (avgY > (height * 10.0) ) avgY = height * 10.0;
708 if (avgY < (-height * 10.0) ) avgY = -height * 10.0;
709 cairo_line_to(cr, prevX, avgY);