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 <ardour/audio_buffer.h>
26 #include <ardour/data_type.h>
28 #include <gtkmm/box.h>
29 #include <gtkmm/button.h>
30 #include <gtkmm/checkbutton.h>
35 PluginEqGui::PluginEqGui(boost::shared_ptr<ARDOUR::PluginInsert> pluginInsert)
41 _samplerate = ARDOUR_UI::instance()->the_session()->frame_rate();
43 _plugin = pluginInsert->get_impulse_analysis_plugin();
46 set_buffer_size(4096);
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);
52 // Setup analysis drawing area
53 _analysis_scale_surface = 0;
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);
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));
65 dBScaleModel = Gtk::ListStore::create(dBColumns);
67 dBScaleCombo = new Gtk::ComboBox(dBScaleModel);
68 dBScaleCombo -> set_title("dB scale");
70 #define ADD_DB_ROW(MIN,MAX,STEP,NAME) \
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; \
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");
86 dBScaleCombo -> pack_start(dBColumns.name);
87 dBScaleCombo -> set_active(1);
89 dBScaleCombo -> signal_changed().connect( sigc::mem_fun(*this, &PluginEqGui::change_dB_scale) );
91 Gtk::Label *dBComboLabel = new Gtk::Label("dB scale");
93 Gtk::HBox *dBSelectBin = new Gtk::HBox(false, 5);
94 dBSelectBin->add( *manage(dBComboLabel));
95 dBSelectBin->add( *manage(dBScaleCombo));
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));
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);
108 PluginEqGui::~PluginEqGui()
110 if (_analysis_scale_surface) {
111 cairo_surface_destroy (_analysis_scale_surface);
115 _plugin->deactivate();
117 // all gui objects are *manage'd by the inherited Table object
122 PluginEqGui::on_hide()
125 Gtk::Table::on_hide();
129 PluginEqGui::stop_updating()
131 if (_update_connection.connected()) {
132 _update_connection.disconnect();
137 PluginEqGui::start_updating()
139 if (!_update_connection.connected() && is_visible()) {
140 _update_connection = Glib::signal_timeout().connect( sigc::mem_fun(this, &PluginEqGui::timeout_callback), 250);
145 PluginEqGui::on_show()
147 Gtk::Table::on_show();
151 Gtk::Widget *toplevel = get_toplevel();
153 std::cerr << "No toplevel widget for PluginEqGui?!?!" << std::endl;
156 if (!_window_unmap_connection.connected()) {
157 _window_unmap_connection = toplevel->signal_unmap().connect( sigc::mem_fun(this, &PluginEqGui::stop_updating));
160 if (!_window_map_connection.connected()) {
161 _window_map_connection = toplevel->signal_map().connect( sigc::mem_fun(this, &PluginEqGui::start_updating));
167 PluginEqGui::change_dB_scale()
169 Gtk::TreeModel::iterator iter = dBScaleCombo -> get_active();
171 Gtk::TreeModel::Row row;
173 if(iter && (row = *iter)) {
174 _min_dB = row[dBColumns.dBMin];
175 _max_dB = row[dBColumns.dBMax];
176 _step_dB = row[dBColumns.dBStep];
184 PluginEqGui::redraw_scales()
187 if (_analysis_scale_surface) {
188 cairo_surface_destroy (_analysis_scale_surface);
189 _analysis_scale_surface = 0;
192 _analysis_area->queue_draw();
196 PluginEqGui::set_buffer_size(uint32_t size)
198 if (_buffer_size == size)
208 _impulse_fft = new FFT(_buffer_size);
210 uint32_t inputs = _plugin->get_info()->n_inputs.n_audio();
211 uint32_t outputs = _plugin->get_info()->n_outputs.n_audio();
213 uint32_t n_chans = std::max(inputs, outputs);
214 _bufferset.ensure_buffers(ARDOUR::DataType::AUDIO, n_chans, _buffer_size);
216 ARDOUR::ChanCount chanCount(ARDOUR::DataType::AUDIO, n_chans);
217 _bufferset.set_count(chanCount);
221 PluginEqGui::resize_analysis_area(Gtk::Allocation& size)
223 _analysis_width = (float)size.get_width();
224 _analysis_height = (float)size.get_height();
226 if (_analysis_scale_surface) {
227 cairo_surface_destroy (_analysis_scale_surface);
228 _analysis_scale_surface = 0;
233 PluginEqGui::timeout_callback()
241 PluginEqGui::run_analysis()
243 uint32_t inputs = _plugin->get_info()->n_inputs.n_audio();
244 uint32_t outputs = _plugin->get_info()->n_outputs.n_audio();
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);
256 _plugin->connect_and_run(_bufferset, x, y, _buffer_size, (nframes_t)0);
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));
264 // normalize the output
265 _impulse_fft->calculate();
267 // This signals calls expose_analysis_area()
268 _analysis_area->queue_draw();
273 PluginEqGui::expose_analysis_area(GdkEventExpose *evt)
275 redraw_analysis_area();
281 PluginEqGui::draw_analysis_scales(cairo_t *ref_cr)
283 // TODO: check whether we need rounding
284 _analysis_scale_surface = cairo_surface_create_similar(cairo_get_target(ref_cr),
289 cairo_t *cr = cairo_create (_analysis_scale_surface);
291 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
292 cairo_rectangle(cr, 0.0, 0.0, _analysis_width, _analysis_height);
296 draw_scales_power(_analysis_area, cr);
297 if (_phase_button->get_active()) {
298 draw_scales_phase(_analysis_area, cr);
306 PluginEqGui::redraw_analysis_area()
310 cr = gdk_cairo_create(GDK_DRAWABLE(_analysis_area->get_window()->gobj()));
312 if (_analysis_scale_surface == 0) {
313 draw_analysis_scales(cr);
319 cairo_set_source_surface(cr, _analysis_scale_surface, 0.0, 0.0);
322 if (_phase_button->get_active()) {
323 plot_phase(_analysis_area, cr);
325 plot_amplitude(_analysis_area, cr);
333 #define PHASE_PROPORTION 0.6
336 PluginEqGui::draw_scales_phase(Gtk::Widget *w, cairo_t *cr)
339 cairo_font_extents_t extents;
340 cairo_font_extents(cr, &extents);
343 cairo_text_extents_t t_ext;
345 for (uint32_t i = 0; i < 3; i++) {
347 y = _analysis_height/2.0 - (float)i*(_analysis_height/8.0)*PHASE_PROPORTION;
349 cairo_set_source_rgb(cr, .8, .9, 0.2);
351 snprintf(buf,256, "0\u00b0");
353 snprintf(buf,256, "%d\u00b0", (i * 45));
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);
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);
368 y = _analysis_height/2.0 + (float)i*(_analysis_height/8.0)*PHASE_PROPORTION;
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);
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);
382 cairo_set_line_width (cr, 0.25 + 1.0/(float)(i+1));
388 PluginEqGui::plot_phase(Gtk::Widget *w, cairo_t *cr)
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;
402 y = _analysis_height/2.0 - (_impulse_fft->phase_at_bin(i)/M_PI)*(_analysis_height/2.0)*PHASE_PROPORTION;
405 cairo_move_to(cr, x, y);
409 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
410 cairo_line_to(cr, prevX, avgY/(float)avgNum);
422 cairo_set_line_width (cr, 2.0);
427 PluginEqGui::draw_scales_power(Gtk::Widget *w, cairo_t *cr)
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 };
431 float divisor = _samplerate / 2.0 / _impulse_fft->bins();
434 cairo_set_line_width (cr, 1.5);
435 cairo_set_font_size(cr, 9);
437 cairo_font_extents_t extents;
438 cairo_font_extents(cr, &extents);
439 float fontXOffset = extents.descent + 1.0;
443 for (uint32_t i = 0; scales[i] != -1.0; ++i) {
444 float bin = scales[i] / divisor;
446 x = log10f(1.0 + bin / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
447 x *= _analysis_width;
449 if (scales[i] < 1000.0) {
450 snprintf(buf, 256, "%0.0f", scales[i]);
452 snprintf(buf, 256, "%0.0fk", scales[i]/1000.0);
455 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
457 cairo_move_to(cr, x + fontXOffset, 3.0);
459 cairo_rotate(cr, M_PI / 2.0);
460 cairo_show_text(cr, buf);
461 cairo_rotate(cr, -M_PI / 2.0);
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);
472 //double dashes[] = { 1.0, 3.0, 4.5, 3.0 };
473 double dashes[] = { 3.0, 5.0 };
475 for (float dB = 0.0; dB < _max_dB; dB += _step_dB ) {
476 snprintf(buf, 256, "+%0.0f", dB );
478 y = ( _max_dB - dB) / ( _max_dB - _min_dB );
479 //std::cerr << " y = " << y << std::endl;
480 y *= _analysis_height;
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);
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);
495 cairo_set_dash(cr, dashes, 2, 0.0);
501 for (float dB = - _step_dB; dB > _min_dB; dB -= _step_dB ) {
502 snprintf(buf, 256, "%0.0f", dB );
504 y = ( _max_dB - dB) / ( _max_dB - _min_dB );
505 y *= _analysis_height;
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);
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);
518 cairo_set_dash(cr, 0, 0, 0.0);
525 return 10.0 * log10f(a);
529 PluginEqGui::plot_amplitude(Gtk::Widget *w, cairo_t *cr)
537 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
538 cairo_set_line_width (cr, 2.5);
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;
545 float yCoeff = ( power_to_dB(_impulse_fft->power_at_bin(i)) - _min_dB) / (_max_dB - _min_dB);
547 y = _analysis_height - _analysis_height*yCoeff;
550 cairo_move_to(cr, x, y);
554 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
555 cairo_line_to(cr, prevX, avgY/(float)avgNum);