import os
import wave
import numpy as np
import pylab as plt
def load_wav(path):
"""
功能:读取文件夹下wav文件,进行2s剪切
输入:文件夹路径
输出:归一化后的数据
时间:2019年10月12日
版本:V1.0
"""
files = []
for f in os.listdir(path):
if not f.startswith('.') and f.endswith('.wav'):
files.append(f)
for f in files:
w = wave.open(path+"/"+f, "rb")
params = w.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]
nums=int(nframes / (framerate * 2)) + 1
str_data = w.readframes(nframes)
wave_data = np.fromstring(str_data, dtype=np.short)
for i in np.arange(nums):
print((i * 2) * framerate, (i * 2 + 2) * framerate)
str_data1 = wave_data[(i * 2) * framerate:(i * 2 + 2) * framerate]
w.close()
f1 = wave.open(r"D:/work/音频剪切原始数据/" +str(i+1)+"-"+ f, "wb")
f1.setnchannels(nchannels)
f1.setsampwidth(sampwidth)
f1.setframerate(framerate)
f1.writeframes(str_data1.tostring())
return 0
if __name__ == '__main__' :
load_wav("D:/work/音频原始数据")