overhaul region/range spectrum analysis

This commit is contained in:
Robin Gareus 2016-05-22 19:29:08 +02:00
parent ef365d0310
commit 1b3b42403b
6 changed files with 481 additions and 455 deletions

View file

@ -41,20 +41,14 @@
using namespace ARDOUR; using namespace ARDOUR;
using namespace PBD; using namespace PBD;
AnalysisWindow::AnalysisWindow() : AnalysisWindow::AnalysisWindow()
: source_selection_label (_("Signal source"))
source_selection_label (_("Signal source")), , source_selection_ranges_rb (_("Selected ranges"))
source_selection_ranges_rb (_("Selected ranges")), , source_selection_regions_rb (_("Selected regions"))
source_selection_regions_rb (_("Selected regions")), , show_minmax_button (_("Show frequency power range"))
, show_normalized_button (_("Fit dB range"))
display_model_label (_("Display model")), , show_proportional_button (_("Proportional Spectum, -18dB"))
display_model_composite_separate_rb (_("Composite graphs for each track")), , fft_graph (16384)
display_model_composite_all_tracks_rb (_("Composite graph of all tracks")),
show_minmax_button (_("Show frequency power range")),
show_normalized_button (_("Normalize values")),
fft_graph (16384)
{ {
set_name(_("FFT analysis window")); set_name(_("FFT analysis window"));
set_title (_("Spectral Analysis")); set_title (_("Spectral Analysis"));
@ -107,49 +101,31 @@ AnalysisWindow::AnalysisWindow() :
sigc::bind ( sigc::mem_fun(*this, &AnalysisWindow::source_selection_changed), &source_selection_regions_rb)); sigc::bind ( sigc::mem_fun(*this, &AnalysisWindow::source_selection_changed), &source_selection_regions_rb));
} }
vbox.pack_start(hseparator1, false, false);
// "Display model"
vbox.pack_start(display_model_label, false, false);
{
Gtk::RadioButtonGroup group = display_model_composite_separate_rb.get_group();
display_model_composite_all_tracks_rb.set_group (group);
display_model_composite_separate_rb.set_active();
vbox.pack_start (display_model_composite_separate_rb, false, false);
vbox.pack_start (display_model_composite_all_tracks_rb, false, false);
// "Composite graphs for all tracks"
display_model_composite_separate_rb.signal_toggled().connect (
sigc::bind ( sigc::mem_fun(*this, &AnalysisWindow::display_model_changed), &display_model_composite_separate_rb));
// "Composite graph of all tracks"
display_model_composite_all_tracks_rb.signal_toggled().connect (
sigc::bind ( sigc::mem_fun(*this, &AnalysisWindow::display_model_changed), &display_model_composite_all_tracks_rb));
}
// Analyze button // Analyze button
refresh_button.set_name("EditorGTKButton"); refresh_button.set_name("EditorGTKButton");
refresh_button.set_label(_("Re-analyze data")); refresh_button.set_label(_("Re-analyze data"));
refresh_button.signal_clicked().connect ( sigc::bind ( sigc::mem_fun(*this, &AnalysisWindow::analyze_data), &refresh_button)); refresh_button.signal_clicked().connect ( sigc::bind ( sigc::mem_fun(*this, &AnalysisWindow::analyze_data), &refresh_button));
vbox.pack_start(refresh_button, false, false, 10); vbox.pack_start(refresh_button, false, false, 10);
vbox.pack_start(hseparator1, false, false);
// Feature checkboxes // Feature checkboxes
// normalize, fit y-range
show_normalized_button.signal_toggled().connect( sigc::mem_fun(*this, &AnalysisWindow::show_normalized_changed));
vbox.pack_start(show_normalized_button, false, false);
// minmax // minmax
show_minmax_button.signal_toggled().connect( sigc::mem_fun(*this, &AnalysisWindow::show_minmax_changed)); show_minmax_button.signal_toggled().connect( sigc::mem_fun(*this, &AnalysisWindow::show_minmax_changed));
vbox.pack_start(show_minmax_button, false, false); vbox.pack_start(show_minmax_button, false, false);
// normalize // pink-noise / proportional spectrum
show_normalized_button.signal_toggled().connect( sigc::mem_fun(*this, &AnalysisWindow::show_normalized_changed)); show_proportional_button.signal_toggled().connect( sigc::mem_fun(*this, &AnalysisWindow::show_proportional_changed));
vbox.pack_start(show_normalized_button, false, false); vbox.pack_start(show_proportional_button, false, false);
@ -185,6 +161,12 @@ AnalysisWindow::show_normalized_changed()
fft_graph.set_show_normalized(show_normalized_button.get_active()); fft_graph.set_show_normalized(show_normalized_button.get_active());
} }
void
AnalysisWindow::show_proportional_changed()
{
fft_graph.set_show_proportioanl(show_proportional_button.get_active());
}
void void
AnalysisWindow::set_rangemode() AnalysisWindow::set_rangemode()
{ {

View file

@ -46,7 +46,6 @@ namespace ARDOUR {
class Session; class Session;
} }
class AnalysisWindow : public Gtk::Window, public ARDOUR::SessionHandlePtr class AnalysisWindow : public Gtk::Window, public ARDOUR::SessionHandlePtr
{ {
public: public:
@ -65,8 +64,10 @@ private:
void source_selection_changed (Gtk::RadioButton *); void source_selection_changed (Gtk::RadioButton *);
void display_model_changed (Gtk::RadioButton *); void display_model_changed (Gtk::RadioButton *);
void show_minmax_changed (); void show_minmax_changed ();
void show_normalized_changed (); void show_normalized_changed ();
void show_proportional_changed ();
void analyze_data (Gtk::Button *); void analyze_data (Gtk::Button *);
@ -95,22 +96,16 @@ private:
Gtk::Label source_selection_label; Gtk::Label source_selection_label;
Gtk::RadioButton source_selection_ranges_rb; Gtk::RadioButton source_selection_ranges_rb;
Gtk::RadioButton source_selection_regions_rb; Gtk::RadioButton source_selection_regions_rb;
Gtk::HSeparator hseparator1; Gtk::HSeparator hseparator1;
Gtk::Label display_model_label;
Gtk::RadioButton display_model_composite_separate_rb;
Gtk::RadioButton display_model_composite_all_tracks_rb;
Gtk::Button refresh_button; Gtk::Button refresh_button;
Gtk::CheckButton show_minmax_button; Gtk::CheckButton show_minmax_button;
Gtk::CheckButton show_normalized_button; Gtk::CheckButton show_normalized_button;
Gtk::CheckButton show_proportional_button;
// The graph // The graph
FFTGraph fft_graph; FFTGraph fft_graph;

View file

@ -14,7 +14,6 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
#ifdef COMPILER_MSVC #ifdef COMPILER_MSVC
@ -40,12 +39,15 @@ using std::min; using std::max;
#include "fft_graph.h" #include "fft_graph.h"
#include "analysis_window.h" #include "analysis_window.h"
#include "public_editor.h"
#include "i18n.h"
using namespace std; using namespace std;
using namespace Gtk; using namespace Gtk;
using namespace Gdk; using namespace Gdk;
FFTGraph::FFTGraph(int windowSize) FFTGraph::FFTGraph (int windowSize)
{ {
_logScale = 0; _logScale = 0;
@ -58,123 +60,125 @@ FFTGraph::FFTGraph(int windowSize)
_show_minmax = false; _show_minmax = false;
_show_normalized = false; _show_normalized = false;
_show_proportional = false;
setWindowSize(windowSize); setWindowSize (windowSize);
} }
void void
FFTGraph::setWindowSize(int windowSize) FFTGraph::setWindowSize (int windowSize)
{ {
if (_a_window) { if (_a_window) {
Glib::Threads::Mutex::Lock lm (_a_window->track_list_lock); Glib::Threads::Mutex::Lock lm (_a_window->track_list_lock);
setWindowSize_internal(windowSize); setWindowSize_internal (windowSize);
} else { } else {
setWindowSize_internal(windowSize); setWindowSize_internal (windowSize);
} }
} }
void void
FFTGraph::setWindowSize_internal(int windowSize) FFTGraph::setWindowSize_internal (int windowSize)
{ {
// remove old tracklist & graphs // remove old tracklist & graphs
if (_a_window) { if (_a_window) {
_a_window->clear_tracklist(); _a_window->clear_tracklist ();
} }
_windowSize = windowSize; _windowSize = windowSize;
_dataSize = windowSize / 2; _dataSize = windowSize / 2;
if (_in != 0) { if (_in != 0) {
fftwf_destroy_plan(_plan); fftwf_destroy_plan (_plan);
free(_in); free (_in);
_in = 0; _in = 0;
} }
if (_out != 0) { if (_out != 0) {
free(_out); free (_out);
_out = 0; _out = 0;
} }
if (_hanning != 0) { if (_hanning != 0) {
free(_hanning); free (_hanning);
_hanning = 0; _hanning = 0;
} }
if (_logScale != 0) { if (_logScale != 0) {
free(_logScale); free (_logScale);
_logScale = 0; _logScale = 0;
} }
// When destroying, window size is set to zero to free up memory // When destroying, window size is set to zero to free up memory
if (windowSize == 0) if (windowSize == 0) {
return; return;
}
// FFT input & output buffers // FFT input & output buffers
_in = (float *) fftwf_malloc(sizeof(float) * _windowSize); _in = (float *) fftwf_malloc (sizeof (float) * _windowSize);
_out = (float *) fftwf_malloc(sizeof(float) * _windowSize); _out = (float *) fftwf_malloc (sizeof (float) * _windowSize);
// Hanning window // Hanning window
_hanning = (float *) malloc(sizeof(float) * _windowSize); _hanning = (float *) malloc (sizeof (float) * _windowSize);
// normalize the window // normalize the window
double sum = 0.0; double sum = 0.0;
for (int i=0; i < _windowSize; i++) { for (unsigned int i = 0; i < _windowSize; ++i) {
_hanning[i]=0.81f * ( 0.5f - (0.5f * (float) cos(2.0f * M_PI * (float)i / (float)(_windowSize)))); _hanning[i] = 0.5f - (0.5f * (float) cos (2.0f * M_PI * (float)i / (float)(_windowSize)));
sum += _hanning[i]; sum += _hanning[i];
} }
double isum = 1.0 / sum; double isum = 2.0 / sum;
for (int i=0; i < _windowSize; i++) { for (unsigned int i = 0; i < _windowSize; i++) {
_hanning[i] *= isum; _hanning[i] *= isum;
} }
_logScale = (int *) malloc(sizeof(int) * _dataSize); _logScale = (int *) malloc (sizeof (int) * _dataSize);
//float count = 0;
for (int i = 0; i < _dataSize; i++) { for (unsigned int i = 0; i < _dataSize; i++) {
_logScale[i] = 0; _logScale[i] = 0;
} }
_plan = fftwf_plan_r2r_1d(_windowSize, _in, _out, FFTW_R2HC, FFTW_ESTIMATE); _plan = fftwf_plan_r2r_1d (_windowSize, _in, _out, FFTW_R2HC, FFTW_MEASURE);
} }
FFTGraph::~FFTGraph() FFTGraph::~FFTGraph ()
{ {
// This will free everything // This will free everything
setWindowSize(0); setWindowSize (0);
} }
bool bool
FFTGraph::on_expose_event (GdkEventExpose* /*event*/) FFTGraph::on_expose_event (GdkEventExpose* /*event*/)
{ {
redraw(); redraw ();
return true; return true;
} }
FFTResult * FFTResult *
FFTGraph::prepareResult(Gdk::Color color, string trackname) FFTGraph::prepareResult (Gdk::Color color, string trackname)
{ {
FFTResult *res = new FFTResult(this, color, trackname); FFTResult *res = new FFTResult (this, color, trackname);
return res; return res;
} }
void void
FFTGraph::set_analysis_window(AnalysisWindow *a_window) FFTGraph::set_analysis_window (AnalysisWindow *a_window)
{ {
_a_window = a_window; _a_window = a_window;
} }
void int
FFTGraph::draw_scales(Glib::RefPtr<Gdk::Window> window) FFTGraph::draw_scales (Glib::RefPtr<Gdk::Window> window)
{ {
int label_height = v_margin;
Glib::RefPtr<Gtk::Style> style = get_style(); Glib::RefPtr<Gtk::Style> style = get_style ();
Glib::RefPtr<Gdk::GC> black = style->get_black_gc(); Glib::RefPtr<Gdk::GC> black = style->get_black_gc ();
Glib::RefPtr<Gdk::GC> white = style->get_white_gc(); Glib::RefPtr<Gdk::GC> white = style->get_white_gc ();
window->draw_rectangle(black, true, 0, 0, width, height); window->draw_rectangle (black, true, 0, 0, width, height);
/** /**
* 4 5 * 4 5
@ -186,97 +190,121 @@ FFTGraph::draw_scales(Glib::RefPtr<Gdk::Window> window)
**/ **/
// Line 1 // Line 1
window->draw_line(white, h_margin, v_margin, h_margin, height - v_margin ); window->draw_line (white, hl_margin, v_margin, hl_margin, height - v_margin);
// Line 2 // Line 2
window->draw_line(white, width - h_margin + 1, v_margin, width - h_margin + 1, height - v_margin ); window->draw_line (white, width - hr_margin + 1, v_margin, width - hr_margin + 1, height - v_margin);
// Line 3 // Line 3
window->draw_line(white, h_margin, height - v_margin, width - h_margin, height - v_margin ); window->draw_line (white, hl_margin, height - v_margin, width - hr_margin, height - v_margin);
#define DB_METRIC_LENGTH 8
// Line 4 // Line 4
window->draw_line(white, h_margin - DB_METRIC_LENGTH, v_margin, h_margin, v_margin ); window->draw_line (white, 3, v_margin, hl_margin, v_margin);
// Line 5 // Line 5
window->draw_line(white, width - h_margin + 1, v_margin, width - h_margin + DB_METRIC_LENGTH, v_margin ); window->draw_line (white, width - hr_margin + 1, v_margin, width - 3, v_margin);
if (graph_gc == 0) { if (graph_gc == 0) {
graph_gc = GC::create( get_window() ); graph_gc = GC::create (get_window ());
} }
Color grey; Color grey;
grey.set_rgb_p (0.2, 0.2, 0.2);
grey.set_rgb_p(0.2, 0.2, 0.2); graph_gc->set_rgb_fg_color (grey);
graph_gc->set_rgb_fg_color( grey );
if (layout == 0) { if (layout == 0) {
layout = create_pango_layout (""); layout = create_pango_layout ("");
layout->set_font_description (get_style()->get_font()); layout->set_font_description (get_style ()->get_font ());
} }
// Draw logscale // Draw x-axis scale 1/3 octaves centered around 1K
int logscale_pos = 0; int overlap = 0;
int position_on_scale;
// make sure 1K (x=0) is visible
for (int x = 0; x < 27; ++x) {
float freq = powf (2.f, x / 3.0) * 1000.f;
if (freq <= _fft_start) { continue; }
if (freq >= _fft_end) { break; }
/* TODO, write better scales and change the log function so that octaves are of equal pixel length const float pos = currentScaleWidth * logf (freq / _fft_start) / _fft_log_base;
float scale_points[10] = { 55.0, 110.0, 220.0, 440.0, 880.0, 1760.0, 3520.0, 7040.0, 14080.0, 28160.0 }; const int coord = floor (hl_margin + pos);
for (int x = 0; x < 10; x++) {
// i = 0.. _dataSize-1
float freq_at_bin = (SR/2.0) * ((double)i / (double)_dataSize);
freq_at_pixel = FFT_START * exp( FFT_RANGE * pixel / (double)(currentScaleWidth - 1) );
}
*/
for (int x = 1; x < 8; x++) {
position_on_scale = (int)floor( (double)currentScaleWidth*(double)x/8.0);
while (_logScale[logscale_pos] < position_on_scale)
logscale_pos++;
int coord = (int)(v_margin + 1.0 + position_on_scale);
int SR = 44100;
int rate_at_pos = (int)((double)(SR/2) * (double)logscale_pos / (double)_dataSize);
char buf[32];
if (rate_at_pos < 1000)
snprintf(buf,32,"%dHz",rate_at_pos);
else
snprintf(buf,32,"%dk",(int)floor( (float)rate_at_pos/(float)1000) );
std::string label = buf;
layout->set_text(label);
window->draw_line(graph_gc, coord, v_margin, coord, height - v_margin - 1);
int width, height;
layout->get_pixel_size (width, height);
window->draw_layout(white, coord - width / 2, v_margin / 2, layout);
if (coord < overlap) {
continue;
} }
std::stringstream ss;
if (freq >= 10000) {
ss << std::setprecision (1) << std::fixed << freq / 1000 << "K";
} else if (freq >= 1000) {
ss << std::setprecision (2) << std::fixed << freq / 1000 << "K";
} else {
ss << std::setprecision (0) << std::fixed << freq << "Hz";
}
layout->set_text (ss.str ());
int lw, lh;
layout->get_pixel_size (lw, lh);
overlap = coord + lw + 3;
if (coord + lw / 2 > width - hr_margin - 2) {
break;
}
if (v_margin / 2 + lh > label_height) {
label_height = v_margin / 2 + lh;
}
window->draw_line (graph_gc, coord, v_margin, coord, height - v_margin - 1);
window->draw_layout (white, coord - lw / 2, v_margin / 2, layout);
}
// now from 1K down to 4Hz
for (int x = 0; x > -24; --x) {
float freq = powf (2.f, x / 3.0) * 1000.f;
if (freq >= _fft_end) { continue; }
if (freq <= _fft_start) { break; }
const float pos = currentScaleWidth * logf (freq / _fft_start) / _fft_log_base;
const int coord = floor (hl_margin + pos);
if (x != 0 && coord > overlap) {
continue;
}
std::stringstream ss;
if (freq >= 10000) {
ss << std::setprecision (1) << std::fixed << freq / 1000 << "K";
} else if (freq >= 1000) {
ss << std::setprecision (2) << std::fixed << freq / 1000 << "K";
} else {
ss << std::setprecision (0) << std::fixed << freq << "Hz";
}
layout->set_text (ss.str ());
int lw, lh;
layout->get_pixel_size (lw, lh);
overlap = coord - lw - 3;
if (coord - lw / 2 < hl_margin + 2) {
break;
}
if (x == 0) {
// just get overlap position
continue;
}
if (v_margin / 2 + lh > label_height) {
label_height = v_margin / 2 + lh;
}
window->draw_line (graph_gc, coord, v_margin, coord, height - v_margin - 1);
window->draw_layout (white, coord - lw / 2, v_margin / 2, layout);
}
return label_height;
} }
void void
FFTGraph::redraw() FFTGraph::redraw ()
{ {
Glib::Threads::Mutex::Lock lm (_a_window->track_list_lock); Glib::Threads::Mutex::Lock lm (_a_window->track_list_lock);
draw_scales(get_window()); int yoff = draw_scales (get_window ());
if (_a_window == 0) if (_a_window == 0)
return; return;
@ -284,57 +312,105 @@ FFTGraph::redraw()
if (!_a_window->track_list_ready) if (!_a_window->track_list_ready)
return; return;
cairo_t *cr; float minf;
cr = gdk_cairo_create(GDK_DRAWABLE(get_window()->gobj())); float maxf;
cairo_set_line_width(cr, 1.5);
cairo_translate(cr, (float)v_margin + 1.0, (float)h_margin);
TreeNodeChildren track_rows = _a_window->track_list.get_model ()->children ();
if (!_show_normalized) {
// Find "session wide" min & max maxf = 0.0f;
float minf = 1000000000000.0; minf = -108.0f;
float maxf = -1000000000000.0; } else {
minf = 999.0f;
TreeNodeChildren track_rows = _a_window->track_list.get_model()->children(); maxf = -999.0f;
for (TreeIter i = track_rows.begin (); i != track_rows.end (); i++) {
for (TreeIter i = track_rows.begin(); i != track_rows.end(); i++) {
TreeModel::Row row = *i; TreeModel::Row row = *i;
FFTResult *res = row[_a_window->tlcols.graph]; FFTResult *res = row[_a_window->tlcols.graph];
// disregard fft analysis from empty signals // disregard fft analysis from empty signals
if (res->minimum() == res->maximum()) { if (res->minimum (_show_proportional) == res->maximum (_show_proportional)) {
continue;
}
// don't include invisible graphs
if (!row[_a_window->tlcols.visible]) {
continue; continue;
} }
if ( res->minimum() < minf) { minf = std::min (minf, res->minimum (_show_proportional));
minf = res->minimum(); maxf = std::max (maxf, res->maximum (_show_proportional));
}
if ( res->maximum() > maxf) {
maxf = res->maximum();
} }
} }
if (!_show_normalized) { // clamp range, > -200dBFS, at least 24dB (two y-axis labels) range
minf = -150.0f; minf = std::max (-200.f, minf);
maxf = 0.0f; if (maxf <= minf) {
return;
} }
//int graph_height = height - 2 * h_margin; if (maxf - minf < 24) {
maxf += 6.f;
minf = maxf - 24.f;
}
cairo_t *cr;
cr = gdk_cairo_create (GDK_DRAWABLE (get_window ()->gobj ()));
cairo_set_line_width (cr, 1.5);
cairo_translate (cr, hl_margin + 1, yoff);
float fft_pane_size_w = width - hl_margin - hr_margin;
float fft_pane_size_w = (float)(width - 2*v_margin) - 1.0; float fft_pane_size_h = height - v_margin - 1 - yoff;
float fft_pane_size_h = (float)(height - 2*h_margin);
double pixels_per_db = (double)fft_pane_size_h / (double)(maxf - minf); double pixels_per_db = (double)fft_pane_size_h / (double)(maxf - minf);
cairo_rectangle(cr, 0.0, 0.0, fft_pane_size_w, fft_pane_size_h); // draw y-axis dB
cairo_clip(cr); cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
for (TreeIter i = track_rows.begin(); i != track_rows.end(); i++) { int btm_lbl = fft_pane_size_h;
{
// y-axis legend
layout->set_text (_("dBFS"));
int lw, lh;
layout->get_pixel_size (lw, lh);
cairo_move_to (cr, -2 - lw, fft_pane_size_h - lh / 2);
pango_cairo_update_layout (cr, layout->gobj ());
pango_cairo_show_layout (cr, layout->gobj ());
btm_lbl = fft_pane_size_h - lh;
}
for (int x = -6; x >= -200; x -= 12) {
float yp = 1.5 + fft_pane_size_h - rint ((x - minf) * pixels_per_db);
assert (layout);
std::stringstream ss;
ss << x;
layout->set_text (ss.str ());
int lw, lh;
layout->get_pixel_size (lw, lh);
if (yp + 2 + lh / 2 > btm_lbl) {
continue;
}
if (yp < 2 + lh / 2) {
continue;
}
cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
cairo_move_to (cr, -2 - lw, yp - lh / 2);
pango_cairo_update_layout (cr, layout->gobj ());
pango_cairo_show_layout (cr, layout->gobj ());
cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
cairo_move_to (cr, 0, yp);
cairo_line_to (cr, fft_pane_size_w, yp);
cairo_stroke (cr);
}
cairo_rectangle (cr, 1, 1, fft_pane_size_w, fft_pane_size_h);
cairo_clip (cr);
cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
for (TreeIter i = track_rows.begin (); i != track_rows.end (); i++) {
TreeModel::Row row = *i; TreeModel::Row row = *i;
// don't show graphs for tracks which are deselected // don't show graphs for tracks which are deselected
@ -345,144 +421,117 @@ FFTGraph::redraw()
FFTResult *res = row[_a_window->tlcols.graph]; FFTResult *res = row[_a_window->tlcols.graph];
// don't show graphs for empty signals // don't show graphs for empty signals
if (res->minimum() == res->maximum()) { if (res->minimum (_show_proportional) == res->maximum (_show_proportional)) {
continue; continue;
} }
float mpp; float mpp;
float X,Y;
if (_show_minmax) { if (_show_minmax) {
mpp = -1000000.0;
cairo_set_source_rgba(cr, res->get_color().get_red_p(), res->get_color().get_green_p(), res->get_color().get_blue_p(), 0.30); X = 0.5f + _logScale[0];
cairo_move_to(cr, 0.5f + (float)_logScale[0], 0.5f + (float)( fft_pane_size_h - (int)floor( (res->maxAt(0) - minf) * pixels_per_db) )); Y = 1.5f + fft_pane_size_h - pixels_per_db * (res->maxAt (0, _show_proportional) - minf);
cairo_move_to (cr, X, Y);
// Draw the line of maximum values // Draw the line of maximum values
for (int x = 1; x < res->length(); x++) { mpp = minf;
if (res->maxAt(x) > mpp) for (unsigned int x = 1; x < res->length () - 1; ++x) {
mpp = res->maxAt(x); mpp = std::max (mpp, res->maxAt (x, _show_proportional));
mpp = fmax(mpp, minf);
mpp = fmin(mpp, maxf);
// If the next point on the log scale is at the same location, if (_logScale[x] == _logScale[x + 1]) {
// don't draw yet
if (x + 1 < res->length() && _logScale[x] == _logScale[x + 1]) {
continue; continue;
} }
float X = 0.5f + (float)_logScale[x]; mpp = fmin (mpp, maxf);
float Y = 0.5f + (float)( fft_pane_size_h - (int)floor( (mpp - minf) * pixels_per_db) ); X = 0.5f + _logScale[x];
Y = 1.5f + fft_pane_size_h - pixels_per_db * (mpp - minf);
cairo_line_to(cr, X, Y); cairo_line_to (cr, X, Y);
mpp = minf;
mpp = -1000000.0;
} }
mpp = +10000000.0; mpp = maxf;
// Draw back to the start using the minimum value // Draw back to the start using the minimum value
for (int x = res->length()-1; x >= 0; x--) { for (int x = res->length () - 1; x >= 0; --x) {
if (res->minAt(x) < mpp) mpp = std::min (mpp, res->minAt (x, _show_proportional));
mpp = res->minAt(x);
mpp = fmax(mpp, minf);
mpp = fmin(mpp, maxf);
// If the next point on the log scale is at the same location, if (_logScale[x] == _logScale[x + 1]) {
// don't draw yet
if (x - 1 > 0 && _logScale[x] == _logScale[x - 1]) {
continue; continue;
} }
float X = 0.5f + (float)_logScale[x]; mpp = fmax (mpp, minf);
float Y = 0.5f + (float)( fft_pane_size_h - (int)floor( (mpp - minf) * pixels_per_db) ); X = 0.5f + _logScale[x];
Y = 1.5f + fft_pane_size_h - pixels_per_db * (mpp - minf);
cairo_line_to(cr, X, Y ); cairo_line_to (cr, X, Y);
mpp = maxf;
mpp = +10000000.0;
} }
cairo_close_path(cr); cairo_set_source_rgba (cr, res->get_color ().get_red_p (), res->get_color ().get_green_p (), res->get_color ().get_blue_p (), 0.30);
cairo_close_path (cr);
cairo_fill(cr); cairo_fill (cr);
} }
// draw max of averages
X = 0.5f + _logScale[0];
Y = 1.5f + fft_pane_size_h - pixels_per_db * (res->avgAt (0, _show_proportional) - minf);
cairo_move_to (cr, X, Y);
mpp = minf;
for (unsigned int x = 0; x < res->length () - 1; x++) {
mpp = std::max (mpp, res->avgAt (x, _show_proportional));
// Set color from track if (_logScale[x] == _logScale[x + 1]) {
cairo_set_source_rgb(cr, res->get_color().get_red_p(), res->get_color().get_green_p(), res->get_color().get_blue_p());
mpp = -1000000.0;
cairo_move_to(cr, 0.5, fft_pane_size_h-0.5);
for (int x = 0; x < res->length(); x++) {
if (res->avgAt(x) > mpp)
mpp = res->avgAt(x);
mpp = fmax(mpp, minf);
mpp = fmin(mpp, maxf);
// If the next point on the log scale is at the same location,
// don't draw yet
if (x + 1 < res->length() && _logScale[x] == _logScale[x + 1]) {
continue; continue;
} }
cairo_line_to(cr, 0.5f + (float)_logScale[x], 0.5f + (float)( fft_pane_size_h - (int)floor( (mpp - minf) * pixels_per_db) )); mpp = fmax (mpp, minf);
mpp = fmin (mpp, maxf);
mpp = -1000000.0; X = 0.5f + _logScale[x];
Y = 1.5f + fft_pane_size_h - pixels_per_db * (mpp - minf);
cairo_line_to (cr, X, Y);
mpp = minf;
} }
cairo_stroke(cr); cairo_set_source_rgb (cr, res->get_color ().get_red_p (), res->get_color ().get_green_p (), res->get_color ().get_blue_p ());
cairo_stroke (cr);
} }
cairo_destroy (cr);
cairo_destroy(cr);
} }
void void
FFTGraph::on_size_request(Gtk::Requisition* requisition) FFTGraph::on_size_request (Gtk::Requisition* requisition)
{ {
width = max(requisition->width, minScaleWidth + h_margin * 2); width = max (requisition->width, minScaleWidth + hl_margin + hr_margin);
height = max(requisition->height, minScaleHeight + 2 + v_margin * 2); height = max (requisition->height, minScaleHeight + 2 + v_margin * 2);
update_size(); update_size ();
requisition->width = width;; requisition->width = width;;
requisition->height = height; requisition->height = height;
} }
void void
FFTGraph::on_size_allocate(Gtk::Allocation & alloc) FFTGraph::on_size_allocate (Gtk::Allocation & alloc)
{ {
width = alloc.get_width(); width = alloc.get_width ();
height = alloc.get_height(); height = alloc.get_height ();
update_size(); update_size ();
DrawingArea::on_size_allocate (alloc); DrawingArea::on_size_allocate (alloc);
} }
void void
FFTGraph::update_size() FFTGraph::update_size ()
{ {
currentScaleWidth = width - h_margin*2; framecnt_t SR = PublicEditor::instance ().session ()->nominal_frame_rate ();
currentScaleHeight = height - 2 - v_margin*2; _fft_start = SR / (double)_dataSize;
_fft_end = .5 * SR;
float SR = 44100; _fft_log_base = logf (.5 * _dataSize);
float FFT_START = SR/(double)_dataSize; currentScaleWidth = width - hl_margin - hr_margin;
float FFT_END = SR/2.0; _logScale[0] = 0;
float FFT_RANGE = log( FFT_END / FFT_START); for (unsigned int i = 1; i < _dataSize; ++i) {
float pixel = 0; _logScale[i] = floor (currentScaleWidth * logf (.5 * i) / _fft_log_base);
for (int i = 0; i < _dataSize; i++) {
float freq_at_bin = (SR/2.0) * ((double)i / (double)_dataSize);
float freq_at_pixel;
pixel--;
do {
pixel++;
freq_at_pixel = FFT_START * exp( FFT_RANGE * pixel / (double)(currentScaleWidth - 1) );
} while (freq_at_bin > freq_at_pixel);
_logScale[i] = (int)floor(pixel);
} }
} }

