Tempo ramps - consolidate meter dragging code.
[ardour.git] / gtk2_ardour / editor_drag.cc
index 0dedff5446d01ddb03c3d2233df776d72dab408c..16c1862dd8fee3050f04df31135274ae0d2eabc2 100644 (file)
@@ -3142,10 +3142,6 @@ MeterMarkerDrag::setup_pointer_frame_offset ()
 void
 MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
 {
-       if (!_marker->meter().movable()) {
-               //return;
-       }
-
        if (first_move) {
 
                // create a dummy marker to catch events, then hide it.
@@ -3174,10 +3170,7 @@ MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
                } else {
                        _editor->begin_reversible_command (_("copy meter mark"));
 
-                       Timecode::BBT_Time bbt;
-                       map.bbt_time (adjusted_current_frame (event, false), bbt);
-                       /* round bbt to bars */
-                       map.round_bbt (bbt, -1);
+                       Timecode::BBT_Time bbt = _real_section->bbt();
 
                        /* we can't add a meter where one currently exists */
                        if (_real_section->frame() < adjusted_current_frame (event, false)) {
@@ -3186,31 +3179,24 @@ MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
                                --bbt.bars;
                        }
                        const double beat = map.bbt_to_beats (bbt);
+                       _real_section = map.add_meter (Meter (_marker->meter().divisions_per_bar(), _marker->meter().note_divisor())
+                                                      , beat, bbt, map.frame_time (bbt), _real_section->position_lock_style());
 
-                       if (_real_section->position_lock_style() == AudioTime) {
-                               _real_section = map.add_meter (Meter (_marker->meter().divisions_per_bar(), _marker->meter().note_divisor())
-                                                              , map.frame_time (bbt), beat, bbt);
-                       } else {
-                               _real_section = map.add_meter (Meter (_marker->meter().divisions_per_bar(), _marker->meter().note_divisor()), beat, bbt);
-                       }
                }
        }
 
-       framepos_t const pf = adjusted_current_frame (event, false);
-       _marker->set_position (pf);
-       if (_marker->meter().position_lock_style() == MusicTime) {
-               TempoMap& map (_editor->session()->tempo_map());
-               Timecode::BBT_Time bbt;
-               map.bbt_time (adjusted_current_frame (event, false), bbt);
-               /* round bbt to bars */
-               map.round_bbt (bbt, -1);
-               const double beat = map.bbt_to_beats (bbt);
-               _editor->session()->tempo_map().gui_move_meter (_real_section, beat);
-       } else {
-               _editor->session()->tempo_map().gui_move_meter (_real_section, pf);
+       framepos_t pf = adjusted_current_frame (event);
+
+       if (_real_section->position_lock_style() == AudioTime && _editor->snap_musical()) {
+               /* never snap to music for audio locked */
+               pf = adjusted_current_frame (event, false);
        }
+
+       _editor->session()->tempo_map().gui_move_meter (_real_section, pf);
+
+       setup_pointer_frame_offset ();
        _marker->set_position (pf);
-       show_verbose_cursor_time (pf);
+       show_verbose_cursor_time (_real_section->frame());
 }
 
 void
@@ -3282,6 +3268,7 @@ TempoMarkerDrag::motion (GdkEvent* event, bool first_move)
        if (!_real_section->active()) {
                return;
        }
