Przeglądaj źródła

InstrumentReader: read NoteType from Xml (e.g. Quarter)

sschmid 5 lat temu
rodzic
commit
43ab59f0ac

+ 4 - 1
src/MusicalScore/ScoreIO/InstrumentReader.ts

@@ -24,6 +24,7 @@ import {ExpressionReader} from "./MusicSymbolModules/ExpressionReader";
 import {RepetitionInstructionReader} from "./MusicSymbolModules/RepetitionInstructionReader";
 import {SlurReader} from "./MusicSymbolModules/SlurReader";
 import {StemDirectionType} from "../VoiceData/VoiceEntry";
+import {NoteType, NoteTypeHandler} from "../VoiceData";
 //import Dictionary from "typescript-collections/dist/lib/Dictionary";
 
 // FIXME: The following classes are missing
@@ -232,6 +233,7 @@ export class InstrumentReader {
           }
           // alternative: check for <type size="cue">
           const typeNode: IXmlElement = xmlNode.element("type");
+          let noteTypeXml: NoteType = NoteType.UNDEFINED;
           if (typeNode !== undefined) {
             const sizeAttr: Attr = typeNode.attribute("size");
             if (sizeAttr !== undefined && sizeAttr !== null) {
@@ -239,6 +241,7 @@ export class InstrumentReader {
                 isCueNote = true;
               }
             }
+            noteTypeXml = NoteTypeHandler.StringToNoteType(typeNode.value);
           }
 
           // check stem element
