UNPKG

@stringsync/vexml

Version:

MusicXML to Vexflow

31 lines (30 loc) 831 B
import * as util from '../util'; /** * A CursorFrameLocator that uses binary search to locate the frame at a given time. */ export class BSearchCursorFrameLocator { path; constructor(path) { this.path = path; } locate(time) { const frames = this.path.getFrames(); let left = 0; let right = frames.length - 1; while (left <= right) { const mid = Math.floor((left + right) / 2); const entry = frames.at(mid); util.assertDefined(entry); if (entry.tRange.includes(time)) { return mid; } if (entry.tRange.end.isGreaterThanOrEqual(time)) { right = mid - 1; } else { left = mid + 1; } } return null; } }