{
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<TempoSection*> (*i) != 0;
+ bool const b = dynamic_cast<TempoSection*> (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);
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);
}
/* new meters *always* start on a beat. */
-
where.ticks = 0;
do_insert (new MeterSection (where, meter.beats_per_bar(), meter.note_divisor()), true);
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;
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) {
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;
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.
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;
+}