如何在Python 3中比较和可视化两个.wav文件?

nhaq1z21  于 2023-01-14  发布在  Python
关注(0)|答案(3)|浏览(185)

我在Jupyter上使用Python 3,我试图找出一个音频文件(有一些噪音)的准确性,原来的没有。请找到代码,我在网上找到,下面,

import librosa
import matplotlib.pyplot as plt
from dtw import dtw

#Loading audio files
y1, sr1 = librosa.load('data/dev1_female3_liverec_130ms_1m_sim_1.wav') 
y2, sr2 = librosa.load('data/dev1_female3_liverec_130ms_1m_sim_1o.wav') 

#Showing multiple plots using subplot
plt.subplot(1, 2, 1) 
mfcc1 = librosa.feature.mfcc(y1,sr1)   #Computing MFCC values
librosa.display.specshow(mfcc1)

plt.subplot(1, 2, 2)
mfcc2 = librosa.feature.mfcc(y2, sr2)
librosa.display.specshow(mfcc2)

dist, cost, path = dtw(mfcc1.T, mfcc2.T)
print("The normalized distance between the two : ",dist)   # 0 for similar audios 

plt.imshow(cost.T, origin='lower', cmap=plt.get_cmap('gray'), interpolation='nearest')
plt.plot(path[0], path[1], 'w')   #creating plot for DTW

plt.show()

我收到错误“未找到模块名称librosa”

sbtkgmzw

sbtkgmzw1#

首先你必须安装librosa,如果你使用的是anaconda发行版的python,那么在anaconda提示符下运行pip install librosa或者运行你的CMD。
其次jupyter上的import librosa,然后开始工作。

ibrsph3r

ibrsph3r2#

安装librosa lib后,它在librosa.display上给予问题,因此请将librosa.display导入为dis并使用。另外,它将在dtw(mfcc1.T,mfcc2.T)行上出现问题。有关此问题,请参阅以下内容

  1. https://github.com/pierre-rouanet/dtw/issues/18 2)https://github.com/pierre-rouanet/dtw/pull/19
2o7dmzc5

2o7dmzc53#

.....故障排除

  1. Python环境检查
    请检查python环境是否为项目代码对应的python环境
20230112 11:06:30 ~ $ python3

Python 3.10.6 (main, Aug 11 2022, 13:36:31) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

\>>> import sys

\>>> print(sys.executable)

/opt/homebrew/opt/python@3.10/bin/python3.10

\>>> quit()
20230112 11:06:47 ~ $
  1. Package 检查
    使用命令"pip list"
    如果你没有看到librosa,用"pip install librosa"命令安装它。
    • 2023年1月12日备注:**
    • 当前的librosa(版本:0.9.2),仅支持python版本大于等于3.7且小于3.11**

... Bug我运行您的代码并得到一个错误:
TypeError:无法解包不可迭代的DTW对象
.....更多错误消息:
(venv_比较_两个_音频)20230111 17:57:56比较_两个_音频$/用户/xxx/选项/迷你视频3/环境/venv_比较_两个_音频/bin/python3.10/用户/xxx/迷你应用程序/比较_两个_音频/比较_两个_音频. py
/用户/xxx/迷你应用程序/比较两个音频/比较两个音频. py:14:未来警告:将y =[0.0.0...0.0.0.],sr = 22050作为关键字参数传递。从版本0.10开始,将这些参数作为位置参数传递将导致错误

mfcc1 = librosa.feature.mfcc(y1,sr1)   #Computing MFCC values

/用户/xxx/迷你应用程序/比较两个音频/比较两个音频. py:18:未来警告:将y =[0.0.0...0.0.0.],sr = 22050作为关键字参数传递。从版本0.10开始,将这些参数作为位置参数传递将导致错误

mfcc2 = librosa.feature.mfcc(y2, sr2)

追溯(最近调用最后调用):
文件"/用户/xxx/迷你应用程序/比较两个音频/比较两个音频. py ",第21行,位于

dist, cost, path = dtw(mfcc1.T, mfcc2.T)

TypeError:无法解包不可迭代的DTW对象
(venv比较两个音频)20230111 17:58:02比较两个音频$
.....代码:

import librosa
import librosa.display
import matplotlib.pyplot as plt
from dtw import dtw
import os
import pathlib

#Loading audio files
y1, sr1 = librosa.load(os.path.join(pathlib.Path(__file__).resolve().parent, "audios", "chunk0.wav"))    # 'data/dev1_female3_liverec_130ms_1m_sim_1.wav'
y2, sr2 = librosa.load(os.path.join(pathlib.Path(__file__).resolve().parent, "audios", "chunk1.wav"))    # 'data/dev1_female3_liverec_130ms_1m_sim_1o.wav'

#Showing multiple plots using subplot
plt.subplot(1, 2, 1) 
mfcc1 = librosa.feature.mfcc(y1,sr1)   #Computing MFCC values
librosa.display.specshow(mfcc1)

plt.subplot(1, 2, 2)
mfcc2 = librosa.feature.mfcc(y2, sr2)
librosa.display.specshow(mfcc2)

dist, cost, path = dtw(mfcc1.T, mfcc2.T)
print("The normalized distance between the two : ",dist)   # 0 for similar audios 

plt.imshow(cost.T, origin='lower', cmap=plt.get_cmap('gray'), interpolation='nearest')
plt.plot(path[0], path[1], 'w')   #creating plot for DTW

plt.show()

..... Python环境

miniconda3 $ conda create -c conda-forge -n venv_compare_two_audios python=3.10
miniconda3 $ conda activate venv_compare_two_audios
miniconda3 $ conda install -c conda-forge librosa
miniconda3 $ conda install -c conda-forge dtw-python

相关问题