Remove MIDI track default channel and its menu, and choose the channel for new notes...
[ardour2.git] / gtk2_ardour / plugin_eq_gui.cc
blob013edd61971f7d1cc0e6547b43eee2be6f3247b9
1 /*
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"
22 #include "fft.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>
35 #include <iostream>
36 #include <cmath>
38 #include "i18n.h"
40 using namespace ARDOUR;
42 PluginEqGui::PluginEqGui(boost::shared_ptr<ARDOUR::PluginInsert> pluginInsert)
43 : _min_dB(-12.0)
44 , _max_dB(+12.0)
45 , _step_dB(3.0)
46 , _impulse_fft(0)
47 , _signal_input_fft(0)
48 , _signal_output_fft(0)
49 , _plugin_insert(pluginInsert)
51 _signal_analysis_running = false;
52 _samplerate = ARDOUR_UI::instance()->the_session()->frame_rate();
54 _log_coeff = (1.0 - 2.0 * (1000.0/(_samplerate/2.0))) / powf(1000.0/(_samplerate/2.0), 2.0);
55 _log_max = log10f(1 + _log_coeff);
57 // Setup analysis drawing area
58 _analysis_scale_surface = 0;
60 _analysis_area = new Gtk::DrawingArea();
61 _analysis_width = 256.0;
62 _analysis_height = 256.0;
63 _analysis_area->set_size_request(_analysis_width, _analysis_height);
65 _analysis_area->signal_expose_event().connect( sigc::mem_fun (*this, &PluginEqGui::expose_analysis_area));
66 _analysis_area->signal_size_allocate().connect( sigc::mem_fun (*this, &PluginEqGui::resize_analysis_area));
68 // dB selection
69 dBScaleModel = Gtk::ListStore::create(dBColumns);
71 /* this grotty-looking cast allows compilation against gtkmm 2.24.0, which
72 added a new ComboBox constructor.
74 dBScaleCombo = new Gtk::ComboBox ((Glib::RefPtr<Gtk::TreeModel> &) dBScaleModel);
75 dBScaleCombo->set_title (_("dB scale"));
77 #define ADD_DB_ROW(MIN,MAX,STEP,NAME) \
78 { \
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");
92 #undef ADD_DB_ROW
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));
105 // Phase checkbutton
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));
110 // populate table
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 PluginEqGui::~PluginEqGui()
118 stop_listening ();
120 if (_analysis_scale_surface) {
121 cairo_surface_destroy (_analysis_scale_surface);
124 delete _impulse_fft;
125 _impulse_fft = 0;
126 delete _signal_input_fft;
127 _signal_input_fft = 0;
128 delete _signal_output_fft;
129 _signal_output_fft = 0;
131 // all gui objects are *manage'd by the inherited Table object
134 void
135 PluginEqGui::start_listening ()
137 if (!_plugin) {
138 _plugin = _plugin_insert->get_impulse_analysis_plugin();
141 _plugin->activate();
142 set_buffer_size(4096, 16384);
143 // Connect the realtime signal collection callback
144 _plugin_insert->AnalysisDataGathered.connect (analysis_connection, invalidator (*this), ui_bind (&PluginEqGui::signal_collect_callback, this, _1, _2), gui_context());
147 void
148 PluginEqGui::stop_listening ()
150 analysis_connection.disconnect ();
151 _plugin->deactivate ();
154 void
155 PluginEqGui::on_hide()
157 stop_updating();
158 Gtk::Table::on_hide();
161 void
162 PluginEqGui::stop_updating()
164 if (_update_connection.connected()) {
165 _update_connection.disconnect();
169 void
170 PluginEqGui::start_updating()
172 if (!_update_connection.connected() && is_visible()) {
173 _update_connection = Glib::signal_timeout().connect( sigc::mem_fun(this, &PluginEqGui::timeout_callback), 250);
177 void
178 PluginEqGui::on_show()
180 Gtk::Table::on_show();
182 start_updating();
184 Gtk::Widget *toplevel = get_toplevel();
185 if (toplevel) {
186 if (!_window_unmap_connection.connected()) {
187 _window_unmap_connection = toplevel->signal_unmap().connect( sigc::mem_fun(this, &PluginEqGui::stop_updating));
190 if (!_window_map_connection.connected()) {
191 _window_map_connection = toplevel->signal_map().connect( sigc::mem_fun(this, &PluginEqGui::start_updating));
196 void
197 PluginEqGui::change_dB_scale()
199 Gtk::TreeModel::iterator iter = dBScaleCombo -> get_active();
201 Gtk::TreeModel::Row row;
203 if(iter && (row = *iter)) {
204 _min_dB = row[dBColumns.dBMin];
205 _max_dB = row[dBColumns.dBMax];
206 _step_dB = row[dBColumns.dBStep];
209 redraw_scales();
213 void
214 PluginEqGui::redraw_scales()
217 if (_analysis_scale_surface) {
218 cairo_surface_destroy (_analysis_scale_surface);
219 _analysis_scale_surface = 0;
222 _analysis_area->queue_draw();
224 // TODO: Add graph legend!
227 void
228 PluginEqGui::set_buffer_size(uint32_t size, uint32_t signal_size)
230 if (_buffer_size == size && _signal_buffer_size == signal_size) {
231 return;
234 GTKArdour::FFT *tmp1 = _impulse_fft;
235 GTKArdour::FFT *tmp2 = _signal_input_fft;
236 GTKArdour::FFT *tmp3 = _signal_output_fft;
238 try {
239 _impulse_fft = new GTKArdour::FFT(size);
240 _signal_input_fft = new GTKArdour::FFT(signal_size);
241 _signal_output_fft = new GTKArdour::FFT(signal_size);
242 } catch( ... ) {
243 // Don't care about lost memory, we're screwed anyhow
244 _impulse_fft = tmp1;
245 _signal_input_fft = tmp2;
246 _signal_output_fft = tmp3;
247 throw;
250 delete tmp1;
251 delete tmp2;
252 delete tmp3;
254 _buffer_size = size;
255 _signal_buffer_size = signal_size;
257 ARDOUR::ChanCount count = ARDOUR::ChanCount::max (_plugin->get_info()->n_inputs, _plugin->get_info()->n_outputs);
259 for (ARDOUR::DataType::iterator i = ARDOUR::DataType::begin(); i != ARDOUR::DataType::end(); ++i) {
260 _bufferset.ensure_buffers (*i, count.get (*i), _buffer_size);
261 _collect_bufferset.ensure_buffers (*i, count.get (*i), _buffer_size);
264 _bufferset.set_count (count);
265 _collect_bufferset.set_count (count);
268 void
269 PluginEqGui::resize_analysis_area (Gtk::Allocation& size)
271 _analysis_width = (float)size.get_width();
272 _analysis_height = (float)size.get_height();
274 if (_analysis_scale_surface) {
275 cairo_surface_destroy (_analysis_scale_surface);
276 _analysis_scale_surface = 0;
280 bool
281 PluginEqGui::timeout_callback()
283 if (!_signal_analysis_running) {
284 _signal_analysis_running = true;
285 _plugin_insert -> collect_signal_for_analysis(_signal_buffer_size);
287 run_impulse_analysis();
289 return true;
292 void
293 PluginEqGui::signal_collect_callback(ARDOUR::BufferSet *in, ARDOUR::BufferSet *out)
295 ENSURE_GUI_THREAD (*this, &PluginEqGui::signal_collect_callback, in, out)
297 _signal_input_fft ->reset();
298 _signal_output_fft->reset();
300 for (uint32_t i = 0; i < _plugin_insert->input_streams().n_audio(); ++i) {
301 _signal_input_fft ->analyze(in ->get_audio(i).data(), GTKArdour::FFT::HANN);
304 for (uint32_t i = 0; i < _plugin_insert->output_streams().n_audio(); ++i) {
305 _signal_output_fft->analyze(out->get_audio(i).data(), GTKArdour::FFT::HANN);
308 _signal_input_fft ->calculate();
309 _signal_output_fft->calculate();
311 _signal_analysis_running = false;
313 // This signals calls expose_analysis_area()
314 _analysis_area->queue_draw();
317 void
318 PluginEqGui::run_impulse_analysis()
320 uint32_t inputs = _plugin->get_info()->n_inputs.n_audio();
321 uint32_t outputs = _plugin->get_info()->n_outputs.n_audio();
323 // Create the impulse, can't use silence() because consecutive calls won't work
324 for (uint32_t i = 0; i < inputs; ++i) {
325 ARDOUR::AudioBuffer& buf = _bufferset.get_audio(i);
326 ARDOUR::Sample* d = buf.data();
327 memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size);
328 *d = 1.0;
331 ARDOUR::ChanMapping in_map(_plugin->get_info()->n_inputs);
332 ARDOUR::ChanMapping out_map(_plugin->get_info()->n_outputs);
334 _plugin->connect_and_run(_bufferset, in_map, out_map, _buffer_size, 0);
335 framecnt_t f = _plugin->signal_latency ();
336 // Adding user_latency() could be interesting
338 // Gather all output, taking latency into account.
339 _impulse_fft->reset();
341 // Silence collect buffers to copy data to, can't use silence() because consecutive calls won't work
342 for (uint32_t i = 0; i < outputs; ++i) {
343 ARDOUR::AudioBuffer &buf = _collect_bufferset.get_audio(i);
344 ARDOUR::Sample *d = buf.data();
345 memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size);
348 if (f == 0) {
349 //std::cerr << "0: no latency, copying full buffer, trivial.." << std::endl;
350 for (uint32_t i = 0; i < outputs; ++i) {
351 memcpy(_collect_bufferset.get_audio(i).data(),
352 _bufferset.get_audio(i).data(), _buffer_size * sizeof(float));
354 } else {
355 //int C = 0;
356 //std::cerr << (++C) << ": latency is " << f << " frames, doing split processing.." << std::endl;
357 framecnt_t target_offset = 0;
358 framecnt_t frames_left = _buffer_size; // refaktoroi
359 do {
360 if (f >= _buffer_size) {
361 //std::cerr << (++C) << ": f (=" << f << ") is larger than buffer_size, still trying to reach the actual output" << std::endl;
362 // there is no data in this buffer regarding to the input!
363 f -= _buffer_size;
364 } else {
365 // this buffer contains either the first, last or a whole bu the output of the impulse
366 // first part: offset is 0, so we copy to the start of _collect_bufferset
367 // we start at output offset "f"
368 // .. and copy "buffer size" - "f" - "offset" frames
370 framecnt_t length = _buffer_size - f - target_offset;
372 //std::cerr << (++C) << ": copying " << length << " frames to _collect_bufferset.get_audio(i)+" << target_offset << " from bufferset at offset " << f << std::endl;
373 for (uint32_t i = 0; i < outputs; ++i) {
374 memcpy(_collect_bufferset.get_audio(i).data(target_offset),
375 _bufferset.get_audio(i).data() + f,
376 length * sizeof(float));
379 target_offset += length;
380 frames_left -= length;
381 f = 0;
383 if (frames_left > 0) {
384 // Silence the buffers
385 for (uint32_t i = 0; i < inputs; ++i) {
386 ARDOUR::AudioBuffer &buf = _bufferset.get_audio(i);
387 ARDOUR::Sample *d = buf.data();
388 memset(d, 0, sizeof(ARDOUR::Sample)*_buffer_size);
391 in_map = ARDOUR::ChanMapping(_plugin->get_info()->n_inputs);
392 out_map = ARDOUR::ChanMapping(_plugin->get_info()->n_outputs);
393 _plugin->connect_and_run(_bufferset, in_map, out_map, _buffer_size, 0);
395 } while ( frames_left > 0);
400 for (uint32_t i = 0; i < outputs; ++i) {
401 _impulse_fft->analyze(_collect_bufferset.get_audio(i).data());
404 // normalize the output
405 _impulse_fft->calculate();
407 // This signals calls expose_analysis_area()
408 _analysis_area->queue_draw();
411 bool
412 PluginEqGui::expose_analysis_area(GdkEventExpose *)
414 redraw_analysis_area();
415 return true;
418 void
419 PluginEqGui::draw_analysis_scales(cairo_t *ref_cr)
421 // TODO: check whether we need rounding
422 _analysis_scale_surface = cairo_surface_create_similar(cairo_get_target(ref_cr),
423 CAIRO_CONTENT_COLOR,
424 _analysis_width,
425 _analysis_height);
427 cairo_t *cr = cairo_create (_analysis_scale_surface);
429 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
430 cairo_rectangle(cr, 0.0, 0.0, _analysis_width, _analysis_height);
431 cairo_fill(cr);
434 draw_scales_power(_analysis_area, cr);
435 if (_phase_button->get_active()) {
436 draw_scales_phase(_analysis_area, cr);
439 cairo_destroy(cr);
443 void
444 PluginEqGui::redraw_analysis_area()
446 cairo_t *cr;
448 cr = gdk_cairo_create(GDK_DRAWABLE(_analysis_area->get_window()->gobj()));
450 if (_analysis_scale_surface == 0) {
451 draw_analysis_scales(cr);
455 cairo_copy_page(cr);
457 cairo_set_source_surface(cr, _analysis_scale_surface, 0.0, 0.0);
458 cairo_paint(cr);
460 if (_phase_button->get_active()) {
461 plot_impulse_phase(_analysis_area, cr);
463 plot_impulse_amplitude(_analysis_area, cr);
465 // TODO: make this optional
466 plot_signal_amplitude_difference(_analysis_area, cr);
468 cairo_destroy(cr);
473 #define PHASE_PROPORTION 0.5
475 void
476 PluginEqGui::draw_scales_phase(Gtk::Widget */*w*/, cairo_t *cr)
478 float y;
479 cairo_font_extents_t extents;
480 cairo_font_extents(cr, &extents);
482 char buf[256];
483 cairo_text_extents_t t_ext;
485 for (uint32_t i = 0; i < 3; i++) {
487 y = _analysis_height/2.0 - (float)i*(_analysis_height/8.0)*PHASE_PROPORTION;
489 cairo_set_source_rgb(cr, .8, .9, 0.2);
490 if (i == 0) {
491 snprintf(buf,256, "0\u00b0");
492 } else {
493 snprintf(buf,256, "%d\u00b0", (i * 45));
495 cairo_text_extents(cr, buf, &t_ext);
496 cairo_move_to(cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
497 cairo_show_text(cr, buf);
499 if (i == 0)
500 continue;
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);
508 y = _analysis_height/2.0 + (float)i*(_analysis_height/8.0)*PHASE_PROPORTION;
510 // label
511 snprintf(buf,256, "-%d\u00b0", (i * 45));
512 cairo_set_source_rgb(cr, .8, .9, 0.2);
513 cairo_text_extents(cr, buf, &t_ext);
514 cairo_move_to(cr, _analysis_width - t_ext.width - t_ext.x_bearing - 2.0, y - extents.descent);
515 cairo_show_text(cr, buf);
517 // line
518 cairo_set_source_rgba(cr, .8, .9, 0.2, 0.6/(float)i);
519 cairo_move_to(cr, 0.0, y);
520 cairo_line_to(cr, _analysis_width, y);
522 cairo_set_line_width (cr, 0.25 + 1.0/(float)(i+1));
523 cairo_stroke(cr);
527 void
528 PluginEqGui::plot_impulse_phase(Gtk::Widget *w, cairo_t *cr)
530 float x,y;
532 int prevX = 0;
533 float avgY = 0.0;
534 int avgNum = 0;
536 // float width = w->get_width();
537 float height = w->get_height();
539 cairo_set_source_rgba(cr, 0.95, 0.3, 0.2, 1.0);
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 y = _analysis_height/2.0 - (_impulse_fft->phase_at_bin(i)/M_PI)*(_analysis_height/2.0)*PHASE_PROPORTION;
547 if ( i == 0 ) {
548 cairo_move_to(cr, x, y);
550 avgY = 0;
551 avgNum = 0;
552 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
553 avgY = avgY/(float)avgNum;
554 if (avgY > (height * 10.0) ) avgY = height * 10.0;
555 if (avgY < (-height * 10.0) ) avgY = -height * 10.0;
556 cairo_line_to(cr, prevX, avgY);
557 //cairo_line_to(cr, prevX, avgY/(float)avgNum);
559 avgY = 0;
560 avgNum = 0;
564 prevX = rint(x);
565 avgY += y;
566 avgNum++;
569 cairo_set_line_width (cr, 2.0);
570 cairo_stroke(cr);
573 void
574 PluginEqGui::draw_scales_power(Gtk::Widget */*w*/, cairo_t *cr)
576 if (_impulse_fft == 0) {
577 return;
580 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 };
581 float divisor = _samplerate / 2.0 / _impulse_fft->bins();
582 float x;
584 cairo_set_line_width (cr, 1.5);
585 cairo_set_font_size(cr, 9);
587 cairo_font_extents_t extents;
588 cairo_font_extents(cr, &extents);
589 // float fontXOffset = extents.descent + 1.0;
591 char buf[256];
593 for (uint32_t i = 0; scales[i] != -1.0; ++i) {
594 float bin = scales[i] / divisor;
596 x = log10f(1.0 + bin / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
597 x *= _analysis_width;
599 if (scales[i] < 1000.0) {
600 snprintf(buf, 256, "%0.0f", scales[i]);
601 } else {
602 snprintf(buf, 256, "%0.0fk", scales[i]/1000.0);
605 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
607 //cairo_move_to(cr, x + fontXOffset, 3.0);
608 cairo_move_to(cr, x - extents.height, 3.0);
610 cairo_rotate(cr, M_PI / 2.0);
611 cairo_show_text(cr, buf);
612 cairo_rotate(cr, -M_PI / 2.0);
613 cairo_stroke(cr);
615 cairo_set_source_rgb(cr, 0.3, 0.3, 0.3);
616 cairo_move_to(cr, x, _analysis_height);
617 cairo_line_to(cr, x, 0.0);
618 cairo_stroke(cr);
621 float y;
623 //double dashes[] = { 1.0, 3.0, 4.5, 3.0 };
624 double dashes[] = { 3.0, 5.0 };
626 for (float dB = 0.0; dB < _max_dB; dB += _step_dB ) {
627 snprintf(buf, 256, "+%0.0f", dB );
629 y = ( _max_dB - dB) / ( _max_dB - _min_dB );
630 //std::cerr << " y = " << y << std::endl;
631 y *= _analysis_height;
633 if (dB != 0.0) {
634 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
635 cairo_move_to(cr, 1.0, y + extents.height + 1.0);
636 cairo_show_text(cr, buf);
637 cairo_stroke(cr);
640 cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
641 cairo_move_to(cr, 0, y);
642 cairo_line_to(cr, _analysis_width, y);
643 cairo_stroke(cr);
645 if (dB == 0.0) {
646 cairo_set_dash(cr, dashes, 2, 0.0);
652 for (float dB = - _step_dB; dB > _min_dB; dB -= _step_dB ) {
653 snprintf(buf, 256, "%0.0f", dB );
655 y = ( _max_dB - dB) / ( _max_dB - _min_dB );
656 y *= _analysis_height;
658 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
659 cairo_move_to(cr, 1.0, y - extents.descent - 1.0);
660 cairo_show_text(cr, buf);
661 cairo_stroke(cr);
663 cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
664 cairo_move_to(cr, 0, y);
665 cairo_line_to(cr, _analysis_width, y);
666 cairo_stroke(cr);
669 cairo_set_dash(cr, 0, 0, 0.0);
673 inline float
674 power_to_dB(float a)
676 return 10.0 * log10f(a);
679 void
680 PluginEqGui::plot_impulse_amplitude(Gtk::Widget *w, cairo_t *cr)
682 float x,y;
683 int prevX = 0;
684 float avgY = 0.0;
685 int avgNum = 0;
687 // float width = w->get_width();
688 float height = w->get_height();
690 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
691 cairo_set_line_width (cr, 2.5);
693 for (uint32_t i = 0; i < _impulse_fft->bins()-1; i++) {
694 // x coordinate of bin i
695 x = log10f(1.0 + (float)i / (float)_impulse_fft->bins() * _log_coeff) / _log_max;
696 x *= _analysis_width;
698 float yCoeff = ( power_to_dB(_impulse_fft->power_at_bin(i)) - _min_dB) / (_max_dB - _min_dB);
700 y = _analysis_height - _analysis_height*yCoeff;
702 if ( i == 0 ) {
703 cairo_move_to(cr, x, y);
705 avgY = 0;
706 avgNum = 0;
707 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
708 avgY = avgY/(float)avgNum;
709 if (avgY > (height * 10.0) ) avgY = height * 10.0;
710 if (avgY < (-height * 10.0) ) avgY = -height * 10.0;
711 cairo_line_to(cr, prevX, avgY);
712 //cairo_line_to(cr, prevX, avgY/(float)avgNum);
714 avgY = 0;
715 avgNum = 0;
719 prevX = rint(x);
720 avgY += y;
721 avgNum++;
724 cairo_stroke(cr);
727 void
728 PluginEqGui::plot_signal_amplitude_difference(Gtk::Widget *w, cairo_t *cr)
730 float x,y;
732 int prevX = 0;
733 float avgY = 0.0;
734 int avgNum = 0;
736 // float width = w->get_width();
737 float height = w->get_height();
739 cairo_set_source_rgb(cr, 0.0, 1.0, 0.0);
740 cairo_set_line_width (cr, 2.5);
742 for (uint32_t i = 0; i < _signal_input_fft->bins()-1; i++) {
743 // x coordinate of bin i
744 x = log10f(1.0 + (float)i / (float)_signal_input_fft->bins() * _log_coeff) / _log_max;
745 x *= _analysis_width;
747 float power_out = power_to_dB(_signal_output_fft->power_at_bin(i));
748 float power_in = power_to_dB(_signal_input_fft ->power_at_bin(i));
749 float power = power_out - power_in;
751 // for SaBer
753 double p = 10.0 * log10( 1.0 + (double)_signal_output_fft->power_at_bin(i) - (double)
754 - _signal_input_fft ->power_at_bin(i));
755 //p *= 1000000.0;
756 float power = (float)p;
758 if ( (i % 1000) == 0) {
759 std::cerr << i << ": " << power << std::endl;
763 if (std::isinf(power)) {
764 if (power < 0) {
765 power = _min_dB - 1.0;
766 } else {
767 power = _max_dB - 1.0;
769 } else if (std::isnan(power)) {
770 power = _min_dB - 1.0;
773 float yCoeff = ( power - _min_dB) / (_max_dB - _min_dB);
775 y = _analysis_height - _analysis_height*yCoeff;
777 if ( i == 0 ) {
778 cairo_move_to(cr, x, y);
780 avgY = 0;
781 avgNum = 0;
782 } else if (rint(x) > prevX || i == _impulse_fft->bins()-1 ) {
783 avgY = avgY/(float)avgNum;
784 if (avgY > (height * 10.0) ) avgY = height * 10.0;
785 if (avgY < (-height * 10.0) ) avgY = -height * 10.0;
786 cairo_line_to(cr, prevX, avgY);
788 avgY = 0;
789 avgNum = 0;
793 prevX = rint(x);
794 avgY += y;
795 avgNum++;
798 cairo_stroke(cr);