Tempo ramps - api rename, fix various meter and tempo dialog bugs.
[ardour.git] / gtk2_ardour / tempo_dialog.cc
index e11644131b4c4bf73590154edf8d685058648711..881da33617a51a6cc63dbe4c05f06f158feba31b 100644 (file)
@@ -23,8 +23,8 @@
 
 #include "gtkmm2ext/utils.h"
 
-#include "ardour_ui.h"
 #include "tempo_dialog.h"
+#include "ui_config.h"
 
 #include "i18n.h"
 
@@ -47,10 +47,10 @@ TempoDialog::TempoDialog (TempoMap& map, framepos_t frame, const string&)
        Tempo tempo (map.tempo_at (frame));
        map.bbt_time (frame, when);
 
-       init (when, tempo.beats_per_minute(), tempo.note_type(), true);
+       init (when, tempo.beats_per_minute(), tempo.note_type(), TempoSection::Type::Constant, true);
 }
 
-TempoDialog::TempoDialog (TempoSection& section, const string&)
+TempoDialog::TempoDialog (TempoMap& map, TempoSection& section, const string&)
        : ArdourDialog (_("Edit Tempo"))
        , bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
        , bpm_spinner (bpm_adjustment)
@@ -59,11 +59,13 @@ TempoDialog::TempoDialog (TempoSection& section, const string&)
        , pulse_selector_label (_("Pulse note"), ALIGN_LEFT, ALIGN_CENTER)
        , tap_tempo_button (_("Tap tempo"))
 {
-       init (section.start(), section.beats_per_minute(), section.note_type(), section.movable());
+       Timecode::BBT_Time when;
+       map.bbt_time (map.frame_at_beat (section.beat()), when);
+       init (when, section.beats_per_minute(), section.note_type(), section.type(), section.movable());
 }
 
 void
-TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type, bool movable)
+TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type, TempoSection::Type type, bool movable)
 {
        vector<string> strings;
        NoteTypes::iterator x;
@@ -72,6 +74,7 @@ TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type,
        bpm_spinner.set_digits (2);
        bpm_spinner.set_wrap (true);
        bpm_spinner.set_value (bpm);
+       bpm_spinner.set_alignment (1.0);
 
        note_types.insert (make_pair (_("whole"), 1.0));
        strings.push_back (_("whole"));
@@ -104,13 +107,31 @@ TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type,
        if (x == note_types.end()) {
                pulse_selector.set_active_text (strings[3]); // "quarter"
        }
-       
+
+       strings.clear();
+
+       tempo_types.insert (make_pair (_("ramped"), TempoSection::Type::Ramp));
+       strings.push_back (_("ramped"));
+       tempo_types.insert (make_pair (_("constant"), TempoSection::Type::Constant));
+       strings.push_back (_("constant"));
+       set_popdown_strings (tempo_type, strings);
+       TempoTypes::iterator tt;
+       for (tt = tempo_types.begin(); tt != tempo_types.end(); ++tt) {
+               if (tt->second == type) {
+                       tempo_type.set_active_text (tt->first);
+                       break;
+               }
+       }
+       if (tt == tempo_types.end()) {
+               tempo_type.set_active_text (strings[0]); // "ramped"
+       }
+
        Table* table;
 
-       if (ARDOUR_UI::config()->get_allow_non_quarter_pulse()) {
-               table = manage (new Table (5, 5));
+       if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
+               table = manage (new Table (5, 6));
        } else {
-               table = manage (new Table (5, 4));
+               table = manage (new Table (5, 5));
        }
 
        table->set_spacings (6);
@@ -121,7 +142,7 @@ TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type,
        table->attach (*bpm_label, 0, 1, 0, 1);
        table->attach (bpm_spinner, 1, 5, 0, 1);
 
-       if (ARDOUR_UI::config()->get_allow_non_quarter_pulse()) {
+       if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
                table->attach (pulse_selector_label, 0, 1, 1, 2);
                table->attach (pulse_selector, 1, 5, 1, 2);
                row = 2;
@@ -139,6 +160,8 @@ TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type,
        if (movable) {
                when_bar_entry.set_width_chars(4);
                when_beat_entry.set_width_chars (4);
+               when_bar_entry.set_alignment (1.0);
+               when_beat_entry.set_alignment (1.0);
 
                when_bar_label.set_name ("MetricLabel");
                when_beat_label.set_name ("MetricLabel");
@@ -153,8 +176,12 @@ TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type,
                table->attach (*when_label, 0, 1, row, row+1);
        }
 
+       Label* tempo_type_label = manage (new Label(_("Tempo Type:"), ALIGN_LEFT, ALIGN_CENTER));
+       table->attach (*tempo_type_label, 0, 1, row+1, row+2);
+       table->attach (tempo_type, 1, 2, row+1, row + 2);
        get_vbox()->set_border_width (12);
        get_vbox()->pack_end (*table);
+
        table->show_all ();
 
        add_button (Stock::CANCEL, RESPONSE_CANCEL);
@@ -178,7 +205,11 @@ TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type,
        when_beat_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
        when_beat_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
        pulse_selector.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::pulse_change));
+       tempo_type.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::tempo_type_change));
        tap_tempo_button.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_button_press), false);
+       tap_tempo_button.signal_focus_out_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_focus_out));
+
+       tapped = false;
 }
 
 bool
