简体   繁体   English

Music21 Midi错误:类型对象'_io.StringIO'没有属性'StringIO'。 怎么解决?

[英]Music21 Midi Error: type object '_io.StringIO' has no attribute 'StringIO'. How to fix it?

So, I've followed this question in order to get some sound playing with Music21 , and here's the code: 所以,我已经按照这个问题来播放Music21的声音了,这里是代码:

from music21 import *
import random

def main():

#  Set up a detuned piano 
#  (where each key has a random 
#  but consistent detuning from 30 cents flat to sharp)
#  and play a Bach Chorale on it in real time.


    keyDetune = []
    for i in range(0, 127):
        keyDetune.append(random.randint(-30, 30))

    b = corpus.parse('bach/bwv66.6')
    for n in b.flat.notes:
        n.microtone = keyDetune[n.midi]
    sp = midi.realtime.StreamPlayer(b)
    sp.play()

    return 0

if __name__ == '__main__':

    main()

And here's the traceback: 这是追溯:

Traceback (most recent call last):
  File "main.py", line 49, in <module>
    main()
  File "main.py", line 44, in main
    sp.play()
  File "G:\Development\Python Development\Anaconda3\lib\site-packages\music21\mi
di\realtime.py", line 104, in play
    streamStringIOFile = self.getStringIOFile()
  File "G:\Development\Python Development\Anaconda3\lib\site-packages\music21\mi
di\realtime.py", line 110, in getStringIOFile
    return stringIOModule.StringIO(streamMidiWritten)
AttributeError: type object '_io.StringIO' has no attribute 'StringIO'
Press any key to continue . . .

I'm running Python 3.4 x86 (Anaconda Distribution) on Windows 7 x64. 我在Windows 7 x64上运行Python 3.4 x86(Anaconda Distribution)。 I have no idea on how to fix this (But probably is some obscure Python 2.x to Python 3.x incompatibility issue, as always ) 我不知道如何解决这个问题(但可能是一些模糊的Python 2.x到Python 3.x不兼容问题, 一如既往

EDIT: 编辑:

I've edited the import as suggested in the answer, and now I got a TypeError: 我按照答案中的建议编辑了导入,现在我得到了一个TypeError:

在此输入图像描述

What would you recommend me to do as an alternative to "play some audio" with Music21? 你建议我做什么作为Music21的“播放音频”的替代方案? (Fluidsynth or whatever, anything). (Fluidsynth或其他任何东西)。

You may be right... I think the error may actually be in Music21, with the way it handles importing StringIO 你可能是对的......我认为错误可能实际上在Music21中,它处理StringIO

Python 2 has StringIO.StringIO , whereas Python 2有StringIO.StringIO ,而

Python 3 has io.StringIO Python 3有io.StringIO

..but if you look at the import statement in music21\\midi\\realtime.py music21\\midi\\realtime.py但是如果你看一下music21\\midi\\realtime.py中的import语句

try:
    import cStringIO as stringIOModule
except ImportError:
    try:
        import StringIO as stringIOModule
    except ImportError:
        from io import StringIO as stringIOModule

The last line is importing io.StringIO , and so later on the call to stringIOModule.StringIO() fails because it's actually calling io.StringIO.StringIO . 最后一行是导入io.StringIO ,因此稍后调用stringIOModule.StringIO()失败,因为它实际上调用了io.StringIO.StringIO

I would try to edit the import statement to: 我会尝试将import语句编辑为:

    except ImportError:
        import io as stringIOModule

And see if that fixes it. 并看看是否修复它。

return stringIOModule.StringIO(streamMidiWritten) AttributeError: type object '_io.StringIO' has no attribute 'StringIO' Press any key to continue . return stringIOModule.StringIO(streamMidiWritten)AttributeError:type object'_io.StringIO'没有属性'StringIO'按任意键继续。 . .

Please @Ericsson, just remove the StringIO from the return value and you are good to go. 请@Ericsson,只需从返回值中删除StringIO,就可以了。

It will now be: return stringIOModule(streamMidiWritten) 它现在将是:return stringIOModule(streamMidiWritten)

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

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