Python音乐库?
5 个回答
8
几年前我也做过这个。我用的是pymedia。我不确定它现在还在不在,不过这里有一些我当时玩的时候写的测试代码。这个代码大约有三年了。
补充:这个示例代码可以播放一个MP3文件。
import pymedia
import time
demuxer = pymedia.muxer.Demuxer('mp3') #this thing decodes the multipart file i call it a demucker
f = open(r"path to \song.mp3", 'rb')
spot = f.read()
frames = demuxer.parse(spot)
print 'read it has %i frames' % len(frames)
decoder = pymedia.audio.acodec.Decoder(demuxer.streams[0]) #this thing does the actual decoding
frame = decoder.decode(spot)
print dir(frame)
#sys.exit(1)
sound = pymedia.audio.sound
print frame.bitrate, frame.sample_rate
song = sound.Output( frame.sample_rate, frame.channels, 16 ) #this thing handles playing the song
while len(spot) > 0:
try:
if frame: song.play(frame.data)
spot = f.read(512)
frame = decoder.decode(spot)
except:
pass
while song.isPlaying(): time.sleep(.05)
print 'well done'
14
仔细看看 cSounds。这个工具有Python的接口,可以让你进行非常灵活的数字合成。还有一些非常完整的包可以使用。
想了解一个包,可以查看 http://www.csounds.com/node/188。
如果你想知道在cSounds中如何使用Python脚本,可以访问 http://www.csounds.com/journal/issue6/pythonOpcodes.html。