相关文章推荐
英勇无比的铁链  ·  “Yiwu ...·  1 年前    · 

scipy.io.wavfile.read, soundfile.read, librosa.load三种读取音频文件的方式的区别

import scipy.io.wavfile as wavfile
import soundfile as sf
import librosa
import matplotlib.pyplot as plt
filename = 'bluesky.wav'
# wavfile.read读取 , 只有这一种未进行归一化,且fs和data的返回顺序相反
fs_wavfile, data_wavfile = wavfile.read(filename)
plt.subplot(311)
plt.plot(data_wavfile)
plt.title('wavfile')
print(data_wavfile.shape)
print(type(data_wavfile))
print(type(data_wavfile[0]))
print('----------------')
# soundfile.read 读取
data_soundfile, fs_soundfile = sf.read(filename)
plt.subplot(312)
plt.plot(data_soundfile)
plt.title('soundfile')
print(data_soundfile.shape)
print(type(data_soundfile))
print(type(data_soundfile[0]))
print('----------------')
# librosa.load 读取
# 如果 sr 缺省,librosa.load()会默认以22050的采样率读取音频文件,高于该采样率的音频文件会被下采样,低于该采样率的文件会被上采样。
# 因此,如果希望以原始采样率读取音频文件,sr 应当设为 None。具体做法为 y, sr = librosa(filename, sr=None)。
data_librosa, fs_librosa = librosa.load(filename, sr = None)
plt.subplot(313)
plt.plot(data_librosa)
plt.title('librosa')
print(data_librosa.shape)
print(type(data_librosa))
print(type(data_librosa[0]))
print('----------------')

运行结果:

(32000,)
<class 'numpy.ndarray'>
<class 'numpy.int16'>
----------------
(32000,)
<class 'numpy.ndarray'>
<class 'numpy.float64'>
----------------
(32000,)
<class 'numpy.ndarray'>
<class 'numpy.float32'>
----------------
  • wavfile不会自动归一化,且两个返回值是先返回采样率fs,再返回声音数据data
  • librosa.load读取时,默认采样率为22050,高于该采样率的音频文件会被下采样,低于该采样率的文件会被上采样。因此,如果希望以原始采样率读取音频文件,sr 应当设为 None。具体做法为 :y, sr = librosa.load(filename, sr=None)。
  • soundfile.read 返回值为声音波形数据data和采样率fs
  •