Skip to main content

Recover background audio with librosa and saving it with soundfile

Updated 1 min readMarkdown
Part of seriesMusic with code
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')

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

127 views

About the author

  • Python Developer | Audio Editor | Technical Writer | OSS Contributor | Tag Moderator @ @ThePracticalDEV | Valorant TonyPoppins #881488

No comments yet. Be the first to comment.

More from this blog