瀏覽代碼

improve Pitch.ToString(), shows key + accidental human-readable (e.g. F#)

sschmidTU 6 年之前
父節點
當前提交
76c9c66fc3

+ 47 - 2
src/Common/DataObjects/Pitch.ts

@@ -236,6 +236,47 @@ export class Pitch {
         }
     }
 
+    /**
+     * Converts AccidentalEnum to a string which represents an accidental in VexFlow
+     * Can also be useful in other cases, but has to match Vexflow accidental codes.
+     * @param accidental
+     * @returns {string} Vexflow Accidental code
+     */
+    public static accidentalVexflow(accidental: AccidentalEnum): string {
+        let acc: string;
+        switch (accidental) {
+            case AccidentalEnum.NATURAL:
+                acc = "n";
+                break;
+            case AccidentalEnum.FLAT:
+                acc = "b";
+                break;
+            case AccidentalEnum.SHARP:
+                acc = "#";
+                break;
+            case AccidentalEnum.DOUBLESHARP:
+                acc = "##";
+                break;
+            case AccidentalEnum.TRIPLESHARP:
+                acc = "++";
+                break;
+            case AccidentalEnum.DOUBLEFLAT:
+                acc = "bb";
+                break;
+            case AccidentalEnum.TRIPLEFLAT:
+                acc = "bbs"; // there is no "bbb" in VexFlow yet, unfortunately.
+                break;
+            case AccidentalEnum.QUARTERTONESHARP:
+                acc = "+";
+                break;
+            case AccidentalEnum.QUARTERTONEFLAT:
+                acc = "d";
+                break;
+            default:
+        }
+        return acc;
+    }
+
     public get AccidentalHalfTones(): number {
         return Pitch.HalfTonesFromAccidental(this.accidental);
     }
@@ -298,8 +339,12 @@ export class Pitch {
     }
 
     public ToString(): string {
-        return "Key: " + Pitch.getNoteEnumString(this.fundamentalNote) + ", Note: " + this.fundamentalNote +
-        ", octave: " + this.octave.toString() + ", alter: " + this.accidental;
+        let accidentalString: string = Pitch.accidentalVexflow(this.accidental);
+        if (!accidentalString) {
+            accidentalString = "";
+        }
+        return "Key: " + Pitch.getNoteEnumString(this.fundamentalNote) + accidentalString +
+        ", Note: " + this.fundamentalNote + ", octave: " + this.octave.toString();
     }
 
     public OperatorEquals(p2: Pitch): boolean {

+ 3 - 43
src/MusicalScore/Graphical/VexFlow/VexFlowConverter.ts

@@ -108,7 +108,7 @@ export class VexFlowConverter {
      */
     public static pitch(note: VexFlowGraphicalNote, pitch: Pitch): [string, string, ClefInstruction] {
         const fund: string = NoteEnum[pitch.FundamentalNote].toLowerCase();
-        const acc: string = VexFlowConverter.accidental(pitch.Accidental);
+        const acc: string = Pitch.accidentalVexflow(pitch.Accidental);
         // The octave seems to need a shift of three FIXME?
         const octave: number = pitch.Octave - note.Clef().OctaveOffset + 3;
         const notehead: Notehead = note.sourceNote.NoteHead;
@@ -145,46 +145,6 @@ export class VexFlowConverter {
         }
     }
 
-    /**
-     * Converts AccidentalEnum to a string which represents an accidental in VexFlow
-     * @param accidental
-     * @returns {string}
-     */
-    public static accidental(accidental: AccidentalEnum): string {
-        let acc: string;
-        switch (accidental) {
-            case AccidentalEnum.NATURAL:
-                acc = "n";
-                break;
-            case AccidentalEnum.FLAT:
-                acc = "b";
-                break;
-            case AccidentalEnum.SHARP:
-                acc = "#";
-                break;
-            case AccidentalEnum.DOUBLESHARP:
-                acc = "##";
-                break;
-            case AccidentalEnum.TRIPLESHARP:
-                acc = "++";
-                break;
-            case AccidentalEnum.DOUBLEFLAT:
-                acc = "bb";
-                break;
-            case AccidentalEnum.TRIPLEFLAT:
-                acc = "bbs"; // there is no "bbb" in VexFlow yet, unfortunately.
-                break;
-            case AccidentalEnum.QUARTERTONESHARP:
-                acc = "+";
-                break;
-            case AccidentalEnum.QUARTERTONEFLAT:
-                acc = "d";
-                break;
-            default:
-        }
-        return acc;
-    }
-
     public static GhostNote(frac: Fraction): Vex.Flow.GhostNote {
         return new Vex.Flow.GhostNote({
             duration: VexFlowConverter.duration(frac, false),
@@ -467,10 +427,10 @@ export class VexFlowConverter {
         }
         if (vfOrna !== undefined) {
             if (oContainer.AccidentalBelow !== AccidentalEnum.NONE) {
-                vfOrna.setLowerAccidental(this.accidental(oContainer.AccidentalBelow));
+                vfOrna.setLowerAccidental(Pitch.accidentalVexflow(oContainer.AccidentalBelow));
             }
             if (oContainer.AccidentalAbove !== AccidentalEnum.NONE) {
-                vfOrna.setUpperAccidental(this.accidental(oContainer.AccidentalAbove));
+                vfOrna.setUpperAccidental(Pitch.accidentalVexflow(oContainer.AccidentalAbove));
             }
             vfOrna.setPosition(vfPosition);
             vfnote.addModifier(0, vfOrna);

+ 1 - 1
src/MusicalScore/Graphical/VexFlow/VexFlowGraphicalNote.ts

@@ -40,7 +40,7 @@ export class VexFlowGraphicalNote extends GraphicalNote {
      */
     public setPitch(pitch: Pitch): void {
         if (this.vfnote) {
-            const acc: string = VexFlowConverter.accidental(pitch.Accidental);
+            const acc: string = Pitch.accidentalVexflow(pitch.Accidental);
             if (acc) {
                 alert(acc);
                 this.vfnote[0].addAccidental(this.vfnote[1], new Vex.Flow.Accidental(acc));