Creating images of notes in music21

痴心易碎 提交于 2019-12-04 08:48:29

LILYPOND

I had same error. I have managed to configure LilyPond for music21 in the following way:

  1. Moved LilyPond folder to path without blank spaces (from C:\Program Files (x86)\LilyPond\usr\bin to C:\LilyPond\usr\bin). I saw in music21 code that it does not put necessary quotas around path when executing lilypond command, so had to resolve the problem this way.
  2. Created configuration file in music21 and set lilypondPath

    us = environment.UserSettings()
    us.create()
    us['lilypondPath'] = 'C:/LilyPond/usr/bin/lilypond.exe'
    

    you can check whether it is set properly:

    print us['lilypondPath']
    
  3. Well, this might be not necessary, but during my attempts I restarted everything several times, so you may try it at the end if everything does not work immediately.

MUSESCORE

  1. Just in case, installed Musescore to path without blank spaces (
  2. Added twice musescore path to environment (found this new way of setting environment variables), once as "musescoreDirectPNGPath":

    environment.set("musescoreDirectPNGPath", "C:/MuseScore2/bin/MuseScore.exe")

    and then as "musicxmlPath":

    environment.set("musicxmlPath", "C:/MuseScore2/bin/MuseScore.exe")

  3. After several tries, debugging etc. I have learnt that it is important to pass in file name '.xml' extension instead of '.png' if we want to use Musescore:

    stream_name.show('musicalxml.xml')

    Musescore cannot open .png file, but it can open .xml file.

Finally, I can add some code that generates files without opening lilypond or musescore. Hope that someone finds it usefull

LILYPOND:

# music21object - stream or score or any object that can be showed
conv =  music21.converter.subConverters.ConverterLilypond()
scorename = 'myScoreName'
filepath = 'C:/path/to/musical_scores/' + scorename
conv.write(music21object, fmt = 'lilypond', fp=filepath, subformats = ['pdf'])

MUSESCORE:

from music21.converter.subConverters import ConverterMusicXML
conv_musicxml = ConverterMusicXML()
scorename = 'myScoreName.xml'
filepath = 'C:/path/to/musical_scores/' + scorename
out_filepath = conv_musicxml.write(music21object, 'musicxml', fp=filepath, subformats=['png'])

Notice, that scorename has '.xml' extension.

Unfortunately, it does not save file in the specified filepath. Musescore adds "-1" to filename, but it is possible to get this changed filepath (as out_filepath in code above) and rename later to what we want.

I was able to setup musescore with a path with spaces. The most Important thing is to make sure to use inverted slash. This is how I did it:

# Create the user environment for music21
us = m21.environment.UserSettings()
us['musicxmlPath'] = 'C:/Program Files (x86)/MuseScore 2/bin/MuseScore.exe'
us['musescoreDirectPNGPath'] = 'C:/Program Files (x86)/MuseScore 2/bin/MuseScore.exe'

Hope it helps!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!