Search in sources :

Example 6 with VoiceSpacing

use of com.xenoage.zong.musiclayout.spacing.VoiceSpacing in project Zong by Xenoage.

the class AlignedVoicesSpacerTest method computeTestGrace.

/**
 * Tests the strategy with a voice that uses grace notes.
 * The distance of grace notes to their main notes should not be stretched,
 * but should stay the same.
 */
@Test
public void computeTestGrace() {
    float is = 2;
    // voice spacing:
    // beats:   0.2...gg8
    // offsets: | |   |||
    // | |   ||⌎- 60
    // | |   |⌎-- 59 (grace note)
    // | |   ⌎--- 58 (grace note)
    // | ⌎------- 51
    // ⌎--------- 50
    VoiceSpacing voiceSpacing = new VoiceSpacing(Companion.voice(), is, alist(spacing(beat(0), 0f), spacing(beat(2), 1f), graceSpacing(beat(8), 8f), graceSpacing(beat(8), 9f), spacing(beat(8), 10f)));
    // given beat offsets:
    // beats:   0.......8
    // offsets: |       |
    // |       ⌎- 30
    // ⌎--------- 10
    List<BeatOffset> beatOffsets = alist(new BeatOffset(beat(0), 10f), new BeatOffset(beat(8), 30f));
    // shared beats: 4, 8.
    // 
    // resulting spacing:
    // beats:   0.2...gg8
    // offsets: | |   |||
    // | |   ||⌎- (30 - 10) / (60 - 50) * (60 - 50) + 10 = 30 = x } (shared beats 0 and 8)
    // | |   |⌎-- x - (10 - 9) = 30 - (1 * IS) = 28 (because it is a grace note in front of x)
    // | |   ⌎--- x - (10 - 8) = 30 - (2 * IS) = 26 (because it is a grace note in front of x)
    // | ⌎------- (30 - 10) / (60 - 50) * (51 - 50) + 10 = 12 } (shared beats 0 and 8)
    // ⌎--------- (30 - 10) / (60 - 50) * (50 - 50) + 10 = 10 } (shared beats 0 and 8)
    testee.compute(voiceSpacing, beatOffsets);
    List<ElementSpacing> finalSpacing = voiceSpacing.elements;
    assertEquals(5, finalSpacing.size());
    assertEquals(beat(0), finalSpacing.get(0).beat);
    assertEquals(10f / is, finalSpacing.get(0).xIs, df);
    assertEquals(beat(2), finalSpacing.get(1).beat);
    assertEquals(12f / is, finalSpacing.get(1).xIs, df);
    assertEquals(beat(8), finalSpacing.get(2).beat);
    assertEquals(26f / is, finalSpacing.get(2).xIs, df);
    assertEquals(beat(8), finalSpacing.get(3).beat);
    assertEquals(28f / is, finalSpacing.get(3).xIs, df);
    assertEquals(beat(8), finalSpacing.get(4).beat);
    assertEquals(30f / is, finalSpacing.get(4).xIs, df);
}
Also used : ElementSpacing(com.xenoage.zong.musiclayout.spacing.ElementSpacing) BeatOffset(com.xenoage.zong.musiclayout.spacing.BeatOffset) VoiceSpacing(com.xenoage.zong.musiclayout.spacing.VoiceSpacing) Test(org.junit.Test)

Example 7 with VoiceSpacing

use of com.xenoage.zong.musiclayout.spacing.VoiceSpacing in project Zong by Xenoage.

the class SingleVoiceSpacerTest method testGrace2.

/**
 * Computes a voice spacing with grace notes,
 * where the element before the grace notes has enough empty rear space
 * to take at least one of the grace notes.
 * <pre>
 * Single elements: [-r1------][g1][g2][--r4--]
 * Combined:        --r1_~g1~g2~~~r4---
 * </pre> (~: area used by two elements, _: minimal distance between elements)
 */
