Ejemplo n.º 1
0
  /** Reads the given MusicXML document and returns the score. */
  public static Score read(
      MxlScorePartwise doc, Score baseScore, ErrorProcessing err, boolean ignoreErrors) {
    MusicReaderContext context =
        new MusicReaderContext(baseScore, new MusicReaderSettings(err, ignoreErrors));

    // read the parts
    int staffIndexOffset = 0;
    It<MxlPart> mxlParts = it(doc.getParts());
    for (MxlPart mxlPart : mxlParts) {
      // clear part-dependent context values
      int stavesCount =
          baseScore.getStavesList().getParts().get(mxlParts.getIndex()).getStavesCount();
      context = context.beginNewPart(mxlParts.getIndex());
      staffIndexOffset += stavesCount;
      // read the measures
      It<MxlMeasure> mxlMeasures = it(mxlPart.getMeasures());
      for (MxlMeasure mxlMeasure : mxlMeasures) {
        try {
          context = readMeasure(context, mxlMeasure, mxlMeasures.getIndex());
        } catch (MusicReaderException ex) {
          throw new RuntimeException("Error at " + ex.getContext().toString(), ex);
        } catch (Exception ex) {
          throw new RuntimeException("Error (roughly) around " + context.toString(), ex);
        }
      }
    }

    // go through the whole score, and fill empty measures (that means, measures where
    // voice 0 has no single VoiceElement) with rests
    Fraction measureDuration = fr(1, 4);
    for (int iStaff = 0; iStaff < context.getScore().getStavesCount(); iStaff++) {
      Staff staff = context.getScore().getStaff(atStaff(iStaff));
      for (int iMeasure = 0; iMeasure < staff.getMeasures().size(); iMeasure++) {
        Measure measure = staff.getMeasures().get(iMeasure);
        Time newTime = context.getScore().getScoreHeader().getColumnHeader(iMeasure).getTime();
        if (newTime != null) {
          // time signature has changed
          if (newTime instanceof NormalTime) {
            measureDuration = ((NormalTime) newTime).getBeatsPerMeasure();
          } else {
            measureDuration = fr(1, 4); // default: 1/4
          }
        }
        Voice voice0 = measure.getVoices().get(0);
        if (voice0.isEmpty()) {
          // TODO: "whole rests" or split. currently, also 3/4 rests are possible
          context =
              context.withScore(
                  ScoreController.writeVoiceElement(
                      context.getScore(), mp(iStaff, iMeasure, 0, _0), new Rest(measureDuration)));
        }
      }
    }

    return context.getScore();
  }
Ejemplo n.º 2
0
 /** Reads the given barline element. Currently only left and right barlines are supported. */
 private static MusicReaderContext readBarline(MusicReaderContext context, MxlBarline mxlBarline) {
   MxlRightLeftMiddle location = mxlBarline.getLocation();
   MxlRepeat repeat = mxlBarline.getRepeat();
   int measureIndex = context.getMP().getMeasure();
   BarlineStyle style = null;
   if (mxlBarline.getBarStyle() != null)
     style = BarlineStyleReader.read(mxlBarline.getBarStyle().getBarStyle());
   if (repeat != null) {
     // repeat barline
     if (location == MxlRightLeftMiddle.Left) {
       // left barline
       if (repeat.getDirection() == MxlBackwardForward.Forward) {
         style = notNull(style, BarlineStyle.HeavyLight);
         context =
             context.withScore(
                 ScoreController.writeColumnStartBarline(
                     context.getScore(), measureIndex, createForwardRepeatBarline(style)));
       }
     } else if (location == MxlRightLeftMiddle.Right) {
       // right barline
       if (repeat.getDirection() == MxlBackwardForward.Backward) {
         style = notNull(style, BarlineStyle.LightHeavy);
         int times = notNull(repeat.getTimes(), 1).intValue();
         context =
             context.withScore(
                 ScoreController.writeColumnEndBarline(
                     context.getScore(), measureIndex, createBackwardRepeatBarline(style, times)));
       }
     }
   } else {
     // regular barline
     style = notNull(style, BarlineStyle.Regular);
     if (location == MxlRightLeftMiddle.Left) {
       // left barline
       context =
           context.withScore(
               ScoreController.writeColumnStartBarline(
                   context.getScore(), measureIndex, createBarline(style)));
     } else if (location == MxlRightLeftMiddle.Right) {
       // right barline
       context =
           context.withScore(
               ScoreController.writeColumnEndBarline(
                   context.getScore(), measureIndex, createBarline(style)));
     }
   }
   return context;
 }
Ejemplo n.º 3
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;
  }