1

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!

5
  • 1
    FWIW, numpy isn't part of the stdlib. Commented May 21, 2013 at 21:29
  • my bad. Didn't know that. I guess standard library with the except of numpy. Commented May 21, 2013 at 21:34
  • What exactly do you mean by "creating a waveform representation"? You mean a graphical representation of the waveform (i.e., a graph of sample value vs. time)? If so, are you looking to generate a 200000x200 PNG file, to draw an animated graph in a Tk window in real-time, or… ? Commented May 21, 2013 at 21:36
  • 1.bp.blogspot.com/-GV_aYt3elV0/T84nErDNJ3I/AAAAAAAAAWI/… Something like this. This is for representation in autodesk maya. It will be a animation curve. Commented May 21, 2013 at 21:39
  • 1
    Not stdlib, but I'd still consider looking at combining audiolab (docs) and matplotlib. Commented May 21, 2013 at 21:50

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Additionally: An example of a module that includes C code (that could Easily be converted to Python code instead..) github.com/superjoe30/PyWaveform
This is awesome, but i'm really just in need of an amplitude array from a wav file. I'm pretty sure I can work from here though!
0

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.

1 Comment

Ah, missed the part you said "standard library". Anyway, this question is one of the first resources I found, hope TimeSide solves someone's problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.