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

后端 未结 8 696
夕颜
夕颜 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: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")
    
    
    >>> # 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

提交回复
热议问题