Search in sources :

Example 11 with BeatOffset

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

the class AlignedVoicesSpacer method computeSharedBeats.

/**
 * Returns the shared beats of the given {@link ElementSpacing}s and {@link BeatOffset}s.
 * If there are no beats used by both lists, an empty list is returned.
 */
public List<BeatOffset> computeSharedBeats(List<ElementSpacing> spacingElements, List<BeatOffset> beatOffsets) {
    ArrayList<BeatOffset> ret = alist(Math.min(spacingElements.size(), beatOffsets.size()));
    int iElement = 0, iBeat = 0;
    Fraction lastAddedBeat = Companion.fr(-1);
    while (iElement < spacingElements.size() && iBeat < beatOffsets.size()) {
        Fraction elementBeat = spacingElements.get(iElement).beat;
        BeatOffset beatOffset = beatOffsets.get(iBeat);
        if (elementBeat.equals(beatOffset.beat)) {
            if (beatOffset.beat.equals(lastAddedBeat)) {
                // when this beat was already added, replace it. the
                // rightmost offset is the offset we need.
                ret.set(ret.size() - 1, beatOffset);
            } else {
                ret.add(beatOffset);
            }
            lastAddedBeat = beatOffset.beat;
            iElement++;
        } else if (elementBeat.compareTo(beatOffset.beat) > 0) {
            iBeat++;
        } else {
            iElement++;
        }
    }
    return ret;
}
Also used : BeatOffset(com.xenoage.zong.musiclayout.spacing.BeatOffset) Fraction(com.xenoage.utils.math.Fraction)

Example 12 with BeatOffset

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

the class BarlinesBeatOffsetterTest method computeBeatOffsetsTest.

@Test
public void computeBeatOffsetsTest() {
    // notes:      |    1/4    1/4   ||   1/4    |   1/4    |
    // barlines:   |:               :||:         |         :|
    // original distance between notes
    float d = 2;
    // interline space
    float is = 1.5f;
    // create original offsets
    List<BeatOffset> baseOffsets = alist(new BeatOffset(Companion.fr(0, 4), 0 * d), new BeatOffset(Companion.fr(1, 4), 1 * d), new BeatOffset(Companion.fr(2, 4), 2 * d), new BeatOffset(Companion.fr(3, 4), 3 * d), new BeatOffset(Companion.fr(4, 4), 4 * d));
    // create barlines
    ColumnHeader ch = new ColumnHeader(null, 0);
    ch.setStartBarline(Companion.barlineForwardRepeat(HeavyLight));
    ch.setMiddleBarline(Companion.barlineMiddleBothRepeat(LightLight, 1), Companion.fr(2, 4));
    ch.setMiddleBarline(Companion.barlineRegular(), Companion.fr(3, 4));
    ch.setEndBarline(Companion.barlineBackwardRepeat(LightHeavy, 1));
    // compute new offsets and check results
    BarlinesBeatOffsetter.Result result = testee.compute(baseOffsets, ch, is);
    float dRep = BarlinesBeatOffsetter.repeatSpace * is;
    float dMid = BarlinesBeatOffsetter.midBarlineSpace * is;
    // note offsets
    List<BeatOffset> vo = result.voiceElementOffsets;
    assertEquals(5, vo.size());
    assertEquals(new BeatOffset(Companion.fr(0, 4), 0 * d + 1 * dRep), vo.get(0));
    assertEquals(new BeatOffset(Companion.fr(1, 4), 1 * d + 1 * dRep), vo.get(1));
    assertEquals(new BeatOffset(Companion.fr(2, 4), 2 * d + 3 * dRep + 1 * dMid), vo.get(2));
    assertEquals(new BeatOffset(Companion.fr(3, 4), 3 * d + 3 * dRep + 2 * dMid), vo.get(3));
    assertEquals(new BeatOffset(Companion.fr(4, 4), 4 * d + 3 * dRep + 2 * dMid), vo.get(4));
    // barline offsets
    List<BeatOffset> bo = result.barlineOffsets;
    assertEquals(4, bo.size());
    assertEquals(new BeatOffset(Companion.fr(0, 4), 0 * d + 0 * dRep), bo.get(0));
    assertEquals(new BeatOffset(Companion.fr(2, 4), 2 * d + 2 * dRep), bo.get(1));
    assertEquals(new BeatOffset(Companion.fr(3, 4), 3 * d + 3 * dRep + 1 * dMid), bo.get(2));
    assertEquals(new BeatOffset(Companion.fr(4, 4), 4 * d + 4 * dRep + 2 * dMid), bo.get(3));
}
Also used : ColumnHeader(com.xenoage.zong.core.header.ColumnHeader) BeatOffset(com.xenoage.zong.musiclayout.spacing.BeatOffset) Test(org.junit.Test)

