Read the data of a single channel from a stereo wave file in Python

此生再无相见时 提交于 2019-12-03 08:32:00

scipy.io.wavfile.read returns the tuple (rate, data). If the file is stereo, data is a numpy array with shape (nsamples, 2). To get a specific channel, use a slice of data. For example,

rate, data = wavfile.read(path)
# data0 is the data from channel 0.
data0 = data[:, 0]
mtrw

The wave module returns the frames as a string of bytes, which can be converted to numbers with the struct module. For instance:

def oneChannel(fname, chanIdx):
""" list with specified channel's data from multichannel wave with 16-bit data """
    f = wave.open(fname, 'rb')
    chans = f.getnchannels()
    samps = f.getnframes()
    sampwidth = f.getsampwidth()
    assert sampwidth == 2
    s = f.readframes(samps) #read the all the samples from the file into a byte string
    f.close()
    unpstr = '<{0}h'.format(samps*chans) #little-endian 16-bit samples
    x = list(struct.unpack(unpstr, s)) #convert the byte string into a list of ints
    return x[chanIdx::chans] #return the desired channel

If your WAV file has some other sample size, you can use the (uglier) function in another answer I wrote here.

I've never used scipy's wavfile function so I can't compare speed, but the wave and struct approach I use here has always worked for me.

rate, audio = wavfile.read(path)

audio = np.mean(audio, axis=1)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!