我正在使用pydub模块编写代码来从音频文件中获取数据,但我想使用librosa模块执行相同的操作,如何转换我的代码并获得与librosa相同的结果这是我的代码:
import numpy as np
from pydub import AudioSegment
audiofile = AudioSegment.from_file(filename)
data = np.fromstring(audiofile._data,np.int16)
channels = []
for c in range(audiofile.channels):
channels.append(data[c::audiofile.channels])
fs = audiofile.frame_rate
return channels, fs 发布于 2020-08-02 01:17:27
您可以使用:
from pydub import AudioSegment
sound = AudioSegment.from_file("file.wav")
samples = sound.get_array_of_samples()
arr = np.array(samples).astype(np.float32)/32768 # 16 bit
arr = librosa.core.resample(arr, sound.frame_rate, 22050, res_type='kaiser_best')
print(arr)输出:
array([-0.0065596 , -0.00243502, 0.00489785, ..., -0.04385557,
-0.04421588, -0.05063475], dtype=float32)在librosa中测试:
y, sr = librosa.load('file.wav', sr=22050)
print(y)输出:
array([-0.0065596 , -0.00243502, 0.00489785, ..., -0.04385557,
-0.04421588, -0.05063475], dtype=float32)https://stackoverflow.com/questions/62916406
复制相似问题