How do I compare and visualise two .wav files in Python 3?

4k views Asked by At

I am using Python 3 on Jupyter and I am trying to find out the accuracy of an audio file (has some noise) with the original one which has none. Please find the code, that I found online, below,

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()

I am getting an error "No module names librosa is found"

3

There are 3 answers

0
Mayur Bujade On

After installing librosa lib. its give issue on librosa.display so please import librosa.display as dis and use. Also it will give issue on line dtw(mfcc1.T, mfcc2.T) For this please below refer

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

..... Error troubleshooting

1.Python environment check

Please check whether the python environment is the python environment corresponding to the project code

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/[email protected]/bin/python3.10

\>>> quit()
20230112 11:06:47 ~ $ 

2.Package check

Use the command "pip list"

If you don't see librosa, install it with the command "pip install librosa".

Note on 2023-01-12:

The current librosa (version: 0.9.2), only python versions >=3.7,<3.11 are supported


..... Bug I run your code and get an error:

TypeError: cannot unpack non-iterable DTW object

..... More error messages:

(venv_compare_two_audios) 20230111 17:57:56 compare_two_audios $ /Users/xxx/opt/miniconda3/envs/venv_compare_two_audios/bin/python3 .10 /Users/xxx/miniapps/compare_two_audios/compare_two_audios.py

/Users/xxx/miniapps/compare_two_audios/compare_two_audios.py:14: FutureWarning: Pass y=[0. 0. 0. ... 0. 0. 0.], sr=22050 as keyword args. From version 0.10 passing these as positional arguments will result in an error

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

/Users/xxx/miniapps/compare_two_audios/compare_two_audios.py:18: FutureWarning: Pass y=[0. 0. 0. ... 0. 0. 0.], sr=22050 as keyword args. From version 0.10 passing these as positional arguments will result in an error

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

Traceback (most recent call last):

File "/Users/xxx/miniapps/compare_two_audios/compare_two_audios.py", line 21, in

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

TypeError: cannot unpack non-iterable DTW object

(venv_compare_two_audios) 20230111 17:58:02 compare_two_audios $

..... code:

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 environment

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
0
Reshma Walhekar On

First you have to install librosa , if you are using anaconda distribution of python then in anaconda prompt run pip install librosa or else run it your CMD .

Secondly import librosa on jupyter and then start working .