Browse Source

Add note shape calculation

Andrea Condoluci 9 years ago
parent
commit
264864f21e

+ 39 - 1
src/MusicalScore/Calculation/MeasureSizeCalculator.ts

@@ -1,7 +1,12 @@
 import Vex = require("vexflow");
+import StaveNote = Vex.Flow.StaveNote;
+
+// The type PositionAndShapeInfo is still to be ported in TypeScript
+type PositionAndShapeInfo = any;
+declare var PositionAndShapeInfo: any;
 
 /* TODO
- * Take into account StaveModifiers
+ * Complete support for StaveModifiers
  * Take into account Ties and Slurs
  */
 
@@ -48,6 +53,39 @@ export class MeasureSizeCalculator {
     this.format();
   }
 
+  // Returns the shape of the note head at position _index_ inside _note_.
+  // Remember: in VexFlow, StaveNote correspond to PhonicScore's VoiceEntries.
+  //  public static getVexFlowNoteHeadShape(note: StaveNote, index: number): PositionAndShapeInfo {
+  //  // note_heads is not public in StaveNote, but we access it anyway...
+  //  let bb = note.note_heads[index].getBoundingBox();
+  //  let info: any = new PositionAndShapeInfo();
+  //  let x: number = bb.getX();
+  //  let y: number = bb.getY();
+  //  let w: number = bb.getW();
+  //  let h: number = bb.getH();
+  //  info.Left = info.Right = bb.getW() / 2;
+  //  info.Top = info.Bottom = bb.getH() / 2;
+  //  info.X = bb.getX() + info.Left;
+  //  info.Y = bb.getY() + info.Bottom;
+  //  return info;
+  //}
+
+  // Returns the shape of all the note heads inside a StaveNote.
+  // Remember: in VexFlow, StaveNote correspond to PhonicScore's VoiceEntries.
+  public static getVexFlowStaveNoteShape(note: StaveNote): PositionAndShapeInfo {
+    let info: any = new PositionAndShapeInfo();
+    let bounds: any = note.getNoteHeadBounds();
+    let beginX: number = note.getNoteHeadBeginX();
+    let endX: number = note.getNoteHeadEndX();
+
+    info.Left = info.Right = (endX - beginX) / 2;
+    info.Top = info.Bottom = (bounds.y_top - bounds.y_bottom) / 2;
+    info.X = beginX + info.Left;
+    info.Y = bounds.y_bottom + info.Bottom;
+    return info;
+  }
+
+
   public static getClefBoundingBox(clef: Vex.Flow.Clef): Vex.Flow.BoundingBox {
     let clef2: any = clef;
     clef2.placeGlyphOnLine(clef2.glyph, clef2.stave, clef2.clef.line);

+ 2 - 2
src/MusicalScore/ScoreIO/InstrumentReader.ts

@@ -162,7 +162,7 @@ export class InstrumentReader {
           }
 
           let restNote: boolean = xmlNode.element("rest") !== undefined;
-          Logging.log("New note found!", noteDivisions, noteDuration.toString(), restNote);
+          //Logging.log("New note found!", noteDivisions, noteDuration.toString(), restNote);
           let isGraceNote: boolean = xmlNode.element("grace") !== undefined || noteDivisions === 0 || isChord && lastNoteWasGrace;
           let musicTimestamp: Fraction = currentFraction.clone();
           if (isChord) {
@@ -173,7 +173,7 @@ export class InstrumentReader {
             this.inSourceMeasureInstrumentIndex + noteStaff - 1,
             this.currentStaff
           ).staffEntry;
-          Logging.log("currentStaffEntry", this.currentStaffEntry, this.currentMeasure.VerticalSourceStaffEntryContainers.length);
+          //Logging.log("currentStaffEntry", this.currentStaffEntry, this.currentMeasure.VerticalSourceStaffEntryContainers.length);
 
           if (!this.currentVoiceGenerator.hasVoiceEntry() || (!isChord && !isGraceNote && !lastNoteWasGrace) || (!lastNoteWasGrace && isGraceNote)) {
             this.currentVoiceGenerator.createVoiceEntry(musicTimestamp, this.currentStaffEntry, !restNote);

+ 2 - 2
src/MusicalScore/ScoreIO/VoiceGenerator.ts

@@ -85,7 +85,7 @@ export class VoiceGenerator {
     ): Note {
         this.currentStaffEntry = parentStaffEntry;
         this.currentMeasure = parentMeasure;
-        Logging.debug("read called:", restNote);
+        //Logging.debug("read called:", restNote);
         try {
             this.currentNote = restNote
                 ? this.addRestNote(noteDuration, divisions)
@@ -265,7 +265,7 @@ export class VoiceGenerator {
     private addSingleNote(
         node: IXmlElement, noteDuration: number, divisions: number, graceNote: boolean, chord: boolean, guitarPro: boolean
     ): Note {
-        Logging.debug("addSingleNote called");
+        //Logging.debug("addSingleNote called");
         let noteAlter: AccidentalEnum = AccidentalEnum.NONE;
         let noteStep: NoteEnum = NoteEnum.C;
         let noteOctave: number = 0;

+ 1 - 0
test/MusicalScore/ScoreIO/MusicSheetReader.ts

@@ -59,6 +59,7 @@ describe("Music Sheet Reader Tests", () => {
 
     it("Notes", (done: MochaDone) => {
         // Staff Entries on first measure
+
         // chai.expect(sheet.SourceMeasures[0].VerticalSourceStaffEntryContainers[0].StaffEntries.length).to.equal(4);
         done();
     });

+ 4 - 0
typings/vexflow.d.ts

@@ -30,6 +30,10 @@ declare namespace Vex {
 
     export class StaveNote {
       constructor(note_struct: any);
+
+      public getNoteHeadBounds(): any;
+      public getNoteHeadBeginX(): number;
+      public getNoteHeadEndX(): number;
     }
 
     export class Stave {