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

后端 未结 8 661
夕颜
夕颜 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
    
    0 讨论(0)
  • 2020-12-09 13:36

    I wrote a simple wrapper over the wave module in the std lib. it's called pydub and it has a method for reading samples from the audio data as ints.

    >>> from pydub import AudioSegment
    >>> song = AudioSegment.from_wav("your_song.wav")
    <pydub.audio_segment.AudioSegment at 0x1068868d0>
    
    >>> # This song is stereo
    >>> song.channels
    2
    
    >>> # get the 5000th "frame" in the song
    >>> frame = song.get_frame(5000)
    
    >>> sample_left, sample_right = frame[:2], frame[2:]
    >>> def sample_to_int(sample): 
            return int(sample.encode("hex"), 16)
    
    >>> sample_to_int(sample_left)
    8448
    
    >>> sample_to_int(sample_right)
    9984
    

    Hopefully this helps

    0 讨论(0)
  • 2020-12-09 13:38

    audiolab is the best way, but it doesn't work in every environment and the developer's not working on it. I'm still using Python 2.5 so I can use it.

    Did you install libsndfile?

    0 讨论(0)
  • 2020-12-09 13:39

    After trying so many things that does not work I used the decode library from Use (Python) Gstreamer to decode audio (to PCM data) and build a function to parse the raw pcm data into a scipy array.

    It's nice and can open any audio file that gstreamer can open: http://gist.github.com/592776 (see Test and the end of the file for usage info)

    0 讨论(0)
  • 2020-12-09 13:41

    You can also use the wave module along with the numpy.fromstring() function to convert it to an array

    import wave
    import numpy
    
    fp = wave.open('test.wav')
    nchan = fp.getnchannels()
    N = fp.getnframes()
    dstr = fp.readframes(N*nchan)
    data = numpy.fromstring(dstr, numpy.int16)
    data = numpy.reshape(data, (-1,nchan))
    
    0 讨论(0)
  • 2020-12-09 13:44

    audiolab seems to be not maintained anymore, you should try PySoundFile.

    Installation is simple:

    pip install PySoundFile --user
    

    And reading a sound file as well:

    import soundfile as sf
    x, fs = sf.read('/usr/share/sounds/purple/receive.wav')
    

    Have a look at this overview about different Python libraries for handling sound files.

    0 讨论(0)
提交回复
热议问题