Vorbis Quality Config GUI
[ardour.git] / gtk2_ardour / rhythm_ferret.cc
index da31feca3c8fc6f81f3ae3f7489138fbec86301d..8eb64840f6a721c4d7aaf7535882d982a92001b5 100644 (file)
@@ -1,24 +1,39 @@
+/*
+    Copyright (C) 2012 Paul Davis
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
 #include <gtkmm/stock.h>
 #include <gtkmm2ext/utils.h>
 
 #include "pbd/memento_command.h"
 #include "pbd/convert.h"
 
-#include "ardour/transient_detector.h"
-#include "ardour/onset_detector.h"
-#include "ardour/audiosource.h"
 #include "ardour/audioregion.h"
-#include "ardour/playlist.h"
-#include "ardour/region_factory.h"
+#include "ardour/onset_detector.h"
 #include "ardour/session.h"
+#include "ardour/transient_detector.h"
 
 #include "rhythm_ferret.h"
 #include "audio_region_view.h"
 #include "editor.h"
-#include "utils.h"
 #include "time_axis_view.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace Gtk;
@@ -43,20 +58,25 @@ static const gchar * _onset_function_strings[] = {
        N_("Phase Deviation"),
        N_("Kullback-Liebler"),
        N_("Modified Kullback-Liebler"),
+#ifdef HAVE_AUBIO4
+       N_("Spectral Flux"),
+#endif
        0
 };
 
 static const gchar * _operation_strings[] = {
        N_("Split region"),
-       N_("Set tempo map"),
-       N_("Conform region"),
+#if 0 // these don't do what a user expects
+       N_("Snap regions"),
+       N_("Conform regions"),
+#endif
        0
 };
 
 RhythmFerret::RhythmFerret (Editor& e)
        : ArdourDialog (_("Rhythm Ferret"))
        , editor (e)
-       , detection_threshold_adjustment (3, 0, 20, 1, 4)
+       , detection_threshold_adjustment (-35, -80, -6, 1, 6)
        , detection_threshold_scale (detection_threshold_adjustment)
        , sensitivity_adjustment (40, 0, 100, 1, 10)
        , sensitivity_scale (sensitivity_adjustment)
@@ -65,6 +85,10 @@ RhythmFerret::RhythmFerret (Editor& e)
        , peak_picker_threshold_scale (peak_picker_threshold_adjustment)
        , silence_threshold_adjustment (-90.0, -120.0, 0.0, 1, 10)
        , silence_threshold_scale (silence_threshold_adjustment)
+#ifdef HAVE_AUBIO4
+       , minioi_adjustment (4, 0, 40, 1, 5)
+       , minioi_scale (minioi_adjustment)
+#endif
        , trigger_gap_adjustment (3, 0, 100, 1, 10)
        , trigger_gap_spinner (trigger_gap_adjustment)
        , action_button (Stock::APPLY)
@@ -84,6 +108,7 @@ RhythmFerret::RhythmFerret (Editor& e)
           XXX there should be a non-hacky way to set this
         */
        onset_detection_function_selector.set_active_text (onset_function_strings[3]);
+       detection_threshold_scale.set_digits (3);
 
        Table* t = manage (new Table (7, 3));
        t->set_spacings (12);
@@ -98,30 +123,37 @@ RhythmFerret::RhythmFerret (Editor& e)
        t->attach (onset_detection_function_selector, 1, 2, n, n + 1, FILL);
        ++n;
 
-       t->attach (*manage (new Label (_("Trigger gap"), 1, 0.5)), 0, 1, n, n + 1, FILL);
+       t->attach (*manage (new Label (_("Trigger gap (postproc)"), 1, 0.5)), 0, 1, n, n + 1, FILL);
        t->attach (trigger_gap_spinner, 1, 2, n, n + 1, FILL);
        t->attach (*manage (new Label (_("ms"))), 2, 3, n, n + 1, FILL);
        ++n;
 