+
        if (first_move) {
 
                // mvc drag - create a dummy marker to catch events, hide it.
@@ -3301,34 +3288,93 @@ TempoMarkerDrag::motion (GdkEvent* event, bool first_move)
 
                /* use the new marker for the grab */
                swap_grab (&_marker->the_item(), 0, GDK_CURRENT_TIME);
+               _marker->hide();
 
+               TempoMap& map (_editor->session()->tempo_map());
+               /* get current state */
+               before_state = &map.get_state();
                if (!_copy) {
                        _editor->begin_reversible_command (_("move tempo mark"));
-                       TempoMap& map (_editor->session()->tempo_map());
-                       /* get current state */
-                       before_state = &map.get_state();
+               } else {
+                       _editor->begin_reversible_command (_("copy tempo mark"));
+                       framepos_t frame;
+                       bool use_snap = false;
+
+                       if (!_editor->snap_musical()) {
+                               frame = adjusted_current_frame (event);
+                       } else {
+                               frame = adjusted_current_frame (event, false);
+                               if (ArdourKeyboard::indicates_snap (event->button.state)) {
+                                       if (_editor->snap_mode() == Editing::SnapOff) {
+                                               use_snap = true;
+                                       } else {
+                                               use_snap = false;
+                                       }
+                               } else {
+                                       if (_editor->snap_mode() == Editing::SnapOff) {
+                                               use_snap = false;
+                                       } else {
+                                               use_snap = true;
+                                       }
+                               }
+                       }
+
+                       Timecode::BBT_Time bbt;
+                       map.bbt_time (frame, bbt);
+
+                       /* add new tempo section to map, ensuring we don't refer to existing tempos for snap */
+
+                       if (_real_section->position_lock_style() == MusicTime) {
+                               if (use_snap && _editor->snap_type() == SnapToBar) {
+                                       map.round_bbt (bbt, -1, (frame > _real_section->frame()) ? RoundUpMaybe : RoundDownMaybe);
+                               } else if (use_snap) {
+                                       map.round_bbt (bbt, _editor->get_grid_beat_divisions (0), RoundNearest);
+                               }
+                               double const pulse = map.predict_tempo_position (_real_section, bbt).first;
+                               _real_section = map.add_tempo (_marker->tempo(), pulse, 0, _real_section->type(), MusicTime);
+                       } else {
+                               if (use_snap && _editor->snap_type() == SnapToBar) {
+                                       map.round_bbt (bbt, -1, (frame > _real_section->frame()) ? RoundUpMaybe : RoundDownMaybe);
+                               } else if (use_snap) {
+                                       map.round_bbt (bbt, _editor->get_grid_beat_divisions (0), RoundNearest);
+                               }
+                               if (use_snap) {
+                                       frame = map.predict_tempo_position (_real_section, bbt).second;
+                               }
+                               _real_section = map.add_tempo (_marker->tempo(), 0.0, frame, _real_section->type(), AudioTime);
+                       }
                }
-               _marker->hide();
+
        }
 
        framepos_t pf;
-       Tempo const tp = _marker->tempo();
 
