%matplotlib inline
import seaborn
import numpy, scipy, matplotlib.pyplot as plt, IPython.display as ipd
import librosa, librosa.display
plt.rcParams['figure.figsize'] = (14, 5)
Load two files: one harmonic, and one percussive.
xh, sr_h = librosa.load('audio/prelude_cmaj.wav', duration=7, sr=None)
ipd.Audio(xh, rate=sr)
xp, sr_p = librosa.load('audio/125_bounce.wav', duration=7, sr=None)
ipd.Audio(xp, rate=sr)
print len(xh), len(xp)
print sr_h, sr_p
Add the two signals together, and rescale:
x = xh/xh.max() + xp/xp.max()
x = 0.5 * x/x.max()
x.max()
Listen to the combined audio signal:
ipd.Audio(x, rate=sr_h)
Compute the STFT:
X = librosa.stft(x)
Take the log-ampllitude for display purposes:
Xmag = librosa.amplitude_to_db(X)
Display the log-magnitude spectrogram:
librosa.display.specshow(Xmag, sr=sr_h, x_axis='time', y_axis='log')
plt.colorbar()
Perform harmonic-percussive source separation:
H, P = librosa.decompose.hpss(X)
Compute the log-amplitudes of the outputs:
Hmag = librosa.amplitude_to_db(H)
Pmag = librosa.amplitude_to_db(P)
Display each output:
librosa.display.specshow(Hmag, sr=sr_h, x_axis='time', y_axis='log')
plt.colorbar()
librosa.display.specshow(Pmag, sr=sr_p, x_axis='time', y_axis='log')
plt.colorbar()
Transform the harmonic output back to the time domain:
h = librosa.istft(H)
Listen to the harmonic output:
ipd.Audio(h, rate=sr_h)
Transform the percussive output back to the time domain:
p = librosa.istft(P)
Listen to the percussive output:
ipd.Audio(p, rate=sr_p)