Procházet zdrojové kódy

Moved utils in VexFlowConverter

Andrea Condoluci před 9 roky
rodič
revize
b34a93b850

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

@@ -36,6 +36,7 @@ declare namespace Vex {
       public getNoteHeadBounds(): any;
       public getNoteHeadBeginX(): number;
       public getNoteHeadEndX(): number;
+      public addAccidental(index: number, accidental: Accidental): StaveNote;
     }
 
     export class Stave {
@@ -95,6 +96,10 @@ declare namespace Vex {
       constructor(keySpec: string, cancelKeySpec: string, alterKeySpec?: string);
     }
 
+    export class Accidental {
+      constructor(type: string);
+    }
+
   }
 }
 

+ 2 - 2
src/MusicalScore/Graphical/StaffMeasure.ts

@@ -144,7 +144,7 @@ export class StaffMeasure extends GraphicalObject {
         let duration: Fraction = new Fraction(0, 1);
         for (let idx: number = 0, len: number = this.staffEntries.length; idx < len; ++idx) {
             let graphicalStaffEntry: GraphicalStaffEntry = this.staffEntries[idx];
-            duration.push(graphicalStaffEntry.findStaffEntryMinNoteLength());
+            duration.Add(graphicalStaffEntry.findStaffEntryMinNoteLength());
         }
         return duration === this.parentSourceMeasure.Duration;
     }
@@ -193,7 +193,7 @@ export class StaffMeasure extends GraphicalObject {
                 for (let idx3: number = 0, len3: number = graphicalStaffEntry.notes.length; idx3 < len3; ++idx3) {
                     let graphicalNotes: GraphicalNote[] = graphicalStaffEntry.notes[idx3];
                     if (graphicalNotes.length > 0 && graphicalNotes[0].sourceNote.ParentVoiceEntry.ParentVoice === voice) {
-                        voiceDuration.push(graphicalNotes[0].graphicalNoteLength);
+                        voiceDuration.Add(graphicalNotes[0].graphicalNoteLength);
                     }
                 }
             }

+ 126 - 0
src/MusicalScore/Graphical/VexFlow/VexFlowConverter.ts

