Search in sources :

Example 6 with MxlMeasure

use of com.xenoage.zong.musicxml.types.partwise.MxlMeasure in project Zong by Xenoage.

the class Test21d method test.

@Test
public void test() {
    MxlPart part = getFirstPart();
    List<Tuple2<MP, ? extends Direction>> expectedDirections = getExpectedDirections();
    // check only directions in this test
    int iDirection = 0;
    for (int iMeasure = 0; iMeasure <= 1; iMeasure++) {
        MxlMeasure measure = part.getMeasures().get(iMeasure);
        for (MxlMusicDataContent data : measure.getMusicData().getContent()) {
            if (data.getMusicDataContentType() == MxlMusicDataContentType.Direction) {
                // check type
                MxlDirection dir = (MxlDirection) data;
                MxlDirectionTypeContent content = dir.getDirectionTypes().get(0).getContent();
                if (iDirection == 0) {
                    // Words "Largo"
                    assertEquals(0, iMeasure);
                    assertEquals(MxlDirectionTypeContentType.Words, content.getDirectionTypeContentType());
                    assertEquals("Largo", ((MxlWords) content).getFormattedText().getValue());
                } else if (iDirection == 1) {
                    // Dynamic "fp"
                    assertEquals(0, iMeasure);
                    assertEquals(MxlDirectionTypeContentType.Dynamics, content.getDirectionTypeContentType());
                    assertEquals(DynamicValue.fp, ((MxlDynamics) content).getElement());
                } else if (iDirection == 2) {
                    // Dynamic "p"
                    assertEquals(1, iMeasure);
                    assertEquals(MxlDirectionTypeContentType.Dynamics, content.getDirectionTypeContentType());
                    assertEquals(DynamicValue.p, ((MxlDynamics) content).getElement());
                }
                iDirection++;
            }
        }
    }
    assertEquals("not all directions found", expectedDirections.size(), iDirection);
}
Also used : MxlMusicDataContent(com.xenoage.zong.musicxml.types.choice.MxlMusicDataContent) MxlDynamics(com.xenoage.zong.musicxml.types.MxlDynamics) Tuple2(com.xenoage.utils.kernel.Tuple2) MxlDirectionTypeContent(com.xenoage.zong.musicxml.types.choice.MxlDirectionTypeContent) MxlWords(com.xenoage.zong.musicxml.types.MxlWords) MxlPart(com.xenoage.zong.musicxml.types.partwise.MxlPart) MxlDirection(com.xenoage.zong.musicxml.types.MxlDirection) Direction(com.xenoage.zong.core.music.direction.Direction) MxlMeasure(com.xenoage.zong.musicxml.types.partwise.MxlMeasure) MxlDirection(com.xenoage.zong.musicxml.types.MxlDirection) Test(org.junit.Test)

Example 7 with MxlMeasure

use of com.xenoage.zong.musicxml.types.partwise.MxlMeasure in project Zong by Xenoage.

the class Test01b method test.

@Test
public void test() {
    Pitch[] expectedPitches = getExpectedPitches();
    MxlPart part = getFirstPart();
    int iPitch = 0;
    for (int iM = 0; iM < part.getMeasures().size(); iM++) {
        MxlMeasure measure = part.getMeasures().get(iM);
        for (MxlMusicDataContent data : measure.getMusicData().getContent()) {
            if (data.getMusicDataContentType() == MxlMusicDataContentType.Note) {
                // check note and pitch
                MxlFullNote note = ((MxlNote) data).getContent().getFullNote();
                MxlPitch pitch = (MxlPitch) (note.getContent());
                assertEquals("note " + iPitch, expectedPitches[iPitch++], pitch.getPitch());
            }
        }
    }
    assertEquals("not all notes found", expectedPitches.length, iPitch);
}
Also used : MxlMusicDataContent(com.xenoage.zong.musicxml.types.choice.MxlMusicDataContent) MxlFullNote(com.xenoage.zong.musicxml.types.groups.MxlFullNote) MxlPitch(com.xenoage.zong.musicxml.types.MxlPitch) MxlPitch(com.xenoage.zong.musicxml.types.MxlPitch) Pitch(com.xenoage.zong.core.music.Pitch) MxlPart(com.xenoage.zong.musicxml.types.partwise.MxlPart) MxlMeasure(com.xenoage.zong.musicxml.types.partwise.MxlMeasure) Test(org.junit.Test)