-       if (Keyboard::modifier_state_equals (event->button.state, ArdourKeyboard::constraint_modifier ())) {
+       if (Keyboard::modifier_state_contains (event->button.state, ArdourKeyboard::constraint_modifier ())) {
+               /* use vertical movement to alter tempo .. should be log */
                double new_bpm = _real_section->beats_per_minute() + ((last_pointer_y() - current_pointer_y()) / 5.0);
-               _editor->session()->tempo_map().gui_change_tempo (_real_section, Tempo (new_bpm, _real_section->note_type()));
                stringstream strs;
+
+               _editor->session()->tempo_map().gui_change_tempo (_real_section, Tempo (new_bpm, _real_section->note_type()));
                strs << new_bpm;
                show_verbose_cursor_text (strs.str());
-       } else if (_movable) {
+
+       } else if (_movable && !_real_section->locked_to_meter()) {
+
                if (!_editor->snap_musical()) {
+                       /* snap normally (this is not self-referential).*/
                        pf = adjusted_current_frame (event);
+
                } else {
-                       bool use_snap;
+                       /* but this is.
+                          we can't use the map for anything related to tempo,
+                          so we round bbt using meters, which have no dependency
+                          on pulse for this kind of thing.
+                       */
                        TempoMap& map (_editor->session()->tempo_map());
+                       Timecode::BBT_Time when;
+                       bool use_snap;
 
-                       pf = adjusted_current_frame (event, false);
-                       if (Keyboard::modifier_state_equals (event->button.state, ArdourKeyboard::snap_modifier ())) {
+                       if (ArdourKeyboard::indicates_snap (event->button.state)) {
                                if (_editor->snap_mode() == Editing::SnapOff) {
                                        use_snap = true;
                                } else {
@@ -3342,35 +3388,24 @@ TempoMarkerDrag::motion (GdkEvent* event, bool first_move)
                                }
                        }
 
-                       Timecode::BBT_Time when;
+                       pf = adjusted_current_frame (event);
                        map.bbt_time (pf, when);
 
-                       if (_real_section->position_lock_style() == MusicTime) {
+                       if (use_snap && _editor->snap_type() == SnapToBar) {
+                               map.round_bbt (when, -1, (pf > _real_section->frame()) ? RoundUpMaybe : RoundDownMaybe);
 
-                               const double pulse = map.predict_tempo_pulse (_real_section, pf);
-                               when = map.pulse_to_bbt (pulse);
-                               if (use_snap && _editor->snap_type() == SnapToBar) {
-                                       map.round_bbt (when, -1);
-                               } else if (use_snap) {
-                                       map.round_bbt (when, _editor->get_grid_beat_divisions (0));
-                               }
-                               const double beat = map.bbt_to_beats (when);
-                               map.gui_move_tempo_beat (_real_section, beat);
-                       } else {
-                               if (use_snap && _editor->snap_type() == SnapToBar) {
-                                       map.round_bbt (when, -1);
-                               } else if (use_snap) {
-                                       map.round_bbt (when, _editor->get_grid_beat_divisions (0));
-                               }
-                               if (use_snap) {
-                                       pf = map.predict_tempo_frame (_real_section, when);
-                               }
-                               map.gui_move_tempo_frame (_real_section, pf);
                        }
+                       const pair<double, framepos_t> future_pos = map.predict_tempo_position (_real_section, when);
+                       map.gui_move_tempo (_real_section, future_pos);
+
                }
 
                show_verbose_cursor_time (_real_section->frame());
        }
+
+       /* this has moved the bar lines themselves, so recalibrate the offset */
+       setup_pointer_frame_offset();
+
        _marker->set_position (pf);
 }
 
@@ -3387,38 +3422,11 @@ TempoMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
                return;
        }
 
-
        TempoMap& map (_editor->session()->tempo_map());
 
-       if (_copy == true) {
-               _editor->begin_reversible_command (_("copy tempo mark"));
-               XMLNode &before = map.get_state();
-
-               if (_marker->tempo().position_lock_style() == MusicTime) {
-                       double const pulse = map.predict_tempo_pulse (_real_section, _real_section->frame());
-                       map.add_tempo (_marker->tempo(), pulse, _marker->tempo().type());
-               } else {
-                       map.add_tempo (_marker->tempo(), _real_section->frame(), _marker->tempo().type());
-               }
-
-               XMLNode &after = map.get_state();
-               _editor->session()->add_command (new MementoCommand<TempoMap>(map, &before, &after));
-               _editor->commit_reversible_command ();
-
-       } else {
-               if (_marker->tempo().position_lock_style() == MusicTime) {
-                       double const pulse = map.predict_tempo_pulse (_real_section, _real_section->frame());
-                       map.replace_tempo (*_real_section, Tempo (_real_section->beats_per_minute(), _real_section->note_type())
-                                          , pulse, _marker->tempo().type());
-               } else {
-                       map.replace_tempo (*_real_section, Tempo (_real_section->beats_per_minute(), _real_section->note_type())
-                                          , _real_section->frame(), _marker->tempo().type());
-               }
-
-               XMLNode &after = map.get_state();
-               _editor->session()->add_command (new MementoCommand<TempoMap>(map, before_state, &after));
-               _editor->commit_reversible_command ();
-       }
+       XMLNode &after = map.get_state();
+       _editor->session()->add_command (new MementoCommand<TempoMap>(map, before_state, &after));
+       _editor->commit_reversible_command ();
 
        // delete the dummy marker we used for visual representation while moving.
        // a new visual marker will show up automatically.
@@ -3431,18 +3439,104 @@ TempoMarkerDrag::aborted (bool moved)
        _marker->set_position (_marker->tempo().frame());
        if (moved) {
                TempoMap& map (_editor->session()->tempo_map());
-               /* we removed it before, so add it back now */
-               if (_marker->tempo().position_lock_style() == MusicTime) {
-                       map.add_tempo (_marker->tempo(), _marker->tempo().pulse(), _marker->tempo().type());
-               } else {
-                       map.add_tempo (_marker->tempo(), _marker->tempo().frame(), _marker->tempo().type());
-               }
+               map.set_state (*before_state, Stateful::current_state_version);
                // delete the dummy marker we used for visual representation while moving.
                // a new visual marker will show up automatically.
                delete _marker;
        }
 }
 
