First commit on FFT analysis window. Still some functionality missing,
[ardour.git] / gtk2_ardour / analysis_window.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
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/playlist.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 "regionview.h"
38
39 #include "i18n.h"
40
41 using namespace ARDOUR;
42
43 AnalysisWindow::AnalysisWindow()
44         : ArdourDialog(_("analysis window")),
45         
46           fft_graph (2048),
47         
48           source_selection_label       (_("Signal source")),
49           source_selection_ranges_rb   (_("Selected ranges")),
50           source_selection_regions_rb  (_("Selected regions")),
51           
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"))
55
56 {
57
58         track_list_ready = false;
59         
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);
68
69
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));
72         
73         track_col->add_attribute(renderer->property_foreground_gdk(), tlcols.color);
74         track_col->set_expand(true);
75
76
77         tlmodel->signal_row_changed().connect (
78                         mem_fun(*this, &AnalysisWindow::track_list_row_changed) );
79         
80         fft_graph.set_analysis_window(this);
81                 
82         vbox.pack_start(track_list);
83
84
85         // "Signal source"
86         vbox.pack_start(source_selection_label, false, false);
87
88         {
89                 Gtk::RadioButtonGroup group = source_selection_ranges_rb.get_group();
90                 source_selection_regions_rb.set_group(group);
91
92                 source_selection_ranges_rb.set_active();
93                 
94                 vbox.pack_start (source_selection_ranges_rb,  false, false);
95                 vbox.pack_start (source_selection_regions_rb, false, false);
96                 
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));
100
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));
104         }
105         
106         vbox.pack_start(hseparator1, false, false);
107         
108         // "Display model"
109         vbox.pack_start(display_model_label, false, false);
110         {
111                 Gtk::RadioButtonGroup group = display_model_composite_separate_rb.get_group();
112                 display_model_composite_all_tracks_rb.set_group (group);
113                 
114                 display_model_composite_separate_rb.set_active();
115                 
116                 vbox.pack_start (display_model_composite_separate_rb,   false, false);
117                 vbox.pack_start (display_model_composite_all_tracks_rb, false, false);
118
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));
122                 
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));
126         }
127
128         vbox.pack_start(hseparator2, false, false);
129
130         refresh_button.set_name("EditorGTKButton");
131         refresh_button.set_label(_("Analyze data"));
132
133         refresh_button.signal_clicked().connect ( bind ( mem_fun(*this, &AnalysisWindow::analyze_data), &refresh_button)); 
134
135         vbox.pack_start(refresh_button, false, false, 10);
136         
137         
138         hbox.pack_start(vbox);
139         
140         // Analysis window on the right
141         fft_graph.ensure_style();
142
143         hbox.add(fft_graph);
144         
145         
146
147         // And last we pack the hbox
148         get_vbox()->pack_start(hbox);
149
150         track_list.show_all();
151
152         get_vbox()->show_all();
153 }
154
155 AnalysisWindow::~AnalysisWindow()
156 {
157
158 }
159
160 void
161 AnalysisWindow::set_rangemode()
162 {
163         source_selection_ranges_rb.set_active(true);
164 }
165
166 void
167 AnalysisWindow::set_regionmode()
168 {
169         source_selection_regions_rb.set_active(true);
170 }
171
172 void 
173 AnalysisWindow::track_list_row_changed(const Gtk::TreeModel::Path& path, const Gtk::TreeModel::iterator& iter)
174 {
175         if (track_list_ready) {
176                 fft_graph.redraw();
177         }
178 }
179
180
181 void
182 AnalysisWindow::clear_tracklist()
183 {
184         // Empty track list & free old graphs
185         Gtk::TreeNodeChildren children = track_list.get_model()->children();
186         
187         for (Gtk::TreeIter i = children.begin(); i != children.end(); i++) {
188                 Gtk::TreeModel::Row row = *i;
189
190                 FFTResult *delete_me = row[tlcols.graph];
191                 if (delete_me == 0)
192                         continue;
193
194                 // Make sure it's not drawn
195                 row[tlcols.graph] = 0;
196                 
197                 delete delete_me;
198         }
199                 
200         tlmodel->clear();
201 }
202
203 void
204 AnalysisWindow::analyze_data (Gtk::Button *button)
205 {
206         track_list_ready = false;
207         {
208                 LockMonitor lm (track_list_lock, __LINE__, __FILE__);
209
210                 // Empty track list & free old graphs
211                 clear_tracklist();
212         
213                 // first we gather the FFTResults of all tracks
214         
215                 Sample *buf    = (Sample *) malloc(sizeof(Sample) * fft_graph.windowSize());
216                 Sample *mixbuf = (Sample *) malloc(sizeof(Sample) * fft_graph.windowSize());
217                 float  *gain   = (float *)  malloc(sizeof(float) * fft_graph.windowSize());
218                 char   *work   = (char *)   malloc(sizeof(char) * fft_graph.windowSize());
219         
220                 Selection s = PublicEditor::instance().get_selection();
221                 TimeSelection ts = s.time;
222                 AudioRegionSelection ars = s.audio_regions;
223         
224         
225                 for (TrackSelection::iterator i = s.tracks.begin(); i != s.tracks.end(); ++i) {
226                         ARDOUR::Playlist *pl = (*i)->playlist();
227                         RouteUI *rui = dynamic_cast<RouteUI *>(*i);
228                         
229                         // Busses don't have playlists, so we need to check that we actually are working with a playlist
230                         if (!pl || !rui)
231                                 continue;
232
233                         FFTResult *res = fft_graph.prepareResult(*&rui->color(), *&rui->route().name());
234                 
235                         // if timeSelection
236                         if (source_selection_ranges_rb.get_active()) {
237 //                              cerr << "Analyzing ranges on track " << *&rui->route().name() << endl;
238                                 
239                                 for (std::list<ARDOUR::AudioRange>::iterator j = ts.begin(); j != ts.end(); ++j) {
240
241                                         jack_nframes_t i = 0;
242                                         int n;
243                         
244                                         while ( i < (*j).length() ) {
245                                                 // TODO: What about stereo+ channels? composite all to one, I guess
246
247                                                 n = fft_graph.windowSize();
248
249                                                 if (i + n >= (*j).length() ) {
250                                                         n = (*j).length() - i;
251                                                 }
252                                 
253                                                 n = pl->read(buf, mixbuf, gain, work, (*j).start + i, n);
254         
255                                                 if ( n < fft_graph.windowSize()) {
256                                                         for (int j = n; j < fft_graph.windowSize(); j++) {
257                                                                 buf[j] = 0.0;
258                                                         }
259                                                 }
260         
261                                                 res->analyzeWindow(buf);
262                                 
263                                                 i += n;
264                                         }
265                                 }
266                         } else if (source_selection_regions_rb.get_active()) {
267 //                              cerr << "Analyzing selected regions on track " << *&rui->route().name() << endl;
268                                 
269                                 TimeAxisView *current_axis = (*i);
270                                 
271                                 for (std::set<AudioRegionView *>::iterator j = ars.begin(); j != ars.end(); ++j) {
272                                         // Check that the region really is selected on _this_ track/solo
273                                         if ( &(*j)->get_time_axis_view() != current_axis)
274                                                 continue;
275
276 //                                      cerr << " - " << (*j)->region.name() << ": " << (*j)->region.length() << " samples starting at " << (*j)->region.position() << endl;
277                                         jack_nframes_t i = 0;
278                                         int n;
279
280                                         while ( i < (*j)->region.length() ) {
281                                                 // TODO: What about stereo+ channels? composite all to one, I guess
282
283                                                 n = fft_graph.windowSize();
284                                                 if (i + n >= (*j)->region.length() ) {
285                                                         n = (*j)->region.length() - i;
286                                                 }
287                                 
288                                                 n = (*j)->region.read_at(buf, mixbuf, gain, work, (*j)->region.position() + i, n);
289         
290                                                 if ( n < fft_graph.windowSize()) {
291                                                         for (int j = n; j < fft_graph.windowSize(); j++) {
292                                                                 buf[j] = 0.0;
293                                                         }
294                                                 }
295         
296                                                 res->analyzeWindow(buf);
297                                 
298                                                 i += n;
299                                         }
300 //                                      cerr << "Found: " << (*j)->get_item_name() << endl;
301
302                                 }
303
304                         }
305                         res->finalize();
306
307                                 
308                         Gtk::TreeModel::Row newrow = *(tlmodel)->append();
309                         newrow[tlcols.trackname]   = rui->route().name();
310                         newrow[tlcols.visible]     = true;
311                         newrow[tlcols.color]       = *&rui->color();
312                         newrow[tlcols.graph]       = res;
313                 }       
314
315         
316                 free(buf);
317                 free(mixbuf);
318                 free(work);
319
320                 track_list_ready = true;
321         } /* end lock */
322         
323         fft_graph.redraw();
324 }
325
326 void
327 AnalysisWindow::source_selection_changed (Gtk::RadioButton *button)
328 {
329         // We are only interested in activation signals, not deactivation signals
330         if (!button->get_active())
331                 return;
332
333         /*
334         cerr << "AnalysisWindow: signal source = ";
335         
336         if (button == &source_selection_ranges_rb) {
337                 cerr << "selected ranges" << endl;
338                 
339         } else if (button == &source_selection_regions_rb) {
340                 cerr << "selected regions" << endl;
341                 
342         } else {
343                 cerr << "unknown?" << endl;
344         }
345         */
346 }
347
348 void
349 AnalysisWindow::display_model_changed (Gtk::RadioButton *button)
350 {
351         // We are only interested in activation signals, not deactivation signals
352         if (!button->get_active())
353                 return;
354
355         /*
356         cerr << "AnalysisWindow: display model = ";
357         
358         if (button == &display_model_composite_separate_rb) {
359                 cerr << "separate composites of tracks" << endl;
360         } else if (button == &display_model_composite_all_tracks_rb) {
361                 cerr << "composite of all tracks" << endl;
362         } else {
363                 cerr << "unknown?" << endl;
364         }
365         */
366 }
367           
368