赞
踩
- import wave
- import numpy as np
-
- f = wave.open(wav_path)
- params = f.getparams()
- nchannels, sampwidth, framerate, nframes = params[:4]
- # 声道数; 量化位数(byte); 采样频率; 采样点数
- str_audio = f.readframes(nframes)
- f.close()
-
- audio = np.frombuffer(str_data, dtype=np.short)
查看sampwidth这一参数,量化位数(sampwidth)就是记录声波每次采样所需的位数,一般有16,24,32位:
sampwidth(byte) | dtype |
2byte=16bit | int16 or i2 (整型short) |
3byte=24bit | 需要wavio包读取 |
4byte=32bit | int32 or i4 (长整型long) |
可以使用wavio包,直接读取成numpy数组
- import wavio
-
- audio = wavio.read(wav_path)
附上wavio的github链接:
https://github.com/WarrenWeckesser/wavio/blob/master/wavio.py
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。