Example 8 with MxlMeasure

use of com.xenoage.zong.musicxml.types.partwise.MxlMeasure 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)

Example 9 with MxlMeasure

use of com.xenoage.zong.musicxml.types.partwise.MxlMeasure in project Zong by Xenoage.

the class StavesListReader method countStaves.

/**
 * Counts the number of staves used in each part and returns them.
 * @return a hashmap which maps a part ID to the number of staves in this part
 */
private HashMap<String, Integer> countStaves(MxlScorePartwise mxlScore) {
    HashMap<String, Integer> ret = map();
    // check all parts
    for (MxlPart mxlPart : mxlScore.getParts()) {
        String id = mxlPart.getId();
        // heck all measures for attributes with staves-element and store the greatest value
        int maxStaves = 1;
        for (MxlMeasure mxlMeasure : mxlPart.getMeasures()) {
            for (MxlMusicDataContent content : mxlMeasure.getMusicData().getContent()) {
                if (content.getMusicDataContentType() == MxlMusicDataContentType.Attributes) {
                    Integer xmlStaves = ((MxlAttributes) content).getStaves();
                    if (xmlStaves != null) {
                        maxStaves = Math.max(maxStaves, xmlStaves);
                    }
                }
            }
        }
        // set the number of staves of the part
        ret.put(id, maxStaves);
    }
    return ret;
}
Also used : MxlMusicDataContent(com.xenoage.zong.musicxml.types.choice.MxlMusicDataContent) MxlPart(com.xenoage.zong.musicxml.types.partwise.MxlPart) MxlMeasure(com.xenoage.zong.musicxml.types.partwise.MxlMeasure)

Example 10 with MxlMeasure

use of com.xenoage.zong.musicxml.types.partwise.MxlMeasure in project Zong by Xenoage.

the class Test03b method test.

@Test
public void test() {
    // elements in this measure: attributes, note, note, backup, note, note
    MxlMeasure measure = getFirstMeasure();
    List<MxlMusicDataContent> content = measure.getMusicData().getContent();
    assertEquals(6, content.size());
    assertEquals(MxlMusicDataContentType.Attributes, content.get(0).getMusicDataContentType());
    assertEquals(MxlMusicDataContentType.Note, content.get(1).getMusicDataContentType());
    assertEquals(MxlMusicDataContentType.Note, content.get(2).getMusicDataContentType());
    assertEquals(MxlMusicDataContentType.Backup, content.get(3).getMusicDataContentType());
    assertEquals(MxlMusicDataContentType.Note, content.get(4).getMusicDataContentType());
    assertEquals(MxlMusicDataContentType.Note, content.get(5).getMusicDataContentType());
}
Also used : MxlMusicDataContent(com.xenoage.zong.musicxml.types.choice.MxlMusicDataContent) MxlMeasure(com.xenoage.zong.musicxml.types.partwise.MxlMeasure) Test(org.junit.Test)

Aggregations

MxlMeasure (com.xenoage.zong.musicxml.types.partwise.MxlMeasure)12 MxlMusicDataContent (com.xenoage.zong.musicxml.types.choice.MxlMusicDataContent)11 MxlPart (com.xenoage.zong.musicxml.types.partwise.MxlPart)10 Test (org.junit.Test)10 ToDo (musicxmltestsuite.tests.utils.ToDo)4 TraditionalKey (com.xenoage.zong.core.music.key.TraditionalKey)3 MxlAttributes (com.xenoage.zong.musicxml.types.MxlAttributes)3 MxlPitch (com.xenoage.zong.musicxml.types.MxlPitch)3 MxlFullNote (com.xenoage.zong.musicxml.types.groups.MxlFullNote)3 Pitch (com.xenoage.zong.core.music.Pitch)2 MxlKey (com.xenoage.zong.musicxml.types.MxlKey)2 MxlNormalNote (com.xenoage.zong.musicxml.types.choice.MxlNormalNote)2 Tuple2 (com.xenoage.utils.kernel.Tuple2)1 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 Measure (com.xenoage.zong.core.music.Measure)1 Part (com.xenoage.zong.core.music.Part)1