AudioSegment到Librosa [英] AudioSegment to Librosa
本文介绍了AudioSegment到Librosa的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
i,m使用 pydub
模块编写代码以从音频文件中获取数据,但是我想使用 librosa
模块执行相同的操作,如何转换我的代码,并使用 librosa
获得相同的结果这是我的代码:
i,m writing a code using pydub
module to get data from an audio file , but i want to do the same actions using librosa
module , how to convert my code and get the same result with librosa
here's my code :
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
推荐答案
您可以使用:
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中测试:
Test in 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)
这篇关于AudioSegment到Librosa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文