Creating images of notes in music21

↘锁芯ラ 提交于 2019-12-06 03:30:53

问题


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)?


回答1:


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.




回答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!



来源:https://stackoverflow.com/questions/25879764/creating-images-of-notes-in-music21

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