From 6309807bc77347ad2889a3e08fa2721ffa345194 Mon Sep 17 00:00:00 2001 From: nick_m Date: Tue, 24 May 2016 04:17:35 +1000 Subject: [PATCH] Tempo ramps - rename for consistency, remove some testing code, add meter based bbt->pulse conversions. --- gtk2_ardour/editor_drag.cc | 2 +- gtk2_ardour/editor_tempodisplay.cc | 8 +-- libs/ardour/ardour/tempo.h | 15 +++-- libs/ardour/lv2_plugin.cc | 2 +- libs/ardour/region.cc | 2 +- libs/ardour/tempo.cc | 97 +++++++++++++++++++++--------- 6 files changed, 87 insertions(+), 39 deletions(-) diff --git a/gtk2_ardour/editor_drag.cc b/gtk2_ardour/editor_drag.cc index 16c1862dd8..13bc2f73d1 100644 --- a/gtk2_ardour/editor_drag.cc +++ b/gtk2_ardour/editor_drag.cc @@ -3178,7 +3178,7 @@ MeterMarkerDrag::motion (GdkEvent* event, bool first_move) } else { --bbt.bars; } - const double beat = map.bbt_to_beats (bbt); + const double beat = map.beat_at_bbt (bbt); _real_section = map.add_meter (Meter (_marker->meter().divisions_per_bar(), _marker->meter().note_divisor()) , beat, bbt, map.frame_time (bbt), _real_section->position_lock_style()); diff --git a/gtk2_ardour/editor_tempodisplay.cc b/gtk2_ardour/editor_tempodisplay.cc index 82906137c7..e0af960d40 100644 --- a/gtk2_ardour/editor_tempodisplay.cc +++ b/gtk2_ardour/editor_tempodisplay.cc @@ -340,9 +340,9 @@ Editor::mouse_add_new_meter_event (framepos_t frame) XMLNode &before = map.get_state(); if (meter_dialog.get_lock_style() == MusicTime) { - map.add_meter (Meter (bpb, note_type), map.bbt_to_beats (requested), requested, 0, MusicTime); + map.add_meter (Meter (bpb, note_type), map.beat_at_bbt (requested), requested, 0, MusicTime); } else { - map.add_meter (Meter (bpb, note_type), map.bbt_to_beats (requested), requested, map.frame_time (requested), AudioTime); + map.add_meter (Meter (bpb, note_type), map.beat_at_bbt (requested), requested, map.frame_time (requested), AudioTime); } _session->add_command(new MementoCommand(map, &before, &map.get_state())); @@ -389,8 +389,8 @@ Editor::edit_meter_section (MeterSection* section) double const note_type = meter_dialog.get_note_type (); Timecode::BBT_Time when; - meter_dialog.get_bbt_time(when); - framepos_t const frame = _session->tempo_map().frame_at_beat (_session->tempo_map().bbt_to_beats (when)); + meter_dialog.get_bbt_time (when); + framepos_t const frame = _session->tempo_map().frame_time (when); begin_reversible_command (_("replace meter mark")); XMLNode &before = _session->tempo_map().get_state(); diff --git a/libs/ardour/ardour/tempo.h b/libs/ardour/ardour/tempo.h index 5416ad3462..160f7c266a 100644 --- a/libs/ardour/ardour/tempo.h +++ b/libs/ardour/ardour/tempo.h @@ -442,9 +442,11 @@ class LIBARDOUR_API TempoMap : public PBD::StatefulDestructible framecnt_t frame_rate () const { return _frame_rate; } - double bbt_to_beats (const Timecode::BBT_Time& bbt); - Timecode::BBT_Time beats_to_bbt (const double& beats); - Timecode::BBT_Time pulse_to_bbt (const double& pulse); + double beat_at_bbt (const Timecode::BBT_Time& bbt); + Timecode::BBT_Time bbt_at_beat (const double& beats); + + double pulse_at_bbt (const Timecode::BBT_Time& bbt); + Timecode::BBT_Time bbt_at_pulse (const double& pulse); double pulse_at_beat (const double& beat) const; double beat_at_pulse (const double& pulse) const; @@ -464,8 +466,11 @@ private: double beat_at_frame_locked (const Metrics& metrics, const framecnt_t& frame) const; framecnt_t frame_at_beat_locked (const Metrics& metrics, const double& beat) const; - double bbt_to_beats_locked (const Metrics& metrics, const Timecode::BBT_Time& bbt) const ; - Timecode::BBT_Time beats_to_bbt_locked (const Metrics& metrics, const double& beats) const; + double beat_at_bbt_locked (const Metrics& metrics, const Timecode::BBT_Time& bbt) const ; + Timecode::BBT_Time bbt_at_beat_locked (const Metrics& metrics, const double& beats) const; + + double pulse_at_bbt_locked (const Metrics& metrics, const Timecode::BBT_Time& bbt) const; + Timecode::BBT_Time bbt_at_pulse_locked (const Metrics& metrics, const double& pulse) const; framepos_t frame_time_locked (const Metrics& metrics, const Timecode::BBT_Time&) const; diff --git a/libs/ardour/lv2_plugin.cc b/libs/ardour/lv2_plugin.cc index 091ca0d667..fa11f7b051 100644 --- a/libs/ardour/lv2_plugin.cc +++ b/libs/ardour/lv2_plugin.cc @@ -2342,7 +2342,7 @@ LV2Plugin::connect_and_run(BufferSet& bufs, ++m; } else { tmetric.set_metric(metric); - bbt = tmap.pulse_to_bbt (metric->pulse()); + bbt = tmap.bbt_at_pulse (metric->pulse()); write_position(&_impl->forge, _ev_buffers[port_index], tmetric, bbt, _session.transport_speed(), metric->frame(), diff --git a/libs/ardour/region.cc b/libs/ardour/region.cc index ca178a9f7b..0ae709d99e 100644 --- a/libs/ardour/region.cc +++ b/libs/ardour/region.cc @@ -1285,7 +1285,7 @@ Region::_set_state (const XMLNode& node, int /*version*/, PropertyChange& what_c &bbt_time.ticks) != 3) { _position_lock_style = AudioTime; } else { - _beat = _session.tempo_map().bbt_to_beats (bbt_time); + _beat = _session.tempo_map().beat_at_bbt (bbt_time); } } } diff --git a/libs/ardour/tempo.cc b/libs/ardour/tempo.cc index 4a0151b22c..faa93632b8 100644 --- a/libs/ardour/tempo.cc +++ b/libs/ardour/tempo.cc @@ -775,10 +775,10 @@ TempoMap::do_insert (MetricSection* section) if ((m->bbt().beats != 1) || (m->bbt().ticks != 0)) { - pair corrected = make_pair (m->pulse(), m->bbt()); + pair corrected = make_pair (m->beat(), m->bbt()); corrected.second.beats = 1; corrected.second.ticks = 0; - corrected.first = bbt_to_beats_locked (_metrics, corrected.second); + corrected.first = beat_at_bbt_locked (_metrics, corrected.second); warning << string_compose (_("Meter changes can only be positioned on the first beat of a bar. Moving from %1 to %2"), m->bbt(), corrected.second) << endmsg; //m->set_pulse (corrected); @@ -981,7 +981,7 @@ TempoMap::replace_meter (const MeterSection& ms, const Meter& meter, const BBT_T { { Glib::Threads::RWLock::WriterLock lm (lock); - const double beat = bbt_to_beats_locked (_metrics, where); + const double beat = beat_at_bbt_locked (_metrics, where); if (ms.movable()) { remove_meter_locked (ms); @@ -1576,15 +1576,15 @@ TempoMap::frame_at_beat_locked (const Metrics& metrics, const double& beat) cons } double -TempoMap::bbt_to_beats (const Timecode::BBT_Time& bbt) +TempoMap::beat_at_bbt (const Timecode::BBT_Time& bbt) { Glib::Threads::RWLock::ReaderLock lm (lock); - return bbt_to_beats_locked (_metrics, bbt); + return beat_at_bbt_locked (_metrics, bbt); } double -TempoMap::bbt_to_beats_locked (const Metrics& metrics, const Timecode::BBT_Time& bbt) const +TempoMap::beat_at_bbt_locked (const Metrics& metrics, const Timecode::BBT_Time& bbt) const { /* CALLER HOLDS READ LOCK */ @@ -1614,14 +1614,14 @@ TempoMap::bbt_to_beats_locked (const Metrics& metrics, const Timecode::BBT_Time& } Timecode::BBT_Time -TempoMap::beats_to_bbt (const double& beats) +TempoMap::bbt_at_beat (const double& beats) { Glib::Threads::RWLock::ReaderLock lm (lock); - return beats_to_bbt_locked (_metrics, beats); + return bbt_at_beat_locked (_metrics, beats); } Timecode::BBT_Time -TempoMap::beats_to_bbt_locked (const Metrics& metrics, const double& b) const +TempoMap::bbt_at_beat_locked (const Metrics& metrics, const double& b) const { /* CALLER HOLDS READ LOCK */ MeterSection* prev_m = 0; @@ -1671,13 +1671,57 @@ TempoMap::beats_to_bbt_locked (const Metrics& metrics, const double& b) const return ret; } +double +TempoMap::pulse_at_bbt (const Timecode::BBT_Time& bbt) +{ + Glib::Threads::RWLock::ReaderLock lm (lock); + + return pulse_at_bbt_locked (_metrics, bbt); +} + +double +TempoMap::pulse_at_bbt_locked (const Metrics& metrics, const Timecode::BBT_Time& bbt) const +{ + /* CALLER HOLDS READ LOCK */ + + MeterSection* prev_m = 0; + + /* because audio-locked meters have 'fake' integral beats, + there is no pulse offset here. + */ + for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) { + MeterSection* m; + if ((m = dynamic_cast (*i)) != 0) { + if (prev_m) { + if (m->bbt().bars > bbt.bars) { + break; + } + } + prev_m = m; + } + } + + const double remaining_bars = bbt.bars - prev_m->bbt().bars; + const double remaining_pulses = remaining_bars * prev_m->divisions_per_bar() / prev_m->note_divisor(); + const double ret = remaining_pulses + prev_m->pulse(); + + return ret; +} + Timecode::BBT_Time -TempoMap::pulse_to_bbt (const double& pulse) +TempoMap::bbt_at_pulse (const double& pulse) { Glib::Threads::RWLock::ReaderLock lm (lock); + + return bbt_at_pulse_locked (_metrics, pulse); +} + +Timecode::BBT_Time +TempoMap::bbt_at_pulse_locked (const Metrics& metrics, const double& pulse) const +{ MeterSection* prev_m = 0; - for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) { + for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) { MeterSection* m = 0; if ((m = dynamic_cast (*i)) != 0) { @@ -1737,7 +1781,7 @@ TempoMap::bbt_time (framepos_t frame, BBT_Time& bbt) Glib::Threads::RWLock::ReaderLock lm (lock); const double beat = beat_at_frame_locked (_metrics, frame); - bbt = beats_to_bbt_locked (_metrics, beat); + bbt = bbt_at_beat_locked (_metrics, beat); } framepos_t @@ -1762,7 +1806,7 @@ TempoMap::frame_time_locked (const Metrics& metrics, const BBT_Time& bbt) const { /* HOLD THE READER LOCK */ - const framepos_t ret = frame_at_beat_locked (metrics, bbt_to_beats_locked (metrics, bbt)); + const framepos_t ret = frame_at_beat_locked (metrics, beat_at_bbt_locked (metrics, bbt)); return ret; } @@ -2304,8 +2348,7 @@ TempoMap::can_solve_bbt (TempoSection* ts, const BBT_Time& bbt) } } - const double beat = bbt_to_beats_locked (copy, bbt); - const bool ret = solve_map_pulse (copy, tempo_copy, pulse_at_beat_locked (copy, beat)); + const bool ret = solve_map_pulse (copy, tempo_copy, pulse_at_bbt_locked (copy, bbt)); Metrics::const_iterator d = copy.begin(); while (d != copy.end()) { @@ -2333,7 +2376,7 @@ TempoMap::predict_tempo_position (TempoSection* section, const BBT_Time& bbt) TempoSection* tempo_copy = copy_metrics_and_point (_metrics, future_map, section); - const double beat = bbt_to_beats_locked (future_map, bbt); + const double beat = beat_at_bbt_locked (future_map, bbt); if (solve_map_pulse (future_map, tempo_copy, pulse_at_beat_locked (future_map, beat))) { ret.first = tempo_copy->pulse(); @@ -2409,7 +2452,7 @@ TempoMap::gui_move_meter (MeterSection* ms, const framepos_t& frame) MeterSection* copy = copy_metrics_and_point (_metrics, future_map, ms); const double beat = beat_at_frame_locked (_metrics, frame); - const Timecode::BBT_Time bbt = beats_to_bbt_locked (_metrics, beat); + const Timecode::BBT_Time bbt = bbt_at_beat_locked (_metrics, beat); if (solve_map_bbt (future_map, copy, bbt)) { solve_map_bbt (_metrics, ms, bbt); @@ -2711,7 +2754,7 @@ TempoMap::round_bbt (BBT_Time& when, const int32_t& sub_num, RoundMode dir) when.beats = 1; when.ticks = 0; } else { - const double bpb = meter_section_at_beat (bbt_to_beats_locked (_metrics, when)).divisions_per_bar(); + const double bpb = meter_section_at_beat (beat_at_bbt_locked (_metrics, when)).divisions_per_bar(); if ((double) when.beats > bpb / 2.0) { ++when.bars; } @@ -2722,7 +2765,7 @@ TempoMap::round_bbt (BBT_Time& when, const int32_t& sub_num, RoundMode dir) return; } else if (sub_num == 0) { - const double bpb = meter_section_at_beat (bbt_to_beats_locked (_metrics, when)).divisions_per_bar(); + const double bpb = meter_section_at_beat (beat_at_bbt_locked (_metrics, when)).divisions_per_bar(); if ((double) when.ticks > BBT_Time::ticks_per_beat / 2.0) { ++when.beats; while ((double) when.beats > bpb) { @@ -2757,7 +2800,7 @@ TempoMap::round_bbt (BBT_Time& when, const int32_t& sub_num, RoundMode dir) if (when.ticks >= BBT_Time::ticks_per_beat) { ++when.beats; - const double bpb = meter_section_at_beat (bbt_to_beats_locked (_metrics, when)).divisions_per_bar(); + const double bpb = meter_section_at_beat (beat_at_bbt_locked (_metrics, when)).divisions_per_bar(); if ((double) when.beats > bpb) { ++when.bars; when.beats = 1; @@ -2778,7 +2821,7 @@ TempoMap::round_bbt (BBT_Time& when, const int32_t& sub_num, RoundMode dir) if (when.ticks < difference) { --when.beats; - const double bpb = meter_section_at_beat (bbt_to_beats_locked (_metrics, when)).divisions_per_bar(); + const double bpb = meter_section_at_beat (beat_at_bbt_locked (_metrics, when)).divisions_per_bar(); if ((double) when.beats < bpb) { --when.bars; //when.beats = 1; @@ -2823,7 +2866,7 @@ TempoMap::round_to_type (framepos_t frame, RoundMode dir, BBTPointType type) Glib::Threads::RWLock::ReaderLock lm (lock); const double beat_at_framepos = beat_at_frame_locked (_metrics, frame); - BBT_Time bbt (beats_to_bbt_locked (_metrics, beat_at_framepos)); + BBT_Time bbt (bbt_at_beat_locked (_metrics, beat_at_framepos)); switch (type) { case Bar: @@ -2886,7 +2929,7 @@ TempoMap::get_grid (vector& points, pos = frame_at_beat_locked (_metrics, cnt); const TempoSection tempo = tempo_section_at_locked (_metrics, pos); const MeterSection meter = meter_section_at_locked (_metrics, pos); - const BBT_Time bbt = beats_to_bbt (cnt); + const BBT_Time bbt = bbt_at_beat_locked (_metrics, cnt); points.push_back (BBTPoint (meter, tempo_at_locked (_metrics, pos), pos, bbt.bars, bbt.beats, tempo.c_func())); ++cnt; } @@ -3397,7 +3440,7 @@ TempoMap::insert_time (framepos_t where, framecnt_t amount) continue; } const double beat = beat_at_pulse_locked (_metrics, t->pulse()); - pair start = make_pair (beat, beats_to_bbt_locked (_metrics, beat)); + pair start = make_pair (beat, bbt_at_beat_locked (_metrics, beat)); ms->set_beat (start); ms->set_pulse (t->pulse()); } @@ -3542,7 +3585,7 @@ TempoMap::framepos_plus_bbt (framepos_t pos, BBT_Time op) const { Glib::Threads::RWLock::ReaderLock lm (lock); - BBT_Time pos_bbt = beats_to_bbt_locked (_metrics, beat_at_frame_locked (_metrics, pos)); + BBT_Time pos_bbt = bbt_at_beat_locked (_metrics, beat_at_frame_locked (_metrics, pos)); pos_bbt.ticks += op.ticks; if (pos_bbt.ticks >= BBT_Time::ticks_per_beat) { ++pos_bbt.beats; @@ -3550,10 +3593,10 @@ TempoMap::framepos_plus_bbt (framepos_t pos, BBT_Time op) const } pos_bbt.beats += op.beats; /* the meter in effect will start on the bar */ - double divisions_per_bar = meter_section_at_beat (bbt_to_beats_locked (_metrics, BBT_Time (pos_bbt.bars + op.bars, 1, 0))).divisions_per_bar(); + double divisions_per_bar = meter_section_at_beat (beat_at_bbt_locked (_metrics, BBT_Time (pos_bbt.bars + op.bars, 1, 0))).divisions_per_bar(); while (pos_bbt.beats >= divisions_per_bar + 1) { ++pos_bbt.bars; - divisions_per_bar = meter_section_at_beat (bbt_to_beats_locked (_metrics, BBT_Time (pos_bbt.bars + op.bars, 1, 0))).divisions_per_bar(); + divisions_per_bar = meter_section_at_beat (beat_at_bbt_locked (_metrics, BBT_Time (pos_bbt.bars + op.bars, 1, 0))).divisions_per_bar(); pos_bbt.beats -= divisions_per_bar; } pos_bbt.bars += op.bars; -- 2.30.2