Search in sources :

Example 21 with Voice

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

the class Test03b method test.

@Test
public void test() {
    Measure measure = getScore().getMeasure(mp0);
    // two voices
    assertEquals(2, measure.getVoices().size());
    // check first voice
    Voice voice = measure.getVoice(0);
    assertEquals(2, voice.getElements().size());
    assertEquals(Companion.pi(0, 0, 4), ((Chord) voice.getElement(0)).getNotes().get(0).getPitch());
    assertEquals(Companion.fr(1, 4), voice.getElement(0).getDuration());
    assertEquals(Companion.pi(0, 0, 4), ((Chord) voice.getElement(1)).getNotes().get(0).getPitch());
    assertEquals(Companion.fr(1, 4), voice.getElement(1).getDuration());
    // check second voice
    // in Zong!, there is no "empty" space in voices. Instead, an invisible rest is used
    voice = measure.getVoice(1);
    assertTrue(voice.getElement(0) instanceof Rest);
    assertTrue(((Rest) voice.getElement(0)).isHidden());
    assertEquals(Companion.fr(1, 4), voice.getElement(0).getDuration());
    assertEquals(Companion.pi(5, 0, 3), ((Chord) voice.getElement(1)).getNotes().get(0).getPitch());
    assertEquals(Companion.fr(1, 4), voice.getElement(1).getDuration());
    assertEquals(Companion.pi(5, 0, 3), ((Chord) voice.getElement(2)).getNotes().get(0).getPitch());
    assertEquals(Companion.fr(1, 4), voice.getElement(2).getDuration());
}
Also used : Rest(com.xenoage.zong.core.music.rest.Rest) Measure(com.xenoage.zong.core.music.Measure) Voice(com.xenoage.zong.core.music.Voice) Test(org.junit.Test)

Example 22 with Voice

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

the class Test42a method test.

@Test
public void test() {
    Staff staff = getFirstStaff();
    // test chords
    for (int iMeasure : range(2)) {
        for (int iVoice : range(2)) {
            MP mpVoice = atVoice(0, iMeasure, iVoice);
            Voice expectedVoice = expectedStaff.getVoice(mpVoice);
            Voice voice = staff.getVoice(mpVoice);
            assertEqualsVoice(expectedVoice, voice, mpVoice);
        }
    }
    // test slurs
    assertSlurBetween(getVoiceChords(staff.getVoice(atVoice(0, 1, 0))), 1, 2, SlurType.Slur);
    assertSlurBetween(getVoiceChords(staff.getVoice(atVoice(0, 1, 1))), 1, 2, SlurType.Slur);
}
Also used : MP(com.xenoage.zong.core.position.MP) Staff(com.xenoage.zong.core.music.Staff) VoiceTest.assertEqualsVoice(musicxmltestsuite.tests.utils.VoiceTest.assertEqualsVoice) MP.atVoice(com.xenoage.zong.core.position.MP.atVoice) Voice(com.xenoage.zong.core.music.Voice) Test(org.junit.Test)

Example 23 with Voice

use of com.xenoage.zong.core.music.Voice 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 24 with Voice

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

the class VoicesBeatOffsetterTest method createVoiceSpacings.

/**
 * Create {@link VoiceSpacing}s for the first measure column
 * of the given {@link Score}.
 */
private LinkedList<VoiceSpacing> createVoiceSpacings(Score score) {
    LinkedList<VoiceSpacing> ret = new LinkedList<>();
    for (int iStaff : range(0, score.getStavesCount() - 1)) {
        Measure measure = score.getMeasure(atMeasure(iStaff, 0));
        for (Voice voice : measure.getVoices()) {
            Fraction beat = Companion.fr(0);
            ArrayList<ElementSpacing> se = alist();
            float offset = 0;
            for (VoiceElement e : voice.getElements()) {
                // compute width
                float width = 0;
                if (e.getDuration().equals(Companion.get_0()))
                    width = width_grace;
                else if (e.getDuration().equals(dur_1_8))
                    width = width_1_8;
                else if (e.getDuration().equals(dur_1_6))
                    width = width_1_6;
                else if (e.getDuration().equals(dur_1_4))
                    width = width_1_4;
                else if (e.getDuration().equals(dur_3_8))
                    width = width_3_8;
                else if (e.getDuration().equals(dur_1_2))
                    width = width_1_2;
                else if (e.getDuration().equals(dur_1_1))
                    width = width_1_1;
                // create spacing element with offset
                se.add(new ChordSpacing(new ChordNotation((Chord) e), beat, offset));
                beat = beat.add(e.getDuration());
                offset += width;
            }
            se.add(new BorderSpacing(beat, offset));
            ret.add(new VoiceSpacing(voice, score.getFormat().getInterlineSpace(), se));
        }
    }
    return ret;
}
Also used : ChordNotation(com.xenoage.zong.musiclayout.notation.ChordNotation) Fraction(com.xenoage.utils.math.Fraction) LinkedList(java.util.LinkedList) VoiceElement(com.xenoage.zong.core.music.VoiceElement) Measure(com.xenoage.zong.core.music.Measure) Voice(com.xenoage.zong.core.music.Voice)

