UNPKG

lrc-kit

Version:
90 lines (89 loc) 2.98 kB
import { Lrc } from './lrc'; var Runner = /** @class */ (function () { function Runner(lrc, offset) { if (lrc === void 0) { lrc = new Lrc(); } if (offset === void 0) { offset = true; } this.offset = offset; this._currentIndex = -1; this.setLrc(lrc); } Runner.prototype.setLrc = function (lrc) { this.lrc = lrc.clone(); this.lrcUpdate(); }; Runner.prototype.lrcUpdate = function () { if (this.offset) { this._offsetAlign(); } this._sort(); }; Runner.prototype._offsetAlign = function () { if ('offset' in this.lrc.info) { var offset = parseInt(this.lrc.info.offset) / 1000; if (!isNaN(offset)) { this.lrc.offset(offset); delete this.lrc.info.offset; } } }; Runner.prototype._sort = function () { this.lrc.lyrics.sort(function (a, b) { return a.timestamp - b.timestamp; }); }; Runner.prototype.timeUpdate = function (timestamp) { if (this._currentIndex >= this.lrc.lyrics.length) { this._currentIndex = this.lrc.lyrics.length - 1; } else if (this._currentIndex < -1) { this._currentIndex = -1; } this._currentIndex = this._findIndex(timestamp, this._currentIndex); }; Runner.prototype._findIndex = function (timestamp, startIndex) { var curFrontTimestamp = startIndex == -1 ? Number.NEGATIVE_INFINITY : this.lrc.lyrics[startIndex].timestamp; var curBackTimestamp = startIndex == this.lrc.lyrics.length - 1 ? Number.POSITIVE_INFINITY : this.lrc.lyrics[startIndex + 1].timestamp; if (timestamp < curFrontTimestamp) { return this._findIndex(timestamp, startIndex - 1); } else if (timestamp === curBackTimestamp) { if (curBackTimestamp === Number.POSITIVE_INFINITY) { return startIndex; } else { return startIndex + 1; } } else if (timestamp > curBackTimestamp) { return this._findIndex(timestamp, startIndex + 1); } else { return startIndex; } }; Runner.prototype.getInfo = function () { return this.lrc.info; }; Runner.prototype.getLyrics = function () { return this.lrc.lyrics; }; Runner.prototype.getLyric = function (index) { if (index === void 0) { index = this.curIndex(); } if (index >= 0 && index <= this.lrc.lyrics.length - 1) { return this.lrc.lyrics[index]; } else { throw new Error('Index not exist'); } }; Runner.prototype.curIndex = function () { return this._currentIndex; }; Runner.prototype.curLyric = function () { return this.getLyric(); }; return Runner; }()); export { Runner };