2

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

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.

|improve this answer|||||
  • 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 at 18:15

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.