Example 25 with Voice

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

the class StretchMeasuresTest method createSystemWith1MeasureGrace.

/**
 * Creates and returns a simple {@link SystemSpacing} with only one
 * measure and three notes: two main notes and two grace notes between them.
 */
public static SystemSpacing createSystemWith1MeasureGrace(float offsetChord1, float offsetChord2, float offsetMeasureEnd, float graceDistance) {
    Chord chord1 = chord(Companion.pi(0, 0, 4), Companion.fr(2, 4));
    Chord chord2grace = graceChord(Companion.pi(1, 0, 4));
    Chord chord3grace = graceChord(Companion.pi(2, 0, 4));
    Chord chord4 = chord(Companion.pi(3, 0, 4), Companion.fr(2, 4));
    Voice voice = new Voice(alist(chord1, chord2grace, chord3grace, chord4));
    List<BeatOffset> beatOffsets = alist(new BeatOffset(Companion.fr(0, 4), offsetChord1), new BeatOffset(Companion.fr(2, 4), offsetChord2), new BeatOffset(Companion.fr(4, 4), offsetMeasureEnd));
    float is = 1;
    List<VoiceSpacing> voiceSpacings = alist(new VoiceSpacing(voice, is, alist(new ChordSpacing(new ChordNotation(chord1), beatOffsets.get(0).getBeat(), beatOffsets.get(0).getOffsetMm()), new ChordSpacing(new ChordNotation(chord2grace), beatOffsets.get(1).getBeat(), beatOffsets.get(1).getOffsetMm() - 2 * graceDistance), new ChordSpacing(new ChordNotation(chord3grace), beatOffsets.get(1).getBeat(), beatOffsets.get(1).getOffsetMm() - 1 * graceDistance), new ChordSpacing(new ChordNotation(chord4), beatOffsets.get(1).getBeat(), beatOffsets.get(1).getOffsetMm()))));
    MeasureSpacing measureSpacing = new MeasureSpacing(atMeasure(0, 0), is, voiceSpacings, empty, null);
    ColumnSpacing mcs = new ColumnSpacing(-1, alist(measureSpacing), beatOffsets, alist(new BeatOffset(Companion.fr(0, 4), 0), new BeatOffset(Companion.fr(4, 4), offsetMeasureEnd)));
    SystemSpacing system = new SystemSpacing(alist(mcs), 0, 0, offsetMeasureEnd, null, 0);
    return system;
}
Also used : ChordSpacing(com.xenoage.zong.musiclayout.spacing.ChordSpacing) ChordNotation(com.xenoage.zong.musiclayout.notation.ChordNotation) ColumnSpacing(com.xenoage.zong.musiclayout.spacing.ColumnSpacing) MeasureSpacing(com.xenoage.zong.musiclayout.spacing.MeasureSpacing) BeatOffset(com.xenoage.zong.musiclayout.spacing.BeatOffset) SystemSpacing(com.xenoage.zong.musiclayout.spacing.SystemSpacing) VoiceSpacing(com.xenoage.zong.musiclayout.spacing.VoiceSpacing) ChordFactory.graceChord(com.xenoage.zong.core.music.chord.ChordFactory.graceChord) Chord(com.xenoage.zong.core.music.chord.Chord) Voice(com.xenoage.zong.core.music.Voice)

Aggregations

Voice (com.xenoage.zong.core.music.Voice)39 Test (org.junit.Test)25 Rest (com.xenoage.zong.core.music.rest.Rest)16 Score (com.xenoage.zong.core.Score)15 MP (com.xenoage.zong.core.position.MP)14 VoiceElement (com.xenoage.zong.core.music.VoiceElement)12 Chord (com.xenoage.zong.core.music.chord.Chord)10 Measure (com.xenoage.zong.core.music.Measure)8 VoiceSpacing (com.xenoage.zong.musiclayout.spacing.VoiceSpacing)7 Staff (com.xenoage.zong.core.music.Staff)6 ChordNotation (com.xenoage.zong.musiclayout.notation.ChordNotation)6 ElementSpacing (com.xenoage.zong.musiclayout.spacing.ElementSpacing)5 Fraction (com.xenoage.utils.math.Fraction)4 VoiceTest (com.xenoage.zong.core.music.VoiceTest)4 Notations (com.xenoage.zong.musiclayout.notation.Notations)4 RestNotation (com.xenoage.zong.musiclayout.notation.RestNotation)4 LayoutSettingsTest (com.xenoage.zong.musiclayout.settings.LayoutSettingsTest)4 ElementWidth (com.xenoage.zong.musiclayout.spacing.ElementWidth)4 MP.atVoice (com.xenoage.zong.core.position.MP.atVoice)3 BeatOffset (com.xenoage.zong.musiclayout.spacing.BeatOffset)3