所以,我已经关注了这个问题,以便用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()
这是回溯:
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 . . .
我在 Windows 7 x64 上运行 Python 3.4 x86(Anaconda 发行版)。我不知道如何解决这个问题(但可能是一些晦涩难懂的 Python 2.x 与 Python 3.x 不兼容问题,一如既往)
编辑:
我已经按照答案中的建议编辑了导入,现在我得到了一个 TypeError:
您会建议我做什么来替代使用 Music21“播放一些音频”?(Fluidsynth 或其他任何东西)。