ly.music package

Module contents

An api to read music from the tokens of a ly.document.Document into a tree structure.

This is meant to quickly read music from a document, to perform modifications on the document, and to interpret music and markup and to convert or export it to other formats.

All nodes are a subclass of items.Item, (which inherits from node.WeakNode).

Tree structures are created from nested LilyPond structures, markup and scheme code. Some Item types have special methods to query information. The Music type, for example, has a length() method that returns the duration of the music fragment.

Using the Music.events() method and the events module, it is possible to iterate in musical time over the music tree, e.g. to convert music to another format.

This package is not yet capable to construct documents entirely from scratch. This needs to be developed. Until that time, the ly.dom module can be used instead.

Some Item types can have a list of child items, but the tree structure is as linear as possible.

A convenience function is available to create a ly.music.items.Document instance for the specified ly.document.Document.

Here is an example:

>>> import ly.document
>>> import ly.music
>>> d=ly.document.Document(r'''
\version "2.18.0"

music = \relative {
  \time 4/4
  \key d \minor
  d4 e f g
  a g f e
  d2
}

\score {
  \new Staff <<
    \music
  >>
}
''')
>>> m=ly.music.document(d)
>>> print m.dump()
<Document>
  <Version u'\\version'>
    <String u'"'>
  <Assignment u'music'>
    <Relative u'\\relative'>
      <MusicList u'{'>
        <TimeSignature u'\\time'>
        <KeySignature u'\\key'>
          <Note u'd'>
          <Command u'\\minor'>
        <Note u'd'>
        <Note u'e'>
        <Note u'f'>
        <Note u'g'>
        <Note u'a'>
        <Note u'g'>
        <Note u'f'>
        <Note u'e'>
        <Note u'd'>
  <Score u'\\score'>
    <Context u'\\new'>
      <MusicList u'<<'>
        <UserCommand u'\\music'>
>>> m[2][0][0]
<MusicList u'<<'>
>>> m[2][0][0].length()
Fraction(5, 2)
>>>
ly.music.document(doc)[source]

Return a music.items.Document instance for the ly.document.Document.

Submodules

ly.music.event module

Translates a music.items.Document tree into lists of events.

class ly.music.event.Events[source]

Bases: object

Traverses a music tree and records music events from it.

read(node, time=0, scaling=1)[source]

Read events from the node and all its child nodes; return time.

traverse(node, time, scaling)[source]

Traverse node and call event handlers; record and return the time.

unfold_repeats = False

ly.music.items module

The items a music expression is constructed with in a tree structure.

Whitespace and comments are left out.

All nodes (instances of Item) have a ‘position’ attribute that indicates where the item starts in the source text. Almost all items have the token that starts the expression in the ‘token’ attribute and possibly other tokens in the ‘tokens’ attribute, as a tuple.

The ‘end_position()’ method returns the position where the node (including its child nodes) ends.

You can get the whole tree structure of a LilyPond document by instantiating a Document with the ly.document.Document instance. (It will read all the tokens from the document using the Reader from the read module.) As a convenience, the ly.music.document(doc) function does this.

If you want to add new Item types, you should also add a method to read.Reader to construct those items.

class ly.music.items.Absolute(parent=None)[source]

Bases: ly.music.items.Music

An absolute music expression. Has one child (normally Music).

class ly.music.items.AfterGrace(parent=None)[source]

Bases: ly.music.items.Music

The afterGrace function with its two arguments.

Only the duration of the first is counted.

class ly.music.items.Alternative(parent=None)[source]

Bases: ly.music.items.Music

An alternative expression.

class ly.music.items.Articulation(parent=None)[source]

Bases: ly.music.items.Item

An articulation, fingering, string number, or other symbol.

class ly.music.items.Assignment(parent=None)[source]

Bases: ly.music.items.Item

A variable = value construct.

name()[source]

The variable name.

value()[source]

The assigned value.

class ly.music.items.Beam(parent=None)[source]

Bases: ly.music.items.Item

A [ or ].

event = None
class ly.music.items.Book(parent=None)[source]

Bases: ly.music.items.Container

A book { … } construct.

class ly.music.items.BookPart(parent=None)[source]

Bases: ly.music.items.Container

A bookpart { … } construct.

