Search in sources :

Example 1 with MusicContext

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

the class ChordNotator method compute.

public ChordNotation compute(Chord chord, Context context, @MaybeNull Notations notations) {
    Score score = context.score;
    float interlineSpace = score.getInterlineSpace(context.mp);
    FontInfo lyricsFont = score.getFormat().getLyricFont();
    MusicContext mc = score.getMusicContext(context.mp, BeforeOrAt, Before);
    // grace or normal chord?
    boolean grace = chord.isGrace();
    ChordWidths chordWidths = (grace ? context.settings.graceChordWidths : context.settings.chordWidths);
    ChordSpacings spacings = (grace ? context.settings.spacings.graceChordSpacings : context.settings.spacings.normalChordSpacings);
    // use or compute stem direction
    StemDirection stemDirection = chord.getStem().getDirection();
    if (stemDirection == StemDirection.Default) {
        // if stem direction was not computed yet, compute it now
        if (notations != null)
            stemDirection = notations.getChord(chord).stemDirection;
        if (stemDirection == StemDirection.Default) {
            Map<Chord, StemDirection> computedStems = stemDirector.compute(chord);
            stemDirection = computedStems.get(chord);
            // also remember the other computed stems
            if (notations != null)
                for (Chord computedChord : computedStems.keySet()) notations.getChord(computedChord).stemDirection = computedStems.get(computedChord);
        }
    }
    // notes displacement
    NotesNotation notes = notesNotator.compute(chord, stemDirection, chordWidths, mc);
    float leftSuspendedWidth = (notes.leftSuspended ? notes.noteheadWidthIs : 0);
    // accidentals
    AccidentalsNotation accs = accidentalsNotator.compute(chord, notes, chordWidths, mc);
    // symbol's width: width of the noteheads and dots
    float symbolWidth = notes.widthIs - leftSuspendedWidth;
    float frontGap = accs.widthIs + leftSuspendedWidth;
    // rear gap: empty duration-dependent space behind the chord minus the symbol's width
    float rearGap = spacings.getWidth(chord.getDisplayedDuration()) - symbolWidth;
    // lyric width
    float lyricWidth = 0;
    TextMeasurer textMeasurer = platformUtils().getTextMeasurer();
    for (Lyric lyric : chord.getLyrics()) {
        if (lyric != null && lyric.getText() != null) {
            // width of lyric in interline spaces
            FormattedText lyricText = styleText(lyric.getText(), new FormattedTextStyle(lyricsFont));
            float l = lyricText.getWidth() / interlineSpace;
            // for start and end syllable, request "-" more space, for middle syllables "--"
            // TODO: unsymmetric - start needs space on the right, end on the left, ...
            SyllableType lyricType = lyric.getSyllableType();
            if (lyricType == SyllableType.Begin || lyricType == SyllableType.End) {
                l += textMeasurer.measure(lyricsFont, "-").getWidth() / interlineSpace;
            } else if (lyricType == SyllableType.Middle) {
                l += textMeasurer.measure(lyricsFont, "--").getWidth() / interlineSpace;
            }
            // save width of the widest lyric
            lyricWidth = Math.max(lyricWidth, l);
        }
    }
    // compute length of the stem (if any)
    float scaling = grace ? context.settings.scalingGrace : 1;
    StemNotation stem = stemNotator.compute(chord.getStem(), notes.getLps(), stemDirection, context.mp.getStaff(), Companion.staffLines(mc.getLinesCount()), scaling);
    // compute articulations
    ArticulationsNotation arts = articulationsNotator.compute(chord, stemDirection, notes, mc.getLinesCount());
    return new ChordNotation(chord, chord.getMP(), new ElementWidth(frontGap, symbolWidth, rearGap, lyricWidth), context.mp.getStaff(), notes, stemDirection, stem, accs, arts);
}
Also used : ElementWidth(com.xenoage.zong.musiclayout.spacing.ElementWidth) ChordNotation(com.xenoage.zong.musiclayout.notation.ChordNotation) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) FormattedText(com.xenoage.zong.core.text.FormattedText) MusicContext(com.xenoage.zong.core.music.MusicContext) StemNotation(com.xenoage.zong.musiclayout.notation.chord.StemNotation) Score(com.xenoage.zong.core.Score) ArticulationsNotation(com.xenoage.zong.musiclayout.notation.chord.ArticulationsNotation) TextMeasurer(com.xenoage.utils.font.TextMeasurer) ChordWidths(com.xenoage.zong.musiclayout.settings.ChordWidths) AccidentalsNotation(com.xenoage.zong.musiclayout.notation.chord.AccidentalsNotation) Lyric(com.xenoage.zong.core.music.lyric.Lyric) ChordSpacings(com.xenoage.zong.musiclayout.settings.ChordSpacings) SyllableType(com.xenoage.zong.core.music.lyric.SyllableType) NotesNotation(com.xenoage.zong.musiclayout.notation.chord.NotesNotation) FontInfo(com.xenoage.utils.font.FontInfo) StemDirection(com.xenoage.zong.core.music.chord.StemDirection) Chord(com.xenoage.zong.core.music.chord.Chord)