+BBTRulerDrag::BBTRulerDrag (Editor* e, ArdourCanvas::Item* i)
+       : Drag (e, i)
+       , _pulse (0.0)
+       , _beat (0.0)
+       , _tempo (0)
+       , before_state (0)
+{
+       DEBUG_TRACE (DEBUG::Drags, "New BBTRulerDrag\n");
+
+}
+
+void
+BBTRulerDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
+{
+       Drag::start_grab (event, cursor);
+
+       TempoMap& map (_editor->session()->tempo_map());
+       ostringstream sstr;
+
+       _tempo = const_cast<TempoSection*> (&map.tempo_section_at (raw_grab_frame()));
+       sstr << "^" << fixed << setprecision(3) << map.tempo_at (adjusted_current_frame (event)).beats_per_minute() << "\n";
+       sstr << "<" << fixed << setprecision(3) << _tempo->beats_per_minute();
+       show_verbose_cursor_text (sstr.str());
+       finished (event, false);
+}
+
+void
+BBTRulerDrag::setup_pointer_frame_offset ()
+{
+       TempoMap& map (_editor->session()->tempo_map());
+       const double beat_at_frame = map.beat_at_frame (raw_grab_frame());
+       const uint32_t divisions = _editor->get_grid_beat_divisions (0);
+       if (divisions > 0) {
+               _beat = floor (beat_at_frame) + (floor (((beat_at_frame - floor (beat_at_frame)) * divisions)) / divisions);
+       } else {
+               /* while it makes some sense for the user to determine the division to 'grab',
+                  grabbing a bar often leads to confusing results wrt the actual tempo section being altered
+                  and the result over steep tempo curves. Use sixteenths.
+               */
+               _beat = floor (beat_at_frame) + (floor (((beat_at_frame - floor (beat_at_frame)) * 4)) / 4);
+       }
+       _pulse = map.pulse_at_beat (_beat);
+       _pointer_frame_offset = raw_grab_frame() - map.frame_at_beat (_beat);
+}
+
+void
+BBTRulerDrag::motion (GdkEvent* event, bool first_move)
+{
+       TempoMap& map (_editor->session()->tempo_map());
+
+       if (first_move) {
+               /* get current state */
+               before_state = &map.get_state();
+               _editor->begin_reversible_command (_("dilate tempo"));
+       }
+
+       framepos_t const pf = adjusted_current_frame (event, false);
+
+       if (Keyboard::modifier_state_contains (event->button.state, ArdourKeyboard::constraint_modifier())) {
+               /* adjust previous tempo to match pointer frame */
+               _editor->session()->tempo_map().gui_dilate_tempo (_tempo, map.frame_at_pulse (_pulse), pf, _pulse);
+       }
+       ostringstream sstr;
+       sstr << "^" << fixed << setprecision(3) << map.tempo_at (pf).beats_per_minute() << "\n";
+       sstr << "<" << fixed << setprecision(3) << _tempo->beats_per_minute();
+       show_verbose_cursor_text (sstr.str());
+}
+
+void
+BBTRulerDrag::finished (GdkEvent* event, bool movement_occurred)
+{
+       if (!movement_occurred) {
+               return;
+       }
+
+       TempoMap& map (_editor->session()->tempo_map());
+
+       XMLNode &after = map.get_state();
+       _editor->session()->add_command(new MementoCommand<TempoMap>(map, before_state, &after));
+       _editor->commit_reversible_command ();
+}
+
+void
+BBTRulerDrag::aborted (bool moved)
+{
+       if (moved) {
+               _editor->session()->tempo_map().set_state (*before_state, Stateful::current_state_version);
+       }
+}
+
+
 CursorDrag::CursorDrag (Editor* e, EditorCursor& c, bool s)
        : Drag (e, &c.track_canvas_item(), false)
        , _cursor (c)