Python音乐库?

41 投票
5 回答
35022 浏览
提问于 2025-04-11 09:18

我想用Python写一个简单的打击乐器,纯粹是为了好玩。我在网上查了一些资料,发现了关于音乐基本音频的Python页面,还有一个关于生成音频文件的StackOverflow问题,但我真正想要的是一个不错的音乐创作库。这里有没有人尝试过类似的事情?如果有,你们是怎么解决的?我找到的这些库,或者其他我没找到的,有没有适合音频处理的好库?

至少,我希望能在Python中做一些类似于Audacity的功能,但如果有人知道能做更多的库,我非常乐意听听。

5 个回答

4

有很多种Python音乐软件,你可以在这里找到一个目录。

如果你往下滚动那个链接的页面,你会看到一个关于在Python中编程音乐的部分,里面介绍了几个音乐创作的工具包,包括MusicKitPySndObj

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

撰写回答