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