2 Copyright (C) 2012 Paul Davis
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.
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.
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.
20 #include <gtkmm/stock.h>
21 #include <gtkmm2ext/utils.h>
23 #include "pbd/memento_command.h"
24 #include "pbd/convert.h"
26 #include "ardour/audioregion.h"
27 #include "ardour/onset_detector.h"
28 #include "ardour/session.h"
29 #include "ardour/transient_detector.h"
31 #include "rhythm_ferret.h"
32 #include "audio_region_view.h"
34 #include "time_axis_view.h"
42 using namespace ARDOUR;
44 /* order of these must match the AnalysisMode enums
47 static const gchar * _analysis_mode_strings[] = {
48 N_("Percussive Onset"),
53 static const gchar * _onset_function_strings[] = {
55 N_("Spectral Difference"),
56 N_("High-Frequency Content"),
58 N_("Phase Deviation"),
59 N_("Kullback-Liebler"),
60 N_("Modified Kullback-Liebler"),
64 static const gchar * _operation_strings[] = {
67 N_("Conform regions"),
71 RhythmFerret::RhythmFerret (Editor& e)
72 : ArdourDialog (_("Rhythm Ferret"))
74 , detection_threshold_adjustment (0.015, 0.0, 0.1, 0.001, 0.1)
75 , detection_threshold_scale (detection_threshold_adjustment)
76 , sensitivity_adjustment (40, 0, 100, 1, 10)
77 , sensitivity_scale (sensitivity_adjustment)
78 , analyze_button (_("Analyze"))
79 , peak_picker_threshold_adjustment (0.3, 0.0, 1.0, 0.01, 0.1)
80 , peak_picker_threshold_scale (peak_picker_threshold_adjustment)
81 , silence_threshold_adjustment (-90.0, -120.0, 0.0, 1, 10)
82 , silence_threshold_scale (silence_threshold_adjustment)
83 , trigger_gap_adjustment (3, 0, 100, 1, 10)
84 , trigger_gap_spinner (trigger_gap_adjustment)
85 , action_button (Stock::APPLY)
87 operation_strings = I18N (_operation_strings);
88 Gtkmm2ext::set_popdown_strings (operation_selector, operation_strings);
89 operation_selector.set_active (0);
91 analysis_mode_strings = I18N (_analysis_mode_strings);
92 Gtkmm2ext::set_popdown_strings (analysis_mode_selector, analysis_mode_strings);
93 analysis_mode_selector.set_active_text (analysis_mode_strings.front());
94 analysis_mode_selector.signal_changed().connect (sigc::mem_fun (*this, &RhythmFerret::analysis_mode_changed));
96 onset_function_strings = I18N (_onset_function_strings);
97 Gtkmm2ext::set_popdown_strings (onset_detection_function_selector, onset_function_strings);
98 /* Onset plugin uses complex domain as default function
99 XXX there should be a non-hacky way to set this
101 onset_detection_function_selector.set_active_text (onset_function_strings[3]);
102 detection_threshold_scale.set_digits (3);
104 Table* t = manage (new Table (7, 3));
105 t->set_spacings (12);
109 t->attach (*manage (new Label (_("Mode"), 1, 0.5)), 0, 1, n, n + 1, FILL);
110 t->attach (analysis_mode_selector, 1, 2, n, n + 1, FILL);
113 t->attach (*manage (new Label (_("Detection function"), 1, 0.5)), 0, 1, n, n + 1, FILL);
114 t->attach (onset_detection_function_selector, 1, 2, n, n + 1, FILL);
117 t->attach (*manage (new Label (_("Trigger gap"), 1, 0.5)), 0, 1, n, n + 1, FILL);
118 t->attach (trigger_gap_spinner, 1, 2, n, n + 1, FILL);
119 t->attach (*manage (new Label (_("ms"))), 2, 3, n, n + 1, FILL);
122 t->attach (*manage (new Label (_("Threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
123 t->attach (detection_threshold_scale, 1, 2, n, n + 1, FILL);
124 t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
127 t->attach (*manage (new Label (_("Peak threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
128 t->attach (peak_picker_threshold_scale, 1, 2, n, n + 1, FILL);
129 t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
132 t->attach (*manage (new Label (_("Silence threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
133 t->attach (silence_threshold_scale, 1, 2, n, n + 1, FILL);
134 t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
137 t->attach (*manage (new Label (_("Sensitivity"), 1, 0.5)), 0, 1, n, n + 1, FILL);
138 t->attach (sensitivity_scale, 1, 2, n, n + 1, FILL);
141 t->attach (*manage (new Label (_("Operation"), 1, 0.5)), 0, 1, n, n + 1, FILL);
142 t->attach (operation_selector, 1, 2, n, n + 1, FILL);
145 analyze_button.signal_clicked().connect (sigc::mem_fun (*this, &RhythmFerret::run_analysis));
146 action_button.signal_clicked().connect (sigc::mem_fun (*this, &RhythmFerret::do_action));
148 get_vbox()->set_border_width (6);
149 get_vbox()->set_spacing (6);
150 get_vbox()->pack_start (*t);
152 add_action_widget (analyze_button, 1);
153 add_action_widget (action_button, 0);
156 analysis_mode_changed ();
160 RhythmFerret::analysis_mode_changed ()
162 bool const perc = get_analysis_mode() == PercussionOnset;
164 trigger_gap_spinner.set_sensitive (!perc);
165 detection_threshold_scale.set_sensitive (perc);
166 sensitivity_scale.set_sensitive (perc);
167 onset_detection_function_selector.set_sensitive (!perc);
168 peak_picker_threshold_scale.set_sensitive (!perc);
169 silence_threshold_scale.set_sensitive (!perc);
172 RhythmFerret::AnalysisMode
173 RhythmFerret::get_analysis_mode () const
175 string str = analysis_mode_selector.get_active_text ();
177 if (str == analysis_mode_strings[(int) NoteOnset]) {
181 return PercussionOnset;
185 RhythmFerret::get_action () const
187 if (operation_selector.get_active_row_number() == 1) {
188 return SnapRegionsToGrid;
189 } else if (operation_selector.get_active_row_number() == 2) {
190 return ConformRegion;
197 RhythmFerret::run_analysis ()
205 regions_with_transients = editor.get_selection().regions;
207 current_results.clear ();
209 if (regions_with_transients.empty()) {
213 for (RegionSelection::iterator i = regions_with_transients.begin(); i != regions_with_transients.end(); ++i) {
215 boost::shared_ptr<Readable> rd = boost::static_pointer_cast<AudioRegion> ((*i)->region());
217 switch (get_analysis_mode()) {
218 case PercussionOnset:
219 run_percussion_onset_analysis (rd, (*i)->region()->position(), current_results);
222 run_note_onset_analysis (rd, (*i)->region()->position(), current_results);
228 (*i)->region()->set_transients (current_results);
229 current_results.clear();
234 RhythmFerret::run_percussion_onset_analysis (boost::shared_ptr<Readable> readable, frameoffset_t /*offset*/, AnalysisFeatureList& results)
236 TransientDetector t (_session->frame_rate());
238 for (uint32_t i = 0; i < readable->n_channels(); ++i) {
240 AnalysisFeatureList these_results;
243 t.set_threshold (detection_threshold_adjustment.get_value());
244 t.set_sensitivity (sensitivity_adjustment.get_value());
246 if (t.run ("", readable.get(), i, these_results)) {
252 results.insert (results.end(), these_results.begin(), these_results.end());
253 these_results.clear ();
255 t.update_positions (readable.get(), i, results);
262 RhythmFerret::get_note_onset_function ()
264 string txt = onset_detection_function_selector.get_active_text();
266 for (int n = 0; _onset_function_strings[n]; ++n) {
267 /* compare translated versions */
268 if (txt == onset_function_strings[n]) {
273 fatal << string_compose (_("programming error: %1 (%2)"), X_("illegal note onset function string"), txt)
276 abort(); /*NOTREACHED*/
281 RhythmFerret::run_note_onset_analysis (boost::shared_ptr<Readable> readable, frameoffset_t /*offset*/, AnalysisFeatureList& results)
284 OnsetDetector t (_session->frame_rate());
286 for (uint32_t i = 0; i < readable->n_channels(); ++i) {
288 AnalysisFeatureList these_results;
292 t.set_function (get_note_onset_function());
293 t.set_silence_threshold (silence_threshold_adjustment.get_value());
294 t.set_peak_threshold (peak_picker_threshold_adjustment.get_value());
296 if (t.run ("", readable.get(), i, these_results)) {
302 results.insert (results.end(), these_results.begin(), these_results.end());
303 these_results.clear ();
306 } catch (failed_constructor& err) {
307 error << "Could not load note onset detection plugin" << endmsg;
311 if (!results.empty()) {
312 OnsetDetector::cleanup_onsets (results, _session->frame_rate(), trigger_gap_adjustment.get_value());
319 RhythmFerret::do_action ()
325 switch (get_action()) {
329 case SnapRegionsToGrid:
330 editor.snap_regions_to_grid();
333 editor.close_region_gaps();
341 RhythmFerret::do_split_action ()
343 /* XXX: this is quite a special-case; (currently) the only operation which is
344 performed on the selection only (without entered_regionview or the edit point
347 RegionSelection regions = editor.selection->regions;
349 if (regions.empty()) {
353 editor.EditorFreeze(); /* Emit signal */
355 editor.begin_reversible_command (_("split regions (rhythm ferret)"));
357 /* Merge the transient positions for regions in consideration */
358 AnalysisFeatureList merged_features;
360 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
362 AnalysisFeatureList features;
363 features = (*i)->region()->transients();
365 merged_features.insert (merged_features.end(), features.begin(), features.end());
368 merged_features.sort();
369 merged_features.unique();
371 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ) {
373 RegionSelection::iterator tmp;
378 editor.split_region_at_points ((*i)->region(), merged_features, false, false);
380 /* i is invalid at this point */
384 editor.commit_reversible_command ();
386 editor.EditorThaw(); /* Emit signal */
390 RhythmFerret::set_session (Session* s)
392 ArdourDialog::set_session (s);
393 current_results.clear ();
397 RhythmFerret::on_hide ()
399 ArdourDialog::on_hide ();
403 /* Clear any transients that we have added */
405 RhythmFerret::clear_transients ()
407 current_results.clear ();
409 for (RegionSelection::iterator i = regions_with_transients.begin(); i != regions_with_transients.end(); ++i) {
410 (*i)->region()->set_transients (current_results);
413 regions_with_transients.clear ();