class ly.music.items.Change(parent=None)[source]

Bases: ly.music.items.Translator

A change music expression.

class ly.music.items.Chord(parent=None)[source]

Bases: ly.music.items.Durable, ly.music.items.Container

class ly.music.items.ChordItem(parent=None)[source]

Bases: ly.music.items.Item

An item inside a ChordSpecifier, e.g. a number or modifier.

class ly.music.items.ChordMode(parent=None)[source]

Bases: ly.music.items.InputMode

A chordmode or chords expression.

class ly.music.items.ChordSpecifier(parent=None)[source]

Bases: ly.music.items.Item

Chord specifications after a note in chord mode.

Has children of Note or ChordItem class.

class ly.music.items.Clef(parent=None)[source]

Bases: ly.music.items.Item

A clef item.

specifier()[source]
class ly.music.items.Command(parent=None)[source]

Bases: ly.music.items.Item

A LilyPond command.

class ly.music.items.Container(parent=None)[source]

Bases: ly.music.items.Item

An item having a list of child items.

class ly.music.items.Context(parent=None)[source]

Bases: ly.music.items.Translator, ly.music.items.Music

A new or context music expression.

class ly.music.items.Document(doc)[source]

Bases: ly.music.items.Item

A toplevel item representing a ly.document.Document.

get_included_document_node(node)[source]

Return a Document for the Include node. May return None.

get_music(filename)[source]

Return the music Document for the specified filename.

This implementation loads a ly.document.Document using utf-8 encoding. Inherit from this class to implement other loading mechanisms or caching.

iter_music(node=None)[source]

Iter over the music, following references to other assignments.

music_events_til_position(position)[source]

Return a list of tuples.

Every tuple is a (parent, nodes, scaling). If an empty list is returned, there is no music expression at this position.

node(position, depth=- 1)[source]

Return the node at or just before the specified position.

resolve_filename(filename)[source]

Resolve filename against our document and include_path.

substitute_for_node(node)[source]

Returns a node that replaces the specified node (e.g. in music).

For example: a variable reference returns its value. Returns nothing if the node is not substitutable. Returns the node itself if it was substitutable, but the substitution failed.

time_length(start, end)[source]

Return the length of the music between start and end positions.

Returns None if start and end are not in the same expression.

time_position(position)[source]

Return the time position in the music at the specified cursor position.

The value is a fraction. If None is returned, we are not in a music expression.

class ly.music.items.DrumMode(parent=None)[source]

Bases: ly.music.items.InputMode

A drummode or drums expression.

class ly.music.items.DrumNote(parent=None)[source]

Bases: ly.music.items.Durable

class ly.music.items.Durable(parent=None)[source]

Bases: ly.music.items.Item

An Item that has a musical duration, in the duration attribute.

duration = (0, 1)
events(e, time, scaling)[source]

Let the event.Events instance handle the events. Return the time.

length()[source]

Return the musical duration (our base * our scaling).

class ly.music.items.Duration(parent=None)[source]

Bases: ly.music.items.Item

A written duration

class ly.music.items.Dynamic(parent=None)[source]

Bases: ly.music.items.Item

Any dynamic symbol.

class ly.music.items.FigureMode(parent=None)[source]

Bases: ly.music.items.InputMode

A figuremode or figures expression.

class ly.music.items.Grace(parent=None)[source]

Bases: ly.music.items.Music

Music that has grace timing, i.e. 0 as far as computation is concerned.

events(e, time, scaling)[source]

Let the event.Events instance handle the events. Return the time.

preceding(node=None)[source]

Return a two-tuple (nodes, scaling).

The nodes are the nodes in time before the node (which must be a child), and the scaling is 0 for (because we have grace notes).

If node is None, all nodes that would precede a fictive node at the end are returned.

class ly.music.items.Header(parent=None)[source]

Bases: ly.music.items.Container

A header { … } construct.

class ly.music.items.Include(parent=None)[source]

Bases: ly.music.items.Item

An include command (not changing the language).

filename()[source]

Returns the filename.

class ly.music.items.InputMode(parent=None)[source]

Bases: ly.music.items.Music

Base class for inputmode-changing commands.

class ly.music.items.Item(parent=None)[source]

Bases: ly.node.WeakNode

