[英]Creating images of notes in music21
運行時出現錯誤:
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
我已經安裝了 scipy 和 mathplotlib,所以 music21 不會再抱怨它們不可用了。 我在 Windows 7 上運行 Python 3.4。
如果我改用s.show('musicxml.png')
來獲取我的圖像,我會收到錯誤消息:
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
我需要做什么才能獲取圖像(最好是 svg)?
百合
我有同樣的錯誤。 我已設法通過以下方式為 music21 配置 LilyPond:
C:\\Program Files (x86)\\LilyPond\\usr\\bin
到C:\\LilyPond\\usr\\bin
)。 我在 music21 代碼中看到,在執行 lilypond 命令時,它沒有在路徑周圍放置必要的配額,因此必須以這種方式解決問題。在music21中創建配置文件並設置lilypondPath
us = environment.UserSettings() us.create() us['lilypondPath'] = 'C:/LilyPond/usr/bin/lilypond.exe'
您可以檢查它是否設置正確:
print us['lilypondPath']
好吧,這可能不是必需的,但在我的嘗試中,我重新啟動了好幾次,所以如果一切都不能立即生效,你可以在最后嘗試。
音樂節
向環境添加了兩次 musescore 路徑(找到了這種設置環境變量的新方法),一次為"musescoreDirectPNGPath"
:
environment.set("musescoreDirectPNGPath", "C:/MuseScore2/bin/MuseScore.exe")
然后作為"musicxmlPath"
:
environment.set("musicxmlPath", "C:/MuseScore2/bin/MuseScore.exe")
經過多次嘗試、調試等。我了解到,如果我們想使用 Musescore,重要的是傳入文件名“.xml”而不是“.png”擴展名:
stream_name.show('musicalxml.xml')
Musescore 不能打開 .png 文件,但它可以打開 .xml 文件。
最后,我可以添加一些無需打開 lilypond 或 musescore 即可生成文件的代碼。 希望有人覺得它有用
百合:
# 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'])
音樂評分:
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'])
請注意,該 scorename 具有“.xml”擴展名。
不幸的是,它不會將文件保存在指定的文件路徑中。 Musescore 將“-1”添加到文件名,但可以獲取此更改后的文件路徑(如上面代碼中的 out_filepath)並稍后重命名為我們想要的。
我能夠使用帶空格的路徑設置 musescore。 最重要的是確保使用反斜杠。 我是這樣做的:
# 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'
希望能幫助到你!
嘗試#sudo apt-get update 安裝 lilypond !apt-get update !apt-get -y install lilypond
否則,lilypond 希望下載的附加文件會因系統過時而失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.