@Test
public void testGrace2() {
    // create voice and notations
    Voice voice = new Voice(alist((VoiceElement) r1, g1, g2, r4));
    Notations notations = new Notations();
    notations.add(new RestNotation(r1, new ElementWidth(2, 2, 7), null));
    notations.add(new ChordNotation(g1, new ElementWidth(1, 2, 1)));
    notations.add(new ChordNotation(g2, new ElementWidth(1, 2, 1)));
    notations.add(new RestNotation(r4, new ElementWidth(3, 2, 3), null));
    // compute spacing
    VoiceSpacing vs = testee.compute(voice, 400f, Companion.fr(4, 4), 5, notations, layoutSettings);
    // check spacing
    ElementSpacing[] ses = vs.elements.toArray(new ElementSpacing[0]);
    ;
    float s = layoutSettings.offsetMeasureStart;
    float d = layoutSettings.spacings.widthDistanceMin;
    assertEquals(5, ses.length);
    assertEquals(s + 2, ses[0].xIs, DELTA_FLOAT);
    assertEquals(s + 5 + d, ses[1].xIs, DELTA_FLOAT);
    assertEquals(s + 8 + d, ses[2].xIs, DELTA_FLOAT);
    assertEquals(s + 13 + d, ses[3].xIs, DELTA_FLOAT);
    assertEquals(s + 18 + d, ses[4].xIs, DELTA_FLOAT);
    // check beats
    assertEquals(Companion.fr(0, 8), ses[0].beat);
    assertEquals(Companion.fr(2, 8), ses[1].beat);
    assertEquals(Companion.fr(2, 8), ses[2].beat);
    assertEquals(Companion.fr(2, 8), ses[3].beat);
    assertEquals(Companion.fr(6, 8), ses[4].beat);
}
Also used : ElementSpacing(com.xenoage.zong.musiclayout.spacing.ElementSpacing) ElementWidth(com.xenoage.zong.musiclayout.spacing.ElementWidth) ChordNotation(com.xenoage.zong.musiclayout.notation.ChordNotation) VoiceElement(com.xenoage.zong.core.music.VoiceElement) RestNotation(com.xenoage.zong.musiclayout.notation.RestNotation) Notations(com.xenoage.zong.musiclayout.notation.Notations) VoiceSpacing(com.xenoage.zong.musiclayout.spacing.VoiceSpacing) Voice(com.xenoage.zong.core.music.Voice) Test(org.junit.Test) VoiceTest(com.xenoage.zong.core.music.VoiceTest) LayoutSettingsTest(com.xenoage.zong.musiclayout.settings.LayoutSettingsTest)

Example 8 with VoiceSpacing

use of com.xenoage.zong.musiclayout.spacing.VoiceSpacing in project Zong by Xenoage.

the class SingleVoiceSpacerTest method testSimple.

/**
 * Computes a simple voice spacing.
 * <pre>
 * Single elements: [-r1---][-r2-][-r3--][----r4--]
 * Combined:        --r1--~~r2_~-r3_~~---r4---
 * </pre> (~: area used by two elements, _: minimal distance between elements)
 */
