store void pointers to processor UIs in Processors, and reset ProcessorWindowProxy...
[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 (3, 0, 20, 1, 4)
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
88         Table* t = manage (new Table (7, 3));
89         t->set_spacings (12);
90
91         int n = 0;
92
93         t->attach (*manage (new Label (_("Mode"), 1, 0.5)), 0, 1, n, n + 1, FILL);
94         t->attach (analysis_mode_selector, 1, 2, n, n + 1, FILL);
95         ++n;
96
97         t->attach (*manage (new Label (_("Detection function"), 1, 0.5)), 0, 1, n, n + 1, FILL);
98         t->attach (onset_detection_function_selector, 1, 2, n, n + 1, FILL);
99         ++n;
100
101         t->attach (*manage (new Label (_("Trigger gap"), 1, 0.5)), 0, 1, n, n + 1, FILL);
102         t->attach (trigger_gap_spinner, 1, 2, n, n + 1, FILL);
103         t->attach (*manage (new Label (_("ms"))), 2, 3, n, n + 1, FILL);
104         ++n;
105
106         t->attach (*manage (new Label (_("Threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
107         t->attach (detection_threshold_scale, 1, 2, n, n + 1, FILL);
108         t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
109         ++n;
110
111         t->attach (*manage (new Label (_("Peak threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
112         t->attach (peak_picker_threshold_scale, 1, 2, n, n + 1, FILL);
113         t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
114         ++n;
115         
116         t->attach (*manage (new Label (_("Silence threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
117         t->attach (silence_threshold_scale, 1, 2, n, n + 1, FILL);
118         t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
119         ++n;
120
121         t->attach (*manage (new Label (_("Sensitivity"), 1, 0.5)), 0, 1, n, n + 1, FILL);
122         t->attach (sensitivity_scale, 1, 2, n, n + 1, FILL);
123         ++n;
124
125         t->attach (*manage (new Label (_("Operation"), 1, 0.5)), 0, 1, n, n + 1, FILL);
126         t->attach (operation_selector, 1, 2, n, n + 1, FILL);
127         ++n;
128
129         analyze_button.signal_clicked().connect (sigc::mem_fun (*this, &RhythmFerret::run_analysis));
130         action_button.signal_clicked().connect (sigc::mem_fun (*this, &RhythmFerret::do_action));
131
132         get_vbox()->set_border_width (6);
133         get_vbox()->set_spacing (6);
134         get_vbox()->pack_start (*t);
135
136         add_action_widget (analyze_button, 1);
137         add_action_widget (action_button, 0);
138
139         show_all ();
140         analysis_mode_changed ();
141 }
142
143 void
144 RhythmFerret::analysis_mode_changed ()
145 {
146         bool const perc = get_analysis_mode() == PercussionOnset;
147         
148         detection_threshold_scale.set_sensitive (perc);
149         sensitivity_scale.set_sensitive (perc);
150         onset_detection_function_selector.set_sensitive (!perc);
151         peak_picker_threshold_scale.set_sensitive (!perc);
152         silence_threshold_scale.set_sensitive (!perc);
153 }
154
155 RhythmFerret::AnalysisMode
156 RhythmFerret::get_analysis_mode () const
157 {
158         string str = analysis_mode_selector.get_active_text ();
159
160         if (str == analysis_mode_strings[(int) NoteOnset]) {
161                 return NoteOnset;
162         }
163
164         return PercussionOnset;
165 }
166
167 RhythmFerret::Action
168 RhythmFerret::get_action () const
169 {
170         if (operation_selector.get_active_row_number() == 1) {
171                 return SnapRegionsToGrid;
172         } else if (operation_selector.get_active_row_number() == 2) {
173                 return ConformRegion;
174         }
175
176         return SplitRegion;
177 }
178
179 void
180 RhythmFerret::run_analysis ()
181 {
182         if (!_session) {
183                 return;
184         }
185
186         clear_transients ();
187
188         regions_with_transients = editor.get_selection().regions;
189
190         current_results.clear ();
191
192         if (regions_with_transients.empty()) {
193                 return;
194         }
195
196         for (RegionSelection::iterator i = regions_with_transients.begin(); i != regions_with_transients.end(); ++i) {
197
198                 boost::shared_ptr<Readable> rd = boost::static_pointer_cast<AudioRegion> ((*i)->region());
199
200                 switch (get_analysis_mode()) {
201                 case PercussionOnset:
202                         run_percussion_onset_analysis (rd, (*i)->region()->position(), current_results);
203                         break;
204                 case NoteOnset:
205                         run_note_onset_analysis (rd, (*i)->region()->position(), current_results);
206                         break;
207                 default:
208                         break;
209                 }
210                 
211                 (*i)->region()->set_transients (current_results);
212                 current_results.clear();
213         }
214 }
215
216 int
217 RhythmFerret::run_percussion_onset_analysis (boost::shared_ptr<Readable> readable, framepos_t /*offset*/, AnalysisFeatureList& results)
218 {
219         TransientDetector t (_session->frame_rate());
220
221         for (uint32_t i = 0; i < readable->n_channels(); ++i) {
222
223                 AnalysisFeatureList these_results;
224
225                 t.reset ();
226                 t.set_threshold (detection_threshold_adjustment.get_value());
227                 t.set_sensitivity (sensitivity_adjustment.get_value());
228
229                 if (t.run ("", readable.get(), i, these_results)) {
230                         continue;
231                 }
232
233                 /* translate all transients to give absolute position */
234
235                 //for (AnalysisFeatureList::iterator x = these_results.begin(); x != these_results.end(); ++x) {
236                 //      (*x) += offset;
237                 //}
238
239                 /* merge */
240
241                 results.insert (results.end(), these_results.begin(), these_results.end());
242                 these_results.clear ();
243         }
244
245         if (!results.empty()) {
246                 TransientDetector::cleanup_transients (results, _session->frame_rate(), trigger_gap_adjustment.get_value());
247         }
248
249         return 0;
250 }
251
252 int
253 RhythmFerret::get_note_onset_function ()
254 {
255         string txt = onset_detection_function_selector.get_active_text();
256
257         for (int n = 0; _onset_function_strings[n]; ++n) {
258                 /* compare translated versions */
259                 if (txt == onset_function_strings[n]) {
260                         return n;
261                 }
262         }
263         fatal << string_compose (_("programming error: %1 (%2)"), X_("illegal note onset function string"), txt)
264               << endmsg;
265         /*NOTREACHED*/
266         return -1;
267 }
268
269 int
270 RhythmFerret::run_note_onset_analysis (boost::shared_ptr<Readable> readable, framepos_t /*offset*/, AnalysisFeatureList& results)
271 {
272         try {
273                 OnsetDetector t (_session->frame_rate());
274
275                 for (uint32_t i = 0; i < readable->n_channels(); ++i) {
276
277                         AnalysisFeatureList these_results;
278
279                         t.reset ();
280
281                         t.set_function (get_note_onset_function());
282                         t.set_silence_threshold (silence_threshold_adjustment.get_value());
283                         t.set_peak_threshold (peak_picker_threshold_adjustment.get_value());
284
285                         if (t.run ("", readable.get(), i, these_results)) {
286                                 continue;
287                         }
288
289                         /* translate all transients to give absolute position */
290
291                         //for (AnalysisFeatureList::iterator x = these_results.begin(); x != these_results.end(); ++x) {
292                         //      (*x) += offset;
293                         //}
294
295                         /* merge */
296
297                         results.insert (results.end(), these_results.begin(), these_results.end());
298                         these_results.clear ();
299                 }
300
301         } catch (failed_constructor& err) {
302                 error << "Could not load note onset detection plugin" << endmsg;
303                 return -1;
304         }
305
306         if (!results.empty()) {
307                 OnsetDetector::cleanup_onsets (results, _session->frame_rate(), trigger_gap_adjustment.get_value());
308         }
309
310         return 0;
311 }
312
313 void
314 RhythmFerret::do_action ()
315 {
316         if (!_session) {
317                 return;
318         }
319
320         switch (get_action()) {
321         case SplitRegion:
322                 do_split_action ();
323                 break;
324         case SnapRegionsToGrid:
325                 editor.snap_regions_to_grid();
326                 break;
327         case ConformRegion:
328                 editor.close_region_gaps();
329                 break;
330         default:
331                 break;
332         }
333 }
334
335 void
336 RhythmFerret::do_split_action ()
337 {
338         /* XXX: this is quite a special-case; (currently) the only operation which is
339            performed on the selection only (without entered_regionview or the edit point
340            being considered)
341         */
342         RegionSelection regions = editor.get_regions_from_selection();
343         
344         if (regions.empty()) {
345                 return;
346         }
347
348         _session->begin_reversible_command (_("split regions (rhythm ferret)"));
349         
350         /* Merge the transient positions for regions in consideration */
351         AnalysisFeatureList merged_features;
352         
353         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
354                 
355                 AnalysisFeatureList features;
356                 features = (*i)->region()->transients();
357                 
358                 merged_features.insert (merged_features.end(), features.begin(), features.end());
359         }
360         
361         merged_features.sort();
362         merged_features.unique();
363
364         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ) {
365
366                 RegionSelection::iterator tmp;
367
368                 tmp = i;
369                 ++tmp;
370
371                 AnalysisFeatureList features;
372                 features = (*i)->region()->transients();
373                 editor.split_region_at_points ((*i)->region(), merged_features, false, true);
374
375                 /* i is invalid at this point */
376
377                 i = tmp;
378         }
379
380         _session->commit_reversible_command ();
381 }
382
383 void
384 RhythmFerret::set_session (Session* s)
385 {
386         ArdourDialog::set_session (s);
387         current_results.clear ();
388 }
389
390 void
391 RhythmFerret::on_hide ()
392 {
393         ArdourDialog::on_hide ();
394         clear_transients ();
395 }
396
397 /* Clear any transients that we have added */
398 void
399 RhythmFerret::clear_transients ()
400 {
401         current_results.clear ();
402         for (RegionSelection::iterator i = regions_with_transients.begin(); i != regions_with_transients.end(); ++i) {
403                 (*i)->region()->set_transients (current_results);
404         }
405         regions_with_transients.clear ();
406 }
407