Use an enum for RoundMode instead of magic numbers.

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.
This commit is contained in:
David Robillard 2014-11-16 01:05:21 -05:00
parent 9c5e63bcc6
commit fd9ccc7058
14 changed files with 60 additions and 35 deletions

View 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. * @param event Event to get current key modifier information from, or 0.
*/ */
void 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) { if (!_session || !event) {
return; return;
@ -2571,7 +2571,7 @@ Editor::snap_to_with_modifier (framepos_t& start, GdkEvent const * event, int32_
} }
void 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) { if (!_session || _snap_mode == SnapOff) {
return; return;
@ -2581,7 +2581,7 @@ Editor::snap_to (framepos_t& start, int32_t direction, bool for_mark)
} }
void 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()); 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); 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 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_second = _session->frame_rate();
const framepos_t one_minute = _session->frame_rate() * 60; const framepos_t one_minute = _session->frame_rate() * 60;

View 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* mouse_mode_tearoff () const { return _mouse_mode_tearoff; }
Gtkmm2ext::TearOff* tools_tearoff () const { return _tools_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 (framepos_t& first,
void snap_to_with_modifier (framepos_t& first, GdkEvent const *, int32_t direction = 0, bool for_mark = false); ARDOUR::RoundMode direction = ARDOUR::RoundNearest,
void snap_to (framepos_t& first, framepos_t& last, int32_t direction = 0, bool for_mark = false); 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 (std::string cmd_name);
void begin_reversible_command (GQuark); void begin_reversible_command (GQuark);
@ -2018,8 +2028,13 @@ class Editor : public PublicEditor, public PBD::ScopedConnectionList, public ARD
void select_next_route (); void select_next_route ();
void select_prev_route (); void select_prev_route ();
void snap_to_internal (framepos_t& first, int32_t direction = 0, bool for_mark = false); void snap_to_internal (framepos_t& first,
void timecode_snap_to_internal (framepos_t& first, int32_t direction = 0, bool for_mark = false); 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; RhythmFerret* rhythm_ferret;

View file

@ -2764,7 +2764,7 @@ TempoMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
motion (event, false); motion (event, false);
TempoMap& map (_editor->session()->tempo_map()); 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; Timecode::BBT_Time when;
map.bbt_time (beat_time, when); map.bbt_time (beat_time, when);
@ -3401,7 +3401,7 @@ MarkerDrag::motion (GdkEvent* event, bool)
} else if (new_start < copy_location->end()) { } else if (new_start < copy_location->end()) {
copy_location->set_start (new_start); copy_location->set_start (new_start);
} else if (newframe > 0) { } else if (newframe > 0) {
_editor->snap_to (next, 1, true); _editor->snap_to (next, RoundUpAlways, true);
copy_location->set_end (next); copy_location->set_end (next);
copy_location->set_start (newframe); copy_location->set_start (newframe);
} }
@ -3414,7 +3414,7 @@ MarkerDrag::motion (GdkEvent* event, bool)
} else if (new_end > copy_location->start()) { } else if (new_end > copy_location->start()) {
copy_location->set_end (new_end); copy_location->set_end (new_end);
} else if (newframe > 0) { } else if (newframe > 0) {
_editor->snap_to (next, -1, true); _editor->snap_to (next, RoundDownAlways, true);
copy_location->set_start (next); copy_location->set_start (next);
copy_location->set_end (newframe); copy_location->set_end (newframe);
} }
@ -4221,9 +4221,9 @@ SelectionDrag::motion (GdkEvent* event, bool first_move)
if (first_move) { if (first_move) {
grab = adjusted_current_frame (event, false); grab = adjusted_current_frame (event, false);
if (grab < pending_position) { if (grab < pending_position) {
_editor->snap_to (grab, -1); _editor->snap_to (grab, RoundDownAlways);
} else { } else {
_editor->snap_to (grab, 1); _editor->snap_to (grab, RoundUpAlways);
} }
} }

View file

@ -1521,7 +1521,7 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
case MarkerBarItem: case MarkerBarItem:
if (!_dragging_playhead) { if (!_dragging_playhead) {
snap_to_with_modifier (where, event, 0, true); snap_to_with_modifier (where, event, RoundNearest, true);
mouse_add_new_marker (where); mouse_add_new_marker (where);
} }
return true; return true;
@ -1529,7 +1529,7 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
case CdMarkerBarItem: case CdMarkerBarItem:
if (!_dragging_playhead) { if (!_dragging_playhead) {
// if we get here then a dragged range wasn't done // 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); mouse_add_new_marker (where, true);
} }
return true; return true;

View file

