浏览代码

Vex.Flow.Voices

Andrea Condoluci 9 年之前
父节点
当前提交
08b9ab2ae2
共有 2 个文件被更改,包括 37 次插入5 次删除
  1. 2 0
      external/vexflow/vexflow.d.ts
  2. 35 5
      src/MusicalScore/Graphical/VexFlow/VexFlowMeasure.ts

+ 2 - 0
external/vexflow/vexflow.d.ts

@@ -1,5 +1,6 @@
 declare namespace Vex {
   export module Flow {
+    const RESOLUTION: any;
 
     export class Formatter {
       constructor();
@@ -26,6 +27,7 @@ declare namespace Vex {
       public getBoundingBox(): BoundingBox;
       public setStave(stave: Stave): Voice;
       public addTickables(notes: StaveNote[]): Voice;
+      public addTickable(note: StaveNote): Voice;
     }
 
     export class StaveNote {

+ 35 - 5
src/MusicalScore/Graphical/VexFlow/VexFlowMeasure.ts

@@ -9,6 +9,7 @@ import {RhythmInstruction} from "../../VoiceData/Instructions/RhythmInstruction"
 import {ClefEnum} from "../../VoiceData/Instructions/ClefInstruction";
 import {RhythmSymbolEnum} from "../../VoiceData/Instructions/RhythmInstruction";
 import {KeyEnum} from "../../VoiceData/Instructions/KeyInstruction";
+import {VoiceEntry} from "../../VoiceData/VoiceEntry";
 
 export class VexFlowMeasure extends StaffMeasure {
     constructor(staff: Staff, staffLine: StaffLine = undefined, sourceMeasure: SourceMeasure = undefined) {
@@ -27,8 +28,11 @@ export class VexFlowMeasure extends StaffMeasure {
 
     private stave: Vex.Flow.Stave;
 
+    private static toVFStaveNote(voiceEntry: VoiceEntry): Vex.Flow.StaveNote {
+        return undefined;
+    }
 
-    private static toVexFlowClef(clef: ClefInstruction) {
+    private static toVFClef(clef: ClefInstruction) {
         let type: string;
         switch (clef.ClefType) {
             case ClefEnum.G:
@@ -51,7 +55,7 @@ export class VexFlowMeasure extends StaffMeasure {
         return new Vex.Flow.Clef(type);
     }
 
-    private static toVexFlowTimeSignature(rhythm: RhythmInstruction): Vex.Flow.TimeSignature {
+    private static toVFTimeSignature(rhythm: RhythmInstruction): Vex.Flow.TimeSignature {
         let timeSpec: string;
         switch (rhythm.SymbolEnum) {
             case RhythmSymbolEnum.NONE:
@@ -122,7 +126,7 @@ export class VexFlowMeasure extends StaffMeasure {
      * @param clef
      */
     public addClefAtBegin(clef: ClefInstruction): void {
-        let vfclef: Vex.Flow.Clef = VexFlowMeasure.toVexFlowClef(clef);
+        let vfclef: Vex.Flow.Clef = VexFlowMeasure.toVFClef(clef);
         this.stave.addClef(vfclef, undefined, undefined, Vex.Flow.StaveModifier.Position.BEGIN);
         this.increaseBeginInstructionWidth(vfclef);
     }
@@ -148,7 +152,7 @@ export class VexFlowMeasure extends StaffMeasure {
      * @param rhythm
      */
     public addRhythmAtBegin(rhythm: RhythmInstruction): void {
-        let timeSig: Vex.Flow.TimeSignature = VexFlowMeasure.toVexFlowTimeSignature(rhythm);
+        let timeSig: Vex.Flow.TimeSignature = VexFlowMeasure.toVFTimeSignature(rhythm);
         this.stave.addModifier(
             timeSig,
             Vex.Flow.StaveModifier.Position.BEGIN
@@ -162,7 +166,7 @@ export class VexFlowMeasure extends StaffMeasure {
      * @param clef
      */
     public addClefAtEnd(clef: ClefInstruction): void {
-        let vfclef: Vex.Flow.Clef = VexFlowMeasure.toVexFlowClef(clef);
+        let vfclef: Vex.Flow.Clef = VexFlowMeasure.toVFClef(clef);
         this.stave.addClef(vfclef, undefined, undefined, Vex.Flow.StaveModifier.Position.END);
         this.increaseBeginInstructionWidth(vfclef);
     }
@@ -196,6 +200,32 @@ export class VexFlowMeasure extends StaffMeasure {
         this.stave.draw();
     }
 
+    public getVexFlowVoices(): { [id: number]: Vex.Flow.Voice; } {
+        let notes: { [id: number]: Vex.Flow.StaveNote[]; } = {};
+        for (let entry of this.staffEntries) {
+            for (let voiceEntry of entry.sourceStaffEntry.VoiceEntries) {
+                let id: number = voiceEntry.ParentVoice.VoiceId;
+                if (!(id in notes)) {
+                    notes[id] = [];
+                }
+                notes[id].push(VexFlowMeasure.toVFStaveNote(voiceEntry));
+            }
+        }
+        let voices: { [id: number]: Vex.Flow.Voice; } = {};
+        for (let id in notes) {
+            if (notes.hasOwnProperty(id)) {
+                let voice: Vex.Flow.Voice = new Vex.Flow.Voice({
+                    beat_value: 4,
+                    num_beats: 4,
+                    resolution: Vex.Flow.RESOLUTION,
+                });
+                voice.addTickables(notes[id]);
+                voices[id] = voice;
+            }
+        }
+        return voices;
+    }
+
     private increaseBeginInstructionWidth(modifier: any): void {
         // FIXME: Check possible paddings...
         //this.beginInstructionsWidth += modifier.getWidth();