UNPKG

@stringsync/vexml

Version:

MusicXML to Vexflow

53 lines (52 loc) 1.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FastCursorFrameLocator = void 0; const duration_1 = require("./duration"); /** * A CursorFrameLocator that uses O(1) time complexity to locate the frame at a given time before falling back to a more * expensive locator. */ class FastCursorFrameLocator { path; fallback; index = 0; constructor(path, fallback) { this.path = path; this.fallback = fallback; } locate(time) { const frames = this.path.getFrames(); if (time.isLessThan(duration_1.Duration.zero())) { return this.update(0); } if (time.isGreaterThan(this.getDuration())) { return this.update(frames.length - 1); } const previousIndex = this.index - 1; if (previousIndex >= 0 && frames.at(previousIndex)?.tRange.includes(time)) { return this.update(previousIndex); } const currentIndex = this.index; if (frames.at(currentIndex)?.tRange.includes(time)) { return this.update(currentIndex); } const nextIndex = this.index + 1; if (frames.at(nextIndex)?.tRange.includes(time)) { return this.update(nextIndex); } const index = this.fallback.locate(time); if (typeof index === 'number') { return this.update(index); } this.update(0); return null; } update(index) { this.index = index; return index; } getDuration() { return this.path.getFrames().at(-1)?.tRange.end ?? duration_1.Duration.zero(); } } exports.FastCursorFrameLocator = FastCursorFrameLocator;