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
 @Test
 public void test() {
   Pitch[] expectedPitches = getExpectedPitches();
   int iPitch = 0;
   Staff staff = getFirstStaff();
   for (int iM = 0; iM < staff.getMeasures().size(); iM++) {
     Measure measure = staff.getMeasures().get(iM);
     Voice voice = measure.getVoice(0);
     for (VoiceElement e : voice.getElements()) {
       if (e instanceof Chord) {
         // check note and pitch
         Chord chord = (Chord) e;
         assertEquals(expectedPitches[iPitch++], chord.getNotes().get(0).getPitch());
       }
     }
   }
   // TODO - ignore this test, since MusicXML input file has a bug (only a single measure),
   // so currently only the first measure is tested
   // assertEquals("not all notes found", expectedPitches.length, iPitch);
 }
Ejemplo n.º 3
0
 /**
  * Creates a score with a single staff, two measures, and a number of clefs, keys and chords. The
  * score, the list of clefs and the list of keys is returned.
  */
 private Tuple3<Score, List<Clef>, List<Key>> createTestScoreClefsKeys() {
   // create two measures:
   // clef-g, key-Gmaj, C#1/4, clef-f, Db1/4, clef-g, key-Fmaj, Cnat1/4 |
   // C#1/4, key-Cmaj, clef-f, C2/4.
   Score score = ScoreFactory.create1Staff();
   new MeasureAdd(score, 1).execute();
   List<Clef> clefs = new ArrayList<Clef>();
   List<Key> keys = new ArrayList<Key>();
   Clef c;
   Key k;
   // measure 0
   Measure measure = score.getMeasure(atMeasure(0, 0));
   new Clef(ClefType.clefTreble);
   clefs.add(c = new Clef(ClefType.clefTreble));
   new MeasureElementWrite(c, measure, fr(0, 4)).execute();
   keys.add(k = new TraditionalKey(1, Mode.Major));
   new MeasureElementWrite(k, measure, fr(0, 4)).execute();
   new VoiceElementWrite(
           measure.getVoice(0), atElement(0, 0, 0, 0), chord(pi(0, 1, 4), fr(1, 4)), null)
       .execute();
   clefs.add(c = new Clef(ClefType.clefBass));
   new MeasureElementWrite(c, measure, fr(1, 4)).execute();
   new VoiceElementWrite(
           measure.getVoice(0), atElement(0, 0, 0, 1), chord(pi(1, -1, 4), fr(1, 4)), null)
       .execute();
   clefs.add(c = new Clef(ClefType.clefTreble));
   new MeasureElementWrite(c, measure, fr(2, 4)).execute();
   keys.add(k = new TraditionalKey(-1, Mode.Major));
   new MeasureElementWrite(k, measure, fr(2, 4)).execute();
   new VoiceElementWrite(
           measure.getVoice(0), atElement(0, 0, 0, 2), chord(pi(0, 0, 4), fr(1, 4)), null)
       .execute();
   // measure 1
   measure = score.getMeasure(atMeasure(0, 1));
   new VoiceElementWrite(
           measure.getVoice(0), atElement(0, 1, 0, 0), chord(pi(0, 1, 4), fr(1, 4)), null)
       .execute();
   keys.add(k = new TraditionalKey(0, Mode.Major));
   new MeasureElementWrite(k, measure, fr(1, 4)).execute();
   clefs.add(c = new Clef(ClefType.clefBass));
   new MeasureElementWrite(c, measure, fr(1, 4)).execute();
   new VoiceElementWrite(
           measure.getVoice(0), atElement(0, 1, 0, 1), chord(pi(0, 0, 4), fr(2, 4)), null)
       .execute();
   return t3(score, clefs, keys);
 }