Use an enum for RoundMode instead of magic numbers.
authorDavid Robillard <d@drobilla.net>
Sun, 16 Nov 2014 06:05:21 +0000 (01:05 -0500)
committerDavid Robillard <d@drobilla.net>
Mon, 17 Nov 2014 03:35:45 +0000 (22:35 -0500)
No functional changes in this one (for easier auditing), but towards having
round up/down only if necessary modes, rather than kludging around that
situation with a double round as we do currently.

14 files changed:
gtk2_ardour/editor.cc
gtk2_ardour/editor.h
gtk2_ardour/editor_drag.cc
gtk2_ardour/editor_mouse.cc
gtk2_ardour/editor_ops.cc
gtk2_ardour/midi_region_view.cc
gtk2_ardour/midi_time_axis.cc
gtk2_ardour/public_editor.h
gtk2_ardour/region_view.cc
gtk2_ardour/step_editor.cc
gtk2_ardour/tempo_dialog.cc
libs/ardour/ardour/tempo.h
libs/ardour/ardour/types.h
libs/ardour/tempo.cc

index da5dcb2df5f67483f2c337ef799a89b4a4a6189a..ac59ecab4fda1e0df317e4400a83fd26f00be2e9 100644 (file)
@@ -2553,7 +2553,7 @@ Editor::trackview_by_y_position (double y, bool trackview_relative_offset) const
  *  @param event Event to get current key modifier information from, or 0.
  */
 void