@@ -243,7 +274,7 @@ double
 TempoDialog::get_note_type ()
 {
        NoteTypes::iterator x = note_types.find (pulse_selector.get_active_text());
-       
+
        if (x == note_types.end()) {
                error << string_compose(_("incomprehensible pulse note type (%1)"), pulse_selector.get_active_text()) << endmsg;
                return 0;
@@ -252,42 +283,74 @@ TempoDialog::get_note_type ()
        return x->second;
 }
 
+TempoSection::Type
+TempoDialog::get_tempo_type ()
+{
+       TempoTypes::iterator x = tempo_types.find (tempo_type.get_active_text());
+
+       if (x == tempo_types.end()) {
+               error << string_compose(_("incomprehensible tempo type (%1)"), tempo_type.get_active_text()) << endmsg;
+               return TempoSection::Type::Constant;
+       }
+
+       return x->second;
+}
+
 void
 TempoDialog::pulse_change ()
 {
        set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
 }
 
+void
+TempoDialog::tempo_type_change ()
+{
+       set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
+}
 
 bool
 TempoDialog::tap_tempo_button_press (GdkEventButton *ev)
 {
-       gint64 now;
-       now = ev->time; // milliseconds
-
-       if (last_tap > 0) {
-               double interval, bpm;
-               static const double decay = 0.5;
-
-               interval = (now - last_tap) * 1.0e-3;
-               if (interval <= 6.0) {
-                       // >= 10 bpm, say
-                       if (average_interval > 0) {
-                               average_interval = interval * decay
-                                       + average_interval * (1.0-decay);
-                       } else {
-                               average_interval = interval;
-                       }
-
-                       bpm = 60.0 / average_interval;
-                       bpm_spinner.set_value (bpm);
+       double t;
+
+       // Linear least-squares regression
+       if (tapped) {
+               t = 1e-6 * (g_get_monotonic_time () - first_t); // Subtract first_t to avoid precision problems
+
+               double n = tap_count;
+               sum_y += t;
+               sum_x += n;
+               sum_xy += n * t;
+               sum_xx += n * n;
+               double T = (sum_xy/n - sum_x/n * sum_y/n) / (sum_xx/n - sum_x/n * sum_x/n);
+
+               if (t - last_t < T / 1.2 || t - last_t > T * 1.2) {
+                       tapped = false;
                } else {
-                       average_interval = 0;
+                       bpm_spinner.set_value (60.0 / T);
                }
-       } else {
-               average_interval = 0;
        }
-       last_tap = now;
+       if (!tapped) {
+               first_t = g_get_monotonic_time ();
+               t = 0.0;
+               sum_y = 0.0;
+               sum_x = 1.0;
+               sum_xy = 0.0;
+               sum_xx = 1.0;
+               tap_count = 1.0;
+
+               tapped = true;
+       }
+       tap_count++;
+       last_t = t;
+
+       return true;
+}
+
+bool
+TempoDialog::tap_tempo_focus_out (GdkEventFocus* )
+{
+       tapped = false;
        return false;
 }
 
@@ -302,10 +365,12 @@ MeterDialog::MeterDialog (TempoMap& map, framepos_t frame, const string&)
        init (when, meter.divisions_per_bar(), meter.note_divisor(), true);
 }
 
-MeterDialog::MeterDialog (MeterSection& section, const string&)
+MeterDialog::MeterDialog (TempoMap& map, MeterSection& section, const string&)
        : ArdourDialog (_("Edit Meter"))
 {
-       init (section.start(), section.divisions_per_bar(), section.note_divisor(), section.movable());
+       Timecode::BBT_Time when;
+       map.bbt_time (map.frame_at_beat (section.beat()), when);
+       init (when, section.divisions_per_bar(), section.note_divisor(), section.movable());
 }
 
 void
@@ -318,6 +383,7 @@ MeterDialog::init (const Timecode::BBT_Time& when, double bpb, double divisor, b
        snprintf (buf, sizeof (buf), "%.2f", bpb);
        bpb_entry.set_text (buf);
        bpb_entry.select_region (0, -1);
+       bpb_entry.set_alignment (1.0);
 
        note_types.insert (make_pair (_("whole"), 1.0));
        strings.push_back (_("whole"));
@@ -346,7 +412,7 @@ MeterDialog::init (const Timecode::BBT_Time& when, double bpb, double divisor, b
                        break;
                }
        }
-       
+
        if (x == note_types.end()) {
                note_type.set_active_text (strings[3]); // "quarter"
        }
@@ -363,6 +429,7 @@ MeterDialog::init (const Timecode::BBT_Time& when, double bpb, double divisor, b
 
        snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
        when_bar_entry.set_text (buf);
+       when_bar_entry.set_alignment (1.0);
 
        if (movable) {
                Label* when_label = manage (new Label(_("Meter begins at bar:"), ALIGN_LEFT, ALIGN_CENTER));
@@ -476,7 +543,7 @@ double
 MeterDialog::get_note_type ()
 {
        NoteTypes::iterator x = note_types.find (note_type.get_active_text());
-       
+
        if (x == note_types.end()) {
                error << string_compose(_("incomprehensible meter note type (%1)"), note_type.get_active_text()) << endmsg;
                return 0;