Convert 3-byte stereo WAV-file to numpy array

后端 未结 3 1500
小蘑菇
小蘑菇 2021-01-13 04:29

I have been given a large WAV-file of continuous underwater recording which I would like to convert to a numpy array for analysis. I am struggling to do this.

So far

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-13 05:20

    Here's a loop that handles 2, 3, and 4 byte WAV files with arbitrary numbers of channels:

    def dataFromWave(fname):
    """ return list with interleaved samples """
        f = wave.open(fname, 'rb')
        chans = f.getnchannels()
        samps = f.getnframes()
        sampwidth = f.getsampwidth()
        if  sampwidth == 3: #have to read this one sample at a time
            s = ''
            for k in xrange(samps):
                fr = f.readframes(1)
                for c in xrange(0,3*chans,3):                
                    s += '\0'+fr[c:(c+3)] # put TRAILING 0 to make 32-bit (file is little-endian)
        else:
            s = f.readframes(samps)
        f.close()
        unpstr = '<{0}{1}'.format(samps*chans, {1:'b',2:'h',3:'i',4:'i',8:'q'}[sampwidth])
        x = list(struct.unpack(unpstr, s))
        if sampwidth == 3:
            x = [k >> 8 for k in x] #downshift to get +/- 2^24 with sign extension
        return x
    

提交回复
热议问题