Example #1
0
 @Test
 public void test() {
   MxlPart part = getFirstPart();
   List<Tuple2<MP, ? extends Direction>> expectedDirections = getExpectedDirections();
   // check only directions in this test
   int iDirection = 0;
   for (int iMeasure = 0; iMeasure <= 1; iMeasure++) {
     MxlMeasure measure = part.getMeasures().get(iMeasure);
     for (MxlMusicDataContent data : measure.getMusicData().getContent()) {
       if (data.getMusicDataContentType() == MxlMusicDataContentType.Direction) {
         // check type
         MxlDirection dir = (MxlDirection) data;
         MxlDirectionTypeContent content = dir.getDirectionTypes().get(0).getContent();
         if (iDirection == 0) {
           // Words "Largo"
           assertEquals(0, iMeasure);
           assertEquals(MxlDirectionTypeContentType.Words, content.getDirectionTypeContentType());
           assertEquals("Largo", ((MxlWords) content).getFormattedText().getValue());
         } else if (iDirection == 1) {
           // Dynamics "fp"
           assertEquals(0, iMeasure);
           assertEquals(
               MxlDirectionTypeContentType.Dynamics, content.getDirectionTypeContentType());
           assertEquals(DynamicsType.fp, ((MxlDynamics) content).getElement());
         } else if (iDirection == 2) {
           // Dynamics "p"
           assertEquals(1, iMeasure);
           assertEquals(
               MxlDirectionTypeContentType.Dynamics, content.getDirectionTypeContentType());
           assertEquals(DynamicsType.p, ((MxlDynamics) content).getElement());
         }
         iDirection++;
       }
     }
   }
   assertEquals("not all directions found", expectedDirections.size(), iDirection);
 }
Example #2
0
  /** 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;
  }