Search in sources :

Example 1 with Measure

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

the class ColumnSpacer method compute.

/**
 * Computes a {@link ColumnSpacing} from a measure column.
 * @param context         the current context, with the current {@link MP} and precomputed
 *                        element {@link Notation}s
 * @param createLeading   true, if a leading spacing has to be created, otherwise false
 * @param notations       the precomputed notations of the measure and voice elements
 */
public ColumnSpacing compute(Context context, boolean createLeading, Notations notations) {
    context.saveMp();
    int measureIndex = context.mp.measure;
    Column column = context.score.getColumn(measureIndex);
    ColumnHeader columnHeader = context.score.getHeader().getColumnHeader(measureIndex);
    // compute the optimal spacings for each voice separately
    List<List<VoiceSpacing>> voiceSpacingsByStaff = alist();
    for (int iStaff : range(column)) {
        List<VoiceSpacing> vss = alist();
        Measure measure = column.get(iStaff);
        for (Voice voice : measure.getVoices()) {
            context.mp = MP.atVoice(iStaff, measureIndex, measure.getVoices().indexOf(voice));
            VoiceSpacing vs = singleVoiceSpacer.compute(context, notations);
            vss.add(vs);
        }
        voiceSpacingsByStaff.add(vss);
    }
    // compute the measure elements (like inner clefs) and accordingly updated voice spacings
    ArrayList<List<ElementSpacing>> optimalMeasureElementsSpacingsByStaff = alist();
    for (int iStaff : range(column)) {
        context.mp = MP.atMeasure(iStaff, measureIndex);
        List<ElementSpacing> measureSpacing = measureElementsSpacer.compute(context, createLeading, voiceSpacingsByStaff.get(iStaff), notations);
        optimalMeasureElementsSpacingsByStaff.add(measureSpacing);
    }
    // compute the common beat offsets of this measure column
    Fraction measureBeats = context.score.getMeasureBeats(measureIndex);
    VoiceSpacingsByStaff voiceSpacings = new VoiceSpacingsByStaff(voiceSpacingsByStaff);
    List<BeatOffset> beatOffsets = voicesBeatOffsetter.compute(voiceSpacings.getAll(), measureBeats, context.settings.offsetBeatsMinimal);
    // recompute beat offsets with respect to barlines
    BarlinesBeatOffsetter.Result offsets = barlinesBeatOffsetter.compute(beatOffsets, columnHeader, context.score.getMaxIS());
    beatOffsets = offsets.voiceElementOffsets;
    List<BeatOffset> barlineOffsets = offsets.barlineOffsets;
    // compute the spacings for the whole column, so that equal beats are aligned
    ArrayList<List<ElementSpacing>> alignedMeasureElementsSpacingsByStaff = alist();
    for (int iStaff : range(column)) {
        Measure measure = column.get(iStaff);
        // voice spacings
        for (int iVoice : range(measure.getVoices())) alignedVoicesSpacer.compute(voiceSpacings.get(iStaff, iVoice), beatOffsets);
        // measure elements, based on the aligned voice spacings
        context.mp = atMeasure(iStaff, measureIndex);
        alignedMeasureElementsSpacingsByStaff.add(measureElementsSpacer.compute(context, createLeading, voiceSpacings.getStaff(iStaff), notations));
    }
    // compute spacings for each staff
    List<MeasureSpacing> measureSpacings = alist(column.size());
    for (int iStaff : range(column)) {
        // create leading spacing, if needed
        LeadingSpacing leadingSpacing = null;
        if (createLeading) {
            context.mp = atBeat(iStaff, measureIndex, 0, Fraction.Companion.get_0());
            leadingSpacing = leadingSpacer.compute(context, notations);
        }
        // create measure spacing
        float interlineSpace = context.score.getInterlineSpace(iStaff);
        measureSpacings.add(new MeasureSpacing(atMeasure(iStaff, measureIndex), interlineSpace, voiceSpacings.getStaff(iStaff), alignedMeasureElementsSpacingsByStaff.get(iStaff), leadingSpacing));
    }
    context.restoreMp();
    return new ColumnSpacing(measureIndex, measureSpacings, beatOffsets, barlineOffsets);
}
Also used : LeadingSpacing(com.xenoage.zong.musiclayout.spacing.LeadingSpacing) ColumnSpacing(com.xenoage.zong.musiclayout.spacing.ColumnSpacing) Fraction(com.xenoage.utils.math.Fraction) ElementSpacing(com.xenoage.zong.musiclayout.spacing.ElementSpacing) ColumnHeader(com.xenoage.zong.core.header.ColumnHeader) Column(com.xenoage.zong.core.music.util.Column) BarlinesBeatOffsetter(com.xenoage.zong.musiclayout.spacer.beat.BarlinesBeatOffsetter) MP.atMeasure(com.xenoage.zong.core.position.MP.atMeasure) Measure(com.xenoage.zong.core.music.Measure) MeasureSpacing(com.xenoage.zong.musiclayout.spacing.MeasureSpacing) VoiceSpacingsByStaff(com.xenoage.zong.musiclayout.layouter.columnspacing.VoiceSpacingsByStaff) BeatOffset(com.xenoage.zong.musiclayout.spacing.BeatOffset) ArrayList(java.util.ArrayList) List(java.util.List) VoiceSpacing(com.xenoage.zong.musiclayout.spacing.VoiceSpacing) Voice(com.xenoage.zong.core.music.Voice)

