What is the easiest way to read wav-files using Python [summary]?

后端 未结 8 689
夕颜
夕颜 2020-12-09 13:13

I want to use Python to access a wav-file and write its content in a form which allows me to analyze it (let\'s say arrays).

  1. I heard that \"audiolab\" is a sui
8条回答
  •  情话喂你
    2020-12-09 13:35

    Have you tried the wave module? It has fewer dependencies:

    http://docs.python.org/library/wave.html

    def everyOther (v, offset=0):
       return [v[i] for i in range(offset, len(v), 2)]
    
    def wavLoad (fname):
       wav = wave.open (fname, "r")
       (nchannels, sampwidth, framerate, nframes, comptype, compname) = wav.getparams ()
       frames = wav.readframes (nframes * nchannels)
       out = struct.unpack_from ("%dh" % nframes * nchannels, frames)
    
       # Convert 2 channles to numpy arrays
       if nchannels == 2:
           left = array (list (everyOther (out, 0)))
           right = array (list  (everyOther (out, 1)))
       else:
           left = array (out)
           right = left
    

提交回复
热议问题