View file

@ -38,62 +38,68 @@ class AnalysisWindow;
class FFTGraph : public Gtk::DrawingArea class FFTGraph : public Gtk::DrawingArea
{ {
public: public:
FFTGraph(int windowSize); FFTGraph (int windowSize);
~FFTGraph(); ~FFTGraph ();
void set_analysis_window(AnalysisWindow *a_window); void set_analysis_window (AnalysisWindow *a_window);
int windowSize() const { return _windowSize; } int windowSize () const { return _windowSize; }
void setWindowSize(int windowSize); void setWindowSize (int windowSize);
void redraw(); void redraw ();
bool on_expose_event (GdkEventExpose* event); bool on_expose_event (GdkEventExpose* event);
void on_size_request(Gtk::Requisition* requisition); void on_size_request (Gtk::Requisition* requisition);
void on_size_allocate(Gtk::Allocation & alloc); void on_size_allocate (Gtk::Allocation & alloc);
FFTResult *prepareResult(Gdk::Color color, std::string trackname); FFTResult *prepareResult (Gdk::Color color, std::string trackname);
void set_show_minmax (bool v) { _show_minmax = v; redraw(); } void set_show_minmax (bool v) { _show_minmax = v; redraw (); }
void set_show_normalized (bool v) { _show_normalized = v; redraw(); } void set_show_normalized (bool v) { _show_normalized = v; redraw (); }
void set_show_proportioanl(bool v) { _show_proportional = v; redraw (); }
private: private:
void update_size(); void update_size ();
void setWindowSize_internal(int windowSize); void setWindowSize_internal (int windowSize);
void draw_scales(Glib::RefPtr<Gdk::Window> window); int draw_scales (Glib::RefPtr<Gdk::Window> window);
static const int minScaleWidth = 512; static const int minScaleWidth = 512;
static const int minScaleHeight = 420; static const int minScaleHeight = 420;
int currentScaleWidth; static const int hl_margin = 40; // this should scale with font (dBFS labels)
int currentScaleHeight; static const int hr_margin = 12;
static const int v_margin = 12;
static const int h_margin = 20; int currentScaleWidth;
static const int v_margin = 20;
Glib::RefPtr<Gdk::GC> graph_gc; Glib::RefPtr<Gdk::GC> graph_gc;
int width; int width;
int height; int height;
int _windowSize; unsigned int _windowSize;
int _dataSize; unsigned int _dataSize;
Glib::RefPtr<Pango::Layout> layout; Glib::RefPtr<Pango::Layout> layout;
AnalysisWindow *_a_window; AnalysisWindow *_a_window;
fftwf_plan _plan; fftwf_plan _plan;
float *_out; float* _out;
float *_in; float* _in;
float *_hanning; float* _hanning;
int *_logScale; int* _logScale;
bool _show_minmax; bool _show_minmax;
bool _show_normalized; bool _show_normalized;
bool _show_proportional;
float _fft_start;
float _fft_end;
float _fft_log_base;
friend class FFTResult; friend class FFTResult;
}; };