Example 2 with MusicContext

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

the class StemReader method getOuterNoteLp.

/**
 * Gets the line position of the note at the bottom or top side of the given chord.
 */
private static float getOuterNoteLp(Context context, Chord chord, VSide side, int staff) {
    MusicContext mc = context.getMusicContext(staff);
    List<Pitch> pitches = chord.getPitches();
    return mc.getLp(side == VSide.Top ? getLast(pitches) : getFirst(pitches));
}
Also used : Pitch(com.xenoage.zong.core.music.Pitch) MusicContext(com.xenoage.zong.core.music.MusicContext)

Example 3 with MusicContext

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

the class Test12b method test.

@Test
public void test() {
    Score score = getScore();
    // musical context must be 4/4, C clef and no accidentals
    MusicContext context = score.getMusicContext(mp0, Interval.At, Interval.At);
    assertEquals(Companion.fr(4, 4), score.getMeasureBeats(0));
    assertEquals(ClefType.Companion.getClefTreble(), context.getClef());
    for (int i = 0; i < 7; i++) assertEquals(0, context.getKey().getAlterations()[i]);
    // there should be a C clef in the first measure
    assertEquals(ClefType.Companion.getClefTreble(), score.getMeasure(mp0).getClefs().get(Companion.get_0()).getType());
    // there should be a time signature and key signature in the measure column
    ColumnHeader header = score.getHeader().getColumnHeader(0);
    assertEquals(TimeType.Companion.getTime_4_4(), header.getTime().getType());
    assertNotNull(header.getKeys().get(Companion.get_0()));
}
Also used : Score(com.xenoage.zong.core.Score) ColumnHeader(com.xenoage.zong.core.header.ColumnHeader) MusicContext(com.xenoage.zong.core.music.MusicContext) Test(org.junit.Test)

Example 4 with MusicContext

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

the class LeadingSpacer method compute.

/**
 * Computes the {@link LeadingSpacing} for the current measure.
 */