@@ -0,0 +1,126 @@
+import {ClefEnum} from "../../VoiceData/Instructions/ClefInstruction";
+import {ClefInstruction} from "../../VoiceData/Instructions/ClefInstruction";
+import {VexFlowConverter} from "./VexFlowConverter";
+import {VoiceEntry} from "../../VoiceData/VoiceEntry";
+import {Pitch} from "../../../Common/DataObjects/pitch";
+import {Fraction} from "../../../Common/DataObjects/fraction";
+import {RhythmInstruction} from "../../VoiceData/Instructions/RhythmInstruction";
+import {RhythmSymbolEnum} from "../../VoiceData/Instructions/RhythmInstruction";
+import {KeyInstruction} from "../../VoiceData/Instructions/KeyInstruction";
+import {KeyEnum} from "../../VoiceData/Instructions/KeyInstruction";
+import {AccidentalEnum} from "../../../Common/DataObjects/pitch";
+import {NoteEnum} from "../../../Common/DataObjects/pitch";
+
+export class VexFlowConverter {
+    private static majorMap: {[_: number]: string; } = {
+        "0": "C", 1: "G", 2: "D", 3: "A", 4: "E", 5: "B", 6: "F#", 7: "C#",
+        8: "G#", "-1": "F", "-8": "Fb", "-7": "Cb", "-6": "Gb", "-5": "Db", "-4": "Ab", "-3": "Eb", "-2": "Bb",
+    };
+    private static minorMap: {[_: number]: string; } = {
+        "1": "E", "7": "A#", "0": "A", "6": "D#", "3": "F#", "-5": "Bb", "-4": "F", "-7": "Ab", "-6": "Eb",
+        "-1": "D", "4": "C#", "-3": "C", "-2": "G", "2": "B", "5": "G#", "-8": "Db", "8": "E#",
+    };
+
+    public static duration(fraction: Fraction): string {
+        return undefined;
+    }
+
+    public static pitch(pitch: Pitch): [string, string] {
+        let fund: string = NoteEnum[pitch.FundamentalNote];
+        let octave: number = pitch.Octave;
+        let acc: string = "";
+
+        switch (pitch.Accidental) {
+            case AccidentalEnum.NONE:
+                break;
+            case AccidentalEnum.FLAT:
+                acc = "b";
+                break;
+            case AccidentalEnum.SHARP:
+                acc = "#";
+                break;
+            case AccidentalEnum.DOUBLESHARP:
+                acc = "##";
+                break;
+            case AccidentalEnum.DOUBLEFLAT:
+                acc = "bb";
+                break;
+            default:
+        }
+        return [fund + acc + "/" + octave, acc];
+    }
+
+    public static StaveNote(voiceEntry: VoiceEntry): Vex.Flow.StaveNote {
+        let keys: string[] = []; //["b/4"]
+        let duration: string = VexFlowConverter.duration(voiceEntry.Notes[0].Length);
+        let accidentals: string[] = [];
+        for (let note of voiceEntry.Notes) {
+            let res: [string, string] = VexFlowConverter.pitch(note.Pitch);
+            keys.push(res[0]);
+            accidentals.push(res[1]);
+        }
+        let vfnote: Vex.Flow.StaveNote = new Vex.Flow.StaveNote({
+            duration: duration,
+            keys: keys,
+        });
+        for (let i: number = 0, len: number = keys.length; i < len; i += 1) {
+            let acc: string = accidentals[i];
+            if (acc) {
+                vfnote.addAccidental(i, new Vex.Flow.Accidental(acc));
+            }
+        }
+        return vfnote;
+    }
+
+    public static Clef(clef: ClefInstruction): Vex.Flow.Clef {
+        let type: string;
+        switch (clef.ClefType) {
+            case ClefEnum.G:
+                type = "treble";
+                break;
+            case ClefEnum.F:
+                type = "bass";
+                break;
+            case ClefEnum.C:
+                type = "baritone-c";
+                break;
+            case ClefEnum.percussion:
+                type = "percussion";
+                break;
+            case ClefEnum.TAB:
+                type = "tab";
+                break;
+            default:
+        }
+        return new Vex.Flow.Clef(type);
+    }
+
+    public static TimeSignature(rhythm: RhythmInstruction): Vex.Flow.TimeSignature {
+        let timeSpec: string;
+        switch (rhythm.SymbolEnum) {
+            case RhythmSymbolEnum.NONE:
+                timeSpec = rhythm.Rhythm.Numerator + "/" + rhythm.Rhythm.Denominator;
+                break;
+            case RhythmSymbolEnum.COMMON:
+                timeSpec = "C";
+                break;
+            case RhythmSymbolEnum.CUT:
+                timeSpec = "C|";
+                break;
+            default:
+        }
+        return new Vex.Flow.TimeSignature(timeSpec);
+    }
+
+    public static keySignature(key: KeyInstruction): string {
+        switch (key.Mode) {
+            case KeyEnum.none:
+                return undefined;
+            case KeyEnum.minor:
+                return VexFlowConverter.minorMap[key.Key];
+            case KeyEnum.major:
+                return VexFlowConverter.majorMap[key.Key] + "m";
+            default:
+        }
+    }
+}

+ 12 - 78
src/MusicalScore/Graphical/VexFlow/VexFlowMeasure.ts

@@ -6,10 +6,7 @@ import {SystemLinesEnum} from "../SystemLinesEnum";
 import {ClefInstruction} from "../../VoiceData/Instructions/ClefInstruction";
 import {KeyInstruction} from "../../VoiceData/Instructions/KeyInstruction";
 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";
