3

I have a lot of lilypond files each representing a single musical piece. There are many kinds of instrumentations in the lilypond files.

I want to get some simple statistics from a single lilypond file as:

  • number of systems (which instruments are involved?)
  • higest note in each system
  • lowest note in each system
  • shortest note in each system
  • number of bars in the piece
  • length of audio-file produces by lilypond

A sample output could be:

piece: silent night
system 1: 
  - instrument: trumpet
  - transposition: bes (for trumpet in Bb)
  - clef: treble
  - key: e-flat major
  - key signature: es (3x b)
  - lowest note: c1
  - highest note: d2
  - shortest note: 4 (quarter note)
  - max-chord: 1 (1 note at a time)
system 2: 
  - instrument: piano
  - transposition: c
  - clef: treble/bass
  - key: e-flat major
  - key signature: es (3x b)
  - lowest note: c1
  - highest note: d2
  - shortest note: 4 (quarter note)
  - max-chord: 4 (4 notes at a time)

I a looking for a script that takes a lilypond source file .ly as input and outputs the analysis data in structured format (JSON, YML,...).

There seems to be no easy way to achieve this. I found Music21 but it is not directly related to Lilypond and that approach seems overly complex for my simple analysis.

2 Answers 2

3

The bad news is that the only reliable parser for LilyPond files is LilyPond. Once you take into account conventions and personal discipline, you might get away with more text-based approaches.

The good news is that what LilyPond does with its parsed files is entirely determined by "hooks".

By redefining the hooks

toplevel-book-handler
toplevel-bookpart-handler
toplevel-score-handler
toplevel-music-handler
toplevel-text-handler
book-bookpart-handler
book-score-handler
book-music-handler
book-text-handler
bookpart-score-handler
bookpart-music-handler
bookpart-text-handler

in your own init file that you pass via --init= (the default here is init.ly) you can redefine what LilyPond does after having parsed the listed entities. Since by far the largest amount of time is spent with actual typesetting, appropriating the parser in that manner will give you a way to get results quite faster than actually running LilyPond "proper".

Of course, doing so would not quite meet the scope of StackExchange, so you might want to consider contacting one of the communication media of the project when you get stuck, such as its user and/or developer list or one of the national LilyPond groups.

1
  • Wow, thank you. But that sounds utterly overloaded and complex for my little analysis task. I‘ll try to look into what it needs to write some code for the hooks.
    – WeSee
    Feb 23, 2020 at 18:15
1

Maybe I found a solution for doing basic Lilypond music analysis:

  • export lilypond to humdrum format
  • extracting the part (aka "spine" in humdrum) to be analyzed using humdum extract
  • peforming analysis with unix commandline tools as bash, sed, awk, grep, sort,...

Before I start further digging in humdrum, has anybody experiences with lilypond to humdrum exports and humdrum spine analysis for scores with wind instuments or piano?

EDIT:

Humdrum is definitively a solution. There is a Humdrum command census which does exactely this kind of analysis in the question.

When given a Humdrum input file (J.S. Bach Die Kunst der Fuge) as

J.S. Bach Die Kunst der Fuge

**kern
*clefG2
*k[b-]
*M2/2
=-
2d/
2a/
=
2f/
2d/
=
2c#/
4d/
4e/
=
2f/
2r
*-

and applying the command

$ cat bach.krn | docker run weesee/humdrum census -k

the output will be

HUMDRUM DATA

Number of data tokens:     13
Number of null tokens:     0
Number of multiple-stops:  0
Number of data records:    13
Number of comments:        0
Number of interpretations: 5
Number of records:         18

KERN DATA

Number of note-heads:      8
Number of notes:           8
Longest note:              2
Shortest note:             4
Highest note:              a
Lowest note:               c#
Number of rests:           1
Maximum number of voices:  1
Number of single barlines: 4
Number of double barlines: 0

For more information see this repo and the Humdrum documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.