View file

@ -1,22 +1,21 @@
/* /*
Copyright (C) 2006 Paul Davis * Copyright (C) 2006, 2016 Paul Davis
Written by Sampo Savolainen * Written by Sampo Savolainen & Robin Gareus
*
This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or
it under the terms of the GNU General Public License as published by * modify it under the terms of the GNU General Public License
the Free Software Foundation; either version 2 of the License, or * as published by the Free Software Foundation; either version 2
(at your option) any later version. * of the License, or (at your option) any later version.
*
This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. * GNU General Public License for more details.
*
You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
*/
#include "fft_result.h" #include "fft_result.h"
#include "fft_graph.h" #include "fft_graph.h"
@ -24,8 +23,7 @@
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <cmath> #include <cmath>
#include <algorithm>
#include <iostream>
using namespace std; using namespace std;
@ -35,18 +33,24 @@ FFTResult::FFTResult(FFTGraph *graph, Gdk::Color color, string trackname)
_windowSize = _graph->windowSize(); _windowSize = _graph->windowSize();
_dataSize = _windowSize / 2; _dataSize = _windowSize / 2;
_averages = 0; _averages = 0;
_min_flat = _max_flat = 0.0;
_min_prop = _max_prop = 0.0;
_data_avg = (float *) malloc(sizeof(float) * _dataSize); _data_flat_avg = (float *) malloc (sizeof(float) * _dataSize);
memset(_data_avg,0,sizeof(float) * _dataSize); _data_flat_min = (float *) malloc (sizeof(float) * _dataSize);
_data_flat_max = (float *) malloc (sizeof(float) * _dataSize);
_data_prop_avg = (float *) malloc (sizeof(float) * _dataSize);
_data_prop_min = (float *) malloc (sizeof(float) * _dataSize);
_data_prop_max = (float *) malloc (sizeof(float) * _dataSize);
_data_min = (float *) malloc(sizeof(float) * _dataSize); for (unsigned int i = 0; i < _dataSize; i++) {
_data_max = (float *) malloc(sizeof(float) * _dataSize); _data_flat_min[i] = FLT_MAX;
_data_flat_max[i] = FLT_MIN;
for (int i = 0; i < _dataSize; i++) { _data_flat_avg[i] = 0;
_data_min[i] = FLT_MAX; _data_prop_min[i] = FLT_MAX;
_data_max[i] = FLT_MIN; _data_prop_max[i] = FLT_MIN;
_data_prop_avg[i] = 0;
} }
_color = color; _color = color;
@ -56,34 +60,31 @@ FFTResult::FFTResult(FFTGraph *graph, Gdk::Color color, string trackname)
void void
FFTResult::analyzeWindow(float *window) FFTResult::analyzeWindow(float *window)
{ {
float *_hanning = _graph->_hanning; float const * const _hanning = _graph->_hanning;
float *_in = _graph->_in; float *_in = _graph->_in;
float *_out = _graph->_out; float *_out = _graph->_out;
int i;
// Copy the data and apply the hanning window // Copy the data and apply the hanning window
for (i = 0; i < _windowSize; i++) { for (unsigned int i = 0; i < _windowSize; ++i) {
_in[i] = window[ i ] * _hanning[ i ]; _in[i] = window[i] * _hanning[i];
} }
fftwf_execute(_graph->_plan); fftwf_execute(_graph->_plan);
// calculate signal power per bin
float b = _out[0] * _out[0]; float b = _out[0] * _out[0];
_data_avg[0] += b; _data_flat_avg[0] += b;
if (b < _data_min[0]) _data_min[0] = b; if (b < _data_flat_min[0]) _data_flat_min[0] = b;
if (b > _data_max[0]) _data_max[0] = b; if (b > _data_flat_max[0]) _data_flat_max[0] = b;
for (i=1; i < _dataSize - 1; i++) { // TODO: check with Jesse whether this is really correct for (unsigned int i = 1; i < _dataSize - 1; ++i) {
b = (_out[i] * _out[i]); b = (_out[i] * _out[i]) + (_out[_windowSize - i] * _out[_windowSize - i]);
_data_flat_avg[i] += b;
_data_avg[i] += b; // + (_out[_windowSize-i] * _out[_windowSize-i]);, TODO: thanks to Stefan Kost if (_data_flat_min[i] > b) _data_flat_min[i] = b;
if (_data_flat_max[i] < b ) _data_flat_max[i] = b;
if (_data_min[i] > b) _data_min[i] = b;
if (_data_max[i] < b ) _data_max[i] = b;
} }
_averages++; _averages++;
} }
@ -91,32 +92,43 @@ void
FFTResult::finalize() FFTResult::finalize()
{ {
if (_averages == 0) { if (_averages == 0) {
_minimum = 0.0; _min_flat = _max_flat = 0.0;
_maximum = 0.0; _min_prop = _max_prop = 0.0;
return; return;
} }
// Average & scale // Average & scale
for (int i = 0; i < _dataSize; i++) { for (unsigned int i = 0; i < _dataSize - 1; ++i) {
_data_avg[i] /= _averages; _data_flat_avg[i] /= _averages;
_data_avg[i] = 10.0f * log10f(_data_avg[i]); // proportional, pink spectrum @ -18dB
_data_prop_avg[i] = _data_flat_avg [i] * i / 63.096f;
_data_min[i] = 10.0f * log10f(_data_min[i]); _data_prop_min[i] = _data_flat_min [i] * i / 63.096f;
if (_data_min[i] < -10000.0f) { _data_prop_max[i] = _data_flat_max [i] * i / 63.096f;
_data_min[i] = -10000.0f;
} }
_data_max[i] = 10.0f * log10f(_data_max[i]);
_data_prop_avg[0] = _data_flat_avg [0] / 63.096f;
_data_prop_min[0] = _data_flat_min [0] / 63.096f;
_data_prop_max[0] = _data_flat_max [0] / 63.096f;
// calculate power
for (unsigned int i = 0; i < _dataSize - 1; ++i) {
_data_flat_min[i] = power_to_db (_data_flat_min[i]);
_data_flat_max[i] = power_to_db (_data_flat_max[i]);
_data_flat_avg[i] = power_to_db (_data_flat_avg[i]);
_data_prop_min[i] = power_to_db (_data_prop_min[i]);
_data_prop_max[i] = power_to_db (_data_prop_max[i]);
_data_prop_avg[i] = power_to_db (_data_prop_avg[i]);
} }
// find min & max // find min & max
_minimum = _maximum = _data_avg[0]; _min_flat = _max_flat = _data_flat_avg[0];
_min_prop = _max_prop = _data_prop_avg[0];
for (int i = 1; i < _dataSize; i++) { for (unsigned int i = 1; i < _dataSize - 1; ++i) {
if (_data_avg[i] < _minimum && !isinf(_data_avg[i])) { _min_flat = std::min (_min_flat, _data_flat_avg[i]);
_minimum = _data_avg[i]; _max_flat = std::max (_max_flat, _data_flat_avg[i]);
} else if (_data_avg[i] > _maximum && !isinf(_data_avg[i])) { _min_prop = std::min (_min_prop, _data_prop_avg[i]);
_maximum = _data_avg[i]; _max_prop = std::max (_max_prop, _data_prop_avg[i]);
}
} }
_averages = 0; _averages = 0;
@ -124,36 +136,10 @@ FFTResult::finalize()
FFTResult::~FFTResult() FFTResult::~FFTResult()
{ {
free(_data_avg); free(_data_flat_avg);
free(_data_min); free(_data_flat_min);
free(_data_max); free(_data_flat_max);
free(_data_prop_avg);
free(_data_prop_min);
free(_data_prop_max);
} }
float
FFTResult::avgAt(int x)
{
if (x < 0 || x>= _dataSize)
return 0.0f;
return _data_avg[x];
}
float
FFTResult::minAt(int x)
{
if (x < 0 || x>= _dataSize)
return 0.0f;
return _data_min[x];
}
float
FFTResult::maxAt(int x)
{
if (x < 0 || x>= _dataSize)
return 0.0f;
return _data_max[x];
}

View file

@ -1,22 +1,21 @@
/* /*
Copyright (C) 2006 Paul Davis * Copyright (C) 2006, 2016 Paul Davis
Written by Sampo Savolainen * Written by Sampo Savolainen & Robin Gareus
*
This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or
it under the terms of the GNU General Public License as published by * modify it under the terms of the GNU General Public License
the Free Software Foundation; either version 2 of the License, or * as published by the Free Software Foundation; either version 2
(at your option) any later version. * of the License, or (at your option) any later version.
*
This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. * GNU General Public License for more details.
*
You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
*/
#ifndef __ardour_fft_result_h #ifndef __ardour_fft_result_h
#define __ardour_fft_result_h #define __ardour_fft_result_h
@ -31,47 +30,56 @@ class FFTGraph;
class FFTResult class FFTResult
{ {
public: public:
~FFTResult(); ~FFTResult ();
void analyzeWindow(float *window); void analyzeWindow (float *window);
void finalize(); void finalize ();
int length() const { return _dataSize; } unsigned int length () const { return _dataSize; }
float avgAt(int x); float avgAt (unsigned int x, bool p) const
float maxAt(int x); { return p ? _data_prop_avg[x] : _data_flat_avg[x]; }
float minAt(int x); float maxAt (unsigned int x, bool p) const
{ return p ? _data_prop_max[x] : _data_flat_max[x]; }
float minAt (unsigned int x, bool p) const
{ return p ? _data_prop_min[x] : _data_flat_min[x]; }
float minimum() const { return _minimum; } float minimum (bool p) const
float maximum() const { return _maximum; } { return p ? _min_prop : _min_flat; }
float maximum (bool p) const
{ return p ? _max_prop : _max_flat; }
Gdk::Color get_color() const { return _color; } const Gdk::Color& get_color () const { return _color; }
private: private:
FFTResult(FFTGraph *graph, Gdk::Color color, std::string trackname); FFTResult (FFTGraph *graph, Gdk::Color color, std::string trackname);
friend class FFTGraph;
int _averages; int _averages;
float* _data_avg; float* _data_flat_avg;
float* _data_max; float* _data_flat_max;
float* _data_min; float* _data_flat_min;
float* _data_prop_avg;
float* _data_prop_max;
float* _data_prop_min;
float* _work; unsigned int _windowSize;
unsigned int _dataSize;
int _windowSize; float _min_flat;
int _dataSize; float _max_flat;
float _min_prop;
float _minimum; float _max_prop;
float _maximum;
FFTGraph *_graph; FFTGraph *_graph;
Gdk::Color _color; Gdk::Color _color;
std::string _trackname; std::string _trackname;
friend class FFTGraph; static float power_to_db (float v) { return v > 1e-20 ? 10.0f * log10f (v) : -200.0f; }
}; };
#endif /* __ardour_fft_result_h */ #endif /* __ardour_fft_result_h */