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"
28 #include "ardour/chan_mapping.h"
29 #include "ardour/session.h"
31 #include <gtkmm/box.h>
32 #include <gtkmm/button.h>
33 #include <gtkmm/checkbutton.h>
38 PluginEqGui::PluginEqGui(boost::shared_ptr<ARDOUR::PluginInsert> pluginInsert)
44 _signal_output_fft(0),
45 _plugin_insert(pluginInsert)
47 _signal_analysis_running = false;
48 _samplerate = ARDOUR_UI::instance()->the_session()->frame_rate();
50 _plugin = _plugin_insert->get_impulse_analysis_plugin();
53 set_buffer_size(4096, 16384);
54 //set_buffer_size(4096, 4096);
56 _log_coeff = (1.0 - 2.0 * (1000.0/(_samplerate/2.0))) / powf(1000.0/(_samplerate/2.0), 2.0);
57 _log_max = log10f(1 + _log_coeff);
60 // Setup analysis drawing area
61 _analysis_scale_surface = 0;
63 _analysis_area = new Gtk::DrawingArea();
64 _analysis_width = 256.0;
65 _analysis_height = 256.0;
66 _analysis_area->set_size_request(_analysis_width, _analysis_height);
68 _analysis_area->signal_expose_event().connect( sigc::mem_fun (*this, &PluginEqGui::expose_analysis_area));
69 _analysis_area->signal_size_allocate().connect( sigc::mem_fun (*this, &PluginEqGui::resize_analysis_area));
72 dBScaleModel = Gtk::ListStore::create(dBColumns);
74 dBScaleCombo = new Gtk::ComboBox(dBScaleModel);
75 dBScaleCombo->set_title (_("dB scale"));
77 #define ADD_DB_ROW(MIN,MAX,STEP,NAME) \
79 Gtk::TreeModel::Row row = *(dBScaleModel->append()); \
80 row[dBColumns.dBMin] = (MIN); \
81 row[dBColumns.dBMax] = (MAX); \
82 row[dBColumns.dBStep] = (STEP); \
83 row[dBColumns.name] = NAME; \
86 ADD_DB_ROW( -6, +6, 1, "-6dB .. +6dB");
87 ADD_DB_ROW(-12, +12, 3, "-12dB .. +12dB");
88 ADD_DB_ROW(-24, +24, 5, "-24dB .. +24dB");
89 ADD_DB_ROW(-36, +36, 6, "-36dB .. +36dB");
90 ADD_DB_ROW(-64, +64,12, "-64dB .. +64dB");
94 dBScaleCombo -> pack_start(dBColumns.name);
95 dBScaleCombo -> set_active(1);
97 dBScaleCombo -> signal_changed().connect( sigc::mem_fun(*this, &PluginEqGui::change_dB_scale) );
99 Gtk::Label *dBComboLabel = new Gtk::Label (_("dB scale"));
101 Gtk::HBox *dBSelectBin = new Gtk::HBox(false, 5);
102 dBSelectBin->add( *manage(dBComboLabel));
103 dBSelectBin->add( *manage(dBScaleCombo));
106 _phase_button = new Gtk::CheckButton (_("Show phase"));
107 _phase_button->set_active(true);
108 _phase_button->signal_toggled().connect( sigc::mem_fun(*this, &PluginEqGui::redraw_scales));
111 attach( *manage(_analysis_area), 1, 3, 1, 2);
112 attach( *manage(dBSelectBin), 1, 2, 2, 3, Gtk::SHRINK, Gtk::SHRINK);
113 attach( *manage(_phase_button), 2, 3, 2, 3, Gtk::SHRINK, Gtk::SHRINK);
116 // Connect the realtime signal collection callback
117 _plugin_insert->AnalysisDataGathered.connect (analysis_connection, invalidator (*this), ui_bind (&PluginEqGui::signal_collect_callback, this, _1, _2), gui_context());
120 PluginEqGui::~PluginEqGui()
122 if (_analysis_scale_surface) {
123 cairo_surface_destroy (_analysis_scale_surface);
127 delete _signal_input_fft;
128 delete _signal_output_fft;
130 _plugin->deactivate();
132 // all gui objects are *manage'd by the inherited Table object
137 PluginEqGui::on_hide()
140 Gtk::Table::on_hide();
144 PluginEqGui::stop_updating()
146 if (_update_connection.connected()) {
147 _update_connection.disconnect();
152 PluginEqGui::start_updating()
154 if (!_update_connection.connected() && is_visible()) {
155 _update_connection = Glib::signal_timeout().connect( sigc::mem_fun(this, &PluginEqGui::timeout_callback), 250);
160 PluginEqGui::on_show()
162 Gtk::Table::on_show();
166 Gtk::Widget *toplevel = get_toplevel();
168 std::cerr << "No toplevel widget for PluginEqGui?!?!" << std::endl;
171 if (!_window_unmap_connection.connected()) {
172 _window_unmap_connection = toplevel->signal_unmap().connect( sigc::mem_fun(this, &PluginEqGui::stop_updating));
175 if (!_window_map_connection.connected()) {
176 _window_map_connection = toplevel->signal_map().connect( sigc::mem_fun(this, &PluginEqGui::start_updating));
182 PluginEqGui::change_dB_scale()
184 Gtk::TreeModel::iterator iter = dBScaleCombo -> get_active();
186 Gtk::TreeModel::Row row;
188 if(iter && (row = *iter)) {
189 _min_dB = row[dBColumns.dBMin];
190 _max_dB = row[dBColumns.dBMax];
191 _step_dB = row[dBColumns.dBStep];
199 PluginEqGui::redraw_scales()
202 if (_analysis_scale_surface) {
203 cairo_surface_destroy (_analysis_scale_surface);
204 _analysis_scale_surface = 0;
207 _analysis_area->queue_draw();
209 // TODO: Add graph legend!
213 PluginEqGui::set_buffer_size(uint32_t size, uint32_t signal_size)
215 if (_buffer_size == size && _signal_buffer_size == signal_size)
219 FFT *tmp1 = _impulse_fft;
220 FFT *tmp2 = _signal_input_fft;
221 FFT *tmp3 = _signal_output_fft;
224 _impulse_fft = new FFT(size);
225 _signal_input_fft = new FFT(signal_size);
226 _signal_output_fft = new FFT(signal_size);
228 // Don't care about lost memory, we're screwed anyhow
230 _signal_input_fft = tmp2;
231 _signal_output_fft = tmp3;
240 _signal_buffer_size = signal_size;
242 ARDOUR::ChanCount count = ARDOUR::ChanCount::max (_plugin->get_info()->n_inputs, _plugin->get_info()->n_outputs);
243 for (ARDOUR::DataType::iterator i = ARDOUR::DataType::begin(); i != ARDOUR::DataType::end(); ++i) {
244 _bufferset.ensure_buffers (*i, count.get (*i), _buffer_size);
245 _collect_bufferset.ensure_buffers (*i, count.get (*i), _buffer_size);
248 _bufferset.set_count (count);
249 _collect_bufferset.set_count (count);
253 PluginEqGui::resize_analysis_area(Gtk::Allocation& size)
255 _analysis_width = (float)size.get_width();
256 _analysis_height = (float)size.get_height();
258 if (_analysis_scale_surface) {
259 cairo_surface_destroy (_analysis_scale_surface);
260 _analysis_scale_surface = 0;
265 PluginEqGui::timeout_callback()
267 if (!_signal_analysis_running) {
268 _signal_analysis_running = true;
269 _plugin_insert -> collect_signal_for_analysis(_signal_buffer_size);
271 run_impulse_analysis();
277 PluginEqGui::signal_collect_callback(ARDOUR::BufferSet *in, ARDOUR::BufferSet *out)
279 ENSURE_GUI_THREAD (*this, &PluginEqGui::signal_collect_callback, in, out)
281 _signal_input_fft ->reset();
282 _signal_output_fft->reset();
284 for (uint32_t i = 0; i < _plugin_insert->input_streams().n_audio(); ++i) {
285 _signal_input_fft ->analyze(in ->get_audio(i).data(), FFT::HANN);
288 for (uint32_t i = 0; i < _plugin_insert->output_streams().n_audio(); ++i) {
289 _signal_output_fft->analyze(out->get_audio(i).data(), FFT::HANN);
292 _signal_input_fft ->calculate();
293 _signal_output_fft->calculate();
295 _signal_analysis_running = false;
297 // This signals calls expose_analysis_area()
298 _analysis_area->queue_draw();
302 PluginEqGui::run_impulse_analysis()
304 uint32_t inputs = _plugin->get_info()->n_inputs.n_audio();
305 uint32_t outputs = _plugin->get_info()->n_outputs.n_audio();
307 // Create the impulse, can't use silence() because consecutive calls won't work
308 for (uint32_t i = 0; i < inputs; ++i) {
309 ARDOUR::AudioBuffer& buf = _bufferset.get_audio(i);
310 ARDOUR::Sample* d = buf.data();
311 memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size);
315 ARDOUR::ChanMapping in_map(_plugin->get_info()->n_inputs);
316 ARDOUR::ChanMapping out_map(_plugin->get_info()->n_outputs);
318 _plugin->connect_and_run(_bufferset, in_map, out_map, _buffer_size, (nframes_t)0);
319 nframes_t f = _plugin->signal_latency();
320 // Adding user_latency() could be interesting
322 // Gather all output, taking latency into account.
323 _impulse_fft->reset();
325 // Silence collect buffers to copy data to, can't use silence() because consecutive calls won't work
326 for (uint32_t i = 0; i < outputs; ++i) {
327 ARDOUR::AudioBuffer &buf = _collect_bufferset.get_audio(i);
328 ARDOUR::Sample *d = buf.data();
329 memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size);
333 //std::cerr << "0: no latency, copying full buffer, trivial.." << std::endl;
334 for (uint32_t i = 0; i < outputs; ++i) {
335 memcpy(_collect_bufferset.get_audio(i).data(),
336 _bufferset.get_audio(i).data(), _buffer_size * sizeof(float));
340 //std::cerr << (++C) << ": latency is " << f << " frames, doing split processing.." << std::endl;
341 nframes_t target_offset = 0;
342 nframes_t frames_left = _buffer_size; // refaktoroi
344 if (f >= _buffer_size) {
345 //std::cerr << (++C) << ": f (=" << f << ") is larger than buffer_size, still trying to reach the actual output" << std::endl;
346 // there is no data in this buffer regarding to the input!
349 // this buffer contains either the first, last or a whole bu the output of the impulse
350 // first part: offset is 0, so we copy to the start of _collect_bufferset
351 // we start at output offset "f"
352 // .. and copy "buffer size" - "f" - "offset" frames
354 nframes_t length = _buffer_size - f - target_offset;
356 //std::cerr << (++C) << ": copying " << length << " frames to _collect_bufferset.get_audio(i)+" << target_offset << " from bufferset at offset " << f << std::endl;
357 for (uint32_t i = 0; i < outputs; ++i) {
358 memcpy(_collect_bufferset.get_audio(i).data(target_offset),
359 _bufferset.get_audio(i).data() + f,
360 length * sizeof(float));
363 target_offset += length;
364 frames_left -= length;
367 if (frames_left > 0) {
368 // Silence the buffers
369 for (uint32_t i = 0; i < inputs; ++i) {
370 ARDOUR::AudioBuffer &buf = _bufferset.get_audio(i);
371 ARDOUR::Sample *d = buf.data();
372 memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size);
375 in_map = ARDOUR::ChanMapping(_plugin->get_info()->n_inputs);
376 out_map = ARDOUR::ChanMapping(_plugin->get_info()->n_outputs);
377 _plugin->connect_and_run(_bufferset, in_map, out_map, _buffer_size, (nframes_t)0);
379 } while ( frames_left > 0);
384 for (uint32_t i = 0; i < outputs; ++i) {
385 _impulse_fft->analyze(_collect_bufferset.get_audio(i).data());
388 // normalize the output
389 _impulse_fft->calculate();
391 // This signals calls expose_analysis_area()
392 _analysis_area->queue_draw();
396 PluginEqGui::expose_analysis_area(GdkEventExpose *)
398 redraw_analysis_area();
404 PluginEqGui::draw_analysis_scales(cairo_t *ref_cr)
406 // TODO: check whether we need rounding
407 _analysis_scale_surface = cairo_surface_create_similar(cairo_get_target(ref_cr),
412 cairo_t *cr = cairo_create (_analysis_scale_surface);
414 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
415 cairo_rectangle(cr, 0.0, 0.0, _analysis_width, _analysis_height);
419 draw_scales_power(_analysis_area, cr);
420 if (_phase_button->get_active()) {
421 draw_scales_phase(_analysis_area, cr);
429 PluginEqGui::redraw_analysis_area()
433 cr = gdk_cairo_create(GDK_DRAWABLE(_analysis_area->get_window()->gobj()));
435 if (_analysis_scale_surface == 0) {
436 draw_analysis_scales(cr);
442 cairo_set_source_surface(cr, _analysis_scale_surface, 0.0, 0.0);
445 if (_phase_button->get_active()) {
446 plot_impulse_phase(_analysis_area, cr);
448 plot_impulse_amplitude(_analysis_area, cr);
450 // TODO: make this optional
451 plot_signal_amplitude_difference(_analysis_area, cr);
458 #define PHASE_PROPORTION 0.5
461 PluginEqGui::draw_scales_phase(Gtk::Widget */*w*/, cairo_t *cr)
464 cairo_font_extents_t extents;
465 cairo_font_extents(cr, &extents);
468 cairo_text_extents_t t_ext;
470 for (uint32_t i = 0; i < 3; i++) {
472 y = _analysis_height/2.0 - (float)i*(_analysis_height/8.0)*PHASE_PROPORTION;
474 cairo_set_source_rgb(cr, .8, .9, 0.2);
476 snprintf(buf,256, "0\u00b0");
478 snprintf(buf,256, "%d\u00b0", (i * 45));
480 cairo_text_extents(cr, buf, &t_ext);
481 cairo_move_to(cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
482 cairo_show_text(cr, buf);
488 cairo_set_source_rgba(cr, .8, .9, 0.2, 0.6/(float)i);
489 cairo_move_to(cr, 0.0, y);
490 cairo_line_to(cr, _analysis_width, y);
493 y = _analysis_height/2.0 + (float)i*(_analysis_height/8.0)*PHASE_PROPORTION;
496 snprintf(buf,256, "-%d\u00b0", (i * 45));
497 cairo_set_source_rgb(cr, .8, .9, 0.2);
498 cairo_text_extents(cr, buf, &t_ext);
499 cairo_move_to(cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
500 cairo_show_text(cr, buf);
503 cairo_set_source_rgba(cr, .8, .9, 0.2, 0.6/(float)i);
504 cairo_move_to(cr, 0.0, y);
505 cairo_line_to(cr, _analysis_width, y);
507 cairo_set_line_width (cr, 0.25 + 1.0/(float)(i+1));
513 PluginEqGui::plot_impulse_phase(Gtk::Widget *w, cairo_t *cr)
521 // float width = w->get_width();
522 float height = w->get_height();
524 cairo_set_source_rgba(cr, 0.95, 0.3, 0.2, 1.0);
525 for (uint32_t i = 0; i < _impulse_fft->bins()-1; i++) {
526 // x coordinate of bin i
527 x = log10f(1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
528 x *= _analysis_width;
530 y = _analysis_height/2.0 - (_impulse_fft->phase_at_bin(i)/M_PI)*(_analysis_height/2.0)*PHASE_PROPORTION;
533 cairo_move_to(cr, x, y);
537 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
538 avgY = avgY/(float)avgNum;
539 if (avgY > (height * 10.0) ) avgY = height * 10.0;
540 if (avgY < (-height * 10.0) ) avgY = -height * 10.0;
541 cairo_line_to(cr, prevX, avgY);
542 //cairo_line_to(cr, prevX, avgY/(float)avgNum);
554 cairo_set_line_width (cr, 2.0);
559 PluginEqGui::draw_scales_power(Gtk::Widget */*w*/, cairo_t *cr)
561 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 };
563 float divisor = _samplerate / 2.0 / _impulse_fft->bins();
566 cairo_set_line_width (cr, 1.5);
567 cairo_set_font_size(cr, 9);
569 cairo_font_extents_t extents;
570 cairo_font_extents(cr, &extents);
571 // float fontXOffset = extents.descent + 1.0;
575 for (uint32_t i = 0; scales[i] != -1.0; ++i) {
576 float bin = scales[i] / divisor;
578 x = log10f(1.0 + bin / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
579 x *= _analysis_width;
581 if (scales[i] < 1000.0) {
582 snprintf(buf, 256, "%0.0f", scales[i]);
584 snprintf(buf, 256, "%0.0fk", scales[i]/1000.0);
587 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
589 //cairo_move_to(cr, x + fontXOffset, 3.0);
590 cairo_move_to(cr, x - extents.height, 3.0);
592 cairo_rotate(cr, M_PI / 2.0);
593 cairo_show_text(cr, buf);
594 cairo_rotate(cr, -M_PI / 2.0);
597 cairo_set_source_rgb(cr, 0.3, 0.3, 0.3);
598 cairo_move_to(cr, x, _analysis_height);
599 cairo_line_to(cr, x, 0.0);
605 //double dashes[] = { 1.0, 3.0, 4.5, 3.0 };
606 double dashes[] = { 3.0, 5.0 };
608 for (float dB = 0.0; dB < _max_dB; dB += _step_dB ) {
609 snprintf(buf, 256, "+%0.0f", dB );
611 y = ( _max_dB - dB) / ( _max_dB - _min_dB );
612 //std::cerr << " y = " << y << std::endl;
613 y *= _analysis_height;
616 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
617 cairo_move_to(cr, 1.0, y + extents.height + 1.0);
618 cairo_show_text(cr, buf);
622 cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
623 cairo_move_to(cr, 0, y);
624 cairo_line_to(cr, _analysis_width, y);
628 cairo_set_dash(cr, dashes, 2, 0.0);
634 for (float dB = - _step_dB; dB > _min_dB; dB -= _step_dB ) {
635 snprintf(buf, 256, "%0.0f", dB );
637 y = ( _max_dB - dB) / ( _max_dB - _min_dB );
638 y *= _analysis_height;
640 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
641 cairo_move_to(cr, 1.0, y - extents.descent - 1.0);
642 cairo_show_text(cr, buf);
645 cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
646 cairo_move_to(cr, 0, y);
647 cairo_line_to(cr, _analysis_width, y);
651 cairo_set_dash(cr, 0, 0, 0.0);
658 return 10.0 * log10f(a);
662 PluginEqGui::plot_impulse_amplitude(Gtk::Widget *w, cairo_t *cr)
670 // float width = w->get_width();
671 float height = w->get_height();
673 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
674 cairo_set_line_width (cr, 2.5);
676 for (uint32_t i = 0; i < _impulse_fft->bins()-1; i++) {
677 // x coordinate of bin i
678 x = log10f(1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
679 x *= _analysis_width;
681 float yCoeff = ( power_to_dB(_impulse_fft->power_at_bin(i)) - _min_dB) / (_max_dB - _min_dB);
683 y = _analysis_height - _analysis_height*yCoeff;
686 cairo_move_to(cr, x, y);
690 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
691 avgY = avgY/(float)avgNum;
692 if (avgY > (height * 10.0) ) avgY = height * 10.0;
693 if (avgY < (-height * 10.0) ) avgY = -height * 10.0;
694 cairo_line_to(cr, prevX, avgY);
695 //cairo_line_to(cr, prevX, avgY/(float)avgNum);
711 PluginEqGui::plot_signal_amplitude_difference(Gtk::Widget *w, cairo_t *cr)
719 // float width = w->get_width();
720 float height = w->get_height();
722 cairo_set_source_rgb(cr, 0.0, 1.0, 0.0);
723 cairo_set_line_width (cr, 2.5);
725 for (uint32_t i = 0; i < _signal_input_fft->bins()-1; i++) {
726 // x coordinate of bin i
727 x = log10f(1.0 + (float)i / (float)_signal_input_fft->bins() * _log_coeff) / _log_max;
728 x *= _analysis_width;
730 float power_out = power_to_dB(_signal_output_fft->power_at_bin(i));
731 float power_in = power_to_dB(_signal_input_fft ->power_at_bin(i));
732 float power = power_out - power_in;
736 double p = 10.0 * log10( 1.0 + (double)_signal_output_fft->power_at_bin(i) - (double)
737 - _signal_input_fft ->power_at_bin(i));
739 float power = (float)p;
741 if ( (i % 1000) == 0) {
742 std::cerr << i << ": " << power << std::endl;
746 if (std::isinf(power)) {
748 power = _min_dB - 1.0;
750 power = _max_dB - 1.0;
752 } else if (std::isnan(power)) {
753 power = _min_dB - 1.0;
756 float yCoeff = ( power - _min_dB) / (_max_dB - _min_dB);
758 y = _analysis_height - _analysis_height*yCoeff;
761 cairo_move_to(cr, x, y);
765 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
766 avgY = avgY/(float)avgNum;
767 if (avgY > (height * 10.0) ) avgY = height * 10.0;
768 if (avgY < (-height * 10.0) ) avgY = -height * 10.0;
769 cairo_line_to(cr, prevX, avgY);