Search in sources :

Example 1 with MeasureFullException

use of com.xenoage.zong.utils.exceptions.MeasureFullException in project Zong by Xenoage.

the class VoiceElementWriteTest method testTimeAware.

/**
 * Obey the time signature. Fail is written element is too long.
 */
@Test
public void testTimeAware() {
    Score score = ScoreFactory.create1Staff4Measures();
    score.getHeader().getColumnHeaders().get(1).setTime(new TimeSignature(TimeType.Companion.getTime_3_4()));
    // create options
    VoiceElementWrite.Options options = new VoiceElementWrite.Options();
    options.checkTimeSignature = true;
    // measure 0: senza misura: write 100 1/4 chords
    Voice voice = score.getVoice(MP.atVoice(0, 0, 0));
    for (int i : range(100)) new VoiceElementWrite(voice, atElement(0, 0, 0, i), new Rest(Companion.fr(1, 4)), options).execute();
    // measure 2: must work for 3/4, then fail
    for (int i : range(3)) new VoiceElementWrite(voice, atElement(0, 2, 0, i), new Rest(Companion.fr(1, 4)), options).execute();
    try {
        new VoiceElementWrite(voice, atElement(0, 2, 0, 3), new Rest(Companion.fr(1, 4)), options).execute();
        fail();
    } catch (MeasureFullException ex) {
    }
}
Also used : Score(com.xenoage.zong.core.Score) Rest(com.xenoage.zong.core.music.rest.Rest) MeasureFullException(com.xenoage.zong.utils.exceptions.MeasureFullException) Voice(com.xenoage.zong.core.music.Voice) TimeSignature(com.xenoage.zong.core.music.time.TimeSignature) Test(org.junit.Test)

Example 2 with MeasureFullException

use of com.xenoage.zong.utils.exceptions.MeasureFullException in project Zong by Xenoage.

the class Context method writeVoiceElement.

/**
 * Writes the given {@link VoiceElement} to the current position
 * without moving the cursor forward. When the element could be written, true is returned,
 * otherwise (e.g. when the measure was full), false is returned.
 */
public boolean writeVoiceElement(VoiceElement element, int staffIndexInPart, int voice) {
    MP mp = this.mp.withStaff(getPartStaffIndices().getStart() + staffIndexInPart).withVoice(voice);
    try {
        // create voice if needed
        Measure measure = score.getMeasure(mp);
        if (measure.getVoices().size() < voice + 1)
            execute(new VoiceAdd(measure, voice));
        execute(new VoiceElementWrite(score.getVoice(mp), mp, element, writeVoicElementOptions));
        return true;
    } catch (MeasureFullException ex) {
        reportError(ex.getMessage());
        return false;
    }
}
Also used : VoiceElementWrite(com.xenoage.zong.commands.core.music.VoiceElementWrite) MP(com.xenoage.zong.core.position.MP) VoiceAdd(com.xenoage.zong.commands.core.music.VoiceAdd) MeasureFullException(com.xenoage.zong.utils.exceptions.MeasureFullException)

Aggregations

MeasureFullException (com.xenoage.zong.utils.exceptions.MeasureFullException)2 VoiceAdd (com.xenoage.zong.commands.core.music.VoiceAdd)1 VoiceElementWrite (com.xenoage.zong.commands.core.music.VoiceElementWrite)1 Score (com.xenoage.zong.core.Score)1 Voice (com.xenoage.zong.core.music.Voice)1 Rest (com.xenoage.zong.core.music.rest.Rest)1 TimeSignature (com.xenoage.zong.core.music.time.TimeSignature)1 MP (com.xenoage.zong.core.position.MP)1 Test (org.junit.Test)1