use of com.xenoage.zong.io.musicxml.in.util.MusicReaderException in project Zong by Xenoage.
the class ScoreReader method readToScore.
public void readToScore(Score score, ErrorHandling errorHandling) {
Context context = new Context(score, new ReaderSettings(errorHandling));
// create the measures of the parts
It<MxlPart> mxlParts = it(doc.getParts());
for (MxlPart mxlPart : mxlParts) {
// create measures
execute(new MeasureAddUpTo(score, mxlPart.getMeasures().size()));
// initialize each measure with a C clef
Part part = score.getStavesList().getParts().get(mxlParts.getIndex());
StavesRange stavesRange = score.getStavesList().getPartStaffIndices(part);
for (int staff : stavesRange.getRange()) {
execute(new MeasureElementWrite(new Clef(ClefType.Companion.getClefTreble()), score.getMeasure(MP.atMeasure(staff, 0)), Companion.get_0()));
}
}
// write a 4/4 measure and C key signature in the first measure
execute(new ColumnElementWrite(new TimeSignature(TimeType.Companion.getTime_4_4()), score.getColumnHeader(0), Companion.get_0(), null));
execute(new ColumnElementWrite(new TraditionalKey(0), score.getColumnHeader(0), Companion.get_0(), null));
// read the parts
mxlParts = it(doc.getParts());
for (MxlPart mxlPart : mxlParts) {
// clear part-dependent context values
context.beginNewPart(mxlParts.getIndex());
// read the measures
It<MxlMeasure> mxlMeasures = it(mxlPart.getMeasures());
for (MxlMeasure mxlMeasure : mxlMeasures) {
try {
MeasureReader.readToContext(mxlMeasure, mxlMeasures.getIndex(), context);
} catch (MusicReaderException ex) {
throw new RuntimeException("Error at " + ex.getContext().toString(), ex);
} catch (Exception ex) {
throw new RuntimeException("Error (roughly) around " + context.toString(), ex);
}
}
}
// remove unclosed elements
context.removeUnclosedWedges();
// go through the whole score, and fill empty measures (that means, measures where
// voice 0 has no single VoiceElement) with rests
Fraction measureDuration = Companion.fr(1, 4);
for (int iStaff = 0; iStaff < score.getStavesCount(); iStaff++) {
Staff staff = score.getStaff(atStaff(iStaff));
for (int iMeasure : range(staff.getMeasures())) {
Measure measure = staff.getMeasure(iMeasure);
TimeSignature newTime = score.getHeader().getColumnHeader(iMeasure).getTime();
if (newTime != null) {
// time signature has changed
measureDuration = newTime.getType().getMeasureBeats();
}
if (measureDuration == null) {
// senza misura
// use whole rest
measureDuration = Companion.fr(4, 4);
}
Voice voice0 = measure.getVoice(0);
if (voice0.isEmpty()) {
// TODO: "whole rests" or split. currently, also 3/4 rests are possible
MP mp = atElement(iStaff, iMeasure, 0, 0);
new VoiceElementWrite(score.getVoice(mp), mp, new Rest(measureDuration), null).execute();
}
}
}
}
use of com.xenoage.zong.io.musicxml.in.util.MusicReaderException in project Zong by Xenoage.
the class ChordReader method readToContext.
/**
* Reads the given chord, consisting of the given list of note elements,
* including beams, notations and lyrics.
* All but the first given note must have a chord-element inside.
*/
public void readToContext(Context context) {
this.context = context;
readFirstNote();
// find staff
// (not supported yet: multi-staff chords)
staff = notNull(mxlFirstNote.getStaff(), 1) - 1;
// find voice
// TODO: might not exist! we have to use a helper algorithm to determine the right voice
// then, see MusicReader class documentation.
int staffVoice = 0;
mxlVoice = null;
MxlEditorialVoice editorialVoice = mxlFirstNote.getEditorialVoice();
if (editorialVoice != null) {
mxlVoice = editorialVoice.getVoice();
if (mxlVoice != null) {
try {
staffVoice = context.getVoice(staff, mxlVoice);
} catch (MusicReaderException ex) {
context.reportError(ex.getMessage());
return;
}
}
}
// write chord or rest
boolean isWritten = false;
if (chordOrRest != null)
isWritten = context.writeVoiceElement(chordOrRest, staff, staffVoice);
// more details for chord
if (isWritten && chord != null) {
// check if chord could be written. if not, return
if (getMP(chord) == null)
return;
readFirstNoteNotations();
readOtherChordNotes();
readStem();
readBeams();
new LyricReader(mxlNotes).readToChord(chord);
}
if (chordOrRest != null)
context.moveCursorForward(chordOrRest.getDuration());
}
Aggregations