+import {VexFlowConverter} from "./VexFlowConverter";
 
 export class VexFlowMeasure extends StaffMeasure {
     constructor(staff: Staff, staffLine: StaffLine = undefined, sourceMeasure: SourceMeasure = undefined) {
@@ -17,72 +14,7 @@ export class VexFlowMeasure extends StaffMeasure {
         // this.MinimumStaffEntriesWidth =
     }
 
-    private static majorMap: {[_: number]: string; } = {
-        "0": "C", 1: "G", 2: "D", 3: "A", 4: "E", 5: "B", 6: "F#", 7: "C#",
-        8: "G#", "-1": "F", "-8": "Fb", "-7": "Cb", "-6": "Gb", "-5": "Db", "-4": "Ab", "-3": "Eb", "-2": "Bb",
-    };
-    private static minorMap: {[_: number]: string; } = {
-        "1": "E", "7": "A#", "0": "A", "6": "D#", "3": "F#", "-5": "Bb", "-4": "F", "-7": "Ab", "-6": "Eb",
-        "-1": "D", "4": "C#", "-3": "C", "-2": "G", "2": "B", "5": "G#", "-8": "Db", "8": "E#",
-    };
-
-    private stave: Vex.Flow.Stave;
-
-    private static toVFStaveNote(voiceEntry: VoiceEntry): Vex.Flow.StaveNote {
-        return undefined;
-    }
-
-    private static toVFClef(clef: ClefInstruction) {
-        let type: string;
-        switch (clef.ClefType) {
-            case ClefEnum.G:
-                type = "treble";
-                break;
-            case ClefEnum.F:
-                type = "bass";
-                break;
-            case ClefEnum.C:
-                type = "baritone-c";
-                break;
-            case ClefEnum.percussion:
-                type = "percussion";
-                break;
-            case ClefEnum.TAB:
-                type = "tab";
-                break;
-            default:
-        }
-        return new Vex.Flow.Clef(type);
-    }
-
-    private static toVFTimeSignature(rhythm: RhythmInstruction): Vex.Flow.TimeSignature {
-        let timeSpec: string;
-        switch (rhythm.SymbolEnum) {
-            case RhythmSymbolEnum.NONE:
-                timeSpec = rhythm.Rhythm.Numerator + "/" + rhythm.Rhythm.Denominator;
-                break;
-            case RhythmSymbolEnum.COMMON:
-                timeSpec = "C";
-                break;
-            case RhythmSymbolEnum.CUT:
-                timeSpec = "C|";
-                break;
-            default:
-        }
-        return new Vex.Flow.TimeSignature(timeSpec);
-    }
-
-    private static toKeySignatureString(key: KeyInstruction): string {
-        switch (key.Mode) {
-            case KeyEnum.none:
-                return undefined;
-            case KeyEnum.minor:
-                return VexFlowMeasure.minorMap[key.Key];
-            case KeyEnum.major:
-                return VexFlowMeasure.majorMap[key.Key] + "m";
-            default:
-        }
-    }
+    public stave: Vex.Flow.Stave;
 
     /**
      * Reset all the geometric values and parameters of this measure and put it in an initialized state.
@@ -126,7 +58,7 @@ export class VexFlowMeasure extends StaffMeasure {
      * @param clef
      */
     public addClefAtBegin(clef: ClefInstruction): void {
-        let vfclef: Vex.Flow.Clef = VexFlowMeasure.toVFClef(clef);
+        let vfclef: Vex.Flow.Clef = VexFlowConverter.Clef(clef);
         this.stave.addClef(vfclef, undefined, undefined, Vex.Flow.StaveModifier.Position.BEGIN);
         this.increaseBeginInstructionWidth(vfclef);
     }
@@ -140,8 +72,8 @@ export class VexFlowMeasure extends StaffMeasure {
      */
     public addKeyAtBegin(currentKey: KeyInstruction, previousKey: KeyInstruction, currentClef: ClefInstruction): void {
         let keySig: Vex.Flow.KeySignature = new Vex.Flow.KeySignature(
-            VexFlowMeasure.toKeySignatureString(currentKey),
-            VexFlowMeasure.toKeySignatureString(previousKey)
+            VexFlowConverter.keySignature(currentKey),
+            VexFlowConverter.keySignature(previousKey)
         );
         this.stave.addModifier(keySig, Vex.Flow.StaveModifier.Position.BEGIN);
     }
@@ -152,7 +84,7 @@ export class VexFlowMeasure extends StaffMeasure {
      * @param rhythm
      */
     public addRhythmAtBegin(rhythm: RhythmInstruction): void {
-        let timeSig: Vex.Flow.TimeSignature = VexFlowMeasure.toVFTimeSignature(rhythm);
+        let timeSig: Vex.Flow.TimeSignature = VexFlowConverter.TimeSignature(rhythm);
         this.stave.addModifier(
             timeSig,
             Vex.Flow.StaveModifier.Position.BEGIN
@@ -166,7 +98,7 @@ export class VexFlowMeasure extends StaffMeasure {
      * @param clef
      */
     public addClefAtEnd(clef: ClefInstruction): void {
-        let vfclef: Vex.Flow.Clef = VexFlowMeasure.toVFClef(clef);
+        let vfclef: Vex.Flow.Clef = VexFlowConverter.Clef(clef);
         this.stave.addClef(vfclef, undefined, undefined, Vex.Flow.StaveModifier.Position.END);
         this.increaseBeginInstructionWidth(vfclef);
     }
@@ -208,15 +140,17 @@ export class VexFlowMeasure extends StaffMeasure {
                 if (!(id in notes)) {
                     notes[id] = [];
                 }
-                notes[id].push(VexFlowMeasure.toVFStaveNote(voiceEntry));
+                notes[id].push(VexFlowConverter.StaveNote(voiceEntry));
             }
         }
         let voices: { [id: number]: Vex.Flow.Voice; } = {};
+        let num: number = this.parentSourceMeasure.Duration.Numerator;
+        let den: number = this.parentSourceMeasure.Duration.Denominator;
         for (let id in notes) {
             if (notes.hasOwnProperty(id)) {
                 let voice: Vex.Flow.Voice = new Vex.Flow.Voice({
-                    beat_value: 4,
-                    num_beats: 4,
+                    beat_value: den,
+                    num_beats: num,
                     resolution: Vex.Flow.RESOLUTION,
                 });
                 voice.addTickables(notes[id]);