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 using namespace ARDOUR;
40 PluginEqGui::PluginEqGui(boost::shared_ptr<ARDOUR::PluginInsert> pluginInsert)
45 , _signal_input_fft(0)
46 , _signal_output_fft(0)
47 , _plugin_insert(pluginInsert)
49 _signal_analysis_running = false;
50 _samplerate = ARDOUR_UI::instance()->the_session()->frame_rate();
52 _log_coeff = (1.0 - 2.0 * (1000.0/(_samplerate/2.0))) / powf(1000.0/(_samplerate/2.0), 2.0);
53 _log_max = log10f(1 + _log_coeff);
55 // Setup analysis drawing area
56 _analysis_scale_surface = 0;
58 _analysis_area = new Gtk::DrawingArea();
59 _analysis_width = 256.0;
60 _analysis_height = 256.0;
61 _analysis_area->set_size_request(_analysis_width, _analysis_height);
63 _analysis_area->signal_expose_event().connect( sigc::mem_fun (*this, &PluginEqGui::expose_analysis_area));
64 _analysis_area->signal_size_allocate().connect( sigc::mem_fun (*this, &PluginEqGui::resize_analysis_area));
67 dBScaleModel = Gtk::ListStore::create(dBColumns);
69 /* this grotty-looking cast allows compilation against gtkmm 2.24.0, which
70 added a new ComboBox constructor.
72 dBScaleCombo = new Gtk::ComboBox ((Glib::RefPtr<Gtk::TreeModel> &) dBScaleModel);
73 dBScaleCombo->set_title (_("dB scale"));
75 #define ADD_DB_ROW(MIN,MAX,STEP,NAME) \
77 Gtk::TreeModel::Row row = *(dBScaleModel->append()); \
78 row[dBColumns.dBMin] = (MIN); \
79 row[dBColumns.dBMax] = (MAX); \
80 row[dBColumns.dBStep] = (STEP); \
81 row[dBColumns.name] = NAME; \
84 ADD_DB_ROW( -6, +6, 1, "-6dB .. +6dB");
85 ADD_DB_ROW(-12, +12, 3, "-12dB .. +12dB");
86 ADD_DB_ROW(-24, +24, 5, "-24dB .. +24dB");
87 ADD_DB_ROW(-36, +36, 6, "-36dB .. +36dB");
88 ADD_DB_ROW(-64, +64,12, "-64dB .. +64dB");
92 dBScaleCombo -> pack_start(dBColumns.name);
93 dBScaleCombo -> set_active(1);
95 dBScaleCombo -> signal_changed().connect( sigc::mem_fun(*this, &PluginEqGui::change_dB_scale) );
97 Gtk::Label *dBComboLabel = new Gtk::Label (_("dB scale"));
99 Gtk::HBox *dBSelectBin = new Gtk::HBox(false, 5);
100 dBSelectBin->add( *manage(dBComboLabel));
101 dBSelectBin->add( *manage(dBScaleCombo));
104 _phase_button = new Gtk::CheckButton (_("Show phase"));
105 _phase_button->set_active(true);
106 _phase_button->signal_toggled().connect( sigc::mem_fun(*this, &PluginEqGui::redraw_scales));
109 attach( *manage(_analysis_area), 1, 3, 1, 2);
110 attach( *manage(dBSelectBin), 1, 2, 2, 3, Gtk::SHRINK, Gtk::SHRINK);
111 attach( *manage(_phase_button), 2, 3, 2, 3, Gtk::SHRINK, Gtk::SHRINK);
114 PluginEqGui::~PluginEqGui()
118 if (_analysis_scale_surface) {
119 cairo_surface_destroy (_analysis_scale_surface);
124 delete _signal_input_fft;
125 _signal_input_fft = 0;
126 delete _signal_output_fft;
127 _signal_output_fft = 0;
129 // all gui objects are *manage'd by the inherited Table object
133 PluginEqGui::start_listening ()
136 _plugin = _plugin_insert->get_impulse_analysis_plugin();
140 set_buffer_size(4096, 16384);
141 // Connect the realtime signal collection callback
142 _plugin_insert->AnalysisDataGathered.connect (analysis_connection, invalidator (*this), ui_bind (&PluginEqGui::signal_collect_callback, this, _1, _2), gui_context());
146 PluginEqGui::stop_listening ()
148 analysis_connection.disconnect ();
149 _plugin->deactivate ();
153 PluginEqGui::on_hide()
156 Gtk::Table::on_hide();
160 PluginEqGui::stop_updating()
162 if (_update_connection.connected()) {
163 _update_connection.disconnect();
168 PluginEqGui::start_updating()
170 if (!_update_connection.connected() && is_visible()) {
171 _update_connection = Glib::signal_timeout().connect( sigc::mem_fun(this, &PluginEqGui::timeout_callback), 250);
176 PluginEqGui::on_show()
178 Gtk::Table::on_show();
182 Gtk::Widget *toplevel = get_toplevel();
184 if (!_window_unmap_connection.connected()) {
185 _window_unmap_connection = toplevel->signal_unmap().connect( sigc::mem_fun(this, &PluginEqGui::stop_updating));
188 if (!_window_map_connection.connected()) {
189 _window_map_connection = toplevel->signal_map().connect( sigc::mem_fun(this, &PluginEqGui::start_updating));
195 PluginEqGui::change_dB_scale()
197 Gtk::TreeModel::iterator iter = dBScaleCombo -> get_active();
199 Gtk::TreeModel::Row row;
201 if(iter && (row = *iter)) {
202 _min_dB = row[dBColumns.dBMin];
203 _max_dB = row[dBColumns.dBMax];
204 _step_dB = row[dBColumns.dBStep];
212 PluginEqGui::redraw_scales()
215 if (_analysis_scale_surface) {
216 cairo_surface_destroy (_analysis_scale_surface);
217 _analysis_scale_surface = 0;
220 _analysis_area->queue_draw();
222 // TODO: Add graph legend!
226 PluginEqGui::set_buffer_size(uint32_t size, uint32_t signal_size)
228 if (_buffer_size == size && _signal_buffer_size == signal_size) {
232 GTKArdour::FFT *tmp1 = _impulse_fft;
233 GTKArdour::FFT *tmp2 = _signal_input_fft;
234 GTKArdour::FFT *tmp3 = _signal_output_fft;
237 _impulse_fft = new GTKArdour::FFT(size);
238 _signal_input_fft = new GTKArdour::FFT(signal_size);
239 _signal_output_fft = new GTKArdour::FFT(signal_size);
241 // Don't care about lost memory, we're screwed anyhow
243 _signal_input_fft = tmp2;
244 _signal_output_fft = tmp3;
253 _signal_buffer_size = signal_size;
255 ARDOUR::ChanCount count = ARDOUR::ChanCount::max (_plugin->get_info()->n_inputs, _plugin->get_info()->n_outputs);
257 for (ARDOUR::DataType::iterator i = ARDOUR::DataType::begin(); i != ARDOUR::DataType::end(); ++i) {
258 _bufferset.ensure_buffers (*i, count.get (*i), _buffer_size);
259 _collect_bufferset.ensure_buffers (*i, count.get (*i), _buffer_size);
262 _bufferset.set_count (count);
263 _collect_bufferset.set_count (count);
267 PluginEqGui::resize_analysis_area (Gtk::Allocation& size)
269 _analysis_width = (float)size.get_width();
270 _analysis_height = (float)size.get_height();
272 if (_analysis_scale_surface) {
273 cairo_surface_destroy (_analysis_scale_surface);
274 _analysis_scale_surface = 0;
279 PluginEqGui::timeout_callback()
281 if (!_signal_analysis_running) {
282 _signal_analysis_running = true;
283 _plugin_insert -> collect_signal_for_analysis(_signal_buffer_size);
285 run_impulse_analysis();
291 PluginEqGui::signal_collect_callback(ARDOUR::BufferSet *in, ARDOUR::BufferSet *out)
293 ENSURE_GUI_THREAD (*this, &PluginEqGui::signal_collect_callback, in, out)
295 _signal_input_fft ->reset();
296 _signal_output_fft->reset();
298 for (uint32_t i = 0; i < _plugin_insert->input_streams().n_audio(); ++i) {
299 _signal_input_fft ->analyze(in ->get_audio(i).data(), GTKArdour::FFT::HANN);
302 for (uint32_t i = 0; i < _plugin_insert->output_streams().n_audio(); ++i) {
303 _signal_output_fft->analyze(out->get_audio(i).data(), GTKArdour::FFT::HANN);
306 _signal_input_fft ->calculate();
307 _signal_output_fft->calculate();
309 _signal_analysis_running = false;
311 // This signals calls expose_analysis_area()
312 _analysis_area->queue_draw();
316 PluginEqGui::run_impulse_analysis()
318 uint32_t inputs = _plugin->get_info()->n_inputs.n_audio();
319 uint32_t outputs = _plugin->get_info()->n_outputs.n_audio();
321 // Create the impulse, can't use silence() because consecutive calls won't work
322 for (uint32_t i = 0; i < inputs; ++i) {
323 ARDOUR::AudioBuffer& buf = _bufferset.get_audio(i);
324 ARDOUR::Sample* d = buf.data();
325 memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size);
329 ARDOUR::ChanMapping in_map(_plugin->get_info()->n_inputs);
330 ARDOUR::ChanMapping out_map(_plugin->get_info()->n_outputs);
332 _plugin->connect_and_run(_bufferset, in_map, out_map, _buffer_size, 0);
333 framecnt_t f = _plugin->signal_latency ();
334 // Adding user_latency() could be interesting
336 // Gather all output, taking latency into account.
337 _impulse_fft->reset();
339 // Silence collect buffers to copy data to, can't use silence() because consecutive calls won't work
340 for (uint32_t i = 0; i < outputs; ++i) {
341 ARDOUR::AudioBuffer &buf = _collect_bufferset.get_audio(i);
342 ARDOUR::Sample *d = buf.data();
343 memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size);
347 //std::cerr << "0: no latency, copying full buffer, trivial.." << std::endl;
348 for (uint32_t i = 0; i < outputs; ++i) {
349 memcpy(_collect_bufferset.get_audio(i).data(),
350 _bufferset.get_audio(i).data(), _buffer_size * sizeof(float));
354 //std::cerr << (++C) << ": latency is " << f << " frames, doing split processing.." << std::endl;
355 framecnt_t target_offset = 0;
356 framecnt_t frames_left = _buffer_size; // refaktoroi
358 if (f >= _buffer_size) {
359 //std::cerr << (++C) << ": f (=" << f << ") is larger than buffer_size, still trying to reach the actual output" << std::endl;
360 // there is no data in this buffer regarding to the input!
363 // this buffer contains either the first, last or a whole bu the output of the impulse
364 // first part: offset is 0, so we copy to the start of _collect_bufferset
365 // we start at output offset "f"
366 // .. and copy "buffer size" - "f" - "offset" frames
368 framecnt_t length = _buffer_size - f - target_offset;
370 //std::cerr << (++C) << ": copying " << length << " frames to _collect_bufferset.get_audio(i)+" << target_offset << " from bufferset at offset " << f << std::endl;
371 for (uint32_t i = 0; i < outputs; ++i) {
372 memcpy(_collect_bufferset.get_audio(i).data(target_offset),
373 _bufferset.get_audio(i).data() + f,
374 length * sizeof(float));
377 target_offset += length;
378 frames_left -= length;
381 if (frames_left > 0) {
382 // Silence the buffers
383 for (uint32_t i = 0; i < inputs; ++i) {
384 ARDOUR::AudioBuffer &buf = _bufferset.get_audio(i);
385 ARDOUR::Sample *d = buf.data();
386 memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size);
389 in_map = ARDOUR::ChanMapping(_plugin->get_info()->n_inputs);
390 out_map = ARDOUR::ChanMapping(_plugin->get_info()->n_outputs);
391 _plugin->connect_and_run(_bufferset, in_map, out_map, _buffer_size, 0);
393 } while ( frames_left > 0);
398 for (uint32_t i = 0; i < outputs; ++i) {
399 _impulse_fft->analyze(_collect_bufferset.get_audio(i).data());
402 // normalize the output
403 _impulse_fft->calculate();
405 // This signals calls expose_analysis_area()
406 _analysis_area->queue_draw();
410 PluginEqGui::expose_analysis_area(GdkEventExpose *)
412 redraw_analysis_area();
417 PluginEqGui::draw_analysis_scales(cairo_t *ref_cr)
419 // TODO: check whether we need rounding
420 _analysis_scale_surface = cairo_surface_create_similar(cairo_get_target(ref_cr),
425 cairo_t *cr = cairo_create (_analysis_scale_surface);
427 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
428 cairo_rectangle(cr, 0.0, 0.0, _analysis_width, _analysis_height);
432 draw_scales_power(_analysis_area, cr);
433 if (_phase_button->get_active()) {
434 draw_scales_phase(_analysis_area, cr);
442 PluginEqGui::redraw_analysis_area()
446 cr = gdk_cairo_create(GDK_DRAWABLE(_analysis_area->get_window()->gobj()));
448 if (_analysis_scale_surface == 0) {
449 draw_analysis_scales(cr);
455 cairo_set_source_surface(cr, _analysis_scale_surface, 0.0, 0.0);
458 if (_phase_button->get_active()) {
459 plot_impulse_phase(_analysis_area, cr);
461 plot_impulse_amplitude(_analysis_area, cr);
463 // TODO: make this optional
464 plot_signal_amplitude_difference(_analysis_area, cr);
471 #define PHASE_PROPORTION 0.5
474 PluginEqGui::draw_scales_phase(Gtk::Widget */*w*/, cairo_t *cr)
477 cairo_font_extents_t extents;
478 cairo_font_extents(cr, &extents);
481 cairo_text_extents_t t_ext;
483 for (uint32_t i = 0; i < 3; i++) {
485 y = _analysis_height/2.0 - (float)i*(_analysis_height/8.0)*PHASE_PROPORTION;
487 cairo_set_source_rgb(cr, .8, .9, 0.2);
489 snprintf(buf,256, "0\u00b0");
491 snprintf(buf,256, "%d\u00b0", (i * 45));
493 cairo_text_extents(cr, buf, &t_ext);
494 cairo_move_to(cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
495 cairo_show_text(cr, buf);
501 cairo_set_source_rgba(cr, .8, .9, 0.2, 0.6/(float)i);
502 cairo_move_to(cr, 0.0, y);
503 cairo_line_to(cr, _analysis_width, y);
506 y = _analysis_height/2.0 + (float)i*(_analysis_height/8.0)*PHASE_PROPORTION;
509 snprintf(buf,256, "-%d\u00b0", (i * 45));
510 cairo_set_source_rgb(cr, .8, .9, 0.2);
511 cairo_text_extents(cr, buf, &t_ext);
512 cairo_move_to(cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
513 cairo_show_text(cr, buf);
516 cairo_set_source_rgba(cr, .8, .9, 0.2, 0.6/(float)i);
517 cairo_move_to(cr, 0.0, y);
518 cairo_line_to(cr, _analysis_width, y);
520 cairo_set_line_width (cr, 0.25 + 1.0/(float)(i+1));
526 PluginEqGui::plot_impulse_phase(Gtk::Widget *w, cairo_t *cr)
534 // float width = w->get_width();
535 float height = w->get_height();
537 cairo_set_source_rgba(cr, 0.95, 0.3, 0.2, 1.0);
538 for (uint32_t i = 0; i < _impulse_fft->bins()-1; i++) {
539 // x coordinate of bin i
540 x = log10f(1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
541 x *= _analysis_width;
543 y = _analysis_height/2.0 - (_impulse_fft->phase_at_bin(i)/M_PI)*(_analysis_height/2.0)*PHASE_PROPORTION;
546 cairo_move_to(cr, x, y);
550 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
551 avgY = avgY/(float)avgNum;
552 if (avgY > (height * 10.0) ) avgY = height * 10.0;
553 if (avgY < (-height * 10.0) ) avgY = -height * 10.0;
554 cairo_line_to(cr, prevX, avgY);
555 //cairo_line_to(cr, prevX, avgY/(float)avgNum);
567 cairo_set_line_width (cr, 2.0);
572 PluginEqGui::draw_scales_power(Gtk::Widget */*w*/, cairo_t *cr)
574 if (_impulse_fft == 0) {
578 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 };
579 float divisor = _samplerate / 2.0 / _impulse_fft->bins();
582 cairo_set_line_width (cr, 1.5);
583 cairo_set_font_size(cr, 9);
585 cairo_font_extents_t extents;
586 cairo_font_extents(cr, &extents);
587 // float fontXOffset = extents.descent + 1.0;
591 for (uint32_t i = 0; scales[i] != -1.0; ++i) {
592 float bin = scales[i] / divisor;
594 x = log10f(1.0 + bin / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
595 x *= _analysis_width;
597 if (scales[i] < 1000.0) {
598 snprintf(buf, 256, "%0.0f", scales[i]);
600 snprintf(buf, 256, "%0.0fk", scales[i]/1000.0);
603 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
605 //cairo_move_to(cr, x + fontXOffset, 3.0);
606 cairo_move_to(cr, x - extents.height, 3.0);
608 cairo_rotate(cr, M_PI / 2.0);
609 cairo_show_text(cr, buf);
610 cairo_rotate(cr, -M_PI / 2.0);
613 cairo_set_source_rgb(cr, 0.3, 0.3, 0.3);
614 cairo_move_to(cr, x, _analysis_height);
615 cairo_line_to(cr, x, 0.0);
621 //double dashes[] = { 1.0, 3.0, 4.5, 3.0 };
622 double dashes[] = { 3.0, 5.0 };
624 for (float dB = 0.0; dB < _max_dB; dB += _step_dB ) {
625 snprintf(buf, 256, "+%0.0f", dB );
627 y = ( _max_dB - dB) / ( _max_dB - _min_dB );
628 //std::cerr << " y = " << y << std::endl;
629 y *= _analysis_height;
632 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
633 cairo_move_to(cr, 1.0, y + extents.height + 1.0);
634 cairo_show_text(cr, buf);
638 cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
639 cairo_move_to(cr, 0, y);
640 cairo_line_to(cr, _analysis_width, y);
644 cairo_set_dash(cr, dashes, 2, 0.0);
650 for (float dB = - _step_dB; dB > _min_dB; dB -= _step_dB ) {
651 snprintf(buf, 256, "%0.0f", dB );
653 y = ( _max_dB - dB) / ( _max_dB - _min_dB );
654 y *= _analysis_height;
656 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
657 cairo_move_to(cr, 1.0, y - extents.descent - 1.0);
658 cairo_show_text(cr, buf);
661 cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
662 cairo_move_to(cr, 0, y);
663 cairo_line_to(cr, _analysis_width, y);
667 cairo_set_dash(cr, 0, 0, 0.0);
674 return 10.0 * log10f(a);
678 PluginEqGui::plot_impulse_amplitude(Gtk::Widget *w, cairo_t *cr)
685 // float width = w->get_width();
686 float height = w->get_height();
688 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
689 cairo_set_line_width (cr, 2.5);
691 for (uint32_t i = 0; i < _impulse_fft->bins()-1; i++) {
692 // x coordinate of bin i
693 x = log10f(1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
694 x *= _analysis_width;
696 float yCoeff = ( power_to_dB(_impulse_fft->power_at_bin(i)) - _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);
710 //cairo_line_to(cr, prevX, avgY/(float)avgNum);
726 PluginEqGui::plot_signal_amplitude_difference(Gtk::Widget *w, cairo_t *cr)
734 // float width = w->get_width();
735 float height = w->get_height();
737 cairo_set_source_rgb(cr, 0.0, 1.0, 0.0);
738 cairo_set_line_width (cr, 2.5);
740 for (uint32_t i = 0; i < _signal_input_fft->bins()-1; i++) {
741 // x coordinate of bin i
742 x = log10f(1.0 + (float)i / (float)_signal_input_fft->bins() * _log_coeff) / _log_max;
743 x *= _analysis_width;
745 float power_out = power_to_dB(_signal_output_fft->power_at_bin(i));
746 float power_in = power_to_dB(_signal_input_fft ->power_at_bin(i));
747 float power = power_out - power_in;
751 double p = 10.0 * log10( 1.0 + (double)_signal_output_fft->power_at_bin(i) - (double)
752 - _signal_input_fft ->power_at_bin(i));
754 float power = (float)p;
756 if ( (i % 1000) == 0) {
757 std::cerr << i << ": " << power << std::endl;
761 if (std::isinf(power)) {
763 power = _min_dB - 1.0;
765 power = _max_dB - 1.0;
767 } else if (std::isnan(power)) {
768 power = _min_dB - 1.0;
771 float yCoeff = ( power - _min_dB) / (_max_dB - _min_dB);
773 y = _analysis_height - _analysis_height*yCoeff;
776 cairo_move_to(cr, x, y);
780 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
781 avgY = avgY/(float)avgNum;
782 if (avgY > (height * 10.0) ) avgY = height * 10.0;
783 if (avgY < (-height * 10.0) ) avgY = -height * 10.0;
784 cairo_line_to(cr, prevX, avgY);