Creating images of notes in music21
Asked Answered
I

3

10

I get an error when running:

from music21 import *

n1 = note.Note('C4', quarterLength=1)
n2 = note.Note('A4', quarterLength=1)
s = stream.Stream()
s.append(n1)
s.append(n2)
s.show('lily.svg')

Traceback (most recent call last):
  File "C:\Python34\test.py", line 7, in <module>
    s.show('lily.svg')
  File "C:\Python34\lib\site-packages\music21\base.py", line 2206, in show
    return formatWriter.show(self, regularizedConverterFormat, app=app, subformats=subformats, **keywords)
  File "C:\Python34\lib\site-packages\music21\converter\subConverters.py", line 277, in show
    returnedFilePath = self.write(obj, fmt, subformats=subformats, **keywords)
  File "C:\Python34\lib\site-packages\music21\converter\subConverters.py", line 245, in write
    conv = lily.translate.LilypondConverter()
  File "C:\Python34\lib\site-packages\music21\lily\translate.py", line 147, in __init__
    self.setupTools()
  File "C:\Python34\lib\site-packages\music21\lily\translate.py", line 177, in setupTools
    versionString = versionString.split()[-1]
IndexError: list index out of range

I have installed scipy and mathplotlib so music21 doesn't complain anymore about them not being available. I run Python 3.4 on Windows 7.

If I instead use s.show('musicxml.png') to get my images I get the error:

Traceback (most recent call last):
  File "C:\Python34\test.py", line 7, in <module>
    s.show('musicxml.png')
  File "C:\Python34\lib\site-packages\music21\base.py", line 2206, in show
    return formatWriter.show(self, regularizedConverterFormat, app=app, subformats=subformats, **keywords)
  File "C:\Python34\lib\site-packages\music21\converter\subConverters.py", line 147, in show
    returnedFilePath = self.write(obj, fmt, subformats=subformats, **keywords)
  File "C:\Python34\lib\site-packages\music21\converter\subConverters.py", line 637, in write
    fp = self.runThroughMusescore(fp, **keywords)
  File "C:\Python34\lib\site-packages\music21\converter\subConverters.py", line 606, in runThroughMusescore
    elif not os.path.exists(musescoreFile):
  File "C:\Python34\lib\genericpath.py", line 19, in exists
    os.stat(path)
TypeError: stat: can't specify None for path argument

What do I have to do to get images (preferably svg)?

Iredale answered 16/9, 2014 at 23:7 Comment(3)
Do you have lilypond or musescore installed in your system? I've checked the source code and it is trying to locate those applications. Lilypond for the svg output and musescore for the png.Amata
I have lilypond installed at in my path as C:\Program Files (x86)\LilyPond\usr\binIredale
If you edit this file: C:\Python34\lib\site-packages\music21\lily\translate.py and insert 'print versionString' on line 177, before '_versionString = versionString.split()[-1] _' you might see the error you are getting.Amata
U
7

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.

Unrestraint answered 13/6, 2015 at 18:6 Comment(0)
P
2

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!

Preferable answered 29/9, 2017 at 20:1 Comment(0)
M
1

try #sudo apt-get update to install lilypond !apt-get update !apt-get -y install lilypond

else the additional files that the lilypond wishes to download fails due to an outdated system.

Meritorious answered 6/10, 2022 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.