@stringsync/vexml
Version:
MusicXML to Vexflow
120 lines (119 loc) • 4.85 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.Rest = void 0;
const util = __importStar(require("../../util"));
const conversions = __importStar(require("./conversions"));
const fraction_1 = require("./fraction");
const pitch_1 = require("./pitch");
const contexts_1 = require("./contexts");
const beam_1 = require("./beam");
const tuplet_1 = require("./tuplet");
class Rest {
config;
log;
measureBeat;
durationType;
dotCount;
duration;
displayPitch;
beam;
tuplets;
constructor(config, log, measureBeat, durationType, dotCount, duration, displayPitch, beam, tuplets) {
this.config = config;
this.log = log;
this.measureBeat = measureBeat;
this.durationType = durationType;
this.dotCount = dotCount;
this.duration = duration;
this.displayPitch = displayPitch;
this.beam = beam;
this.tuplets = tuplets;
}
static create(config, log, measureBeat, duration, musicXML) {
util.assert(musicXML.note.isRest(), 'Expected note to be a rest');
const displayStep = musicXML.note.getRestDisplayStep();
const displayOctave = musicXML.note.getRestDisplayOctave();
let displayPitch = null;
if (displayStep && typeof displayOctave === 'number') {
displayPitch = new pitch_1.Pitch(config, log, displayStep, displayOctave);
}
let durationType = conversions.fromNoteTypeToDurationType(musicXML.note.getType());
let dotCount = musicXML.note.getDotCount();
if (!durationType) {
[durationType, dotCount] = conversions.fromFractionToDurationType(duration);
}
let beam = null;
if (musicXML.note.getBeams().length > 0) {
beam = beam_1.Beam.create(config, log, { beam: musicXML.note.getBeams().at(0) });
}
const tuplets = musicXML.note
.getNotations()
.flatMap((n) => n.getTuplets())
.map((tuplet) => tuplet_1.Tuplet.create(config, log, { tuplet }));
return new Rest(config, log, measureBeat, durationType, dotCount, duration, displayPitch, beam, tuplets);
}
static whole(config, log, time) {
const measureBeat = util.Fraction.zero();
const duration = time.toFraction().multiply(new util.Fraction(4, 1));
const [durationType, dotCount] = conversions.fromFractionToDurationType(duration);
return new Rest(config, log, measureBeat, durationType, dotCount, duration, null, null, []);
}
parse(voiceCtx) {
const voiceEntryCtx = contexts_1.VoiceEntryContext.rest(voiceCtx);
const tupletIds = util.unique([
...this.tuplets.map((tuplet) => tuplet.parse(voiceEntryCtx)).filter((id) => id !== null),
...voiceEntryCtx.continueOpenTuplets(),
]);
return {
type: 'rest',
durationType: this.durationType,
dotCount: this.dotCount,
measureBeat: this.getMeasureBeat().parse(),
duration: this.getDuration().parse(),
displayPitch: this.displayPitch?.parse() ?? null,
beamId: this.beam?.parse(voiceEntryCtx) ?? null,
pedalMark: voiceEntryCtx.continueOpenPedal(),
tupletIds,
};
}
getMeasureBeat() {
return new fraction_1.Fraction(this.measureBeat);
}
getDuration() {
return new fraction_1.Fraction(this.duration);
}
}
exports.Rest = Rest;