@@ -113,13 +113,16 @@ impl Tablature {
113113 ///
114114 /// Returns the measure and beat indexes
115115 pub fn get_measure_beat_indexes_for_tick ( & self , track_id : usize , tick : u32 ) -> ( usize , usize ) {
116- // get first measure containing the tick
116+ // range scan on `measure_per_tick` to find measure index for tick
117117 let measure_index = self
118118 . measure_per_tick
119- . range ( tick / 2 ..=tick) // synthetic lower bound to avoid large range
119+ . range ( 0 ..=tick) // range is growing but the cost is still O(log n) with size of the btreemap
120120 . next_back ( )
121- . map ( |elem| * elem. 1 )
122- . unwrap_or ( 0 ) ;
121+ . map ( |( _event_tick, & m_id) | m_id)
122+ . unwrap_or_else ( || {
123+ log:: warn!( "No measure index found for tick:{tick}" ) ;
124+ 0
125+ } ) ;
123126
124127 // indexed as u32 to save space
125128 let measure_index = measure_index as usize ;
@@ -142,8 +145,11 @@ impl Tablature {
142145 ///
143146 /// Returns the amount of scroll needed to focus on the beat
144147 pub fn focus_on_tick ( & mut self , tick : u32 ) -> Option < f32 > {
145- let ( new_measure_id, new_beat_id) =
146- self . get_measure_beat_indexes_for_tick ( self . track_id , tick) ;
148+ let ( new_measure_id, new_beat_id) = if tick == 1 {
149+ ( 0 , 0 )
150+ } else {
151+ self . get_measure_beat_indexes_for_tick ( self . track_id , tick)
152+ } ;
147153 let current_focus_id = self . focused_measure ;
148154 let current_canvas = self . canvas_measures . get_mut ( current_focus_id) . unwrap ( ) ;
149155 if current_focus_id == new_measure_id {
0 commit comments