In [5]:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
import librosa
import librosa.display
import IPython.display
In [7]:
file_path = "../data/songData/genres/blues/blues.00000.wav"
x_1, sr = librosa.load(file_path)
plt.figure(figsize=(16, 4))
librosa.display.waveplot(x_1, sr=sr)
plt.title('original version')
plt.tight_layout()
IPython.display.Audio(data=x_1, rate=sr)
Out[7]:
In [8]:
# tempo change to fast
x_2 = librosa.effects.time_stretch(x_1, 1.2)
plt.figure(figsize=(16, 4))
librosa.display.waveplot(x_2, sr=fs)
plt.title('fast tempo version')
plt.tight_layout()
IPython.display.Audio(data=x_2, rate=sr)
Out[8]:
In [12]:
n_fft = 4410
hop_size = 2205
x1_chroma = librosa.feature.chroma_stft(y=x_1, sr=sr, tuning=0, norm=2,
hop_length=hop_size, n_fft=n_fft)
x2_chroma = librosa.feature.chroma_stft(y=x_2, sr=sr, tuning=0, norm=2,
hop_length=hop_size, n_fft=n_fft)
plt.figure(figsize=(12, 6))
plt.subplot(2, 1, 1)
librosa.display.specshow(x1_chroma, x_axis='time', y_axis='chroma',
cmap='gray_r', hop_length=hop_size)
plt.title('Chroma Representation of $X_1$')
plt.colorbar()
plt.subplot(2, 1, 2)
librosa.display.specshow(x2_chroma, x_axis='time', y_axis='chroma',
cmap='gray_r', hop_length=hop_size)
plt.title('Chroma Representation of $X_2$')
plt.colorbar()
plt.tight_layout()
In [15]:
D, wp = librosa.core.dtw(X=x1_chroma, Y=x2_chroma, metric='cosine')
wp_s = np.asarray(wp) * hop_size / sr
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111)
librosa.display.specshow(D, x_axis='time', y_axis='time',
cmap='gray_r', hop_length=hop_size)
imax = ax.imshow(D, cmap=plt.get_cmap('gray_r'),
origin='rower', interpolation='nearest', aspect='auto')
ax.plot(wp_s[:, 1], wp_s[:, 0], marker='o', color='r')
plt.title('Warping path on Acc. Const Matrix $D$')
plt.colorbar()
Out[15]: