在音乐中创建音符图像21

时间:2014-09-16 23:07:17

标签: python 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)?

2 个答案:

答案 0 :(得分:3)

<强> LilyPond的

我有同样的错误。我已经设法通过以下方式为music21配置LilyPond:

  1. 将LilyPond文件夹移至没有空格的路径(从C:\Program Files (x86)\LilyPond\usr\binC:\LilyPond\usr\bin)。 我在music21代码中看到,在执行lilypond命令时,它没有在路径周围放置必要的配额,所以必须以这种方式解决问题。
  2. Created configuration file in music21 and set lilypondPath

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

    你可以检查它是否设置正确:

    print us['lilypondPath']
    
  3. 嗯,这可能没有必要,但在我尝试的过程中,我重复了几次,所以如果一切都不能立即起作用,你可以在最后尝试。

  4. <强> MUSESCORE

    1. 以防万一,将Musescore安装到没有空格的路径(
    2. 添加两次musescore路径到环境(找到这种设置环境变量的新方法),一次为"musescoreDirectPNGPath"

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

      然后是"musicxmlPath"

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

    3. 经过几次尝试,调试等后,我了解到如果我们想使用Musescore,传入文件名“.xml”扩展名而不是“.png”非常重要:

      stream_name.show( 'musicalxml.xml')

      Musescore无法打开.png文件,但可以打开.xml文件。

    4. 最后,我可以添加一些生成文件的代码,而无需打开lilypond或musescore。希望有人发现它有用

      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'])
      

      请注意,该得分名称的扩展名为“.xml”。

      不幸的是,它不会将文件保存在指定的文件路径中。 Musescore将“-1”添加到文件名,但是可以获得这个更改的文件路径(在上面的代码中作为out_filepath)并稍后重命名为我们想要的。

答案 1 :(得分:0)

我能够使用带空格的路径设置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'

希望它有所帮助!