Example 2 with Measure

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

the class ScoreTest method testMeasureElements.

private static <T> void testMeasureElements(List<Tuple2<MP, T>> expectedElements, Score score, BiFunction<Measure, Fraction, T> getElementAtBeat) {
    for (Tuple2<MP, T> expectedElement : expectedElements) {
        MP mp = expectedElement.get1();
        Measure measure = score.getMeasure(mp);
        T element = getElementAtBeat.apply(measure, mp.beat);
        assertNotNull("" + mp, element);
        assertEquals("" + mp, expectedElement.get2(), element);
    }
}
Also used : MP(com.xenoage.zong.core.position.MP) Measure(com.xenoage.zong.core.music.Measure)

Example 3 with Measure

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

the class WedgeStamper method stampSystem.

/**
 * Creates all wedge stampings in the given system.
 * @param openWedges  input and output parameter: voltas, which are still open from the
 *                    last system. After the method returns, it contains the voltas which
 *                    are still open after this system
 */
public List<WedgeStamping> stampSystem(SystemSpacing system, Score score, StaffStampings staffStampings, OpenWedges openWedges) {
    List<WedgeStamping> ret = alist();
    // find new wedges beginning in this staff
    for (int iStaff : range(score.getStavesCount())) {
        Staff staff = score.getStaff(iStaff);
        for (int iMeasure : system.getMeasures()) {
            Measure measure = staff.getMeasure(iMeasure);
            for (BeatE<Direction> dir : measure.getDirections()) {
                if (dir.getElement() instanceof Wedge) {
                    Wedge wedge = (Wedge) dir.getElement();
                    // if the position of the end element is known
                    if (wedge.getWedgeEnd().getMP() != null)
                        openWedges.wedges.add(new ContinuedWedge((Wedge) dir.getElement()));
                }
            }
        }
    }
    // draw wedges in the cache, and remove them if closed in this system
    for (Iterator<ContinuedWedge> itW = openWedges.wedges.iterator(); itW.hasNext(); ) {
        ContinuedWedge wedge = itW.next();
        ret.add(stamp(wedge.element, staffStampings.get(system.getSystemIndexInFrame(), wedge.element.getMP().staff)));
        if (MP.getMP(wedge.element.getWedgeEnd()).measure <= system.getEndMeasure()) {
            // wedge is closed
            itW.remove();
        }
    }
    return ret;
}
Also used : ContinuedWedge(com.xenoage.zong.musiclayout.continued.ContinuedWedge) Staff(com.xenoage.zong.core.music.Staff) Measure(com.xenoage.zong.core.music.Measure) WedgeStamping(com.xenoage.zong.musiclayout.stampings.WedgeStamping) Direction(com.xenoage.zong.core.music.direction.Direction) Wedge(com.xenoage.zong.core.music.direction.Wedge) ContinuedWedge(com.xenoage.zong.musiclayout.continued.ContinuedWedge)

Example 4 with Measure

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

the class InstrumentsReaderTest method testInstrumentChanges.

/**
 * Read the file "InstrumentChanges.xml".
 * It must contain 3 instruments, namely a Clarinet in B, an Alto Sax in Eb
 * and a Trombone in C. Check also the transpositons and see if the instrument
 * changes happen at the right positions.
 */
