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