-       t->attach (*manage (new Label (_("Threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
-       t->attach (detection_threshold_scale, 1, 2, n, n + 1, FILL);
-       t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
-       ++n;
-
        t->attach (*manage (new Label (_("Peak threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
        t->attach (peak_picker_threshold_scale, 1, 2, n, n + 1, FILL);
-       t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
        ++n;
-       
+
        t->attach (*manage (new Label (_("Silence threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
        t->attach (silence_threshold_scale, 1, 2, n, n + 1, FILL);
        t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
        ++n;
 
+#ifdef HAVE_AUBIO4
+       t->attach (*manage (new Label (_("Min Inter-Onset Time"), 1, 0.5)), 0, 1, n, n + 1, FILL);
+       t->attach (minioi_scale, 1, 2, n, n + 1, FILL);
+       t->attach (*manage (new Label (_("ms"))), 2, 3, n, n + 1, FILL);
+       ++n;
+#endif
+
+
        t->attach (*manage (new Label (_("Sensitivity"), 1, 0.5)), 0, 1, n, n + 1, FILL);
        t->attach (sensitivity_scale, 1, 2, n, n + 1, FILL);
        ++n;
 
+       t->attach (*manage (new Label (_("Cut Pos Threshold"), 1, 0.5)), 0, 1, n, n + 1, FILL);
+       t->attach (detection_threshold_scale, 1, 2, n, n + 1, FILL);
+       t->attach (*manage (new Label (_("dB"))), 2, 3, n, n + 1, FILL);
+       ++n;
+
        t->attach (*manage (new Label (_("Operation"), 1, 0.5)), 0, 1, n, n + 1, FILL);
        t->attach (operation_selector, 1, 2, n, n + 1, FILL);
        ++n;
@@ -140,16 +172,27 @@ RhythmFerret::RhythmFerret (Editor& e)
        analysis_mode_changed ();
 }
 
+void
+RhythmFerret::on_response (int response_id)
+{
+       Gtk::Dialog::on_response (response_id);
+}
+
 void
 RhythmFerret::analysis_mode_changed ()
 {
        bool const perc = get_analysis_mode() == PercussionOnset;
-       
+
+       // would be nice to actually hide/show the rows.
        detection_threshold_scale.set_sensitive (perc);
        sensitivity_scale.set_sensitive (perc);
+       trigger_gap_spinner.set_sensitive (!perc);
        onset_detection_function_selector.set_sensitive (!perc);
        peak_picker_threshold_scale.set_sensitive (!perc);
        silence_threshold_scale.set_sensitive (!perc);
+#ifdef HAVE_AUBIO4
+       minioi_scale.set_sensitive (!perc);
+#endif
 }
 
 RhythmFerret::AnalysisMode
@@ -168,7 +211,7 @@ RhythmFerret::Action
 RhythmFerret::get_action () const
 {
        if (operation_selector.get_active_row_number() == 1) {
-               return DefineTempoMap;
+               return SnapRegionsToGrid;
        } else if (operation_selector.get_active_row_number() == 2) {
                return ConformRegion;
        }
@@ -183,15 +226,17 @@ RhythmFerret::run_analysis ()
                return;
        }
 
-       RegionSelection& regions (editor.get_selection().regions);
+       clear_transients ();
+
+       regions_with_transients = editor.get_selection().regions;
 
        current_results.clear ();
 
-       if (regions.empty()) {
+       if (regions_with_transients.empty()) {
                return;
        }
 
-       for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
+       for (RegionSelection::iterator i = regions_with_transients.begin(); i != regions_with_transients.end(); ++i) {
 
                boost::shared_ptr<Readable> rd = boost::static_pointer_cast<AudioRegion> ((*i)->region());
 
@@ -205,43 +250,43 @@ RhythmFerret::run_analysis ()
                default:
                        break;
                }
-               
-               (*i)->region()->set_transients (current_results);
+
+               (*i)->region()->set_onsets (current_results);
                current_results.clear();
        }
 }
 
 int
-RhythmFerret::run_percussion_onset_analysis (boost::shared_ptr<Readable> readable, framepos_t /*offset*/, AnalysisFeatureList& results)
+RhythmFerret::run_percussion_onset_analysis (boost::shared_ptr<Readable> readable, sampleoffset_t /*offset*/, AnalysisFeatureList& results)
 {
-       TransientDetector t (_session->frame_rate());
-
-       for (uint32_t i = 0; i < readable->n_channels(); ++i) {
+       try {
+               TransientDetector t (_session->sample_rate());
 
-               AnalysisFeatureList these_results;
+               for (uint32_t i = 0; i < readable->n_channels(); ++i) {
 
-               t.reset ();
-               t.set_threshold (detection_threshold_adjustment.get_value());
-               t.set_sensitivity (sensitivity_adjustment.get_value());
+                       AnalysisFeatureList these_results;
 
-               if (t.run ("", readable.get(), i, these_results)) {
-                       continue;
-               }
+                       t.reset ();
+                       float dB = detection_threshold_adjustment.get_value();
+                       float coeff = dB > -80.0f ? pow (10.0f, dB * 0.05f) : 0.0f;
+                       t.set_threshold (coeff);
+                       t.set_sensitivity (4, sensitivity_adjustment.get_value());
 
-               /* translate all transients to give absolute position */
+                       if (t.run ("", readable.get(), i, these_results)) {
+                               continue;
+                       }
 
-               //for (AnalysisFeatureList::iterator x = these_results.begin(); x != these_results.end(); ++x) {
-               //      (*x) += offset;
-               //}
+                       /* merge */
 
-               /* merge */
+                       results.insert (results.end(), these_results.begin(), these_results.end());
+                       these_results.clear ();
 
-               results.insert (results.end(), these_results.begin(), these_results.end());
-               these_results.clear ();
-       }
+                       t.update_positions (readable.get(), i, results);
+               }
 
-       if (!results.empty()) {
-               TransientDetector::cleanup_transients (results, _session->frame_rate(), trigger_gap_adjustment.get_value());
+       } catch (failed_constructor& err) {
+               error << "Could not load percussion onset detection plugin" << endmsg;
+               return -1;
        }
 
        return 0;
@@ -258,38 +303,38 @@ RhythmFerret::get_note_onset_function ()
                        return n;
                }
        }
+
        fatal << string_compose (_("programming error: %1 (%2)"), X_("illegal note onset function string"), txt)
              << endmsg;
-       /*NOTREACHED*/
+
+       abort(); /*NOTREACHED*/
        return -1;
 }
 
 int
-RhythmFerret::run_note_onset_analysis (boost::shared_ptr<Readable> readable, framepos_t /*offset*/, AnalysisFeatureList& results)
+RhythmFerret::run_note_onset_analysis (boost::shared_ptr<Readable> readable, sampleoffset_t /*offset*/, AnalysisFeatureList& results)
 {
        try {
-               OnsetDetector t (_session->frame_rate());
+               OnsetDetector t (_session->sample_rate());
 
                for (uint32_t i = 0; i < readable->n_channels(); ++i) {
 
                        AnalysisFeatureList these_results;
 
-                       t.reset ();
-
                        t.set_function (get_note_onset_function());
                        t.set_silence_threshold (silence_threshold_adjustment.get_value());
                        t.set_peak_threshold (peak_picker_threshold_adjustment.get_value());
+#ifdef HAVE_AUBIO4
+                       t.set_minioi (minioi_adjustment.get_value());
+#endif
+
+                       // aubio-vamp only picks up new settings on reset.
+                       t.reset ();
 
                        if (t.run ("", readable.get(), i, these_results)) {
                                continue;
                        }
 
-                       /* translate all transients to give absolute position */
-
-                       //for (AnalysisFeatureList::iterator x = these_results.begin(); x != these_results.end(); ++x) {
-                       //      (*x) += offset;
-                       //}
-
                        /* merge */
 
                        results.insert (results.end(), these_results.begin(), these_results.end());
@@ -302,7 +347,7 @@ RhythmFerret::run_note_onset_analysis (boost::shared_ptr<Readable> readable, fra
        }
 
        if (!results.empty()) {
-               OnsetDetector::cleanup_onsets (results, _session->frame_rate(), trigger_gap_adjustment.get_value());
+               OnsetDetector::cleanup_onsets (results, _session->sample_rate(), trigger_gap_adjustment.get_value());
        }
 
        return 0;
@@ -319,6 +364,10 @@ RhythmFerret::do_action ()
        case SplitRegion:
                do_split_action ();
                break;
+       case SnapRegionsToGrid:
+               // split first, select all.. ?!
+               editor.snap_regions_to_grid();
+               break;
        case ConformRegion:
                editor.close_region_gaps();
                break;
@@ -330,25 +379,31 @@ RhythmFerret::do_action ()
 void
 RhythmFerret::do_split_action ()
 {
-       RegionSelection& regions (editor.get_selection().regions);
-       
+       /* XXX: this is quite a special-case; (currently) the only operation which is
+          performed on the selection only (without entered_regionview or the edit point
+          being considered)
+       */
+       RegionSelection regions = editor.selection->regions;
+
        if (regions.empty()) {
                return;
        }
 
-       _session->begin_reversible_command (_("split regions (rhythm ferret)"));
-       
+       editor.EditorFreeze(); /* Emit signal */
+
+       editor.begin_reversible_command (_("split regions (rhythm ferret)"));
+
        /* Merge the transient positions for regions in consideration */
        AnalysisFeatureList merged_features;
-       
+
        for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
-               
+
                AnalysisFeatureList features;
-               features = (*i)->region()->transients();
-               
-               merged_features.insert (merged_features.end(), features.begin(), features.end());               
+               (*i)->region()->transients(features);
+
+               merged_features.insert (merged_features.end(), features.begin(), features.end());
        }
-       
+
        merged_features.sort();
        merged_features.unique();
 
@@ -359,16 +414,15 @@ RhythmFerret::do_split_action ()
                tmp = i;
                ++tmp;
 
-               AnalysisFeatureList features;
-               features = (*i)->region()->transients();
-               editor.split_region_at_points ((*i)->region(), merged_features, false);
+               editor.split_region_at_points ((*i)->region(), merged_features, false, false);
 
                /* i is invalid at this point */
-
                i = tmp;
        }
 
-       _session->commit_reversible_command ();
+       editor.commit_reversible_command ();
+
+       editor.EditorThaw(); /* Emit signal */
 }
 
 void
@@ -382,5 +436,19 @@ void
 RhythmFerret::on_hide ()
 {
        ArdourDialog::on_hide ();
+       clear_transients ();
+}
+
+/* Clear any transients that we have added */
+void
+RhythmFerret::clear_transients ()
+{
+       current_results.clear ();
+
+       for (RegionSelection::iterator i = regions_with_transients.begin(); i != regions_with_transients.end(); ++i) {
+               (*i)->region()->set_onsets (current_results);
+       }
+
+       regions_with_transients.clear ();
 }