From a75868c767dc5a74f4b7361a6255972bad7f7c61 Mon Sep 17 00:00:00 2001 From: Sampo Savolainen Date: Mon, 20 Oct 2008 18:57:34 +0000 Subject: Added facilities into PluginInsert for the GUI to gather parts of the real signal passed through the insert. Also added rudimentary plugin input/output difference analysis in the plugin eq gui for the collected signal. git-svn-id: svn://localhost/ardour2/branches/3.0@3987 d708f5d6-7413-0410-9779-e7cbd77b26cf --- gtk2_ardour/fft.cc | 40 +++++++- gtk2_ardour/fft.h | 12 ++- gtk2_ardour/plugin_eq_gui.cc | 195 +++++++++++++++++++++++++++++++++---- gtk2_ardour/plugin_eq_gui.h | 16 ++- libs/ardour/ardour/plugin_insert.h | 16 +++ libs/ardour/plugin_insert.cc | 65 ++++++++++++- 6 files changed, 314 insertions(+), 30 deletions(-) diff --git a/gtk2_ardour/fft.cc b/gtk2_ardour/fft.cc index eda1a96e3f..f78a1e94ad 100644 --- a/gtk2_ardour/fft.cc +++ b/gtk2_ardour/fft.cc @@ -26,7 +26,8 @@ FFT::FFT(uint32_t windowSize) : _window_size(windowSize), _data_size(_window_size/2), - _iterations(0) + _iterations(0), + _hann_window(0) { _fftInput = (float *) fftwf_malloc(sizeof(float) * _window_size); @@ -50,12 +51,19 @@ FFT::reset() } void -FFT::analyze(ARDOUR::Sample *input) +FFT::analyze(ARDOUR::Sample *input, WindowingType windowing_type) { _iterations++; memcpy(_fftInput, input, sizeof(float) * _window_size); + if (windowing_type == HANN) { + float *window = get_hann_window(); + for (uint32_t i = 0; i < _window_size; i++) { + _fftInput[i] *= window[i]; + } + } + fftwf_execute(_plan); _power_at_bin[0] += _fftOutput[0] * _fftOutput[0]; @@ -96,9 +104,37 @@ FFT::calculate() } } +float * +FFT::get_hann_window() +{ + if (_hann_window) + return _hann_window; + + + _hann_window = (float *) malloc(sizeof(float) * _window_size); + + double sum = 0.0; + + for (uint32_t i=0; i < _window_size; i++) { + _hann_window[i]=0.81f * ( 0.5f - (0.5f * (float) cos(2.0f * M_PI * (float)i / (float)(_window_size)))); + sum += _hann_window[i]; + } + + double isum = 1.0 / sum; + + for (uint32_t i=0; i < _window_size; i++) { + _hann_window[i] *= isum; + } + + return _hann_window; +} + FFT::~FFT() { + if (_hann_window) { + free(_hann_window); + } fftwf_destroy_plan(_plan); free(_power_at_bin); free(_phase_at_bin); diff --git a/gtk2_ardour/fft.h b/gtk2_ardour/fft.h index d80616b77f..3a4d0f8063 100644 --- a/gtk2_ardour/fft.h +++ b/gtk2_ardour/fft.h @@ -38,8 +38,13 @@ class FFT FFT(uint32_t); ~FFT(); + enum WindowingType { + NONE, + HANN + }; + void reset(); - void analyze(ARDOUR::Sample *); + void analyze(ARDOUR::Sample *, WindowingType w = NONE); void calculate(); uint32_t bins() const { return _data_size; } @@ -47,12 +52,17 @@ class FFT float power_at_bin(uint32_t i) const { return _power_at_bin[i]; } float phase_at_bin(uint32_t i) const { return _phase_at_bin[i]; } + private: + float *get_hann_window(); + uint32_t const _window_size; uint32_t const _data_size; uint32_t _iterations; + float *_hann_window; + float *_fftInput; float *_fftOutput; diff --git a/gtk2_ardour/plugin_eq_gui.cc b/gtk2_ardour/plugin_eq_gui.cc index 6b176fbde8..bacfbe98b2 100644 --- a/gtk2_ardour/plugin_eq_gui.cc +++ b/gtk2_ardour/plugin_eq_gui.cc @@ -22,6 +22,7 @@ #include "fft.h" #include "ardour_ui.h" +#include "gui_thread.h" #include #include @@ -36,14 +37,19 @@ PluginEqGui::PluginEqGui(boost::shared_ptr pluginInsert) : _min_dB(-12.0), _max_dB(+12.0), _step_dB(3.0), - _impulse_fft(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 = pluginInsert->get_impulse_analysis_plugin(); + _plugin = _plugin_insert->get_impulse_analysis_plugin(); _plugin->activate(); - set_buffer_size(4096); + 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_max = log10f(1 + _log_coeff); @@ -80,6 +86,7 @@ PluginEqGui::PluginEqGui(boost::shared_ptr pluginInsert) ADD_DB_ROW(-12, +12, 3, "-12dB .. +12dB"); ADD_DB_ROW(-24, +24, 5, "-24dB .. +24dB"); ADD_DB_ROW(-36, +36, 6, "-36dB .. +36dB"); + ADD_DB_ROW(-64, +64,12, "-64dB .. +64dB"); #undef ADD_DB_ROW @@ -103,6 +110,10 @@ 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() @@ -112,6 +123,9 @@ PluginEqGui::~PluginEqGui() } delete _impulse_fft; + delete _signal_input_fft; + delete _signal_output_fft; + _plugin->deactivate(); // all gui objects are *manage'd by the inherited Table object @@ -190,26 +204,46 @@ PluginEqGui::redraw_scales() } _analysis_area->queue_draw(); + + // TODO: Add graph legend! } void -PluginEqGui::set_buffer_size(uint32_t size) +PluginEqGui::set_buffer_size(uint32_t size, uint32_t signal_size) { - if (_buffer_size == size) + if (_buffer_size == size && _signal_buffer_size == signal_size) return; - _buffer_size = size; - if (_impulse_fft) { - delete _impulse_fft; - _impulse_fft = 0; + FFT *tmp1 = _impulse_fft; + FFT *tmp2 = _signal_input_fft; + 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); + } catch( ... ) { + // Don't care about lost memory, we're screwed anyhow + _impulse_fft = tmp1; + _signal_input_fft = tmp2; + _signal_output_fft = tmp3; + throw; } - _impulse_fft = new FFT(_buffer_size); + if (tmp1) delete tmp1; + if (tmp2) delete tmp1; + if (tmp3) delete tmp1; + + _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(); + // 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); @@ -232,13 +266,42 @@ PluginEqGui::resize_analysis_area(Gtk::Allocation& size) bool PluginEqGui::timeout_callback() { - run_analysis(); + if (!_signal_analysis_running) { + _signal_analysis_running = true; + _plugin_insert -> collect_signal_for_analysis(_signal_buffer_size); + } + run_impulse_analysis(); return true; } void -PluginEqGui::run_analysis() +PluginEqGui::signal_collect_callback(ARDOUR::BufferSet *in, ARDOUR::BufferSet *out) +{ + ENSURE_GUI_THREAD(bind (mem_fun (*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); + } + + 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_input_fft ->calculate(); + _signal_output_fft->calculate(); + + _signal_analysis_running = false; + + // This signals calls expose_analysis_area() + _analysis_area->queue_draw(); +} + +void +PluginEqGui::run_impulse_analysis() { uint32_t inputs = _plugin->get_info()->n_inputs.n_audio(); uint32_t outputs = _plugin->get_info()->n_outputs.n_audio(); @@ -320,17 +383,19 @@ PluginEqGui::redraw_analysis_area() cairo_paint(cr); if (_phase_button->get_active()) { - plot_phase(_analysis_area, cr); + plot_impulse_phase(_analysis_area, cr); } - plot_amplitude(_analysis_area, cr); + plot_impulse_amplitude(_analysis_area, cr); + // TODO: make this optional + plot_signal_amplitude_difference(_analysis_area, cr); cairo_destroy(cr); } -#define PHASE_PROPORTION 0.6 +#define PHASE_PROPORTION 0.5 void PluginEqGui::draw_scales_phase(Gtk::Widget *w, cairo_t *cr) @@ -385,7 +450,7 @@ PluginEqGui::draw_scales_phase(Gtk::Widget *w, cairo_t *cr) } void -PluginEqGui::plot_phase(Gtk::Widget *w, cairo_t *cr) +PluginEqGui::plot_impulse_phase(Gtk::Widget *w, cairo_t *cr) { float x,y; @@ -393,6 +458,9 @@ PluginEqGui::plot_phase(Gtk::Widget *w, cairo_t *cr) float avgY = 0.0; int avgNum = 0; + float width = w->get_width(); + float height = w->get_height(); + cairo_set_source_rgba(cr, 0.95, 0.3, 0.2, 1.0); for (uint32_t i = 0; i < _impulse_fft->bins()-1; i++) { // x coordinate of bin i @@ -407,7 +475,11 @@ PluginEqGui::plot_phase(Gtk::Widget *w, cairo_t *cr) avgY = 0; avgNum = 0; } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) { - cairo_line_to(cr, prevX, avgY/(float)avgNum); + avgY = avgY/(float)avgNum; + if (avgY > (height * 10.0) ) avgY = height * 10.0; + if (avgY < (-height * 10.0) ) avgY = -height * 10.0; + cairo_line_to(cr, prevX, avgY); + //cairo_line_to(cr, prevX, avgY/(float)avgNum); avgY = 0; avgNum = 0; @@ -454,7 +526,8 @@ PluginEqGui::draw_scales_power(Gtk::Widget *w, cairo_t *cr) cairo_set_source_rgb(cr, 0.4, 0.4, 0.4); - cairo_move_to(cr, x + fontXOffset, 3.0); + //cairo_move_to(cr, x + fontXOffset, 3.0); + cairo_move_to(cr, x - extents.height, 3.0); cairo_rotate(cr, M_PI / 2.0); cairo_show_text(cr, buf); @@ -526,7 +599,7 @@ power_to_dB(float a) } void -PluginEqGui::plot_amplitude(Gtk::Widget *w, cairo_t *cr) +PluginEqGui::plot_impulse_amplitude(Gtk::Widget *w, cairo_t *cr) { float x,y; @@ -534,6 +607,9 @@ PluginEqGui::plot_amplitude(Gtk::Widget *w, cairo_t *cr) float avgY = 0.0; int avgNum = 0; + float width = w->get_width(); + float height = w->get_height(); + cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); cairo_set_line_width (cr, 2.5); @@ -552,7 +628,11 @@ PluginEqGui::plot_amplitude(Gtk::Widget *w, cairo_t *cr) avgY = 0; avgNum = 0; } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) { - cairo_line_to(cr, prevX, avgY/(float)avgNum); + avgY = avgY/(float)avgNum; + if (avgY > (height * 10.0) ) avgY = height * 10.0; + if (avgY < (-height * 10.0) ) avgY = -height * 10.0; + cairo_line_to(cr, prevX, avgY); + //cairo_line_to(cr, prevX, avgY/(float)avgNum); avgY = 0; avgNum = 0; @@ -567,3 +647,78 @@ PluginEqGui::plot_amplitude(Gtk::Widget *w, cairo_t *cr) cairo_stroke(cr); } +void +PluginEqGui::plot_signal_amplitude_difference(Gtk::Widget *w, cairo_t *cr) +{ + float x,y; + + int prevX = 0; + float avgY = 0.0; + int avgNum = 0; + + float width = w->get_width(); + float height = w->get_height(); + + cairo_set_source_rgb(cr, 0.0, 1.0, 0.0); + cairo_set_line_width (cr, 2.5); + + for (uint32_t i = 0; i < _signal_input_fft->bins()-1; i++) { + // x coordinate of bin i + x = log10f(1.0 + (float)i / (float)_signal_input_fft->bins() * _log_coeff) / _log_max; + x *= _analysis_width; + + 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) + - _signal_input_fft ->power_at_bin(i)); + //p *= 1000000.0; + float power = (float)p; + + if ( (i % 1000) == 0) { + std::cerr << i << ": " << power << std::endl; + } + */ + + if (isinf(power)) { + if (power < 0) { + power = _min_dB - 1.0; + } else { + power = _max_dB - 1.0; + } + } else if (isnan(power)) { + power = _min_dB - 1.0; + } + + float yCoeff = ( power - _min_dB) / (_max_dB - _min_dB); + + y = _analysis_height - _analysis_height*yCoeff; + + if ( i == 0 ) { + cairo_move_to(cr, x, y); + + avgY = 0; + avgNum = 0; + } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) { + avgY = avgY/(float)avgNum; + if (avgY > (height * 10.0) ) avgY = height * 10.0; + if (avgY < (-height * 10.0) ) avgY = -height * 10.0; + cairo_line_to(cr, prevX, avgY); + + avgY = 0; + avgNum = 0; + + } + + prevX = rint(x); + avgY += y; + avgNum++; + } + + cairo_stroke(cr); + + +} diff --git a/gtk2_ardour/plugin_eq_gui.h b/gtk2_ardour/plugin_eq_gui.h index e8fca786f0..cd52ed7682 100644 --- a/gtk2_ardour/plugin_eq_gui.h +++ b/gtk2_ardour/plugin_eq_gui.h @@ -42,11 +42,13 @@ class PluginEqGui : public Gtk::Table private: // Setup - void set_buffer_size(uint32_t); + void set_buffer_size(uint32_t, uint32_t); void change_dB_scale(); // Analysis - void run_analysis(); + void run_impulse_analysis(); + void signal_collect_callback(ARDOUR::BufferSet *, ARDOUR::BufferSet *); + float _signal_analysis_running; // Drawing virtual void on_hide(); @@ -62,10 +64,12 @@ class PluginEqGui : public Gtk::Table bool expose_analysis_area(GdkEventExpose *); void draw_scales_power(Gtk::Widget *, cairo_t *); - void plot_amplitude(Gtk::Widget *,cairo_t *); + void plot_impulse_amplitude(Gtk::Widget *,cairo_t *); void draw_scales_phase(Gtk::Widget *,cairo_t *); - void plot_phase(Gtk::Widget *,cairo_t *); + void plot_impulse_phase(Gtk::Widget *,cairo_t *); + + void plot_signal_amplitude_difference(Gtk::Widget *,cairo_t *); // Helpers bool timeout_callback(); @@ -86,6 +90,7 @@ class PluginEqGui : public Gtk::Table float _log_max; nframes_t _buffer_size; + nframes_t _signal_buffer_size; // buffers ARDOUR::BufferSet _bufferset; @@ -97,7 +102,10 @@ class PluginEqGui : public Gtk::Table // My objects FFT *_impulse_fft; + FFT *_signal_input_fft; + FFT *_signal_output_fft; boost::shared_ptr _plugin; + boost::shared_ptr _plugin_insert; // gui objects Gtk::DrawingArea *_analysis_area; diff --git a/libs/ardour/ardour/plugin_insert.h b/libs/ardour/ardour/plugin_insert.h index 3d4a4b727a..5cf4b4cbe0 100644 --- a/libs/ardour/ardour/plugin_insert.h +++ b/libs/ardour/ardour/plugin_insert.h @@ -106,6 +106,16 @@ class PluginInsert : public Processor boost::shared_ptr get_impulse_analysis_plugin(); + sigc::signal AnalysisDataGathered; + void collect_signal_for_analysis(nframes_t nframes) { + // called from outside the audio thread, so this should be safe + _signal_analysis_input_bufferset.ensure_buffers(input_streams(), nframes); + _signal_analysis_output_bufferset.ensure_buffers(output_streams(), nframes); + + _signal_analysis_collect_nframes_max = nframes; + _signal_analysis_collected_nframes = 0; + } + private: void parameter_changed (Evoral::Parameter, float); @@ -118,6 +128,12 @@ class PluginInsert : public Processor std::vector > _plugins; boost::weak_ptr _impulseAnalysisPlugin; + + nframes_t _signal_analysis_collected_nframes; + nframes_t _signal_analysis_collect_nframes_max; + + BufferSet _signal_analysis_input_bufferset; + BufferSet _signal_analysis_output_bufferset; void automation_run (BufferSet& bufs, nframes_t nframes, nframes_t offset); void connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now = 0); diff --git a/libs/ardour/plugin_insert.cc b/libs/ardour/plugin_insert.cc index 2c08473b54..7ed35dd1a7 100644 --- a/libs/ardour/plugin_insert.cc +++ b/libs/ardour/plugin_insert.cc @@ -57,7 +57,9 @@ using namespace PBD; const string PluginInsert::port_automation_node_name = "PortAutomation"; PluginInsert::PluginInsert (Session& s, boost::shared_ptr plug, Placement placement) - : Processor (s, plug->name(), placement) + : Processor (s, plug->name(), placement), + _signal_analysis_collected_nframes(0), + _signal_analysis_collect_nframes_max(0) { /* the first is the master */ @@ -74,7 +76,9 @@ PluginInsert::PluginInsert (Session& s, boost::shared_ptr plug, Placemen } PluginInsert::PluginInsert (Session& s, const XMLNode& node) - : Processor (s, "unnamed plugin insert", PreFader) + : Processor (s, "unnamed plugin insert", PreFader), + _signal_analysis_collected_nframes(0), + _signal_analysis_collect_nframes_max(0) { if (set_state (node)) { throw failed_constructor(); @@ -92,7 +96,9 @@ PluginInsert::PluginInsert (Session& s, const XMLNode& node) } PluginInsert::PluginInsert (const PluginInsert& other) - : Processor (other._session, other._name, other.placement()) + : Processor (other._session, other._name, other.placement()), + _signal_analysis_collected_nframes(0), + _signal_analysis_collect_nframes_max(0) { uint32_t count = other._plugins.size(); @@ -282,6 +288,13 @@ PluginInsert::deactivate () void PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now) { + // Calculate if, and how many frames we need to collect for analysis + nframes_t collect_signal_nframes = (_signal_analysis_collect_nframes_max - + _signal_analysis_collected_nframes); + if (nframes < collect_signal_nframes) { // we might not get all frames now + collect_signal_nframes = nframes; + } + uint32_t in_index = 0; uint32_t out_index = 0; @@ -311,10 +324,45 @@ PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t off } } + if (collect_signal_nframes > 0) { + // collect input + //std::cerr << "collect input, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl; + //std::cerr << " streams " << input_streams().n_audio() << std::endl; + //std::cerr << "filling buffer with " << collect_signal_nframes << " frames at " << _signal_analysis_collected_nframes << std::endl; + for (uint32_t i = 0; i < input_streams().n_audio(); ++i) { + _signal_analysis_input_bufferset.get_audio(i).read_from( + bufs.get_audio(i), + collect_signal_nframes, + _signal_analysis_collected_nframes); // offset is for target buffer + } + } + for (vector >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) { (*i)->connect_and_run (bufs, in_index, out_index, nframes, offset); } + if (collect_signal_nframes > 0) { + // collect output + //std::cerr << " output, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl; + //std::cerr << " streams " << output_streams().n_audio() << std::endl; + for (uint32_t i = 0; i < output_streams().n_audio(); ++i) { + _signal_analysis_output_bufferset.get_audio(i).read_from( + bufs.get_audio(i), + collect_signal_nframes, + _signal_analysis_collected_nframes); // offset is for target buffer + } + + _signal_analysis_collected_nframes += collect_signal_nframes; + assert(_signal_analysis_collected_nframes <= _signal_analysis_collect_nframes_max); + + if (_signal_analysis_collected_nframes == _signal_analysis_collect_nframes_max) { + _signal_analysis_collect_nframes_max = 0; + _signal_analysis_collected_nframes = 0; + + AnalysisDataGathered(&_signal_analysis_input_bufferset, + &_signal_analysis_output_bufferset); + } + } /* leave remaining channel buffers alone */ } @@ -508,6 +556,17 @@ PluginInsert::configure_io (ChanCount in, ChanCount out) return false; } + // we don't know the analysis window size, so we must work with the + // current buffer size here. each request for data fills in these + // buffers and the analyser makes sure it gets enough data for the + // analysis window + _signal_analysis_input_bufferset.ensure_buffers (in, session().engine().frames_per_cycle()); + _signal_analysis_input_bufferset.set_count(in); + + _signal_analysis_output_bufferset.ensure_buffers(out, session().engine().frames_per_cycle()); + _signal_analysis_output_bufferset.set_count(out); + + return Processor::configure_io (in, out); } -- cgit v1.2.3