문제

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!

도움이 되었습니까?

해결책 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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top