Represents any item in the music of a document.

This can be just a token, or an interpreted construct such as a note, rest or sequential or simultaneous construct , etc.

Some Item instances just have one responsible token, but others have a list or tuple to tokens.

An Item also has a pointer to the Document it originates from.

document = None
end_position()[source]

Return the end position of this node.

events(e, time, scaling)[source]

Let the event.Events instance handle the events. Return the time.

has_output(_seen_docs=None)[source]

Return True if this node has toplevel music, markup, book etc.

I.e. returns True when LilyPond would likely generate output. Usually you’ll call this method on a Document, Score, BookPart or Book node.

You should not supply the _seen_docs argument; it is used internally to avoid traversing recursively nested include files.

iter_toplevel_items()[source]

Yield the toplevel items of our Document node in backward direction.

Iteration starts with the node just before the node “self” is a descendant of.

iter_toplevel_items_include()[source]

Same as iter_toplevel_items(), but follows include commands.

length()[source]

Return the musical duration.

music_children(depth=- 1)[source]

Yield all the children that are new music expressions

(i.e. that are inside other constructions).

music_parent()[source]

Walk up the parent tree until Music is found; return the outermost Music node.

Returns None is the node does not belong to any music expression (e.g. a toplevel Markup or Scheme object).

plaintext()[source]

Return a plaintext value for this node.

This only makes sense for items like Markup or String. For other types, an empty string is returned

position = -1
token = None
tokens = ()
class ly.music.items.KeySignature(parent=None)[source]

Bases: ly.music.items.Item

A key pitch mode command.

mode()[source]

The mode, e.g. “major”, “minor”, etc.

pitch()[source]

The ly.pitch.Pitch that denotes the pitch.

class ly.music.items.Keyword(parent=None)[source]

Bases: ly.music.items.Item

A LilyPond keyword.

class ly.music.items.Language(parent=None)[source]

Bases: ly.music.items.Item

A command (language or certain include commands) that changes the pitch language.

language = None
class ly.music.items.Layout(parent=None)[source]

Bases: ly.music.items.Container

A layout { … } construct.

class ly.music.items.LayoutContext(parent=None)[source]

Bases: ly.music.items.Container

A context { … } construct within Layout or Midi.

class ly.music.items.LyricItem(parent=None)[source]

Bases: ly.music.items.Item

Another lyric item (skip, extender, hyphen or tie).

class ly.music.items.LyricMode(parent=None)[source]

Bases: ly.music.items.InputMode

A lyricmode, lyrics or addlyrics expression.

class ly.music.items.LyricText(parent=None)[source]

Bases: ly.music.items.Durable

A lyric text (word, markup or string), with a Duration.

class ly.music.items.LyricsTo(parent=None)[source]

Bases: ly.music.items.InputMode

A lyricsto expression.

context_id()[source]
class ly.music.items.Markup(parent=None)[source]

Bases: ly.music.items.Item

A command starting markup (markup, -lines and -list).

plaintext()[source]

Return the plain text value of this node.

class ly.music.items.MarkupCommand(parent=None)[source]

Bases: ly.music.items.Item

A markup command, such as italic etc.

plaintext()[source]

Return the plain text value of this node.

class ly.music.items.MarkupList(parent=None)[source]

Bases: ly.music.items.Item

The group of markup items inside { and }. NOTE: not a markuplist.

plaintext()[source]

Return the plain text value of this node.

class ly.music.items.MarkupScore(parent=None)[source]

Bases: ly.music.items.Item

A score inside Markup.

class ly.music.items.MarkupUserCommand(parent=None)[source]

Bases: ly.music.items.Item

A user-defined markup command

name()[source]

Return the name of this user command (without the leading backslash).

value()[source]

Find the value assigned to this variable.

class ly.music.items.MarkupWord(parent=None)[source]

Bases: ly.music.items.Item

A MarkupWord token.

plaintext()[source]

Return a plaintext value for this node.

This only makes sense for items like Markup or String. For other types, an empty string is returned

class ly.music.items.Midi(parent=None)[source]

Bases: ly.music.items.Container

A midi { … } construct.

class ly.music.items.Music(parent=None)[source]

Bases: ly.music.items.Container

Any music expression, to be inherited of.

