remove all lines to avoid recompiles after commits
[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         : ArdourDialog(_("analysis window")),
46         
47           source_selection_label       (_("Signal source")),
48           source_selection_ranges_rb   (_("Selected ranges")),
49           source_selection_regions_rb  (_("Selected regions")),
50         
51           display_model_label                   (_("Display model")),
52           display_model_composite_separate_rb   (_("Composite graphs for each track")),
53           display_model_composite_all_tracks_rb (_("Composite graph of all tracks")),
54
55           fft_graph (2048)
56 {
57         track_list_ready = false;
58         
59         // Left side: track list + controls
60         tlmodel = Gtk::ListStore::create(tlcols);
61         track_list.set_model (tlmodel);
62         track_list.append_column(_("Track"), tlcols.trackname);
63         track_list.append_column_editable(_("Show"), tlcols.visible);
64         track_list.set_headers_visible(true);
65         track_list.set_reorderable(false);
66         track_list.get_selection()->set_mode (Gtk::SELECTION_NONE);
67
68
69         Gtk::TreeViewColumn* track_col = track_list.get_column(0);
70         Gtk::CellRendererText* renderer = dynamic_cast<Gtk::CellRendererText*>(track_list.get_column_cell_renderer (0));
71         
72         track_col->add_attribute(renderer->property_foreground_gdk(), tlcols.color);
73         track_col->set_expand(true);
74
75
76         tlmodel->signal_row_changed().connect (
77                         mem_fun(*this, &AnalysisWindow::track_list_row_changed) );
78         
79         fft_graph.set_analysis_window(this);
80                 
81         vbox.pack_start(track_list);
82
83
84         // "Signal source"
85         vbox.pack_start(source_selection_label, false, false);
86
87         {
88                 Gtk::RadioButtonGroup group = source_selection_ranges_rb.get_group();
89                 source_selection_regions_rb.set_group(group);
90
91                 source_selection_ranges_rb.set_active();
92                 
93                 vbox.pack_start (source_selection_ranges_rb,  false, false);
94                 vbox.pack_start (source_selection_regions_rb, false, false);
95                 
96                 // "Selected ranges" radio
97                 source_selection_ranges_rb.signal_toggled().connect (
98                                 bind ( mem_fun(*this, &AnalysisWindow::source_selection_changed), &source_selection_ranges_rb));
99
100                 // "Selected regions" radio
101                 source_selection_regions_rb.signal_toggled().connect (
102                                 bind ( mem_fun(*this, &AnalysisWindow::source_selection_changed), &source_selection_regions_rb));
103         }
104         
105         vbox.pack_start(hseparator1, false, false);
106         
107         // "Display model"
108         vbox.pack_start(display_model_label, false, false);
109         {
110                 Gtk::RadioButtonGroup group = display_model_composite_separate_rb.get_group();
111                 display_model_composite_all_tracks_rb.set_group (group);
112                 
113                 display_model_composite_separate_rb.set_active();
114                 
115                 vbox.pack_start (display_model_composite_separate_rb,   false, false);
116                 vbox.pack_start (display_model_composite_all_tracks_rb, false, false);
117
118                 // "Composite graphs for all tracks"
119                 display_model_composite_separate_rb.signal_toggled().connect (
120                                 bind ( mem_fun(*this, &AnalysisWindow::display_model_changed), &display_model_composite_separate_rb));
121                 
122                 // "Composite graph of all tracks"
123                 display_model_composite_all_tracks_rb.signal_toggled().connect (
124                                 bind ( mem_fun(*this, &AnalysisWindow::display_model_changed), &display_model_composite_all_tracks_rb));
125         }
126
127         vbox.pack_start(hseparator2, false, false);
128
129         refresh_button.set_name("EditorGTKButton");
130         refresh_button.set_label(_("Analyze data"));
131
132         refresh_button.signal_clicked().connect ( bind ( mem_fun(*this, &AnalysisWindow::analyze_data), &refresh_button)); 
133
134         vbox.pack_start(refresh_button, false, false, 10);
135         
136         
137         hbox.pack_start(vbox);
138         
139         // Analysis window on the right
140         fft_graph.ensure_style();
141
142         hbox.add(fft_graph);
143         
144         
145
146         // And last we pack the hbox
147         get_vbox()->pack_start(hbox);
148
149         track_list.show_all();
150
151         get_vbox()->show_all();
152 }
153
154 AnalysisWindow::~AnalysisWindow()
155 {
156
157 }
158
159 void
160 AnalysisWindow::set_rangemode()
161 {
162         source_selection_ranges_rb.set_active(true);
163 }
164
165 void
166 AnalysisWindow::set_regionmode()
167 {
168         source_selection_regions_rb.set_active(true);
169 }
170
171 void 
172 AnalysisWindow::track_list_row_changed(const Gtk::TreeModel::Path& path, const Gtk::TreeModel::iterator& iter)
173 {
174         if (track_list_ready) {
175                 fft_graph.redraw();
176         }
177 }
178
179
180 void
181 AnalysisWindow::clear_tracklist()
182 {
183         // Empty track list & free old graphs
184         Gtk::TreeNodeChildren children = track_list.get_model()->children();
185         
186         for (Gtk::TreeIter i = children.begin(); i != children.end(); i++) {
187                 Gtk::TreeModel::Row row = *i;
188
189                 FFTResult *delete_me = row[tlcols.graph];
190                 if (delete_me == 0)
191                         continue;
192
193                 // Make sure it's not drawn
194                 row[tlcols.graph] = 0;
195                 
196                 delete delete_me;
197         }
198                 
199         tlmodel->clear();
200 }
201
202 void
203 AnalysisWindow::analyze()
204 {
205         analyze_data(&refresh_button);
206 }
207
208 void
209 AnalysisWindow::analyze_data (Gtk::Button *button)
210 {
211         track_list_ready = false;
212         {
213                 Glib::Mutex::Lock lm  (track_list_lock);
214
215                 // Empty track list & free old graphs
216                 clear_tracklist();
217         
218                 // first we gather the FFTResults of all tracks
219         
220                 Sample *buf    = (Sample *) malloc(sizeof(Sample) * fft_graph.windowSize());
221                 Sample *mixbuf = (Sample *) malloc(sizeof(Sample) * fft_graph.windowSize());
222                 float  *gain   = (float *)  malloc(sizeof(float) * fft_graph.windowSize());
223         
224                 Selection s = PublicEditor::instance().get_selection();
225                 TimeSelection ts = s.time;
226                 RegionSelection ars = s.regions;
227         
228                 for (TrackSelection::iterator i = s.tracks.begin(); i != s.tracks.end(); ++i) {
229                         boost::shared_ptr<AudioPlaylist> pl
230                                 = boost::dynamic_pointer_cast<AudioPlaylist>((*i)->playlist());
231
232                         if (!pl)
233                                 continue;
234
235                         RouteUI *rui = dynamic_cast<RouteUI *>(*i);
236                         
237                         // Busses don't have playlists, so we need to check that we actually are working with a playlist
238                         if (!pl || !rui)
239                                 continue;
240
241                         FFTResult *res = fft_graph.prepareResult(rui->color(), rui->route()->name());
242                 
243                         // if timeSelection
244                         if (source_selection_ranges_rb.get_active()) {
245 //                              cerr << "Analyzing ranges on track " << *&rui->route().name() << endl;
246                                 
247                                 for (std::list<AudioRange>::iterator j = ts.begin(); j != ts.end(); ++j) {
248
249                                         nframes_t i = 0;
250                                         int n;
251                         
252                                         while ( i < (*j).length() ) {
253                                                 // TODO: What about stereo+ channels? composite all to one, I guess
254
255                                                 n = fft_graph.windowSize();
256
257                                                 if (i + n >= (*j).length() ) {
258                                                         n = (*j).length() - i;
259                                                 }
260                                 
261                                                 n = pl->read(buf, mixbuf, gain, (*j).start + i, n);
262         
263                                                 if ( n < fft_graph.windowSize()) {
264                                                         for (int j = n; j < fft_graph.windowSize(); j++) {
265                                                                 buf[j] = 0.0;
266                                                         }
267                                                 }
268         
269                                                 res->analyzeWindow(buf);
270                                 
271                                                 i += n;
272                                         }
273                                 }
274                         } else if (source_selection_regions_rb.get_active()) {
275 //                              cerr << "Analyzing selected regions on track " << *&rui->route().name() << endl;
276                                 
277                                 TimeAxisView *current_axis = (*i);
278                                 
279                                 for (RegionSelection::iterator j = ars.begin(); j != ars.end(); ++j) {
280                                         // Check that the region is actually audio (so we can analyze it)
281                                         AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*j);
282                                         if (!arv)
283                                                 continue;
284                                         
285                                         // Check that the region really is selected on _this_ track/solo
286                                         if ( &arv->get_time_axis_view() != current_axis)
287                                                 continue;
288
289 //                                      cerr << " - " << (*j)->region().name() << ": " << (*j)->region().length() << " samples starting at " << (*j)->region().position() << endl;
290                                         nframes_t i = 0;
291                                         int n;
292
293                                         while ( i < arv->region()->length() ) {
294                                                 // TODO: What about stereo+ channels? composite all to one, I guess
295
296                                                 n = fft_graph.windowSize();
297                                                 if (i + n >= arv->region()->length() ) {
298                                                         n = arv->region()->length() - i;
299                                                 }
300
301                                                 n = arv->audio_region()->read_at(buf, mixbuf, gain, arv->region()->position() + i, n);
302         
303                                                 if ( n < fft_graph.windowSize()) {
304                                                         for (int j = n; j < fft_graph.windowSize(); j++) {
305                                                                 buf[j] = 0.0;
306                                                         }
307                                                 }
308         
309                                                 res->analyzeWindow(buf);
310                                 
311                                                 i += n;
312                                         }
313 //                                      cerr << "Found: " << (*j)->get_item_name() << endl;
314
315                                 }
316
317                         }
318                         res->finalize();
319
320                                 
321                         Gtk::TreeModel::Row newrow = *(tlmodel)->append();
322                         newrow[tlcols.trackname]   = rui->route()->name();
323                         newrow[tlcols.visible]     = true;
324                         newrow[tlcols.color]       = rui->color();
325                         newrow[tlcols.graph]       = res;
326                 }       
327
328         
329                 free(buf);
330                 free(mixbuf);
331
332                 track_list_ready = true;
333         } /* end lock */
334         
335         fft_graph.redraw();
336 }
337
338 void
339 AnalysisWindow::source_selection_changed (Gtk::RadioButton *button)
340 {
341         // We are only interested in activation signals, not deactivation signals
342         if (!button->get_active())
343                 return;
344
345         /*
346         cerr << "AnalysisWindow: signal source = ";
347         
348         if (button == &source_selection_ranges_rb) {
349                 cerr << "selected ranges" << endl;
350                 
351         } else if (button == &source_selection_regions_rb) {
352                 cerr << "selected regions" << endl;
353                 
354         } else {
355                 cerr << "unknown?" << endl;
356         }
357         */
358 }
359
360 void
361 AnalysisWindow::display_model_changed (Gtk::RadioButton *button)
362 {
363         // We are only interested in activation signals, not deactivation signals
364         if (!button->get_active())
365                 return;
366
367         /*
368         cerr << "AnalysisWindow: display model = ";
369         
370         if (button == &display_model_composite_separate_rb) {
371                 cerr << "separate composites of tracks" << endl;
372         } else if (button == &display_model_composite_all_tracks_rb) {
373                 cerr << "composite of all tracks" << endl;
374         } else {
375                 cerr << "unknown?" << endl;
376         }
377         */
378 }
379           
380