处理音频文件的python模块
作者:互联网
首先是wave
def read_wav_data(filename):''' 读取一个wav文件,返回声音信号的时域谱矩阵和播放时间 ''' filename="/home/chenyang/"+filename[2:]with wave.open(filename,"rb") as wav: # 打开一个wav格式的声音文件流 num_frame = wav.getnframes() # 获取帧数 num_channel=wav.getnchannels() # 获取声道数 framerate=wav.getframerate() # 获取帧速率 num_sample_width=wav.getsampwidth() # 获取实例的比特宽度,即每一帧的字节数 str_data = wav.readframes(num_frame) # 读取全部的帧 wav.close() # 关闭流 wave_data = np.fromstring(str_data, dtype = np.short) # 将声音文件数据转换为数组矩阵形式 wave_data.shape = -1, num_channel # 按照声道数将数组整形,单声道时候是一列数组,双声道时候是两列的矩阵 wave_data = wave_data.T # 将矩阵转置#wave_data = wave_data return wave_data, framerate
接着就是
from python_speech_features import mfccfrom python_speech_features import delta
def GetMfccFeature(wavsignal, fs):# 获取输入特征 feat_mfcc=mfcc(wavsignal[0],fs) feat_mfcc_d=delta(feat_mfcc,2) feat_mfcc_dd=delta(feat_mfcc_d,2)# 返回值分别是mfcc特征向量的矩阵及其一阶差分和二阶差分矩阵 wav_feature = np.column_stack((feat_mfcc, feat_mfcc_d, feat_mfcc_dd))return wav_feature
接着就是
from scipy.io import wavfile as wav
fs, audio = wav.read(file)
这个是语谱图函数
def GetFrequencyFeature(wavsignal, fs):''' '''if(16000 != fs):raise ValueError('[Error] ASRT currently only supports wav audio files with a sampling rate of 16000 Hz, but this audio is ' + str(fs) + ' Hz. ')# wav波形 加时间窗以及时移10ms time_window = 25 # 单位ms window_length = fs / 1000 * time_window # 计算窗长度的公式,目前全部为400固定值 wav_arr = np.array(wavsignal)#wav_length = len(wavsignal[0]) wav_length = wav_arr.shape[1] range0_end = int(len(wavsignal[0])/fs*1000 - time_window) // 10 + 1 # 计算循环终止的位置,也就是最终生成的窗数 data_input = np.zeros((range0_end, window_length // 2), dtype = np.float) # 用于存放最终的频率特征数据 data_line = np.zeros((1, window_length), dtype = np.float)for i in range(0, range0_end): p_start = i * 160 p_end = p_start + 400 data_line = wav_arr[0, p_start:p_end] data_line = data_line * w # 加窗 data_line = np.abs(fft(data_line)) / wav_length data_input[i]=data_line[0: window_length // 2] # 设置为400除以2的值(即200)是取一半数据,因为是对称的#print(data_input.shape) data_input = np.log(data_input + 1)return data_input
还用
torchaudio
https://pytorch.org/audio/
aukit
标签:mfcc,python,feat,音频文件,模块,np,wav,wave,data 来源: https://blog.51cto.com/u_15177056/2725318