UNPKG

@stringsync/vexml

Version:

MusicXML to Vexflow

46 lines (45 loc) 1.53 kB
import * as vexflow from 'vexflow'; import { Rect } from '../spatial'; const KEY_SIGNATURE_PADDING = 15; /** Represents a musical key signature, not document key. */ export class Key { config; log; document; key; constructor(config, log, document, key) { this.config = config; this.log = log; this.document = document; this.key = key; } render() { const vexflowKeySignature = this.getVexflowKeySignature(); return { type: 'key', key: this.key, rect: Rect.empty(), // placeholder width: this.getWidth(), vexflowKeySignature, }; } getAlterations() { const keySignature = this.document.getStave(this.key).signature.key; const alterations = new Array(); if (Math.abs(keySignature.fifths) > 7) { const additional = Math.abs(keySignature.fifths) - 7; for (let index = 0; index < additional; index++) { alterations.push(keySignature.fifths > 0 ? '##' : 'bb'); } } return alterations; } getWidth() { const vexflowKeySignature = this.getVexflowKeySignature(); return vexflowKeySignature.getWidth() + KEY_SIGNATURE_PADDING; } getVexflowKeySignature() { const keySignature = this.document.getStave(this.key).signature.key; return new vexflow.KeySignature(keySignature.rootNote, keySignature.previousKey?.rootNote, this.getAlterations()); } }