UNPKG

@stringsync/vexml

Version:

MusicXML to Vexflow

110 lines (109 loc) 3.87 kB
"use strict"; 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.Voice = void 0; const util = __importStar(require("../../util")); const contexts_1 = require("./contexts"); const rest_1 = require("./rest"); class Voice { config; log; id; events; constructor(config, log, id, events) { this.config = config; this.log = log; this.id = id; this.events = events; util.assert(events.every((event) => event.voiceId === id), 'Expected all events to belong to the current voice'); } static create(config, log, id, events) { return new Voice(config, log, id, events); } parse(staveCtx) { const voiceCtx = new contexts_1.VoiceContext(staveCtx, this.id); if (voiceCtx.getMultiRestCount() > 0) { const time = voiceCtx.getTime(); const rest = rest_1.Rest.whole(this.config, this.log, time); return { type: 'voice', entries: [rest.parse(voiceCtx)], beams: [], tuplets: [], }; } return { type: 'voice', entries: this.parseEntries(voiceCtx), beams: voiceCtx.getBeams(), tuplets: voiceCtx.getTuplets(), }; } parseEntries(voiceCtx) { const entries = new Array(); for (const event of this.events) { switch (event.type) { case 'note': entries.push(event.note.parse(voiceCtx)); break; case 'rest': entries.push(event.rest.parse(voiceCtx)); break; case 'chord': entries.push(event.chord.parse(voiceCtx)); break; case 'dynamics': entries.push(event.dynamics.parse()); break; case 'wedge': event.wedge.parse(voiceCtx); break; case 'pedal': event.pedal.parse(voiceCtx); break; case 'octaveshift': event.octaveShift.parse(voiceCtx); break; default: util.assertUnreachable(); } } return entries; } isTickable(event) { return event.type === 'note' || event.type === 'rest' || event.type === 'chord'; } } exports.Voice = Voice;