Ejemplo n.º 1
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;
  }
Ejemplo n.º 2
0
  /** Reads the given print element. */
  private static MusicReaderContext readPrint(MusicReaderContext context, MxlPrint mxlPrint) {
    MxlPrintAttributes mxlPA = mxlPrint.getPrintAttributes();

    // system and page break
    Boolean newSystem = mxlPA.getNewSystem();
    SystemBreak systemBreak =
        (newSystem == null ? null : (newSystem ? SystemBreak.NewSystem : SystemBreak.NoNewSystem));
    Boolean newPage = mxlPA.getNewPage();
    PageBreak pageBreak =
        (newPage == null ? null : (newPage ? PageBreak.NewPage : PageBreak.NoNewPage));
    context =
        context.withScore(
            ScoreController.writeColumnElement(
                context.getScore(),
                atMeasure(context.getMP().getMeasure()),
                new Break(pageBreak, systemBreak)));

    // we assume that custom system layout information is just used in combination with
    // forced system/page breaks. so we ignore system-layout elements which are not combined
    // with system/page breaks.
    // the first measure of a score is also ok.
    if (context.getMP().getMeasure() == 0
        || systemBreak == SystemBreak.NewSystem
        || pageBreak == PageBreak.NewPage) {

      // first page or new page?
      boolean isPageBreak = pageBreak == PageBreak.NewPage;
      boolean isPageStarted = (context.getMP().getMeasure() == 0 || isPageBreak);
      if (isPageBreak) {
        // increment page index
        context = context.incPageIndex();
      }

      // first system or new system?
      boolean isSystemBreak = isPageBreak || systemBreak == SystemBreak.NewSystem;
      if (isSystemBreak) {
        // increment system index
        context = context.incSystemIndex();
      }

      // read system layout, if there
      MxlSystemLayout mxlSystemLayout = mxlPrint.getLayout().getSystemLayout();
      if (mxlSystemLayout != null) {
        SystemLayoutReader.Value sl =
            SystemLayoutReader.read(mxlSystemLayout, context.getTenthMm());
        SystemLayout systemLayout = sl.systemLayout;

        // for first systems on a page, use top-system-distance
        if (isPageStarted) {
          systemLayout = systemLayout.withSystemDistance(sl.topSystemDistance);
        }

        // apply values
        ScoreHeader scoreHeader =
            context
                .getScore()
                .getScoreHeader()
                .withSystemLayout(context.getSystemIndex(), systemLayout);
        context = context.withScore(context.getScore().withScoreHeader(scoreHeader));
      }
    }

    // staff layouts
    for (MxlStaffLayout mxlStaffLayout : mxlPrint.getLayout().getStaffLayouts()) {
      int staffIndex = mxlStaffLayout.getNumberNotNull() - 1;
      context =
          context.withScore(
              ScoreController.withStaffLayout(
                  context.getScore(),
                  context.getSystemIndex(),
                  context.getPartStavesIndices().getStart() + staffIndex,
                  readStaffLayout(mxlStaffLayout, context.getTenthMm()).staffLayout));
    }

    return context;
  }