serialize/deserialize MIDISceneChange color; put out of bounds color into static...
[ardour.git] / libs / ardour / tempo.cc
index 80091e7eec01ed90b35514e40a023c9b444d5977..621d1e2c747d0d30bfe4322184cee1db044b8b54 100644 (file)
@@ -884,6 +884,7 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
        TempoSection* ts;
        MeterSection* ms;
        double beat_frames;
+       double current_frame_exact;
        framepos_t bar_start_frame;
 
        DEBUG_TRACE (DEBUG::TempoMath, string_compose ("Extend map to %1 from %2 = %3\n", end, current, current_frame));
@@ -895,11 +896,13 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
        }
 
        beat_frames = meter->frames_per_grid (*tempo,_frame_rate);
+       current_frame_exact = current_frame;
 
        while (current_frame < end) {
 
                current.beats++;
-               current_frame += beat_frames;
+               current_frame_exact += beat_frames;
+               current_frame = llrint(current_frame_exact);
 
                if (current.beats > meter->divisions_per_bar()) {
                        current.bars++;
@@ -942,7 +945,8 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
                                                                                               tempo->start(), current_frame, tempo->bar_offset()));
                                                
                                                /* back up to previous beat */
-                                               current_frame -= beat_frames;
+                                               current_frame_exact -= beat_frames;
+                                               current_frame = llrint(current_frame_exact);
 
                                                /* set tempo section location
                                                 * based on offset from last
@@ -963,7 +967,8 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
 
                                                double offset_within_old_beat = (tempo->frame() - current_frame) / beat_frames;
 
-                                               current_frame += (offset_within_old_beat * beat_frames) + ((1.0 - offset_within_old_beat) * next_beat_frames);
+                                               current_frame_exact += (offset_within_old_beat * beat_frames) + ((1.0 - offset_within_old_beat) * next_beat_frames);
+                                               current_frame = llrint(current_frame_exact);
 
                                                /* next metric doesn't have to
                                                 * match this precisely to
@@ -1012,11 +1017,11 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
 
                if (current.beats == 1) {
                        DEBUG_TRACE (DEBUG::TempoMath, string_compose ("Add Bar at %1|1 @ %2\n", current.bars, current_frame));
-                       _map.push_back (BBTPoint (*meter, *tempo,(framepos_t) llrint(current_frame), current.bars, 1));
+                       _map.push_back (BBTPoint (*meter, *tempo, current_frame, current.bars, 1));
                        bar_start_frame = current_frame;
                } else {
                        DEBUG_TRACE (DEBUG::TempoMath, string_compose ("Add Beat at %1|%2 @ %3\n", current.bars, current.beats, current_frame));
-                       _map.push_back (BBTPoint (*meter, *tempo, (framepos_t) llrint(current_frame), current.bars, current.beats));
+                       _map.push_back (BBTPoint (*meter, *tempo, current_frame, current.bars, current.beats));
                }
 
                if (next_metric == metrics.end()) {
@@ -1031,12 +1036,10 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
 }
 
 TempoMetric
-TempoMap::metric_at (framepos_t frame) const
+TempoMap::metric_at (framepos_t frame, Metrics::const_iterator* last) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
        TempoMetric m (first_meter(), first_tempo());
-       const Meter* meter;
-       const Tempo* tempo;
 
        /* at this point, we are *guaranteed* to have m.meter and m.tempo pointing
           at something, because we insert the default tempo and meter during
@@ -1051,14 +1054,11 @@ TempoMap::metric_at (framepos_t frame) const
                        break;
                }
 
-               if ((tempo = dynamic_cast<const TempoSection*>(*i)) != 0) {
-                       m.set_tempo (*tempo);
-               } else if ((meter = dynamic_cast<const MeterSection*>(*i)) != 0) {
-                       m.set_meter (*meter);
-               }
+               m.set_metric(*i);
 
-               m.set_frame ((*i)->frame ());
-               m.set_start ((*i)->start ());
+               if (last) {
+                       *last = i;
+               }
        }
        
        return m;
@@ -1069,8 +1069,6 @@ TempoMap::metric_at (BBT_Time bbt) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
        TempoMetric m (first_meter(), first_tempo());
-       const Meter* meter;
-       const Tempo* tempo;
 
        /* at this point, we are *guaranteed* to have m.meter and m.tempo pointing
           at something, because we insert the default tempo and meter during
@@ -1087,14 +1085,7 @@ TempoMap::metric_at (BBT_Time bbt) const
                        break;
                }
 
-               if ((tempo = dynamic_cast<const TempoSection*>(*i)) != 0) {
-                       m.set_tempo (*tempo);
-               } else if ((meter = dynamic_cast<const MeterSection*>(*i)) != 0) {
-                       m.set_meter (*meter);
-               }
-
-               m.set_frame ((*i)->frame ());
-               m.set_start (section_start);
+               m.set_metric (*i);
        }
 
        return m;