X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Ftempo.cc;h=9b06d8868fdf37118203358ea66ce22b430b4dc7;hb=7ab8dbc2fe679d75821ca585dd7c2663f640116e;hp=b8144d6e3c27264877a168f625ec7120fcc25731;hpb=c4c25e19bc75354ffc69e258c9f167ad586a1cd9;p=ardour.git diff --git a/libs/ardour/tempo.cc b/libs/ardour/tempo.cc index b8144d6e3c..9b06d8868f 100644 --- a/libs/ardour/tempo.cc +++ b/libs/ardour/tempo.cc @@ -24,7 +24,6 @@ #include -#include #include #include "pbd/xml++.h" @@ -300,7 +299,7 @@ void TempoMap::move_tempo (TempoSection& tempo, const BBT_Time& when) { if (move_metric_section (tempo, when) == 0) { - StateChanged (Change (0)); + PropertyChanged (PropertyChange ()); } } @@ -308,7 +307,7 @@ void TempoMap::move_meter (MeterSection& meter, const BBT_Time& when) { if (move_metric_section (meter, when) == 0) { - StateChanged (Change (0)); + PropertyChanged (PropertyChange ()); } } @@ -335,7 +334,7 @@ TempoMap::remove_tempo (const TempoSection& tempo) } if (removed) { - StateChanged (Change (0)); + PropertyChanged (PropertyChange ()); } } @@ -362,7 +361,7 @@ TempoMap::remove_meter (const MeterSection& tempo) } if (removed) { - StateChanged (Change (0)); + PropertyChanged (PropertyChange ()); } } @@ -371,16 +370,46 @@ TempoMap::do_insert (MetricSection* section, bool with_bbt) { Metrics::iterator i; + /* Look for any existing MetricSection that is of the same type and + at the same time as the new one, and remove it before adding + the new one. + */ + + Metrics::iterator to_remove = metrics->end (); + for (i = metrics->begin(); i != metrics->end(); ++i) { - if (with_bbt) { - if ((*i)->start() < section->start()) { - continue; - } - } else { - if ((*i)->frame() < section->frame()) { - continue; - } + int const c = (*i)->compare (section, with_bbt); + + if (c < 0) { + /* this section is before the one to be added; go back round */ + continue; + } else if (c > 0) { + /* this section is after the one to be added; there can't be any at the same time */ + break; + } + + /* hacky comparison of type */ + bool const a = dynamic_cast (*i) != 0; + bool const b = dynamic_cast (section) != 0; + + if (a == b) { + to_remove = i; + break; + } + } + + if (to_remove != metrics->end()) { + /* remove the MetricSection at the same time as the one we are about to add */ + metrics->erase (to_remove); + } + + /* Add the given MetricSection */ + + for (i = metrics->begin(); i != metrics->end(); ++i) { + + if ((*i)->compare (section, with_bbt) < 0) { + continue; } metrics->insert (i, section); @@ -401,13 +430,12 @@ TempoMap::add_tempo (const Tempo& tempo, BBT_Time where) Glib::RWLock::WriterLock lm (lock); /* new tempos always start on a beat */ - where.ticks = 0; do_insert (new TempoSection (where, tempo.beats_per_minute(), tempo.note_type()), true); } - StateChanged (Change (0)); + PropertyChanged (PropertyChange ()); } void @@ -418,7 +446,7 @@ TempoMap::add_tempo (const Tempo& tempo, nframes64_t where) do_insert (new TempoSection (where, tempo.beats_per_minute(), tempo.note_type()), false); } - StateChanged (Change (0)); + PropertyChanged (PropertyChange ()); } void @@ -446,7 +474,7 @@ TempoMap::replace_tempo (TempoSection& existing, const Tempo& replacement) } if (replaced) { - StateChanged (Change (0)); + PropertyChanged (PropertyChange ()); } } @@ -469,13 +497,12 @@ TempoMap::add_meter (const Meter& meter, BBT_Time where) } /* new meters *always* start on a beat. */ - where.ticks = 0; do_insert (new MeterSection (where, meter.beats_per_bar(), meter.note_divisor()), true); } - StateChanged (Change (0)); + PropertyChanged (PropertyChange ()); } void @@ -486,7 +513,7 @@ TempoMap::add_meter (const Meter& meter, nframes64_t where) do_insert (new MeterSection (where, meter.beats_per_bar(), meter.note_divisor()), false); } - StateChanged (Change (0)); + PropertyChanged (PropertyChange ()); } void @@ -512,7 +539,7 @@ TempoMap::replace_meter (MeterSection& existing, const Meter& replacement) } if (replaced) { - StateChanged (Change (0)); + PropertyChanged (PropertyChange ()); } } @@ -525,7 +552,7 @@ TempoMap::change_initial_tempo (double beats_per_minute, double note_type) for (Metrics::iterator i = metrics->begin(); i != metrics->end(); ++i) { if ((t = dynamic_cast (*i)) != 0) { *((Tempo*) t) = newtempo; - StateChanged (Change (0)); + PropertyChanged (PropertyChange ()); break; } } @@ -571,7 +598,7 @@ TempoMap::change_existing_tempo_at (nframes64_t where, double beats_per_minute, /* reset */ *((Tempo*)prev) = newtempo; - StateChanged (Change (0)); + PropertyChanged (PropertyChange ()); } const MeterSection& @@ -1104,7 +1131,6 @@ TempoMap::round_to_beat (nframes64_t fr, int dir) nframes64_t TempoMap::round_to_beat_subdivision (nframes64_t fr, int sub_num, int dir) { - BBT_Time the_beat; uint32_t ticks_one_half_subdivisions_worth; uint32_t ticks_one_subdivisions_worth; @@ -1131,13 +1157,7 @@ TempoMap::round_to_beat_subdivision (nframes64_t fr, int sub_num, int dir) difference = ticks_one_subdivisions_worth - mod; } - if (the_beat.ticks + difference >= (uint32_t)Meter::ticks_per_beat) { - the_beat.beats++; - the_beat.ticks += difference; - the_beat.ticks -= (uint32_t)Meter::ticks_per_beat; - } else { - the_beat.ticks += difference; - } + the_beat = bbt_add (the_beat, BBT_Time (0, 0, difference)); } else if (dir < 0) { @@ -1148,41 +1168,22 @@ TempoMap::round_to_beat_subdivision (nframes64_t fr, int sub_num, int dir) if (mod == 0) { /* right on the subdivision, so the difference is just the subdivision ticks */ difference = ticks_one_subdivisions_worth; - cerr << "On the sub, move by 1 sub = " << difference << endl; } else { /* not on subdivision, compute distance to previous subdivision, which is just the modulus. */ difference = mod; - cerr << "off the sub, move by 1 sub = " << difference << endl; } - - cerr << "ticks = " << the_beat.ticks << endl; - - if (the_beat.ticks < difference) { - cerr << "backup beats, set ticks to " - << (uint32_t)Meter::ticks_per_beat - difference << endl; - the_beat.beats--; - the_beat.ticks = (uint32_t)Meter::ticks_per_beat - difference; - } else { - cerr << " reduce ticks\n"; - the_beat.ticks -= difference; - } + the_beat = bbt_subtract (the_beat, BBT_Time (0, 0, difference)); } else { /* round to nearest */ if (the_beat.ticks % ticks_one_subdivisions_worth > ticks_one_half_subdivisions_worth) { difference = ticks_one_subdivisions_worth - (the_beat.ticks % ticks_one_subdivisions_worth); - if (the_beat.ticks + difference >= (uint32_t)Meter::ticks_per_beat) { - the_beat.beats++; - the_beat.ticks += difference; - the_beat.ticks -= (uint32_t)Meter::ticks_per_beat; - } else { - the_beat.ticks += difference; - } + the_beat = bbt_add (the_beat, BBT_Time (0, 0, difference)); } else { // difference = ticks_one_subdivisions_worth - (the_beat.ticks % ticks_one_subdivisions_worth); the_beat.ticks -= the_beat.ticks % ticks_one_subdivisions_worth; @@ -1205,7 +1206,7 @@ TempoMap::round_to_type (nframes64_t frame, int dir, BBTPointType type) switch (type) { case Bar: - DEBUG_TRACE(DEBUG::SnapBBT, string_compose ("round from %1 (%3) to bars in direction %2\n", frame, (dir < 0 ? "back" : "forward"), bbt)); + DEBUG_TRACE(DEBUG::SnapBBT, string_compose ("round from %1 (%3) to bars in direction %2\n", frame, dir, bbt)); if (dir < 0) { @@ -1235,25 +1236,25 @@ TempoMap::round_to_type (nframes64_t frame, int dir, BBTPointType type) /* "true" rounding */ - /* round to nearest beat */ - if (bbt.ticks >= (Meter::ticks_per_beat/2)) { - try { - bbt = bbt_add (bbt, one_beat, metric); - } - catch (...) { - return frame; - } - } + float midbar_beats; + float midbar_ticks; - /* round to nearest bar */ - if (bbt.beats >= metric.meter().beats_per_bar()/2) { - try { - bbt = bbt_add (bbt, one_bar, metric); - } - catch (...) { - return frame; - } - } + midbar_beats = metric.meter().beats_per_bar() / 2 + 1; + midbar_ticks = Meter::ticks_per_beat * fmod (midbar_beats, 1.0f); + midbar_beats = floor (midbar_beats); + + BBT_Time midbar (bbt.bars, lrintf (midbar_beats), lrintf (midbar_ticks)); + + if (bbt < midbar) { + /* round down */ + bbt.beats = 1; + bbt.ticks = 0; + } else { + /* round up */ + bbt.bars++; + bbt.beats = 1; + bbt.ticks = 0; + } } /* force beats & ticks to their values at the start of a bar */ bbt.beats = 1; @@ -1610,7 +1611,7 @@ TempoMap::set_state (const XMLNode& node, int /*version*/) } } - StateChanged (Change (0)); + PropertyChanged (PropertyChange ()); return 0; } @@ -1674,7 +1675,7 @@ TempoMap::insert_time (nframes64_t where, nframes64_t amount) timestamp_metrics (false); - StateChanged (Change (0)); + PropertyChanged (PropertyChange ()); } BBT_Time @@ -1684,17 +1685,22 @@ TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& other) const return bbt_add (start, other, metric); } +/** + * add the BBT interval @param increment to @param start and return the result + */ BBT_Time -TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& other, const TempoMetric& metric) const +TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& increment, const TempoMetric& /*metric*/) const { BBT_Time result = start; - BBT_Time op = other; + BBT_Time op = increment; /* argument is const, but we need to modify it */ uint32_t ticks = result.ticks + op.ticks; if (ticks >= Meter::ticks_per_beat) { op.beats++; result.ticks = ticks % (uint32_t) Meter::ticks_per_beat; - } + } else { + result.ticks += op.ticks; + } /* now comes the complicated part. we have to add one beat a time, checking for a new metric on every beat. @@ -1780,11 +1786,14 @@ TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& other, const TempoMetr return result; } +/** + * subtract the BBT interval @param decrement from @param start and return the result + */ BBT_Time -TempoMap::bbt_subtract (const BBT_Time& start, const BBT_Time& other) const +TempoMap::bbt_subtract (const BBT_Time& start, const BBT_Time& decrement) const { BBT_Time result = start; - BBT_Time op = other; + BBT_Time op = decrement; /* argument is const, but we need to modify it */ if (op.ticks > result.ticks) { /* subtract an extra beat later; meanwhile set ticks to the right "carry" value */ @@ -1817,7 +1826,8 @@ TempoMap::bbt_subtract (const BBT_Time& start, const BBT_Time& other) const */ const MeterSection* meter = 0; - list::const_reverse_iterator next_meter; + list::reverse_iterator next_meter; // older versions of GCC don't + // support const_reverse_iterator::operator!=() for (next_meter = meter_sections.rbegin(); next_meter != meter_sections.rend(); ++next_meter) { @@ -1886,3 +1896,33 @@ TempoMap::bbt_subtract (const BBT_Time& start, const BBT_Time& other) const result.bars -= op.bars; return result; } + +/** Compare the time of this with that of another MetricSection. + * @param with_bbt True to compare using ::start(), false to use ::frame(). + * @return -1 for less than, 0 for equal, 1 for greater than. + */ + +int +MetricSection::compare (MetricSection* other, bool with_bbt) const +{ + if (with_bbt) { + if (start() == other->start()) { + return 0; + } else if (start() < other->start()) { + return -1; + } else { + return 1; + } + } else { + if (frame() == other->frame()) { + return 0; + } else if (frame() < other->frame()) { + return -1; + } else { + return 1; + } + } + + /* NOTREACHED */ + return 0; +}