Search in sources :

Example 1 with StavesRange

use of com.xenoage.zong.core.music.group.StavesRange 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);
}
Also used : PartNameStamper(com.xenoage.zong.musiclayout.stamper.PartNameStamper) SlurCache(com.xenoage.zong.musiclayout.layouter.cache.util.SlurCache) LastLyrics(com.xenoage.zong.musiclayout.layouter.scoreframelayout.util.LastLyrics) StavesRange(com.xenoage.zong.core.music.group.StavesRange) OpenSlursCache(com.xenoage.zong.musiclayout.layouter.cache.OpenSlursCache) SymbolPool(com.xenoage.zong.symbols.SymbolPool) OpenTupletsCache(com.xenoage.zong.musiclayout.layouter.cache.OpenTupletsCache) FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) BracketGroup(com.xenoage.zong.core.music.group.BracketGroup) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) Tuplet(com.xenoage.zong.core.music.tuplet.Tuplet) StamperContext(com.xenoage.zong.musiclayout.stamper.StamperContext) Score(com.xenoage.zong.core.Score) ScoreHeader(com.xenoage.zong.core.header.ScoreHeader) Part(com.xenoage.zong.core.music.Part) StavesList(com.xenoage.zong.core.music.StavesList) ScoreFrameLayout(com.xenoage.zong.musiclayout.ScoreFrameLayout) OpenLyricsCache(com.xenoage.zong.musiclayout.layouter.cache.OpenLyricsCache) StaffStampings(com.xenoage.zong.musiclayout.layouter.scoreframelayout.util.StaffStampings) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement)

Example 2 with StavesRange

use of com.xenoage.zong.core.music.group.StavesRange 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;
}
Also used : StavesRange(com.xenoage.zong.core.music.group.StavesRange) MxlPart(com.xenoage.zong.musicxml.types.partwise.MxlPart) Part(com.xenoage.zong.core.music.Part) StavesList(com.xenoage.zong.core.music.StavesList) Staff(com.xenoage.zong.core.music.Staff)

Example 3 with StavesRange

use of com.xenoage.zong.core.music.group.StavesRange in project Zong by Xenoage.

the class StavesListTest method addBarlineGroupTest.

@Test
public void addBarlineGroupTest() {
    StavesList stavesList = create5Staves();
    // add barline groups from staves 0 to 2 and 1 to 3. should merge to 0 to 3
    stavesList.addBarlineGroup(new StavesRange(0, 2), BarlineGroup.Style.Common);
    stavesList.addBarlineGroup(new StavesRange(1, 3), BarlineGroup.Style.Common);
    assertEquals(1, stavesList.getBarlineGroups().size());
    assertEquals(new BarlineGroup(new StavesRange(0, 3), BarlineGroup.Style.Common), stavesList.getBarlineGroups().get(0));
}
Also used : StavesRange(com.xenoage.zong.core.music.group.StavesRange) BarlineGroup(com.xenoage.zong.core.music.group.BarlineGroup) Test(org.junit.Test)

Example 4 with StavesRange

use of com.xenoage.zong.core.music.group.StavesRange 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();
            }
        }
    }
}
Also used : VoiceElementWrite(com.xenoage.zong.commands.core.music.VoiceElementWrite) MP.atStaff(com.xenoage.zong.core.position.MP.atStaff) Staff(com.xenoage.zong.core.music.Staff) ColumnElementWrite(com.xenoage.zong.commands.core.music.ColumnElementWrite) MxlPart(com.xenoage.zong.musicxml.types.partwise.MxlPart) Clef(com.xenoage.zong.core.music.clef.Clef) TraditionalKey(com.xenoage.zong.core.music.key.TraditionalKey) MxlMeasure(com.xenoage.zong.musicxml.types.partwise.MxlMeasure) MeasureAddUpTo(com.xenoage.zong.commands.core.music.MeasureAddUpTo) StavesRange(com.xenoage.zong.core.music.group.StavesRange) Rest(com.xenoage.zong.core.music.rest.Rest) MxlMeasure(com.xenoage.zong.musicxml.types.partwise.MxlMeasure) Measure(com.xenoage.zong.core.music.Measure) MP(com.xenoage.zong.core.position.MP) MusicReaderException(com.xenoage.zong.io.musicxml.in.util.MusicReaderException) Fraction(com.xenoage.utils.math.Fraction) MusicReaderException(com.xenoage.zong.io.musicxml.in.util.MusicReaderException) TimeSignature(com.xenoage.zong.core.music.time.TimeSignature) MxlPart(com.xenoage.zong.musicxml.types.partwise.MxlPart) Part(com.xenoage.zong.core.music.Part) MeasureElementWrite(com.xenoage.zong.commands.core.music.MeasureElementWrite) Voice(com.xenoage.zong.core.music.Voice)

Aggregations

StavesRange (com.xenoage.zong.core.music.group.StavesRange)4 Part (com.xenoage.zong.core.music.Part)3 Staff (com.xenoage.zong.core.music.Staff)2 StavesList (com.xenoage.zong.core.music.StavesList)2 MxlPart (com.xenoage.zong.musicxml.types.partwise.MxlPart)2 Fraction (com.xenoage.utils.math.Fraction)1 ColumnElementWrite (com.xenoage.zong.commands.core.music.ColumnElementWrite)1 MeasureAddUpTo (com.xenoage.zong.commands.core.music.MeasureAddUpTo)1 MeasureElementWrite (com.xenoage.zong.commands.core.music.MeasureElementWrite)1 VoiceElementWrite (com.xenoage.zong.commands.core.music.VoiceElementWrite)1 Score (com.xenoage.zong.core.Score)1 ScoreHeader (com.xenoage.zong.core.header.ScoreHeader)1 Measure (com.xenoage.zong.core.music.Measure)1 Voice (com.xenoage.zong.core.music.Voice)1 Clef (com.xenoage.zong.core.music.clef.Clef)1 BarlineGroup (com.xenoage.zong.core.music.group.BarlineGroup)1 BracketGroup (com.xenoage.zong.core.music.group.BracketGroup)1 TraditionalKey (com.xenoage.zong.core.music.key.TraditionalKey)1 Rest (com.xenoage.zong.core.music.rest.Rest)1 TimeSignature (com.xenoage.zong.core.music.time.TimeSignature)1