Search code examples
pythonlilypondmusic21

Importing Lilypond file into a local music21 corpus


I'm hoping to run a corpus study on some local Lilypond (.ly) files, but I'm having trouble importing them into a local music21 corpus.

I can only assume the answer is on the music21.converter page, but I somehow can't seem to untangle it. As suggested at the top of that page, I have tried:

converter.parse('/home/richard/test.ly')

Only to receive:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/richard/.local/lib/python3.9/site-packages/music21/converter/__init__.py", line 1168, in parse
    return parseFile(valueStr, number=number, format=m21Format,
  File "/home/richard/.local/lib/python3.9/site-packages/music21/converter/__init__.py", line 1027, in parseFile
    v.parseFile(fp, number=number, format=format, forceSource=forceSource, **keywords)
  File "/home/richard/.local/lib/python3.9/site-packages/music21/converter/__init__.py", line 548, in parseFile
    useFormat = self.getFormatFromFileExtension(fp)
  File "/home/richard/.local/lib/python3.9/site-packages/music21/converter/__init__.py", line 526, in getFormatFromFileExtension
    raise ConverterFileException(f'cannot find a format extensions for: {fp}')
music21.converter.ConverterFileException: cannot find a format extensions for: /home/richard/test.ly

Can anyone clarify the proper way to import these .ly files? I'm unfortunately new to Python, so I'm afraid I don't entirely know what I don't know.


Solution

  • Music21 does not import Lilypond files, only export. Because .ly files are essentially Scheme code with macros, it is extremely hard to import for any program not itself written in Scheme. See https://music.stackexchange.com/questions/42315/lilypond-to-musicxml-to-sibelius for some options. The best one I've found is to use an older version of Frescobaldi.