-Editor::snap_to_with_modifier (framepos_t& start, GdkEvent const * event, int32_t direction, bool for_mark)
+Editor::snap_to_with_modifier (framepos_t& start, GdkEvent const * event, RoundMode direction, bool for_mark)
 {
        if (!_session || !event) {
                return;
@@ -2571,7 +2571,7 @@ Editor::snap_to_with_modifier (framepos_t& start, GdkEvent const * event, int32_
 }
 
 void
-Editor::snap_to (framepos_t& start, int32_t direction, bool for_mark)
+Editor::snap_to (framepos_t& start, RoundMode direction, bool for_mark)
 {
        if (!_session || _snap_mode == SnapOff) {
                return;
@@ -2581,7 +2581,7 @@ Editor::snap_to (framepos_t& start, int32_t direction, bool for_mark)
 }
 
 void
-Editor::timecode_snap_to_internal (framepos_t& start, int32_t direction, bool /*for_mark*/)
+Editor::timecode_snap_to_internal (framepos_t& start, RoundMode direction, bool /*for_mark*/)
 {
        const framepos_t one_timecode_second = (framepos_t)(rint(_session->timecode_frames_per_second()) * _session->frames_per_timecode_frame());
        framepos_t one_timecode_minute = (framepos_t)(rint(_session->timecode_frames_per_second()) * _session->frames_per_timecode_frame() * 60);
@@ -2638,7 +2638,7 @@ Editor::timecode_snap_to_internal (framepos_t& start, int32_t direction, bool /*
 }
 
 void
-Editor::snap_to_internal (framepos_t& start, int32_t direction, bool for_mark)
+Editor::snap_to_internal (framepos_t& start, RoundMode direction, bool for_mark)
 {
        const framepos_t one_second = _session->frame_rate();
        const framepos_t one_minute = _session->frame_rate() * 60;
index 2831fc894fb8cb47ed4062c9972c48177ce9915e..09b81c4e64449a5583c9e3392e63e22b5a67a777 100644 (file)
@@ -412,9 +412,19 @@ class Editor : public PublicEditor, public PBD::ScopedConnectionList, public ARD
        Gtkmm2ext::TearOff* mouse_mode_tearoff () const { return _mouse_mode_tearoff; }
        Gtkmm2ext::TearOff* tools_tearoff () const { return _tools_tearoff; }
 
-       void snap_to (framepos_t& first, int32_t direction = 0, bool for_mark = false);
-       void snap_to_with_modifier (framepos_t& first, GdkEvent const *, int32_t direction = 0, bool for_mark = false);
-       void snap_to (framepos_t& first, framepos_t& last, int32_t direction = 0, bool for_mark = false);
+       void snap_to (framepos_t&       first,
+                     ARDOUR::RoundMode direction = ARDOUR::RoundNearest,
+                     bool              for_mark  = false);
+
+       void snap_to_with_modifier (framepos_t&       first,
+                                   GdkEvent const *  ev,
+                                   ARDOUR::RoundMode direction = ARDOUR::RoundNearest,
+                                   bool              for_mark  = false);
+
+       void snap_to (framepos_t&       first,
+                     framepos_t&       last,
+                     ARDOUR::RoundMode direction = ARDOUR::RoundNearest,
+                     bool              for_mark  = false);
 
        void begin_reversible_command (std::string cmd_name);
        void begin_reversible_command (GQuark);
@@ -2018,8 +2028,13 @@ class Editor : public PublicEditor, public PBD::ScopedConnectionList, public ARD
        void select_next_route ();
        void select_prev_route ();
 
-       void snap_to_internal (framepos_t& first, int32_t direction = 0, bool for_mark = false);
-       void timecode_snap_to_internal (framepos_t& first, int32_t direction = 0, bool for_mark = false);
+       void snap_to_internal (framepos_t&       first,
+                              ARDOUR::RoundMode direction = ARDOUR::RoundNearest,
+                              bool              for_mark  = false);
+
+       void timecode_snap_to_internal (framepos_t&       first,
+                                       ARDOUR::RoundMode direction = ARDOUR::RoundNearest,
+                                       bool              for_mark  = false);
 
        RhythmFerret* rhythm_ferret;
 
index 1237c73c3dc2f83dccffca9107b9cb233f766c4b..9df013264fa580ca3237c727b63163c97c23f949 100644 (file)
@@ -2764,7 +2764,7 @@ TempoMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
        motion (event, false);
 
        TempoMap& map (_editor->session()->tempo_map());
-       framepos_t beat_time = map.round_to_beat (last_pointer_frame(), 0);
+       framepos_t beat_time = map.round_to_beat (last_pointer_frame(), RoundNearest);
        Timecode::BBT_Time when;
 
        map.bbt_time (beat_time, when);
@@ -3401,7 +3401,7 @@ MarkerDrag::motion (GdkEvent* event, bool)
                                } else  if (new_start < copy_location->end()) {
                                        copy_location->set_start (new_start);
                                } else if (newframe > 0) {
-                                       _editor->snap_to (next, 1, true);
+                                       _editor->snap_to (next, RoundUpAlways, true);
                                        copy_location->set_end (next);
                                        copy_location->set_start (newframe);
                                }
@@ -3414,7 +3414,7 @@ MarkerDrag::motion (GdkEvent* event, bool)
                                } else if (new_end > copy_location->start()) {
                                        copy_location->set_end (new_end);
                                } else if (newframe > 0) {
-                                       _editor->snap_to (next, -1, true);
+                                       _editor->snap_to (next, RoundDownAlways, true);
                                        copy_location->set_start (next);
                                        copy_location->set_end (newframe);
                                }
@@ -4221,9 +4221,9 @@ SelectionDrag::motion (GdkEvent* event, bool first_move)
                if (first_move) {
                        grab = adjusted_current_frame (event, false);
                        if (grab < pending_position) {
-                               _editor->snap_to (grab, -1);
+                               _editor->snap_to (grab, RoundDownAlways);
                        }  else {
-                               _editor->snap_to (grab, 1);
+                               _editor->snap_to (grab, RoundUpAlways);
                        }
                }
 
index 3ad9f0a54c50e58b23ff5520ab5aea7a83535e92..f7a7855eaada998d8a0ace19d6653c647e10aca8 100644 (file)
@@ -1521,7 +1521,7 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
 
                case MarkerBarItem:
                        if (!_dragging_playhead) {
-                               snap_to_with_modifier (where, event, 0, true);
+                               snap_to_with_modifier (where, event, RoundNearest, true);
                                mouse_add_new_marker (where);
                        }
                        return true;
@@ -1529,7 +1529,7 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
                case CdMarkerBarItem:
                        if (!_dragging_playhead) {
                                // if we get here then a dragged range wasn't done
-                               snap_to_with_modifier (where, event, 0, true);
+                               snap_to_with_modifier (where, event, RoundNearest, true);
                                mouse_add_new_marker (where, true);
                        }
                        return true;
index f6ec077301ff5585e0f8db52d3c5729e027817e4..3d5ae6cb14877324531d3b93e04942242f1f7241 100644 (file)
@@ -6463,7 +6463,7 @@ Editor::playhead_forward_to_grid ()
        framepos_t pos = playhead_cursor->current_frame ();
        if (pos < max_framepos - 1) {
                pos += 2;
-               snap_to_internal (pos, 1, false);
+               snap_to_internal (pos, RoundUpAlways, false);
                _session->request_locate (pos);
        }
 }
@@ -6479,7 +6479,7 @@ Editor::playhead_backward_to_grid ()
        framepos_t pos = playhead_cursor->current_frame ();
        if (pos > 2) {
                pos -= 2;
-               snap_to_internal (pos, -1, false);
+               snap_to_internal (pos, RoundDownAlways, false);
                _session->request_locate (pos);
        }
 }
index a620154866009ec74f2bdde601d86b176427b967..d519bd1a0485f1654d078fb0f262fbd366dd191c 100644 (file)
@@ -3087,7 +3087,7 @@ MidiRegionView::nudge_notes (bool forward)
                        next_pos -= 1;
                }
 
-               trackview.editor().snap_to (next_pos, (forward ? 1 : -1), false);
+               trackview.editor().snap_to (next_pos, (forward ? RoundUpAlways : RoundDownAlways), false);
                distance = ref_point - next_pos;
        }
 
index 772baf834f776019eeded00d2bf196122e4c2c8e..4d55f2e8e1a085ab42694b2e42e2dfd6b8333f96 100644 (file)
@@ -1587,7 +1587,7 @@ MidiTimeAxisView::add_region (framepos_t pos, framecnt_t length, bool commit)
        real_editor->begin_reversible_command (Operations::create_region);
        playlist()->clear_changes ();
 
-       real_editor->snap_to (pos, 0);
+       real_editor->snap_to (pos, RoundNearest);
 
        boost::shared_ptr<Source> src = _session->create_midi_source_by_stealing_name (view()->trackview().track());
        PropertyList plist;
index 4bf03bc72f68a8de37303920d6b531b1d864ee8a..9969d9d49e63dcdb13c2e57e525b60d28d8551ae 100644 (file)
@@ -141,7 +141,9 @@ class PublicEditor : public Gtk::Window, public PBD::StatefulDestructible, publi
        virtual void set_snap_threshold (double t) = 0;
 
        /** Snap a value according to the current snap setting. */
-       virtual void snap_to (framepos_t& first, int32_t direction = 0, bool for_mark = false) = 0;
+       virtual void snap_to (framepos_t&       first,
+                             ARDOUR::RoundMode direction = ARDOUR::RoundNearest,
+                             bool              for_mark  = false) = 0;
 
        /** Undo some transactions.
         * @param n Number of transactions to undo.
@@ -411,7 +413,10 @@ class PublicEditor : public Gtk::Window, public PBD::StatefulDestructible, publi
        virtual ARDOUR::Location* find_location_from_marker (Marker *, bool &) const = 0;
        virtual Marker* find_marker_from_location_id (PBD::ID const &, bool) const = 0;
 
-       virtual void snap_to_with_modifier (framepos_t &, GdkEvent const *, int32_t direction = 0, bool for_mark = false) = 0;
+       virtual void snap_to_with_modifier (framepos_t &      first,
+                                           GdkEvent const *  ev,
+                                           ARDOUR::RoundMode direction = ARDOUR::RoundNearest,
+                                           bool              for_mark  = false) = 0;
 
        virtual void get_regions_at (RegionSelection &, framepos_t where, TrackViewList const &) const = 0;
        virtual RegionSelection get_regions_from_selection_and_mouse (framepos_t) = 0;
index 4e59137957cab07c3effd687de45caf2173a46d8..49b47332ae1239e2e9171f434355d42d129163b6 100644 (file)
@@ -963,12 +963,12 @@ RegionView::snap_frame_to_frame (frameoffset_t x) const
 
        /* try a snap in either direction */
        framepos_t frame = session_frame;
-       editor.snap_to (frame, 0);
+       editor.snap_to (frame, RoundNearest);
 
        /* if we went off the beginning of the region, snap forwards */
        if (frame < _region->position ()) {
                frame = session_frame;
-               editor.snap_to (frame, 1);
+               editor.snap_to (frame, RoundUpAlways);
        }
 
        /* back to region relative */
index 4665da348b0079750b698937d300011329317eaa..ef3caad5d1e3219b53bf2b3856cd067e6d53b9d1 100644 (file)
@@ -398,7 +398,7 @@ StepEditor::step_edit_bar_sync ()
         }
 
         framepos_t fpos = step_edit_region_view->region_beats_to_absolute_frames (step_edit_beat_pos);
-        fpos = _session->tempo_map().round_to_bar (fpos, 1);
+        fpos = _session->tempo_map().round_to_bar (fpos, RoundUpAlways);
         step_edit_beat_pos = ceil (step_edit_region_view->region_frames_to_region_beats (fpos - step_edit_region->position()));
         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
 }
index a96958136910827123c3630fa8240e5db65627df..5671ae4dda3b453f9b602129dfa7e5e05213887d 100644 (file)
@@ -289,7 +289,7 @@ MeterDialog::MeterDialog (TempoMap& map, framepos_t frame, const string&)
        : ArdourDialog (_("New Meter"))
 {
        Timecode::BBT_Time when;
-       frame = map.round_to_bar(frame,0);
+       frame = map.round_to_bar(frame, RoundNearest);
        Meter meter (map.meter_at(frame));
 
        map.bbt_time (frame, when);
index 8fa5ed45a0242104f51d9fe78bafee09ae3d687f..15cd1662f03af174ab450488b362546742e8eaa8 100644 (file)
@@ -297,10 +297,9 @@ class LIBARDOUR_API TempoMap : public PBD::StatefulDestructible
        void replace_tempo (const TempoSection&, const Tempo&, const Timecode::BBT_Time& where);
        void replace_meter (const MeterSection&, const Meter&, const Timecode::BBT_Time& where);
 
-       framepos_t round_to_bar  (framepos_t frame, int dir);
-       framepos_t round_to_beat (framepos_t frame, int dir);
-       framepos_t round_to_beat_subdivision (framepos_t fr, int sub_num, int dir);
-       framepos_t round_to_tick (framepos_t frame, int dir);
+       framepos_t round_to_bar  (framepos_t frame, RoundMode dir);
+       framepos_t round_to_beat (framepos_t frame, RoundMode dir);
+       framepos_t round_to_beat_subdivision (framepos_t fr, int sub_num, RoundMode dir);
 
        void set_length (framepos_t frames);
 
@@ -355,7 +354,7 @@ class LIBARDOUR_API TempoMap : public PBD::StatefulDestructible
        BBTPointList::const_iterator bbt_before_or_at (const Timecode::BBT_Time&);
        BBTPointList::const_iterator bbt_after_or_at (framepos_t);
        
-       framepos_t round_to_type (framepos_t fr, int dir, BBTPointType);
+       framepos_t round_to_type (framepos_t fr, RoundMode dir, BBTPointType);
        void bbt_time (framepos_t, Timecode::BBT_Time&, const BBTPointList::const_iterator&);
        framecnt_t bbt_duration_at_unlocked (const Timecode::BBT_Time& when, const Timecode::BBT_Time& bbt, int dir);
        
index 165afd0dd9b2f72fc39f51db37f3ebf0a5be92c4..1b0ba5ea3c0113b572c2e4d22686abeb078f3d22 100644 (file)
@@ -216,6 +216,12 @@ namespace ARDOUR {
                TrackColor
        };
 
+       enum RoundMode {
+               RoundDownAlways = -1,  ///< Always round down, even if on a division
+               RoundNearest    = 0,   ///< Round to nearest
+               RoundUpAlways   = 1    ///< Always round up, even if on a division
+       };
+
        class AnyTime {
        public:
                enum Type {
index 6f3f7341160dd6e84cf33e9720131fc49b1c7c81..77128bd0c2482fcc8690cfc8ca4cbf12b6428309 100644 (file)
@@ -1223,19 +1223,19 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
 }
 
 framepos_t
-TempoMap::round_to_bar (framepos_t fr, int dir)
+TempoMap::round_to_bar (framepos_t fr, RoundMode dir)
 {
        return round_to_type (fr, dir, Bar);
 }
 
 framepos_t
-TempoMap::round_to_beat (framepos_t fr, int dir)
+TempoMap::round_to_beat (framepos_t fr, RoundMode dir)
 {
        return round_to_type (fr, dir, Beat);
 }
 
 framepos_t
-TempoMap::round_to_beat_subdivision (framepos_t fr, int sub_num, int dir)
+TempoMap::round_to_beat_subdivision (framepos_t fr, int sub_num, RoundMode dir)
 {
        require_map_to (fr);
 
@@ -1354,7 +1354,7 @@ TempoMap::round_to_beat_subdivision (framepos_t fr, int sub_num, int dir)
 }
 
 framepos_t
-TempoMap::round_to_type (framepos_t frame, int dir, BBTPointType type)
+TempoMap::round_to_type (framepos_t frame, RoundMode dir, BBTPointType type)
 {
        require_map_to (frame);