Merge branch 'master' of git.ardour.org:ardour/ardour
[ardour.git] / gtk2_ardour / rhythm_ferret.cc
1 /*
2     Copyright (C) 2012 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 #include <gtkmm/stock.h>
21 #include <gtkmm2ext/utils.h>
22
23 #include "pbd/memento_command.h"
24 #include "pbd/convert.h"
25
26 #include "ardour/audioregion.h"
27 #include "ardour/onset_detector.h"
28 #include "ardour/session.h"
29 #include "ardour/transient_detector.h"
30
31 #include "rhythm_ferret.h"
32 #include "audio_region_view.h"
33 #include "editor.h"
34 #include "utils.h"
35 #include "time_axis_view.h"
36
37 #include "i18n.h"
38
39 using namespace std;
40 using namespace Gtk;
41 using namespace Gdk;
42 using namespace PBD;
43 using namespace ARDOUR;
44
45 /* order of these must match the AnalysisMode enums
46    in rhythm_ferret.h
47 */
48 static const gchar * _analysis_mode_strings[] = {
49         N_("Percussive Onset"),
50         N_("Note Onset"),
51         0
52 };
53
54 static const gchar * _onset_function_strings[] = {
55         N_("Energy Based"),
56         N_("Spectral Difference"),
57         N_("High-Frequency Content"),
58         N_("Complex Domain"),
59         N_("Phase Deviation"),
60         N_("Kullback-Liebler"),
61         N_("Modified Kullback-Liebler"),
62         0
63 };
64
65 static const gchar * _operation_strings[] = {
66         N_("Split region"),
67         N_("Snap regions"),
68         N_("Conform regions"),
69         0
70 };
71
72 RhythmFerret::RhythmFerret (Editor& e)
73         : ArdourDialog (_("Rhythm Ferret"))
74         , editor (e)
75         , detection_threshold_adjustment (0.015, 0.0, 0.1, 0.001, 0.1)
76         , detection_threshold_scale (detection_threshold_adjustment)
77         , sensitivity_adjustment (40, 0, 100, 1, 10)
78         , sensitivity_scale (sensitivity_adjustment)
79         , analyze_button (_("Analyze"))
80         , peak_picker_threshold_adjustment (0.3, 0.0, 1.0, 0.01, 0.1)
81         , peak_picker_threshold_scale (peak_picker_threshold_adjustment)
82         , silence_threshold_adjustment (-90.0, -120.0, 0.0, 1, 10)
83         , silence_threshold_scale (silence_threshold_adjustment)
84         , trigger_gap_adjustment (3, 0, 100, 1, 10)
85         , trigger_gap_spinner (trigger_gap_adjustment)
86         , action_button (Stock::APPLY)
87 {
88         operation_strings = I18N (_operation_strings);
89         Gtkmm2ext::set_popdown_strings (operation_selector, operation_strings);
90         operation_selector.set_active (0);
91
92         analysis_mode_strings = I18N (_analysis_mode_strings);
93         Gtkmm2ext::set_popdown_strings (analysis_mode_selector, analysis_mode_strings);
94         analysis_mode_selector.set_active_text (analysis_mode_strings.front());
95         analysis_mode_selector.signal_changed().connect (sigc::mem_fun (*this, &RhythmFerret::analysis_mode_changed));
96
97         onset_function_strings = I18N (_onset_function_strings);
98         Gtkmm2ext::set_popdown_strings (onset_detection_function_selector, onset_function_strings);
99         /* Onset plugin uses complex domain as default function
100            XXX there should be a non-hacky way to set this
101          */
102         onset_detection_function_selector.set_active_text (onset_function_strings[3]);
103         detection_threshold_scale.set_digits (3);
104
105         Table* t = manage (new Table (7, 3));
106         t->set_spacings (12);
107
108         int n = 0;
109
110         t->attach (*manage (new Label (_("Mode"), 1, 0.5)), 0, 1, n, n + 1, FILL);
111         t->attach (analysis_mode_selector, 1, 2, n, n + 1, FILL);
112         ++n;
113
114         t->attach (*manage (new Label (_("Detection function"), 1, 0.5)), 0, 1, n, n + 1, FILL);
115         t->attach (onset_detection_function_selector, 1, 2, n, n + 1, FILL);
116         ++n;
117
118         t->attach (*manage (new Label (_("Trigger gap"), 1, 0.5)), 0, 1, n, n + 1, FILL);
119         t->attach (trigger_gap_spinner, 1, 2, n, n + 1, FILL);
120         t->attach (*manage (new Label (_("ms"))), 2, 3, n, n + 1, FILL);
121         ++n;
122
123         t->attach (*manage (new Label (_("Threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
124         t->attach (detection_threshold_scale, 1, 2, n, n + 1, FILL);
125         t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
126         ++n;
127
128         t->attach (*manage (new Label (_("Peak threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
129         t->attach (peak_picker_threshold_scale, 1, 2, n, n + 1, FILL);
130         t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
131         ++n;
132
133         t->attach (*manage (new Label (_("Silence threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
134         t->attach (silence_threshold_scale, 1, 2, n, n + 1, FILL);
135         t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
136         ++n;
137
138         t->attach (*manage (new Label (_("Sensitivity"), 1, 0.5)), 0, 1, n, n + 1, FILL);
139         t->attach (sensitivity_scale, 1, 2, n, n + 1, FILL);
140         ++n;
141
142         t->attach (*manage (new Label (_("Operation"), 1, 0.5)), 0, 1, n, n + 1, FILL);
143         t->attach (operation_selector, 1, 2, n, n + 1, FILL);
144         ++n;
145
146         analyze_button.signal_clicked().connect (sigc::mem_fun (*this, &RhythmFerret::run_analysis));
147         action_button.signal_clicked().connect (sigc::mem_fun (*this, &RhythmFerret::do_action));
148
149         get_vbox()->set_border_width (6);
150         get_vbox()->set_spacing (6);
151         get_vbox()->pack_start (*t);
152
153         add_action_widget (analyze_button, 1);
154         add_action_widget (action_button, 0);
155
156         show_all ();
157         analysis_mode_changed ();
158 }
159
160 void
161 RhythmFerret::analysis_mode_changed ()
162 {
163         bool const perc = get_analysis_mode() == PercussionOnset;
164
165         trigger_gap_spinner.set_sensitive (!perc);
166         detection_threshold_scale.set_sensitive (perc);
167         sensitivity_scale.set_sensitive (perc);
168         onset_detection_function_selector.set_sensitive (!perc);
169         peak_picker_threshold_scale.set_sensitive (!perc);
170         silence_threshold_scale.set_sensitive (!perc);
171 }
172
173 RhythmFerret::AnalysisMode
174 RhythmFerret::get_analysis_mode () const
175 {
176         string str = analysis_mode_selector.get_active_text ();
177
178         if (str == analysis_mode_strings[(int) NoteOnset]) {
179                 return NoteOnset;
180         }
181
182         return PercussionOnset;
183 }
184
185 RhythmFerret::Action
186 RhythmFerret::get_action () const
187 {
188         if (operation_selector.get_active_row_number() == 1) {
189                 return SnapRegionsToGrid;
190         } else if (operation_selector.get_active_row_number() == 2) {
191                 return ConformRegion;
192         }
193
194         return SplitRegion;
195 }
196
197 void
198 RhythmFerret::run_analysis ()
199 {
200         if (!_session) {
201                 return;
202         }
203
204         clear_transients ();
205
206         regions_with_transients = editor.get_selection().regions;
207
208         current_results.clear ();
209
210         if (regions_with_transients.empty()) {
211                 return;
212         }
213
214         for (RegionSelection::iterator i = regions_with_transients.begin(); i != regions_with_transients.end(); ++i) {
215
216                 boost::shared_ptr<Readable> rd = boost::static_pointer_cast<AudioRegion> ((*i)->region());
217
218                 switch (get_analysis_mode()) {
219                 case PercussionOnset:
220                         run_percussion_onset_analysis (rd, (*i)->region()->position(), current_results);
221                         break;
222                 case NoteOnset:
223                         run_note_onset_analysis (rd, (*i)->region()->position(), current_results);
224                         break;
225                 default:
226                         break;
227                 }
228
229                 (*i)->region()->set_transients (current_results);
230                 current_results.clear();
231         }
232 }
233
234 int
235 RhythmFerret::run_percussion_onset_analysis (boost::shared_ptr<Readable> readable, frameoffset_t /*offset*/, AnalysisFeatureList& results)
236 {
237         TransientDetector t (_session->frame_rate());
238
239         for (uint32_t i = 0; i < readable->n_channels(); ++i) {
240
241                 AnalysisFeatureList these_results;
242
243                 t.reset ();
244                 t.set_threshold (detection_threshold_adjustment.get_value());
245                 t.set_sensitivity (sensitivity_adjustment.get_value());
246
247                 if (t.run ("", readable.get(), i, these_results)) {
248                         continue;
249                 }
250
251                 /* merge */
252
253                 results.insert (results.end(), these_results.begin(), these_results.end());
254                 these_results.clear ();
255
256                 t.update_positions (readable.get(), i, results);
257         }
258
259         return 0;
260 }
261
262 int
263 RhythmFerret::get_note_onset_function ()
264 {
265         string txt = onset_detection_function_selector.get_active_text();
266
267         for (int n = 0; _onset_function_strings[n]; ++n) {
268                 /* compare translated versions */
269                 if (txt == onset_function_strings[n]) {
270                         return n;
271                 }
272         }
273
274         fatal << string_compose (_("programming error: %1 (%2)"), X_("illegal note onset function string"), txt)
275               << endmsg;
276
277         /*NOTREACHED*/
278         return -1;
279 }
280
281 int
282 RhythmFerret::run_note_onset_analysis (boost::shared_ptr<Readable> readable, frameoffset_t /*offset*/, AnalysisFeatureList& results)
283 {
284         try {
285                 OnsetDetector t (_session->frame_rate());
286
287                 for (uint32_t i = 0; i < readable->n_channels(); ++i) {
288
289                         AnalysisFeatureList these_results;
290
291                         t.reset ();
292
293                         t.set_function (get_note_onset_function());
294                         t.set_silence_threshold (silence_threshold_adjustment.get_value());
295                         t.set_peak_threshold (peak_picker_threshold_adjustment.get_value());
296
297                         if (t.run ("", readable.get(), i, these_results)) {
298                                 continue;
299                         }
300
301                         /* merge */
302
303                         results.insert (results.end(), these_results.begin(), these_results.end());
304                         these_results.clear ();
305                 }
306
307         } catch (failed_constructor& err) {
308                 error << "Could not load note onset detection plugin" << endmsg;
309                 return -1;
310         }
311
312         if (!results.empty()) {
313                 OnsetDetector::cleanup_onsets (results, _session->frame_rate(), trigger_gap_adjustment.get_value());
314         }
315
316         return 0;
317 }
318
319 void
320 RhythmFerret::do_action ()
321 {
322         if (!_session) {
323                 return;
324         }
325
326         switch (get_action()) {
327         case SplitRegion:
328                 do_split_action ();
329                 break;
330         case SnapRegionsToGrid:
331                 editor.snap_regions_to_grid();
332                 break;
333         case ConformRegion:
334                 editor.close_region_gaps();
335                 break;
336         default:
337                 break;
338         }
339 }
340
341 void
342 RhythmFerret::do_split_action ()
343 {
344         /* XXX: this is quite a special-case; (currently) the only operation which is
345            performed on the selection only (without entered_regionview or the edit point
346            being considered)
347         */
348         RegionSelection regions = editor.selection->regions;
349
350         if (regions.empty()) {
351                 return;
352         }
353
354         editor.EditorFreeze(); /* Emit signal */
355
356         _session->begin_reversible_command (_("split regions (rhythm ferret)"));
357
358         /* Merge the transient positions for regions in consideration */
359         AnalysisFeatureList merged_features;
360
361         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
362
363                 AnalysisFeatureList features;
364                 features = (*i)->region()->transients();
365
366                 merged_features.insert (merged_features.end(), features.begin(), features.end());
367         }
368
369         merged_features.sort();
370         merged_features.unique();
371
372         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ) {
373
374                 RegionSelection::iterator tmp;
375
376                 tmp = i;
377                 ++tmp;
378
379                 editor.split_region_at_points ((*i)->region(), merged_features, false, false);
380
381                 /* i is invalid at this point */
382                 i = tmp;
383         }
384
385         _session->commit_reversible_command ();
386
387         editor.EditorThaw(); /* Emit signal */
388 }
389
390 void
391 RhythmFerret::set_session (Session* s)
392 {
393         ArdourDialog::set_session (s);
394         current_results.clear ();
395 }
396
397 void
398 RhythmFerret::on_hide ()
399 {
400         ArdourDialog::on_hide ();
401         clear_transients ();
402 }
403
404 /* Clear any transients that we have added */
405 void
406 RhythmFerret::clear_transients ()
407 {
408         current_results.clear ();
409
410         for (RegionSelection::iterator i = regions_with_transients.begin(); i != regions_with_transients.end(); ++i) {
411                 (*i)->region()->set_transients (current_results);
412         }
413
414         regions_with_transients.clear ();
415 }
416