Abjad / abjad

Abjad is a Python API for building LilyPond files. Use Abjad to make PDFs of music notation.
https://abjad.github.io
GNU General Public License v3.0
234 stars 41 forks source link

NEW. Added abjad.VoiceNumber(n=None) indicator. #1528

Closed trevorbaca closed 1 year ago

trevorbaca commented 1 year ago

Set n to 1, 2, 3, 4 or none. Models LilyPond \voiceOne, \voiceTwo, \voiceThree, \voiceFour, \oneVoice commands.

def make_staff():
    staff = abjad.Staff()
    voice_1 = abjad.Voice("g'8 a' b' c''")
    command = abjad.VoiceNumber(n=1)
    abjad.attach(command, voice_1[0])
    voice_2 = abjad.Voice("e'8 f' g' a'")
    command = abjad.VoiceNumber(n=2)
    abjad.attach(command, voice_2[0])
    container = abjad.Container([voice_1, voice_2], simultaneous=True)
    staff.append(container)
    voice = abjad.Voice("c''4 a'")
    command = abjad.VoiceNumber()
    abjad.attach(command, voice[0])
    staff.append(voice)

>>> staff = make_staff()
>>> string = abjad.lilypond(staff)
>>> print(string)
\new Staff
{
    <<
        \new Voice
        {
            \voiceOne
            g'8
            a'8
            b'8
            c''8
        }
        \new Voice
        {
            \voiceTwo
            e'8
            f'8
            g'8
            a'8
        }
    >>
    \new Voice
    {
        \oneVoice
        c''4
        a'4
    }
}

The abjad.VoiceNumber indicator is contexted at the level of abjad.Voice. Use abjad.get.effective() to get the abjad.VoiceNumber indicator in effect for any leaf:

>>> for leaf in abjad.select.leaves(staff):
...     command = abjad.get.effective(leaf, abjad.VoiceNumber)
...     print(f"{leaf}, {command}")
Note("g'8"), VoiceNumber(n=1)
Note("a'8"), VoiceNumber(n=1)
Note("b'8"), VoiceNumber(n=1)
Note("c''8"), VoiceNumber(n=1)
Note("e'8"), VoiceNumber(n=2)
Note("f'8"), VoiceNumber(n=2)
Note("g'8"), VoiceNumber(n=2)
Note("a'8"), VoiceNumber(n=2)
Note("c''4"), VoiceNumber(n=None)
Note("a'4"), VoiceNumber(n=None)

Closes #1135.