소스 검색

Fixing the accidentals...

Andrea Condoluci 9 년 전
부모
커밋
36ec04e744

+ 10 - 11
src/MusicalScore/Graphical/VexFlow/VexFlowConverter.ts

@@ -49,10 +49,10 @@ export class VexFlowConverter {
      */
     public static pitch(pitch: Pitch, clef: ClefInstruction): [string, string, ClefInstruction] {
         let fund: string = NoteEnum[pitch.FundamentalNote].toLowerCase();
-        let octave: number = pitch.Octave + clef.OctaveOffset + 3; // FIXME + 3
-        let acc: string = "";
-
-        return [fund + acc + "/" + octave, acc, clef];
+        // The octave seems to need a shift of three FIXME?
+        let octave: number = pitch.Octave + clef.OctaveOffset + 3;
+        let acc: string = VexFlowConverter.accidental(pitch.Accidental);
+        return [fund + "b/" + octave, acc, clef];
     }
 
     /**
@@ -61,7 +61,7 @@ export class VexFlowConverter {
      * @returns {string}
      */
     public static accidental(accidental: AccidentalEnum): string {
-        let acc: string = "";
+        let acc: string;
         switch (accidental) {
             case AccidentalEnum.NONE:
                 break;
@@ -85,15 +85,14 @@ export class VexFlowConverter {
 
     public static StaveNote(notes: GraphicalNote[]): Vex.Flow.StaveNote {
         let keys: string[] = [];
+        let accidentals: string[] = [];
         let frac: Fraction = notes[0].sourceNote.Length;
         let duration: string = VexFlowConverter.duration(frac);
-        let accidentals: string[] = [];
         let vfclef: string;
         for (let note of notes) {
             let res: [string, string, ClefInstruction] = (note as VexFlowGraphicalNote).vfpitch;
             if (res === undefined) {
                 keys = ["b/4"];
-                accidentals = [];
                 duration += "r";
                 break;
             }
@@ -113,10 +112,10 @@ export class VexFlowConverter {
             },
             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));
+        for (let i: number = 0, len: number = notes.length; i < len; i += 1) {
+            (notes[i] as VexFlowGraphicalNote).setIndex(vfnote, i);
+            if (accidentals[i]) {
+                vfnote.addAccidental(i, new Vex.Flow.Accidental(accidentals[i]));
             }
         }
         return vfnote;

+ 31 - 4
src/MusicalScore/Graphical/VexFlow/VexFlowGraphicalNote.ts

@@ -1,19 +1,46 @@
+import Vex = require("vexflow");
 import {GraphicalNote} from "../GraphicalNote";
 import {Note} from "../../VoiceData/Note";
 import {GraphicalStaffEntry} from "../GraphicalStaffEntry";
 import {ClefInstruction} from "../../VoiceData/Instructions/ClefInstruction";
 import {VexFlowConverter} from "./VexFlowConverter";
+import {Pitch} from "../../../Common/DataObjects/pitch";
 
 export class VexFlowGraphicalNote extends GraphicalNote {
     constructor(note: Note, parent: GraphicalStaffEntry, activeClef: ClefInstruction) {
         super(note, parent);
+        this.clef = activeClef;
         if (note.Pitch) {
-            // ToDo: don't use accidental info here - set it in factory.
-            this.vfpitch = VexFlowConverter.pitch(note.Pitch, activeClef);
-        } else {
-            this.vfpitch = undefined;
+            this.vfpitch = VexFlowConverter.pitch(note.Pitch, this.clef);
+            this.vfpitch[1] = undefined;
         }
     }
 
     public vfpitch: [string, string, ClefInstruction];
+    private vfnote: [Vex.Flow.StaveNote, number];
+    private clef: ClefInstruction;
+
+    public setPitch(pitch: Pitch): void {
+        if (this.vfnote) {
+            let acc: string = VexFlowConverter.accidental(pitch.Accidental);
+            if (acc) {
+                alert(acc);
+                this.vfnote[0].addAccidental(this.vfnote[1], new Vex.Flow.Accidental(acc));
+            }
+        } else {
+            this.vfpitch = VexFlowConverter.pitch(pitch, this.clef);
+        }
+    }
+
+    /**
+     * Set the corresponding VexFlow StaveNote together with its index
+     * @param note
+     * @param index
+     */
+    public setIndex(note: Vex.Flow.StaveNote, index: number): void {
+        this.vfnote = [note, index];
+        //if (this.vfpitch && this.vfpitch[1]) {
+        //    note.addAccidental(index, new Vex.Flow.Accidental(this.vfpitch[1]));
+        //}
+    }
 }

+ 3 - 2
src/MusicalScore/Graphical/VexFlow/VexFlowGraphicalSymbolFactory.ts

@@ -18,6 +18,7 @@ import {GraphicalNote} from "../GraphicalNote";
 import {Pitch} from "../../../Common/DataObjects/pitch";
 import {TechnicalInstruction} from "../../VoiceData/Instructions/TechnicalInstruction";
 import {VexFlowGraphicalNote} from "./VexFlowGraphicalNote";
+import {VexFlowConverter} from "./VexFlowConverter";
 
 export class VexFlowGraphicalSymbolFactory implements IGraphicalSymbolFactory {
     /**
@@ -128,8 +129,8 @@ export class VexFlowGraphicalSymbolFactory implements IGraphicalSymbolFactory {
      */
     public addGraphicalAccidental(graphicalNote: GraphicalNote, pitch: Pitch, grace: boolean, graceScalingFactor: number): void {
         // ToDo: set accidental here from pitch.Accidental
-        // let note: VexFlowGraphicalNote = <VexFlowGraphicalNote> graphicalNote;
-        //note.vfpitch = 
+        let note: VexFlowGraphicalNote = <VexFlowGraphicalNote> graphicalNote;
+        note.setPitch(pitch);
     }
 
     /**

+ 6 - 4
src/MusicalScore/Graphical/VexFlow/VexFlowMeasure.ts

@@ -52,8 +52,10 @@ export class VexFlowMeasure extends StaffMeasure {
      */
     public resetLayout(): void {
         this.stave = new Vex.Flow.Stave(0, 0, 0);
-        this.beginInstructionsWidth = 0;
-        this.endInstructionsWidth = 0;
+        // Take into account some space for the begin and end lines of the stave
+        // Will be changed when repetitions will be implemented
+        this.beginInstructionsWidth = 20 / this.unit;
+        this.endInstructionsWidth = 20 / this.unit;
     }
 
     /**
@@ -242,8 +244,8 @@ export class VexFlowMeasure extends StaffMeasure {
             if (gnotes.hasOwnProperty(voiceID)) {
                 if (!(voiceID in vfVoices)) {
                     vfVoices[voiceID] = new Vex.Flow.Voice({
-                        beat_value: this.parentSourceMeasure.Duration.Denominator,
-                        num_beats: this.parentSourceMeasure.Duration.Numerator,
+                        beat_value: 4, //this.parentSourceMeasure.Duration.Denominator,
+                        num_beats: 3, //this.parentSourceMeasure.Duration.Numerator,
                         resolution: Vex.Flow.RESOLUTION,
                     }).setMode(Vex.Flow.Voice.Mode.SOFT);
                 }

+ 1 - 1
test/MusicalScore/Graphical/VexFlow/VexFlowMusicSheetDrawer.ts

@@ -9,7 +9,7 @@ import {IXmlElement} from "../../../../src/Common/FileIO/Xml";
 describe("VexFlow Music Sheet Drawer", () => {
 
     it(".drawSheet (Clementi pt. 1)", (done: MochaDone) => {
-        let path: string = "test/data/MuzioClementi_SonatinaOpus36No1_Part1.xml";
+        let path: string = "test/data/MuzioClementi_SonatinaOpus36No1_Part2.xml";
         // "test/data/MuzioClementi_SonatinaOpus36No1_Part1.xml";
         let score: IXmlElement = TestUtils.getScore(path);
         chai.expect(score).to.not.be.undefined;