Vicente Antonio G. Reyes
Vicente Antonio G. Reyes


Vicente Antonio G. Reyes

Recover background audio with librosa and saving it with soundfile

Recover background audio with librosa and saving it with soundfile

Vicente Antonio G. Reyes's photo
Vicente Antonio G. Reyes
Β·Feb 20, 2023Β·

1 min read

Play this article

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

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)

That's it.

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

Share this