如何在VexFlow中渲染配乐?

时间:2019-06-15 21:45:23

标签: vexflow

使用Tone.js,我可以播放此旋律:

const textMeasures = ['rest/4 B4/16 A4/16 G#4/16 A4/16',
'C5/8 rest/8 D5/16 C5/16 B4/16 C5/16',
'E5/8 rest/8 F5/16 E5/16 D#5/16 E5/16',
'B5/16 A5/16 G#5/16 A5/16 B5/16 A5/16 G#5/16 A5/16',
'C6/4 A5/8 C6/8',
'B5/8 A5/8 G5/8 A5/8',
'B5/8 A5/8 G5/8 A5/8',
'B5/8 A5/8 G5/8 F#5/8',
'E5/4'];

现在,我想使用VexFlow在一些五线谱上呈现这些音符。

请记住,在移动设备上的显示应该很好,并且配乐上可能有多个声音。

现在,我创建了一种按小节有五线谱的方法:

private renderSoundtrackVexflow(name: string, soundtrack: Soundtrack) {
  const context = this.renderVexflowContext(name, VEXFLOW_WIDTH, VEXFLOW_HEIGHT);
  context.setFont('Arial', 10, '').setBackgroundFillStyle('#eed'); // TODO Hard coded values

  if (soundtrack.hasTracks()) {
    let staveIndex: number = 0;
    const voices: Array<vexflow.Flow.Voice> = new Array<vexflow.Flow.Voice>();
    for (const track of soundtrack.tracks) {
      if (track.hasMeasures()) {
        for (const measure of track.measures) {
          if (measure.hasNotes()) {
            const stave = new this.VF.Stave(0, staveIndex * (VEXFLOW_STAVE_HEIGHT + VEXFLOW_STAVE_MARGIN), VEXFLOW_WIDTH);
            staveIndex++;
            stave.setContext(context);
            stave.addClef(VEXFLOW_CLEF);
            stave.addTimeSignature(this.renderTimeSignature(measure));
            stave.draw();

            const notes = new Array<vexflow.Flow.StaveNote>();
            const voice = new this.VF.Voice();
            voice.setStrict(false);
            voice.setContext(context);
            voice.setStave(stave);
            for (const placedNote of measure.placedNotes) {
              const note: Note = placedNote.note;
              notes.push(new this.VF.StaveNote({ keys: [ this.renderAbc(note) ], duration: this.renderDuration(note) }));
            }
            voice.addTickables(notes);
            voices.push(voice);
          }
        }
        const formatter = new this.VF.Formatter();
        formatter.joinVoices(voices).format(voices, VEXFLOW_WIDTH);
        for (const voice of voices) {
          voice.draw();
          console.log('Min voice width: ' + formatter.getMinTotalWidth(voice));
        }
      }
    }
  }
}

但是音符会在五线谱上显示一点,但是某些音符会在页面的五线谱之间显示。

Install Howto

1 个答案:

答案 0 :(得分:0)

问题有点老了,但是如果您仍然需要解决方案...

问题在于所有音符的笔杆都朝上。要解决此问题,请将auto_stem: trueclef: "treble"添加到传递给VF.StaveNote()的选项列表中,有关更多信息,请参见VexFlow: Stem Direction。这是创建注释的更正行:

notes.push(new this.VF.StaveNote({
      keys: [ this.renderAbc(note) ],
      duration: this.renderDuration(note),
      auto_stem: true,
      clef: "treble"
}));