/** Reads the given direction element. */ private static MusicReaderContext readDirection( MusicReaderContext context, MxlDirection mxlDirection) { // staff int staff = notNull(mxlDirection.getStaff(), 1) - 1; // direction-types Words words = null; for (MxlDirectionType mxlType : mxlDirection.getDirectionTypes()) { MxlDirectionTypeContent mxlDTC = mxlType.getContent(); MxlDirectionTypeContentType mxlDTCType = mxlDTC.getDirectionTypeContentType(); switch (mxlDTCType) { case Dynamics: { // dynamics DynamicsType type = ((MxlDynamics) mxlDTC).getElement(); Dynamics dynamics = new Dynamics(type); context = context.writeMeasureElement(dynamics, staff); break; } case Pedal: { // pedal MxlPedal mxlPedal = (MxlPedal) mxlDTC; Pedal.Type type = null; switch (mxlPedal.getType()) { case Start: type = Type.Start; break; case Stop: type = Type.Stop; break; } if (type != null) { Pedal pedal = new Pedal( type, readPosition( mxlPedal.getPrintStyle().getPosition(), context.getTenthMm(), context.getStaffLinesCount(staff))); context = context.writeMeasureElement(pedal, staff); } break; } case Wedge: { // wedge MxlWedge mxlWedge = (MxlWedge) mxlDTC; int number = mxlWedge.getNumber(); Position pos = readPosition( mxlWedge.getPosition(), context.getTenthMm(), context.getStaffLinesCount(staff)); switch (mxlWedge.getType()) { case Crescendo: Wedge crescendo = new Crescendo(null, pos); context = context.writeMeasureElement(crescendo, staff); context = context.openWedge(number, crescendo); break; case Diminuendo: Wedge diminuendo = new Diminuendo(null, pos); context = context.writeMeasureElement(diminuendo, staff); context = context.openWedge(number, diminuendo); break; case Stop: Tuple2<MusicReaderContext, Wedge> t = context.closeWedge(number); context = t.get1(); Wedge wedge = t.get2(); if (wedge == null) throw new RuntimeException("Wedge " + (number + 1) + " is not open!"); context = context.writeMeasureElement(wedge.getWedgeEnd(), staff); break; } break; } case Words: { // words (currently only one element is supported) if (words == null) { MxlWords mxlWords = (MxlWords) mxlDTC; MxlFormattedText mxlFormattedText = mxlWords.getFormattedText(); FontInfo fontInfo = readFontInfo(mxlFormattedText.getPrintStyle().getFont()); Position position = readPosition( mxlFormattedText.getPrintStyle().getPosition(), context.getTenthMm(), context.getStaffLinesCount(staff)); words = new Words(mxlFormattedText.getValue(), fontInfo, position); } break; } } } // sound MxlSound mxlSound = mxlDirection.getSound(); if (mxlSound != null) { // tempo if (mxlSound.getTempo() != null) { // always expressed in quarter notes per minute int quarterNotesPerMinute = mxlSound.getTempo().intValue(); // if there were words found, use them for the tempo Tempo tempo; if (words != null) { tempo = new Tempo(fr(1, 4), quarterNotesPerMinute, words.getText(), words.getPosition()); words = null; // words were used now } else { tempo = new Tempo(fr(1, 4), quarterNotesPerMinute, null, null); } // write to measure context = context.writeMeasureElement(tempo, staff); } } // if there are words that were not used for the tempo, write them now if (words != null) { context = context.writeMeasureElement(words, staff); } return context; }
/** Reads the given attributes element. */ private static MusicReaderContext readAttributes( MusicReaderContext context, MxlAttributes mxlAttributes) { // divisions Integer divisions = mxlAttributes.getDivisions(); if (divisions != null) { context = context.withDivisions(divisions); } // key signature MxlKey mxlKey = mxlAttributes.getKey(); if (mxlKey != null) { // only the fifths element is supported int mxlFifths = mxlKey.getFifths(); // write to column header (TODO: attribute "number" for single staves) Key key = new TraditionalKey(mxlFifths); context = context.writeColumnElement(key); } // time signature MxlTime mxlTime = mxlAttributes.getTime(); if (mxlTime != null) { Time time = null; MxlTimeContentType type = mxlTime.getContent().getTimeContentType(); if (type == MxlTimeContentType.SenzaMisura) { // senza misura time = new SenzaMisura(); } else if (type == MxlTimeContentType.NormalTime) { // at the moment we read only one beats/beat-type // currently we accept only integers > 0 MxlNormalTime mxlNormalTime = (MxlNormalTime) mxlTime.getContent(); time = new NormalTime(mxlNormalTime.getBeats(), mxlNormalTime.getBeatType()); } // write to column header (TODO: attribute "number" for single staves) if (time != null) { context = context.writeColumnElement(time); } } // clefs MxlClef mxlClef = mxlAttributes.getClef(); if (mxlClef != null) { Clef clef = null; switch (mxlClef.getSign()) { /* TODO case C: clef = new Clef(ClefType.C); break; */ case F: clef = new Clef(ClefType.F); break; case G: clef = new Clef(ClefType.G); break; /* TODO case NONE: clef = new Clef(ClefType.C); break; case PERCUSSION: clef = new Clef(ClefType.C); break; case TAB: clef = new Clef(ClefType.C); break; */ } // staff (called "number" in MusicXML), first staff is default int staff = mxlClef.getNumber() - 1; // add to staff if (clef != null) { context = context.writeMeasureElement(clef, staff); } } /* TODO: transposition changes ~= instrument changes //transposition changes MxlTranspose mxlTranspose = mxlAttributes.getTranspose(); if (mxlTranspose != null) { int chromatic = mxlTranspose.getChromatic(); Transpose transpose = new Transpose(chromatic); //write to all staves of this part for (int staff = 0; staff < context.getPartStavesIndices().getCount(); staff++) { writeNoVoiceElement(transpose, staff); } } */ return context; }