2 Copyright (C) 2006 Paul Davis
3 Written by 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.
22 #include <gtkmm2ext/gtk_ui.h>
23 #include <gtkmm/stock.h>
24 #include <gtkmm/label.h>
25 #include <gtkmm/treemodel.h>
26 #include <gtkmm/treeiter.h>
28 #include <ardour/audioregion.h>
29 #include <ardour/playlist.h>
30 #include <ardour/types.h>
32 #include "analysis_window.h"
35 #include "time_axis_view.h"
36 #include "public_editor.h"
37 #include "selection.h"
38 #include "regionview.h"
42 using namespace ARDOUR;
45 AnalysisWindow::AnalysisWindow()
46 : ArdourDialog(_("analysis window")),
48 source_selection_label (_("Signal source")),
49 source_selection_ranges_rb (_("Selected ranges")),
50 source_selection_regions_rb (_("Selected regions")),
52 display_model_label (_("Display model")),
53 display_model_composite_separate_rb (_("Composite graphs for each track")),
54 display_model_composite_all_tracks_rb (_("Composite graph of all tracks")),
58 track_list_ready = false;
60 // Left side: track list + controls
61 tlmodel = Gtk::ListStore::create(tlcols);
62 track_list.set_model (tlmodel);
63 track_list.append_column(_("Track"), tlcols.trackname);
64 track_list.append_column_editable(_("Visible"), tlcols.visible);
65 track_list.set_headers_visible(true);
66 track_list.set_reorderable(false);
67 track_list.get_selection()->set_mode (Gtk::SELECTION_NONE);
70 Gtk::TreeViewColumn* track_col = track_list.get_column(0);
71 Gtk::CellRendererText* renderer = dynamic_cast<Gtk::CellRendererText*>(track_list.get_column_cell_renderer (0));
73 track_col->add_attribute(renderer->property_foreground_gdk(), tlcols.color);
74 track_col->set_expand(true);
77 tlmodel->signal_row_changed().connect (
78 mem_fun(*this, &AnalysisWindow::track_list_row_changed) );
80 fft_graph.set_analysis_window(this);
82 vbox.pack_start(track_list);
86 vbox.pack_start(source_selection_label, false, false);
89 Gtk::RadioButtonGroup group = source_selection_ranges_rb.get_group();
90 source_selection_regions_rb.set_group(group);
92 source_selection_ranges_rb.set_active();
94 vbox.pack_start (source_selection_ranges_rb, false, false);
95 vbox.pack_start (source_selection_regions_rb, false, false);
97 // "Selected ranges" radio
98 source_selection_ranges_rb.signal_toggled().connect (
99 bind ( mem_fun(*this, &AnalysisWindow::source_selection_changed), &source_selection_ranges_rb));
101 // "Selected regions" radio
102 source_selection_regions_rb.signal_toggled().connect (
103 bind ( mem_fun(*this, &AnalysisWindow::source_selection_changed), &source_selection_regions_rb));
106 vbox.pack_start(hseparator1, false, false);
109 vbox.pack_start(display_model_label, false, false);
111 Gtk::RadioButtonGroup group = display_model_composite_separate_rb.get_group();
112 display_model_composite_all_tracks_rb.set_group (group);
114 display_model_composite_separate_rb.set_active();
116 vbox.pack_start (display_model_composite_separate_rb, false, false);
117 vbox.pack_start (display_model_composite_all_tracks_rb, false, false);
119 // "Composite graphs for all tracks"
120 display_model_composite_separate_rb.signal_toggled().connect (
121 bind ( mem_fun(*this, &AnalysisWindow::display_model_changed), &display_model_composite_separate_rb));
123 // "Composite graph of all tracks"
124 display_model_composite_all_tracks_rb.signal_toggled().connect (
125 bind ( mem_fun(*this, &AnalysisWindow::display_model_changed), &display_model_composite_all_tracks_rb));
128 vbox.pack_start(hseparator2, false, false);
130 refresh_button.set_name("EditorGTKButton");
131 refresh_button.set_label(_("Analyze data"));
133 refresh_button.signal_clicked().connect ( bind ( mem_fun(*this, &AnalysisWindow::analyze_data), &refresh_button));
135 vbox.pack_start(refresh_button, false, false, 10);
138 hbox.pack_start(vbox);
140 // Analysis window on the right
141 fft_graph.ensure_style();
147 // And last we pack the hbox
148 get_vbox()->pack_start(hbox);
150 track_list.show_all();
152 get_vbox()->show_all();
155 AnalysisWindow::~AnalysisWindow()
161 AnalysisWindow::set_rangemode()
163 source_selection_ranges_rb.set_active(true);
167 AnalysisWindow::set_regionmode()
169 source_selection_regions_rb.set_active(true);
173 AnalysisWindow::track_list_row_changed(const Gtk::TreeModel::Path& path, const Gtk::TreeModel::iterator& iter)
175 if (track_list_ready) {
182 AnalysisWindow::clear_tracklist()
184 // Empty track list & free old graphs
185 Gtk::TreeNodeChildren children = track_list.get_model()->children();
187 for (Gtk::TreeIter i = children.begin(); i != children.end(); i++) {
188 Gtk::TreeModel::Row row = *i;
190 FFTResult *delete_me = row[tlcols.graph];
194 // Make sure it's not drawn
195 row[tlcols.graph] = 0;
204 AnalysisWindow::analyze()
206 analyze_data(&refresh_button);
210 AnalysisWindow::analyze_data (Gtk::Button *button)
212 track_list_ready = false;
214 Glib::Mutex::Lock lm (track_list_lock);
216 // Empty track list & free old graphs
219 // first we gather the FFTResults of all tracks
221 Sample *buf = (Sample *) malloc(sizeof(Sample) * fft_graph.windowSize());
222 Sample *mixbuf = (Sample *) malloc(sizeof(Sample) * fft_graph.windowSize());
223 float *gain = (float *) malloc(sizeof(float) * fft_graph.windowSize());
224 char *work = (char *) malloc(sizeof(char) * fft_graph.windowSize());
226 Selection s = PublicEditor::instance().get_selection();
227 TimeSelection ts = s.time;
228 AudioRegionSelection ars = s.audio_regions;
231 for (TrackSelection::iterator i = s.tracks.begin(); i != s.tracks.end(); ++i) {
232 ARDOUR::Playlist *pl = (*i)->playlist();
233 RouteUI *rui = dynamic_cast<RouteUI *>(*i);
235 // Busses don't have playlists, so we need to check that we actually are working with a playlist
239 FFTResult *res = fft_graph.prepareResult(rui->color(), rui->route().name());
242 if (source_selection_ranges_rb.get_active()) {
243 // cerr << "Analyzing ranges on track " << *&rui->route().name() << endl;
245 for (std::list<ARDOUR::AudioRange>::iterator j = ts.begin(); j != ts.end(); ++j) {
247 jack_nframes_t i = 0;
250 while ( i < (*j).length() ) {
251 // TODO: What about stereo+ channels? composite all to one, I guess
253 n = fft_graph.windowSize();
255 if (i + n >= (*j).length() ) {
256 n = (*j).length() - i;
259 n = pl->read(buf, mixbuf, gain, work, (*j).start + i, n);
261 if ( n < fft_graph.windowSize()) {
262 for (int j = n; j < fft_graph.windowSize(); j++) {
267 res->analyzeWindow(buf);
272 } else if (source_selection_regions_rb.get_active()) {
273 // cerr << "Analyzing selected regions on track " << *&rui->route().name() << endl;
275 TimeAxisView *current_axis = (*i);
277 for (std::set<AudioRegionView *>::iterator j = ars.begin(); j != ars.end(); ++j) {
278 // Check that the region really is selected on _this_ track/solo
279 if ( &(*j)->get_time_axis_view() != current_axis)
282 // cerr << " - " << (*j)->region.name() << ": " << (*j)->region.length() << " samples starting at " << (*j)->region.position() << endl;
283 jack_nframes_t i = 0;
286 while ( i < (*j)->region.length() ) {
287 // TODO: What about stereo+ channels? composite all to one, I guess
289 n = fft_graph.windowSize();
290 if (i + n >= (*j)->region.length() ) {
291 n = (*j)->region.length() - i;
294 n = (*j)->region.read_at(buf, mixbuf, gain, work, (*j)->region.position() + i, n);
296 if ( n < fft_graph.windowSize()) {
297 for (int j = n; j < fft_graph.windowSize(); j++) {
302 res->analyzeWindow(buf);
306 // cerr << "Found: " << (*j)->get_item_name() << endl;
314 Gtk::TreeModel::Row newrow = *(tlmodel)->append();
315 newrow[tlcols.trackname] = rui->route().name();
316 newrow[tlcols.visible] = true;
317 newrow[tlcols.color] = rui->color();
318 newrow[tlcols.graph] = res;
326 track_list_ready = true;
333 AnalysisWindow::source_selection_changed (Gtk::RadioButton *button)
335 // We are only interested in activation signals, not deactivation signals
336 if (!button->get_active())
340 cerr << "AnalysisWindow: signal source = ";
342 if (button == &source_selection_ranges_rb) {
343 cerr << "selected ranges" << endl;
345 } else if (button == &source_selection_regions_rb) {
346 cerr << "selected regions" << endl;
349 cerr << "unknown?" << endl;
355 AnalysisWindow::display_model_changed (Gtk::RadioButton *button)
357 // We are only interested in activation signals, not deactivation signals
358 if (!button->get_active())
362 cerr << "AnalysisWindow: display model = ";
364 if (button == &display_model_composite_separate_rb) {
365 cerr << "separate composites of tracks" << endl;
366 } else if (button == &display_model_composite_all_tracks_rb) {
367 cerr << "composite of all tracks" << endl;
369 cerr << "unknown?" << endl;