1 /*
2 * Copyright (C) 2008-2012 Carl Hetherington <carl@carlh.net>
3 * Copyright (C) 2008-2012 David Robillard <d@drobilla.net>
4 * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2014-2016 Nick Mainsbridge <mainsbridge@gmail.com>
6 * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include <gtkmm/stock.h>
24 #include <gtkmm2ext/utils.h>
25
26 #include "pbd/memento_command.h"
27 #include "pbd/convert.h"
28
29 #include "ardour/audioregion.h"
30 #include "ardour/onset_detector.h"
31 #include "ardour/session.h"
32 #include "ardour/transient_detector.h"
33
34 #include "rhythm_ferret.h"
35 #include "audio_region_view.h"
36 #include "editor.h"
37 #include "time_axis_view.h"
38
39 #include "pbd/i18n.h"
40
41 using namespace std;
42 using namespace Gtk;
43 using namespace Gdk;
44 using namespace PBD;
45 using namespace ARDOUR;
46
47 /* order of these must match the AnalysisMode enums
48 in rhythm_ferret.h
49 */
50 static const gchar * _analysis_mode_strings[] = {
51 N_("Percussive Onset"),
52 N_("Note Onset"),
53 0
54 };
55
56 static const gchar * _onset_function_strings[] = {
57 N_("Energy Based"),
58 N_("Spectral Difference"),
59 N_("High-Frequency Content"),
60 N_("Complex Domain"),
61 N_("Phase Deviation"),
62 N_("Kullback-Liebler"),
63 N_("Modified Kullback-Liebler"),
64 #ifdef HAVE_AUBIO4
65 N_("Spectral Flux"),
66 #endif
67 0
68 };
69
70 static const gchar * _operation_strings[] = {
71 N_("Split region"),
72 #if 0 // these don't do what a user expects
73 N_("Snap regions"),
74 N_("Conform regions"),
75 #endif
76 0
77 };
78
RhythmFerret(Editor & e)79 RhythmFerret::RhythmFerret (Editor& e)
80 : ArdourDialog (_("Rhythm Ferret"))
81 , editor (e)
82 , detection_threshold_adjustment (-35, -80, -6, 1, 6)
83 , detection_threshold_scale (detection_threshold_adjustment)
84 , sensitivity_adjustment (40, 0, 100, 1, 10)
85 , sensitivity_scale (sensitivity_adjustment)
86 , analyze_button (_("Analyze"))
87 , peak_picker_threshold_adjustment (0.3, 0.0, 1.0, 0.01, 0.1)
88 , peak_picker_threshold_scale (peak_picker_threshold_adjustment)
89 , silence_threshold_adjustment (-90.0, -120.0, 0.0, 1, 10)
90 , silence_threshold_scale (silence_threshold_adjustment)
91 #ifdef HAVE_AUBIO4
92 , minioi_adjustment (4, 0, 40, 1, 5)
93 , minioi_scale (minioi_adjustment)
94 #endif
95 , trigger_gap_adjustment (3, 0, 100, 1, 10)
96 , trigger_gap_spinner (trigger_gap_adjustment)
97 , action_button (Stock::APPLY)
98 {
99 operation_strings = I18N (_operation_strings);
100 Gtkmm2ext::set_popdown_strings (operation_selector, operation_strings);
101 operation_selector.set_active (0);
102
103 analysis_mode_strings = I18N (_analysis_mode_strings);
104 Gtkmm2ext::set_popdown_strings (analysis_mode_selector, analysis_mode_strings);
105 analysis_mode_selector.set_active_text (analysis_mode_strings.front());
106 analysis_mode_selector.signal_changed().connect (sigc::mem_fun (*this, &RhythmFerret::analysis_mode_changed));
107
108 onset_function_strings = I18N (_onset_function_strings);
109 Gtkmm2ext::set_popdown_strings (onset_detection_function_selector, onset_function_strings);
110 /* Onset plugin uses complex domain as default function
111 XXX there should be a non-hacky way to set this
112 */
113 onset_detection_function_selector.set_active_text (onset_function_strings[3]);
114 detection_threshold_scale.set_digits (3);
115
116 Table* t = manage (new Table (7, 3));
117 t->set_spacings (12);
118
119 int n = 0;
120
121 t->attach (*manage (new Label (_("Mode"), 1, 0.5)), 0, 1, n, n + 1, FILL);
122 t->attach (analysis_mode_selector, 1, 2, n, n + 1, FILL);
123 ++n;
124
125 t->attach (*manage (new Label (_("Detection function"), 1, 0.5)), 0, 1, n, n + 1, FILL);
126 t->attach (onset_detection_function_selector, 1, 2, n, n + 1, FILL);
127 ++n;
128
129 t->attach (*manage (new Label (_("Trigger gap (postproc)"), 1, 0.5)), 0, 1, n, n + 1, FILL);
130 t->attach (trigger_gap_spinner, 1, 2, n, n + 1, FILL);
131 t->attach (*manage (new Label (_("ms"))), 2, 3, n, n + 1, FILL);
132 ++n;
133
134 t->attach (*manage (new Label (_("Peak threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
135 t->attach (peak_picker_threshold_scale, 1, 2, n, n + 1, FILL);
136 ++n;
137
138 t->attach (*manage (new Label (_("Silence threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
139 t->attach (silence_threshold_scale, 1, 2, n, n + 1, FILL);
140 t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
141 ++n;
142
143 #ifdef HAVE_AUBIO4
144 t->attach (*manage (new Label (_("Min Inter-Onset Time"), 1, 0.5)), 0, 1, n, n + 1, FILL);
145 t->attach (minioi_scale, 1, 2, n, n + 1, FILL);
146 t->attach (*manage (new Label (_("ms"))), 2, 3, n, n + 1, FILL);
147 ++n;
148 #endif
149
150
151 t->attach (*manage (new Label (_("Sensitivity"), 1, 0.5)), 0, 1, n, n + 1, FILL);
152 t->attach (sensitivity_scale, 1, 2, n, n + 1, FILL);
153 ++n;
154
155 t->attach (*manage (new Label (_("Cut Pos Threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
156 t->attach (detection_threshold_scale, 1, 2, n, n + 1, FILL);
157 t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
158 ++n;
159
160 t->attach (*manage (new Label (_("Operation"), 1, 0.5)), 0, 1, n, n + 1, FILL);
161 t->attach (operation_selector, 1, 2, n, n + 1, FILL);
162 ++n;
163
164 analyze_button.signal_clicked().connect (sigc::mem_fun (*this, &RhythmFerret::run_analysis));
165 action_button.signal_clicked().connect (sigc::mem_fun (*this, &RhythmFerret::do_action));
166
167 get_vbox()->set_border_width (6);
168 get_vbox()->set_spacing (6);
169 get_vbox()->pack_start (*t);
170
171 add_action_widget (analyze_button, 1);
172 add_action_widget (action_button, 0);
173
174 show_all ();
175 analysis_mode_changed ();
176 }
177
178 void
on_response(int response_id)179 RhythmFerret::on_response (int response_id)
180 {
181 Gtk::Dialog::on_response (response_id);
182 }
183
184 void
analysis_mode_changed()185 RhythmFerret::analysis_mode_changed ()
186 {
187 bool const perc = get_analysis_mode() == PercussionOnset;
188
189 // would be nice to actually hide/show the rows.
190 detection_threshold_scale.set_sensitive (perc);
191 sensitivity_scale.set_sensitive (perc);
192 trigger_gap_spinner.set_sensitive (!perc);
193 onset_detection_function_selector.set_sensitive (!perc);
194 peak_picker_threshold_scale.set_sensitive (!perc);
195 silence_threshold_scale.set_sensitive (!perc);
196 #ifdef HAVE_AUBIO4
197 minioi_scale.set_sensitive (!perc);
198 #endif
199 }
200
201 RhythmFerret::AnalysisMode
get_analysis_mode() const202 RhythmFerret::get_analysis_mode () const
203 {
204 string str = analysis_mode_selector.get_active_text ();
205
206 if (str == analysis_mode_strings[(int) NoteOnset]) {
207 return NoteOnset;
208 }
209
210 return PercussionOnset;
211 }
212
213 RhythmFerret::Action
get_action() const214 RhythmFerret::get_action () const
215 {
216 if (operation_selector.get_active_row_number() == 1) {
217 return SnapRegionsToGrid;
218 } else if (operation_selector.get_active_row_number() == 2) {
219 return ConformRegion;
220 }
221
222 return SplitRegion;
223 }
224
225 void
run_analysis()226 RhythmFerret::run_analysis ()
227 {
228 if (!_session) {
229 return;
230 }
231
232 clear_transients ();
233
234 regions_with_transients = editor.get_selection().regions;
235
236 current_results.clear ();
237
238 if (regions_with_transients.empty()) {
239 return;
240 }
241
242 for (RegionSelection::iterator i = regions_with_transients.begin(); i != regions_with_transients.end(); ++i) {
243
244 boost::shared_ptr<Readable> rd = boost::static_pointer_cast<AudioRegion> ((*i)->region());
245
246 switch (get_analysis_mode()) {
247 case PercussionOnset:
248 run_percussion_onset_analysis (rd, (*i)->region()->position(), current_results);
249 break;
250 case NoteOnset:
251 run_note_onset_analysis (rd, (*i)->region()->position(), current_results);
252 break;
253 default:
254 break;
255 }
256
257 (*i)->region()->set_onsets (current_results);
258 current_results.clear();
259 }
260 }
261
262 int
run_percussion_onset_analysis(boost::shared_ptr<Readable> readable,sampleoffset_t,AnalysisFeatureList & results)263 RhythmFerret::run_percussion_onset_analysis (boost::shared_ptr<Readable> readable, sampleoffset_t /*offset*/, AnalysisFeatureList& results)
264 {
265 try {
266 TransientDetector t (_session->sample_rate());
267
268 for (uint32_t i = 0; i < readable->n_channels(); ++i) {
269
270 AnalysisFeatureList these_results;
271
272 t.reset ();
273 float dB = detection_threshold_adjustment.get_value();
274 float coeff = dB > -80.0f ? pow (10.0f, dB * 0.05f) : 0.0f;
275 t.set_threshold (coeff);
276 t.set_sensitivity (4, sensitivity_adjustment.get_value());
277
278 if (t.run ("", readable.get(), i, these_results)) {
279 continue;
280 }
281
282 /* merge */
283
284 results.insert (results.end(), these_results.begin(), these_results.end());
285 these_results.clear ();
286
287 t.update_positions (readable.get(), i, results);
288 }
289
290 } catch (failed_constructor& err) {
291 error << "Could not load percussion onset detection plugin" << endmsg;
292 return -1;
293 }
294
295 return 0;
296 }
297
298 int
get_note_onset_function()299 RhythmFerret::get_note_onset_function ()
300 {
301 string txt = onset_detection_function_selector.get_active_text();
302
303 for (int n = 0; _onset_function_strings[n]; ++n) {
304 /* compare translated versions */
305 if (txt == onset_function_strings[n]) {
306 return n;
307 }
308 }
309
310 fatal << string_compose (_("programming error: %1 (%2)"), X_("illegal note onset function string"), txt)
311 << endmsg;
312
313 abort(); /*NOTREACHED*/
314 return -1;
315 }
316
317 int
run_note_onset_analysis(boost::shared_ptr<Readable> readable,sampleoffset_t,AnalysisFeatureList & results)318 RhythmFerret::run_note_onset_analysis (boost::shared_ptr<Readable> readable, sampleoffset_t /*offset*/, AnalysisFeatureList& results)
319 {
320 try {
321 OnsetDetector t (_session->sample_rate());
322
323 for (uint32_t i = 0; i < readable->n_channels(); ++i) {
324
325 AnalysisFeatureList these_results;
326
327 t.set_function (get_note_onset_function());
328 t.set_silence_threshold (silence_threshold_adjustment.get_value());
329 t.set_peak_threshold (peak_picker_threshold_adjustment.get_value());
330 #ifdef HAVE_AUBIO4
331 t.set_minioi (minioi_adjustment.get_value());
332 #endif
333
334 // aubio-vamp only picks up new settings on reset.
335 t.reset ();
336
337 if (t.run ("", readable.get(), i, these_results)) {
338 continue;
339 }
340
341 /* merge */
342
343 results.insert (results.end(), these_results.begin(), these_results.end());
344 these_results.clear ();
345 }
346
347 } catch (failed_constructor& err) {
348 error << "Could not load note onset detection plugin" << endmsg;
349 return -1;
350 }
351
352 if (!results.empty()) {
353 OnsetDetector::cleanup_onsets (results, _session->sample_rate(), trigger_gap_adjustment.get_value());
354 }
355
356 return 0;
357 }
358
359 void
do_action()360 RhythmFerret::do_action ()
361 {
362 if (!_session) {
363 return;
364 }
365
366 switch (get_action()) {
367 case SplitRegion:
368 do_split_action ();
369 break;
370 case SnapRegionsToGrid:
371 // split first, select all.. ?!
372 editor.snap_regions_to_grid();
373 break;
374 case ConformRegion:
375 editor.close_region_gaps();
376 break;
377 default:
378 break;
379 }
380 }
381
382 void
do_split_action()383 RhythmFerret::do_split_action ()
384 {
385 /* XXX: this is quite a special-case; (currently) the only operation which is
386 performed on the selection only (without entered_regionview or the edit point
387 being considered)
388 */
389 RegionSelection regions = editor.selection->regions;
390
391 if (regions.empty()) {
392 return;
393 }
394
395 editor.EditorFreeze(); /* Emit signal */
396
397 editor.begin_reversible_command (_("split regions (rhythm ferret)"));
398
399 /* Merge the transient positions for regions in consideration */
400 AnalysisFeatureList merged_features;
401
402 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
403
404 AnalysisFeatureList features;
405 (*i)->region()->transients(features);
406
407 merged_features.insert (merged_features.end(), features.begin(), features.end());
408 }
409
410 merged_features.sort();
411 merged_features.unique();
412
413 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ) {
414
415 RegionSelection::iterator tmp;
416
417 tmp = i;
418 ++tmp;
419
420 editor.split_region_at_points ((*i)->region(), merged_features, false, false);
421
422 /* i is invalid at this point */
423 i = tmp;
424 }
425
426 editor.commit_reversible_command ();
427
428 editor.EditorThaw(); /* Emit signal */
429 }
430
431 void
set_session(Session * s)432 RhythmFerret::set_session (Session* s)
433 {
434 ArdourDialog::set_session (s);
435 current_results.clear ();
436 }
437
438 void
on_hide()439 RhythmFerret::on_hide ()
440 {
441 ArdourDialog::on_hide ();
442 clear_transients ();
443 }
444
445 /* Clear any transients that we have added */
446 void
clear_transients()447 RhythmFerret::clear_transients ()
448 {
449 current_results.clear ();
450
451 for (RegionSelection::iterator i = regions_with_transients.begin(); i != regions_with_transients.end(); ++i) {
452 (*i)->region()->set_onsets (current_results);
453 }
454
455 regions_with_transients.clear ();
456 }
457
458