events(e, time, scaling)[source]

Let the event.Events instance handle the events. Return the time.

length()[source]

Return the musical duration.

preceding(node=None)[source]

Return a two-tuple (nodes, scaling).

The nodes are the nodes in time before the node (which must be a child), and the scaling is the scaling this node applies (normally 1).

If node is None, all nodes that would precede a fictive node at the end are returned.

class ly.music.items.MusicList(parent=None)[source]

Bases: ly.music.items.Music

A music expression, either << >> or { }.

events(e, time, scaling)[source]

Let the event.Events instance handle the events. Return the time.

preceding(node=None)[source]

Return a two-tuple (nodes, scaling).

The nodes are the nodes in time before the node (which must be a child), and the scaling is the scaling this node applies (normally 1).

If node is None, all nodes that would precede a fictive node at the end are returned.

simultaneous = False
class ly.music.items.Note(parent=None)[source]

Bases: ly.music.items.Durable

A Note that has a ly.pitch.Pitch

accidental_token = None
octave_token = None
octavecheck_token = None
pitch = None
class ly.music.items.NoteMode(parent=None)[source]

Bases: ly.music.items.InputMode

A notemode or notes expression.

class ly.music.items.Number(parent=None)[source]

Bases: ly.music.items.Item

A numerical value, directly entered.

value()[source]
class ly.music.items.Override(parent=None)[source]

Bases: ly.music.items.Item

An override command.

context()[source]
grob()[source]
class ly.music.items.Paper(parent=None)[source]

Bases: ly.music.items.Container

A paper { … } construct.

class ly.music.items.PartCombine(parent=None)[source]

Bases: ly.music.items.Music

The partcombine command with 2 music arguments.

events(e, time, scaling)[source]

Let the event.Events instance handle the events. Return the time.

preceding(node=None)[source]

Return a two-tuple (nodes, scaling).

The nodes are the nodes in time before the node (which must be a child), and the scaling is the scaling this node applies (normally 1).

If node is None, all nodes that would precede a fictive node at the end are returned.

class ly.music.items.Partial(parent=None)[source]

Bases: ly.music.items.Item

A partial command.

duration = (0, 1)
partial_length()[source]

Return the duration given as argument as a Fraction.

class ly.music.items.PathItem(parent=None)[source]

Bases: ly.music.items.Item

An item in the path of an override or revert command.

class ly.music.items.PhrasingSlur(parent=None)[source]

Bases: ly.music.items.Item

A ( or ).

event = None
class ly.music.items.PipeSymbol(parent=None)[source]

Bases: ly.music.items.Item

A pipe symbol: |

class ly.music.items.Postfix(parent=None)[source]

Bases: ly.music.items.Item

Any item that is prefixed with a _, - or ^ direction token.

class ly.music.items.Q(parent=None)[source]

Bases: ly.music.items.Durable

class ly.music.items.Relative(parent=None)[source]

Bases: ly.music.items.Music

A relative music expression. Has one or two children (Note, Music).

class ly.music.items.Repeat(parent=None)[source]

Bases: ly.music.items.Music

A repeat expression.

events(e, time, scaling)[source]

Let the event.Events instance handle the events. Return the time.

repeat_count()[source]
specifier()[source]
class ly.music.items.Rest(parent=None)[source]

Bases: ly.music.items.Durable

class ly.music.items.Revert(parent=None)[source]

Bases: ly.music.items.Item

A revert command.

context()[source]
grob()[source]
class ly.music.items.Scaler(parent=None)[source]

Bases: ly.music.items.Music

A music construct that scales the duration of its contents.

In the numerator and denominator attributes the values specified for LilyPond are stored, e.g. with times 3/2 { c d e }, the numerator is integer 3 and the denominator is integer 2. Note that for tuplet and times the meaning of these numbers is reversed.

The algebraic scaling is stored in the scaling attribute.

denominator = 0
events(e, time, scaling)[source]

Let the event.Events instance handle the events. Return the time.

numerator = 0
preceding(node=None)[source]

Return a two-tuple (nodes, scaling).

The nodes are the nodes in time before the node (which must be a child), and the scaling is the scaling this node applies.

If node is None, all nodes that would precede a fictive node at the end are returned.

