1.引入包

import numpy as np
import wave
import math
import glob
import matplotlib.pyplot as pl

2.读取.wav声音文件

#根据声音路径,得到录音特征值
def get_wav_mfcc(wave_path):
    f = wave.open(wave_path,'rb')
    params = f.getparams()
    nchannels, sampwidth, framerate, nframes = params[:4]
    strData = f.readframes(nframes)#读取音频,字符串格式
    waveData = np.fromstring(strData,dtype=np.int16)#将字符串转化为int
    waveData = waveData*1.0/(max(abs(waveData)))#wave幅值归一化
    waveData = np.reshape(waveData,[nframes,nchannels]).T
    f.close()
    return waveData
    

3.绘制声音图像

waveData=get_wav_mfcc("train\\1-137-A-32.wav")
time = np.arange(0,500)
pl.plot(time, waveData[0:500]) 
pl.xlabel("时间") 
pl.show()