summaryrefslogtreecommitdiff
path: root/gtk2_ardour/analysis_window.cc
blob: b6802352aa1154270f5f24493f0aca283e8066c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * Copyright (C) 2006-2009 David Robillard <d@drobilla.net>
 * Copyright (C) 2006-2017 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2006 Sampo Savolainen <v2@iki.fi>
 * Copyright (C) 2007 Doug McLain <doug@nostar.net>
 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
 * Copyright (C) 2014-2016 Robin Gareus <robin@gareus.org>
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <gtkmm2ext/gtk_ui.h>
#include <gtkmm/stock.h>
#include <gtkmm/label.h>
#include <gtkmm/treemodel.h>
#include <gtkmm/treeiter.h>

#include "ardour/audioregion.h"
#include "ardour/audioplaylist.h"
#include "ardour/types.h"

#include "analysis_window.h"

#include "route_ui.h"
#include "time_axis_view.h"
#include "public_editor.h"
#include "selection.h"
#include "audio_region_view.h"

#include "pbd/i18n.h"

using namespace ARDOUR;
using namespace PBD;

AnalysisWindow::AnalysisWindow()
	: source_selection_label       (_("Signal source"))
	, source_selection_ranges_rb   (_("Selected ranges"))
	, source_selection_regions_rb  (_("Selected regions"))
	, show_minmax_button           (_("Show frequency power range"))
	, show_normalized_button       (_("Fit dB range"))
	, show_proportional_button     (_("Proportional Spectrum, -18dB"))
	, fft_graph                    (16384)
{
	set_name  (_("FFT analysis window"));
	set_title (_("Spectral Analysis"));

	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(_("Show"), 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<Gtk::CellRendererText*>(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 (
			sigc::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 (
				sigc::bind ( sigc::mem_fun(*this, &AnalysisWindow::source_selection_changed), &source_selection_ranges_rb));

		// "Selected regions" radio
		source_selection_regions_rb.signal_toggled().connect (
				sigc::bind ( sigc::mem_fun(*this, &AnalysisWindow::source_selection_changed), &source_selection_regions_rb));
	}

	// Analyze button

	refresh_button.set_name("EditorGTKButton");
	refresh_button.set_label(_("Re-analyze data"));


	refresh_button.signal_clicked().connect ( sigc::bind ( sigc::mem_fun(*this, &AnalysisWindow::analyze_data), &refresh_button));

	vbox.pack_start(refresh_button, false, false, 10);

	vbox.pack_start(hseparator1, false, false);

	// Feature checkboxes

	// normalize, fit y-range
	show_normalized_button.signal_toggled().connect( sigc::mem_fun(*this, &AnalysisWindow::show_normalized_changed));
	vbox.pack_start(show_normalized_button, false, false);

	// minmax
	show_minmax_button.signal_toggled().connect( sigc::mem_fun(*this, &AnalysisWindow::show_minmax_changed));
	vbox.pack_start(show_minmax_button, false, false);

	// pink-noise / proportional spectrum
	show_proportional_button.signal_toggled().connect( sigc::mem_fun(*this, &AnalysisWindow::show_proportional_changed));
	vbox.pack_start(show_proportional_button, false, false);



	hbox.pack_start(vbox, Gtk::PACK_SHRINK);

	// Analysis window on the right
	fft_graph.ensure_style();

	hbox.add(fft_graph);



	// And last we pack the hbox
	add(hbox);
	show_all();
	track_list.show_all();
}

AnalysisWindow::~AnalysisWindow()
{

}

void
AnalysisWindow::show_minmax_changed()
{
	fft_graph.set_show_minmax(show_minmax_button.get_active());
}

void
AnalysisWindow::show_normalized_changed()
{
	fft_graph.set_show_normalized(show_normalized_button.get_active());
}

void
AnalysisWindow::show_proportional_changed()
{
	fft_graph.set_show_proportioanl(show_proportional_button.get_active());
}

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()
{
	analyze_data(&refresh_button);
}

void
AnalysisWindow::analyze_data (Gtk::Button * /*button*/)
{
	track_list_ready = false;
	{
		Glib::Threads::Mutex::Lock lm  (track_list_lock);

		// 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());

		Selection& s (PublicEditor::instance().get_selection());


		// if timeSelection
		if (source_selection_ranges_rb.get_active()) {
			TimeSelection ts = s.time;

			for (TrackSelection::iterator i = s.tracks.begin(); i != s.tracks.end(); ++i) {
				boost::shared_ptr<AudioPlaylist> pl
					= boost::dynamic_pointer_cast<AudioPlaylist>((*i)->playlist());

				if (!pl)
					continue;

				RouteUI *rui = dynamic_cast<RouteUI *>(*i);
				int n_inputs = rui->route()->n_inputs().n_audio(); // FFT is audio only

				// Busses don't have playlists, so we need to check that we actually are working with a playlist
				if (!pl || !rui)
					continue;

				// std::cerr << "Analyzing ranges on track " << rui->route()->name() << std::endl;

				FFTResult *res = fft_graph.prepareResult(rui->route_color(), rui->route()->name());
				for (std::list<AudioRange>::iterator j = ts.begin(); j != ts.end(); ++j) {

					int n;
					for (int channel = 0; channel < n_inputs; channel++) {
						samplecnt_t x = 0;

						while (x < j->length()) {
							// TODO: What about stereo+ channels? composite all to one, I guess

							n = fft_graph.windowSize();

							if (x + n >= (*j).length() ) {
								n = (*j).length() - x;
							}

							n = pl->read(buf, mixbuf, gain, (*j).start + x, n, channel);

							if ( n < fft_graph.windowSize()) {
								for (int j = n; j < fft_graph.windowSize(); j++) {
									buf[j] = 0.0;
								}
							}

							res->analyzeWindow(buf);

							x += n;
						}
					}
				}
				res->finalize();

				Gtk::TreeModel::Row newrow = *(tlmodel)->append();
				newrow[tlcols.trackname]   = rui->route()->name();
				newrow[tlcols.visible]     = true;
				newrow[tlcols.color]       = rui->route_color ();
				newrow[tlcols.graph]       = res;
			}
		} else if (source_selection_regions_rb.get_active()) {
			RegionSelection ars = s.regions;
			// std::cerr << "Analyzing selected regions" << std::endl;

			for (RegionSelection::iterator j = ars.begin(); j != ars.end(); ++j) {
				// Check that the region is actually audio (so we can analyze it)
				AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*j);
				if (!arv)
					continue;

				// std::cerr << " - " << (*j)->region().name() << ": " << (*j)->region().length() << " samples starting at " << (*j)->region().position() << std::endl;
				RouteTimeAxisView *rtav = dynamic_cast<RouteTimeAxisView *>(&arv->get_time_axis_view());
				if (!rtav) {
					/* shouldn't happen... */
					continue;
				}
				FFTResult *res = fft_graph.prepareResult(rtav->color(), arv->get_item_name());
				int n;
				for (unsigned int channel = 0; channel < arv->region()->n_channels(); channel++) {

					samplecnt_t x = 0;
					samplecnt_t length = arv->region()->length();

					while (x < length) {
						// TODO: What about stereo+ channels? composite all to one, I guess

						n = fft_graph.windowSize();
						if (x + n >= length ) {
							n = length - x;
						}

						memset (buf, 0, n * sizeof (Sample));
						n = arv->audio_region()->read_at(buf, mixbuf, gain, arv->region()->position() + x, n, channel);

						if (n == 0)
							break;

						if ( n < fft_graph.windowSize()) {
							for (int j = n; j < fft_graph.windowSize(); j++) {
								buf[j] = 0.0;
							}
						}

						res->analyzeWindow(buf);
						x += n;
					}
				}
				// std::cerr << "Found: " << (*j)->get_item_name() << std::endl;
				res->finalize();

				Gtk::TreeModel::Row newrow = *(tlmodel)->append();
				newrow[tlcols.trackname]   = arv->get_item_name();
				newrow[tlcols.visible]     = true;
				newrow[tlcols.color]       = rtav->color();
				newrow[tlcols.graph]       = res;

			}

		}


		free(buf);
		free(mixbuf);
		free(gain);

		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;
	}
	*/
}