@ -6463,7 +6463,7 @@ Editor::playhead_forward_to_grid ()
framepos_t pos = playhead_cursor->current_frame (); framepos_t pos = playhead_cursor->current_frame ();
if (pos < max_framepos - 1) { if (pos < max_framepos - 1) {
pos += 2; pos += 2;
snap_to_internal (pos, 1, false); snap_to_internal (pos, RoundUpAlways, false);
_session->request_locate (pos); _session->request_locate (pos);
} }
} }
@ -6479,7 +6479,7 @@ Editor::playhead_backward_to_grid ()
framepos_t pos = playhead_cursor->current_frame (); framepos_t pos = playhead_cursor->current_frame ();
if (pos > 2) { if (pos > 2) {
pos -= 2; pos -= 2;
snap_to_internal (pos, -1, false); snap_to_internal (pos, RoundDownAlways, false);
_session->request_locate (pos); _session->request_locate (pos);
} }
} }

View file

@ -3087,7 +3087,7 @@ MidiRegionView::nudge_notes (bool forward)
next_pos -= 1; 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; distance = ref_point - next_pos;
} }

View file

@ -1587,7 +1587,7 @@ MidiTimeAxisView::add_region (framepos_t pos, framecnt_t length, bool commit)
real_editor->begin_reversible_command (Operations::create_region); real_editor->begin_reversible_command (Operations::create_region);
playlist()->clear_changes (); 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()); boost::shared_ptr<Source> src = _session->create_midi_source_by_stealing_name (view()->trackview().track());
PropertyList plist; PropertyList plist;

View file

@ -141,7 +141,9 @@ class PublicEditor : public Gtk::Window, public PBD::StatefulDestructible, publi
virtual void set_snap_threshold (double t) = 0; virtual void set_snap_threshold (double t) = 0;
/** Snap a value according to the current snap setting. */ /** 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. /** Undo some transactions.
* @param n Number of transactions to undo. * @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 ARDOUR::Location* find_location_from_marker (Marker *, bool &) const = 0;
virtual Marker* find_marker_from_location_id (PBD::ID const &, 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 void get_regions_at (RegionSelection &, framepos_t where, TrackViewList const &) const = 0;
virtual RegionSelection get_regions_from_selection_and_mouse (framepos_t) = 0; virtual RegionSelection get_regions_from_selection_and_mouse (framepos_t) = 0;

View file

@ -963,12 +963,12 @@ RegionView::snap_frame_to_frame (frameoffset_t x) const
/* try a snap in either direction */ /* try a snap in either direction */
framepos_t frame = session_frame; 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 we went off the beginning of the region, snap forwards */
if (frame < _region->position ()) { if (frame < _region->position ()) {
frame = session_frame; frame = session_frame;
editor.snap_to (frame, 1); editor.snap_to (frame, RoundUpAlways);
} }
/* back to region relative */ /* back to region relative */

View 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); 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_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); step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
} }

View file

@ -289,7 +289,7 @@ MeterDialog::MeterDialog (TempoMap& map, framepos_t frame, const string&)
: ArdourDialog (_("New Meter")) : ArdourDialog (_("New Meter"))
{ {
Timecode::BBT_Time when; Timecode::BBT_Time when;
frame = map.round_to_bar(frame,0); frame = map.round_to_bar(frame, RoundNearest);
Meter meter (map.meter_at(frame)); Meter meter (map.meter_at(frame));
map.bbt_time (frame, when); map.bbt_time (frame, when);

View 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_tempo (const TempoSection&, const Tempo&, const Timecode::BBT_Time& where);
void replace_meter (const MeterSection&, const Meter&, 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_bar (framepos_t frame, RoundMode dir);
framepos_t round_to_beat (framepos_t frame, int dir); framepos_t round_to_beat (framepos_t frame, RoundMode dir);
framepos_t round_to_beat_subdivision (framepos_t fr, int sub_num, int dir); framepos_t round_to_beat_subdivision (framepos_t fr, int sub_num, RoundMode dir);
framepos_t round_to_tick (framepos_t frame, int dir);
void set_length (framepos_t frames); 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_before_or_at (const Timecode::BBT_Time&);
BBTPointList::const_iterator bbt_after_or_at (framepos_t); 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&); 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); framecnt_t bbt_duration_at_unlocked (const Timecode::BBT_Time& when, const Timecode::BBT_Time& bbt, int dir);

View file

@ -216,6 +216,12 @@ namespace ARDOUR {
TrackColor 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 { class AnyTime {
public: public:
enum Type { enum Type {

View file

@ -1223,19 +1223,19 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
} }
framepos_t 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); return round_to_type (fr, dir, Bar);
} }
framepos_t 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); return round_to_type (fr, dir, Beat);
} }
framepos_t 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); require_map_to (fr);
@ -1354,7 +1354,7 @@ TempoMap::round_to_beat_subdivision (framepos_t fr, int sub_num, int dir)
} }
framepos_t 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); require_map_to (frame);