public LeadingSpacing compute(Context context, Notations notations) {
    float xOffset = context.settings.offsetMeasureStart;
    boolean useKey = false;
    MusicContext musicContext = context.getMusicContext(At, null);
    Key key = musicContext.getKey();
    if (key instanceof TraditionalKey) {
        useKey = true;
    }
    List<ElementSpacing> elements = alist(useKey ? 2 : 1);
    // it is not the same element instance, but has the same meaning
    Clef clef = new Clef(musicContext.getClef());
    ClefNotation clefNotation = new ClefNotation(clef, new ElementWidth(0, context.settings.spacings.widthClef, 0), musicContext.getClef().getLp(), 1);
    notations.add(clefNotation);
    xOffset += context.settings.spacings.widthClef / 2;
    elements.add(new SimpleSpacing(clefNotation, Companion.fr(0), xOffset));
    xOffset += context.settings.spacings.widthClef / 2;
    if (useKey) {
        TraditionalKey tkey = (TraditionalKey) key;
        xOffset += context.settings.spacings.widthDistanceMin;
        // it is not the same element instance, but has the same meaning
        TraditionalKey tradKey = new TraditionalKey(tkey.getFifths(), tkey.getMode());
        TraditionalKeyNotation keyNotation = traditionalKeyNotator.compute(tradKey, context);
        notations.add(keyNotation);
        elements.add(new SimpleSpacing(keyNotation, Companion.fr(0), xOffset));
        xOffset += keyNotation.getWidth().getWidth();
    }
    return new LeadingSpacing(elements, xOffset);
}
Also used : ElementWidth(com.xenoage.zong.musiclayout.spacing.ElementWidth) LeadingSpacing(com.xenoage.zong.musiclayout.spacing.LeadingSpacing) SimpleSpacing(com.xenoage.zong.musiclayout.spacing.SimpleSpacing) Clef(com.xenoage.zong.core.music.clef.Clef) ClefNotation(com.xenoage.zong.musiclayout.notation.ClefNotation) TraditionalKey(com.xenoage.zong.core.music.key.TraditionalKey) MusicContext(com.xenoage.zong.core.music.MusicContext) ElementSpacing(com.xenoage.zong.musiclayout.spacing.ElementSpacing) TraditionalKeyNotation(com.xenoage.zong.musiclayout.notation.TraditionalKeyNotation) TraditionalKey(com.xenoage.zong.core.music.key.TraditionalKey) Key(com.xenoage.zong.core.music.key.Key)

Example 5 with MusicContext

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

the class OneMeasureOneStaff method compute.

@Override
public StemDirection[] compute(Beam beam, Score score) {
    int staffLinesCount = getStaffLinesCount(beam.getChord(0), score);
    ChordLps[] chordsLps = new ChordLps[beam.size()];
    for (int iChord : range(chordsLps)) {
        Chord chord = beam.getChord(iChord);
        MP mp = MP.getMP(chord);
        MusicContext mc = score.getMusicContext(mp, BeforeOrAt, Before);
        chordsLps[iChord] = new ChordLps(chord, mc);
    }
    return compute(chordsLps, staffLinesCount);
}
Also used : MP(com.xenoage.zong.core.position.MP) ChordLps(com.xenoage.zong.musiclayout.notation.chord.ChordLps) Chord(com.xenoage.zong.core.music.chord.Chord) MusicContext(com.xenoage.zong.core.music.MusicContext)

Aggregations

MusicContext (com.xenoage.zong.core.music.MusicContext)6 Chord (com.xenoage.zong.core.music.chord.Chord)3 Score (com.xenoage.zong.core.Score)2 StemDirection (com.xenoage.zong.core.music.chord.StemDirection)2 ChordLps (com.xenoage.zong.musiclayout.notation.chord.ChordLps)2 NotesNotation (com.xenoage.zong.musiclayout.notation.chord.NotesNotation)2 StemNotation (com.xenoage.zong.musiclayout.notation.chord.StemNotation)2 ElementWidth (com.xenoage.zong.musiclayout.spacing.ElementWidth)2 FontInfo (com.xenoage.utils.font.FontInfo)1 TextMeasurer (com.xenoage.utils.font.TextMeasurer)1 Fraction (com.xenoage.utils.math.Fraction)1 ColumnHeader (com.xenoage.zong.core.header.ColumnHeader)1 Pitch (com.xenoage.zong.core.music.Pitch)1 Clef (com.xenoage.zong.core.music.clef.Clef)1 Key (com.xenoage.zong.core.music.key.Key)1 TraditionalKey (com.xenoage.zong.core.music.key.TraditionalKey)1 Lyric (com.xenoage.zong.core.music.lyric.Lyric)1 SyllableType (com.xenoage.zong.core.music.lyric.SyllableType)1 MP (com.xenoage.zong.core.position.MP)1 FormattedText (com.xenoage.zong.core.text.FormattedText)1