scaling = 1
class ly.music.items.Scheme(parent=None)[source]

Bases: ly.music.items.Item

A Scheme expression inside LilyPond.

get_fraction()[source]

A basic way to get one (may be fractional) numerical value.

get_int()[source]

A basic way to get one integer value.

get_list_ints()[source]

A basic way to get a list of integer values.

get_ly_make_moment()[source]

A basic way to get a ly:make-moment fraction.

get_pair_ints()[source]

Very basic way to get two integers specified as a pair.

get_string()[source]

A basic way to get a quoted string value (without the quotes).

plaintext()[source]

A crude way to get the plain text in this node.

class ly.music.items.SchemeItem(parent=None)[source]

Bases: ly.music.items.Item

Any scheme token.

class ly.music.items.SchemeLily(parent=None)[source]

Bases: ly.music.items.Container

A music expression inside #{ and #}.

class ly.music.items.SchemeList(parent=None)[source]

Bases: ly.music.items.Container

A ( … ) expression.

class ly.music.items.SchemeQuote(parent=None)[source]

Bases: ly.music.items.Item

A ‘ in scheme.

class ly.music.items.Score(parent=None)[source]

Bases: ly.music.items.Container

A score { … } construct.

class ly.music.items.Set(parent=None)[source]

Bases: ly.music.items.Item

A set command.

context()[source]

The context, if specified.

property()[source]

The property.

value()[source]

The value, given as argument. This is simply the child element.

class ly.music.items.Skip(parent=None)[source]

Bases: ly.music.items.Durable

class ly.music.items.Slur(parent=None)[source]

Bases: ly.music.items.Item

A ( or ).

event = None
class ly.music.items.String(parent=None)[source]

Bases: ly.music.items.Item

A double-quoted string.

plaintext()[source]

Return the plaintext value of this string, without escapes and quotes.

value()[source]
class ly.music.items.StringTuning(parent=None)[source]

Bases: ly.music.items.Item

A stringTuning command (with a chord as argument).

class ly.music.items.Tag(parent=None)[source]

Bases: ly.music.items.Music

A tag, keepWithTag or removeWithTag command.

events(e, time, scaling)[source]

Let the event.Events instance handle the events. Return the time.

preceding(node=None)[source]

Return a two-tuple (nodes, scaling).

The nodes are the nodes in time before the node (which must be a child), and the scaling is the scaling this node applies (normally 1).

If node is None, all nodes that would precede a fictive node at the end are returned.

class ly.music.items.Tempo(parent=None)[source]

Bases: ly.music.items.Item

duration = (0, 1)
fraction()[source]

Return the note value as a fraction given before the equal sign.

tempo()[source]

Return a list of integer values describing the tempo or range.

text()[source]

Return the text, if set. Can be Markup, Scheme, or String.

class ly.music.items.Tie(parent=None)[source]

Bases: ly.music.items.Item

A tie.

class ly.music.items.TimeSignature(parent=None)[source]

Bases: ly.music.items.Item

A time command.

beatstructure()[source]

The scheme expressions denoting the beat structure, if specified.

fraction()[source]

The lower number as a Fraction (e.g. for 3/2 it returns 1/2).

measure_length()[source]

The length of one measure in this time signature as a Fraction.

numerator()[source]

The upper number (e.g. for 3/2 it returns 3).

class ly.music.items.Token(parent=None)[source]

Bases: ly.music.items.Item

Any token that is not otherwise recognized

class ly.music.items.Translator(parent=None)[source]

Bases: ly.music.items.Item

Base class for a change, new, or context music expression.

context()[source]
context_id()[source]

The context id, if specified after an equal sign.

class ly.music.items.Transpose(parent=None)[source]

Bases: ly.music.items.Music

A transpose music expression. Has normally three children (Note, Note, Music).

class ly.music.items.Tremolo(parent=None)[source]

Bases: ly.music.items.Item

A tremolo item “:”. The duration attribute is a tuple (base, scaling).

duration = (0, 1)
class ly.music.items.Tweak(parent=None)[source]

Bases: ly.music.items.Item

A tweak command.

class ly.music.items.Unpitched(parent=None)[source]

Bases: ly.music.items.Durable

A “note” without pitch, just a standalone duration.

