@hypst/time-beat-format
Version:
Formatting time, notes and beats.
115 lines (114 loc) • 4.32 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const HMSTime_1 = require("./HMSTime");
const definitions_1 = require("./definitions");
const utils_1 = require("./utils");
class BeatTime {
constructor(option, ...args) {
this._note = 0;
this._beat = 0;
this._division = 0;
this._option = option;
if (typeof (args[0]) == 'string') {
this.parseString(args[0]);
}
else if (typeof (args[0]) == 'number' && typeof (args[1]) == 'number' && typeof (args[2]) == 'number') {
this.parseNumber(...args);
}
else if (args[0] instanceof HMSTime_1.HMSTime) {
this.parseHMSTime(args[0]);
}
else if (args[0] instanceof BeatTime) {
this.parseHMSTime(args[0].toHMSTime());
}
}
get note() {
let noteDelta = this._option.isRelativeTime ? 0 : 1;
return this._note + noteDelta;
}
get beat() {
let noteDelta = this._option.isRelativeTime ? 0 : 1;
return this._beat + noteDelta;
}
get division() {
return this._division;
}
set note(v) {
this.parseNumber(v);
}
set beat(v) {
this.parseNumber(undefined, v);
}
set division(v) {
this.parseNumber(undefined, undefined, v);
}
parseString(s) {
let noteStr = "";
let beatStr = "";
let divisionStr = "";
s.replace(BeatTime.BEAT_REGEXP, (match, d1, p1, d2, p2, d3) => {
if (d2) {
beatStr = d2;
noteStr = d1;
}
else {
beatStr = d1;
}
if (d3) {
divisionStr = d3;
}
this.parseNumber(Number(noteStr), Number(beatStr), Number(divisionStr));
return match;
});
}
parseNumber(note, beat, division) {
let noteDelta = this._option.isRelativeTime ? 0 : 1;
if (note)
note -= noteDelta;
if (beat)
beat -= noteDelta;
if ((note && note < 0) || (beat && beat < 0) || (division && division < 0)) {
throw new definitions_1.NegativeValueError();
}
if (note !== undefined) {
this._note = note;
}
if (beat !== undefined) {
this._beat = beat;
}
if (division !== undefined) {
this._division = division;
}
this._division = Math.round(this._division);
this._beat += Math.floor(this.division / this._option.divisionsPerBeat);
this._division %= this._option.divisionsPerBeat;
this._note += Math.floor(this.beat / this._option.beatsPerNote);
this._beat %= this._option.beatsPerNote;
}
parseHMSTime(timeObj) {
let noteDelta = this._option.isRelativeTime ? 0 : 1;
let msPerBeat = 60 * 1000 / this._option.bpm;
let msPerDivision = msPerBeat / this._option.divisionsPerBeat;
this.parseNumber(noteDelta, noteDelta, timeObj.toMillisecond() / msPerDivision);
}
toString() {
var _a, _b, _c;
let note = utils_1.fillNumberWidth(this.note, (_a = this._option.numberWidthOption) === null || _a === void 0 ? void 0 : _a.note);
let beat = utils_1.fillNumberWidth(this.beat, (_b = this._option.numberWidthOption) === null || _b === void 0 ? void 0 : _b.beat);
let division = utils_1.fillNumberWidth(this.division, (_c = this._option.numberWidthOption) === null || _c === void 0 ? void 0 : _c.division);
if (note)
note += ':';
if (division)
division = '.' + division;
return note + beat + division;
}
toHMSTime() {
let msPerNote = this._option.beatsPerNote * 60 * 1000 / this._option.bpm;
let msPerBeat = 60 * 1000 / this._option.bpm;
let msPerDivision = msPerBeat / this._option.divisionsPerBeat;
let millisecond = this._note * msPerNote + this._beat * msPerBeat + this._division * msPerDivision;
return new HMSTime_1.HMSTime(millisecond);
}
}
exports.BeatTime = BeatTime;
BeatTime.BEAT_REGEXP = /^([0-9]*)(:|:)?([0-9]*)(\.|。|.)?([0-9]*)$/;