A study on the export and import of musical scores between LilyPond and MuseScore via MusicXML

LilyPond and MuseScore
Table of Contents

About LilyPond and MuseScore.

LilyPond and MuseScore are the two most interesting open source projects dedicated to music notation.

LilyPond is a markup language, compatible with LaTeX (I refer you to this my article on LaTeX and LilyPond for writing text and music), that allows you to write musical scores of high graphical quality using text characters.

This feature makes it, among other things, also usable by AI systems, such as ChatGPT, to write autogenerated scores.

MuseScore has recently reached version 4 with many new features and updates in both visual and audio aspects.

The automatic musical rendition of its scores is such that it is comparable with actual interpretations.

However, this is not an article comparing the two systems: ideally, it is best to know them both.

But there is a problem: you may need to transfer scores from one application to the other to avoid having to rewrite all the work from scratch.

Is it possible to convert score formats directly between both applications?

Unfortunately, no! There is not (yet) a direct conversion function between the applications.

However, there is an intermediate conversion format common to these as well as other music notation applications: MusicXML.

On this page I found a score fragment found in MusicXML format that I used for testing.

It is the first four bars of the composition for voice and piano “Après un rêve,” Op. 7 No. 1, by Gabriel Fauré:

export_pdf_lilypond

The description of only 4 bars, in MusicXML language requires as many as 1570 lines of code and the complete piece consists of 48 bars: you do the math!

The MusicXML language is, therefore, practically unusable for direct writing and reading of scores but is, instead, an excellent transfer system between different music editing applications.

MuseScore exports smoothly to the MusicXML format and reads that format directly, even without the need for import.

The reverse transition is, unfortunately, more problematic: there is still no reliable function to export from LilyPond to the MusicXML format.

There is, however, a function for importing from MusicXML to LilyPond.

A score exported from MuseScore can, therefore, be imported into LilyPond either using the command line or the Frescobaldi GUI, both of which are based on the musicxml2ly command.

The “weak” point is, therefore, in the transition from LilyPond to MusicXML.

First attempt: from LilyPond to MuseScore with python-ly.

The first attempt draws from this page where the following solution is suggested:

from a terminal on your mac or linux box:

pip install python-ly

ly musicxml myscore.ly > myscore.musicxml

more information on ly: http://python-ly.readthedocs.io/en/latest/command.html#commands

I proceed, then, with the installation of python-ly on Arch Linux with pip install python-ly and subsequently, converting from LilyPond to MusicXML in the Arch Linux environment with the following command:

ly musicxml Documents/prove-reve.ly > Documents/export_test.musicxml

An error is reported:

Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
OSError: [Error 9] Bad file descriptor

However, a file is created with the name given in the command.

I try, therefore, to open the file with the command export_test.musicxml.

Opening it provides negative results!

I found another suggestion in this page with a much more complex export sequence:

import ly.musicxml
import ly.document

e = ly.musicxml.writer()

ly_doc = ly.document.Document.load('songs/ly/old-macdonald-had-a-farm.ly')

e.parse_text(ly_doc.plaintext())
ly_doc_xml = e.musicxml()
ly_doc_xml.write('songs/ly/old-macdonald-had-a-farm.xml')

This, however, also failed and gave the following error after the string e = ly.musicxml.writer():

bash: syntax error near unexpected token `('

The first attempt was, therefore, unsuccessful.

Second attempt: from LilyPond to MuseScore via MIDI files.

Some commentators suggest using the MIDI standard as a common basis for transferring scores.

I tried, therefore, to open in MuseScore the MIDI version of “Après un rêve,” obtained directly from the score opened in Frescobaldi thanks to the presence, in the source code, of the command \midi {\tempo 4 = 60 }.

The result is truly disappointing and needs no comment, as can be seen from the two fragments below (even from 4 bars 8 were taken):

Import Midi MuseScore

Import Midi MuseScore 2

Third attempt: from LilyPond to MuseScore via optical PDF score recognition.

MuseScore has a menu item called “Import PDF.”

This is an optical PDF score recognition service based on Audiveris.

The service is run by Musescore.com and this means that you need to log in to access the service.

I tried, therefore, with the fragment of “Après un rêve” and I report, for clarity, the starting and ending result:

  • Starting PDF in LilyPond:
    export_pdf_lilypond
  • Import into MuseScore:
    import_reve-musescore

The result is lacking some essential elements: the second triplet of the third beat in the right hand; the last chord of the third beat in the left hand.

However, it is worth considering.

Fourth attempt: from LilyPond to MuseScore via original Audiveris.

As anticipated above, there is a reduced version of Audiveris in MuseScore.

I, therefore, tried to use the Audiveris software in its original version, currently at 5.3.

The project page can be found at this address.

In Arch you can easily install the application with yay -S audiveris and choosing the version with Java 17.

I did not perform the installation in other operating systems.

At this point I subjected the usual Fauré fragment to optical recognition and the result was, this time, definitely very interesting.

I report, as before, the start and finish screen:

  • Starting PDF in LilyPond:

export_pdf_lilypond

  • Import into MuseScore:

Audiveris import

A few small details need to be corrected, for example, the second beat of the vocal part and the distribution of the text.

But the result is a very good basis for import.

Summary.

The transfer from MusicXML and LilyPond is done without any problem thanks to the direct conversion feature from MuseScore to the MusicXML interchange format and the corresponding LilyPond import feature via musicxml2ly.

Transferring from LilyPond to MusicXML, on the other hand, is problematic because there is, to date, no export function from LilyPond to the same interchange format mentioned above.

The most reliable solution, at present, is the optical reading of the PDF score generated by LilyPond through the open-source software Audiveris in the original version, not the reduced version contained in Frescobaldi.

Thank you for your attention.

Lawyer

Let’s talk about technology?