pitch = None
class ly.music.items.Unset(parent=None)[source]

Bases: ly.music.items.Item

An unset command.

context()[source]

The context, if specified.

property()[source]

The property.

class ly.music.items.UserCommand(parent=None)[source]

Bases: ly.music.items.Music

A user command, most probably referring to music.

events(e, time, scaling)[source]

Let the event.Events instance handle the events. Return the time.

name()[source]

Return the name of this user command (without the leading backslash).

value()[source]

Find the value assigned to this variable.

class ly.music.items.Version(parent=None)[source]

Bases: ly.music.items.Item

A version command.

version()[source]

The version as a tuple of ints.

version_string()[source]

The version as a string.

class ly.music.items.VoiceSeparator(parent=None)[source]

Bases: ly.music.items.Item

A voice separator: \

class ly.music.items.With(parent=None)[source]

Bases: ly.music.items.Container

A with … construct.

ly.music.read module

The Reader is used to construct a tree structure of (musical and other) items in a LilyPond document. The item types that are used are in the items module.

You instantiate a Reader with a ly.document.Source that reads tokens from any (part of a) ly.document.Document.

Whitespace and comments are left out.

All nodes (instances of Item) have a ‘position’ attribute that indicates where the item starts in the source text. Almost all items have the token that starts the expression in the ‘token’ attribute and possibly other tokens in the ‘tokens’ attribute, as a tuple.

The ‘end_position()’ method returns the position where the node (including its child nodes) ends.

class ly.music.read.Reader(source)[source]

Bases: object

Reads tokens from a Source and builds a meaningful tree structure.

add_bracketed(item, source)[source]

Append the arguments between brackets to the item.

Returns True if that succeeded, else False.

add_duration(item, token=None, source=None)[source]

Add a duration attribute to the item.

When there are tokens, a Duration item is also appended to the item.

add_markup_arguments(item)[source]

Add markup arguments to the item.

consume(last_token=None)[source]

Yield the tokens from our source until a parser is exit.

If last_token is given, it is called with the last token that is read.

factory(cls, token=None, consume=False, position=None)[source]

Create Item instance for token.

If consume is True, consume()s the source into item.tokens. If you don’t specify a token, you must specify the position (>= 0).

handle_absolute(t, source)[source]

handle absolute

handle_after_grace(t, source)[source]

handle afterGrace

handle_alternative(t, source)[source]

handle alternative

handle_articulation(t, source=None)[source]

handle Articulation

handle_beam(t, source=None)[source]

handle Beam

handle_bracketed(t, source)[source]

handle header, score, bookpart, book, paper, layout, midi, with, context

handle_chord_start(t, source)[source]

handle ChordStart

handle_clef(t, source)[source]

handle clef

handle_direct_items(t, source)[source]

Tokens that directly translate to an Item.

handle VoiceSeparator, PipeSymbol, Dynamic, Tie

handle_direction(t, source)[source]

handle Direction

handle_grace(t, source)[source]

handle grace, acciaccatura, appoggiatura, slashedGrace

handle_include(t, source)[source]

handle include

handle_inputmode(t, source)[source]

handle notemode, notes, chordmode, chords, figuremode, figures, drummode, drums

handle_key(t, source)[source]

handle key

handle_language(t, source)[source]

handle language

handle_length(t, source)[source]

handle Length

handle_lyricmode(t, source)[source]

handle lyricmode, lyrics, oldaddlyrics, addlyrics, lyricsto

handle_markup(t, source=None)[source]

handle markup, markuplist, markuplines

handle_markup_command(t)[source]

handle MarkupCommand

handle_markup_open_bracket(t)[source]

handle OpenBracketMarkup

handle_markup_score(t)[source]

handle MarkupScore

handle_markup_user_command(t)[source]

handle MarkupUserCommand

handle_markup_word(t)[source]

handle MarkupWord

handle_music_item(t, source)[source]

handle MusicItem

handle_music_list(t, source)[source]

handle OpenBracket, OpenSimultaneous, SimultaneousOrSequentialCommand

handle_name(t, source)[source]

handle Name, ContextProperty

handle_number_class(t, source=None)[source]

handle DecimalValue, IntegerValue, Fraction

handle_override(t, source)[source]

handle override