Example 13 with BeatOffset

use of com.xenoage.zong.musiclayout.spacing.BeatOffset 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)

Example 14 with BeatOffset

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

the class AlignedVoicesSpacerTest method computeTest3.

@Test
public void computeTest3() {
    float is = 4;
    // voice spacing:
    // beats:   0.2
    // offsets: | |
    // | ⌎- 2
    // ⌎--- 0
    VoiceSpacing voiceSpacing = new VoiceSpacing(Companion.voice(), is, alist(spacing(beat(0), 0f), spacing(beat(2), 2f)));
    // given beat offsets:
    // beats:   0.2
    // offsets: | |
    // | ⌎- 6
    // ⌎--- 2
    List<BeatOffset> beatOffsets = alist(new BeatOffset(beat(0), 2f), new BeatOffset(beat(2), 6f));
    // shared beats: 0, 2.
    // 
    // resulting spacing:
    // beats:   0.2
    // offsets: | |
    // | ⌎- 6
    // ⌎--- 2
    testee.compute(voiceSpacing, beatOffsets);
    List<ElementSpacing> finalSpacing = voiceSpacing.elements;
    assertEquals(2, finalSpacing.size());
    assertEquals(beat(0), finalSpacing.get(0).beat);
    assertEquals(2f / is, finalSpacing.get(0).xIs, df);
    assertEquals(beat(2), finalSpacing.get(1).beat);
    assertEquals(6f / is, finalSpacing.get(1).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 15 with BeatOffset

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

the class AlignedVoicesSpacerTest method computeTest2.

@Test
public void computeTest2() {
    float is = 3;
    // voice spacing:
    // beats:   0.2
    // offsets: | |
    // | ⌎- 2
    // ⌎--- 0
    VoiceSpacing voiceSpacing = new VoiceSpacing(Companion.voice(), is, alist(spacing(beat(0), 0f), spacing(beat(2), 2f)));
    // given beat offsets:
    // beats:   0.2
    // offsets: | |
    // | ⌎- 2
    // ⌎--- 0
    List<BeatOffset> beatOffsets = alist(new BeatOffset(beat(0), 0f), new BeatOffset(beat(2), 2f));
    // shared beats: 0, 2.
    // 
    // resulting spacing:
    // beats:   0.2
    // offsets: | |
    // | ⌎- 2
    // ⌎--- 0
    testee.compute(voiceSpacing, beatOffsets);
    List<ElementSpacing> finalSpacing = voiceSpacing.elements;
    assertEquals(2, finalSpacing.size());
    assertEquals(beat(0), finalSpacing.get(0).beat);
    assertEquals(0f / is, finalSpacing.get(0).xIs, df);
    assertEquals(beat(2), finalSpacing.get(1).beat);
    assertEquals(2f / is, finalSpacing.get(1).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)

Aggregations

BeatOffset (com.xenoage.zong.musiclayout.spacing.BeatOffset)16 ElementSpacing (com.xenoage.zong.musiclayout.spacing.ElementSpacing)10 VoiceSpacing (com.xenoage.zong.musiclayout.spacing.VoiceSpacing)9 Fraction (com.xenoage.utils.math.Fraction)6 Test (org.junit.Test)6 ColumnSpacing (com.xenoage.zong.musiclayout.spacing.ColumnSpacing)4 MeasureSpacing (com.xenoage.zong.musiclayout.spacing.MeasureSpacing)4 Voice (com.xenoage.zong.core.music.Voice)3 SystemSpacing (com.xenoage.zong.musiclayout.spacing.SystemSpacing)3 ColumnHeader (com.xenoage.zong.core.header.ColumnHeader)2 Chord (com.xenoage.zong.core.music.chord.Chord)2 ChordFactory.graceChord (com.xenoage.zong.core.music.chord.ChordFactory.graceChord)2 ChordNotation (com.xenoage.zong.musiclayout.notation.ChordNotation)2 ChordSpacing (com.xenoage.zong.musiclayout.spacing.ChordSpacing)2 ArrayList (java.util.ArrayList)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 Point2f (com.xenoage.utils.math.geom.Point2f)1 Size2f (com.xenoage.utils.math.geom.Size2f)1 Score (com.xenoage.zong.core.Score)1