Python Music Library?

Viewed 33890

I'm looking at writing a little drum machine in Python for fun. I've googled some and found the python pages on music and basic audio as well as a StackOverflow question on generating audio files, but what I'm looking for is a decent library for music creation. Has anyone on here tried to do something like this before? If so, what was your solution? What, either of the ones I've found, or something I haven't found, would be a decent library for audio manipulation?

Minimally, I'd like to be able to do something similar to Audacity's scope within python, but if anyone knows of a library that can do more... I'm all ears.

5 Answers

I had to do this years ago. I used pymedia. I am not sure if it is still around any way here is some test code I wrote when I was playing with it. It is about 3 years old though.

Edit: The sample code plays an MP3 file

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'

There is a variety of Python music software, you can find a catalog here.

If you scroll down the linked page, you find a section on Music Programming in Python describing several music creation packages including MusicKit and PySndObj.

Related