From c1cfa12d6e5136d2e3e5501e83ff74c5009a9e60 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 22 Nov 2014 04:05:42 -0500 Subject: [PATCH] Wrap MusicalTime in a class. This lets us get a more explicit handle on time conversions, and is the main step towards using actual beat:tick time and getting away from floating point precision problems. --- gtk2_ardour/automation_region_view.h | 12 +- gtk2_ardour/edit_note_dialog.cc | 4 +- gtk2_ardour/editor.cc | 40 ++-- gtk2_ardour/editor_drag.cc | 6 +- gtk2_ardour/editor_ops.cc | 2 +- gtk2_ardour/hit.h | 2 +- gtk2_ardour/midi_region_view.cc | 55 ++--- gtk2_ardour/midi_region_view.h | 24 +- gtk2_ardour/patch_change_dialog.cc | 2 +- gtk2_ardour/step_editor.cc | 22 +- gtk2_ardour/step_entry.cc | 8 +- libs/ardour/ardour/automation_control.h | 1 + libs/ardour/ardour/automation_list.h | 5 +- libs/ardour/ardour/beats_frames_converter.h | 28 ++- libs/ardour/ardour/midi_model.h | 23 +- libs/ardour/ardour/midi_region.h | 2 + libs/ardour/ardour/midi_source.h | 12 +- libs/ardour/ardour/smf_source.h | 7 +- libs/ardour/beats_frames_converter.cc | 33 ++- libs/ardour/import.cc | 17 +- libs/ardour/midi_diskstream.cc | 2 +- libs/ardour/midi_model.cc | 20 +- libs/ardour/midi_region.cc | 18 +- libs/ardour/midi_source.cc | 6 +- libs/ardour/midi_state_tracker.cc | 2 +- libs/ardour/midi_stretch.cc | 4 +- libs/ardour/quantize.cc | 12 +- libs/ardour/smf_source.cc | 54 ++--- libs/ardour/tempo.cc | 22 +- libs/ardour/test/framepos_minus_beats_test.cc | 17 +- libs/ardour/test/framepos_plus_beats_test.cc | 16 +- libs/evoral/evoral/Event.hpp | 16 +- libs/evoral/evoral/Note.hpp | 10 +- libs/evoral/evoral/PatchChange.hpp | 4 +- libs/evoral/evoral/Sequence.hpp | 14 +- libs/evoral/evoral/types.hpp | 207 +++++++++++++++--- libs/evoral/src/Event.cpp | 1 + libs/evoral/src/Note.cpp | 16 +- libs/evoral/src/Sequence.cpp | 68 +++--- libs/evoral/test/SMFTest.cpp | 12 +- libs/evoral/test/SMFTest.hpp | 3 +- libs/evoral/test/SequenceTest.cpp | 5 +- libs/evoral/test/SequenceTest.hpp | 9 +- libs/midi++2/midnam_patch.cc | 2 +- 44 files changed, 519 insertions(+), 326 deletions(-) diff --git a/gtk2_ardour/automation_region_view.h b/gtk2_ardour/automation_region_view.h index d8f0b2637f..a41c57c782 100644 --- a/gtk2_ardour/automation_region_view.h +++ b/gtk2_ardour/automation_region_view.h @@ -54,11 +54,11 @@ public: float times, boost::shared_ptr slist); - ARDOUR::BeatsFramesConverter const & region_relative_time_converter () const { + ARDOUR::DoubleBeatsFramesConverter const & region_relative_time_converter () const { return _region_relative_time_converter; } - ARDOUR::BeatsFramesConverter const & source_relative_time_converter () const { + ARDOUR::DoubleBeatsFramesConverter const & source_relative_time_converter () const { return _source_relative_time_converter; } @@ -83,10 +83,10 @@ protected: void exited(); private: - ARDOUR::BeatsFramesConverter _region_relative_time_converter; - ARDOUR::BeatsFramesConverter _source_relative_time_converter; - Evoral::Parameter _parameter; - boost::shared_ptr _line; + ARDOUR::DoubleBeatsFramesConverter _region_relative_time_converter; + ARDOUR::DoubleBeatsFramesConverter _source_relative_time_converter; + Evoral::Parameter _parameter; + boost::shared_ptr _line; }; #endif /* __gtk_ardour_automation_region_view_h__ */ diff --git a/gtk2_ardour/edit_note_dialog.cc b/gtk2_ardour/edit_note_dialog.cc index 3d72e080ed..149b212926 100644 --- a/gtk2_ardour/edit_note_dialog.cc +++ b/gtk2_ardour/edit_note_dialog.cc @@ -193,7 +193,7 @@ EditNoteDialog::done (int r) } } - double const t = _region_view->source_relative_time_converter().from (_time_clock.current_time ()); + Evoral::MusicalTime const t = _region_view->source_relative_time_converter().from (_time_clock.current_time ()); if (!_time_all.get_sensitive() || _time_all.get_active ()) { for (set::iterator i = _events.begin(); i != _events.end(); ++i) { @@ -204,7 +204,7 @@ EditNoteDialog::done (int r) } } - double const d = _region_view->region_relative_time_converter().from (_length_clock.current_duration ()); + Evoral::MusicalTime const d = _region_view->region_relative_time_converter().from (_length_clock.current_duration ()); if (!_length_all.get_sensitive() || _length_all.get_active ()) { for (set::iterator i = _events.begin(); i != _events.end(); ++i) { diff --git a/gtk2_ardour/editor.cc b/gtk2_ardour/editor.cc index 98bf1ca35a..ce6e6b15ae 100644 --- a/gtk2_ardour/editor.cc +++ b/gtk2_ardour/editor.cc @@ -3901,64 +3901,64 @@ Editor::get_grid_type_as_beats (bool& success, framepos_t position) switch (_snap_type) { case SnapToBeat: - return 1.0; + return Evoral::MusicalTime(1.0); break; case SnapToBeatDiv128: - return 1.0/128.0; + return Evoral::MusicalTime(1.0/128.0); break; case SnapToBeatDiv64: - return 1.0/64.0; + return Evoral::MusicalTime(1.0/64.0); break; case SnapToBeatDiv32: - return 1.0/32.0; + return Evoral::MusicalTime(1.0/32.0); break; case SnapToBeatDiv28: - return 1.0/28.0; + return Evoral::MusicalTime(1.0/28.0); break; case SnapToBeatDiv24: - return 1.0/24.0; + return Evoral::MusicalTime(1.0/24.0); break; case SnapToBeatDiv20: - return 1.0/20.0; + return Evoral::MusicalTime(1.0/20.0); break; case SnapToBeatDiv16: - return 1.0/16.0; + return Evoral::MusicalTime(1.0/16.0); break; case SnapToBeatDiv14: - return 1.0/14.0; + return Evoral::MusicalTime(1.0/14.0); break; case SnapToBeatDiv12: - return 1.0/12.0; + return Evoral::MusicalTime(1.0/12.0); break; case SnapToBeatDiv10: - return 1.0/10.0; + return Evoral::MusicalTime(1.0/10.0); break; case SnapToBeatDiv8: - return 1.0/8.0; + return Evoral::MusicalTime(1.0/8.0); break; case SnapToBeatDiv7: - return 1.0/7.0; + return Evoral::MusicalTime(1.0/7.0); break; case SnapToBeatDiv6: - return 1.0/6.0; + return Evoral::MusicalTime(1.0/6.0); break; case SnapToBeatDiv5: - return 1.0/5.0; + return Evoral::MusicalTime(1.0/5.0); break; case SnapToBeatDiv4: - return 1.0/4.0; + return Evoral::MusicalTime(1.0/4.0); break; case SnapToBeatDiv3: - return 1.0/3.0; + return Evoral::MusicalTime(1.0/3.0); break; case SnapToBeatDiv2: - return 1.0/2.0; + return Evoral::MusicalTime(1.0/2.0); break; case SnapToBar: if (_session) { - return _session->tempo_map().meter_at (position).divisions_per_bar(); + return Evoral::MusicalTime(_session->tempo_map().meter_at (position).divisions_per_bar()); } break; @@ -3977,7 +3977,7 @@ Editor::get_grid_type_as_beats (bool& success, framepos_t position) break; } - return 0.0; + return Evoral::MusicalTime(); } framecnt_t diff --git a/gtk2_ardour/editor_drag.cc b/gtk2_ardour/editor_drag.cc index ab2d2c071e..fa66c952dc 100644 --- a/gtk2_ardour/editor_drag.cc +++ b/gtk2_ardour/editor_drag.cc @@ -5220,7 +5220,7 @@ NoteCreateDrag::grid_frames (framepos_t t) const bool success; Evoral::MusicalTime grid_beats = _editor->get_grid_type_as_beats (success, t); if (!success) { - grid_beats = 1; + grid_beats = Evoral::MusicalTime(1); } return _region_view->region_beats_to_region_frames (grid_beats); @@ -5276,13 +5276,13 @@ NoteCreateDrag::finished (GdkEvent*, bool had_movement) framecnt_t length = (framecnt_t) fabs ((double)(_note[0] - _note[1])); framecnt_t const g = grid_frames (start); - double const one_tick = 1 / Timecode::BBT_Time::ticks_per_beat; + Evoral::MusicalTime const one_tick = Evoral::MusicalTime::ticks(1); if (_editor->snap_mode() == SnapNormal && length < g) { length = g - one_tick; } - double const length_beats = max (one_tick, _region_view->region_frames_to_region_beats (length)); + Evoral::MusicalTime const length_beats = max (one_tick, _region_view->region_frames_to_region_beats (length)); _region_view->create_note_at (start, _drag_rect->y0(), length_beats, false); } diff --git a/gtk2_ardour/editor_ops.cc b/gtk2_ardour/editor_ops.cc index a7e6d8420f..856b311ebd 100644 --- a/gtk2_ardour/editor_ops.cc +++ b/gtk2_ardour/editor_ops.cc @@ -4988,7 +4988,7 @@ Editor::insert_patch_change (bool from_context) */ MidiRegionView* first = dynamic_cast (rs.front ()); - Evoral::PatchChange empty (0, 0, 0, 0); + Evoral::PatchChange empty (Evoral::MusicalTime(), 0, 0, 0); PatchChangeDialog d (0, _session, empty, first->instrument_info(), Gtk::Stock::ADD); if (d.run() == RESPONSE_CANCEL) { diff --git a/gtk2_ardour/hit.h b/gtk2_ardour/hit.h index 5a6cef9943..f13a0ef27c 100644 --- a/gtk2_ardour/hit.h +++ b/gtk2_ardour/hit.h @@ -30,7 +30,7 @@ namespace ArdourCanvas { class Hit : public NoteBase { public: - typedef Evoral::Note NoteType; + typedef Evoral::Note NoteType; Hit (MidiRegionView& region, ArdourCanvas::Item* parent, diff --git a/gtk2_ardour/midi_region_view.cc b/gtk2_ardour/midi_region_view.cc index b9b53fbd11..b3c2cf1f84 100644 --- a/gtk2_ardour/midi_region_view.cc +++ b/gtk2_ardour/midi_region_view.cc @@ -550,7 +550,7 @@ MidiRegionView::button_release (GdkEventButton* ev) /* Shorten the length by 1 tick so that we can add a new note at the next grid snap without it overlapping this one. */ - beats -= 1.0 / Timecode::BBT_Time::ticks_per_beat; + beats -= Evoral::MusicalTime::tick(); create_note_at (editor.pixel_to_sample (event_x), event_y, beats, true); } @@ -564,7 +564,7 @@ MidiRegionView::button_release (GdkEventButton* ev) /* Shorten the length by 1 tick so that we can add a new note at the next grid snap without it overlapping this one. */ - beats -= 1.0 / Timecode::BBT_Time::ticks_per_beat; + beats -= Evoral::MusicalTime::tick(); create_note_at (editor.pixel_to_sample (event_x), event_y, beats, true); @@ -728,7 +728,7 @@ MidiRegionView::key_press (GdkEventKey* ev) bool shorter = Keyboard::modifier_state_contains (ev->state, Keyboard::PrimaryModifier); bool fine = Keyboard::modifier_state_contains (ev->state, Keyboard::SecondaryModifier); - change_note_lengths (fine, shorter, 0.0, start, end); + change_note_lengths (fine, shorter, Evoral::MusicalTime(), start, end); return true; @@ -909,7 +909,7 @@ MidiRegionView::show_list_editor () * \param snap_t true to snap t to the grid, otherwise false. */ void -MidiRegionView::create_note_at (framepos_t t, double y, double length, bool snap_t) +MidiRegionView::create_note_at (framepos_t t, double y, Evoral::MusicalTime length, bool snap_t) { if (length < 2 * DBL_EPSILON) { return; @@ -1521,7 +1521,7 @@ MidiRegionView::end_write() /** Resolve an active MIDI note (while recording). */ void -MidiRegionView::resolve_note(uint8_t note, double end_time) +MidiRegionView::resolve_note(uint8_t note, Evoral::MusicalTime end_time) { if (midi_view()->note_mode() != Sustained) { return; @@ -1660,7 +1660,7 @@ MidiRegionView::update_note (Note* ev, bool update_ghost_regions) ev->set_y1 (y0 + std::max(1., floor(midi_stream_view()->note_height()) - 1)); - if (note->length() == 0) { + if (!note->length()) { if (_active_notes && note->note() < 128) { // If this note is already active there's a stuck note, // finish the old note rectangle @@ -1862,7 +1862,7 @@ patch_applies (const ARDOUR::MidiModel::constPatchChangePtr pc, double time, uin } void -MidiRegionView::get_patch_key_at (double time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key) const +MidiRegionView::get_patch_key_at (Evoral::MusicalTime time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key) const { // The earliest event not before time MidiModel::PatchChanges::iterator i = _model->patch_change_lower_bound (time); @@ -2260,7 +2260,7 @@ MidiRegionView::note_selected (NoteBase* ev, bool add, bool extend) /* find end of latest note selected, select all between that and the start of "ev" */ Evoral::MusicalTime earliest = Evoral::MaxMusicalTime; - Evoral::MusicalTime latest = 0; + Evoral::MusicalTime latest = Evoral::MusicalTime(); for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) { if ((*i)->note()->end_time() > latest) { @@ -2421,7 +2421,7 @@ MidiRegionView::move_selection(double dx, double dy, double cumulative_dy) } for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) { - if (Evoral::musical_time_equal ((*i)->note()->time(), earliest)) { + if ((*i)->note()->time() == earliest) { to_play.push_back ((*i)->note()); } (*i)->move_event(dx, dy); @@ -2548,7 +2548,7 @@ MidiRegionView::get_end_position_pixels() } framepos_t -MidiRegionView::source_beats_to_absolute_frames(double beats) const +MidiRegionView::source_beats_to_absolute_frames(Evoral::MusicalTime beats) const { /* the time converter will return the frame corresponding to `beats' relative to the start of the source. The start of the source @@ -2558,7 +2558,7 @@ MidiRegionView::source_beats_to_absolute_frames(double beats) const return source_start + _source_relative_time_converter.to (beats); } -double +Evoral::MusicalTime MidiRegionView::absolute_frames_to_source_beats(framepos_t frames) const { /* the `frames' argument needs to be converted into a frame count @@ -2570,12 +2570,12 @@ MidiRegionView::absolute_frames_to_source_beats(framepos_t frames) const } framepos_t -MidiRegionView::region_beats_to_region_frames(double beats) const +MidiRegionView::region_beats_to_region_frames(Evoral::MusicalTime beats) const { return _region_relative_time_converter.to(beats); } -double +Evoral::MusicalTime MidiRegionView::region_frames_to_region_beats(framepos_t frames) const { return _region_relative_time_converter.from(frames); @@ -2827,8 +2827,8 @@ MidiRegionView::trim_note (NoteBase* event, Evoral::MusicalTime front_delta, Evo { bool change_start = false; bool change_length = false; - Evoral::MusicalTime new_start = 0; - Evoral::MusicalTime new_length = 0; + Evoral::MusicalTime new_start; + Evoral::MusicalTime new_length; /* NOTE: the semantics of the two delta arguments are slightly subtle: @@ -2843,7 +2843,7 @@ MidiRegionView::trim_note (NoteBase* event, Evoral::MusicalTime front_delta, Evo if (front_delta < 0) { if (event->note()->time() < -front_delta) { - new_start = 0; + new_start = Evoral::MusicalTime(); } else { new_start = event->note()->time() + front_delta; // moves earlier } @@ -2924,7 +2924,7 @@ MidiRegionView::change_note_time (NoteBase* event, Evoral::MusicalTime delta, bo if (relative) { if (delta < 0.0) { if (event->note()->time() < -delta) { - new_time = 0; + new_time = Evoral::MusicalTime(); } else { new_time = event->note()->time() + delta; } @@ -3053,9 +3053,9 @@ MidiRegionView::transpose (bool up, bool fine, bool allow_smush) void MidiRegionView::change_note_lengths (bool fine, bool shorter, Evoral::MusicalTime delta, bool start, bool end) { - if (delta == 0.0) { + if (!delta) { if (fine) { - delta = 1.0/128.0; + delta = Evoral::MusicalTime(1.0/128.0); } else { /* grab the current grid distance */ delta = get_grid_beats(_region->position()); @@ -3074,7 +3074,9 @@ MidiRegionView::change_note_lengths (bool fine, bool shorter, Evoral::MusicalTim /* note the negation of the delta for start */ - trim_note (*i, (start ? -delta : 0), (end ? delta : 0)); + trim_note (*i, + (start ? -delta : Evoral::MusicalTime()), + (end ? delta : Evoral::MusicalTime())); i = next; } @@ -3402,10 +3404,10 @@ MidiRegionView::paste_internal (framepos_t pos, unsigned paste_count, float time const Evoral::MusicalTime first_time = (*mcb.notes().begin())->time(); const Evoral::MusicalTime last_time = (*mcb.notes().rbegin())->end_time(); const Evoral::MusicalTime duration = last_time - first_time; - const Evoral::MusicalTime snap_duration = ceil(duration / snap_beats) * snap_beats; - const Evoral::MusicalTime paste_offset = paste_count * snap_duration; + const Evoral::MusicalTime snap_duration = duration.snap_to(snap_beats); + const Evoral::MusicalTime paste_offset = snap_duration * paste_count; const Evoral::MusicalTime pos_beats = absolute_frames_to_source_beats(pos) + paste_offset; - Evoral::MusicalTime end_point = 0; + Evoral::MusicalTime end_point = Evoral::MusicalTime(); DEBUG_TRACE (DEBUG::CutNPaste, string_compose ("Paste data spans from %1 to %2 (%3) ; paste pos beats = %4 (based on %5 - %6)\n", first_time, @@ -3585,7 +3587,8 @@ MidiRegionView::update_ghost_note (double x, double y) the start of the source; that is how all note times are stored. */ _ghost_note->note()->set_time ( - std::max(0.0, absolute_frames_to_source_beats (f + _region->position ()))); + std::max(Evoral::MusicalTime(), + absolute_frames_to_source_beats (f + _region->position ()))); _ghost_note->note()->set_length (length); _ghost_note->note()->set_note (midi_stream_view()->y_to_note (y)); _ghost_note->note()->set_channel (mtv->get_channel_for_add ()); @@ -3786,7 +3789,7 @@ MidiRegionView::data_recorded (boost::weak_ptr w) if (ev.type() == MIDI_CMD_NOTE_ON) { boost::shared_ptr note ( - new NoteType (ev.channel(), time_beats, 0, ev.note(), ev.velocity())); + new NoteType (ev.channel(), time_beats, Evoral::MusicalTime(), ev.note(), ev.velocity())); add_note (note, true); @@ -3969,7 +3972,7 @@ MidiRegionView::get_grid_beats(framepos_t pos) const bool success = false; Evoral::MusicalTime beats = editor.get_grid_type_as_beats(success, pos); if (!success) { - beats = 1; + beats = Evoral::MusicalTime(1); } return beats; } diff --git a/gtk2_ardour/midi_region_view.h b/gtk2_ardour/midi_region_view.h index 098361e984..08005497df 100644 --- a/gtk2_ardour/midi_region_view.h +++ b/gtk2_ardour/midi_region_view.h @@ -110,7 +110,7 @@ public: GhostRegion* add_ghost (TimeAxisView&); void add_note(const boost::shared_ptr note, bool visible); - void resolve_note(uint8_t note_num, double end_time); + void resolve_note(uint8_t note_num, Evoral::MusicalTime end_time); void cut_copy_clear (Editing::CutCopyOp); bool paste (framepos_t pos, unsigned paste_count, float times, const ::Selection& selection, ItemCounts& counts); @@ -124,7 +124,7 @@ public: * @key a reference to an instance of MIDI::Name::PatchPrimaryKey whose fields will * will be set according to the result of the lookup */ - void get_patch_key_at (double time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key) const; + void get_patch_key_at (Evoral::MusicalTime time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key) const; /** Convert a given PatchChange into a PatchPrimaryKey */ @@ -258,22 +258,22 @@ public: framepos_t snap_pixel_to_sample(double x); /** Convert a timestamp in beats into frames (both relative to region position) */ - framepos_t region_beats_to_region_frames(double beats) const; + framepos_t region_beats_to_region_frames(Evoral::MusicalTime beats) const; /** Convert a timestamp in beats into absolute frames */ - framepos_t region_beats_to_absolute_frames(double beats) const { + framepos_t region_beats_to_absolute_frames(Evoral::MusicalTime beats) const { return _region->position() + region_beats_to_region_frames (beats); } /** Convert a timestamp in frames to beats (both relative to region position) */ - double region_frames_to_region_beats(framepos_t) const; + Evoral::MusicalTime region_frames_to_region_beats(framepos_t) const; /** Convert a timestamp in beats measured from source start into absolute frames */ - framepos_t source_beats_to_absolute_frames(double beats) const; + framepos_t source_beats_to_absolute_frames(Evoral::MusicalTime beats) const; /** Convert a timestamp in beats measured from source start into region-relative frames */ - framepos_t source_beats_to_region_frames(double beats) const { + framepos_t source_beats_to_region_frames(Evoral::MusicalTime beats) const { return source_beats_to_absolute_frames (beats) - _region->position(); } /** Convert a timestamp in absolute frames to beats measured from source start*/ - double absolute_frames_to_source_beats(framepos_t) const; + Evoral::MusicalTime absolute_frames_to_source_beats(framepos_t) const; ARDOUR::BeatsFramesConverter const & region_relative_time_converter () const { return _region_relative_time_converter; @@ -309,7 +309,13 @@ public: void trim_front_starting (); void trim_front_ending (); - void create_note_at (framepos_t, double, double, bool); + /** Add a note to the model, and the view, at a canvas (click) coordinate. + * \param t time in frames relative to the position of the region + * \param y vertical position in pixels + * \param length duration of the note in beats + * \param snap_t true to snap t to the grid, otherwise false. + */ + void create_note_at (framepos_t t, double y, Evoral::MusicalTime length, bool snap_t); void clear_selection (bool signal = true) { clear_selection_except (0, signal); } diff --git a/gtk2_ardour/patch_change_dialog.cc b/gtk2_ardour/patch_change_dialog.cc index 081604344c..3af5a4ed7f 100644 --- a/gtk2_ardour/patch_change_dialog.cc +++ b/gtk2_ardour/patch_change_dialog.cc @@ -143,7 +143,7 @@ PatchChangeDialog::instrument_info_changed () Evoral::PatchChange PatchChangeDialog::patch () const { - Evoral::MusicalTime t = 0; + Evoral::MusicalTime t = Evoral::MusicalTime(); if (_time_converter) { t = _time_converter->from (_time.current_time ()); diff --git a/gtk2_ardour/step_editor.cc b/gtk2_ardour/step_editor.cc index ef3caad5d1..ec525b84ed 100644 --- a/gtk2_ardour/step_editor.cc +++ b/gtk2_ardour/step_editor.cc @@ -41,7 +41,7 @@ StepEditor::StepEditor (PublicEditor& e, boost::shared_ptr t, MidiTim step_edit_insert_position = 0; _step_edit_triplet_countdown = 0; _step_edit_within_chord = 0; - _step_edit_chord_duration = 0.0; + _step_edit_chord_duration = Evoral::MusicalTime(); step_edit_region_view = 0; _track->PlaylistChanged.connect (*this, invalidator (*this), @@ -60,11 +60,11 @@ StepEditor::start_step_editing () { _step_edit_triplet_countdown = 0; _step_edit_within_chord = 0; - _step_edit_chord_duration = 0.0; + _step_edit_chord_duration = Evoral::MusicalTime(); step_edit_region.reset (); step_edit_region_view = 0; last_added_pitch = -1; - last_added_end = 0; + last_added_end = Evoral::MusicalTime(); resync_step_edit_position (); prepare_step_edit_region (); @@ -198,7 +198,7 @@ StepEditor::check_step_edit () incoming.read_contents (size, buf); if ((buf[0] & 0xf0) == MIDI_CMD_NOTE_ON) { - step_add_note (buf[0] & 0xf, buf[1], buf[2], 0.0); + step_add_note (buf[0] & 0xf, buf[1], buf[2], Evoral::MusicalTime()); } } } @@ -233,7 +233,7 @@ StepEditor::move_step_edit_beat_pos (Evoral::MusicalTime beats) if (-beats < step_edit_beat_pos) { step_edit_beat_pos += beats; // its negative, remember } else { - step_edit_beat_pos = 0; + step_edit_beat_pos = Evoral::MusicalTime(); } } step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos); @@ -292,8 +292,8 @@ StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evo up by 1 tick from where the last note ended */ - at += 1.0/Timecode::BBT_Time::ticks_per_beat; - len -= 1.0/Timecode::BBT_Time::ticks_per_beat; + at += Evoral::MusicalTime::ticks(1); + len -= Evoral::MusicalTime::ticks(1); } step_edit_region_view->step_add_note (channel, pitch, velocity, at, len); @@ -313,7 +313,7 @@ StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evo step_edit_beat_pos += beat_duration; step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos); } else { - step_edit_beat_pos += 1.0/Timecode::BBT_Time::ticks_per_beat; // tiny, but no longer overlapping + step_edit_beat_pos += Evoral::MusicalTime::ticks(1); // tiny, but no longer overlapping _step_edit_chord_duration = max (_step_edit_chord_duration, beat_duration); } @@ -384,7 +384,7 @@ StepEditor::step_edit_rest (Evoral::MusicalTime beats) void StepEditor::step_edit_beat_sync () { - step_edit_beat_pos = ceil (step_edit_beat_pos); + step_edit_beat_pos = step_edit_beat_pos.round_up_to_beat(); step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos); } @@ -399,7 +399,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, RoundUpAlways); - step_edit_beat_pos = ceil (step_edit_region_view->region_frames_to_region_beats (fpos - step_edit_region->position())); + step_edit_beat_pos = step_edit_region_view->region_frames_to_region_beats (fpos - step_edit_region->position()).round_up_to_beat(); step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos); } @@ -425,7 +425,7 @@ StepEditor::region_removed (boost::weak_ptr wr) step_edit_region.reset(); step_edit_region_view = 0; // force a recompute of the insert position - step_edit_beat_pos = -1.0; + step_edit_beat_pos = Evoral::MusicalTime(-1); } } diff --git a/gtk2_ardour/step_entry.cc b/gtk2_ardour/step_entry.cc index ac13770a29..607f4abd62 100644 --- a/gtk2_ardour/step_entry.cc +++ b/gtk2_ardour/step_entry.cc @@ -526,13 +526,13 @@ StepEntry::on_key_release_event (GdkEventKey* ev) void StepEntry::rest_event_handler () { - se->step_edit_rest (0.0); + se->step_edit_rest (Evoral::MusicalTime()); } Evoral::MusicalTime StepEntry::note_length () { - Evoral::MusicalTime base_time = 4.0 / (Evoral::MusicalTime) length_divisor_adjustment.get_value(); + double base_time = 4.0 / (double) length_divisor_adjustment.get_value(); RefPtr act = myactions.find_action ("StepEditing/toggle-triplet"); RefPtr tact = RefPtr::cast_dynamic (act); @@ -549,7 +549,7 @@ StepEntry::note_length () base_time *= 1 + ((dots - 1.0)/dots); } - return base_time; + return Evoral::MusicalTime(base_time); } uint8_t @@ -795,7 +795,7 @@ StepEntry::insert_rest () void StepEntry::insert_grid_rest () { - se->step_edit_rest (0.0); + se->step_edit_rest (Evoral::MusicalTime()); } void diff --git a/libs/ardour/ardour/automation_control.h b/libs/ardour/ardour/automation_control.h index 639c5b5287..d73b2fc822 100644 --- a/libs/ardour/ardour/automation_control.h +++ b/libs/ardour/ardour/automation_control.h @@ -24,6 +24,7 @@ #include #include +#include "evoral/types.hpp" #include "pbd/controllable.h" #include "evoral/Control.hpp" diff --git a/libs/ardour/ardour/automation_list.h b/libs/ardour/ardour/automation_list.h index 72c26420c0..28f9be3fd4 100644 --- a/libs/ardour/ardour/automation_list.h +++ b/libs/ardour/ardour/automation_list.h @@ -27,6 +27,9 @@ #include +#include "evoral/ControlList.hpp" +#include "evoral/Parameter.hpp" + #include "pbd/undo.h" #include "pbd/xml++.h" #include "pbd/statefuldestructible.h" @@ -34,8 +37,6 @@ #include "ardour/ardour.h" -#include "evoral/ControlList.hpp" - namespace ARDOUR { class AutomationList; diff --git a/libs/ardour/ardour/beats_frames_converter.h b/libs/ardour/ardour/beats_frames_converter.h index 2e170d278a..ada09b1179 100644 --- a/libs/ardour/ardour/beats_frames_converter.h +++ b/libs/ardour/ardour/beats_frames_converter.h @@ -20,6 +20,8 @@ */ #include "evoral/TimeConverter.hpp" +#include "evoral/types.hpp" + #include "ardour/libardour_visibility.h" #include "ardour/types.h" @@ -34,15 +36,35 @@ class TempoMap; * from some origin (supplied to the constructor in frames), and converts * them to the opposite unit, taking tempo changes into account. */ -class LIBARDOUR_API BeatsFramesConverter : public Evoral::TimeConverter { +class LIBARDOUR_API BeatsFramesConverter + : public Evoral::TimeConverter { public: BeatsFramesConverter (TempoMap& tempo_map, framepos_t origin) + : Evoral::TimeConverter (origin) + , _tempo_map(tempo_map) + {} + + framepos_t to (Evoral::MusicalTime beats) const; + Evoral::MusicalTime from (framepos_t frames) const; + +private: + TempoMap& _tempo_map; +}; + +/** Converter between beats and frames. Takes distances in beats or frames + * from some origin (supplied to the constructor in frames), and converts + * them to the opposite unit, taking tempo changes into account. + */ +class LIBARDOUR_API DoubleBeatsFramesConverter + : public Evoral::TimeConverter { +public: + DoubleBeatsFramesConverter (TempoMap& tempo_map, framepos_t origin) : Evoral::TimeConverter (origin) , _tempo_map(tempo_map) {} - framepos_t to (double beats) const; - double from (framepos_t frames) const; + framepos_t to (double beats) const; + double from (framepos_t frames) const; private: TempoMap& _tempo_map; diff --git a/libs/ardour/ardour/midi_model.h b/libs/ardour/ardour/midi_model.h index dc1c7af0e9..31df5ef040 100644 --- a/libs/ardour/ardour/midi_model.h +++ b/libs/ardour/ardour/midi_model.h @@ -116,16 +116,11 @@ public: struct NoteChange { NoteDiffCommand::Property property; NotePtr note; - uint32_t note_id; - - union { - uint8_t old_value; - TimeType old_time; - }; - union { - uint8_t new_value; - TimeType new_time; - }; + uint32_t note_id; + uint8_t old_value; // or... + TimeType old_time; // this + uint8_t new_value; // or... + TimeType new_time; // this }; typedef std::list ChangeList; @@ -209,16 +204,16 @@ public: struct Change { PatchChangePtr patch; Property property; - gint patch_id; + gint patch_id; + TimeType old_time; union { - TimeType old_time; uint8_t old_channel; int old_bank; uint8_t old_program; }; + TimeType new_time; union { uint8_t new_channel; - TimeType new_time; uint8_t new_program; int new_bank; }; @@ -311,7 +306,5 @@ private: } /* namespace ARDOUR */ -/* This is a very long comment and stuff oh my god it's so long what are we going to do oh no oh no*/ - #endif /* __ardour_midi_model_h__ */ diff --git a/libs/ardour/ardour/midi_region.h b/libs/ardour/ardour/midi_region.h index 38229b998b..2a4e3a6190 100644 --- a/libs/ardour/ardour/midi_region.h +++ b/libs/ardour/ardour/midi_region.h @@ -23,6 +23,8 @@ #include +#include "evoral/types.hpp" + #include "ardour/ardour.h" #include "ardour/region.h" diff --git a/libs/ardour/ardour/midi_source.h b/libs/ardour/ardour/midi_source.h index 65e382c4b7..2ce92ba3cf 100644 --- a/libs/ardour/ardour/midi_source.h +++ b/libs/ardour/ardour/midi_source.h @@ -43,7 +43,7 @@ template class MidiRingBuffer; class LIBARDOUR_API MidiSource : virtual public Source, public boost::enable_shared_from_this { public: - typedef double TimeType; + typedef Evoral::MusicalTime TimeType; MidiSource (Session& session, std::string name, Source::Flag flags = Source::Flag(0)); MidiSource (Session& session, const XMLNode&); @@ -120,7 +120,7 @@ class LIBARDOUR_API MidiSource : virtual public Source, public boost::enable_sha */ virtual void mark_midi_streaming_write_completed ( Evoral::Sequence::StuckNoteOption stuck_option, - Evoral::MusicalTime when = 0); + Evoral::MusicalTime when = Evoral::MusicalTime()); virtual void session_saved(); @@ -134,8 +134,8 @@ class LIBARDOUR_API MidiSource : virtual public Source, public boost::enable_sha bool length_mutable() const { return true; } - void set_length_beats(double l) { _length_beats = l; } - double length_beats() const { return _length_beats; } + void set_length_beats(TimeType l) { _length_beats = l; } + TimeType length_beats() const { return _length_beats; } virtual void load_model(bool lock=true, bool force_reload=false) = 0; virtual void destroy_model() = 0; @@ -194,8 +194,8 @@ class LIBARDOUR_API MidiSource : virtual public Source, public boost::enable_sha mutable Evoral::Sequence::const_iterator _model_iter; mutable bool _model_iter_valid; - mutable double _length_beats; - mutable framepos_t _last_read_end; + mutable Evoral::MusicalTime _length_beats; + mutable framepos_t _last_read_end; /** The total duration of the current capture. */ framepos_t _capture_length; diff --git a/libs/ardour/ardour/smf_source.h b/libs/ardour/ardour/smf_source.h index 84c45f9b3c..1c1f2bbcba 100644 --- a/libs/ardour/ardour/smf_source.h +++ b/libs/ardour/ardour/smf_source.h @@ -56,7 +56,8 @@ public: void mark_streaming_midi_write_started (NoteMode mode); void mark_streaming_write_completed (); - void mark_midi_streaming_write_completed (Evoral::Sequence::StuckNoteOption, Evoral::MusicalTime when = 0); + void mark_midi_streaming_write_completed (Evoral::Sequence::StuckNoteOption, + Evoral::MusicalTime when = Evoral::MusicalTime()); XMLNode& get_state (); int set_state (const XMLNode&, int version); @@ -88,8 +89,8 @@ public: framepos_t position, framecnt_t cnt); - double _last_ev_time_beats; - framepos_t _last_ev_time_frames; + Evoral::MusicalTime _last_ev_time_beats; + framepos_t _last_ev_time_frames; /** end time (start + duration) of last call to read_unlocked */ mutable framepos_t _smf_last_read_end; /** time (in SMF ticks, 1 tick per _ppqn) of the last event read by read_unlocked */ diff --git a/libs/ardour/beats_frames_converter.cc b/libs/ardour/beats_frames_converter.cc index 94042b1b03..b99edbf51e 100644 --- a/libs/ardour/beats_frames_converter.cc +++ b/libs/ardour/beats_frames_converter.cc @@ -31,26 +31,43 @@ namespace ARDOUR { * taking tempo changes into account. */ framepos_t -BeatsFramesConverter::to (double beats) const +BeatsFramesConverter::to (Evoral::MusicalTime beats) const { - if (beats < 0) { + if (beats < Evoral::MusicalTime()) { std::cerr << "negative beats passed to BFC: " << beats << std::endl; PBD::stacktrace (std::cerr, 30); + return 0; } - assert (beats >= 0); - framecnt_t r = _tempo_map.framepos_plus_beats (_origin_b, beats) - _origin_b; - return r; + return _tempo_map.framepos_plus_beats (_origin_b, beats) - _origin_b; } /** Takes a duration in frames and considers it as a distance from the origin * supplied to the constructor. Returns the equivalent number of beats, * taking tempo changes into account. */ -double +Evoral::MusicalTime BeatsFramesConverter::from (framepos_t frames) const { - double b = _tempo_map.framewalk_to_beats (_origin_b, frames); - return b; + return _tempo_map.framewalk_to_beats (_origin_b, frames); +} + +/** As above, but with beats in double instead (for GUI). */ +framepos_t +DoubleBeatsFramesConverter::to (double beats) const +{ + if (beats < 0.0) { + std::cerr << "negative beats passed to BFC: " << beats << std::endl; + PBD::stacktrace (std::cerr, 30); + return 0; + } + return _tempo_map.framepos_plus_beats (_origin_b, Evoral::MusicalTime(beats)) - _origin_b; +} + +/** As above, but with beats in double instead (for GUI). */ +double +DoubleBeatsFramesConverter::from (framepos_t frames) const +{ + return _tempo_map.framewalk_to_beats (_origin_b, frames).to_double(); } } /* namespace ARDOUR */ diff --git a/libs/ardour/import.cc b/libs/ardour/import.cc index ae5f751767..38a3da2fdc 100644 --- a/libs/ardour/import.cc +++ b/libs/ardour/import.cc @@ -389,10 +389,11 @@ write_midi_data_to_new_files (Evoral::SMF* source, ImportStatus& status, } smfs->append_event_unlocked_beats( - Evoral::Event(0, - (double)t / (double)source->ppqn(), - size, - buf)); + Evoral::Event( + 0, + Evoral::MusicalTime::ticks_at_rate(t, source->ppqn()), + size, + buf)); if (status.progress < 0.99) { status.progress += 0.01; @@ -403,10 +404,10 @@ write_midi_data_to_new_files (Evoral::SMF* source, ImportStatus& status, /* we wrote something */ - const framepos_t pos = 0; - const double length_beats = ceil(t / (double)source->ppqn()); - BeatsFramesConverter converter(smfs->session().tempo_map(), pos); - smfs->update_length(pos + converter.to(length_beats)); + const framepos_t pos = 0; + const Evoral::MusicalTime length_beats = Evoral::MusicalTime::ticks_at_rate(t, source->ppqn()); + BeatsFramesConverter converter(smfs->session().tempo_map(), pos); + smfs->update_length(pos + converter.to(length_beats.round_up_to_beat())); smfs->mark_streaming_write_completed (); if (status.cancel) { diff --git a/libs/ardour/midi_diskstream.cc b/libs/ardour/midi_diskstream.cc index 69eca996aa..40d333d584 100644 --- a/libs/ardour/midi_diskstream.cc +++ b/libs/ardour/midi_diskstream.cc @@ -927,7 +927,7 @@ MidiDiskstream::transport_stopped_wallclock (struct tm& /*when*/, time_t /*twhen /* set length in beats to entire capture length */ BeatsFramesConverter converter (_session.tempo_map(), capture_info.front()->start); - const double total_capture_beats = converter.from (total_capture); + const Evoral::MusicalTime total_capture_beats = converter.from (total_capture); _write_source->set_length_beats (total_capture_beats); /* flush to disk: this step differs from the audio path, diff --git a/libs/ardour/midi_model.cc b/libs/ardour/midi_model.cc index 259a04bc0f..6c2132562a 100644 --- a/libs/ardour/midi_model.cc +++ b/libs/ardour/midi_model.cc @@ -226,13 +226,13 @@ MidiModel::NoteDiffCommand::change (const NotePtr note, Property prop, break; case StartTime: - if (Evoral::musical_time_equal (note->time(), new_time)) { + if (note->time() == new_time) { return; } change.old_time = note->time(); break; case Length: - if (Evoral::musical_time_equal (note->length(), new_time)) { + if (note->length() == new_time) { return; } change.old_time = note->length(); @@ -556,7 +556,7 @@ MidiModel::NoteDiffCommand::unmarshal_note (XMLNode *xml_note) time_str >> time; } else { warning << "note information missing time" << endmsg; - time = 0; + time = MidiModel::TimeType(); } if ((prop = xml_note->property("length")) != 0) { @@ -564,7 +564,7 @@ MidiModel::NoteDiffCommand::unmarshal_note (XMLNode *xml_note) length_str >> length; } else { warning << "note information missing length" << endmsg; - length = 1; + length = MidiModel::TimeType(1); } if ((prop = xml_note->property("velocity")) != 0) { @@ -1253,7 +1253,7 @@ MidiModel::PatchChangeDiffCommand::unmarshal_patch_change (XMLNode* n) { XMLProperty* prop; Evoral::event_id_t id = 0; - Evoral::MusicalTime time = 0; + Evoral::MusicalTime time = Evoral::MusicalTime(); int channel = 0; int program = 0; int bank = 0; @@ -1439,7 +1439,7 @@ MidiModel::write_to (boost::shared_ptr source) source->drop_model(); source->mark_streaming_midi_write_started (note_mode()); - for (Evoral::Sequence::const_iterator i = begin(0, true); i != end(); ++i) { + for (Evoral::Sequence::const_iterator i = begin(TimeType(), true); i != end(); ++i) { source->append_event_unlocked_beats(*i); } @@ -1469,7 +1469,7 @@ MidiModel::sync_to_source () ms->mark_streaming_midi_write_started (note_mode()); - for (Evoral::Sequence::const_iterator i = begin(0, true); i != end(); ++i) { + for (Evoral::Sequence::const_iterator i = begin(TimeType(), true); i != end(); ++i) { ms->append_event_unlocked_beats(*i); } @@ -1503,7 +1503,7 @@ MidiModel::write_section_to (boost::shared_ptr source, Evoral::Music source->drop_model(); source->mark_streaming_midi_write_started (note_mode()); - for (Evoral::Sequence::const_iterator i = begin(0, true); i != end(); ++i) { + for (Evoral::Sequence::const_iterator i = begin(TimeType(), true); i != end(); ++i) { const Evoral::Event& ev (*i); if (ev.time() >= begin_time && ev.time() < end_time) { @@ -1662,7 +1662,7 @@ MidiModel::resolve_overlaps_unlocked (const NotePtr note, void* arg) TimeType ea = note->end_time(); const Pitches& p (pitches (note->channel())); - NotePtr search_note(new Note(0, 0, 0, note->note())); + NotePtr search_note(new Note(0, TimeType(), TimeType(), note->note())); set to_be_deleted; bool set_note_length = false; bool set_note_time = false; @@ -1979,7 +1979,7 @@ MidiModel::insert_silence_at_start (TimeType t) for (Controls::iterator i = controls().begin(); i != controls().end(); ++i) { boost::shared_ptr ac = boost::dynamic_pointer_cast (i->second); XMLNode& before = ac->alist()->get_state (); - i->second->list()->shift (0, t); + i->second->list()->shift (0, t.to_double()); XMLNode& after = ac->alist()->get_state (); s->session().add_command (new MementoCommand (new MidiAutomationListBinder (s, i->first), &before, &after)); } diff --git a/libs/ardour/midi_region.cc b/libs/ardour/midi_region.cc index 71fd796b81..f6631b9de5 100644 --- a/libs/ardour/midi_region.cc +++ b/libs/ardour/midi_region.cc @@ -28,6 +28,8 @@ #include #include +#include "evoral/types.hpp" + #include "pbd/xml++.h" #include "pbd/basename.h" @@ -78,7 +80,7 @@ MidiRegion::register_properties () /* Basic MidiRegion constructor (many channels) */ MidiRegion::MidiRegion (const SourceList& srcs) : Region (srcs) - , _start_beats (Properties::start_beats, 0) + , _start_beats (Properties::start_beats, Evoral::MusicalTime()) , _length_beats (Properties::length_beats, midi_source(0)->length_beats()) { register_properties (); @@ -92,7 +94,7 @@ MidiRegion::MidiRegion (const SourceList& srcs) MidiRegion::MidiRegion (boost::shared_ptr other) : Region (other) , _start_beats (Properties::start_beats, other->_start_beats) - , _length_beats (Properties::length_beats, (Evoral::MusicalTime) 0) + , _length_beats (Properties::length_beats, Evoral::MusicalTime()) { update_length_beats (); register_properties (); @@ -105,14 +107,14 @@ MidiRegion::MidiRegion (boost::shared_ptr other) /** Create a new MidiRegion that is part of an existing one */ MidiRegion::MidiRegion (boost::shared_ptr other, frameoffset_t offset) : Region (other, offset) - , _start_beats (Properties::start_beats, (Evoral::MusicalTime) 0) - , _length_beats (Properties::length_beats, (Evoral::MusicalTime) 0) + , _start_beats (Properties::start_beats, Evoral::MusicalTime()) + , _length_beats (Properties::length_beats, Evoral::MusicalTime()) { BeatsFramesConverter bfc (_session.tempo_map(), _position); Evoral::MusicalTime const offset_beats = bfc.from (offset); - _start_beats = other->_start_beats + offset_beats; - _length_beats = other->_length_beats - offset_beats; + _start_beats = other->_start_beats.val() + offset_beats; + _length_beats = other->_length_beats.val() - offset_beats; register_properties (); @@ -216,7 +218,7 @@ MidiRegion::set_position_internal (framepos_t pos, bool allow_bbt_recompute) /* zero length regions don't exist - so if _length_beats is zero, this object is under construction. */ - if (_length_beats) { + if (_length_beats.val() == Evoral::MusicalTime()) { /* leave _length_beats alone, and change _length to reflect the state of things at the new position (tempo map may dictate a different number of frames */ @@ -440,7 +442,7 @@ MidiRegion::fix_negative_start () model()->insert_silence_at_start (c.from (-_start)); _start = 0; - _start_beats = 0; + _start_beats = Evoral::MusicalTime(); } /** Transpose the notes in this region by a given number of semitones */ diff --git a/libs/ardour/midi_source.cc b/libs/ardour/midi_source.cc index 823ca9dc5c..1c050733c2 100644 --- a/libs/ardour/midi_source.cc +++ b/libs/ardour/midi_source.cc @@ -153,13 +153,13 @@ MidiSource::set_state (const XMLNode& node, int /*version*/) bool MidiSource::empty () const { - return _length_beats == 0; + return !_length_beats; } framecnt_t MidiSource::length (framepos_t pos) const { - if (_length_beats == 0) { + if (!_length_beats) { return 0; } @@ -198,7 +198,7 @@ MidiSource::midi_read (Evoral::EventSink& dst, if (_model) { // Find appropriate model iterator - Evoral::Sequence::const_iterator& i = _model_iter; + Evoral::Sequence::const_iterator& i = _model_iter; if (_last_read_end == 0 || start != _last_read_end || !_model_iter_valid) { // Cached iterator is invalid, search for the first event past start i = _model->begin(converter.from(start), false, filtered); diff --git a/libs/ardour/midi_state_tracker.cc b/libs/ardour/midi_state_tracker.cc index f006004621..0eac3819f2 100644 --- a/libs/ardour/midi_state_tracker.cc +++ b/libs/ardour/midi_state_tracker.cc @@ -173,7 +173,7 @@ MidiStateTracker::resolve_notes (MidiSource& src, Evoral::MusicalTime time) this, (int) note, (int) channel, time)); _active_notes[note + 128 * channel]--; /* don't stack events up at the same time */ - time += 1.0/128.0; + time += Evoral::MusicalTime::tick(); } } } diff --git a/libs/ardour/midi_stretch.cc b/libs/ardour/midi_stretch.cc index 38cab08ace..f5b3a47b41 100644 --- a/libs/ardour/midi_stretch.cc +++ b/libs/ardour/midi_stretch.cc @@ -95,9 +95,9 @@ MidiStretch::run (boost::shared_ptr r, Progress*) /* Note: pass true into force_discrete for the begin() iterator so that the model doesn't * do interpolation of controller data when we stretch. */ - for (Evoral::Sequence::const_iterator i = old_model->begin (0, true); + for (Evoral::Sequence::const_iterator i = old_model->begin (MidiModel::TimeType(), true); i != old_model->end(); ++i) { - const double new_time = i->time() * _request.time_fraction; + const MidiModel::TimeType new_time = i->time() * (double)_request.time_fraction; // FIXME: double copy Evoral::Event ev(*i, true); diff --git a/libs/ardour/quantize.cc b/libs/ardour/quantize.cc index 7da1edaaa1..13b1cf3b36 100644 --- a/libs/ardour/quantize.cc +++ b/libs/ardour/quantize.cc @@ -58,6 +58,8 @@ Quantize::operator () (boost::shared_ptr model, double position, std::vector::Notes>& seqs) { + /* TODO: Rewrite this to be precise with fixed point? */ + /* Calculate offset from start of model to next closest quantize step, to quantize relative to actual session beats (etc.) rather than from the start of the model. @@ -77,8 +79,8 @@ Quantize::operator () (boost::shared_ptr model, */ for (Evoral::Sequence::Notes::iterator i = (*s).begin(); i != (*s).end(); ++i) { - double new_start = round (((*i)->time() - offset) / _start_grid) * _start_grid + offset; - double new_end = round (((*i)->end_time() - offset) / _end_grid) * _end_grid + offset; + double new_start = round (((*i)->time().to_double() - offset) / _start_grid) * _start_grid + offset; + double new_end = round (((*i)->end_time().to_double() - offset) / _end_grid) * _end_grid + offset; if (_swing > 0.0 && !even) { @@ -104,18 +106,18 @@ Quantize::operator () (boost::shared_ptr model, } - double delta = new_start - (*i)->time(); + double delta = new_start - (*i)->time().to_double(); if (fabs (delta) >= _threshold) { if (_snap_start) { delta *= _strength; cmd->change ((*i), MidiModel::NoteDiffCommand::StartTime, - (*i)->time() + delta); + (*i)->time().to_double() + delta); } } if (_snap_end) { - delta = new_end - (*i)->end_time(); + delta = new_end - (*i)->end_time().to_double(); if (fabs (delta) >= _threshold) { double new_dur = new_end - new_start; diff --git a/libs/ardour/smf_source.cc b/libs/ardour/smf_source.cc index 8a956495a5..1ffcdac1b2 100644 --- a/libs/ardour/smf_source.cc +++ b/libs/ardour/smf_source.cc @@ -225,7 +225,7 @@ SMFSource::read_unlocked (Evoral::EventSink& destination, BeatsFramesConverter converter(_session.tempo_map(), source_start); - const uint64_t start_ticks = (uint64_t)(converter.from(start) * ppqn()); + const uint64_t start_ticks = converter.from(start).to_ticks(); DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF read_unlocked: start in ticks %1\n", start_ticks)); if (_smf_last_read_end == 0 || start != _smf_last_read_end) { @@ -273,7 +273,7 @@ SMFSource::read_unlocked (Evoral::EventSink& destination, /* Note that we add on the source start time (in session frames) here so that ev_frame_time is in session frames. */ - const framepos_t ev_frame_time = converter.to(time / (double)ppqn()) + source_start; + const framepos_t ev_frame_time = converter.to(Evoral::MusicalTime::ticks_at_rate(time, ppqn())) + source_start; if (ev_frame_time < start + duration) { destination.write (ev_frame_time, ev_type, ev_size, ev_buffer); @@ -377,9 +377,9 @@ SMFSource::write_unlocked (MidiRingBuffer& source, return cnt; } -/** Append an event with a timestamp in beats (double) */ +/** Append an event with a timestamp in beats */ void -SMFSource::append_event_unlocked_beats (const Evoral::Event& ev) +SMFSource::append_event_unlocked_beats (const Evoral::Event& ev) { if (!_writing || ev.size() == 0) { return; @@ -389,10 +389,10 @@ SMFSource::append_event_unlocked_beats (const Evoral::Event& ev) name().c_str(), ev.id(), ev.time(), ev.size()); for (size_t i = 0; i < ev.size(); ++i) printf("%X ", ev.buffer()[i]); printf("\n");*/ - double time = ev.time(); + Evoral::MusicalTime time = ev.time(); if (time < _last_ev_time_beats) { - const double difference = _last_ev_time_beats - time; - if (difference / (double)ppqn() < 1.0) { + const Evoral::MusicalTime difference = _last_ev_time_beats - time; + if (difference.to_double() / (double)ppqn() < 1.0) { /* Close enough. This problem occurs because Sequence is not actually ordered due to fuzzy time comparison. I'm pretty sure this is inherently a bad idea which causes problems all over the @@ -401,7 +401,7 @@ SMFSource::append_event_unlocked_beats (const Evoral::Event& ev) } else { /* Out of order by more than a tick. */ warning << string_compose(_("Skipping event with unordered beat time %1 < %2 (off by %3 beats, %4 ticks)"), - ev.time(), _last_ev_time_beats, difference, difference / (double)ppqn()) + ev.time(), _last_ev_time_beats, difference, difference.to_double() / (double)ppqn()) << endmsg; return; } @@ -421,8 +421,8 @@ SMFSource::append_event_unlocked_beats (const Evoral::Event& ev) _length_beats = max(_length_beats, time); - const double delta_time_beats = time - _last_ev_time_beats; - const uint32_t delta_time_ticks = (uint32_t)lrint(delta_time_beats * (double)ppqn()); + const Evoral::MusicalTime delta_time_beats = time - _last_ev_time_beats; + const uint32_t delta_time_ticks = delta_time_beats.to_ticks(ppqn()); Evoral::SMF::append_event_delta(delta_time_ticks, ev.size(), ev.buffer(), event_id); _last_ev_time_beats = time; @@ -448,9 +448,9 @@ SMFSource::append_event_unlocked_frames (const Evoral::Event& ev, fr return; } - BeatsFramesConverter converter(_session.tempo_map(), position); - const double ev_time_beats = converter.from(ev.time()); - Evoral::event_id_t event_id; + BeatsFramesConverter converter(_session.tempo_map(), position); + const Evoral::MusicalTime ev_time_beats = converter.from(ev.time()); + Evoral::event_id_t event_id; if (ev.id() < 0) { event_id = Evoral::next_event_id(); @@ -459,10 +459,10 @@ SMFSource::append_event_unlocked_frames (const Evoral::Event& ev, fr } if (_model) { - const Evoral::Event beat_ev (ev.event_type(), - ev_time_beats, - ev.size(), - const_cast(ev.buffer())); + const Evoral::Event beat_ev (ev.event_type(), + ev_time_beats, + ev.size(), + const_cast(ev.buffer())); _model->append (beat_ev, event_id); } @@ -470,7 +470,7 @@ SMFSource::append_event_unlocked_frames (const Evoral::Event& ev, fr const Evoral::MusicalTime last_time_beats = converter.from (_last_ev_time_frames); const Evoral::MusicalTime delta_time_beats = ev_time_beats - last_time_beats; - const uint32_t delta_time_ticks = (uint32_t)(lrint(delta_time_beats * (double)ppqn())); + const uint32_t delta_time_ticks = delta_time_beats.to_ticks(ppqn()); Evoral::SMF::append_event_delta(delta_time_ticks, ev.size(), ev.buffer(), event_id); _last_ev_time_frames = ev.time(); @@ -516,7 +516,7 @@ SMFSource::mark_streaming_midi_write_started (NoteMode mode) MidiSource::mark_streaming_midi_write_started (mode); Evoral::SMF::begin_write (); - _last_ev_time_beats = 0.0; + _last_ev_time_beats = Evoral::MusicalTime(); _last_ev_time_frames = 0; } @@ -586,8 +586,8 @@ SMFSource::safe_midi_file_extension (const string& file) } static bool compare_eventlist ( - const std::pair< Evoral::Event*, gint >& a, - const std::pair< Evoral::Event*, gint >& b) { + const std::pair< Evoral::Event*, gint >& a, + const std::pair< Evoral::Event*, gint >& b) { return ( a.first->time() < b.first->time() ); } @@ -620,7 +620,7 @@ SMFSource::load_model (bool lock, bool force_reload) Evoral::SMF::seek_to_start(); uint64_t time = 0; /* in SMF ticks */ - Evoral::Event ev; + Evoral::Event ev; uint32_t scratch_size = 0; // keep track of scratch and minimize reallocs @@ -632,7 +632,7 @@ SMFSource::load_model (bool lock, bool force_reload) bool have_event_id; // TODO simplify event allocation - std::list< std::pair< Evoral::Event*, gint > > eventlist; + std::list< std::pair< Evoral::Event*, gint > > eventlist; for (unsigned i = 1; i <= num_tracks(); ++i) { if (seek_to_track(i)) continue; @@ -658,8 +658,8 @@ SMFSource::load_model (bool lock, bool force_reload) if (!have_event_id) { event_id = Evoral::next_event_id(); } - uint32_t event_type = midi_parameter_type(buf[0]); - double event_time = time / (double) ppqn(); + const uint32_t event_type = midi_parameter_type(buf[0]); + const Evoral::MusicalTime event_time = Evoral::MusicalTime::ticks_at_rate(time, ppqn()); #ifndef NDEBUG std::string ss; @@ -674,7 +674,7 @@ SMFSource::load_model (bool lock, bool force_reload) #endif eventlist.push_back(make_pair ( - new Evoral::Event ( + new Evoral::Event ( event_type, event_time, size, buf, true) , event_id)); @@ -693,7 +693,7 @@ SMFSource::load_model (bool lock, bool force_reload) eventlist.sort(compare_eventlist); - std::list< std::pair< Evoral::Event*, gint > >::iterator it; + std::list< std::pair< Evoral::Event*, gint > >::iterator it; for (it=eventlist.begin(); it!=eventlist.end(); ++it) { _model->append (*it->first, it->second); delete it->first; diff --git a/libs/ardour/tempo.cc b/libs/ardour/tempo.cc index 57bf9fe674..1767c8100d 100644 --- a/libs/ardour/tempo.cc +++ b/libs/ardour/tempo.cc @@ -1891,10 +1891,11 @@ TempoMap::framepos_plus_beats (framepos_t pos, Evoral::MusicalTime beats) const framecnt_t distance_frames = (next_tempo == metrics.end() ? max_framepos : ((*next_tempo)->frame() - pos)); /* Distance to the end in beats */ - Evoral::MusicalTime distance_beats = distance_frames / tempo->frames_per_beat (_frame_rate); + Evoral::MusicalTime distance_beats = Evoral::MusicalTime::ticks_at_rate( + distance_frames, tempo->frames_per_beat (_frame_rate)); /* Amount to subtract this time */ - double const delta = min (distance_beats, beats); + Evoral::MusicalTime const delta = min (distance_beats, beats); DEBUG_TRACE (DEBUG::TempoMath, string_compose ("\tdistance to %1 = %2 (%3 beats)\n", (next_tempo == metrics.end() ? max_framepos : (*next_tempo)->frame()), @@ -1902,7 +1903,7 @@ TempoMap::framepos_plus_beats (framepos_t pos, Evoral::MusicalTime beats) const /* Update */ beats -= delta; - pos += delta * tempo->frames_per_beat (_frame_rate); + pos += delta.to_ticks(tempo->frames_per_beat (_frame_rate)); DEBUG_TRACE (DEBUG::TempoMath, string_compose ("\tnow at %1, %2 beats left\n", pos, beats)); @@ -1999,10 +2000,11 @@ TempoMap::framepos_minus_beats (framepos_t pos, Evoral::MusicalTime beats) const framecnt_t distance_frames = (pos - tempo->frame()); /* Distance to the start in beats */ - Evoral::MusicalTime distance_beats = distance_frames / tempo->frames_per_beat (_frame_rate); + Evoral::MusicalTime distance_beats = Evoral::MusicalTime::ticks_at_rate( + distance_frames, tempo->frames_per_beat (_frame_rate)); /* Amount to subtract this time */ - double const sub = min (distance_beats, beats); + Evoral::MusicalTime const sub = min (distance_beats, beats); DEBUG_TRACE (DEBUG::TempoMath, string_compose ("\tdistance to %1 = %2 (%3 beats)\n", tempo->frame(), distance_frames, distance_beats)); @@ -2035,7 +2037,7 @@ TempoMap::framepos_minus_beats (framepos_t pos, Evoral::MusicalTime beats) const } } else { pos -= llrint (beats * tempo->frames_per_beat (_frame_rate)); - beats = 0; + beats = Evoral::MusicalTime(); } } @@ -2216,7 +2218,7 @@ TempoMap::framewalk_to_beats (framepos_t pos, framecnt_t distance) const string_compose ("frame %1 walk by %2 frames, start with tempo = %3 @ %4\n", pos, distance, *((const Tempo*)tempo), tempo->frame())); - Evoral::MusicalTime beats = 0; + Evoral::MusicalTime beats = Evoral::MusicalTime(); while (distance) { @@ -2234,8 +2236,8 @@ TempoMap::framewalk_to_beats (framepos_t pos, framecnt_t distance) const distance_to_end = end - pos; } - /* Amount to subtract this time */ - double const sub = min (distance, distance_to_end); + /* Amount to subtract this time in frames */ + framecnt_t const sub = min (distance, distance_to_end); DEBUG_TRACE (DEBUG::TempoMath, string_compose ("to reach end at %1 (end ? %2), distance= %3 sub=%4\n", end, (next_tempo == metrics.end()), distance_to_end, sub)); @@ -2244,7 +2246,7 @@ TempoMap::framewalk_to_beats (framepos_t pos, framecnt_t distance) const pos += sub; distance -= sub; assert (tempo); - beats += sub / tempo->frames_per_beat (_frame_rate); + beats += Evoral::MusicalTime::ticks_at_rate(sub, tempo->frames_per_beat (_frame_rate)); DEBUG_TRACE (DEBUG::TempoMath, string_compose ("now at %1, beats = %2 distance left %3\n", pos, beats, distance)); diff --git a/libs/ardour/test/framepos_minus_beats_test.cc b/libs/ardour/test/framepos_minus_beats_test.cc index 01bafc1606..e4c4844146 100644 --- a/libs/ardour/test/framepos_minus_beats_test.cc +++ b/libs/ardour/test/framepos_minus_beats_test.cc @@ -7,6 +7,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION (FrameposMinusBeatsTest); using namespace std; using namespace ARDOUR; using namespace Timecode; +using namespace Evoral; /* Basic tests with no tempo / meter changes */ void @@ -25,11 +26,11 @@ FrameposMinusBeatsTest::singleTempoTest () map.add_tempo (tempo, BBT_Time (1, 1, 0)); /* Subtract 1 beat from beat 3 of the first bar */ - framepos_t r = map.framepos_minus_beats (frames_per_beat * 2, 1); + framepos_t r = map.framepos_minus_beats (frames_per_beat * 2, MusicalTime(1)); CPPUNIT_ASSERT_EQUAL (r, framepos_t (frames_per_beat * 1)); /* Subtract 4 beats from 3 beats in, to go beyond zero */ - r = map.framepos_minus_beats (frames_per_beat * 3, 4); + r = map.framepos_minus_beats (frames_per_beat * 3, MusicalTime(4)); CPPUNIT_ASSERT_EQUAL (r, framepos_t (- frames_per_beat)); } @@ -69,15 +70,15 @@ FrameposMinusBeatsTest::doubleTempoTest () /* Now some tests */ /* Subtract 1 beat from 1|2 */ - framepos_t r = map.framepos_minus_beats (24e3, 1); + framepos_t r = map.framepos_minus_beats (24e3, MusicalTime(1)); CPPUNIT_ASSERT_EQUAL (r, framepos_t (0)); /* Subtract 2 beats from 4|2 (over the tempo change) */ - r = map.framepos_minus_beats (288e3 + 12e3, 2); + r = map.framepos_minus_beats (288e3 + 12e3, MusicalTime(2)); CPPUNIT_ASSERT_EQUAL (r, framepos_t (288e3 - 24e3)); /* Subtract 2.5 beats from 4|2 (over the tempo change) */ - r = map.framepos_minus_beats (288e3 + 12e3, 2.5); + r = map.framepos_minus_beats (288e3 + 12e3, MusicalTime(2.5)); CPPUNIT_ASSERT_EQUAL (r, framepos_t (288e3 - 24e3 - 12e3)); } @@ -123,15 +124,15 @@ FrameposMinusBeatsTest::doubleTempoWithMeterTest () /* Now some tests */ /* Subtract 1 beat from 1|2 */ - framepos_t r = map.framepos_minus_beats (24e3, 1); + framepos_t r = map.framepos_minus_beats (24e3, MusicalTime(1)); CPPUNIT_ASSERT_EQUAL (r, framepos_t (0)); /* Subtract 2 beats from 4|2 (over the tempo change) */ - r = map.framepos_minus_beats (288e3 + 12e3, 2); + r = map.framepos_minus_beats (288e3 + 12e3, MusicalTime(2)); CPPUNIT_ASSERT_EQUAL (r, framepos_t (288e3 - 24e3)); /* Subtract 2.5 beats from 4|2 (over the tempo change) */ - r = map.framepos_minus_beats (288e3 + 12e3, 2.5); + r = map.framepos_minus_beats (288e3 + 12e3, MusicalTime(2.5)); CPPUNIT_ASSERT_EQUAL (r, framepos_t (288e3 - 24e3 - 12e3)); } diff --git a/libs/ardour/test/framepos_plus_beats_test.cc b/libs/ardour/test/framepos_plus_beats_test.cc index c95c2a7a66..abe1310f62 100644 --- a/libs/ardour/test/framepos_plus_beats_test.cc +++ b/libs/ardour/test/framepos_plus_beats_test.cc @@ -25,11 +25,11 @@ FrameposPlusBeatsTest::singleTempoTest () map.add_tempo (tempo, BBT_Time (1, 1, 0)); /* Add 1 beat to beat 3 of the first bar */ - framepos_t r = map.framepos_plus_beats (frames_per_beat * 2, 1); + framepos_t r = map.framepos_plus_beats (frames_per_beat * 2, Evoral::MusicalTime(1)); CPPUNIT_ASSERT_EQUAL (framepos_t (frames_per_beat * 3), r); /* Add 4 beats to a -ve frame of 1 beat before zero */ - r = map.framepos_plus_beats (-frames_per_beat * 1, 4); + r = map.framepos_plus_beats (-frames_per_beat * 1, Evoral::MusicalTime(4)); CPPUNIT_ASSERT_EQUAL (framepos_t (frames_per_beat * 3), r); } @@ -69,15 +69,15 @@ FrameposPlusBeatsTest::doubleTempoTest () /* Now some tests */ /* Add 1 beat to 1|2 */ - framepos_t r = map.framepos_plus_beats (24e3, 1); + framepos_t r = map.framepos_plus_beats (24e3, Evoral::MusicalTime(1)); CPPUNIT_ASSERT_EQUAL (framepos_t (48e3), r); /* Add 2 beats to 3|4 (over the tempo change) */ - r = map.framepos_plus_beats (264e3, 2); + r = map.framepos_plus_beats (264e3, Evoral::MusicalTime(2)); CPPUNIT_ASSERT_EQUAL (framepos_t (264e3 + 24e3 + 12e3), r); /* Add 2.5 beats to 3|3|960 (over the tempo change) */ - r = map.framepos_plus_beats (264e3 - 12e3, 2.5); + r = map.framepos_plus_beats (264e3 - 12e3, Evoral::MusicalTime(2.5)); CPPUNIT_ASSERT_EQUAL (framepos_t (264e3 + 24e3 + 12e3), r); } @@ -123,15 +123,15 @@ FrameposPlusBeatsTest::doubleTempoWithMeterTest () /* Now some tests */ /* Add 1 beat to 1|2 */ - framepos_t r = map.framepos_plus_beats (24e3, 1); + framepos_t r = map.framepos_plus_beats (24e3, Evoral::MusicalTime(1)); CPPUNIT_ASSERT_EQUAL (framepos_t (48e3), r); /* Add 2 beats to 3|4 (over the tempo change) */ - r = map.framepos_plus_beats (264e3, 2); + r = map.framepos_plus_beats (264e3, Evoral::MusicalTime(2)); CPPUNIT_ASSERT_EQUAL (framepos_t (264e3 + 24e3 + 12e3), r); /* Add 2.5 beats to 3|3|960 (over the tempo change) */ - r = map.framepos_plus_beats (264e3 - 12e3, 2.5); + r = map.framepos_plus_beats (264e3 - 12e3, Evoral::MusicalTime(2.5)); CPPUNIT_ASSERT_EQUAL (framepos_t (264e3 + 24e3 + 12e3), r); } diff --git a/libs/evoral/evoral/Event.hpp b/libs/evoral/evoral/Event.hpp index 59e5612e0e..059b64225b 100644 --- a/libs/evoral/evoral/Event.hpp +++ b/libs/evoral/evoral/Event.hpp @@ -48,7 +48,7 @@ template class LIBEVORAL_API Event { public: #ifdef EVORAL_EVENT_ALLOC - Event (EventType type=0, Time time=0, uint32_t size=0, uint8_t* buf=NULL, bool alloc=false); + Event (EventType type=0, Time time=Time(), uint32_t size=0, uint8_t* buf=NULL, bool alloc=false); /** Copy \a copy. * @@ -114,11 +114,11 @@ public: } inline void clear() { - _type = 0; - _original_time = 0; - _nominal_time = 0; - _size = 0; - _buf = NULL; + _type = 0; + _original_time = Time(); + _nominal_time = Time(); + _size = 0; + _buf = NULL; } #else @@ -154,9 +154,6 @@ protected: #endif }; -} // namespace Evoral - - template /*LIBEVORAL_API*/ std::ostream& operator<<(std::ostream& o, const Evoral::Event