@Test
public void testInstrumentChanges() {
    Score score = MusicXmlScoreFileInputTest.loadXMLTestScore("InstrumentChanges.xml");
    Part part = score.getStavesList().getParts().get(0);
    assertEquals(3, part.getInstruments().size());
    // clarinet
    PitchedInstrument instr0 = (PitchedInstrument) part.getInstruments().get(0);
    assertEquals("Clarinet in Bb", instr0.getName());
    assertEquals(new Integer(-1), instr0.getTranspose().getDiatonic());
    assertEquals(-2, instr0.getTranspose().getChromatic());
    // altosax
    PitchedInstrument instr1 = (PitchedInstrument) part.getInstruments().get(1);
    assertEquals("Alto Saxophone", instr1.getName());
    assertEquals(new Integer(-5), instr1.getTranspose().getDiatonic());
    assertEquals(-9, instr1.getTranspose().getChromatic());
    // trombone
    PitchedInstrument instr2 = (PitchedInstrument) part.getInstruments().get(2);
    assertEquals("Trombone", instr2.getName());
    assertEquals(new Integer(0), instr2.getTranspose().getDiatonic());
    assertEquals(0, instr2.getTranspose().getChromatic());
    // instrument changes in measures 1, 2 and 3
    Measure measure = score.getMeasure(atMeasure(0, 1));
    assertEquals(instr1, getInstrumentChangeAtBeat0(measure).getInstrument());
    measure = score.getMeasure(atMeasure(0, 2));
    assertEquals(instr2, getInstrumentChangeAtBeat0(measure).getInstrument());
    measure = score.getMeasure(atMeasure(0, 3));
    assertEquals(instr0, getInstrumentChangeAtBeat0(measure).getInstrument());
}
Also used : Score(com.xenoage.zong.core.Score) Part(com.xenoage.zong.core.music.Part) PitchedInstrument(com.xenoage.zong.core.instrument.PitchedInstrument) MP.atMeasure(com.xenoage.zong.core.position.MP.atMeasure) Measure(com.xenoage.zong.core.music.Measure) MusicXmlScoreFileInputTest(com.xenoage.zong.io.musicxml.in.MusicXmlScoreFileInputTest) Test(org.junit.Test)

Example 5 with Measure

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

the class Test01a method test.

@ToDo("the editiorial sharp (sharp in parenthesis) in the last measure is not supported yet")
@Test
public void test() {
    Pitch[] expectedPitches = getExpectedPitches();
    Staff staff = getFirstStaff();
    assertEquals(26, staff.getMeasures().size());
    int iPitch = 0;
    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());
            }
        }
    }
    assertEquals("not all notes found", expectedPitches.length, iPitch);
}
Also used : VoiceElement(com.xenoage.zong.core.music.VoiceElement) Staff(com.xenoage.zong.core.music.Staff) Pitch(com.xenoage.zong.core.music.Pitch) Measure(com.xenoage.zong.core.music.Measure) Voice(com.xenoage.zong.core.music.Voice) Chord(com.xenoage.zong.core.music.chord.Chord) ToDo(musicxmltestsuite.tests.utils.ToDo) Test(org.junit.Test)

Aggregations

Measure (com.xenoage.zong.core.music.Measure)12 Voice (com.xenoage.zong.core.music.Voice)8 Staff (com.xenoage.zong.core.music.Staff)5 Test (org.junit.Test)5 VoiceElement (com.xenoage.zong.core.music.VoiceElement)4 Fraction (com.xenoage.utils.math.Fraction)3 Chord (com.xenoage.zong.core.music.chord.Chord)3 MP (com.xenoage.zong.core.position.MP)3 MP.atMeasure (com.xenoage.zong.core.position.MP.atMeasure)3 ColumnHeader (com.xenoage.zong.core.header.ColumnHeader)2 Part (com.xenoage.zong.core.music.Part)2 Pitch (com.xenoage.zong.core.music.Pitch)2 Rest (com.xenoage.zong.core.music.rest.Rest)2 ToDo (musicxmltestsuite.tests.utils.ToDo)2 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 PitchedInstrument (com.xenoage.zong.core.instrument.PitchedInstrument)1