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.


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


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.

  • 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 '20 at 18:15

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?


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


and applying the command

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

the output will be


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


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, privacy policy and cookie policy

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