Strip trailing whitespace and fix other whitespace errors (e.g. space/tab mixing...
[ardour.git] / gtk2_ardour / analysis_window.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3     Written by Sampo Savolainen
4
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.
9
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.
14
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.
18
19 */
20
21 #include <gtkmm2ext/gtk_ui.h>
22 #include <gtkmm/stock.h>
23 #include <gtkmm/label.h>
24 #include <gtkmm/treemodel.h>
25 #include <gtkmm/treeiter.h>
26
27 #include "ardour/audioregion.h"
28 #include "ardour/audioplaylist.h"
29 #include "ardour/types.h"
30
31 #include "analysis_window.h"
32
33 #include "route_ui.h"
34 #include "time_axis_view.h"
35 #include "public_editor.h"
36 #include "selection.h"
37 #include "audio_region_view.h"
38
39 #include "i18n.h"
40
41 using namespace ARDOUR;
42 using namespace PBD;
43
44 AnalysisWindow::AnalysisWindow() :
45
46           source_selection_label       (_("Signal source")),
47           source_selection_ranges_rb   (_("Selected ranges")),
48           source_selection_regions_rb  (_("Selected regions")),
49
50           display_model_label                   (_("Display model")),
51           display_model_composite_separate_rb   (_("Composite graphs for each track")),
52           display_model_composite_all_tracks_rb (_("Composite graph of all tracks")),
53
54           show_minmax_button     (_("Show frequency power range")),
55           show_normalized_button (_("Normalize values")),
56
57           fft_graph (16384)
58 {
59         set_name(_("FFT analysis window"));
60         set_title(_("FFT analysis window"));
61
62         track_list_ready = false;
63
64         // Left side: track list + controls
65         tlmodel = Gtk::ListStore::create(tlcols);
66         track_list.set_model (tlmodel);
67         track_list.append_column(_("Track"), tlcols.trackname);
68         track_list.append_column_editable(_("Show"), tlcols.visible);
69         track_list.set_headers_visible(true);
70         track_list.set_reorderable(false);
71         track_list.get_selection()->set_mode (Gtk::SELECTION_NONE);
72
73
74         Gtk::TreeViewColumn* track_col = track_list.get_column(0);
75         Gtk::CellRendererText* renderer = dynamic_cast<Gtk::CellRendererText*>(track_list.get_column_cell_renderer (0));
76
77         track_col->add_attribute(renderer->property_foreground_gdk(), tlcols.color);
78         track_col->set_expand(true);
79
80
81         tlmodel->signal_row_changed().connect (
82                         mem_fun(*this, &AnalysisWindow::track_list_row_changed) );
83
84         fft_graph.set_analysis_window(this);
85
86         vbox.pack_start(track_list);
87
88
89         // "Signal source"
90         vbox.pack_start(source_selection_label, false, false);
91
92         {
93                 Gtk::RadioButtonGroup group = source_selection_ranges_rb.get_group();
94                 source_selection_regions_rb.set_group(group);
95
96                 source_selection_ranges_rb.set_active();
97
98                 vbox.pack_start (source_selection_ranges_rb,  false, false);
99                 vbox.pack_start (source_selection_regions_rb, false, false);
100
101                 // "Selected ranges" radio
102                 source_selection_ranges_rb.signal_toggled().connect (
103                                 bind ( mem_fun(*this, &AnalysisWindow::source_selection_changed), &source_selection_ranges_rb));
104
105                 // "Selected regions" radio
106                 source_selection_regions_rb.signal_toggled().connect (
107                                 bind ( mem_fun(*this, &AnalysisWindow::source_selection_changed), &source_selection_regions_rb));
108         }
109
110         vbox.pack_start(hseparator1, false, false);
111
112         // "Display model"
113         vbox.pack_start(display_model_label, false, false);
114         {
115                 Gtk::RadioButtonGroup group = display_model_composite_separate_rb.get_group();
116                 display_model_composite_all_tracks_rb.set_group (group);
117
118                 display_model_composite_separate_rb.set_active();
119
120                 vbox.pack_start (display_model_composite_separate_rb,   false, false);
121                 vbox.pack_start (display_model_composite_all_tracks_rb, false, false);
122
123                 // "Composite graphs for all tracks"
124                 display_model_composite_separate_rb.signal_toggled().connect (
125                                 bind ( mem_fun(*this, &AnalysisWindow::display_model_changed), &display_model_composite_separate_rb));
126
127                 // "Composite graph of all tracks"
128                 display_model_composite_all_tracks_rb.signal_toggled().connect (
129                                 bind ( mem_fun(*this, &AnalysisWindow::display_model_changed), &display_model_composite_all_tracks_rb));
130         }
131
132         // Analyze button
133
134         refresh_button.set_name("EditorGTKButton");
135         refresh_button.set_label(_("Re-analyze data"));
136
137         refresh_button.signal_clicked().connect ( bind ( mem_fun(*this, &AnalysisWindow::analyze_data), &refresh_button));
138
139         vbox.pack_start(refresh_button, false, false, 10);
140
141
142         // Feature checkboxes
143
144         // minmax
145         show_minmax_button.signal_toggled().connect( mem_fun(*this, &AnalysisWindow::show_minmax_changed));
146         vbox.pack_start(show_minmax_button, false, false);
147
148         // normalize
149         show_normalized_button.signal_toggled().connect( mem_fun(*this, &AnalysisWindow::show_normalized_changed));
150         vbox.pack_start(show_normalized_button, false, false);
151
152
153
154
155
156         hbox.pack_start(vbox, Gtk::PACK_SHRINK);
157
158         // Analysis window on the right
159         fft_graph.ensure_style();
160
161         hbox.add(fft_graph);
162
163
164
165         // And last we pack the hbox
166         add(hbox);
167         show_all();
168         track_list.show_all();
169 }
170
171 AnalysisWindow::~AnalysisWindow()
172 {
173
174 }
175
176 void
177 AnalysisWindow::show_minmax_changed()
178 {
179         fft_graph.set_show_minmax(show_minmax_button.get_active());
180 }
181
182 void
183 AnalysisWindow::show_normalized_changed()
184 {
185         fft_graph.set_show_normalized(show_normalized_button.get_active());
186 }
187
188 void
189 AnalysisWindow::set_rangemode()
190 {
191         source_selection_ranges_rb.set_active(true);
192 }
193
194 void
195 AnalysisWindow::set_regionmode()
196 {
197         source_selection_regions_rb.set_active(true);
198 }
199
200 void
201 AnalysisWindow::track_list_row_changed(const Gtk::TreeModel::Path& /*path*/, const Gtk::TreeModel::iterator& /*iter*/)
202 {
203         if (track_list_ready) {
204                 fft_graph.redraw();
205         }
206 }
207
208
209 void
210 AnalysisWindow::clear_tracklist()
211 {
212         // Empty track list & free old graphs
213         Gtk::TreeNodeChildren children = track_list.get_model()->children();
214
215         for (Gtk::TreeIter i = children.begin(); i != children.end(); i++) {
216                 Gtk::TreeModel::Row row = *i;
217
218                 FFTResult *delete_me = row[tlcols.graph];
219                 if (delete_me == 0)
220                         continue;
221
222                 // Make sure it's not drawn
223                 row[tlcols.graph] = 0;
224
225                 delete delete_me;
226         }
227
228         tlmodel->clear();
229 }
230
231 void
232 AnalysisWindow::analyze()
233 {
234         analyze_data(&refresh_button);
235 }
236
237 void
238 AnalysisWindow::analyze_data (Gtk::Button */*button*/)
239 {
240         track_list_ready = false;
241         {
242                 Glib::Mutex::Lock lm  (track_list_lock);
243
244                 // Empty track list & free old graphs
245                 clear_tracklist();
246
247                 // first we gather the FFTResults of all tracks
248
249                 Sample *buf    = (Sample *) malloc(sizeof(Sample) * fft_graph.windowSize());
250                 Sample *mixbuf = (Sample *) malloc(sizeof(Sample) * fft_graph.windowSize());
251                 float  *gain   = (float *)  malloc(sizeof(float) * fft_graph.windowSize());
252
253                 Selection s = PublicEditor::instance().get_selection();
254                 TimeSelection ts = s.time;
255                 RegionSelection ars = s.regions;
256
257                 for (TrackSelection::iterator i = s.tracks.begin(); i != s.tracks.end(); ++i) {
258                         boost::shared_ptr<AudioPlaylist> pl
259                                 = boost::dynamic_pointer_cast<AudioPlaylist>((*i)->playlist());
260
261                         if (!pl)
262                                 continue;
263
264                         RouteUI *rui = dynamic_cast<RouteUI *>(*i);
265                         int n_inputs = rui->route()->n_inputs().n_audio(); // FFT is audio only
266
267                         // Busses don't have playlists, so we need to check that we actually are working with a playlist
268                         if (!pl || !rui)
269                                 continue;
270
271                         FFTResult *res = fft_graph.prepareResult(rui->color(), rui->route()->name());
272
273                         // if timeSelection
274                         if (source_selection_ranges_rb.get_active()) {
275 //                              cerr << "Analyzing ranges on track " << *&rui->route().name() << endl;
276
277                                 for (std::list<AudioRange>::iterator j = ts.begin(); j != ts.end(); ++j) {
278
279                                         int n;
280                                         for (int channel = 0; channel < n_inputs; channel++) {
281                                                 nframes_t x = 0;
282
283                                                 while ( x < (*j).length() ) {
284                                                         // TODO: What about stereo+ channels? composite all to one, I guess
285
286                                                         n = fft_graph.windowSize();
287
288                                                         if (x + n >= (*j).length() ) {
289                                                                 n = (*j).length() - x;
290                                                         }
291
292                                                         n = pl->read(buf, mixbuf, gain, (*j).start + x, n, channel);
293
294                                                         if ( n < fft_graph.windowSize()) {
295                                                                 for (int j = n; j < fft_graph.windowSize(); j++) {
296                                                                         buf[j] = 0.0;
297                                                                 }
298                                                         }
299
300                                                         res->analyzeWindow(buf);
301
302                                                         x += n;
303                                                 }
304                                         }
305                                 }
306                         } else if (source_selection_regions_rb.get_active()) {
307 //                              cerr << "Analyzing selected regions on track " << *&rui->route().name() << endl;
308
309                                 TimeAxisView *current_axis = (*i);
310
311                                 for (RegionSelection::iterator j = ars.begin(); j != ars.end(); ++j) {
312                                         // Check that the region is actually audio (so we can analyze it)
313                                         AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*j);
314                                         if (!arv)
315                                                 continue;
316
317                                         // Check that the region really is selected on _this_ track/solo
318                                         if ( &arv->get_time_axis_view() != current_axis)
319                                                 continue;
320
321 //                                      cerr << " - " << (*j)->region().name() << ": " << (*j)->region().length() << " samples starting at " << (*j)->region().position() << endl;
322                                         int n;
323                                         for (int channel = 0; channel < n_inputs; channel++) {
324
325                                                 nframes_t x = 0;
326
327                                                 nframes_t length = arv->region()->length();
328
329                                                 while ( x < length ) {
330                                                         // TODO: What about stereo+ channels? composite all to one, I guess
331
332                                                         n = fft_graph.windowSize();
333                                                         if (x + n >= length ) {
334                                                                 n = length - x;
335                                                         }
336
337                                                         n = arv->audio_region()->read_at(buf, mixbuf, gain, arv->region()->position() + x, n, channel);
338
339                                                         if (n == 0)
340                                                                 break;
341
342                                                         if ( n < fft_graph.windowSize()) {
343                                                                 for (int j = n; j < fft_graph.windowSize(); j++) {
344                                                                         buf[j] = 0.0;
345                                                                 }
346                                                         }
347
348                                                         res->analyzeWindow(buf);
349
350                                                         x += n;
351                                                 }
352                                         }
353 //                                      cerr << "Found: " << (*j)->get_item_name() << endl;
354
355                                 }
356
357                         }
358                         res->finalize();
359
360
361                         Gtk::TreeModel::Row newrow = *(tlmodel)->append();
362                         newrow[tlcols.trackname]   = rui->route()->name();
363                         newrow[tlcols.visible]     = true;
364                         newrow[tlcols.color]       = rui->color();
365                         newrow[tlcols.graph]       = res;
366                 }
367
368
369                 free(buf);
370                 free(mixbuf);
371
372                 track_list_ready = true;
373         } /* end lock */
374
375         fft_graph.redraw();
376 }
377
378 void
379 AnalysisWindow::source_selection_changed (Gtk::RadioButton *button)
380 {
381         // We are only interested in activation signals, not deactivation signals
382         if (!button->get_active())
383                 return;
384
385         /*
386         cerr << "AnalysisWindow: signal source = ";
387
388         if (button == &source_selection_ranges_rb) {
389                 cerr << "selected ranges" << endl;
390
391         } else if (button == &source_selection_regions_rb) {
392                 cerr << "selected regions" << endl;
393
394         } else {
395                 cerr << "unknown?" << endl;
396         }
397         */
398 }
399
400 void
401 AnalysisWindow::display_model_changed (Gtk::RadioButton *button)
402 {
403         // We are only interested in activation signals, not deactivation signals
404         if (!button->get_active())
405                 return;
406
407         /*
408         cerr << "AnalysisWindow: display model = ";
409
410         if (button == &display_model_composite_separate_rb) {
411                 cerr << "separate composites of tracks" << endl;
412         } else if (button == &display_model_composite_all_tracks_rb) {
413                 cerr << "composite of all tracks" << endl;
414         } else {
415                 cerr << "unknown?" << endl;
416         }
417         */
418 }
419
420