From 3891c733af596666b4ed98765d36226582ad624c Mon Sep 17 00:00:00 2001 From: Sampo Savolainen Date: Sun, 12 Mar 2006 21:58:52 +0000 Subject: First commit on FFT analysis window. Still some functionality missing, but it's mostly done. git-svn-id: svn://localhost/trunk/ardour2@383 d708f5d6-7413-0410-9779-e7cbd77b26cf --- SConstruct | 3 + gtk2_ardour/SConscript | 4 + gtk2_ardour/analysis_window.cc | 368 ++++++++++++++++++++++++++++++++++++++ gtk2_ardour/analysis_window.h | 114 ++++++++++++ gtk2_ardour/editor.cc | 23 +++ gtk2_ardour/fft_graph.cc | 397 +++++++++++++++++++++++++++++++++++++++++ gtk2_ardour/fft_graph.h | 90 ++++++++++ gtk2_ardour/fft_result.cc | 97 ++++++++++ gtk2_ardour/fft_result.h | 73 ++++++++ 9 files changed, 1169 insertions(+) create mode 100644 gtk2_ardour/analysis_window.cc create mode 100644 gtk2_ardour/analysis_window.h create mode 100644 gtk2_ardour/fft_graph.cc create mode 100644 gtk2_ardour/fft_graph.h create mode 100644 gtk2_ardour/fft_result.cc create mode 100644 gtk2_ardour/fft_result.h diff --git a/SConstruct b/SConstruct index db0fbf6ed2..da4d446ae6 100644 --- a/SConstruct +++ b/SConstruct @@ -361,6 +361,9 @@ libraries['raptor'].ParseConfig('pkg-config --cflags --libs raptor') libraries['samplerate'] = LibraryInfo() libraries['samplerate'].ParseConfig('pkg-config --cflags --libs samplerate') +libraries['fftw3f'] = LibraryInfo() +libraries['fftw3f'].ParseConfig('pkg-config --cflags --libs fftw3f') + libraries['jack'] = LibraryInfo() libraries['jack'].ParseConfig('pkg-config --cflags --libs jack') diff --git a/gtk2_ardour/SConscript b/gtk2_ardour/SConscript index c0fa90c4d5..afcb92b8e8 100644 --- a/gtk2_ardour/SConscript +++ b/gtk2_ardour/SConscript @@ -44,6 +44,7 @@ gtkardour.Merge ([ libraries['xml'], libraries['soundtouch'], libraries['samplerate'], + libraries['fftw3f'], libraries['jack'], libraries['glade2'], libraries['libglademm'] @@ -175,6 +176,9 @@ utils.cc version.cc visual_time_axis.cc waveview.cc +analysis_window.cc +fft_graph.cc +fft_result.cc """) glade_files=glob.glob('glade/*.glade') diff --git a/gtk2_ardour/analysis_window.cc b/gtk2_ardour/analysis_window.cc new file mode 100644 index 0000000000..a26186ab42 --- /dev/null +++ b/gtk2_ardour/analysis_window.cc @@ -0,0 +1,368 @@ +/* + Copyright (C) 2006 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "analysis_window.h" + +#include "route_ui.h" +#include "time_axis_view.h" +#include "public_editor.h" +#include "selection.h" +#include "regionview.h" + +#include "i18n.h" + +using namespace ARDOUR; + +AnalysisWindow::AnalysisWindow() + : ArdourDialog(_("analysis window")), + + fft_graph (2048), + + source_selection_label (_("Signal source")), + source_selection_ranges_rb (_("Selected ranges")), + source_selection_regions_rb (_("Selected regions")), + + display_model_label (_("Display model")), + display_model_composite_separate_rb (_("Composite graphs for each track")), + display_model_composite_all_tracks_rb (_("Composite graph of all tracks")) + +{ + + track_list_ready = false; + + // Left side: track list + controls + tlmodel = Gtk::ListStore::create(tlcols); + track_list.set_model (tlmodel); + track_list.append_column(_("Track"), tlcols.trackname); + track_list.append_column_editable(_("Visible"), tlcols.visible); + track_list.set_headers_visible(true); + track_list.set_reorderable(false); + track_list.get_selection()->set_mode (Gtk::SELECTION_NONE); + + + Gtk::TreeViewColumn* track_col = track_list.get_column(0); + Gtk::CellRendererText* renderer = dynamic_cast(track_list.get_column_cell_renderer (0)); + + track_col->add_attribute(renderer->property_foreground_gdk(), tlcols.color); + track_col->set_expand(true); + + + tlmodel->signal_row_changed().connect ( + mem_fun(*this, &AnalysisWindow::track_list_row_changed) ); + + fft_graph.set_analysis_window(this); + + vbox.pack_start(track_list); + + + // "Signal source" + vbox.pack_start(source_selection_label, false, false); + + { + Gtk::RadioButtonGroup group = source_selection_ranges_rb.get_group(); + source_selection_regions_rb.set_group(group); + + source_selection_ranges_rb.set_active(); + + vbox.pack_start (source_selection_ranges_rb, false, false); + vbox.pack_start (source_selection_regions_rb, false, false); + + // "Selected ranges" radio + source_selection_ranges_rb.signal_toggled().connect ( + bind ( mem_fun(*this, &AnalysisWindow::source_selection_changed), &source_selection_ranges_rb)); + + // "Selected regions" radio + source_selection_regions_rb.signal_toggled().connect ( + bind ( 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 ( + bind ( 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 ( + bind ( mem_fun(*this, &AnalysisWindow::display_model_changed), &display_model_composite_all_tracks_rb)); + } + + vbox.pack_start(hseparator2, false, false); + + refresh_button.set_name("EditorGTKButton"); + refresh_button.set_label(_("Analyze data")); + + refresh_button.signal_clicked().connect ( bind ( mem_fun(*this, &AnalysisWindow::analyze_data), &refresh_button)); + + vbox.pack_start(refresh_button, false, false, 10); + + + hbox.pack_start(vbox); + + // Analysis window on the right + fft_graph.ensure_style(); + + hbox.add(fft_graph); + + + + // And last we pack the hbox + get_vbox()->pack_start(hbox); + + track_list.show_all(); + + get_vbox()->show_all(); +} + +AnalysisWindow::~AnalysisWindow() +{ + +} + +void +AnalysisWindow::set_rangemode() +{ + source_selection_ranges_rb.set_active(true); +} + +void +AnalysisWindow::set_regionmode() +{ + source_selection_regions_rb.set_active(true); +} + +void +AnalysisWindow::track_list_row_changed(const Gtk::TreeModel::Path& path, const Gtk::TreeModel::iterator& iter) +{ + if (track_list_ready) { + fft_graph.redraw(); + } +} + + +void +AnalysisWindow::clear_tracklist() +{ + // Empty track list & free old graphs + Gtk::TreeNodeChildren children = track_list.get_model()->children(); + + for (Gtk::TreeIter i = children.begin(); i != children.end(); i++) { + Gtk::TreeModel::Row row = *i; + + FFTResult *delete_me = row[tlcols.graph]; + if (delete_me == 0) + continue; + + // Make sure it's not drawn + row[tlcols.graph] = 0; + + delete delete_me; + } + + tlmodel->clear(); +} + +void +AnalysisWindow::analyze_data (Gtk::Button *button) +{ + track_list_ready = false; + { + LockMonitor lm (track_list_lock, __LINE__, __FILE__); + + // Empty track list & free old graphs + clear_tracklist(); + + // first we gather the FFTResults of all tracks + + Sample *buf = (Sample *) malloc(sizeof(Sample) * fft_graph.windowSize()); + Sample *mixbuf = (Sample *) malloc(sizeof(Sample) * fft_graph.windowSize()); + float *gain = (float *) malloc(sizeof(float) * fft_graph.windowSize()); + char *work = (char *) malloc(sizeof(char) * fft_graph.windowSize()); + + Selection s = PublicEditor::instance().get_selection(); + TimeSelection ts = s.time; + AudioRegionSelection ars = s.audio_regions; + + + for (TrackSelection::iterator i = s.tracks.begin(); i != s.tracks.end(); ++i) { + ARDOUR::Playlist *pl = (*i)->playlist(); + RouteUI *rui = dynamic_cast(*i); + + // Busses don't have playlists, so we need to check that we actually are working with a playlist + if (!pl || !rui) + continue; + + FFTResult *res = fft_graph.prepareResult(*&rui->color(), *&rui->route().name()); + + // if timeSelection + if (source_selection_ranges_rb.get_active()) { +// cerr << "Analyzing ranges on track " << *&rui->route().name() << endl; + + for (std::list::iterator j = ts.begin(); j != ts.end(); ++j) { + + jack_nframes_t i = 0; + int n; + + while ( i < (*j).length() ) { + // TODO: What about stereo+ channels? composite all to one, I guess + + n = fft_graph.windowSize(); + + if (i + n >= (*j).length() ) { + n = (*j).length() - i; + } + + n = pl->read(buf, mixbuf, gain, work, (*j).start + i, n); + + if ( n < fft_graph.windowSize()) { + for (int j = n; j < fft_graph.windowSize(); j++) { + buf[j] = 0.0; + } + } + + res->analyzeWindow(buf); + + i += n; + } + } + } else if (source_selection_regions_rb.get_active()) { +// cerr << "Analyzing selected regions on track " << *&rui->route().name() << endl; + + TimeAxisView *current_axis = (*i); + + for (std::set::iterator j = ars.begin(); j != ars.end(); ++j) { + // Check that the region really is selected on _this_ track/solo + if ( &(*j)->get_time_axis_view() != current_axis) + continue; + +// cerr << " - " << (*j)->region.name() << ": " << (*j)->region.length() << " samples starting at " << (*j)->region.position() << endl; + jack_nframes_t i = 0; + int n; + + while ( i < (*j)->region.length() ) { + // TODO: What about stereo+ channels? composite all to one, I guess + + n = fft_graph.windowSize(); + if (i + n >= (*j)->region.length() ) { + n = (*j)->region.length() - i; + } + + n = (*j)->region.read_at(buf, mixbuf, gain, work, (*j)->region.position() + i, n); + + if ( n < fft_graph.windowSize()) { + for (int j = n; j < fft_graph.windowSize(); j++) { + buf[j] = 0.0; + } + } + + res->analyzeWindow(buf); + + i += n; + } +// cerr << "Found: " << (*j)->get_item_name() << endl; + + } + + } + res->finalize(); + + + Gtk::TreeModel::Row newrow = *(tlmodel)->append(); + newrow[tlcols.trackname] = rui->route().name(); + newrow[tlcols.visible] = true; + newrow[tlcols.color] = *&rui->color(); + newrow[tlcols.graph] = res; + } + + + free(buf); + free(mixbuf); + free(work); + + track_list_ready = true; + } /* end lock */ + + fft_graph.redraw(); +} + +void +AnalysisWindow::source_selection_changed (Gtk::RadioButton *button) +{ + // We are only interested in activation signals, not deactivation signals + if (!button->get_active()) + return; + + /* + cerr << "AnalysisWindow: signal source = "; + + if (button == &source_selection_ranges_rb) { + cerr << "selected ranges" << endl; + + } else if (button == &source_selection_regions_rb) { + cerr << "selected regions" << endl; + + } else { + cerr << "unknown?" << endl; + } + */ +} + +void +AnalysisWindow::display_model_changed (Gtk::RadioButton *button) +{ + // We are only interested in activation signals, not deactivation signals + if (!button->get_active()) + return; + + /* + cerr << "AnalysisWindow: display model = "; + + if (button == &display_model_composite_separate_rb) { + cerr << "separate composites of tracks" << endl; + } else if (button == &display_model_composite_all_tracks_rb) { + cerr << "composite of all tracks" << endl; + } else { + cerr << "unknown?" << endl; + } + */ +} + + diff --git a/gtk2_ardour/analysis_window.h b/gtk2_ardour/analysis_window.h new file mode 100644 index 0000000000..c4ae7f2252 --- /dev/null +++ b/gtk2_ardour/analysis_window.h @@ -0,0 +1,114 @@ +/* + Copyright (C) 2006 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#ifndef __ardour_analysis_window_h__ +#define __ardour_analysis_window_h__ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + + +#include "ardour_dialog.h" +#include "fft_graph.h" +#include "fft_result.h" + + +class AnalysisWindow : public ArdourDialog +{ + public: + AnalysisWindow (); + ~AnalysisWindow (); + + void set_rangemode(); + void set_regionmode(); + + void track_list_row_changed(const Gtk::TreeModel::Path& path, const Gtk::TreeModel::iterator& iter); + + + private: + + void clear_tracklist(); + + void source_selection_changed (Gtk::RadioButton *); + void display_model_changed (Gtk::RadioButton *); + + void analyze_data (Gtk::Button *); + + struct TrackListColumns : public Gtk::TreeModel::ColumnRecord { + public: + TrackListColumns () { + add (trackname); + add (visible); + add (color); + add (graph); + } + Gtk::TreeModelColumn trackname; + Gtk::TreeModelColumn visible; + Gtk::TreeModelColumn color; + Gtk::TreeModelColumn graph; + }; + + // Packing essentials + Gtk::HBox hbox; + Gtk::VBox vbox; + + // Left side + Glib::RefPtr tlmodel; + TrackListColumns tlcols; + Gtk::TreeView track_list; + + Gtk::Label source_selection_label; + + Gtk::RadioButton source_selection_ranges_rb; + Gtk::RadioButton source_selection_regions_rb; + + Gtk::HSeparator hseparator1; + + Gtk::Label display_model_label; + Gtk::RadioButton display_model_composite_separate_rb; + Gtk::RadioButton display_model_composite_all_tracks_rb; + + Gtk::HSeparator hseparator2; + + Gtk::Button refresh_button; + + // The graph + FFTGraph fft_graph; + + bool track_list_ready; + PBD::Lock track_list_lock; + + friend class FFTGraph; +}; + +#endif // __ardour_analysis_window_h + diff --git a/gtk2_ardour/editor.cc b/gtk2_ardour/editor.cc index 1012b67211..55809e6cc5 100644 --- a/gtk2_ardour/editor.cc +++ b/gtk2_ardour/editor.cc @@ -67,6 +67,7 @@ #include "canvas_impl.h" #include "actions.h" #include "gui_thread.h" +#include "analysis_window.h" #include "i18n.h" @@ -251,6 +252,7 @@ Editor::Editor (AudioEngine& eng) canvas_height = 0; autoscroll_timeout_tag = -1; interthread_progress_window = 0; + analysis_window = 0; current_interthread_info = 0; _show_measures = true; _show_waveforms = true; @@ -1168,6 +1170,9 @@ Editor::connect_to_session (Session *t) _playlist_selector->set_session (session); nudge_clock.set_session (session); + if (analysis_window != 0) + analysis_window->set_session (session); + switch (session->get_edit_mode()) { case Splice: edit_mode_selector.set_active_text (edit_mode_strings[splice_index]); @@ -1584,6 +1589,21 @@ Editor::build_track_crossfade_context_menu (jack_nframes_t frame) return &track_crossfade_context_menu; } +void +Editor::show_analysis_window() +{ + if (analysis_window == 0) { + analysis_window = new AnalysisWindow(); + + if (session != 0) + analysis_window->set_session(session); + + analysis_window->show_all(); + } + + analysis_window->present(); +} + Menu* Editor::build_track_selection_context_menu (jack_nframes_t ignored) { @@ -1792,8 +1812,11 @@ Editor::add_selection_context_items (Menu_Helpers::MenuList& edit_items) items.push_back (MenuElem (_("Play range"), mem_fun(*this, &Editor::play_selection))); items.push_back (MenuElem (_("Loop range"), mem_fun(*this, &Editor::set_route_loop_selection))); items.push_back (SeparatorElem()); + items.push_back (MenuElem (_("Analyze range"), mem_fun(*this, &Editor::show_analysis_window))); + items.push_back (SeparatorElem()); items.push_back (MenuElem (_("Separate range to track"), mem_fun(*this, &Editor::separate_region_from_selection))); items.push_back (MenuElem (_("Separate range to region list"), mem_fun(*this, &Editor::new_region_from_selection))); + items.push_back (SeparatorElem()); items.push_back (MenuElem (_("Select all in range"), mem_fun(*this, &Editor::select_all_selectables_using_time_selection))); items.push_back (SeparatorElem()); diff --git a/gtk2_ardour/fft_graph.cc b/gtk2_ardour/fft_graph.cc new file mode 100644 index 0000000000..0989247e09 --- /dev/null +++ b/gtk2_ardour/fft_graph.cc @@ -0,0 +1,397 @@ +/* + Copyright (C) 2006 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include + +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include + +#include "fft_graph.h" +#include "analysis_window.h" + +using namespace std; +using namespace Gtk; +using namespace Gdk; + +FFTGraph::FFTGraph(int windowSize) +{ + _logScale = 0; + + _in = 0; + _out = 0; + _hanning = 0; + _logScale = 0; + + _a_window = 0; + + setWindowSize(windowSize); +} + +void +FFTGraph::setWindowSize(int windowSize) +{ + if (_a_window) { + LockMonitor lm (_a_window->track_list_lock, __LINE__, __FILE__); + setWindowSize_internal(windowSize); + } else { + setWindowSize_internal(windowSize); + } +} + +void +FFTGraph::setWindowSize_internal(int windowSize) +{ + // remove old tracklist & graphs + if (_a_window) { + _a_window->clear_tracklist(); + } + + _windowSize = windowSize; + _dataSize = windowSize / 2; + if (_in != 0) { + fftwf_destroy_plan(_plan); + free(_in); + _in = 0; + } + + if (_out != 0) { + free(_out); + _out = 0; + } + + if (_hanning != 0) { + free(_hanning); + _hanning = 0; + } + + if (_logScale != 0) { + free(_logScale); + _logScale = 0; + } + + // When destroying, window size is set to zero to free up memory + if (windowSize == 0) + return; + + // FFT input & output buffers + _in = (float *) fftwf_malloc(sizeof(float) * _windowSize); + _out = (float *) fftwf_malloc(sizeof(float) * _windowSize); + + // Hanning window + _hanning = (float *) malloc(sizeof(float) * _windowSize); + + + // normalize the window + double sum = 0.0; + + for (int i=0; i < _windowSize; i++) { + _hanning[i]=0.81f * ( 0.5f - (0.5f * (float) cos(2.0f * M_PI * (float)i / (float)(_windowSize)))); + sum += _hanning[i]; + } + + double isum = 1.0 / sum; + + for (int i=0; i < _windowSize; i++) { + _hanning[i] *= isum; + } + + _logScale = (int *) malloc(sizeof(int) * _dataSize); + for (int i = 0; i < _dataSize; i++) { + _logScale[i] = (int)floor(log10( 1.0 + i * 9.0 / (double)_dataSize) * (double)scaleWidth); + } + _plan = fftwf_plan_r2r_1d(_windowSize, _in, _out, FFTW_R2HC, FFTW_ESTIMATE); +} + +FFTGraph::~FFTGraph() +{ + // This will free everything + setWindowSize(0); +} + +bool +FFTGraph::on_expose_event (GdkEventExpose* event) +{ + redraw(); + return true; +} + +FFTResult * +FFTGraph::prepareResult(Gdk::Color color, string trackname) +{ + FFTResult *res = new FFTResult(this, color, trackname); + + return res; +} + +void +FFTGraph::analyze(float *window, float *composite) +{ + int i; + // Copy the data and apply the hanning window + for (i = 0; i < _windowSize; i++) { + _in[i] = window[ i ] * _hanning[ i ]; + } + + fftwf_execute(_plan); + + composite[0] += (_out[0] * _out[0]); + + for (i=1; i < _dataSize - 1; i++) { // TODO: check with Jesse whether this is really correct + composite[i] += (_out[i] * _out[i]) + (_out[_windowSize-i] * _out[_windowSize-i]); + } +} + +void +FFTGraph::set_analysis_window(AnalysisWindow *a_window) +{ + _a_window = a_window; +} + +void +FFTGraph::draw_scales(Glib::RefPtr window) +{ + + Glib::RefPtr style = get_style(); + Glib::RefPtr black = style->get_black_gc(); + Glib::RefPtr white = style->get_white_gc(); + + window->draw_rectangle(black, true, 0, 0, width, height); + + /** + * 4 5 + * _ _ + * | | + * 1 | | 2 + * |________| + * 3 + **/ + + // Line 1 + window->draw_line(white, h_margin, v_margin, h_margin, height - v_margin ); + + // Line 2 + window->draw_line(white, width - h_margin, v_margin, width - h_margin, height - v_margin ); + + // Line 3 + window->draw_line(white, h_margin, height - v_margin, width - h_margin, height - v_margin ); + +#define DB_METRIC_LENGTH 8 + // Line 5 + window->draw_line(white, h_margin - DB_METRIC_LENGTH, v_margin, h_margin, v_margin ); + + // Line 6 + window->draw_line(white, width - h_margin, v_margin, width - h_margin + DB_METRIC_LENGTH, v_margin ); + + + if (graph_gc == 0) { + graph_gc = GC::create( get_window() ); + } + + Color grey; + + grey.set_rgb_p(0.2, 0.2, 0.2); + + graph_gc->set_rgb_fg_color( grey ); + + if (layout == 0) { + layout = create_pango_layout (""); + layout->set_font_description (get_style()->get_font()); + } + + // Draw logscale + int logscale_pos = 0; + int position_on_scale; + for (int x = 1; x < 8; x++) { + position_on_scale = (int)floor( (double)scaleWidth*(double)x/8.0); + + while (_logScale[logscale_pos] < position_on_scale) + logscale_pos++; + + int coord = v_margin + 1.0 + position_on_scale; + + int SR = 44100; + + int rate_at_pos = (double)(SR/2) * (double)logscale_pos / (double)_dataSize; + + char buf[32]; + snprintf(buf,32,"%dhz",rate_at_pos); + + std::string label = buf; + + layout->set_text(label); + + window->draw_line(graph_gc, coord, v_margin, coord, height - v_margin); + + int layoutWidth; + int layoutHeight; + layout->get_pixel_size(layoutWidth,layoutHeight); + + + window->draw_layout(white, coord - layoutWidth / 2, v_margin / 2, layout); + + } + +} + +void +FFTGraph::redraw() +{ + LockMonitor lm (_a_window->track_list_lock, __LINE__, __FILE__ ); + + draw_scales(get_window()); + + if (_a_window == 0) + return; + + if (!_a_window->track_list_ready) + return; + + + // Find "session wide" min & max + float min = 1000000000000.0; + float max = -1000000000000.0; + + TreeNodeChildren track_rows = _a_window->track_list.get_model()->children(); + + for (TreeIter i = track_rows.begin(); i != track_rows.end(); i++) { + + TreeModel::Row row = *i; + FFTResult *res = row[_a_window->tlcols.graph]; + + // disregard fft analysis from empty signals + if (res->minimum() == res->maximum()) { + continue; + } + + if ( res->minimum() < min) { + min = res->minimum(); + } + + if ( res->maximum() > max) { + max = res->maximum(); + } + } + + int graph_height = height - 2 * h_margin; + + if (graph_gc == 0) { + graph_gc = GC::create( get_window() ); + } + + + double pixels_per_db = (double)graph_height / (double)(max - min); + + + for (TreeIter i = track_rows.begin(); i != track_rows.end(); i++) { + + TreeModel::Row row = *i; + + // don't show graphs for tracks which are deselected + if (!row[_a_window->tlcols.visible]) { + continue; + } + + FFTResult *res = row[_a_window->tlcols.graph]; + + // don't show graphs for empty signals + if (res->minimum() == res->maximum()) { + continue; + } + + std::string name = row[_a_window->tlcols.trackname]; + + // Set color from track + graph_gc->set_rgb_fg_color( res->get_color() ); + + float mpp = -1000000.0; + int prevx = 0; + float prevSample = min; + + for (int x = 0; x < res->length() - 1; x++) { + + if (res->sampleAt(x) > mpp) + mpp = res->sampleAt(x); + + // 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; + } + + get_window()->draw_line( + graph_gc, + v_margin + 1 + prevx, + graph_height - (int)floor( (prevSample - min) * pixels_per_db) + h_margin - 1, + v_margin + 1 + _logScale[x], + graph_height - (int)floor( (mpp - min) * pixels_per_db) + h_margin - 1); + + prevx = _logScale[x]; + prevSample = mpp; + + + mpp = -1000000.0; + + } + } + +} + +void +FFTGraph::on_size_request(Gtk::Requisition* requisition) +{ + width = scaleWidth + h_margin * 2; + height = scaleHeight + 2 + v_margin * 2; + + if (_logScale != 0) { + free(_logScale); + } + + _logScale = (int *) malloc(sizeof(int) * _dataSize); + //cerr << "LogScale: " << endl; + for (int i = 0; i < _dataSize; i++) { + _logScale[i] = (int)floor(log10( 1.0 + i * 9.0 / (double)_dataSize) * (double)scaleWidth); + //cerr << i << ":\t" << _logScale[i] << endl; + } + + requisition->width = width;; + requisition->height = height; +} + +void +FFTGraph::on_size_allocate(Gtk::Allocation alloc) +{ + width = alloc.get_width(); + height = alloc.get_height(); + + DrawingArea::on_size_allocate (alloc); + +} + diff --git a/gtk2_ardour/fft_graph.h b/gtk2_ardour/fft_graph.h new file mode 100644 index 0000000000..80c78180a6 --- /dev/null +++ b/gtk2_ardour/fft_graph.h @@ -0,0 +1,90 @@ +/* + Copyright (C) 2006 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#ifndef __ardour_fft_graph_h +#define __ardour_fft_graph_h + +#include +#include + +#include +#include +#include + +#include + +#include + +#include "fft_result.h" + +class AnalysisWindow; + +class FFTGraph : public Gtk::DrawingArea +{ + public: + + FFTGraph(int windowSize); + ~FFTGraph(); + + void set_analysis_window(AnalysisWindow *a_window); + + int windowSize() const { return _windowSize; } + void setWindowSize(int windowSize); + + void redraw(); + bool on_expose_event (GdkEventExpose* event); + + void on_size_request(Gtk::Requisition* requisition); + void on_size_allocate(Gtk::Allocation alloc); + FFTResult *prepareResult(Gdk::Color color, std::string trackname); + + private: + + void setWindowSize_internal(int windowSize); + + void draw_scales(Glib::RefPtr window); + + static const int scaleWidth = 512; + static const int scaleHeight = 420; + + static const int h_margin = 20; + static const int v_margin = 20; + + int width; + int height; + + void analyze(float *window, float *composite); + int _windowSize; + int _dataSize; + + Glib::RefPtr layout; + Glib::RefPtr graph_gc; + AnalysisWindow *_a_window; + + fftwf_plan _plan; + + float *_out; + float *_in; + float *_hanning; + int *_logScale; + + friend class FFTResult; +}; + +#endif /* __ardour_fft_graph_h */ diff --git a/gtk2_ardour/fft_result.cc b/gtk2_ardour/fft_result.cc new file mode 100644 index 0000000000..9a55b59cb5 --- /dev/null +++ b/gtk2_ardour/fft_result.cc @@ -0,0 +1,97 @@ +/* + Copyright (C) 2006 Paul Davis + Written by Sampo Savolainen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include +#include +#include +#include +#include + +#include + +using namespace std; + +FFTResult::FFTResult(FFTGraph *graph, Gdk::Color color, string trackname) +{ + _graph = graph; + + _windowSize = _graph->windowSize(); + _dataSize = _windowSize / 2; + + _averages = 0; + + _data = (float *) malloc(sizeof(float) * _dataSize); + memset(_data,0,sizeof(float) * _dataSize); + + _color = color; + _trackname = trackname; +} + +void +FFTResult::analyzeWindow(float *window) +{ + _graph->analyze(window, _data); + _averages++; +} + +void +FFTResult::finalize() +{ + if (_averages == 0) { + _minimum = 0.0; + _maximum = 0.0; + return; + } + + // Average & scale + for (int i = 0; i < _dataSize; i++) { + _data[i] /= _averages; + _data[i] = 10.0f * log10f(_data[i]); + } + + // find min & max + _minimum = _maximum = _data[0]; + + for (int i = 1; i < _dataSize; i++) { + if (_data[i] < _minimum && !isinf(_data[i])) { + _minimum = _data[i]; + } else if (_data[i] > _maximum && !isinf(_data[i])) { + _maximum = _data[i]; + } + } + + _averages = 0; +} + +FFTResult::~FFTResult() +{ + free(_data); +} + + +float +FFTResult::sampleAt(int x) +{ + if (x < 0 || x>= _dataSize) + return 0.0f; + + return _data[x]; +} + diff --git a/gtk2_ardour/fft_result.h b/gtk2_ardour/fft_result.h new file mode 100644 index 0000000000..c8f17dc01c --- /dev/null +++ b/gtk2_ardour/fft_result.h @@ -0,0 +1,73 @@ +/* + Copyright (C) 2006 Paul Davis + Written by Sampo Savolainen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#ifndef __ardour_fft_result_h +#define __ardour_fft_result_h + +#include +#include + +#include + +#include + +class FFTGraph; + +class FFTResult +{ + public: + + ~FFTResult(); + + void analyzeWindow(float *window); + void finalize(); + + const int length() { return _dataSize; } + + float sampleAt(int x); + + const float minimum() { return _minimum; } + const float maximum() { return _maximum; } + + const Gdk::Color get_color() { return _color; } + + private: + FFTResult(FFTGraph *graph, Gdk::Color color, std::string trackname); + + int _averages; + + float* _data; + float* _work; + + int _windowSize; + int _dataSize; + + float _minimum; + float _maximum; + + FFTGraph *_graph; + + Gdk::Color _color; + std::string _trackname; + + friend class FFTGraph; +}; + +#endif /* __ardour_fft_result_h */ -- cgit v1.2.3