@@ -360,7 +363,7 @@ export class InstrumentReader {
             noteDuration = new Fraction(noteDivisions, 4 * this.divisions);
           }
           this.currentVoiceGenerator.read(
-            xmlNode, noteDuration, typeDuration, normalNotes, restNote,
+            xmlNode, noteDuration, typeDuration, noteTypeXml, normalNotes, restNote,
             this.currentStaffEntry, this.currentMeasure,
             measureStartAbsoluteTimestamp,
             this.maxTieNoteFraction, isChord, guitarPro,

+ 9 - 5
src/MusicalScore/ScoreIO/VoiceGenerator.ts

@@ -27,6 +27,7 @@ import { ArticulationReader } from "./MusicSymbolModules/ArticulationReader";
 import { SlurReader } from "./MusicSymbolModules/SlurReader";
 import { Notehead } from "../VoiceData/Notehead";
 import { Arpeggio } from "../VoiceData/Arpeggio";
+import { NoteType } from "../VoiceData/NoteType";
 
 export class VoiceGenerator {
   constructor(instrument: Instrument, voiceId: number, slurReader: SlurReader, mainVoice: Voice = undefined) {
@@ -103,7 +104,7 @@ export class VoiceGenerator {
    * @param printObject whether the note should be rendered (true) or invisible (false)
    * @returns {Note}
    */
-  public read(noteNode: IXmlElement, noteDuration: Fraction, typeDuration: Fraction, normalNotes: number, restNote: boolean,
+  public read(noteNode: IXmlElement, noteDuration: Fraction, typeDuration: Fraction, noteTypeXml: NoteType, normalNotes: number, restNote: boolean,
               parentStaffEntry: SourceStaffEntry, parentMeasure: SourceMeasure,
               measureStartAbsoluteTimestamp: Fraction, maxTieNoteFraction: Fraction, chord: boolean, guitarPro: boolean,
               printObject: boolean, isCueNote: boolean, stemDirectionXml: StemDirectionType, tremoloStrokes: number,
@@ -113,8 +114,8 @@ export class VoiceGenerator {
     //log.debug("read called:", restNote);
     try {
       this.currentNote = restNote
-        ? this.addRestNote(noteDuration, printObject, isCueNote, noteheadColorXml)
-        : this.addSingleNote(noteNode, noteDuration, typeDuration, normalNotes, chord, guitarPro,
+        ? this.addRestNote(noteDuration, noteTypeXml, printObject, isCueNote, noteheadColorXml)
+        : this.addSingleNote(noteNode, noteDuration, noteTypeXml, typeDuration, normalNotes, chord, guitarPro,
                              printObject, isCueNote, stemDirectionXml, tremoloStrokes, stemColorXml, noteheadColorXml);
       // read lyrics
       const lyricElements: IXmlElement[] = noteNode.elements("lyric");
@@ -325,7 +326,8 @@ export class VoiceGenerator {
    * @param guitarPro
    * @returns {Note}
    */
-  private addSingleNote(node: IXmlElement, noteDuration: Fraction, typeDuration: Fraction, normalNotes: number, chord: boolean, guitarPro: boolean,
+  private addSingleNote(node: IXmlElement, noteDuration: Fraction, noteTypeXml: NoteType, typeDuration: Fraction,
+                        normalNotes: number, chord: boolean, guitarPro: boolean,
                         printObject: boolean, isCueNote: boolean, stemDirectionXml: StemDirectionType, tremoloStrokes: number,
                         stemColorXml: string, noteheadColorXml: string): Note {
     //log.debug("addSingleNote called");
@@ -420,6 +422,7 @@ export class VoiceGenerator {
     const noteLength: Fraction = Fraction.createFromFraction(noteDuration);
     const note: Note = new Note(this.currentVoiceEntry, this.currentStaffEntry, noteLength, pitch);
     note.TypeLength = typeDuration;
+    note.NoteTypeXml = noteTypeXml;
     note.NormalNotes = normalNotes;
     note.PrintObject = printObject;
     note.IsCueNote = isCueNote;
@@ -450,9 +453,10 @@ export class VoiceGenerator {
    * @param divisions
    * @returns {Note}
    */
-  private addRestNote(noteDuration: Fraction, printObject: boolean, isCueNote: boolean, noteheadColorXml: string): Note {
+  private addRestNote(noteDuration: Fraction, noteTypeXml: NoteType, printObject: boolean, isCueNote: boolean, noteheadColorXml: string): Note {
     const restFraction: Fraction = Fraction.createFromFraction(noteDuration);
     const restNote: Note = new Note(this.currentVoiceEntry, this.currentStaffEntry, restFraction, undefined);
+    restNote.NoteTypeXml = noteTypeXml;
     restNote.PrintObject = printObject;
     restNote.IsCueNote = isCueNote;
     restNote.NoteheadColorXml = noteheadColorXml;

+ 9 - 0
src/MusicalScore/VoiceData/Note.ts

@@ -10,6 +10,7 @@ import {Slur} from "./Expressions/ContinuousExpressions/Slur";
 import {NoteState} from "../Graphical/DrawingEnums";
 import {Notehead} from "./Notehead";
 import {Arpeggio} from "./Arpeggio";
+import {NoteType} from "./NoteType";
 
 /**
  * Represents a single pitch with a duration (length)
@@ -38,6 +39,8 @@ export class Note {
     private length: Fraction;
     /** The length/duration given in the <type> tag. different from length for tuplets/tremolos. */
     private typeLength: Fraction;
+    /** The NoteType given in the XML, e.g. quarter, which can be a normal quarter or tuplet quarter -> can have different length/fraction */
+    private noteTypeXml: NoteType;
     /** The amount of notes the tuplet of this note (if there is one) replaces. */
     private normalNotes: number;
     /**
@@ -104,6 +107,12 @@ export class Note {
     public set TypeLength(value: Fraction) {
         this.typeLength = value;
     }
+    public get NoteTypeXml(): NoteType {
+        return this.noteTypeXml;
+    }
+    public set NoteTypeXml(value: NoteType) {
+        this.noteTypeXml = value;
+    }
     public get NormalNotes(): number {
         return this.normalNotes;
     }

+ 36 - 0
src/MusicalScore/VoiceData/NoteType.ts

@@ -0,0 +1,36 @@
+export enum NoteType {
+    // xml note types, e.g. given as <note><type>quarter, see:
+    //https://usermanuals.musicxml.com/MusicXML/Content/ST-MusicXML-note-type-value.htm
+
+    UNDEFINED, // e.g. not given in XML
+    _1024th, // enum member cannot start with a number
+    _512th,
+    _256th,
+    _128th,
+    _64th,
+    _16th,
+    EIGTH,
+    QUARTER,
+    HALF,
+    WHOLE,
+    BREVE,
+    LONG,
+    MAXIMA
+}
+
+export class NoteTypeHandler {
+    // tslint:disable-next-line: variable-name
+    public static NoteTypeXmlValues: string[] = ["", "1024th", "512th", "256th", "128th", "64th", "32th", "16th",
+        "eigth", "quarter", "half", "whole", "breve", "long", "maxima"];
+    // alternative to array: use switch/case
+
+
+    public static NoteTypeToString(noteType: NoteType): string {
+        return this.NoteTypeXmlValues[noteType]; // assumes that the enum values are ordered from 0 to x, like NoteTypeXmlValues array members
+    }
+
+    public static StringToNoteType(noteType: string): NoteType {
+        const indexInArray: number = this.NoteTypeXmlValues.indexOf(noteType);
+        return indexInArray !== -1 ? indexInArray : NoteType.UNDEFINED;
+    }
+}

+ 1 - 0
src/MusicalScore/VoiceData/index.ts

@@ -6,6 +6,7 @@ export * from "./ChordSymbolContainer";
 export * from "./LinkedVoice";
 export * from "./Note";
 export * from "./Notehead";
+export * from "./NoteType";
 export * from "./OrnamentContainer";
 export * from "./SourceMeasure";
 export * from "./SourceStaffEntry";

+ 1 - 0
tslint.json

@@ -101,6 +101,7 @@
     "variable-name": [
       true,
       "check-format",
+      "allow-pascal-case",
       "ban-keywords"
     ],
     "whitespace": [