use of com.xenoage.zong.core.music.StavesList in project Zong by Xenoage.
the class MusicXmlScoreFileInput method read.
/**
* Builds a {@link Score} entity from a {@link MxlScorePartwise} document.
* @param doc the provided score-partwise document
* @param errorHandling how to deal with errors
*/
public Score read(MxlScorePartwise mxlScore, Level errorHandling) throws InvalidFormatException {
try {
// create new score
Score score = new Score();
// read information about the score
ScoreInfo scoreInfo = new ScoreInfoReader(mxlScore.getScoreHeader()).read();
score.setInfo(scoreInfo);
// read score format
MxlScoreHeader mxlScoreHeader = mxlScore.getScoreHeader();
MxlDefaults mxlDefaults = mxlScoreHeader.getDefaults();
ScoreFormat scoreFormat = new ScoreFormatReader(mxlDefaults).read();
score.setFormat(scoreFormat);
// read layout format
MxlLayout mxlLayout = (mxlDefaults != null ? mxlDefaults.getLayout() : null);
LayoutFormat layoutFormat = new LayoutFormatReader(mxlLayout, scoreFormat.getInterlineSpace() / 10).read();
// TIDY
score.setMetaData("layoutformat", layoutFormat);
// create the list of staves
ErrorHandling mxlErrorHandling = new ErrorHandling(errorHandling);
StavesListReader stavesListReader = new StavesListReader(mxlScore, mxlErrorHandling);
StavesList stavesList = stavesListReader.read();
stavesList.setScore(score);
score.setStavesList(stavesList);
// read the musical contents
new ScoreReader(mxlScore).readToScore(score, mxlErrorHandling);
// remember the XML document for further application-dependend processing
// TIDY
score.setMetaData("mxldoc", mxlScore);
// when errors were collected, log them and save them as metadata
if (mxlErrorHandling.getErrorMessages().size() > 0) {
INSTANCE.log(Companion.warning("The file could be loaded, but the following error(s) were reported: " + mxlErrorHandling.getErrorMessages()));
score.setMetaData("mxlerrors", mxlErrorHandling.getErrorMessages());
}
return score;
} catch (RuntimeException ex) {
// catch runtime exceptions and rethrow them in the expected type
throw new InvalidFormatException(ex);
}
}
use of com.xenoage.zong.core.music.StavesList in project Zong by Xenoage.
the class ScoreFrameLayouter method computeScoreFrameLayout.
/**
* Creates a {@link ScoreFrameLayout} from the given {@link FrameSpacing}.
*
* @param unclosedElements unclosed elements from the last frame, like slurs
* spanning over more than one frame
*/
public ScoreFrameLayout computeScoreFrameLayout(FrameSpacing frame, int frameIndex, Notations notations, List<ContinuedElement> unclosedElements, Context layouterContext, Map<Beam, BeamSpacing> beamsSpacing) {
layouterContext.saveMp();
Score score = layouterContext.score;
SymbolPool symbols = layouterContext.symbols;
StamperContext context = new StamperContext();
context.layouter = layouterContext;
context.notations = notations;
ScoreHeader header = score.getHeader();
int stavesCount = score.getStavesCount();
StavesList stavesList = score.getStavesList();
ArrayList<StaffStamping> staffStampsPool = alist();
ArrayList<Stamping> otherStampsPool = alist();
// default lyric style
FormattedTextStyle defaultLyricStyle = new FormattedTextStyle(score.getFormat().getLyricFont());
// caches
OpenSlursCache openCurvedLinesCache = new OpenSlursCache();
OpenWedges openWedges = new OpenWedges();
OpenLyricsCache openLyricsCache = new OpenLyricsCache();
LastLyrics lastLyrics = new LastLyrics();
OpenTupletsCache openTupletsCache = new OpenTupletsCache();
OpenVolta openVolta = new OpenVolta();
// add continued elements
for (ContinuedElement ce : unclosedElements) {
if (ce instanceof ContinuedSlur) {
openCurvedLinesCache.add(SlurCache.createContinued((ContinuedSlur) ce));
} else if (ce instanceof ContinuedVolta) {
openVolta.volta = (ContinuedVolta) ce;
} else if (ce instanceof ContinuedWedge) {
openWedges.wedges.add((ContinuedWedge) ce);
}
}
// create staff stampings
StaffStampings staffStampings = staffStamper.createStaffStampings(score, frame);
staffStampings.addAllTo(staffStampsPool);
context.staffStampings = staffStampings;
// go through the systems
for (int iSystem : range(frame.getSystems())) {
context.systemIndex = iSystem;
SystemSpacing system = frame.getSystems().get(iSystem);
List<StaffStamping> systemStaves = staffStampings.getAllOfSystem(iSystem);
StaffStamping systemFirstStaff = getFirst(systemStaves);
// add the part names (first system) or part abbreviations (other systems)
int iStaffInPart = 0;
for (Part part : stavesList.getParts()) {
PartNameStamper.Style style = (frameIndex == 0 && iSystem == 0 ? PartNameStamper.Style.Full : PartNameStamper.Style.Abbreviated);
addNotNull(otherStampsPool, partNameStamper.stamp(part, iStaffInPart, systemStaves, style));
iStaffInPart += part.getStavesCount();
}
// create the brackets at the beginning of the system
for (BracketGroup bracketGroup : stavesList.getBracketGroups()) {
StavesRange r = bracketGroup.getStaves();
otherStampsPool.add(new BracketStamping(systemStaves.get(r.getStart()), systemStaves.get(r.getStop()), system.getMarginLeftMm() - 1.4f, bracketGroup.getStyle()));
}
// create the barlines and measure numbers
otherStampsPool.addAll(barlinesStamper.stamp(system, systemStaves, score));
// fill the staves
for (int iStaff : range(stavesCount)) {
layouterContext.mp = layouterContext.mp.withStaff(iStaff);
context.staffIndex = iStaff;
float xMm = context.getCurrentStaffStamping().positionMm.x;
for (int iMeasure : range(system.columns)) {
int globalMeasureIndex = system.getStartMeasure() + iMeasure;
layouterContext.mp = layouterContext.mp.withMeasure(globalMeasureIndex);
context.measureIndex = globalMeasureIndex;
ColumnSpacing measureColumnSpacing = system.columns.get(iMeasure);
MeasureSpacing measure = measureColumnSpacing.getMeasures().get(iStaff);
// add leading spacing elements, if available
otherStampsPool.addAll(measureStamper.stampLeading(measure, xMm, context));
// add directions
otherStampsPool.addAll(directionStamper.stamp(context));
// add measure elements within this measure
float voicesXMm = xMm + measureColumnSpacing.getLeadingWidthMm();
otherStampsPool.addAll(measureStamper.stampMeasure(measure, voicesXMm, context));
// add voice elements within this measure
otherStampsPool.addAll(voiceStamper.stampVoices(measure, voicesXMm, staffStampings, context, defaultLyricStyle, beamsSpacing, openCurvedLinesCache, openLyricsCache, lastLyrics, openTupletsCache));
xMm += measureColumnSpacing.getWidthMm();
}
}
// create all voltas in this system, including open voltas from the last system
otherStampsPool.addAll(voltaStamper.stampSystem(systemFirstStaff, openVolta, header, defaultLyricStyle));
// create all wedges in this system
otherStampsPool.addAll(wedgeStamper.stampSystem(system, score, staffStampings, openWedges));
}
// create the collected ties and slurs
otherStampsPool.addAll(createTiesAndSlurs(openCurvedLinesCache, staffStampings, frame.getSystems().size()));
// create the open lyric underscore lines
for (Tuple3<StaffTextStamping, NoteheadStamping, Integer> openUnderscore : openLyricsCache.getUnderscores()) {
// TODO: fetch style efficiently
FormattedTextStyle style = defaultLyricStyle;
FormattedTextElement firstElement = openUnderscore.get1().getText().getFirstParagraph().getElements().getFirst();
if (firstElement instanceof FormattedTextString) {
style = ((FormattedTextString) firstElement).getStyle();
}
otherStampsPool.addAll(lyricStamper.createUnderscoreStampings(openUnderscore.get1(), openUnderscore.get2(), style, staffStampings.getAllOfStaff(openUnderscore.get3())));
}
// create tuplet brackets/numbers
for (Tuplet tuplet : openTupletsCache) {
otherStampsPool.add(tupletStamper.createTupletStamping(tuplet, openTupletsCache, symbols));
}
// collect elements that have to be continued on the next frame
ArrayList<ContinuedElement> continuedElements = alist();
for (SlurCache clc : openCurvedLinesCache) {
continuedElements.add(clc.getContinuedCurvedLine());
}
if (openVolta.volta != null)
continuedElements.add(openVolta.volta);
continuedElements.addAll(openWedges.wedges);
layouterContext.restoreMp();
return new ScoreFrameLayout(frame, staffStampsPool, otherStampsPool, continuedElements);
}
use of com.xenoage.zong.core.music.StavesList in project Zong by Xenoage.
the class StavesListReader method createStavesList.
/**
* Creates the (still empty) {@link StavesList} for this document.
*/
private StavesList createStavesList(List<Part> parts, List<PartsBarlineGroup> barlineGroups, List<PartsBracketGroup> bracketGroups) {
StavesList ret = new StavesList();
// add parts
for (Part part : parts) {
ret.getParts().add(part);
for (int i = 0; i < part.getStavesCount(); i++) {
Staff staff = Staff.Companion.staffMinimal();
staff.setParent(ret);
ret.getStaves().add(staff);
}
}
// add groups
for (PartsBarlineGroup barlineGroup : barlineGroups) {
int startIndex = getFirstStaffIndex(barlineGroup.startPartIndex, parts);
int endIndex = getLastStaffIndex(barlineGroup.stopPartIndex, parts);
ret.addBarlineGroup(new StavesRange(startIndex, endIndex), barlineGroup.style);
}
for (PartsBracketGroup bracketGroup : bracketGroups) {
int startIndex = getFirstStaffIndex(bracketGroup.startPartIndex, parts);
int endIndex = getLastStaffIndex(bracketGroup.stopPartIndex, parts);
ret.addBracketGroup(new StavesRange(startIndex, endIndex), bracketGroup.style);
}
// parts with more than one staff
for (int i : range(parts)) {
if (parts.get(i).getStavesCount() > 1 && !isPartInGroup(i, barlineGroups, bracketGroups)) {
int startIndex = getFirstStaffIndex(i, parts);
int endIndex = getLastStaffIndex(i, parts);
ret.addBarlineGroup(new StavesRange(startIndex, endIndex), BarlineGroup.Style.Common);
ret.addBracketGroup(new StavesRange(startIndex, endIndex), BracketGroup.Style.Brace);
}
}
return ret;
}
use of com.xenoage.zong.core.music.StavesList in project Zong by Xenoage.
the class StavesListReaderTest method testStavesList1.
/**
* Reads and checks the parts and staves of the file "BeetAnGeSample.xml"
* from the MusicXML 1.1 sample files.
*/
@Test
public void testStavesList1() {
StavesList sl = createStavesList("data/test/scores/musicxml11/BeetAnGeSample.xml");
if (sl == null)
return;
// parts and staffStampings
assertEquals(2, sl.getParts().size());
assertEquals(3, sl.getStaves().size());
assertEquals(0, sl.getPartStaffIndices(sl.getParts().get(0)).getStart());
assertEquals(0, sl.getPartStaffIndices(sl.getParts().get(0)).getStop());
assertEquals(1, sl.getPartStaffIndices(sl.getParts().get(1)).getStart());
assertEquals(2, sl.getPartStaffIndices(sl.getParts().get(1)).getStop());
// barline groups (implicitly added)
assertNull(sl.getBarlineGroupByStaff(0));
assertNotNull(sl.getBarlineGroupByStaff(1));
assertNotNull(sl.getBarlineGroupByStaff(2));
assertEquals(1, sl.getBarlineGroupByStaff(1).getStaves().getStart());
assertEquals(2, sl.getBarlineGroupByStaff(1).getStaves().getStop());
assertEquals(BarlineGroup.Style.Common, sl.getBarlineGroupByStaff(1).getStyle());
// bracket groups (implicitly added)
assertEquals(1, sl.getBracketGroups().size());
assertEquals(1, sl.getBracketGroups().get(0).getStaves().getStart());
assertEquals(2, sl.getBracketGroups().get(0).getStaves().getStop());
assertEquals(BracketGroup.Style.Brace, sl.getBracketGroups().get(0).getStyle());
}
use of com.xenoage.zong.core.music.StavesList in project Zong by Xenoage.
the class Test41b method test.
@Test
public void test() {
Score score = getScore();
StavesList stavesList = score.getStavesList();
List<Part> parts = stavesList.getParts();
assertEquals(expectedNames.length, parts.size());
for (int i : range(parts)) {
assertEquals(expectedNames[i], parts.get(i).getName());
}
}
Aggregations