Question

I'm creating a waveform representation of a wav file and was curious what the best possible methods to go about this efficiently in python using the standard library. Some audio files could be minutes long.

Thanks!

Était-ce utile?

La solution 2

The TimeSide library solves this problem in a very simple way, while providing some more interesting graphs.

I had this same problem and implemented the algorithm suggested by synthesizerpatel, later I discovered this library and got some better results.

Autres conseils

http://docs.python.org/2/library/wave.html - stdlib for reading wav files. A simple example of using it.

Off the top of my head this is how I'd do it (pseudocode)

fmts = (None, "=B", "=h", None, "=l")
fmt = fmts[sampwidth]
dcs  = (None, 128, 0, None, 0)
dc = dcs[sampwidth]

image_width = 600
image_height = 300
chunk_size = len(wavefile.getnframes()) / image_width

def unpacker(frame):
    return struct.unpack(fmt, frame)[0]

for i in range(chunk_size):
    value = math.avg([unpacker(x) for x in wavefile.read_frames(chunk_size)])
    # and then use value * 300 to figure out the vertical position for the pixel.

There are a variety of libraries you could use instead of writing an image, svg for example.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top