在python中读取wav毫秒

时间:2014-05-24 13:48:03

标签: python audio wave

我需要从wav文件剪切小块(音素),大约0.1秒(例如0.3698125 - 0.466125

我正在使用wave模块,但它无法处理它: - /有人知道如何处理它吗?

此脚本应打开文件,剪切并将其添加到新文件

data = fonemy[fonem][0] = start, end, path ([0.3698125, 0.466125, u'hds/data/speech_16kHz/utt001.wav'])

frames = ""
text   = "hello"     
for fonem in text:
    data = fonemy[fonem][0]
    win = wave.open(data[2], 'rb')
    wout = wave.open('segment.wav', 'wb')
    wout.setparams(win.getparams())
    t0 = data[0]
    t1= data[1]
    s0, s1= int(t0*win.getframerate()), int(t1*win.getframerate())
    win.readframes(s0) # discard
    frames = frames + win.readframes(s1-s0)

wout.writeframes(frames)

1 个答案:

答案 0 :(得分:0)

目前还不清楚“你无法处理它”是什么意思,但这里的一个问题是你试图用float读取帧,但你只能读取整数帧:

>>> w = wave.open("/Users/bgporter/Documents/Juce Demo Audio Recording.wav", "rb")
>>> rate = w.getframerate()
>>> frames = 0.3698125 * rate
>>> w.readframes(frames)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wave.py", line 253, in readframes
    data = self._data_chunk.read(nframes * self._framesize)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/chunk.py", line 134, in read
    data = self.file.read(size)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/chunk.py", line 134, in read
    data = self.file.read(size)
TypeError: integer argument expected, got float

读取部分音频样本没有意义。弄清楚你是想要一个或多一个帧数的数据,并根据需要四舍五入到下一个更高或更低的帧数:

>>> data = w.readframes(int(frames))
>>> len(data)
32616