@Test
public void testSimple() {
    // create voice and notations
    Voice voice = new Voice(alist((VoiceElement) r1, r2, r3, r4));
    Notations notations = new Notations();
    notations.add(new RestNotation(r1, new ElementWidth(2, 2, 4), null));
    notations.add(new RestNotation(r2, new ElementWidth(2, 2, 2), null));
    notations.add(new RestNotation(r3, new ElementWidth(2, 2, 3), null));
    notations.add(new RestNotation(r4, new ElementWidth(5, 2, 3), null));
    // compute spacing
    VoiceSpacing vs = testee.compute(voice, 200f, Companion.fr(4, 4), 5, notations, layoutSettings);
    // check spacing
    ElementSpacing[] ses = vs.elements.toArray(new ElementSpacing[0]);
    float s = layoutSettings.offsetMeasureStart;
    float d = layoutSettings.spacings.widthDistanceMin;
    assertEquals(5, ses.length);
    assertEquals(s + 2, ses[0].xIs, DELTA_FLOAT);
    assertEquals(s + 8, ses[1].xIs, DELTA_FLOAT);
    assertEquals(s + 12 + d, ses[2].xIs, DELTA_FLOAT);
    assertEquals(s + 19 + 2 * d, ses[3].xIs, DELTA_FLOAT);
    assertEquals(s + 24 + 2 * d, ses[4].xIs, DELTA_FLOAT);
    // check beats
    assertEquals(Companion.fr(0, 8), ses[0].beat);
    assertEquals(Companion.fr(2, 8), ses[1].beat);
    assertEquals(Companion.fr(3, 8), ses[2].beat);
    assertEquals(Companion.fr(4, 8), ses[3].beat);
    assertEquals(Companion.fr(8, 8), ses[4].beat);
}
Also used : ElementSpacing(com.xenoage.zong.musiclayout.spacing.ElementSpacing) ElementWidth(com.xenoage.zong.musiclayout.spacing.ElementWidth) VoiceElement(com.xenoage.zong.core.music.VoiceElement) RestNotation(com.xenoage.zong.musiclayout.notation.RestNotation) Notations(com.xenoage.zong.musiclayout.notation.Notations) VoiceSpacing(com.xenoage.zong.musiclayout.spacing.VoiceSpacing) Voice(com.xenoage.zong.core.music.Voice) Test(org.junit.Test) VoiceTest(com.xenoage.zong.core.music.VoiceTest) LayoutSettingsTest(com.xenoage.zong.musiclayout.settings.LayoutSettingsTest)

Example 9 with VoiceSpacing

use of com.xenoage.zong.musiclayout.spacing.VoiceSpacing in project Zong by Xenoage.

the class VoicesBeatOffsetter method compute.

/**
 * Computes the offsets of all the used beats, including
 * at least beat 0 and the beat at the end of the measure.
 * The beats containing the notes with the lowest valuation
 * (or that needs accidentals) dictate the spacing.
 * See "Ross: The Art of Music Engraving", page 79.
 */
public List<BeatOffset> compute(List<VoiceSpacing> voiceSpacings, Fraction measureBeats, float minimalBeatsOffsetIs) {
    // the list of all used beats of the measure
    // TODO: handle upbeat measures correctly!
    SortedList<Fraction> beats = computeVoicesBeats(voiceSpacings);
    // add final used beat
    beats.add(computeLastBeat(voiceSpacings));
    // add final beat in terms of time signature (only correct for non-upbeat measures)
    beats.add(measureBeats);
    // the resulting offsets for each used beat
    ArrayList<BeatOffset> ret = alist();
    // compute the offset of beat 0
    float offsetMm = getOffsetBeat0InMm(voiceSpacings);
    Fraction lastBeat = Companion.get_0();
    ret.add(new BeatOffset(lastBeat, offsetMm));
    // Otherwise we must find the dominant parts within the voices
    if (voiceSpacings.size() == 1) {
        // only one voice
        float interlineSpace = voiceSpacings.get(0).interlineSpace;
        for (ElementSpacing se : voiceSpacings.get(0).elements) {
            // if last beat offset has same beat, overwrite it
            if (ret.get(ret.size() - 1).getBeat().equals(se.beat))
                ret.remove(ret.size() - 1);
            ret.add(new BeatOffset(se.beat, se.xIs * interlineSpace));
        }
    } else {
        // more than one voice
        // use the following algorithm:
        // for each beat, compute the offset, by asking each voice how much space
        // it requires between the last computed beat offset and the current one.
        // each time, take the greatest distance required.
        Iterator<Fraction> beatsIterator = beats.iterator();
        // ignore beat 0, we have handled it before
        beatsIterator.next();
        while (beatsIterator.hasNext()) {
            Fraction beat = beatsIterator.next();
            // find dominating voice and its minimal required distance
            float minimalDistance = 0;
            for (VoiceSpacing voiceSpacing : voiceSpacings) {
                float interlineSpace = voiceSpacing.interlineSpace;
                float voiceMinimalDistance = computeMinimalDistance(lastBeat, beat, beat.equals(measureBeats), voiceSpacing.voice, voiceSpacing.elements, ret, interlineSpace);
                minimalDistance = Math.max(minimalDistance, voiceMinimalDistance);
                // but we do not want to have different beats at the same offset, so add a small distance.
                if (minimalDistance < minimalBeatsOffsetIs * interlineSpace) {
                    minimalDistance = minimalBeatsOffsetIs * interlineSpace;
                }
            }
            // add beat
            offsetMm += minimalDistance;
            ret.add(new BeatOffset(beat, offsetMm));
            lastBeat = beat;
        }
    }
    ret.trimToSize();
    return ret;
}
Also used : ElementSpacing(com.xenoage.zong.musiclayout.spacing.ElementSpacing) BeatOffset(com.xenoage.zong.musiclayout.spacing.BeatOffset) Fraction(com.xenoage.utils.math.Fraction) VoiceSpacing(com.xenoage.zong.musiclayout.spacing.VoiceSpacing)