handle_partcombine(t, source=None)[source]

handle partcombine

handle_partial(t, source)[source]

handle partial

handle_relative(t, source)[source]

handle relative

handle_repeat(t, source)[source]

handle repeat

handle_revert(t, source)[source]

handle revert

handle_scaler(t, source)[source]

handle times, tuplet, scaleDurations

handle_scheme_lilypond_start(t)[source]

handle LilyPondStart

handle_scheme_open_parenthesis(t)[source]

handle OpenParen

handle_scheme_quote(t)[source]

handle Quote

handle_scheme_start(t, source=None)[source]

handle SchemeStart

handle SchemeStart

handle_scheme_token(t)[source]

handle Dot, Bool, Char, Word, Number, Fraction, Float

handle_set(t, source)[source]

handle set

handle_slurs(t, source=None)[source]

handle Slur

handle_string_start(t, source=None)[source]

handle StringStart

handle StringStart

handle StringStart

handle_string_tuning(t, source)[source]

handle stringTuning

handle_tag(t, source)[source]

handle tag, keepWithTag, removeWithTag, appendToTag, pushToTag

handle_tempo(t, source)[source]

handle tempo

handle_time(t, source)[source]

handle time

handle_translator(t, source)[source]

handle new, context, change

handle_transpose(t, source)[source]

handle transpose

handle_tweak(t, source)[source]

handle tweak

handle_unset(t, source)[source]

handle unset

handle_variable_assignment(t, source)[source]

handle PaperVariable, LayoutVariable, HeaderVariable, UserVariable

handle_version(t, source)[source]

handle version

read(source=None)[source]

Yield Item instances reading from source.

read_assignment(t)[source]

Read an assignment from the variable name. May return None.

read_chord_specifier(t, source=None)[source]

Read stuff behind notes in chordmode.

handle ChordSeparator

read_command(t, source)[source]

Read the rest of a command given in t from the source.

handle Command

read_item(t, source=None)[source]

Return one Item that starts with token t. May return None.

read_keyword(t, source)[source]

Read the rest of a keyword given in t from the source.

handle Keyword

read_lyric_item(t)[source]

Read one lyric item. Returns None for tokens it does not handle.

read_markup(t)[source]

Read LilyPond markup (recursively).

read_music_item(t, source)[source]

Read one music item (note, rest, s, skip, or q) from t and source.

read_scheme(t)[source]

Return a Scheme item from the token t.

read_scheme_item(t)[source]

Reads a Scheme expression (just after the # in LilyPond mode).

read_tremolo(t, source=None)[source]

Read a tremolo.

handle TremoloColon

read_user_command(t, source)[source]

Read a user command, this can be a variable reference.

handle UserCommand

set_language(lang)[source]

Changes the pitch name language to use.

Called internally when language or include tokens are encountered with a valid language name/file.

Sets the language attribute to the language name.

test_music_list(t)[source]

Test whether a music list ({ … }, << … >>, starts here.

Also handles simultaneous { … } and sequential { … } correctly. These obscure commands are not even highlighted by lex, but they exist in LilyPond… simultaneous { … } is like << … >> but sequential << … >> just behaves like << … >>

Returns a two-tuple(item; iterable), both may be None. If item is not None, it can be either a UserCommand or a MusicList. If iterable is None, the item is a UserCommand (namely simultaneous or sequential, but not followed by a { or <<); else the item is a MusicList, and the iterable should be read fully to get all the tokens inside the MusicList. If item is None, there is no MusicList and no token is read.

This way you can handle the { … } and << … >> transparently in every input mode.

class ly.music.read.dispatcher[source]

Bases: object

Decorator creator to dispatch commands, keywords, etc. to a method.

method(token)[source]

The registered method to call for the token. May return None.

read_arg(a)[source]
class ly.music.read.dispatcher_class[source]

Bases: ly.music.read.dispatcher

Decorator creator to dispatch the class of a token to a method.

method(token)[source]

The registered method to call for the token’s class. May return None.

read_arg(a)[source]
ly.music.read.skip(source, what=(<class 'ly.lex._token.Space'>, <class 'ly.lex._token.Comment'>))[source]

Yield tokens from source, skipping items of classes specified in what.

By default, comments and whitespace are skipped.