automated build stuff continues
[ardour2.git] / gtk2_ardour / rhythm_ferret.cc
blob8845a29313d2a211d483f9bb77ad90a5a0651eae
1 #include <gtkmm/stock.h>
2 #include <gtkmm2ext/utils.h>
4 #include "pbd/memento_command.h"
5 #include "pbd/convert.h"
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"
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"
21 #include "i18n.h"
23 using namespace std;
24 using namespace Gtk;
25 using namespace Gdk;
26 using namespace PBD;
27 using namespace ARDOUR;
29 /* order of these must match the AnalysisMode enums
30 in rhythm_ferret.h
32 static const gchar * _analysis_mode_strings[] = {
33 N_("Percussive Onset"),
34 N_("Note Onset"),
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"),
49 static const gchar * _operation_strings[] = {
50 N_("Split region"),
51 N_("Snap regions"),
52 N_("Conform regions"),
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)
72 operation_strings = I18N (_operation_strings);
73 Gtkmm2ext::set_popdown_strings (operation_selector, operation_strings);
74 operation_selector.set_active (0);
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));
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
86 onset_detection_function_selector.set_active_text (onset_function_strings[3]);
87 detection_threshold_scale.set_digits (3);
89 Table* t = manage (new Table (7, 3));
90 t->set_spacings (12);
92 int n = 0;
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;
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;
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;
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;
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;
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;
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;
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;
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));
133 get_vbox()->set_border_width (6);
134 get_vbox()->set_spacing (6);
135 get_vbox()->pack_start (*t);
137 add_action_widget (analyze_button, 1);
138 add_action_widget (action_button, 0);
140 show_all ();
141 analysis_mode_changed ();
144 void
145 RhythmFerret::analysis_mode_changed ()
147 bool const perc = get_analysis_mode() == PercussionOnset;
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);
157 RhythmFerret::AnalysisMode
158 RhythmFerret::get_analysis_mode () const
160 string str = analysis_mode_selector.get_active_text ();
162 if (str == analysis_mode_strings[(int) NoteOnset]) {
163 return NoteOnset;
166 return PercussionOnset;
169 RhythmFerret::Action
170 RhythmFerret::get_action () const
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;
178 return SplitRegion;
181 void
182 RhythmFerret::run_analysis ()
184 if (!_session) {
185 return;
188 clear_transients ();
190 regions_with_transients = editor.get_selection().regions;
192 current_results.clear ();
194 if (regions_with_transients.empty()) {
195 return;
198 for (RegionSelection::iterator i = regions_with_transients.begin(); i != regions_with_transients.end(); ++i) {
200 boost::shared_ptr<Readable> rd = boost::static_pointer_cast<AudioRegion> ((*i)->region());
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;
213 (*i)->region()->set_transients (current_results);
214 current_results.clear();
219 RhythmFerret::run_percussion_onset_analysis (boost::shared_ptr<Readable> readable, framepos_t /*offset*/, AnalysisFeatureList& results)
221 TransientDetector t (_session->frame_rate());
223 for (uint32_t i = 0; i < readable->n_channels(); ++i) {
225 AnalysisFeatureList these_results;
227 t.reset ();
228 t.set_threshold (detection_threshold_adjustment.get_value());
229 t.set_sensitivity (sensitivity_adjustment.get_value());
231 if (t.run ("", readable.get(), i, these_results)) {
232 continue;
235 /* merge */
237 results.insert (results.end(), these_results.begin(), these_results.end());
238 these_results.clear ();
240 t.update_positions (readable.get(), i, results);
243 return 0;
247 RhythmFerret::get_note_onset_function ()
249 string txt = onset_detection_function_selector.get_active_text();
251 for (int n = 0; _onset_function_strings[n]; ++n) {
252 /* compare translated versions */
253 if (txt == onset_function_strings[n]) {
254 return n;
258 fatal << string_compose (_("programming error: %1 (%2)"), X_("illegal note onset function string"), txt)
259 << endmsg;
261 /*NOTREACHED*/
262 return -1;
266 RhythmFerret::run_note_onset_analysis (boost::shared_ptr<Readable> readable, framepos_t /*offset*/, AnalysisFeatureList& results)
268 try {
269 OnsetDetector t (_session->frame_rate());
271 for (uint32_t i = 0; i < readable->n_channels(); ++i) {
273 AnalysisFeatureList these_results;
275 t.reset ();
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());
281 if (t.run ("", readable.get(), i, these_results)) {
282 continue;
285 /* merge */
287 results.insert (results.end(), these_results.begin(), these_results.end());
288 these_results.clear ();
291 } catch (failed_constructor& err) {
292 error << "Could not load note onset detection plugin" << endmsg;
293 return -1;
296 if (!results.empty()) {
297 OnsetDetector::cleanup_onsets (results, _session->frame_rate(), trigger_gap_adjustment.get_value());
300 return 0;
303 void
304 RhythmFerret::do_action ()
306 if (!_session) {
307 return;
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;
325 void
326 RhythmFerret::do_split_action ()
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)
332 RegionSelection regions = editor.get_regions_from_selection();
334 if (regions.empty()) {
335 return;
338 editor.EditorFreeze(); /* Emit signal */
340 _session->begin_reversible_command (_("split regions (rhythm ferret)"));
342 /* Merge the transient positions for regions in consideration */
343 AnalysisFeatureList merged_features;
345 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
347 AnalysisFeatureList features;
348 features = (*i)->region()->transients();
350 merged_features.insert (merged_features.end(), features.begin(), features.end());
353 merged_features.sort();
354 merged_features.unique();
356 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ) {
358 RegionSelection::iterator tmp;
360 tmp = i;
361 ++tmp;
363 editor.split_region_at_points ((*i)->region(), merged_features, false, false);
365 /* i is invalid at this point */
366 i = tmp;
369 _session->commit_reversible_command ();
371 editor.EditorThaw(); /* Emit signal */
374 void
375 RhythmFerret::set_session (Session* s)
377 ArdourDialog::set_session (s);
378 current_results.clear ();
381 void
382 RhythmFerret::on_hide ()
384 ArdourDialog::on_hide ();
385 clear_transients ();
388 /* Clear any transients that we have added */
389 void
390 RhythmFerret::clear_transients ()
392 current_results.clear ();
394 for (RegionSelection::iterator i = regions_with_transients.begin(); i != regions_with_transients.end(); ++i) {
395 (*i)->region()->set_transients (current_results);
398 regions_with_transients.clear ();