Example 10 with VoiceSpacing

use of com.xenoage.zong.musiclayout.spacing.VoiceSpacing in project Zong by Xenoage.

the class VoicesBeatOffsetter method computeVoicesBeats.

/**
 * Returns a sorted list of all beats, where
 * chords or rests begin, from the given list of voice spacings.
 * There are no duplicate beats. The ending beats of the voices are not added.
 */
SortedList<Fraction> computeVoicesBeats(List<VoiceSpacing> voiceSpacings) {
    SortedList<Fraction> beats = Companion.sortedListNoDuplicates();
    Fraction beat;
    for (VoiceSpacing voiceSpacing : voiceSpacings) {
        beat = Fraction.Companion.get_0();
        for (ElementSpacing spacingElement : voiceSpacing.elements) {
            MusicElement element = spacingElement.getElement();
            if (element instanceof VoiceElement) {
                // add beat
                beats.add(beat);
                // find the next beat
                beat = beat.add(((VoiceElement) element).getDuration());
            }
        }
    // do not add beat here, because the ending beat of an incomplete measure
    // is not interesting for computing beat offsets.
    }
    return beats;
}
Also used : ElementSpacing(com.xenoage.zong.musiclayout.spacing.ElementSpacing) MusicElement(com.xenoage.zong.core.music.MusicElement) VoiceElement(com.xenoage.zong.core.music.VoiceElement) Fraction(com.xenoage.utils.math.Fraction) VoiceSpacing(com.xenoage.zong.musiclayout.spacing.VoiceSpacing)

Aggregations

VoiceSpacing (com.xenoage.zong.musiclayout.spacing.VoiceSpacing)22 ElementSpacing (com.xenoage.zong.musiclayout.spacing.ElementSpacing)17 Test (org.junit.Test)12 BeatOffset (com.xenoage.zong.musiclayout.spacing.BeatOffset)9 Voice (com.xenoage.zong.core.music.Voice)7 VoiceElement (com.xenoage.zong.core.music.VoiceElement)6 ChordNotation (com.xenoage.zong.musiclayout.notation.ChordNotation)6 ColumnSpacing (com.xenoage.zong.musiclayout.spacing.ColumnSpacing)6 RestNotation (com.xenoage.zong.musiclayout.notation.RestNotation)5 ElementWidth (com.xenoage.zong.musiclayout.spacing.ElementWidth)5 Fraction (com.xenoage.utils.math.Fraction)4 VoiceTest (com.xenoage.zong.core.music.VoiceTest)4 Notations (com.xenoage.zong.musiclayout.notation.Notations)4 LayoutSettingsTest (com.xenoage.zong.musiclayout.settings.LayoutSettingsTest)4 MeasureSpacing (com.xenoage.zong.musiclayout.spacing.MeasureSpacing)4 SystemSpacing (com.xenoage.zong.musiclayout.spacing.SystemSpacing)4 ChordSpacing (com.xenoage.zong.musiclayout.spacing.ChordSpacing)3 Chord (com.xenoage.zong.core.music.chord.Chord)2 ChordFactory.graceChord (com.xenoage.zong.core.music.chord.ChordFactory.graceChord)2 Clef (com.xenoage.zong.core.music.clef.Clef)2