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)
>>>
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.
- 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.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.
- 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_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.
- 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.
- 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.
- 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)¶
- 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.
- 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).
- 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¶
- 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.
- 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.
- 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.
- class ly.music.items.Markup(parent=None)[source]¶
Bases:
ly.music.items.Item
A command starting markup (markup, -lines and -list).
- class ly.music.items.MarkupCommand(parent=None)[source]¶
Bases:
ly.music.items.Item
A markup command, such as italic etc.
- class ly.music.items.MarkupList(parent=None)[source]¶
Bases:
ly.music.items.Item
The group of markup items inside { and }. NOTE: not a markuplist.
- 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
- class ly.music.items.MarkupWord(parent=None)[source]¶
Bases:
ly.music.items.Item
A MarkupWord token.
- 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.
- class ly.music.items.MusicList(parent=None)[source]¶
Bases:
ly.music.items.Music
A music expression, either << >> or { }.
- 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.
- class ly.music.items.Override(parent=None)[source]¶
Bases:
ly.music.items.Item
An override command.
- 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.
- class ly.music.items.Partial(parent=None)[source]¶
Bases:
ly.music.items.Item
A partial command.
- duration = (0, 1)¶
- 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.
- 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.
- 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¶
- 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.
- 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.
- 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.
- 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.
- class ly.music.items.Tempo(parent=None)[source]¶
Bases:
ly.music.items.Item
- duration = (0, 1)¶
- 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.
- 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.
- 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.
- class ly.music.items.UserCommand(parent=None)[source]¶
Bases:
ly.music.items.Music
A user command, most probably referring to music.
- class ly.music.items.Version(parent=None)[source]¶
Bases:
ly.music.items.Item
A version command.
- 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.
- 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_bracketed(t, source)[source]¶
handle header, score, bookpart, book, paper, layout, midi, with, context
- handle_direct_items(t, source)[source]¶
Tokens that directly translate to an Item.
handle VoiceSeparator, PipeSymbol, Dynamic, Tie
- handle_inputmode(t, source)[source]¶
handle notemode, notes, chordmode, chords, figuremode, figures, drummode, drums
- handle_music_list(t, source)[source]¶
handle OpenBracket, OpenSimultaneous, SimultaneousOrSequentialCommand
- handle_string_start(t, source=None)[source]¶
handle StringStart
handle StringStart
handle StringStart
- handle_variable_assignment(t, source)[source]¶
handle PaperVariable, LayoutVariable, HeaderVariable, UserVariable
- 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_keyword(t, source)[source]¶
Read the rest of a keyword given in t from the source.
handle Keyword
- read_music_item(t, source)[source]¶
Read one music item (note, rest, s, skip, or q) from t and source.
- 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.
- class ly.music.read.dispatcher_class[source]¶
Bases:
ly.music.read.dispatcher
Decorator creator to dispatch the class of a token to a method.