简体   繁体   English

在 music21 中创建音符图像

[英]Creating images of notes in music21

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.我已经安装了 scipy 和 mathplotlib,所以 music21 不会再抱怨它们不可用了。 I run Python 3.4 on Windows 7.我在 Windows 7 上运行 Python 3.4。

If I instead use s.show('musicxml.png') to get my images I get the error:如果我改用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

What do I have to do to get images (preferably svg)?我需要做什么才能获取图像(最好是 svg)?

LILYPOND百合

I had same error.我有同样的错误。 I have managed to configure LilyPond for music21 in the following way:我已设法通过以下方式为 music21 配置 LilyPond:

  1. Moved LilyPond folder to path without blank spaces (from C:\\Program Files (x86)\\LilyPond\\usr\\bin to C:\\LilyPond\\usr\\bin ).将 LilyPond 文件夹移动到没有空格的路径(从C:\\Program Files (x86)\\LilyPond\\usr\\binC:\\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.我在 music21 代码中看到,在执行 lilypond 命令时,它没有在路径周围放置必要的配额,因此必须以这种方式解决问题。
  2. Created configuration file in music21 and set lilypondPath在music21中创建配置文件并设置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 (以防万一,将Musescore安装到没有空格的路径(
  2. Added twice musescore path to environment (found this new way of setting environment variables), once as "musescoreDirectPNGPath" :向环境添加了两次 musescore 路径(找到了这种设置环境变量的新方法),一次为"musescoreDirectPNGPath"

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

    and then as "musicxmlPath" :然后作为"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:经过多次尝试、调试等。我了解到,如果我们想使用 Musescore,重要的是传入文件名“.xml”而不是“.png”扩展名:

    stream_name.show('musicalxml.xml') stream_name.show('musicalxml.xml')

    Musescore cannot open .png file, but it can open .xml file. Musescore 不能打开 .png 文件,但它可以打开 .xml 文件。

Finally, I can add some code that generates files without opening lilypond or musescore.最后,我可以添加一些无需打开 lilypond 或 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.请注意,该 scorename 具有“.xml”扩展名。

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. Musescore 将“-1”添加到文件名,但可以获取此更改后的文件路径(如上面代码中的 out_filepath)并稍后重命名为我们想要的。

I was able to setup musescore with a path with spaces.我能够使用带空格的路径设置 musescore。 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!希望能帮助到你!

try #sudo apt-get update to install lilypond !apt-get update !apt-get -y install lilypond尝试#sudo apt-get update 安装 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.否则,lilypond 希望下载的附加文件会因系统过时而失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM