fix up file renaming code a little bit
[ArdourMidi.git] / gtk2_ardour / rhythm_ferret.cc
blobcba0042cfea6a9c0d5e115d5ae6f36fdee3c52a1
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_("Set tempo map"),
52 N_("Conform region"),
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)
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]);
88 Table* t = manage (new Table (7, 3));
89 t->set_spacings (12);
91 int n = 0;
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;
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;
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;
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;
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;
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;
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;
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;
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));
132 get_vbox()->set_border_width (6);
133 get_vbox()->set_spacing (6);
134 get_vbox()->pack_start (*t);
136 add_action_widget (analyze_button, 1);
137 add_action_widget (action_button, 0);
139 show_all ();
140 analysis_mode_changed ();
143 void
144 RhythmFerret::analysis_mode_changed ()
146 bool const perc = get_analysis_mode() == PercussionOnset;
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);
155 RhythmFerret::AnalysisMode
156 RhythmFerret::get_analysis_mode () const
158 string str = analysis_mode_selector.get_active_text ();
160 if (str == analysis_mode_strings[(int) NoteOnset]) {
161 return NoteOnset;
164 return PercussionOnset;
167 RhythmFerret::Action
168 RhythmFerret::get_action () const
170 if (operation_selector.get_active_row_number() == 1) {
171 return DefineTempoMap;
172 } else if (operation_selector.get_active_row_number() == 2) {
173 return ConformRegion;
176 return SplitRegion;
179 void
180 RhythmFerret::run_analysis ()
182 if (!_session) {
183 return;
186 RegionSelection& regions (editor.get_selection().regions);
188 current_results.clear ();
190 if (regions.empty()) {
191 return;
194 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
196 boost::shared_ptr<Readable> rd = boost::static_pointer_cast<AudioRegion> ((*i)->region());
198 switch (get_analysis_mode()) {
199 case PercussionOnset:
200 run_percussion_onset_analysis (rd, (*i)->region()->position(), current_results);
201 break;
202 case NoteOnset:
203 run_note_onset_analysis (rd, (*i)->region()->position(), current_results);
204 break;
205 default:
206 break;
209 (*i)->region()->set_transients (current_results);
210 current_results.clear();
215 RhythmFerret::run_percussion_onset_analysis (boost::shared_ptr<Readable> readable, nframes64_t offset, AnalysisFeatureList& results)
217 TransientDetector t (_session->frame_rate());
219 for (uint32_t i = 0; i < readable->n_channels(); ++i) {
221 AnalysisFeatureList these_results;
223 t.reset ();
224 t.set_threshold (detection_threshold_adjustment.get_value());
225 t.set_sensitivity (sensitivity_adjustment.get_value());
227 if (t.run ("", readable.get(), i, these_results)) {
228 continue;
231 /* translate all transients to give absolute position */
233 //for (AnalysisFeatureList::iterator x = these_results.begin(); x != these_results.end(); ++x) {
234 // (*x) += offset;
237 /* merge */
239 results.insert (results.end(), these_results.begin(), these_results.end());
240 these_results.clear ();
243 if (!results.empty()) {
244 TransientDetector::cleanup_transients (results, _session->frame_rate(), trigger_gap_adjustment.get_value());
247 return 0;
251 RhythmFerret::get_note_onset_function ()
253 string txt = onset_detection_function_selector.get_active_text();
255 for (int n = 0; _onset_function_strings[n]; ++n) {
256 /* compare translated versions */
257 if (txt == onset_function_strings[n]) {
258 return n;
261 fatal << string_compose (_("programming error: %1 (%2)"), X_("illegal note onset function string"), txt)
262 << endmsg;
263 /*NOTREACHED*/
264 return -1;
268 RhythmFerret::run_note_onset_analysis (boost::shared_ptr<Readable> readable, nframes64_t offset, AnalysisFeatureList& results)
270 try {
271 OnsetDetector t (_session->frame_rate());
273 for (uint32_t i = 0; i < readable->n_channels(); ++i) {
275 AnalysisFeatureList these_results;
277 t.reset ();
279 t.set_function (get_note_onset_function());
280 t.set_silence_threshold (silence_threshold_adjustment.get_value());
281 t.set_peak_threshold (peak_picker_threshold_adjustment.get_value());
283 if (t.run ("", readable.get(), i, these_results)) {
284 continue;
287 /* translate all transients to give absolute position */
289 //for (AnalysisFeatureList::iterator x = these_results.begin(); x != these_results.end(); ++x) {
290 // (*x) += offset;
293 /* merge */
295 results.insert (results.end(), these_results.begin(), these_results.end());
296 these_results.clear ();
299 } catch (failed_constructor& err) {
300 error << "Could not load note onset detection plugin" << endmsg;
301 return -1;
304 if (!results.empty()) {
305 OnsetDetector::cleanup_onsets (results, _session->frame_rate(), trigger_gap_adjustment.get_value());
308 return 0;
311 void
312 RhythmFerret::do_action ()
314 if (!_session) {
315 return;
318 switch (get_action()) {
319 case SplitRegion:
320 do_split_action ();
321 break;
322 case ConformRegion:
323 editor.close_region_gaps();
324 break;
325 default:
326 break;
330 void
331 RhythmFerret::do_split_action ()
333 /* this can/will change the current selection, so work with a copy */
335 //RegionSelection& regions (editor.get_selection().regions);
336 RegionSelection regions;
337 editor.get_regions_for_action(regions);
339 if (regions.empty()) {
340 return;
343 _session->begin_reversible_command (_("split regions (rhythm ferret)"));
345 /* Merge the transient positions for regions in consideration */
346 AnalysisFeatureList merged_features;
348 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
350 AnalysisFeatureList features;
351 features = (*i)->region()->transients();
353 merged_features.insert (merged_features.end(), features.begin(), features.end());
356 merged_features.sort();
357 merged_features.unique();
359 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ) {
361 RegionSelection::iterator tmp;
363 tmp = i;
364 ++tmp;
366 AnalysisFeatureList features;
367 features = (*i)->region()->transients();
368 editor.split_region_at_points ((*i)->region(), merged_features, false);
370 /* i is invalid at this point */
372 i = tmp;
375 _session->commit_reversible_command ();
378 void
379 RhythmFerret::set_session (Session* s)
381 ArdourDialog::set_session (s);
382 current_results.clear ();
385 void
386 RhythmFerret::on_hide ()
388 ArdourDialog::on_hide ();