读取wav的不同Python模块:
至少有以下这些库可以读取Wave音频文件:
最简单的例子:
这是SoundFile的一个简单示例:
import soundfile as sf
data, samplerate = sf.read('existing_file.wav')
输出格式:
警告,数据并不总是采用相同的格式,这取决于库。例如:
from scikits import audiolab
from scipy.io import wavfile
from sys import argv
for filepath in argv[1:]:
x, fs, nb_bits = audiolab.wavread(filepath)
print('Reading with scikits.audiolab.wavread:', x)
fs, x = wavfile.read(filepath)
print('Reading with scipy.io.wavfile.read:', x)
输出:
Reading with scikits.audiolab.wavread: [ 0. 0. 0. ..., -0.00097656 -0.00079346 -0.00097656]
Reading with scipy.io.wavfile.read: [ 0 0 0 ..., -32 -26 -32]
SoundFile和Audiolab返回的浮点数介于-1和1之间(就像matab一样,这是音频信号的约定)。Scipy和wave返回整数,您可以根据编码的位数将其转换为浮点数,例如:
from scipy.io.wavfile import read as wavread
samplerate, x = wavread(audiofilename)
if x.dtype == 'int16':
nb_bits = 16
elif x.dtype == 'int32':
nb_bits = 32
max_nb_bit = float(2 ** (nb_bits - 1))
samples = x / (max_nb_bit + 1)