X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=gtk2_ardour%2Fplugin_eq_gui.cc;h=631fb7be716f50d17993048ce134b3e51cc8b7f5;hb=6ee23029a338951705c589be6c61ab52099758b6;hp=a7e6b4261c44e7dd86ed363ab779d7304b452b39;hpb=3d239bb9d59f555d08fd9362e9ce50de1255c633;p=ardour.git diff --git a/gtk2_ardour/plugin_eq_gui.cc b/gtk2_ardour/plugin_eq_gui.cc index a7e6b4261c..631fb7be71 100644 --- a/gtk2_ardour/plugin_eq_gui.cc +++ b/gtk2_ardour/plugin_eq_gui.cc @@ -23,8 +23,10 @@ #include "ardour_ui.h" #include "gui_thread.h" -#include -#include +#include "ardour/audio_buffer.h" +#include "ardour/data_type.h" +#include "ardour/chan_mapping.h" +#include "ardour/session.h" #include #include @@ -33,45 +35,44 @@ #include #include +#include "i18n.h" + +using namespace ARDOUR; + PluginEqGui::PluginEqGui(boost::shared_ptr pluginInsert) - : _min_dB(-12.0), - _max_dB(+12.0), - _step_dB(3.0), - _impulse_fft(0), - _signal_input_fft(0), - _signal_output_fft(0), - _plugin_insert(pluginInsert) + : _min_dB(-12.0) + , _max_dB(+12.0) + , _step_dB(3.0) + , _impulse_fft(0) + , _signal_input_fft(0) + , _signal_output_fft(0) + , _plugin_insert(pluginInsert) { _signal_analysis_running = false; _samplerate = ARDOUR_UI::instance()->the_session()->frame_rate(); - _plugin = _plugin_insert->get_impulse_analysis_plugin(); - _plugin->activate(); - - set_buffer_size(4096, 16384); - //set_buffer_size(4096, 4096); - - _log_coeff = (1.0 - 2.0 * (1000.0/(_samplerate/2.0))) / powf(1000.0/(_samplerate/2.0), 2.0); + _log_coeff = (1.0 - 2.0 * (1000.0/(_samplerate/2.0))) / powf(1000.0/(_samplerate/2.0), 2.0); _log_max = log10f(1 + _log_coeff); - // Setup analysis drawing area _analysis_scale_surface = 0; _analysis_area = new Gtk::DrawingArea(); - _analysis_width = 500.0; - _analysis_height = 500.0; + _analysis_width = 256.0; + _analysis_height = 256.0; _analysis_area->set_size_request(_analysis_width, _analysis_height); _analysis_area->signal_expose_event().connect( sigc::mem_fun (*this, &PluginEqGui::expose_analysis_area)); _analysis_area->signal_size_allocate().connect( sigc::mem_fun (*this, &PluginEqGui::resize_analysis_area)); - // dB selection dBScaleModel = Gtk::ListStore::create(dBColumns); - dBScaleCombo = new Gtk::ComboBox(dBScaleModel); - dBScaleCombo -> set_title("dB scale"); + /* this grotty-looking cast allows compilation against gtkmm 2.24.0, which + added a new ComboBox constructor. + */ + dBScaleCombo = new Gtk::ComboBox ((Glib::RefPtr &) dBScaleModel); + dBScaleCombo->set_title (_("dB scale")); #define ADD_DB_ROW(MIN,MAX,STEP,NAME) \ { \ @@ -95,14 +96,14 @@ PluginEqGui::PluginEqGui(boost::shared_ptr pluginInsert) dBScaleCombo -> signal_changed().connect( sigc::mem_fun(*this, &PluginEqGui::change_dB_scale) ); - Gtk::Label *dBComboLabel = new Gtk::Label("dB scale"); + Gtk::Label *dBComboLabel = new Gtk::Label (_("dB scale")); Gtk::HBox *dBSelectBin = new Gtk::HBox(false, 5); dBSelectBin->add( *manage(dBComboLabel)); dBSelectBin->add( *manage(dBScaleCombo)); - + // Phase checkbutton - _phase_button = new Gtk::CheckButton("Show phase"); + _phase_button = new Gtk::CheckButton (_("Show phase")); _phase_button->set_active(true); _phase_button->signal_toggled().connect( sigc::mem_fun(*this, &PluginEqGui::redraw_scales)); @@ -110,27 +111,45 @@ PluginEqGui::PluginEqGui(boost::shared_ptr pluginInsert) attach( *manage(_analysis_area), 1, 3, 1, 2); attach( *manage(dBSelectBin), 1, 2, 2, 3, Gtk::SHRINK, Gtk::SHRINK); attach( *manage(_phase_button), 2, 3, 2, 3, Gtk::SHRINK, Gtk::SHRINK); - - - // Connect the realtime signal collection callback - _plugin_insert->AnalysisDataGathered.connect( sigc::mem_fun(*this, &PluginEqGui::signal_collect_callback )); } PluginEqGui::~PluginEqGui() { + stop_listening (); + if (_analysis_scale_surface) { cairo_surface_destroy (_analysis_scale_surface); } delete _impulse_fft; + _impulse_fft = 0; delete _signal_input_fft; + _signal_input_fft = 0; delete _signal_output_fft; + _signal_output_fft = 0; - _plugin->deactivate(); - // all gui objects are *manage'd by the inherited Table object } +void +PluginEqGui::start_listening () +{ + if (!_plugin) { + _plugin = _plugin_insert->get_impulse_analysis_plugin(); + } + + _plugin->activate(); + set_buffer_size(4096, 16384); + // Connect the realtime signal collection callback + _plugin_insert->AnalysisDataGathered.connect (analysis_connection, invalidator (*this), boost::bind (&PluginEqGui::signal_collect_callback, this, _1, _2), gui_context()); +} + +void +PluginEqGui::stop_listening () +{ + analysis_connection.disconnect (); + _plugin->deactivate (); +} void PluginEqGui::on_hide() @@ -163,18 +182,15 @@ PluginEqGui::on_show() start_updating(); Gtk::Widget *toplevel = get_toplevel(); - if (!toplevel) { - std::cerr << "No toplevel widget for PluginEqGui?!?!" << std::endl; - } - - if (!_window_unmap_connection.connected()) { - _window_unmap_connection = toplevel->signal_unmap().connect( sigc::mem_fun(this, &PluginEqGui::stop_updating)); - } + if (toplevel) { + if (!_window_unmap_connection.connected()) { + _window_unmap_connection = toplevel->signal_unmap().connect( sigc::mem_fun(this, &PluginEqGui::stop_updating)); + } - if (!_window_map_connection.connected()) { - _window_map_connection = toplevel->signal_map().connect( sigc::mem_fun(this, &PluginEqGui::start_updating)); + if (!_window_map_connection.connected()) { + _window_map_connection = toplevel->signal_map().connect( sigc::mem_fun(this, &PluginEqGui::start_updating)); + } } - } void @@ -188,7 +204,7 @@ PluginEqGui::change_dB_scale() _min_dB = row[dBColumns.dBMin]; _max_dB = row[dBColumns.dBMax]; _step_dB = row[dBColumns.dBStep]; - + redraw_scales(); } @@ -203,7 +219,7 @@ PluginEqGui::redraw_scales() _analysis_scale_surface = 0; } - _analysis_area->queue_draw(); + _analysis_area->queue_draw(); // TODO: Add graph legend! } @@ -211,18 +227,18 @@ PluginEqGui::redraw_scales() void PluginEqGui::set_buffer_size(uint32_t size, uint32_t signal_size) { - if (_buffer_size == size && _signal_buffer_size == signal_size) + if (_buffer_size == size && _signal_buffer_size == signal_size) { return; + } - - FFT *tmp1 = _impulse_fft; - FFT *tmp2 = _signal_input_fft; - FFT *tmp3 = _signal_output_fft; + GTKArdour::FFT *tmp1 = _impulse_fft; + GTKArdour::FFT *tmp2 = _signal_input_fft; + GTKArdour::FFT *tmp3 = _signal_output_fft; try { - _impulse_fft = new FFT(size); - _signal_input_fft = new FFT(signal_size); - _signal_output_fft = new FFT(signal_size); + _impulse_fft = new GTKArdour::FFT(size); + _signal_input_fft = new GTKArdour::FFT(signal_size); + _signal_output_fft = new GTKArdour::FFT(signal_size); } catch( ... ) { // Don't care about lost memory, we're screwed anyhow _impulse_fft = tmp1; @@ -234,25 +250,23 @@ PluginEqGui::set_buffer_size(uint32_t size, uint32_t signal_size) delete tmp1; delete tmp2; delete tmp3; - + _buffer_size = size; _signal_buffer_size = signal_size; - // These are for impulse analysis only, the signal analysis uses the actual - // number of I/O's for the plugininsert - uint32_t inputs = _plugin->get_info()->n_inputs.n_audio(); - uint32_t outputs = _plugin->get_info()->n_outputs.n_audio(); + ARDOUR::ChanCount count = ARDOUR::ChanCount::max (_plugin->get_info()->n_inputs, _plugin->get_info()->n_outputs); - // buffers for the signal analysis are ensured inside PluginInsert - uint32_t n_chans = std::max(inputs, outputs); - _bufferset.ensure_buffers(ARDOUR::DataType::AUDIO, n_chans, _buffer_size); + for (ARDOUR::DataType::iterator i = ARDOUR::DataType::begin(); i != ARDOUR::DataType::end(); ++i) { + _bufferset.ensure_buffers (*i, count.get (*i), _buffer_size); + _collect_bufferset.ensure_buffers (*i, count.get (*i), _buffer_size); + } - ARDOUR::ChanCount chanCount(ARDOUR::DataType::AUDIO, n_chans); - _bufferset.set_count(chanCount); + _bufferset.set_count (count); + _collect_bufferset.set_count (count); } -void -PluginEqGui::resize_analysis_area(Gtk::Allocation& size) +void +PluginEqGui::resize_analysis_area (Gtk::Allocation& size) { _analysis_width = (float)size.get_width(); _analysis_height = (float)size.get_height(); @@ -278,17 +292,17 @@ PluginEqGui::timeout_callback() void PluginEqGui::signal_collect_callback(ARDOUR::BufferSet *in, ARDOUR::BufferSet *out) { - ENSURE_GUI_THREAD(bind (mem_fun (*this, &PluginEqGui::signal_collect_callback), in, out)); + ENSURE_GUI_THREAD (*this, &PluginEqGui::signal_collect_callback, in, out) _signal_input_fft ->reset(); _signal_output_fft->reset(); for (uint32_t i = 0; i < _plugin_insert->input_streams().n_audio(); ++i) { - _signal_input_fft ->analyze(in ->get_audio(i).data(_signal_buffer_size, 0), FFT::HANN); + _signal_input_fft ->analyze(in ->get_audio(i).data(), GTKArdour::FFT::HANN); } - + for (uint32_t i = 0; i < _plugin_insert->output_streams().n_audio(); ++i) { - _signal_output_fft->analyze(out->get_audio(i).data(_signal_buffer_size, 0), FFT::HANN); + _signal_output_fft->analyze(out->get_audio(i).data(), GTKArdour::FFT::HANN); } _signal_input_fft ->calculate(); @@ -297,55 +311,121 @@ PluginEqGui::signal_collect_callback(ARDOUR::BufferSet *in, ARDOUR::BufferSet *o _signal_analysis_running = false; // This signals calls expose_analysis_area() - _analysis_area->queue_draw(); + _analysis_area->queue_draw(); } void PluginEqGui::run_impulse_analysis() { + /* Allocate some thread-local buffers so that Plugin::connect_and_run can use them */ + ARDOUR_UI::instance()->get_process_buffers (); + uint32_t inputs = _plugin->get_info()->n_inputs.n_audio(); uint32_t outputs = _plugin->get_info()->n_outputs.n_audio(); // Create the impulse, can't use silence() because consecutive calls won't work for (uint32_t i = 0; i < inputs; ++i) { - ARDOUR::AudioBuffer &buf = _bufferset.get_audio(i); - ARDOUR::Sample *d = buf.data(_buffer_size, 0); + ARDOUR::AudioBuffer& buf = _bufferset.get_audio(i); + ARDOUR::Sample* d = buf.data(); memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size); *d = 1.0; } - uint32_t x,y; - x=y=0; - _plugin->connect_and_run(_bufferset, x, y, _buffer_size, (nframes_t)0); + ARDOUR::ChanMapping in_map(_plugin->get_info()->n_inputs); + ARDOUR::ChanMapping out_map(_plugin->get_info()->n_outputs); - // Analyze all output buffers + _plugin->connect_and_run(_bufferset, in_map, out_map, _buffer_size, 0); + framecnt_t f = _plugin->signal_latency (); + // Adding user_latency() could be interesting + + // Gather all output, taking latency into account. _impulse_fft->reset(); + + // Silence collect buffers to copy data to, can't use silence() because consecutive calls won't work for (uint32_t i = 0; i < outputs; ++i) { - _impulse_fft->analyze(_bufferset.get_audio(i).data(_buffer_size, 0)); + ARDOUR::AudioBuffer &buf = _collect_bufferset.get_audio(i); + ARDOUR::Sample *d = buf.data(); + memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size); + } + + if (f == 0) { + //std::cerr << "0: no latency, copying full buffer, trivial.." << std::endl; + for (uint32_t i = 0; i < outputs; ++i) { + memcpy(_collect_bufferset.get_audio(i).data(), + _bufferset.get_audio(i).data(), _buffer_size * sizeof(float)); + } + } else { + //int C = 0; + //std::cerr << (++C) << ": latency is " << f << " frames, doing split processing.." << std::endl; + framecnt_t target_offset = 0; + framecnt_t frames_left = _buffer_size; // refaktoroi + do { + if (f >= _buffer_size) { + //std::cerr << (++C) << ": f (=" << f << ") is larger than buffer_size, still trying to reach the actual output" << std::endl; + // there is no data in this buffer regarding to the input! + f -= _buffer_size; + } else { + // this buffer contains either the first, last or a whole bu the output of the impulse + // first part: offset is 0, so we copy to the start of _collect_bufferset + // we start at output offset "f" + // .. and copy "buffer size" - "f" - "offset" frames + + framecnt_t length = _buffer_size - f - target_offset; + + //std::cerr << (++C) << ": copying " << length << " frames to _collect_bufferset.get_audio(i)+" << target_offset << " from bufferset at offset " << f << std::endl; + for (uint32_t i = 0; i < outputs; ++i) { + memcpy(_collect_bufferset.get_audio(i).data(target_offset), + _bufferset.get_audio(i).data() + f, + length * sizeof(float)); + } + + target_offset += length; + frames_left -= length; + f = 0; + } + if (frames_left > 0) { + // Silence the buffers + for (uint32_t i = 0; i < inputs; ++i) { + ARDOUR::AudioBuffer &buf = _bufferset.get_audio(i); + ARDOUR::Sample *d = buf.data(); + memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size); + } + + in_map = ARDOUR::ChanMapping(_plugin->get_info()->n_inputs); + out_map = ARDOUR::ChanMapping(_plugin->get_info()->n_outputs); + _plugin->connect_and_run(_bufferset, in_map, out_map, _buffer_size, 0); + } + } while ( frames_left > 0); + + } + + + for (uint32_t i = 0; i < outputs; ++i) { + _impulse_fft->analyze(_collect_bufferset.get_audio(i).data()); } // normalize the output _impulse_fft->calculate(); // This signals calls expose_analysis_area() - _analysis_area->queue_draw(); + _analysis_area->queue_draw(); + ARDOUR_UI::instance()->drop_process_buffers (); } bool -PluginEqGui::expose_analysis_area(GdkEventExpose *evt) +PluginEqGui::expose_analysis_area(GdkEventExpose *) { redraw_analysis_area(); - - return false; + return true; } void PluginEqGui::draw_analysis_scales(cairo_t *ref_cr) { // TODO: check whether we need rounding - _analysis_scale_surface = cairo_surface_create_similar(cairo_get_target(ref_cr), - CAIRO_CONTENT_COLOR, + _analysis_scale_surface = cairo_surface_create_similar(cairo_get_target(ref_cr), + CAIRO_CONTENT_COLOR, _analysis_width, _analysis_height); @@ -360,9 +440,9 @@ PluginEqGui::draw_analysis_scales(cairo_t *ref_cr) if (_phase_button->get_active()) { draw_scales_phase(_analysis_area, cr); } - + cairo_destroy(cr); - + } void @@ -375,7 +455,7 @@ PluginEqGui::redraw_analysis_area() if (_analysis_scale_surface == 0) { draw_analysis_scales(cr); } - + cairo_copy_page(cr); @@ -397,8 +477,8 @@ PluginEqGui::redraw_analysis_area() #define PHASE_PROPORTION 0.5 -void -PluginEqGui::draw_scales_phase(Gtk::Widget *w, cairo_t *cr) +void +PluginEqGui::draw_scales_phase(Gtk::Widget */*w*/, cairo_t *cr) { float y; cairo_font_extents_t extents; @@ -420,16 +500,16 @@ PluginEqGui::draw_scales_phase(Gtk::Widget *w, cairo_t *cr) cairo_text_extents(cr, buf, &t_ext); cairo_move_to(cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent); cairo_show_text(cr, buf); - + if (i == 0) continue; - + cairo_set_source_rgba(cr, .8, .9, 0.2, 0.6/(float)i); cairo_move_to(cr, 0.0, y); cairo_line_to(cr, _analysis_width, y); - + y = _analysis_height/2.0 + (float)i*(_analysis_height/8.0)*PHASE_PROPORTION; // label @@ -449,7 +529,7 @@ PluginEqGui::draw_scales_phase(Gtk::Widget *w, cairo_t *cr) } } -void +void PluginEqGui::plot_impulse_phase(Gtk::Widget *w, cairo_t *cr) { float x,y; @@ -468,7 +548,7 @@ PluginEqGui::plot_impulse_phase(Gtk::Widget *w, cairo_t *cr) x *= _analysis_width; y = _analysis_height/2.0 - (_impulse_fft->phase_at_bin(i)/M_PI)*(_analysis_height/2.0)*PHASE_PROPORTION; - + if ( i == 0 ) { cairo_move_to(cr, x, y); @@ -483,8 +563,8 @@ PluginEqGui::plot_impulse_phase(Gtk::Widget *w, cairo_t *cr) avgY = 0; avgNum = 0; - - } + + } prevX = rint(x); avgY += y; @@ -496,10 +576,13 @@ PluginEqGui::plot_impulse_phase(Gtk::Widget *w, cairo_t *cr) } void -PluginEqGui::draw_scales_power(Gtk::Widget *w, cairo_t *cr) +PluginEqGui::draw_scales_power(Gtk::Widget */*w*/, cairo_t *cr) { + if (_impulse_fft == 0) { + return; + } + 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 }; - float divisor = _samplerate / 2.0 / _impulse_fft->bins(); float x; @@ -569,8 +652,8 @@ PluginEqGui::draw_scales_power(Gtk::Widget *w, cairo_t *cr) } } - - + + for (float dB = - _step_dB; dB > _min_dB; dB -= _step_dB ) { snprintf(buf, 256, "%0.0f", dB ); @@ -598,11 +681,10 @@ power_to_dB(float a) return 10.0 * log10f(a); } -void +void PluginEqGui::plot_impulse_amplitude(Gtk::Widget *w, cairo_t *cr) { float x,y; - int prevX = 0; float avgY = 0.0; int avgNum = 0; @@ -636,7 +718,7 @@ PluginEqGui::plot_impulse_amplitude(Gtk::Widget *w, cairo_t *cr) avgY = 0; avgNum = 0; - + } prevX = rint(x); @@ -670,7 +752,7 @@ PluginEqGui::plot_signal_amplitude_difference(Gtk::Widget *w, cairo_t *cr) float power_out = power_to_dB(_signal_output_fft->power_at_bin(i)); float power_in = power_to_dB(_signal_input_fft ->power_at_bin(i)); float power = power_out - power_in; - + // for SaBer /* double p = 10.0 * log10( 1.0 + (double)_signal_output_fft->power_at_bin(i) - (double) @@ -710,7 +792,7 @@ PluginEqGui::plot_signal_amplitude_difference(Gtk::Widget *w, cairo_t *cr) avgY = 0; avgNum = 0; - + } prevX = rint(x); @@ -720,5 +802,5 @@ PluginEqGui::plot_signal_amplitude_difference(Gtk::Widget *w, cairo_t *cr) cairo_stroke(cr); - + }