@stringsync/vexml
Version:
MusicXML to Vexflow
83 lines (82 loc) • 3.4 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimestampLocator = void 0;
const util = __importStar(require("../util"));
const duration_1 = require("./duration");
class TimestampLocator {
systems;
constructor(systems) {
this.systems = systems;
}
static create(score, paths) {
const systems = score.getSystems().map((system) => {
const yRange = new util.NumberRange(system.rect().top(), system.rect().bottom());
const frames = new Array();
for (const path of paths) {
frames.push(...path
.getFrames()
.filter((frame) => frame.getActiveElements().some((element) => element.getSystemIndex() === system.getIndex())));
}
return { yRange, frames };
});
return new TimestampLocator(systems);
}
/**
* Locates the timestamp at the given point in the score. If a given point is passed multiple times within a playback
* (e.g. repeats), only the first occurrence will be returned.
*/
locate(point) {
// Let S be the number of systems and E be the number of entries in a system. The time complexity is O(S*E), but
// this is ok because we expect S and E to be small and have an upper bound of O(100).
for (const system of this.systems) {
if (!system.yRange.includes(point.y)) {
continue;
}
for (const frame of system.frames) {
if (!frame.xRange.includes(point.x)) {
continue;
}
const startMs = frame.tRange.start.ms;
const stopMs = frame.tRange.end.ms;
const alpha = (point.x - frame.xRange.start) / frame.xRange.getSize();
const timestampMs = util.lerp(startMs, stopMs, alpha);
return duration_1.Duration.ms(timestampMs);
}
}
return null;
}
}
exports.TimestampLocator = TimestampLocator;