DEV Community

Vicente G. Reyes
Vicente G. Reyes

Posted on

Recover background audio with librosa and saving it with soundfile

In my previous article I separated the vocals from the instruments but didn't save it. This article is aimed towards saving the audio from IPython.Audio in IPython. The full spectrum, foreground spectrum and background spectrum from my previous article looked like this: Audio spectrum of the track

The other goal I tied myself to was to play the audio from the instruments only which I'll also show in this article.

To save the audio, we use soundfile's write() function and get the audio data, sample rate and save it as .wav

import soundfile as sf

sf.write('Instruments.wav', x_background, sr, subtype='PCM_24')
y, sr = librosa.load('Instruments.wav')
ipd.Audio('Instruments.wav')
Enter fullscreen mode Exit fullscreen mode

In recovering the background audio or instrument audio from the masked spectrum, we only use the istft() function from librosa.

# Recover the background audio from the masked spectrogram
x_background = librosa.istft(S_background * phase)
# playback audio
ipd.Audio(data=x_background[90*sr:110*sr], rate=sr)
Enter fullscreen mode Exit fullscreen mode

That's it.

To view code from the Jupyter notebook you can head over to this article

Top comments (0)